1 /* 2 * A pipe pager providing access to pipe content and navigation support. 3 * 4 * Copyright (C) 2021, 2022 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 int PipePager::expected_items() 46 { 47 return PipeObject_expected_items; 48 } 49 50 ipc_server_handler_type PipePager::handler() 51 { 52 return (ipc_server_handler_type) handle_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 unsigned int attached = _paging->detach(); 67 68 /* Notify the other endpoint and unsubscribe. */ 69 70 _paging->notify_others(_writing ? PipePaging::WRITER : PipePaging::READER, NOTIFY_PEER_CLOSED); 71 unsubscribe(); 72 73 /* Deallocate the paging coordinator if no other endpoints are active. */ 74 75 if (!attached) 76 { 77 delete _paging; 78 _paging = NULL; 79 } 80 } 81 82 /* Support paging. */ 83 84 long PipePager::map(offset_t offset, address_t hot_spot, map_flags_t flags, l4_snd_fpage_t *region) 85 { 86 return Pager::map(offset, hot_spot, flags, region); 87 } 88 89 90 91 /* Return whether the pipe is closed or partly closed. */ 92 93 long PipePager::closed(int *closed) 94 { 95 *closed = _paging->closed(); 96 return L4_EOK; 97 } 98 99 /* Return details of the current region. */ 100 101 long PipePager::current_region(offset_t *populated_size, offset_t *size) 102 { 103 if (_mapper != NULL) 104 { 105 *populated_size = _mapper->get_data_size(); 106 *size = _size; 107 return L4_EOK; 108 } 109 else 110 return pipe_error(); 111 } 112 113 /* Obtain the next region and its details. */ 114 115 long PipePager::next_region(offset_t *populated_size, offset_t *size) 116 { 117 /* Obtain a new region if writing. */ 118 119 if (_writing) 120 return next_region_for_writer(populated_size, size); 121 else 122 return next_region_for_reader(populated_size, size); 123 } 124 125 long PipePager::next_region_for_reader(offset_t *populated_size, offset_t *size) 126 { 127 PageMapper *mapper = _paging->next_region(); 128 129 if (mapper == NULL) 130 return pipe_error(); 131 132 _mapper = mapper; 133 134 return current_region(populated_size, size); 135 } 136 137 long PipePager::next_region_for_writer(offset_t *populated_size, offset_t *size) 138 { 139 /* Set the populated size before moving on. */ 140 141 if (_mapper != NULL) 142 _mapper->set_data_size(*populated_size); 143 144 /* Obtain the page mapper for the region. */ 145 146 PageMapper *mapper = _paging->add_region(); 147 148 if (mapper == NULL) 149 return pipe_error(); 150 151 _mapper = mapper; 152 153 return current_region(populated_size, size); 154 } 155 156 long PipePager::pipe_error() 157 { 158 if (_paging->closed()) 159 return -L4_EIO; 160 else 161 return -L4_EBUSY; 162 } 163 164 165 166 /* Update the populated size of a pipe region and notify the other endpoint. */ 167 168 long PipePager::flush(offset_t populated_size, offset_t *size) 169 { 170 if (_mapper != NULL) 171 _mapper->set_data_size(populated_size); 172 173 *size = _size; 174 175 // NOTE: Perhaps employ a distinct event type for metadata updates. 176 177 _paging->notify_others(_writing ? PipePaging::WRITER : PipePaging::READER, 178 NOTIFY_CONTENT_AVAILABLE | NOTIFY_SPACE_AVAILABLE); 179 return L4_EOK; 180 } 181 182 183 184 /* Subscribe to notifications. */ 185 186 long PipePager::subscribe(l4_cap_idx_t notifier, notify_flags_t flags) 187 { 188 /* Readers can subscribe to new data (at end), and pipe closed events. 189 Writers can subscribe to new space and pipe closed events. */ 190 191 return _paging->subscribe(_writing ? PipePaging::WRITER : PipePaging::READER, 192 notifier, flags, this); 193 } 194 195 /* Unsubscribe from notifications. */ 196 197 long PipePager::unsubscribe() 198 { 199 _paging->unsubscribe(_writing ? PipePaging::WRITER : PipePaging::READER, 200 this); 201 return L4_EOK; 202 } 203 204 // vim: tabstop=4 expandtab shiftwidth=4