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