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