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