1 /* 2 * A flexpage abstraction. 3 * 4 * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <l4/re/c/dataspace.h> 23 24 #include <algorithm> 25 26 #include "flexpage.h" 27 28 29 30 /* Reset the flexpage using 'offset', being the file offset. */ 31 32 void Flexpage::reset(offset_t offset) 33 { 34 _counter = 0; 35 _flags = 0; 36 37 /* By definition (see "Flexible-Sized Page Objects - Object-Orientation 38 in Operation Systems"), flexpages are aligned to multiples of their 39 size. 40 41 The size of the flexpage depends on the amount of space around the 42 accessed page. It cannot exceed the size of the memory region. */ 43 44 size = max_multiple(region->start, region->end, PAGE_SIZE); 45 46 /* The base address of the flexpage is computed from the region start 47 and flexpage size. It will be no lower than the region start. 48 49 Sent flexpages may use higher bases due to receive window constraints, 50 these being communicated by the "hot spot". */ 51 52 base_addr = round(region->start, size); 53 54 /* Get the file offset for the base of the flexpage. This will be a 55 multiple of the flexpage size for alignment purposes. */ 56 57 base_offset = trunc(offset, size); 58 59 /* The page being accessed is relative to the base. 60 (This is transient information recording the initialising access 61 details.) */ 62 63 page_offset = trunc(offset - base_offset, PAGE_SIZE); 64 page_addr = base_addr + page_offset; 65 } 66 67 /* Decrement the usage counter, returning whether the flexpage is now no longer 68 used. */ 69 70 bool Flexpage::decrement() 71 { 72 if (_counter) 73 { 74 _counter--; 75 return _counter == 0; 76 } 77 else 78 return 0; 79 } 80 81 /* Increment the usage counter. */ 82 83 void Flexpage::increment() 84 { 85 _counter++; 86 } 87 88 /* Invalidate the flexpage, meaning that it should not now be in use. */ 89 90 void Flexpage::invalidate() 91 { 92 _counter = 0; 93 } 94 95 /* Return whether the flexpage is in use and is therefore valid. */ 96 97 bool Flexpage::valid() 98 { 99 return _counter != 0; 100 } 101 102 /* Return whether the flexpage supports the given file 'position'. */ 103 104 bool Flexpage::supports_position(offset_t position) 105 { 106 return (base_offset <= position) && (position < (base_offset + size)); 107 } 108 109 /* Upgrade the flags involved with this flexpage. This is used to track the 110 maximal flags employed by the different pagers, with the result being used in 111 unmap operations. */ 112 113 void Flexpage::upgrade(map_flags_t flags) 114 { 115 if (flags && (flags != _flags)) 116 _flags |= flags; 117 } 118 119 /* Return whether the flexpage has been modified due to write access having been 120 granted for any user of the page. */ 121 122 bool Flexpage::modified() 123 { 124 return _flags & L4RE_DS_F_W; 125 } 126 127 /* Return a "send" flexpage for an access to 'offset' by positioning it relative 128 to 'hot_spot' for the receive flexpage window. */ 129 130 SendFlexpage Flexpage::to_send(offset_t offset, offset_t hot_spot, 131 map_flags_t flags, offset_t max_offset) 132 { 133 /* The dataspace offset of the flexpage base is a multiple of the flexpage 134 size. */ 135 136 offset_t receive_base_offset = trunc(offset, size); 137 138 /* The offset of the accessed page within the flexpage is constrained by the 139 current flexpage size. */ 140 141 offset_t page_offset = trunc(offset - receive_base_offset, PAGE_SIZE); 142 143 /* The receive flexpage offset (hot spot) must be constrained to the 144 flexpage, both the size and the start. */ 145 146 offset_t receive_size; 147 148 if (max_offset) 149 { 150 receive_size = trunc_multiple(max_offset - receive_base_offset, PAGE_SIZE); 151 receive_size = std::min(size, receive_size); 152 } 153 else 154 receive_size = size; 155 156 if (!receive_size) 157 return SendFlexpage(base_addr, page_order(0), flags); 158 159 offset_t receive_page_offset = hot_spot % receive_size; 160 161 while ((receive_size > PAGE_SIZE) && (receive_page_offset != page_offset)) 162 { 163 receive_size /= 2; 164 receive_page_offset = hot_spot % receive_size; 165 } 166 167 /* The flexpage base address is adjusted using the difference in page 168 offsets. Where the receive flexpage offset is constained further, the 169 base address will be raised to become closer to the accessed page. */ 170 171 offset_t adjustment = page_offset - receive_page_offset; 172 173 return SendFlexpage(base_addr + adjustment, page_order(receive_size), flags); 174 } 175 176 /* Return a representation of the flexpage for unmapping. */ 177 178 SendFlexpage Flexpage::to_unmap() 179 { 180 return SendFlexpage(base_addr, page_order(size), _flags); 181 } 182 183 // vim: tabstop=4 expandtab shiftwidth=4