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