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@673 | 39 | /* Initialise any recorded position in the mapped region for this |
paul@673 | 40 | endpoint. */ |
paul@665 | 41 | |
paul@665 | 42 | _data_current = 0; |
paul@665 | 43 | |
paul@166 | 44 | /* Obtain any initial page mapper, this having been set up in the paging |
paul@166 | 45 | coordinator. */ |
paul@166 | 46 | |
paul@166 | 47 | _mapper = _paging->current_region(_writing); |
paul@65 | 48 | } |
paul@65 | 49 | |
paul@459 | 50 | ipc_server_default_config_type PipePager::config() |
paul@65 | 51 | { |
paul@459 | 52 | return config_PipeObject; |
paul@65 | 53 | } |
paul@65 | 54 | |
paul@65 | 55 | |
paul@65 | 56 | |
paul@127 | 57 | /* Close the pager, detaching from the paging coordinator for the pipe. When |
paul@127 | 58 | both pagers have detached, this will release all active page mappers and |
paul@127 | 59 | related resources. */ |
paul@67 | 60 | |
paul@67 | 61 | void PipePager::close() |
paul@67 | 62 | { |
paul@199 | 63 | if (_paging == NULL) |
paul@199 | 64 | return; |
paul@199 | 65 | |
paul@386 | 66 | /* Notify the other endpoint and unsubscribe. */ |
paul@117 | 67 | |
paul@484 | 68 | _paging->notify_others(_writing ? PipePaging::WRITER : PipePaging::READER, |
paul@484 | 69 | NOTIFY_PEER_CLOSED, NOTIFY_VALUES_NULL); |
paul@548 | 70 | unsubscribe(); |
paul@199 | 71 | |
paul@199 | 72 | /* Deallocate the paging coordinator if no other endpoints are active. */ |
paul@199 | 73 | |
paul@544 | 74 | if (!_paging->detach()) |
paul@199 | 75 | { |
paul@199 | 76 | delete _paging; |
paul@199 | 77 | _paging = NULL; |
paul@199 | 78 | } |
paul@67 | 79 | } |
paul@67 | 80 | |
paul@65 | 81 | /* Support paging. */ |
paul@65 | 82 | |
paul@410 | 83 | long PipePager::map(offset_t offset, map_address_t hot_spot, map_flags_t flags, l4_snd_fpage_t *region) |
paul@65 | 84 | { |
paul@65 | 85 | return Pager::map(offset, hot_spot, flags, region); |
paul@65 | 86 | } |
paul@65 | 87 | |
paul@65 | 88 | |
paul@65 | 89 | |
paul@117 | 90 | /* Return whether the pipe is closed or partly closed. */ |
paul@117 | 91 | |
paul@117 | 92 | long PipePager::closed(int *closed) |
paul@117 | 93 | { |
paul@117 | 94 | *closed = _paging->closed(); |
paul@117 | 95 | return L4_EOK; |
paul@117 | 96 | } |
paul@117 | 97 | |
paul@65 | 98 | /* Return details of the current region. */ |
paul@65 | 99 | |
paul@675 | 100 | long PipePager::refresh(offset_t *position, offset_t *start_pos, |
paul@675 | 101 | offset_t *end_pos, offset_t *size, |
paul@675 | 102 | object_flags_t *object_flags) |
paul@65 | 103 | { |
paul@65 | 104 | if (_mapper != NULL) |
paul@65 | 105 | { |
paul@665 | 106 | if (position != NULL) |
paul@665 | 107 | *position = _data_current; |
paul@665 | 108 | |
paul@675 | 109 | if (start_pos != NULL) |
paul@675 | 110 | *start_pos = 0; |
paul@675 | 111 | |
paul@675 | 112 | *end_pos = _size; |
paul@675 | 113 | *size = _mapper->get_data_size(); |
paul@675 | 114 | |
paul@675 | 115 | if (object_flags != NULL) |
paul@675 | 116 | *object_flags = 0; |
paul@675 | 117 | |
paul@65 | 118 | return L4_EOK; |
paul@65 | 119 | } |
paul@65 | 120 | else |
paul@114 | 121 | return pipe_error(); |
paul@65 | 122 | } |
paul@65 | 123 | |
paul@65 | 124 | /* Obtain the next region and its details. */ |
paul@65 | 125 | |
paul@65 | 126 | long PipePager::next_region(offset_t *populated_size, offset_t *size) |
paul@65 | 127 | { |
paul@65 | 128 | /* Obtain a new region if writing. */ |
paul@65 | 129 | |
paul@66 | 130 | if (_writing) |
paul@65 | 131 | return next_region_for_writer(populated_size, size); |
paul@65 | 132 | else |
paul@65 | 133 | return next_region_for_reader(populated_size, size); |
paul@65 | 134 | } |
paul@65 | 135 | |
paul@65 | 136 | long PipePager::next_region_for_reader(offset_t *populated_size, offset_t *size) |
paul@65 | 137 | { |
paul@72 | 138 | PageMapper *mapper = _paging->next_region(); |
paul@65 | 139 | |
paul@72 | 140 | if (mapper == NULL) |
paul@114 | 141 | return pipe_error(); |
paul@65 | 142 | |
paul@72 | 143 | _mapper = mapper; |
paul@665 | 144 | _data_current = 0; |
paul@65 | 145 | |
paul@675 | 146 | return refresh(NULL, NULL, size, populated_size, NULL); |
paul@65 | 147 | } |
paul@65 | 148 | |
paul@65 | 149 | long PipePager::next_region_for_writer(offset_t *populated_size, offset_t *size) |
paul@65 | 150 | { |
paul@65 | 151 | /* Set the populated size before moving on. */ |
paul@65 | 152 | |
paul@65 | 153 | if (_mapper != NULL) |
paul@65 | 154 | _mapper->set_data_size(*populated_size); |
paul@65 | 155 | |
paul@72 | 156 | /* Obtain the page mapper for the region. */ |
paul@66 | 157 | |
paul@72 | 158 | PageMapper *mapper = _paging->add_region(); |
paul@65 | 159 | |
paul@72 | 160 | if (mapper == NULL) |
paul@114 | 161 | return pipe_error(); |
paul@65 | 162 | |
paul@72 | 163 | _mapper = mapper; |
paul@665 | 164 | _data_current = 0; |
paul@65 | 165 | |
paul@675 | 166 | return refresh(NULL, NULL, size, populated_size, NULL); |
paul@65 | 167 | } |
paul@65 | 168 | |
paul@114 | 169 | long PipePager::pipe_error() |
paul@114 | 170 | { |
paul@114 | 171 | if (_paging->closed()) |
paul@114 | 172 | return -L4_EIO; |
paul@114 | 173 | else |
paul@114 | 174 | return -L4_EBUSY; |
paul@114 | 175 | } |
paul@114 | 176 | |
paul@117 | 177 | |
paul@117 | 178 | |
paul@665 | 179 | /* Update the consumed or populated size of a pipe region and notify the other |
paul@665 | 180 | endpoint. The consumed size is recorded to allow pipes to be shared between |
paul@665 | 181 | programs. */ |
paul@117 | 182 | |
paul@665 | 183 | long PipePager::flush(offset_t position, offset_t *size) |
paul@117 | 184 | { |
paul@665 | 185 | if (_mapper != NULL) |
paul@665 | 186 | { |
paul@665 | 187 | _data_current = position; |
paul@665 | 188 | |
paul@665 | 189 | if (_writing) |
paul@665 | 190 | _mapper->set_data_size(position); |
paul@665 | 191 | } |
paul@117 | 192 | |
paul@117 | 193 | *size = _size; |
paul@117 | 194 | |
paul@122 | 195 | // NOTE: Perhaps employ a distinct event type for metadata updates. |
paul@122 | 196 | |
paul@137 | 197 | _paging->notify_others(_writing ? PipePaging::WRITER : PipePaging::READER, |
paul@484 | 198 | NOTIFY_CONTENT_AVAILABLE | NOTIFY_SPACE_AVAILABLE, |
paul@484 | 199 | NOTIFY_VALUES_NULL); |
paul@117 | 200 | return L4_EOK; |
paul@117 | 201 | } |
paul@117 | 202 | |
paul@117 | 203 | |
paul@117 | 204 | |
paul@488 | 205 | /* Subscribe to notifications. |
paul@488 | 206 | Readers can subscribe to new data (at end), and pipe closed events. |
paul@488 | 207 | Writers can subscribe to new space and pipe closed events. */ |
paul@117 | 208 | |
paul@290 | 209 | long PipePager::subscribe(l4_cap_idx_t notifier, notify_flags_t flags) |
paul@117 | 210 | { |
paul@488 | 211 | /* A single notifier is recorded so that it may be unsubscribed when the |
paul@548 | 212 | endpoint is closed. */ |
paul@488 | 213 | |
paul@548 | 214 | unsubscribe(); |
paul@117 | 215 | |
paul@485 | 216 | _notifier = notifier; |
paul@288 | 217 | return _paging->subscribe(_writing ? PipePaging::WRITER : PipePaging::READER, |
paul@548 | 218 | _notifier, flags); |
paul@290 | 219 | } |
paul@290 | 220 | |
paul@290 | 221 | /* Unsubscribe from notifications. */ |
paul@290 | 222 | |
paul@548 | 223 | long PipePager::unsubscribe() |
paul@290 | 224 | { |
paul@548 | 225 | if (l4_is_valid_cap(_notifier)) |
paul@548 | 226 | { |
paul@548 | 227 | long err = _paging->unsubscribe(_notifier); |
paul@548 | 228 | _notifier = L4_INVALID_CAP; |
paul@548 | 229 | return err; |
paul@548 | 230 | } |
paul@548 | 231 | else |
paul@548 | 232 | return L4_EOK; |
paul@117 | 233 | } |
paul@117 | 234 | |
paul@65 | 235 | // vim: tabstop=4 expandtab shiftwidth=4 |