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