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