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