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