1 /* 2 * A pipe pager providing access to pipe content and navigation support. 3 * 4 * Copyright (C) 2021, 2022, 2023, 2024 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 "pipe_pager.h" 23 #include "pipe_object_server.h" 24 25 26 27 /* Initialise a pager for a pipe with a shared page mapper for moderating 28 access to loaded pages. At first, no mapper will be configured: this must be 29 done by requesting a region. */ 30 31 PipePager::PipePager(PipePaging *paging, bool writing) 32 : Pager(NULL, writing ? L4RE_DS_MAP_FLAG_RW : L4RE_DS_MAP_FLAG_RO), 33 _paging(paging), _writing(writing) 34 { 35 /* Initialise the size of the paged region. */ 36 37 _size = _paging->region_size(); 38 39 /* Initialise any recorded position in the region. */ 40 41 _data_current = 0; 42 43 /* Obtain any initial page mapper, this having been set up in the paging 44 coordinator. */ 45 46 _mapper = _paging->current_region(_writing); 47 } 48 49 ipc_server_default_config_type PipePager::config() 50 { 51 return config_PipeObject; 52 } 53 54 55 56 /* Close the pager, detaching from the paging coordinator for the pipe. When 57 both pagers have detached, this will release all active page mappers and 58 related resources. */ 59 60 void PipePager::close() 61 { 62 if (_paging == NULL) 63 return; 64 65 /* Notify the other endpoint and unsubscribe. */ 66 67 _paging->notify_others(_writing ? PipePaging::WRITER : PipePaging::READER, 68 NOTIFY_PEER_CLOSED, NOTIFY_VALUES_NULL); 69 unsubscribe(); 70 71 /* Deallocate the paging coordinator if no other endpoints are active. */ 72 73 if (!_paging->detach()) 74 { 75 delete _paging; 76 _paging = NULL; 77 } 78 } 79 80 /* Support paging. */ 81 82 long PipePager::map(offset_t offset, map_address_t hot_spot, map_flags_t flags, l4_snd_fpage_t *region) 83 { 84 return Pager::map(offset, hot_spot, flags, region); 85 } 86 87 88 89 /* Return whether the pipe is closed or partly closed. */ 90 91 long PipePager::closed(int *closed) 92 { 93 *closed = _paging->closed(); 94 return L4_EOK; 95 } 96 97 /* Return details of the current region. */ 98 99 long PipePager::current_region(offset_t *position, offset_t *populated_size, offset_t *size) 100 { 101 if (_mapper != NULL) 102 { 103 if (position != NULL) 104 *position = _data_current; 105 106 *populated_size = _mapper->get_data_size(); 107 *size = _size; 108 return L4_EOK; 109 } 110 else 111 return pipe_error(); 112 } 113 114 /* Obtain the next region and its details. */ 115 116 long PipePager::next_region(offset_t *populated_size, offset_t *size) 117 { 118 /* Obtain a new region if writing. */ 119 120 if (_writing) 121 return next_region_for_writer(populated_size, size); 122 else 123 return next_region_for_reader(populated_size, size); 124 } 125 126 long PipePager::next_region_for_reader(offset_t *populated_size, offset_t *size) 127 { 128 PageMapper *mapper = _paging->next_region(); 129 130 if (mapper == NULL) 131 return pipe_error(); 132 133 _mapper = mapper; 134 _data_current = 0; 135 136 return current_region(NULL, populated_size, size); 137 } 138 139 long PipePager::next_region_for_writer(offset_t *populated_size, offset_t *size) 140 { 141 /* Set the populated size before moving on. */ 142 143 if (_mapper != NULL) 144 _mapper->set_data_size(*populated_size); 145 146 /* Obtain the page mapper for the region. */ 147 148 PageMapper *mapper = _paging->add_region(); 149 150 if (mapper == NULL) 151 return pipe_error(); 152 153 _mapper = mapper; 154 _data_current = 0; 155 156 return current_region(NULL, populated_size, size); 157 } 158 159 long PipePager::pipe_error() 160 { 161 if (_paging->closed()) 162 return -L4_EIO; 163 else 164 return -L4_EBUSY; 165 } 166 167 168 169 /* Update the consumed or populated size of a pipe region and notify the other 170 endpoint. The consumed size is recorded to allow pipes to be shared between 171 programs. */ 172 173 long PipePager::flush(offset_t position, offset_t *size) 174 { 175 if (_mapper != NULL) 176 { 177 _data_current = position; 178 179 if (_writing) 180 _mapper->set_data_size(position); 181 } 182 183 *size = _size; 184 185 // NOTE: Perhaps employ a distinct event type for metadata updates. 186 187 _paging->notify_others(_writing ? PipePaging::WRITER : PipePaging::READER, 188 NOTIFY_CONTENT_AVAILABLE | NOTIFY_SPACE_AVAILABLE, 189 NOTIFY_VALUES_NULL); 190 return L4_EOK; 191 } 192 193 194 195 /* Subscribe to notifications. 196 Readers can subscribe to new data (at end), and pipe closed events. 197 Writers can subscribe to new space and pipe closed events. */ 198 199 long PipePager::subscribe(l4_cap_idx_t notifier, notify_flags_t flags) 200 { 201 /* A single notifier is recorded so that it may be unsubscribed when the 202 endpoint is closed. */ 203 204 unsubscribe(); 205 206 _notifier = notifier; 207 return _paging->subscribe(_writing ? PipePaging::WRITER : PipePaging::READER, 208 _notifier, flags); 209 } 210 211 /* Unsubscribe from notifications. */ 212 213 long PipePager::unsubscribe() 214 { 215 if (l4_is_valid_cap(_notifier)) 216 { 217 long err = _paging->unsubscribe(_notifier); 218 _notifier = L4_INVALID_CAP; 219 return err; 220 } 221 else 222 return L4_EOK; 223 } 224 225 // vim: tabstop=4 expandtab shiftwidth=4