paul@93 | 1 | /* |
paul@93 | 2 | * A flexpage abstraction. |
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@115 | 22 | #include <l4/re/c/dataspace.h> |
paul@115 | 23 | |
paul@0 | 24 | #include <algorithm> |
paul@0 | 25 | |
paul@0 | 26 | #include "flexpage.h" |
paul@0 | 27 | |
paul@0 | 28 | |
paul@0 | 29 | |
paul@0 | 30 | /* Reset the flexpage using 'offset', being the file offset. */ |
paul@0 | 31 | |
paul@0 | 32 | void Flexpage::reset(offset_t offset) |
paul@0 | 33 | { |
paul@332 | 34 | if (region == NULL) |
paul@332 | 35 | return; |
paul@332 | 36 | |
paul@0 | 37 | _counter = 0; |
paul@0 | 38 | |
paul@0 | 39 | /* By definition (see "Flexible-Sized Page Objects - Object-Orientation |
paul@0 | 40 | in Operation Systems"), flexpages are aligned to multiples of their |
paul@0 | 41 | size. |
paul@0 | 42 | |
paul@0 | 43 | The size of the flexpage depends on the amount of space around the |
paul@0 | 44 | accessed page. It cannot exceed the size of the memory region. */ |
paul@0 | 45 | |
paul@0 | 46 | size = max_multiple(region->start, region->end, PAGE_SIZE); |
paul@0 | 47 | |
paul@0 | 48 | /* The base address of the flexpage is computed from the region start |
paul@0 | 49 | and flexpage size. It will be no lower than the region start. |
paul@0 | 50 | |
paul@0 | 51 | Sent flexpages may use higher bases due to receive window constraints, |
paul@0 | 52 | these being communicated by the "hot spot". */ |
paul@0 | 53 | |
paul@0 | 54 | base_addr = round(region->start, size); |
paul@0 | 55 | |
paul@0 | 56 | /* Get the file offset for the base of the flexpage. This will be a |
paul@0 | 57 | multiple of the flexpage size for alignment purposes. */ |
paul@0 | 58 | |
paul@0 | 59 | base_offset = trunc(offset, size); |
paul@0 | 60 | |
paul@0 | 61 | /* The page being accessed is relative to the base. |
paul@0 | 62 | (This is transient information recording the initialising access |
paul@0 | 63 | details.) */ |
paul@0 | 64 | |
paul@0 | 65 | page_offset = trunc(offset - base_offset, PAGE_SIZE); |
paul@0 | 66 | page_addr = base_addr + page_offset; |
paul@0 | 67 | } |
paul@0 | 68 | |
paul@332 | 69 | /* Set a region. */ |
paul@332 | 70 | |
paul@332 | 71 | void Flexpage::set_region(Region *region) |
paul@332 | 72 | { |
paul@332 | 73 | this->region = region; |
paul@332 | 74 | } |
paul@332 | 75 | |
paul@110 | 76 | /* Decrement the usage counter, returning whether the flexpage is now no longer |
paul@110 | 77 | used. */ |
paul@110 | 78 | |
paul@0 | 79 | bool Flexpage::decrement() |
paul@0 | 80 | { |
paul@0 | 81 | if (_counter) |
paul@0 | 82 | { |
paul@0 | 83 | _counter--; |
paul@0 | 84 | return _counter == 0; |
paul@0 | 85 | } |
paul@0 | 86 | else |
paul@0 | 87 | return 0; |
paul@0 | 88 | } |
paul@0 | 89 | |
paul@110 | 90 | /* Increment the usage counter. */ |
paul@110 | 91 | |
paul@0 | 92 | void Flexpage::increment() |
paul@0 | 93 | { |
paul@0 | 94 | _counter++; |
paul@0 | 95 | } |
paul@0 | 96 | |
paul@110 | 97 | /* Invalidate the flexpage, meaning that it should not now be in use. */ |
paul@110 | 98 | |
paul@0 | 99 | void Flexpage::invalidate() |
paul@0 | 100 | { |
paul@0 | 101 | _counter = 0; |
paul@0 | 102 | } |
paul@0 | 103 | |
paul@110 | 104 | /* Return whether the flexpage is in use and is therefore valid. */ |
paul@110 | 105 | |
paul@0 | 106 | bool Flexpage::valid() |
paul@0 | 107 | { |
paul@0 | 108 | return _counter != 0; |
paul@0 | 109 | } |
paul@0 | 110 | |
paul@0 | 111 | /* Return whether the flexpage supports the given file 'position'. */ |
paul@0 | 112 | |
paul@0 | 113 | bool Flexpage::supports_position(offset_t position) |
paul@0 | 114 | { |
paul@0 | 115 | return (base_offset <= position) && (position < (base_offset + size)); |
paul@0 | 116 | } |
paul@0 | 117 | |
paul@86 | 118 | /* Upgrade the flags involved with this flexpage. This is used to track the |
paul@86 | 119 | maximal flags employed by the different pagers, with the result being used in |
paul@86 | 120 | unmap operations. */ |
paul@86 | 121 | |
paul@121 | 122 | void Flexpage::upgrade(map_flags_t flags) |
paul@11 | 123 | { |
paul@11 | 124 | if (flags && (flags != _flags)) |
paul@11 | 125 | _flags |= flags; |
paul@11 | 126 | } |
paul@11 | 127 | |
paul@115 | 128 | /* Return whether the flexpage has been modified due to write access having been |
paul@115 | 129 | granted for any user of the page. */ |
paul@115 | 130 | |
paul@115 | 131 | bool Flexpage::modified() |
paul@115 | 132 | { |
paul@115 | 133 | return _flags & L4RE_DS_MAP_FLAG_RW; |
paul@115 | 134 | } |
paul@115 | 135 | |
paul@0 | 136 | /* Return a "send" flexpage for an access to 'offset' by positioning it relative |
paul@0 | 137 | to 'hot_spot' for the receive flexpage window. */ |
paul@0 | 138 | |
paul@86 | 139 | SendFlexpage Flexpage::to_send(offset_t offset, offset_t hot_spot, |
paul@121 | 140 | map_flags_t flags, offset_t max_offset) |
paul@0 | 141 | { |
paul@0 | 142 | /* The dataspace offset of the flexpage base is a multiple of the flexpage |
paul@0 | 143 | size. */ |
paul@0 | 144 | |
paul@0 | 145 | offset_t receive_base_offset = trunc(offset, size); |
paul@0 | 146 | |
paul@0 | 147 | /* The offset of the accessed page within the flexpage is constrained by the |
paul@0 | 148 | current flexpage size. */ |
paul@0 | 149 | |
paul@0 | 150 | offset_t page_offset = trunc(offset - receive_base_offset, PAGE_SIZE); |
paul@0 | 151 | |
paul@0 | 152 | /* The receive flexpage offset (hot spot) must be constrained to the |
paul@0 | 153 | flexpage, both the size and the start. */ |
paul@0 | 154 | |
paul@0 | 155 | offset_t receive_size; |
paul@0 | 156 | |
paul@0 | 157 | if (max_offset) |
paul@23 | 158 | { |
paul@23 | 159 | receive_size = trunc_multiple(max_offset - receive_base_offset, PAGE_SIZE); |
paul@23 | 160 | receive_size = std::min(size, receive_size); |
paul@23 | 161 | } |
paul@0 | 162 | else |
paul@0 | 163 | receive_size = size; |
paul@0 | 164 | |
paul@3 | 165 | if (!receive_size) |
paul@86 | 166 | return SendFlexpage(base_addr, page_order(0), flags); |
paul@3 | 167 | |
paul@0 | 168 | offset_t receive_page_offset = hot_spot % receive_size; |
paul@0 | 169 | |
paul@0 | 170 | while ((receive_size > PAGE_SIZE) && (receive_page_offset != page_offset)) |
paul@0 | 171 | { |
paul@0 | 172 | receive_size /= 2; |
paul@0 | 173 | receive_page_offset = hot_spot % receive_size; |
paul@0 | 174 | } |
paul@0 | 175 | |
paul@0 | 176 | /* The flexpage base address is adjusted using the difference in page |
paul@0 | 177 | offsets. Where the receive flexpage offset is constained further, the |
paul@0 | 178 | base address will be raised to become closer to the accessed page. */ |
paul@0 | 179 | |
paul@0 | 180 | offset_t adjustment = page_offset - receive_page_offset; |
paul@0 | 181 | |
paul@86 | 182 | return SendFlexpage(base_addr + adjustment, page_order(receive_size), flags); |
paul@0 | 183 | } |
paul@0 | 184 | |
paul@0 | 185 | /* Return a representation of the flexpage for unmapping. */ |
paul@0 | 186 | |
paul@0 | 187 | SendFlexpage Flexpage::to_unmap() |
paul@0 | 188 | { |
paul@11 | 189 | return SendFlexpage(base_addr, page_order(size), _flags); |
paul@0 | 190 | } |
paul@0 | 191 | |
paul@332 | 192 | /* Associate another flexpage with this flexpage. */ |
paul@332 | 193 | |
paul@332 | 194 | void Flexpage::associate(Flexpage *flexpage) |
paul@332 | 195 | { |
paul@332 | 196 | derived.push_back(flexpage); |
paul@332 | 197 | } |
paul@332 | 198 | |
paul@332 | 199 | void Flexpage::disassociate() |
paul@332 | 200 | { |
paul@332 | 201 | derived.clear(); |
paul@332 | 202 | } |
paul@332 | 203 | |
paul@0 | 204 | // vim: tabstop=4 expandtab shiftwidth=4 |