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 *populated_size, offset_t *size) 101 { 102 if (_mapper != NULL) 103 { 104 if (position != NULL) 105 *position = _data_current; 106 107 *populated_size = _mapper->get_data_size(); 108 *size = _size; 109 return L4_EOK; 110 } 111 else 112 return pipe_error(); 113 } 114 115 /* Obtain the next region and its details. */ 116 117 long PipePager::next_region(offset_t *populated_size, offset_t *size) 118 { 119 /* Obtain a new region if writing. */ 120 121 if (_writing) 122 return next_region_for_writer(populated_size, size); 123 else 124 return next_region_for_reader(populated_size, size); 125 } 126 127 long PipePager::next_region_for_reader(offset_t *populated_size, offset_t *size) 128 { 129 PageMapper *mapper = _paging->next_region(); 130 131 if (mapper == NULL) 132 return pipe_error(); 133 134 _mapper = mapper; 135 _data_current = 0; 136 137 return refresh(NULL, populated_size, size); 138 } 139 140 long PipePager::next_region_for_writer(offset_t *populated_size, offset_t *size) 141 { 142 /* Set the populated size before moving on. */ 143 144 if (_mapper != NULL) 145 _mapper->set_data_size(*populated_size); 146 147 /* Obtain the page mapper for the region. */ 148 149 PageMapper *mapper = _paging->add_region(); 150 151 if (mapper == NULL) 152 return pipe_error(); 153 154 _mapper = mapper; 155 _data_current = 0; 156 157 return refresh(NULL, populated_size, size); 158 } 159 160 long PipePager::pipe_error() 161 { 162 if (_paging->closed()) 163 return -L4_EIO; 164 else 165 return -L4_EBUSY; 166 } 167 168 169 170 /* Update the consumed or populated size of a pipe region and notify the other 171 endpoint. The consumed size is recorded to allow pipes to be shared between 172 programs. */ 173 174 long PipePager::flush(offset_t position, offset_t *size) 175 { 176 if (_mapper != NULL) 177 { 178 _data_current = position; 179 180 if (_writing) 181 _mapper->set_data_size(position); 182 } 183 184 *size = _size; 185 186 // NOTE: Perhaps employ a distinct event type for metadata updates. 187 188 _paging->notify_others(_writing ? PipePaging::WRITER : PipePaging::READER, 189 NOTIFY_CONTENT_AVAILABLE | NOTIFY_SPACE_AVAILABLE, 190 NOTIFY_VALUES_NULL); 191 return L4_EOK; 192 } 193 194 195 196 /* Subscribe to notifications. 197 Readers can subscribe to new data (at end), and pipe closed events. 198 Writers can subscribe to new space and pipe closed events. */ 199 200 long PipePager::subscribe(l4_cap_idx_t notifier, notify_flags_t flags) 201 { 202 /* A single notifier is recorded so that it may be unsubscribed when the 203 endpoint is closed. */ 204 205 unsubscribe(); 206 207 _notifier = notifier; 208 return _paging->subscribe(_writing ? PipePaging::WRITER : PipePaging::READER, 209 _notifier, flags); 210 } 211 212 /* Unsubscribe from notifications. */ 213 214 long PipePager::unsubscribe() 215 { 216 if (l4_is_valid_cap(_notifier)) 217 { 218 long err = _paging->unsubscribe(_notifier); 219 _notifier = L4_INVALID_CAP; 220 return err; 221 } 222 else 223 return L4_EOK; 224 } 225 226 // vim: tabstop=4 expandtab shiftwidth=4