paul@93 | 1 | /* |
paul@93 | 2 | * Generic pager functionality. |
paul@93 | 3 | * |
paul@332 | 4 | * Copyright (C) 2021, 2022 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@342 | 26 | #include "copied_page_mapper.h" |
paul@342 | 27 | #include "masked_page_mapper.h" |
paul@332 | 28 | |
paul@6 | 29 | |
paul@6 | 30 | |
paul@86 | 31 | /* Initialise the pager with a page mapper and the given flags controlling |
paul@86 | 32 | access to a file. */ |
paul@86 | 33 | |
paul@346 | 34 | Pager::Pager(GenericPageMapper *mapper, map_flags_t map_flags) |
paul@342 | 35 | : _start(0), _size(0), |
paul@342 | 36 | _mapper(mapper), _mapper_base(mapper), _mapper_masked(NULL), |
paul@342 | 37 | _mapper_copied(NULL), |
paul@346 | 38 | _map_flags(map_flags) |
paul@6 | 39 | { |
paul@6 | 40 | } |
paul@6 | 41 | |
paul@79 | 42 | /* Close the pager. */ |
paul@79 | 43 | |
paul@6 | 44 | void Pager::close() |
paul@6 | 45 | { |
paul@342 | 46 | if (_mapper_masked != NULL) |
paul@342 | 47 | { |
paul@342 | 48 | delete _mapper_masked; |
paul@342 | 49 | _mapper_masked = NULL; |
paul@342 | 50 | } |
paul@342 | 51 | |
paul@342 | 52 | if (_mapper_copied != NULL) |
paul@342 | 53 | { |
paul@342 | 54 | delete _mapper_copied; |
paul@342 | 55 | _mapper_copied = NULL; |
paul@342 | 56 | } |
paul@6 | 57 | } |
paul@6 | 58 | |
paul@53 | 59 | /* Flush data to the file. */ |
paul@53 | 60 | |
paul@53 | 61 | long Pager::flush(offset_t populated_size, offset_t *size) |
paul@53 | 62 | { |
paul@55 | 63 | _mapper->flush_all(_start, populated_size); |
paul@53 | 64 | |
paul@55 | 65 | *size = _mapper->get_data_size(); |
paul@55 | 66 | return L4_EOK; |
paul@55 | 67 | } |
paul@53 | 68 | |
paul@55 | 69 | /* Resize the underlying file. */ |
paul@53 | 70 | |
paul@55 | 71 | long Pager::resize(offset_t *size) |
paul@55 | 72 | { |
paul@55 | 73 | _mapper->set_data_size(*size); |
paul@55 | 74 | |
paul@55 | 75 | *size = _mapper->get_data_size(); |
paul@53 | 76 | return L4_EOK; |
paul@53 | 77 | } |
paul@53 | 78 | |
paul@7 | 79 | /* Expose a region of the file. */ |
paul@7 | 80 | |
paul@339 | 81 | long Pager::mmap(offset_t position, offset_t length, |
paul@339 | 82 | offset_t start_visible, offset_t end_visible, |
paul@332 | 83 | offset_t *start_pos, offset_t *end_pos, offset_t *size) |
paul@6 | 84 | { |
paul@90 | 85 | /* Define region characteristics. */ |
paul@90 | 86 | |
paul@7 | 87 | _start = trunc(position, PAGE_SIZE); |
paul@7 | 88 | _size = round(position + length, PAGE_SIZE) - _start; |
paul@6 | 89 | |
paul@90 | 90 | /* Return the start and end positions plus populated extent. */ |
paul@90 | 91 | |
paul@7 | 92 | *start_pos = _start; |
paul@7 | 93 | *end_pos = _start + _size; |
paul@342 | 94 | *size = _mapper_base->get_data_size(); |
paul@7 | 95 | |
paul@332 | 96 | /* Permit masking of mapped regions. */ |
paul@332 | 97 | |
paul@342 | 98 | if ((start_visible || end_visible) && |
paul@342 | 99 | ((*start_pos != start_visible) || (*end_pos != end_visible))) |
paul@332 | 100 | { |
paul@342 | 101 | /* Introduce the masked page and copied page mappers. */ |
paul@342 | 102 | |
paul@342 | 103 | _mapper_masked = new MaskedPageMapper(_mapper_base, start_visible, end_visible); |
paul@342 | 104 | _mapper_copied = new CopiedPageMapper(_mapper_masked); |
paul@342 | 105 | _mapper = _mapper_copied; |
paul@332 | 106 | } |
paul@332 | 107 | else |
paul@342 | 108 | _mapper = _mapper_base; |
paul@332 | 109 | |
paul@7 | 110 | return L4_EOK; |
paul@6 | 111 | } |
paul@6 | 112 | |
paul@6 | 113 | /* Map a flexpage corresponding to the dataspace 'offset' involving a 'hot_spot' |
paul@6 | 114 | (flexpage offset). */ |
paul@6 | 115 | |
paul@346 | 116 | long Pager::map(offset_t offset, address_t hot_spot, map_flags_t map_flags, |
paul@90 | 117 | l4_snd_fpage_t *region) |
paul@6 | 118 | { |
paul@6 | 119 | offset_t file_offset = _start + offset; |
paul@6 | 120 | offset_t max_offset = _start + _size; |
paul@87 | 121 | |
paul@332 | 122 | /* Prevent access beyond that defined by the pager. |
paul@332 | 123 | NOTE: Permitting executable requests here. This needs to be configured |
paul@332 | 124 | when opening the pager or by another means. */ |
paul@87 | 125 | |
paul@346 | 126 | if (map_flags & (~(_map_flags | L4RE_DS_F_X))) |
paul@87 | 127 | return -L4_EACCESS; |
paul@87 | 128 | |
paul@342 | 129 | /* Obtain a flexpage from the page mapper. */ |
paul@6 | 130 | |
paul@346 | 131 | Flexpage *flexpage = _mapper->get(file_offset, map_flags); |
paul@332 | 132 | |
paul@6 | 133 | /* Issue the flexpage via the IPC system. */ |
paul@6 | 134 | |
paul@342 | 135 | long err = ipc_prepare_flexpage(flexpage, file_offset, max_offset, |
paul@346 | 136 | hot_spot, map_flags, region); |
paul@6 | 137 | |
paul@37 | 138 | if (!err) |
paul@87 | 139 | err = complete_Dataspace_map(*region); |
paul@6 | 140 | |
paul@342 | 141 | /* After the obtained flexpage is issued, it is queued for future reuse, if |
paul@342 | 142 | appropriate for the mapper concerned. */ |
paul@6 | 143 | |
paul@342 | 144 | _mapper->queue(flexpage); |
paul@6 | 145 | |
paul@37 | 146 | if (err) |
paul@87 | 147 | return err; |
paul@37 | 148 | |
paul@6 | 149 | return IPC_MESSAGE_SENT; |
paul@6 | 150 | } |
paul@6 | 151 | |
paul@6 | 152 | /* Return the total size of the data. */ |
paul@6 | 153 | |
paul@6 | 154 | offset_t Pager::get_data_size() |
paul@6 | 155 | { |
paul@6 | 156 | return _mapper->get_data_size(); |
paul@6 | 157 | } |
paul@6 | 158 | |
paul@6 | 159 | // vim: tabstop=4 expandtab shiftwidth=4 |