paul@366 | 1 | /* |
paul@366 | 2 | * A system pager implementation residing in the same task as a program. |
paul@366 | 3 | * |
paul@472 | 4 | * Copyright (C) 2022, 2023 Paul Boddie <paul@boddie.org.uk> |
paul@366 | 5 | * |
paul@366 | 6 | * This program is free software; you can redistribute it and/or |
paul@366 | 7 | * modify it under the terms of the GNU General Public License as |
paul@366 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@366 | 9 | * the License, or (at your option) any later version. |
paul@366 | 10 | * |
paul@366 | 11 | * This program is distributed in the hope that it will be useful, |
paul@366 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@366 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@366 | 14 | * GNU General Public License for more details. |
paul@366 | 15 | * |
paul@366 | 16 | * You should have received a copy of the GNU General Public License |
paul@366 | 17 | * along with this program; if not, write to the Free Software |
paul@366 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@366 | 19 | * Boston, MA 02110-1301, USA |
paul@366 | 20 | */ |
paul@366 | 21 | |
paul@366 | 22 | #include <l4/re/env.h> |
paul@431 | 23 | #include <l4/re/c/dataspace.h> |
paul@469 | 24 | #include <l4/util/util.h> |
paul@366 | 25 | |
paul@366 | 26 | #include <mem/memory_utils.h> |
paul@472 | 27 | #include <systypes/base.h> |
paul@366 | 28 | |
paul@366 | 29 | #include <stdio.h> |
paul@366 | 30 | |
paul@366 | 31 | #include "dataspace_client.h" |
paul@366 | 32 | #include "internal_pager.h" |
paul@493 | 33 | #include "pager_object_server.h" |
paul@366 | 34 | |
paul@366 | 35 | |
paul@366 | 36 | |
paul@431 | 37 | #define DEBUG 0 |
paul@431 | 38 | |
paul@431 | 39 | |
paul@431 | 40 | |
paul@366 | 41 | /* A simple system pager also acting as a region mapper. */ |
paul@366 | 42 | |
paul@431 | 43 | InternalPager::InternalPager(address_t start, address_t end) |
paul@431 | 44 | : ExecPager(start, end) |
paul@366 | 45 | { |
paul@366 | 46 | } |
paul@366 | 47 | |
paul@493 | 48 | ipc_server_default_config_type InternalPager::config() |
paul@493 | 49 | { |
paul@493 | 50 | return config_PagerObject; |
paul@493 | 51 | } |
paul@493 | 52 | |
paul@493 | 53 | |
paul@493 | 54 | |
paul@366 | 55 | /* Handle a general exception. */ |
paul@366 | 56 | |
paul@366 | 57 | long InternalPager::exception(l4_exc_regs_t regs, l4_snd_fpage_t *region) |
paul@366 | 58 | { |
paul@366 | 59 | (void) region; |
paul@366 | 60 | |
paul@431 | 61 | printf("InternalPager::exception(...) -> pfa = %lx, pc = %lx\n", l4_utcb_exc_pfa(®s), l4_utcb_exc_pc(®s)); |
paul@366 | 62 | |
paul@431 | 63 | #if DEBUG |
paul@376 | 64 | while (1) |
paul@376 | 65 | l4_sleep_forever(); |
paul@431 | 66 | #endif |
paul@376 | 67 | |
paul@366 | 68 | return L4_EOK; |
paul@366 | 69 | } |
paul@366 | 70 | |
paul@366 | 71 | /* Handle a page fault using any configured regions. */ |
paul@366 | 72 | |
paul@366 | 73 | long InternalPager::page_fault(l4_umword_t pfa, l4_umword_t pc, l4_snd_fpage_t *region) |
paul@366 | 74 | { |
paul@366 | 75 | l4_umword_t addr = pfa & ~7UL, flags = pfa & 7; |
paul@366 | 76 | |
paul@366 | 77 | #if DEBUG |
paul@366 | 78 | printf("page_fault(%lx, %lx) -> %lx (%lx) -> ", pfa, pc, addr, flags); |
paul@431 | 79 | l4_thread_regs_t *regs = l4_utcb_tcr(); |
paul@431 | 80 | printf("regs = %p\n; regs->user[0] = %lx\n", regs, regs->user[0]); |
paul@366 | 81 | #endif |
paul@366 | 82 | |
paul@472 | 83 | /* Find the first region whose start address is beyond the fault address, |
paul@472 | 84 | testing if any immediately preceding region contains the fault address. */ |
paul@472 | 85 | |
paul@431 | 86 | MappedRegions::iterator it = _regions.upper_bound(addr); |
paul@366 | 87 | |
paul@366 | 88 | if (it != _regions.begin()) |
paul@366 | 89 | it--; |
paul@366 | 90 | else |
paul@366 | 91 | { |
paul@431 | 92 | printf("not mapped at %lx for pc %lx\n", addr, pc); |
paul@366 | 93 | return -L4_ENOMEM; |
paul@366 | 94 | } |
paul@366 | 95 | |
paul@431 | 96 | MappedRegion &r = it->second; |
paul@366 | 97 | |
paul@366 | 98 | if ((addr >= r.start) && (addr < r.start + r.size)) |
paul@366 | 99 | { |
paul@419 | 100 | address_t window_size = L4_PAGESIZE; |
paul@376 | 101 | address_t window_base = trunc(addr, window_size); |
paul@419 | 102 | address_t offset = addr - r.start; |
paul@376 | 103 | address_t page_addr = trunc(addr, L4_PAGESIZE); |
paul@376 | 104 | address_t hot_spot = page_addr - window_base; |
paul@366 | 105 | |
paul@366 | 106 | /* Interact with the region's dataspace, specifying a receive window for a |
paul@366 | 107 | map operation. Here, a single page is specified. */ |
paul@366 | 108 | |
paul@366 | 109 | client_Dataspace dataspace(r.ds); |
paul@376 | 110 | l4_snd_fpage_t rw_region = {0, l4_fpage(window_base, L4_PAGESHIFT, 0)}; |
paul@472 | 111 | map_flags_t map_flags = map_flags_for_fault(flags); |
paul@366 | 112 | |
paul@366 | 113 | #if DEBUG |
paul@376 | 114 | printf("window_base = %lx; window_size = %lx\n", window_base, window_size); |
paul@376 | 115 | printf("region = {%lx, {%lx, %d}}\n", rw_region.snd_base, l4_fpage_memaddr(rw_region.fpage), l4_fpage_size(rw_region.fpage)); |
paul@376 | 116 | printf("map(%lx, %lx, %lx) -> %lx\n", offset, hot_spot, map_flags, r.ds); |
paul@366 | 117 | #endif |
paul@366 | 118 | |
paul@376 | 119 | long err = dataspace.map(offset, hot_spot, map_flags & r.flags, &rw_region); |
paul@376 | 120 | |
paul@376 | 121 | /* Indicate an unspecified result, since the mapping should have taken |
paul@376 | 122 | place. */ |
paul@376 | 123 | |
paul@376 | 124 | *region = {0, l4_fpage_invalid()}; |
paul@376 | 125 | |
paul@376 | 126 | return err; |
paul@366 | 127 | } |
paul@366 | 128 | |
paul@366 | 129 | #if DEBUG |
paul@366 | 130 | printf("not mapped!\n"); |
paul@366 | 131 | #endif |
paul@366 | 132 | |
paul@366 | 133 | return -L4_ENOMEM; |
paul@366 | 134 | } |
paul@366 | 135 | |
paul@366 | 136 | /* Attach a region for provision when page faults occur. This is required in |
paul@366 | 137 | the initialisation of a program by the C library which requires a region |
paul@366 | 138 | mapper. */ |
paul@366 | 139 | |
paul@419 | 140 | long InternalPager::attach(address_t *start, address_t size, map_flags_t flags, |
paul@366 | 141 | l4_cap_idx_t ds, address_t offset, |
paul@366 | 142 | unsigned char align) |
paul@366 | 143 | { |
paul@469 | 144 | long err = ExecPager::find(start, &size, flags, offset, align); |
paul@366 | 145 | |
paul@431 | 146 | if (!err) |
paul@431 | 147 | { |
paul@431 | 148 | MappedRegion r(*start, size, flags & L4RE_DS_F_RIGHTS_MASK, ds); |
paul@366 | 149 | add(r); |
paul@366 | 150 | } |
paul@366 | 151 | |
paul@431 | 152 | return err; |
paul@366 | 153 | } |
paul@366 | 154 | |
paul@366 | 155 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@366 | 156 | */ |