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 60 /* Support paging. */ 61 62 long PipePager::map(offset_t offset, address_t hot_spot, flags_t flags, l4_snd_fpage_t *region) 63 { 64 return Pager::map(offset, hot_spot, flags, region); 65 } 66 67 68 69 /* Return details of the current region. */ 70 71 long PipePager::current_region(offset_t *populated_size, offset_t *size) 72 { 73 if (_mapper != NULL) 74 { 75 *populated_size = _mapper->get_data_size(); 76 *size = _size; 77 return L4_EOK; 78 } 79 else 80 return -L4_EIO; 81 } 82 83 /* Obtain the next region and its details. */ 84 85 long PipePager::next_region(offset_t *populated_size, offset_t *size) 86 { 87 /* Obtain a new region if writing. */ 88 89 if (_writing) 90 return next_region_for_writer(populated_size, size); 91 else 92 return next_region_for_reader(populated_size, size); 93 } 94 95 long PipePager::next_region_for_reader(offset_t *populated_size, offset_t *size) 96 { 97 PageMapper *mapper = _paging->next_region(); 98 99 if (mapper == NULL) 100 return -L4_EIO; 101 102 _mapper = mapper; 103 104 return current_region(populated_size, size); 105 } 106 107 long PipePager::next_region_for_writer(offset_t *populated_size, offset_t *size) 108 { 109 /* Set the populated size before moving on. */ 110 111 if (_mapper != NULL) 112 _mapper->set_data_size(*populated_size); 113 114 /* Obtain the page mapper for the region. */ 115 116 PageMapper *mapper = _paging->add_region(); 117 118 if (mapper == NULL) 119 return -L4_EIO; 120 121 _mapper = mapper; 122 123 return current_region(populated_size, size); 124 } 125 126 // vim: tabstop=4 expandtab shiftwidth=4