paul@93 | 1 | /* |
paul@93 | 2 | * A pipe pager providing access to pipe content and navigation support. |
paul@93 | 3 | * |
paul@612 | 4 | * Copyright (C) 2021, 2022, 2023, 2024 Paul Boddie <paul@boddie.org.uk> |
paul@93 | 5 | * |
paul@93 | 6 | * This program is free software; you can redistribute it and/or |
paul@93 | 7 | * modify it under the terms of the GNU General Public License as |
paul@93 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@93 | 9 | * the License, or (at your option) any later version. |
paul@93 | 10 | * |
paul@93 | 11 | * This program is distributed in the hope that it will be useful, |
paul@93 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@93 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@93 | 14 | * GNU General Public License for more details. |
paul@93 | 15 | * |
paul@93 | 16 | * You should have received a copy of the GNU General Public License |
paul@93 | 17 | * along with this program; if not, write to the Free Software |
paul@93 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@93 | 19 | * Boston, MA 02110-1301, USA |
paul@93 | 20 | */ |
paul@93 | 21 | |
paul@65 | 22 | #include "pipe_pager.h" |
paul@65 | 23 | #include "pipe_object_server.h" |
paul@65 | 24 | |
paul@72 | 25 | |
paul@72 | 26 | |
paul@65 | 27 | /* Initialise a pager for a pipe with a shared page mapper for moderating |
paul@65 | 28 | access to loaded pages. At first, no mapper will be configured: this must be |
paul@65 | 29 | done by requesting a region. */ |
paul@65 | 30 | |
paul@66 | 31 | PipePager::PipePager(PipePaging *paging, bool writing) |
paul@87 | 32 | : Pager(NULL, writing ? L4RE_DS_MAP_FLAG_RW : L4RE_DS_MAP_FLAG_RO), |
paul@66 | 33 | _paging(paging), _writing(writing) |
paul@65 | 34 | { |
paul@66 | 35 | /* Initialise the size of the paged region. */ |
paul@65 | 36 | |
paul@66 | 37 | _size = _paging->region_size(); |
paul@166 | 38 | |
paul@665 | 39 | /* Initialise any recorded position in the region. */ |
paul@665 | 40 | |
paul@665 | 41 | _data_current = 0; |
paul@665 | 42 | |
paul@166 | 43 | /* Obtain any initial page mapper, this having been set up in the paging |
paul@166 | 44 | coordinator. */ |
paul@166 | 45 | |
paul@166 | 46 | _mapper = _paging->current_region(_writing); |
paul@65 | 47 | } |
paul@65 | 48 | |
paul@459 | 49 | ipc_server_default_config_type PipePager::config() |
paul@65 | 50 | { |
paul@459 | 51 | return config_PipeObject; |
paul@65 | 52 | } |
paul@65 | 53 | |
paul@65 | 54 | |
paul@65 | 55 | |
paul@127 | 56 | /* Close the pager, detaching from the paging coordinator for the pipe. When |
paul@127 | 57 | both pagers have detached, this will release all active page mappers and |
paul@127 | 58 | related resources. */ |
paul@67 | 59 | |
paul@67 | 60 | void PipePager::close() |
paul@67 | 61 | { |
paul@199 | 62 | if (_paging == NULL) |
paul@199 | 63 | return; |
paul@199 | 64 | |
paul@386 | 65 | /* Notify the other endpoint and unsubscribe. */ |
paul@117 | 66 | |
paul@484 | 67 | _paging->notify_others(_writing ? PipePaging::WRITER : PipePaging::READER, |
paul@484 | 68 | NOTIFY_PEER_CLOSED, NOTIFY_VALUES_NULL); |
paul@548 | 69 | unsubscribe(); |
paul@199 | 70 | |
paul@199 | 71 | /* Deallocate the paging coordinator if no other endpoints are active. */ |
paul@199 | 72 | |
paul@544 | 73 | if (!_paging->detach()) |
paul@199 | 74 | { |
paul@199 | 75 | delete _paging; |
paul@199 | 76 | _paging = NULL; |
paul@199 | 77 | } |
paul@67 | 78 | } |
paul@67 | 79 | |
paul@65 | 80 | /* Support paging. */ |
paul@65 | 81 | |
paul@410 | 82 | long PipePager::map(offset_t offset, map_address_t hot_spot, map_flags_t flags, l4_snd_fpage_t *region) |
paul@65 | 83 | { |
paul@65 | 84 | return Pager::map(offset, hot_spot, flags, region); |
paul@65 | 85 | } |
paul@65 | 86 | |
paul@65 | 87 | |
paul@65 | 88 | |
paul@117 | 89 | /* Return whether the pipe is closed or partly closed. */ |
paul@117 | 90 | |
paul@117 | 91 | long PipePager::closed(int *closed) |
paul@117 | 92 | { |
paul@117 | 93 | *closed = _paging->closed(); |
paul@117 | 94 | return L4_EOK; |
paul@117 | 95 | } |
paul@117 | 96 | |
paul@65 | 97 | /* Return details of the current region. */ |
paul@65 | 98 | |
paul@665 | 99 | long PipePager::current_region(offset_t *position, offset_t *populated_size, offset_t *size) |
paul@65 | 100 | { |
paul@65 | 101 | if (_mapper != NULL) |
paul@65 | 102 | { |
paul@665 | 103 | if (position != NULL) |
paul@665 | 104 | *position = _data_current; |
paul@665 | 105 | |
paul@65 | 106 | *populated_size = _mapper->get_data_size(); |
paul@65 | 107 | *size = _size; |
paul@65 | 108 | return L4_EOK; |
paul@65 | 109 | } |
paul@65 | 110 | else |
paul@114 | 111 | return pipe_error(); |
paul@65 | 112 | } |
paul@65 | 113 | |
paul@65 | 114 | /* Obtain the next region and its details. */ |
paul@65 | 115 | |
paul@65 | 116 | long PipePager::next_region(offset_t *populated_size, offset_t *size) |
paul@65 | 117 | { |
paul@65 | 118 | /* Obtain a new region if writing. */ |
paul@65 | 119 | |
paul@66 | 120 | if (_writing) |
paul@65 | 121 | return next_region_for_writer(populated_size, size); |
paul@65 | 122 | else |
paul@65 | 123 | return next_region_for_reader(populated_size, size); |
paul@65 | 124 | } |
paul@65 | 125 | |
paul@65 | 126 | long PipePager::next_region_for_reader(offset_t *populated_size, offset_t *size) |
paul@65 | 127 | { |
paul@72 | 128 | PageMapper *mapper = _paging->next_region(); |
paul@65 | 129 | |
paul@72 | 130 | if (mapper == NULL) |
paul@114 | 131 | return pipe_error(); |
paul@65 | 132 | |
paul@72 | 133 | _mapper = mapper; |
paul@665 | 134 | _data_current = 0; |
paul@65 | 135 | |
paul@665 | 136 | return current_region(NULL, populated_size, size); |
paul@65 | 137 | } |
paul@65 | 138 | |
paul@65 | 139 | long PipePager::next_region_for_writer(offset_t *populated_size, offset_t *size) |
paul@65 | 140 | { |
paul@65 | 141 | /* Set the populated size before moving on. */ |
paul@65 | 142 | |
paul@65 | 143 | if (_mapper != NULL) |
paul@65 | 144 | _mapper->set_data_size(*populated_size); |
paul@65 | 145 | |
paul@72 | 146 | /* Obtain the page mapper for the region. */ |
paul@66 | 147 | |
paul@72 | 148 | PageMapper *mapper = _paging->add_region(); |
paul@65 | 149 | |
paul@72 | 150 | if (mapper == NULL) |
paul@114 | 151 | return pipe_error(); |
paul@65 | 152 | |
paul@72 | 153 | _mapper = mapper; |
paul@665 | 154 | _data_current = 0; |
paul@65 | 155 | |
paul@665 | 156 | return current_region(NULL, populated_size, size); |
paul@65 | 157 | } |
paul@65 | 158 | |
paul@114 | 159 | long PipePager::pipe_error() |
paul@114 | 160 | { |
paul@114 | 161 | if (_paging->closed()) |
paul@114 | 162 | return -L4_EIO; |
paul@114 | 163 | else |
paul@114 | 164 | return -L4_EBUSY; |
paul@114 | 165 | } |
paul@114 | 166 | |
paul@117 | 167 | |
paul@117 | 168 | |
paul@665 | 169 | /* Update the consumed or populated size of a pipe region and notify the other |
paul@665 | 170 | endpoint. The consumed size is recorded to allow pipes to be shared between |
paul@665 | 171 | programs. */ |
paul@117 | 172 | |
paul@665 | 173 | long PipePager::flush(offset_t position, offset_t *size) |
paul@117 | 174 | { |
paul@665 | 175 | if (_mapper != NULL) |
paul@665 | 176 | { |
paul@665 | 177 | _data_current = position; |
paul@665 | 178 | |
paul@665 | 179 | if (_writing) |
paul@665 | 180 | _mapper->set_data_size(position); |
paul@665 | 181 | } |
paul@117 | 182 | |
paul@117 | 183 | *size = _size; |
paul@117 | 184 | |
paul@122 | 185 | // NOTE: Perhaps employ a distinct event type for metadata updates. |
paul@122 | 186 | |
paul@137 | 187 | _paging->notify_others(_writing ? PipePaging::WRITER : PipePaging::READER, |
paul@484 | 188 | NOTIFY_CONTENT_AVAILABLE | NOTIFY_SPACE_AVAILABLE, |
paul@484 | 189 | NOTIFY_VALUES_NULL); |
paul@117 | 190 | return L4_EOK; |
paul@117 | 191 | } |
paul@117 | 192 | |
paul@117 | 193 | |
paul@117 | 194 | |
paul@488 | 195 | /* Subscribe to notifications. |
paul@488 | 196 | Readers can subscribe to new data (at end), and pipe closed events. |
paul@488 | 197 | Writers can subscribe to new space and pipe closed events. */ |
paul@117 | 198 | |
paul@290 | 199 | long PipePager::subscribe(l4_cap_idx_t notifier, notify_flags_t flags) |
paul@117 | 200 | { |
paul@488 | 201 | /* A single notifier is recorded so that it may be unsubscribed when the |
paul@548 | 202 | endpoint is closed. */ |
paul@488 | 203 | |
paul@548 | 204 | unsubscribe(); |
paul@117 | 205 | |
paul@485 | 206 | _notifier = notifier; |
paul@288 | 207 | return _paging->subscribe(_writing ? PipePaging::WRITER : PipePaging::READER, |
paul@548 | 208 | _notifier, flags); |
paul@290 | 209 | } |
paul@290 | 210 | |
paul@290 | 211 | /* Unsubscribe from notifications. */ |
paul@290 | 212 | |
paul@548 | 213 | long PipePager::unsubscribe() |
paul@290 | 214 | { |
paul@548 | 215 | if (l4_is_valid_cap(_notifier)) |
paul@548 | 216 | { |
paul@548 | 217 | long err = _paging->unsubscribe(_notifier); |
paul@548 | 218 | _notifier = L4_INVALID_CAP; |
paul@548 | 219 | return err; |
paul@548 | 220 | } |
paul@548 | 221 | else |
paul@548 | 222 | return L4_EOK; |
paul@117 | 223 | } |
paul@117 | 224 | |
paul@65 | 225 | // vim: tabstop=4 expandtab shiftwidth=4 |