paul@93 | 1 | /* |
paul@93 | 2 | * A pipe paging coordinator, permitting memory sharing pipe endpoints. |
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@117 | 22 | #include <ipc/cap_alloc.h> |
paul@94 | 23 | #include <mem/memory_preallocated.h> |
paul@94 | 24 | |
paul@72 | 25 | #include "page_queue_partitioned.h" |
paul@65 | 26 | #include "pipe_paging.h" |
paul@65 | 27 | |
paul@124 | 28 | #include "notifier_client.h" |
paul@124 | 29 | |
paul@72 | 30 | |
paul@72 | 31 | |
paul@70 | 32 | PipePaging::PipePaging(Memory *memory, offset_t size) |
paul@70 | 33 | : _memory(NULL), _size(size) |
paul@70 | 34 | { |
paul@72 | 35 | /* Reserve space for two pipe regions. */ |
paul@72 | 36 | |
paul@89 | 37 | _memory = new MemoryPreallocated(memory, round(size, memory->region_size()) * 2); |
paul@72 | 38 | _queue = new PageQueuePartitioned(); |
paul@72 | 39 | _pages = new Pages(_memory, _queue); |
paul@72 | 40 | |
paul@114 | 41 | /* Reset the mappers associated with the regions. */ |
paul@114 | 42 | |
paul@72 | 43 | for (unsigned int i = 0; i < 2; i++) |
paul@72 | 44 | _regions[i] = NULL; |
paul@117 | 45 | |
paul@124 | 46 | /* Initialise endpoints and flags for notifications. */ |
paul@117 | 47 | |
paul@117 | 48 | for (unsigned int i = 0; i < 2; i++) |
paul@122 | 49 | { |
paul@124 | 50 | _notifiers[i] = L4_INVALID_CAP; |
paul@122 | 51 | _flags[i] = 0; |
paul@124 | 52 | _deferred[i] = 0; |
paul@122 | 53 | } |
paul@117 | 54 | } |
paul@117 | 55 | |
paul@124 | 56 | /* Subscribe to an endpoint's notifications using a notification endpoint. */ |
paul@117 | 57 | |
paul@124 | 58 | void PipePaging::subscribe(bool writing, l4_cap_idx_t notifier, notify_flags_t flags) |
paul@117 | 59 | { |
paul@117 | 60 | int i = writing ? 1 : 0; |
paul@117 | 61 | |
paul@124 | 62 | if (l4_is_valid_cap(_notifiers[i])) |
paul@124 | 63 | unsubscribe(writing); |
paul@117 | 64 | |
paul@124 | 65 | _notifiers[i] = notifier; |
paul@122 | 66 | _flags[i] = flags; |
paul@122 | 67 | |
paul@124 | 68 | /* Send deferred conditions on behalf of the other endpoint held before |
paul@124 | 69 | subscription occurred. */ |
paul@124 | 70 | |
paul@124 | 71 | if (_deferred[i]) |
paul@124 | 72 | { |
paul@124 | 73 | notify(!writing, _deferred[i]); |
paul@124 | 74 | _deferred[i] = 0; |
paul@124 | 75 | } |
paul@117 | 76 | } |
paul@117 | 77 | |
paul@124 | 78 | /* Unsubscribe from an endpoint's notifications. */ |
paul@117 | 79 | |
paul@117 | 80 | void PipePaging::unsubscribe(bool writing) |
paul@117 | 81 | { |
paul@117 | 82 | int i = writing ? 1 : 0; |
paul@117 | 83 | |
paul@124 | 84 | if (l4_is_valid_cap(_notifiers[i])) |
paul@117 | 85 | { |
paul@124 | 86 | ipc_cap_free_um(_notifiers[i]); |
paul@124 | 87 | _notifiers[i] = L4_INVALID_CAP; |
paul@122 | 88 | _flags[i] = 0; |
paul@124 | 89 | _deferred[i] = 0; |
paul@117 | 90 | } |
paul@117 | 91 | } |
paul@117 | 92 | |
paul@117 | 93 | /* Notify the other endpoint. */ |
paul@117 | 94 | |
paul@122 | 95 | void PipePaging::notify(bool writing, notify_flags_t flags) |
paul@117 | 96 | { |
paul@117 | 97 | /* Let the writer notify the reader, and the other way round. */ |
paul@117 | 98 | |
paul@117 | 99 | int i = writing ? 0 : 1; |
paul@117 | 100 | |
paul@124 | 101 | /* Notify the other endpoint or hold any notification for potential future |
paul@124 | 102 | subscription. */ |
paul@124 | 103 | |
paul@124 | 104 | if (l4_is_valid_cap(_notifiers[i])) |
paul@124 | 105 | { |
paul@124 | 106 | if (flags & _flags[i]) |
paul@124 | 107 | { |
paul@124 | 108 | client_Notifier notifier(_notifiers[i]); |
paul@124 | 109 | |
paul@124 | 110 | notifier.notify(flags & _flags[i]); |
paul@124 | 111 | } |
paul@124 | 112 | } |
paul@124 | 113 | else |
paul@124 | 114 | _deferred[i] = flags; |
paul@70 | 115 | } |
paul@70 | 116 | |
paul@114 | 117 | /* Return whether one or more endpoints have detached. */ |
paul@114 | 118 | |
paul@114 | 119 | int PipePaging::closed() |
paul@114 | 120 | { |
paul@114 | 121 | return _endpoints < 2; |
paul@114 | 122 | } |
paul@114 | 123 | |
paul@118 | 124 | void PipePaging::discard_region(unsigned int i) |
paul@118 | 125 | { |
paul@118 | 126 | PageMapper *mapper = _regions[i]; |
paul@118 | 127 | |
paul@118 | 128 | if (mapper != NULL) |
paul@118 | 129 | { |
paul@118 | 130 | mapper->detach(); |
paul@118 | 131 | _regions[i] = NULL; |
paul@118 | 132 | delete mapper; |
paul@118 | 133 | } |
paul@118 | 134 | } |
paul@118 | 135 | |
paul@67 | 136 | /* Detach one endpoint. */ |
paul@67 | 137 | |
paul@67 | 138 | void PipePaging::detach() |
paul@67 | 139 | { |
paul@67 | 140 | if (!_endpoints) |
paul@67 | 141 | return; |
paul@67 | 142 | else |
paul@67 | 143 | _endpoints--; |
paul@67 | 144 | |
paul@69 | 145 | /* Return if the other endpoint is attached. */ |
paul@69 | 146 | |
paul@67 | 147 | if (_endpoints) |
paul@67 | 148 | return; |
paul@67 | 149 | |
paul@67 | 150 | /* Discard all regions from the pipe. */ |
paul@67 | 151 | |
paul@72 | 152 | for (unsigned int i = 0; i < 2; i++) |
paul@118 | 153 | discard_region(i); |
paul@70 | 154 | |
paul@124 | 155 | /* Release notifiers. */ |
paul@117 | 156 | |
paul@117 | 157 | for (unsigned int i = 0; i < 2; i++) |
paul@117 | 158 | { |
paul@124 | 159 | if (l4_is_valid_cap(_notifiers[i])) |
paul@117 | 160 | { |
paul@124 | 161 | ipc_cap_free_um(_notifiers[i]); |
paul@124 | 162 | _notifiers[i] = L4_INVALID_CAP; |
paul@117 | 163 | } |
paul@117 | 164 | } |
paul@117 | 165 | |
paul@72 | 166 | /* Delete the page collection and related objects. */ |
paul@70 | 167 | |
paul@70 | 168 | delete _pages; |
paul@72 | 169 | delete _queue; |
paul@72 | 170 | delete _memory; |
paul@67 | 171 | } |
paul@67 | 172 | |
paul@65 | 173 | /* Add a region to the sequence. */ |
paul@65 | 174 | |
paul@72 | 175 | PageMapper *PipePaging::add_region() |
paul@65 | 176 | { |
paul@72 | 177 | /* If the writer already accesses a different region to the reader, no new |
paul@72 | 178 | region is added. */ |
paul@72 | 179 | |
paul@72 | 180 | if (_writing != _reading) |
paul@72 | 181 | return NULL; |
paul@72 | 182 | |
paul@72 | 183 | /* Select the other region of the pair being maintained. */ |
paul@72 | 184 | |
paul@72 | 185 | _writing = 1 - _writing; |
paul@72 | 186 | |
paul@72 | 187 | /* Make a new mapper for the region. */ |
paul@72 | 188 | |
paul@72 | 189 | PageMapper *mapper = new PageMapper(&_accessors[_writing], _pages); |
paul@72 | 190 | |
paul@72 | 191 | /* Initialise and record the mapper. */ |
paul@72 | 192 | |
paul@72 | 193 | mapper->attach(); |
paul@72 | 194 | mapper->set_data_size(0); |
paul@72 | 195 | |
paul@72 | 196 | _regions[_writing] = mapper; |
paul@117 | 197 | |
paul@117 | 198 | /* Let the writer notify the reader. */ |
paul@117 | 199 | |
paul@122 | 200 | notify(true, NOTIFY_CONTENT_AVAILABLE); |
paul@117 | 201 | |
paul@117 | 202 | /* Return the next region's mapper. */ |
paul@117 | 203 | |
paul@72 | 204 | return mapper; |
paul@65 | 205 | } |
paul@65 | 206 | |
paul@72 | 207 | /* Return the current region for reading. */ |
paul@65 | 208 | |
paul@72 | 209 | PageMapper *PipePaging::current_region() |
paul@65 | 210 | { |
paul@72 | 211 | return _regions[_reading]; |
paul@65 | 212 | } |
paul@65 | 213 | |
paul@72 | 214 | /* Return the next region for the reader if the writer is using a different one. |
paul@72 | 215 | Otherwise, return NULL. */ |
paul@65 | 216 | |
paul@65 | 217 | PageMapper *PipePaging::next_region() |
paul@65 | 218 | { |
paul@72 | 219 | /* If the reader already accesses the same region to the writer, no next |
paul@72 | 220 | region can be obtained. */ |
paul@66 | 221 | |
paul@72 | 222 | if (_reading == _writing) |
paul@72 | 223 | return NULL; |
paul@67 | 224 | |
paul@72 | 225 | /* Detach and discard the current page mapper. */ |
paul@72 | 226 | |
paul@118 | 227 | discard_region(_reading); |
paul@65 | 228 | |
paul@114 | 229 | /* Select the next region. */ |
paul@66 | 230 | |
paul@72 | 231 | _reading = 1 - _reading; |
paul@114 | 232 | |
paul@117 | 233 | /* Let the reader notify the writer. */ |
paul@117 | 234 | |
paul@122 | 235 | notify(false, NOTIFY_SPACE_AVAILABLE); |
paul@117 | 236 | |
paul@114 | 237 | /* Return the next region's mapper. */ |
paul@114 | 238 | |
paul@72 | 239 | return _regions[_reading]; |
paul@66 | 240 | } |
paul@66 | 241 | |
paul@65 | 242 | // vim: tabstop=4 expandtab shiftwidth=4 |