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