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