paul@366 | 1 | /* |
paul@366 | 2 | * A system pager implementation residing in a separate task. |
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@366 | 23 | #include <l4/util/util.h> |
paul@366 | 24 | |
paul@366 | 25 | #include <ipc/mem_ipc.h> |
paul@366 | 26 | #include <mem/memory_utils.h> |
paul@366 | 27 | |
paul@366 | 28 | #include <stdio.h> |
paul@366 | 29 | |
paul@366 | 30 | #include "external_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 | ExternalPager::ExternalPager(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 ExternalPager::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("ExternalPager::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 ExternalPager::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@366 | 69 | #endif |
paul@366 | 70 | |
paul@366 | 71 | MappedRegions::iterator it = _regions.upper_bound(addr); |
paul@366 | 72 | |
paul@366 | 73 | if (it != _regions.begin()) |
paul@366 | 74 | it--; |
paul@366 | 75 | else |
paul@366 | 76 | { |
paul@431 | 77 | printf("not mapped at %lx for pc %lx\n", addr, pc); |
paul@366 | 78 | return -L4_ENOMEM; |
paul@366 | 79 | } |
paul@366 | 80 | |
paul@366 | 81 | MappedRegion &r = it->second; |
paul@366 | 82 | |
paul@431 | 83 | if ((addr >= r.start) && (addr < r.start + r.size)) |
paul@366 | 84 | { |
paul@366 | 85 | l4_addr_t page_addr = trunc(addr, L4_PAGESIZE); |
paul@376 | 86 | 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@366 | 87 | |
paul@376 | 88 | if (!map_flags) |
paul@376 | 89 | map_flags = L4RE_DS_F_R; |
paul@376 | 90 | |
paul@431 | 91 | region->fpage = l4_fpage(r.ds_start + (page_addr - r.start), L4_PAGESHIFT, map_flags & r.flags); |
paul@366 | 92 | region->snd_base = page_addr; |
paul@366 | 93 | |
paul@366 | 94 | #if DEBUG |
paul@376 | 95 | printf("%lx...%lx from %lx...%lx offset %lx size %d rights %x ds %lx\n", |
paul@431 | 96 | r.start, region->snd_base, |
paul@431 | 97 | r.ds_start, l4_fpage_memaddr(region->fpage), |
paul@431 | 98 | addr - r.start, |
paul@366 | 99 | l4_fpage_size(region->fpage), |
paul@376 | 100 | l4_fpage_rights(region->fpage), |
paul@376 | 101 | r.ds); |
paul@366 | 102 | |
paul@366 | 103 | printf("%lx -> ", addr); |
paul@366 | 104 | |
paul@366 | 105 | for (unsigned int i = 0; i < sizeof(l4_umword_t); i++) |
paul@431 | 106 | printf("%02x", *((unsigned char *)(r.ds_start + (addr - r.start) + i))); |
paul@366 | 107 | |
paul@366 | 108 | printf("\n"); |
paul@366 | 109 | #endif |
paul@366 | 110 | |
paul@366 | 111 | if (r.flags & L4RE_RM_F_W) |
paul@431 | 112 | l4_touch_rw((const void *) (r.ds_start + (page_addr - r.start)), L4_PAGESIZE); |
paul@366 | 113 | else |
paul@431 | 114 | l4_touch_ro((const void *) (r.ds_start + (page_addr - r.start)), L4_PAGESIZE); |
paul@366 | 115 | |
paul@366 | 116 | return L4_EOK; |
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 ExternalPager::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@366 | 138 | /* Attach the provided dataspace. |
paul@366 | 139 | NOTE: This is only done in this implementation to support the paging |
paul@366 | 140 | mechanism. In a region mapper residing within the actual task, the |
paul@366 | 141 | dataspace's map operation would be invoked to obtain mappings. */ |
paul@366 | 142 | |
paul@366 | 143 | l4_addr_t ds_start; |
paul@431 | 144 | err = ipc_attach_dataspace(ds, size, (void **) &ds_start); |
paul@366 | 145 | |
paul@366 | 146 | if (err) |
paul@366 | 147 | return err; |
paul@366 | 148 | |
paul@366 | 149 | l4_touch_rw((const void *) ds_start, size); |
paul@366 | 150 | |
paul@431 | 151 | MappedRegion r(*start, size, flags & L4RE_DS_F_RIGHTS_MASK, ds, ds_start); |
paul@366 | 152 | add(r); |
paul@366 | 153 | } |
paul@366 | 154 | |
paul@431 | 155 | return err; |
paul@366 | 156 | } |
paul@366 | 157 | |
paul@366 | 158 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@366 | 159 | */ |