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