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