paul@93 | 1 | /* |
paul@93 | 2 | * Generic pager functionality. |
paul@93 | 3 | * |
paul@93 | 4 | * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk> |
paul@93 | 5 | * |
paul@93 | 6 | * This program is free software; you can redistribute it and/or |
paul@93 | 7 | * modify it under the terms of the GNU General Public License as |
paul@93 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@93 | 9 | * the License, or (at your option) any later version. |
paul@93 | 10 | * |
paul@93 | 11 | * This program is distributed in the hope that it will be useful, |
paul@93 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@93 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@93 | 14 | * GNU General Public License for more details. |
paul@93 | 15 | * |
paul@93 | 16 | * You should have received a copy of the GNU General Public License |
paul@93 | 17 | * along with this program; if not, write to the Free Software |
paul@93 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@93 | 19 | * Boston, MA 02110-1301, USA |
paul@93 | 20 | */ |
paul@93 | 21 | |
paul@6 | 22 | #include "dataspace_server.h" |
paul@6 | 23 | #include "ipc.h" |
paul@6 | 24 | #include "pager.h" |
paul@6 | 25 | |
paul@6 | 26 | |
paul@6 | 27 | |
paul@86 | 28 | /* Initialise the pager with a page mapper and the given flags controlling |
paul@86 | 29 | access to a file. */ |
paul@86 | 30 | |
paul@54 | 31 | Pager::Pager(PageMapper *mapper, flags_t flags) |
paul@48 | 32 | : _start(0), _size(0), _mapper(mapper), _flags(flags) |
paul@6 | 33 | { |
paul@65 | 34 | /* Some pagers may not be initialised with a mapper. */ |
paul@65 | 35 | |
paul@65 | 36 | if (_mapper != NULL) |
paul@65 | 37 | _mapper->attach(); |
paul@6 | 38 | } |
paul@6 | 39 | |
paul@79 | 40 | /* Close the pager. */ |
paul@79 | 41 | |
paul@6 | 42 | void Pager::close() |
paul@6 | 43 | { |
paul@65 | 44 | if (_mapper != NULL) |
paul@65 | 45 | _mapper->detach(); |
paul@6 | 46 | } |
paul@6 | 47 | |
paul@53 | 48 | /* Flush data to the file. */ |
paul@53 | 49 | |
paul@53 | 50 | long Pager::flush(offset_t populated_size, offset_t *size) |
paul@53 | 51 | { |
paul@55 | 52 | _mapper->flush_all(_start, populated_size); |
paul@53 | 53 | |
paul@55 | 54 | *size = _mapper->get_data_size(); |
paul@55 | 55 | return L4_EOK; |
paul@55 | 56 | } |
paul@53 | 57 | |
paul@55 | 58 | /* Resize the underlying file. */ |
paul@53 | 59 | |
paul@55 | 60 | long Pager::resize(offset_t *size) |
paul@55 | 61 | { |
paul@55 | 62 | _mapper->set_data_size(*size); |
paul@55 | 63 | |
paul@55 | 64 | *size = _mapper->get_data_size(); |
paul@53 | 65 | return L4_EOK; |
paul@53 | 66 | } |
paul@53 | 67 | |
paul@7 | 68 | /* Expose a region of the file. */ |
paul@7 | 69 | |
paul@90 | 70 | long Pager::mmap(offset_t position, offset_t length, offset_t *start_pos, |
paul@90 | 71 | offset_t *end_pos, offset_t *size) |
paul@6 | 72 | { |
paul@90 | 73 | /* Define region characteristics. */ |
paul@90 | 74 | |
paul@7 | 75 | _start = trunc(position, PAGE_SIZE); |
paul@7 | 76 | _size = round(position + length, PAGE_SIZE) - _start; |
paul@6 | 77 | |
paul@90 | 78 | /* Return the start and end positions plus populated extent. */ |
paul@90 | 79 | |
paul@7 | 80 | *start_pos = _start; |
paul@7 | 81 | *end_pos = _start + _size; |
paul@90 | 82 | *size = _mapper->get_data_size(); |
paul@7 | 83 | |
paul@7 | 84 | return L4_EOK; |
paul@6 | 85 | } |
paul@6 | 86 | |
paul@6 | 87 | /* Map a flexpage corresponding to the dataspace 'offset' involving a 'hot_spot' |
paul@6 | 88 | (flexpage offset). */ |
paul@6 | 89 | |
paul@92 | 90 | long Pager::map(offset_t offset, address_t hot_spot, flags_t flags, |
paul@90 | 91 | l4_snd_fpage_t *region) |
paul@6 | 92 | { |
paul@6 | 93 | offset_t file_offset = _start + offset; |
paul@6 | 94 | offset_t max_offset = _start + _size; |
paul@87 | 95 | |
paul@87 | 96 | /* Prevent access beyond that defined by the pager. */ |
paul@87 | 97 | |
paul@87 | 98 | if (flags & ~_flags) |
paul@87 | 99 | return -L4_EACCESS; |
paul@87 | 100 | |
paul@11 | 101 | Flexpage *flexpage = _mapper->get(file_offset, flags); |
paul@6 | 102 | |
paul@6 | 103 | /* Issue the flexpage via the IPC system. */ |
paul@6 | 104 | |
paul@86 | 105 | long err = ipc_prepare_flexpage(flexpage, file_offset, max_offset, hot_spot, |
paul@86 | 106 | flags, region); |
paul@6 | 107 | |
paul@37 | 108 | if (!err) |
paul@87 | 109 | err = complete_Dataspace_map(*region); |
paul@6 | 110 | |
paul@6 | 111 | /* After the flexpage is issued, it is queued for future reuse. */ |
paul@6 | 112 | |
paul@6 | 113 | _mapper->queue(flexpage); |
paul@6 | 114 | |
paul@37 | 115 | if (err) |
paul@87 | 116 | return err; |
paul@37 | 117 | |
paul@6 | 118 | return IPC_MESSAGE_SENT; |
paul@6 | 119 | } |
paul@6 | 120 | |
paul@6 | 121 | /* Return the total size of the data. */ |
paul@6 | 122 | |
paul@6 | 123 | offset_t Pager::get_data_size() |
paul@6 | 124 | { |
paul@6 | 125 | return _mapper->get_data_size(); |
paul@6 | 126 | } |
paul@6 | 127 | |
paul@6 | 128 | // vim: tabstop=4 expandtab shiftwidth=4 |