paul@93 | 1 | /* |
paul@93 | 2 | * A pipe pager providing access to pipe content and navigation support. |
paul@93 | 3 | * |
paul@93 | 4 | * Copyright (C) 2021 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@166 | 39 | /* Obtain any initial page mapper, this having been set up in the paging |
paul@166 | 40 | coordinator. */ |
paul@166 | 41 | |
paul@166 | 42 | _mapper = _paging->current_region(_writing); |
paul@65 | 43 | } |
paul@65 | 44 | |
paul@65 | 45 | int PipePager::expected_items() |
paul@65 | 46 | { |
paul@65 | 47 | return PipeObject_expected_items; |
paul@65 | 48 | } |
paul@65 | 49 | |
paul@65 | 50 | ipc_server_handler_type PipePager::handler() |
paul@65 | 51 | { |
paul@65 | 52 | return (ipc_server_handler_type) handle_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@199 | 66 | unsigned int attached = _paging->detach(); |
paul@117 | 67 | |
paul@117 | 68 | /* Notify the other endpoint. */ |
paul@117 | 69 | |
paul@137 | 70 | _paging->notify_others(_writing ? PipePaging::WRITER : PipePaging::READER, NOTIFY_PEER_CLOSED); |
paul@199 | 71 | |
paul@199 | 72 | /* Deallocate the paging coordinator if no other endpoints are active. */ |
paul@199 | 73 | |
paul@199 | 74 | if (!attached) |
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@121 | 83 | long PipePager::map(offset_t offset, 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@65 | 100 | long PipePager::current_region(offset_t *populated_size, offset_t *size) |
paul@65 | 101 | { |
paul@65 | 102 | if (_mapper != NULL) |
paul@65 | 103 | { |
paul@65 | 104 | *populated_size = _mapper->get_data_size(); |
paul@65 | 105 | *size = _size; |
paul@65 | 106 | return L4_EOK; |
paul@65 | 107 | } |
paul@65 | 108 | else |
paul@114 | 109 | return pipe_error(); |
paul@65 | 110 | } |
paul@65 | 111 | |
paul@65 | 112 | /* Obtain the next region and its details. */ |
paul@65 | 113 | |
paul@65 | 114 | long PipePager::next_region(offset_t *populated_size, offset_t *size) |
paul@65 | 115 | { |
paul@65 | 116 | /* Obtain a new region if writing. */ |
paul@65 | 117 | |
paul@66 | 118 | if (_writing) |
paul@65 | 119 | return next_region_for_writer(populated_size, size); |
paul@65 | 120 | else |
paul@65 | 121 | return next_region_for_reader(populated_size, size); |
paul@65 | 122 | } |
paul@65 | 123 | |
paul@65 | 124 | long PipePager::next_region_for_reader(offset_t *populated_size, offset_t *size) |
paul@65 | 125 | { |
paul@72 | 126 | PageMapper *mapper = _paging->next_region(); |
paul@65 | 127 | |
paul@72 | 128 | if (mapper == NULL) |
paul@114 | 129 | return pipe_error(); |
paul@65 | 130 | |
paul@72 | 131 | _mapper = mapper; |
paul@65 | 132 | |
paul@65 | 133 | return current_region(populated_size, size); |
paul@65 | 134 | } |
paul@65 | 135 | |
paul@65 | 136 | long PipePager::next_region_for_writer(offset_t *populated_size, offset_t *size) |
paul@65 | 137 | { |
paul@65 | 138 | /* Set the populated size before moving on. */ |
paul@65 | 139 | |
paul@65 | 140 | if (_mapper != NULL) |
paul@65 | 141 | _mapper->set_data_size(*populated_size); |
paul@65 | 142 | |
paul@72 | 143 | /* Obtain the page mapper for the region. */ |
paul@66 | 144 | |
paul@72 | 145 | PageMapper *mapper = _paging->add_region(); |
paul@65 | 146 | |
paul@72 | 147 | if (mapper == NULL) |
paul@114 | 148 | return pipe_error(); |
paul@65 | 149 | |
paul@72 | 150 | _mapper = mapper; |
paul@65 | 151 | |
paul@72 | 152 | return current_region(populated_size, size); |
paul@65 | 153 | } |
paul@65 | 154 | |
paul@114 | 155 | long PipePager::pipe_error() |
paul@114 | 156 | { |
paul@114 | 157 | if (_paging->closed()) |
paul@114 | 158 | return -L4_EIO; |
paul@114 | 159 | else |
paul@114 | 160 | return -L4_EBUSY; |
paul@114 | 161 | } |
paul@114 | 162 | |
paul@117 | 163 | |
paul@117 | 164 | |
paul@117 | 165 | /* Update the populated size of a pipe region and notify the other endpoint. */ |
paul@117 | 166 | |
paul@117 | 167 | long PipePager::flush(offset_t populated_size, offset_t *size) |
paul@117 | 168 | { |
paul@117 | 169 | if (_mapper != NULL) |
paul@117 | 170 | _mapper->set_data_size(populated_size); |
paul@117 | 171 | |
paul@117 | 172 | *size = _size; |
paul@117 | 173 | |
paul@122 | 174 | // NOTE: Perhaps employ a distinct event type for metadata updates. |
paul@122 | 175 | |
paul@137 | 176 | _paging->notify_others(_writing ? PipePaging::WRITER : PipePaging::READER, |
paul@137 | 177 | NOTIFY_CONTENT_AVAILABLE | NOTIFY_SPACE_AVAILABLE); |
paul@117 | 178 | return L4_EOK; |
paul@117 | 179 | } |
paul@117 | 180 | |
paul@117 | 181 | |
paul@117 | 182 | |
paul@117 | 183 | /* Subscribe to notifications. */ |
paul@117 | 184 | |
paul@288 | 185 | long PipePager::subscribe(l4_cap_idx_t notifier, notify_flags_t flags, |
paul@288 | 186 | l4_cap_idx_t *peer) |
paul@117 | 187 | { |
paul@122 | 188 | /* Readers can subscribe to new data (at end), and pipe closed events. |
paul@122 | 189 | Writers can subscribe to new space and pipe closed events. */ |
paul@117 | 190 | |
paul@288 | 191 | return _paging->subscribe(_writing ? PipePaging::WRITER : PipePaging::READER, |
paul@288 | 192 | notifier, flags, peer); |
paul@117 | 193 | } |
paul@117 | 194 | |
paul@65 | 195 | // vim: tabstop=4 expandtab shiftwidth=4 |