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@498 | 26 | #include <ipc/cap_alloc.h> |
paul@366 | 27 | #include <mem/memory_utils.h> |
paul@472 | 28 | #include <systypes/base.h> |
paul@366 | 29 | |
paul@366 | 30 | #include <stdio.h> |
paul@366 | 31 | |
paul@366 | 32 | #include "dataspace_client.h" |
paul@366 | 33 | #include "internal_pager.h" |
paul@493 | 34 | #include "pager_object_server.h" |
paul@366 | 35 | |
paul@366 | 36 | |
paul@366 | 37 | |
paul@431 | 38 | #define DEBUG 0 |
paul@431 | 39 | |
paul@431 | 40 | |
paul@431 | 41 | |
paul@366 | 42 | /* A simple system pager also acting as a region mapper. */ |
paul@366 | 43 | |
paul@431 | 44 | InternalPager::InternalPager(address_t start, address_t end) |
paul@431 | 45 | : ExecPager(start, end) |
paul@366 | 46 | { |
paul@366 | 47 | } |
paul@366 | 48 | |
paul@493 | 49 | ipc_server_default_config_type InternalPager::config() |
paul@493 | 50 | { |
paul@493 | 51 | return config_PagerObject; |
paul@493 | 52 | } |
paul@493 | 53 | |
paul@493 | 54 | |
paul@493 | 55 | |
paul@498 | 56 | /* Close the pager. */ |
paul@498 | 57 | |
paul@498 | 58 | void InternalPager::close() |
paul@498 | 59 | { |
paul@498 | 60 | printf("Internal pager closing...\n"); |
paul@498 | 61 | |
paul@498 | 62 | /* Unmap all regions. */ |
paul@498 | 63 | |
paul@498 | 64 | MappedRegions::iterator it; |
paul@498 | 65 | |
paul@498 | 66 | for (it = _regions.begin(); it != _regions.end(); it++) |
paul@498 | 67 | { |
paul@498 | 68 | MappedRegion &r = it->second; |
paul@498 | 69 | |
paul@498 | 70 | ipc_cap_free_um(r.ds); |
paul@498 | 71 | } |
paul@498 | 72 | } |
paul@498 | 73 | |
paul@498 | 74 | |
paul@498 | 75 | |
paul@366 | 76 | /* Handle a general exception. */ |
paul@366 | 77 | |
paul@366 | 78 | long InternalPager::exception(l4_exc_regs_t regs, l4_snd_fpage_t *region) |
paul@366 | 79 | { |
paul@366 | 80 | (void) region; |
paul@366 | 81 | |
paul@431 | 82 | printf("InternalPager::exception(...) -> pfa = %lx, pc = %lx\n", l4_utcb_exc_pfa(®s), l4_utcb_exc_pc(®s)); |
paul@366 | 83 | |
paul@431 | 84 | #if DEBUG |
paul@376 | 85 | while (1) |
paul@376 | 86 | l4_sleep_forever(); |
paul@431 | 87 | #endif |
paul@376 | 88 | |
paul@366 | 89 | return L4_EOK; |
paul@366 | 90 | } |
paul@366 | 91 | |
paul@366 | 92 | /* Handle a page fault using any configured regions. */ |
paul@366 | 93 | |
paul@366 | 94 | long InternalPager::page_fault(l4_umword_t pfa, l4_umword_t pc, l4_snd_fpage_t *region) |
paul@366 | 95 | { |
paul@366 | 96 | l4_umword_t addr = pfa & ~7UL, flags = pfa & 7; |
paul@366 | 97 | |
paul@366 | 98 | #if DEBUG |
paul@366 | 99 | printf("page_fault(%lx, %lx) -> %lx (%lx) -> ", pfa, pc, addr, flags); |
paul@431 | 100 | l4_thread_regs_t *regs = l4_utcb_tcr(); |
paul@431 | 101 | printf("regs = %p\n; regs->user[0] = %lx\n", regs, regs->user[0]); |
paul@366 | 102 | #endif |
paul@366 | 103 | |
paul@472 | 104 | /* Find the first region whose start address is beyond the fault address, |
paul@472 | 105 | testing if any immediately preceding region contains the fault address. */ |
paul@472 | 106 | |
paul@431 | 107 | MappedRegions::iterator it = _regions.upper_bound(addr); |
paul@366 | 108 | |
paul@366 | 109 | if (it != _regions.begin()) |
paul@366 | 110 | it--; |
paul@366 | 111 | else |
paul@366 | 112 | { |
paul@431 | 113 | printf("not mapped at %lx for pc %lx\n", addr, pc); |
paul@366 | 114 | return -L4_ENOMEM; |
paul@366 | 115 | } |
paul@366 | 116 | |
paul@431 | 117 | MappedRegion &r = it->second; |
paul@366 | 118 | |
paul@366 | 119 | if ((addr >= r.start) && (addr < r.start + r.size)) |
paul@366 | 120 | { |
paul@419 | 121 | address_t window_size = L4_PAGESIZE; |
paul@376 | 122 | address_t window_base = trunc(addr, window_size); |
paul@419 | 123 | address_t offset = addr - r.start; |
paul@376 | 124 | address_t page_addr = trunc(addr, L4_PAGESIZE); |
paul@376 | 125 | address_t hot_spot = page_addr - window_base; |
paul@366 | 126 | |
paul@366 | 127 | /* Interact with the region's dataspace, specifying a receive window for a |
paul@366 | 128 | map operation. Here, a single page is specified. */ |
paul@366 | 129 | |
paul@366 | 130 | client_Dataspace dataspace(r.ds); |
paul@376 | 131 | l4_snd_fpage_t rw_region = {0, l4_fpage(window_base, L4_PAGESHIFT, 0)}; |
paul@472 | 132 | map_flags_t map_flags = map_flags_for_fault(flags); |
paul@366 | 133 | |
paul@366 | 134 | #if DEBUG |
paul@376 | 135 | printf("window_base = %lx; window_size = %lx\n", window_base, window_size); |
paul@376 | 136 | printf("region = {%lx, {%lx, %d}}\n", rw_region.snd_base, l4_fpage_memaddr(rw_region.fpage), l4_fpage_size(rw_region.fpage)); |
paul@376 | 137 | printf("map(%lx, %lx, %lx) -> %lx\n", offset, hot_spot, map_flags, r.ds); |
paul@366 | 138 | #endif |
paul@366 | 139 | |
paul@376 | 140 | long err = dataspace.map(offset, hot_spot, map_flags & r.flags, &rw_region); |
paul@376 | 141 | |
paul@376 | 142 | /* Indicate an unspecified result, since the mapping should have taken |
paul@376 | 143 | place. */ |
paul@376 | 144 | |
paul@376 | 145 | *region = {0, l4_fpage_invalid()}; |
paul@376 | 146 | |
paul@376 | 147 | return err; |
paul@366 | 148 | } |
paul@366 | 149 | |
paul@366 | 150 | #if DEBUG |
paul@366 | 151 | printf("not mapped!\n"); |
paul@366 | 152 | #endif |
paul@366 | 153 | |
paul@366 | 154 | return -L4_ENOMEM; |
paul@366 | 155 | } |
paul@366 | 156 | |
paul@366 | 157 | /* Attach a region for provision when page faults occur. This is required in |
paul@366 | 158 | the initialisation of a program by the C library which requires a region |
paul@366 | 159 | mapper. */ |
paul@366 | 160 | |
paul@419 | 161 | long InternalPager::attach(address_t *start, address_t size, map_flags_t flags, |
paul@366 | 162 | l4_cap_idx_t ds, address_t offset, |
paul@366 | 163 | unsigned char align) |
paul@366 | 164 | { |
paul@469 | 165 | long err = ExecPager::find(start, &size, flags, offset, align); |
paul@366 | 166 | |
paul@431 | 167 | if (!err) |
paul@431 | 168 | { |
paul@431 | 169 | MappedRegion r(*start, size, flags & L4RE_DS_F_RIGHTS_MASK, ds); |
paul@366 | 170 | add(r); |
paul@366 | 171 | } |
paul@502 | 172 | else |
paul@502 | 173 | ipc_cap_free_um(ds); |
paul@366 | 174 | |
paul@431 | 175 | return err; |
paul@366 | 176 | } |
paul@366 | 177 | |
paul@366 | 178 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@366 | 179 | */ |