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