1 /* 2 * A simple pager exposing a single memory region. 3 * 4 * Copyright (C) 2021, 2022, 2023, 2024 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 <l4/re/c/dataspace.h> 23 24 #include <mem/ipc.h> 25 #include <mem/memory_incremental.h> 26 27 #include <string.h> 28 29 #include "dataspace_server.h" 30 #include "simple_pager.h" 31 32 33 34 SimplePager::SimplePager(Memory *memory) 35 { 36 if (memory == NULL) 37 _memory = new MemoryIncremental(); 38 else 39 _memory = memory; 40 41 _region = _memory->region(); 42 } 43 44 void SimplePager::close() 45 { 46 if (_region != NULL) 47 { 48 _memory->release(_region); 49 _region = NULL; 50 } 51 } 52 53 54 55 /* Data access methods. */ 56 57 char *SimplePager::get_string(offset_t offset) 58 { 59 char *buffer = _region->read(offset); 60 offset_t size = _region->size(); 61 62 /* Confine the path to the limit of the buffer. */ 63 64 if ((buffer != NULL) && (strnlen(buffer, size) < size)) 65 return buffer; 66 else 67 return NULL; 68 } 69 70 71 72 /* Map a flexpage corresponding to the dataspace 'offset' involving a 'hot_spot' 73 (flexpage offset). */ 74 75 long SimplePager::map(offset_t offset, map_address_t hot_spot, map_flags_t flags, l4_snd_fpage_t *region) 76 { 77 Flexpage flexpage(_region); 78 79 flexpage.reset(offset); 80 flexpage.upgrade(flags); 81 82 /* Send the flexpage explicitly. */ 83 84 long err = ipc_prepare_flexpage(&flexpage, offset, _region->size(), 85 hot_spot, flags, region); 86 87 if (err) 88 return err; 89 90 err = complete_Dataspace_map(*region); 91 92 if (err) 93 return err; 94 95 return IPC_MESSAGE_SENT; 96 } 97 98 long SimplePager::info(ds_stats_t *stats) 99 { 100 stats->size = _region->size(); 101 stats->flags = L4_FPAGE_RW; 102 103 return L4_EOK; 104 } 105 106 // vim: tabstop=4 expandtab shiftwidth=4