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