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