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