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