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, releasing the paging coordinator for the pipe. This will 53 release all active page mappers. */ 54 55 void PipePager::close() 56 { 57 _paging->detach(); 58 59 /* Notify the other endpoint. */ 60 61 _paging->notify(_writing); 62 } 63 64 /* Support paging. */ 65 66 long PipePager::map(offset_t offset, address_t hot_spot, flags_t flags, l4_snd_fpage_t *region) 67 { 68 return Pager::map(offset, hot_spot, flags, region); 69 } 70 71 72 73 /* Return whether the pipe is closed or partly closed. */ 74 75 long PipePager::closed(int *closed) 76 { 77 *closed = _paging->closed(); 78 return L4_EOK; 79 } 80 81 /* Return details of the current region. */ 82 83 long PipePager::current_region(offset_t *populated_size, offset_t *size) 84 { 85 if (_mapper != NULL) 86 { 87 *populated_size = _mapper->get_data_size(); 88 *size = _size; 89 return L4_EOK; 90 } 91 else 92 return pipe_error(); 93 } 94 95 /* Obtain the next region and its details. */ 96 97 long PipePager::next_region(offset_t *populated_size, offset_t *size) 98 { 99 /* Obtain a new region if writing. */ 100 101 if (_writing) 102 return next_region_for_writer(populated_size, size); 103 else 104 return next_region_for_reader(populated_size, size); 105 } 106 107 long PipePager::next_region_for_reader(offset_t *populated_size, offset_t *size) 108 { 109 PageMapper *mapper = _paging->next_region(); 110 111 if (mapper == NULL) 112 return pipe_error(); 113 114 _mapper = mapper; 115 116 return current_region(populated_size, size); 117 } 118 119 long PipePager::next_region_for_writer(offset_t *populated_size, offset_t *size) 120 { 121 /* Set the populated size before moving on. */ 122 123 if (_mapper != NULL) 124 _mapper->set_data_size(*populated_size); 125 126 /* Obtain the page mapper for the region. */ 127 128 PageMapper *mapper = _paging->add_region(); 129 130 if (mapper == NULL) 131 return pipe_error(); 132 133 _mapper = mapper; 134 135 return current_region(populated_size, size); 136 } 137 138 long PipePager::pipe_error() 139 { 140 if (_paging->closed()) 141 return -L4_EIO; 142 else 143 return -L4_EBUSY; 144 } 145 146 147 148 /* Update the populated size of a pipe region and notify the other endpoint. */ 149 150 long PipePager::flush(offset_t populated_size, offset_t *size) 151 { 152 if (_mapper != NULL) 153 _mapper->set_data_size(populated_size); 154 155 *size = _size; 156 157 _paging->notify(_writing); 158 return L4_EOK; 159 } 160 161 162 163 /* Subscribe to notifications. */ 164 165 long PipePager::subscribe(flags_t flags, l4_cap_idx_t *irq) 166 { 167 // NOTE: Need to interpret flags. 168 169 *irq = _paging->subscribe(_writing); 170 return L4_EOK; 171 } 172 173 long PipePager::unsubscribe() 174 { 175 _paging->unsubscribe(_writing); 176 return L4_EOK; 177 } 178 179 // vim: tabstop=4 expandtab shiftwidth=4