paul@366 | 1 | /* |
paul@366 | 2 | * A system pager implementation residing in a separate task. |
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@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@472 | 27 | #include <systypes/base.h> |
paul@366 | 28 | |
paul@366 | 29 | #include <stdio.h> |
paul@366 | 30 | |
paul@366 | 31 | #include "external_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 | ExternalPager::ExternalPager(address_t start, address_t end) |
paul@484 | 42 | : ExecPager(start, end), NotificationSupport() |
paul@366 | 43 | { |
paul@366 | 44 | } |
paul@366 | 45 | |
paul@366 | 46 | /* Handle a general exception. */ |
paul@366 | 47 | |
paul@366 | 48 | long ExternalPager::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("ExternalPager::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 ExternalPager::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@366 | 70 | #endif |
paul@366 | 71 | |
paul@472 | 72 | /* Find the first region whose start address is beyond the fault address, |
paul@472 | 73 | testing if any immediately preceding region contains the fault address. */ |
paul@472 | 74 | |
paul@366 | 75 | MappedRegions::iterator it = _regions.upper_bound(addr); |
paul@366 | 76 | |
paul@366 | 77 | if (it != _regions.begin()) |
paul@366 | 78 | it--; |
paul@366 | 79 | else |
paul@366 | 80 | { |
paul@431 | 81 | printf("not mapped at %lx for pc %lx\n", addr, pc); |
paul@366 | 82 | return -L4_ENOMEM; |
paul@366 | 83 | } |
paul@366 | 84 | |
paul@472 | 85 | /* Obtain the region and test if it contains the fault address. */ |
paul@472 | 86 | |
paul@366 | 87 | MappedRegion &r = it->second; |
paul@366 | 88 | |
paul@431 | 89 | if ((addr >= r.start) && (addr < r.start + r.size)) |
paul@366 | 90 | { |
paul@366 | 91 | l4_addr_t page_addr = trunc(addr, L4_PAGESIZE); |
paul@472 | 92 | map_flags_t map_flags = map_flags_for_fault(flags); |
paul@376 | 93 | |
paul@431 | 94 | region->fpage = l4_fpage(r.ds_start + (page_addr - r.start), L4_PAGESHIFT, map_flags & r.flags); |
paul@366 | 95 | region->snd_base = page_addr; |
paul@366 | 96 | |
paul@366 | 97 | #if DEBUG |
paul@376 | 98 | printf("%lx...%lx from %lx...%lx offset %lx size %d rights %x ds %lx\n", |
paul@431 | 99 | r.start, region->snd_base, |
paul@431 | 100 | r.ds_start, l4_fpage_memaddr(region->fpage), |
paul@431 | 101 | addr - r.start, |
paul@366 | 102 | l4_fpage_size(region->fpage), |
paul@376 | 103 | l4_fpage_rights(region->fpage), |
paul@376 | 104 | r.ds); |
paul@366 | 105 | |
paul@366 | 106 | printf("%lx -> ", addr); |
paul@366 | 107 | |
paul@366 | 108 | for (unsigned int i = 0; i < sizeof(l4_umword_t); i++) |
paul@431 | 109 | printf("%02x", *((unsigned char *)(r.ds_start + (addr - r.start) + i))); |
paul@366 | 110 | |
paul@366 | 111 | printf("\n"); |
paul@366 | 112 | #endif |
paul@366 | 113 | |
paul@366 | 114 | if (r.flags & L4RE_RM_F_W) |
paul@431 | 115 | l4_touch_rw((const void *) (r.ds_start + (page_addr - r.start)), L4_PAGESIZE); |
paul@366 | 116 | else |
paul@431 | 117 | l4_touch_ro((const void *) (r.ds_start + (page_addr - r.start)), L4_PAGESIZE); |
paul@366 | 118 | |
paul@366 | 119 | return L4_EOK; |
paul@366 | 120 | } |
paul@366 | 121 | |
paul@366 | 122 | #if DEBUG |
paul@366 | 123 | printf("not mapped!\n"); |
paul@366 | 124 | #endif |
paul@366 | 125 | |
paul@366 | 126 | return -L4_ENOMEM; |
paul@366 | 127 | } |
paul@366 | 128 | |
paul@366 | 129 | /* Attach a region for provision when page faults occur. This is required in |
paul@366 | 130 | the initialisation of a program by the C library which requires a region |
paul@366 | 131 | mapper. */ |
paul@366 | 132 | |
paul@419 | 133 | long ExternalPager::attach(address_t *start, address_t size, map_flags_t flags, |
paul@366 | 134 | l4_cap_idx_t ds, address_t offset, |
paul@366 | 135 | unsigned char align) |
paul@366 | 136 | { |
paul@469 | 137 | long err = ExecPager::find(start, &size, flags, offset, align); |
paul@366 | 138 | |
paul@431 | 139 | if (!err) |
paul@431 | 140 | { |
paul@366 | 141 | /* Attach the provided dataspace. |
paul@472 | 142 | |
paul@472 | 143 | This is only done in this implementation to support the paging |
paul@472 | 144 | mechanism. In a region mapper residing within the actual task, the |
paul@472 | 145 | dataspace's map operation would be invoked to obtain mappings. */ |
paul@366 | 146 | |
paul@366 | 147 | l4_addr_t ds_start; |
paul@431 | 148 | err = ipc_attach_dataspace(ds, size, (void **) &ds_start); |
paul@366 | 149 | |
paul@366 | 150 | if (err) |
paul@366 | 151 | return err; |
paul@366 | 152 | |
paul@366 | 153 | l4_touch_rw((const void *) ds_start, size); |
paul@366 | 154 | |
paul@431 | 155 | MappedRegion r(*start, size, flags & L4RE_DS_F_RIGHTS_MASK, ds, ds_start); |
paul@366 | 156 | add(r); |
paul@366 | 157 | } |
paul@366 | 158 | |
paul@431 | 159 | return err; |
paul@366 | 160 | } |
paul@366 | 161 | |
paul@469 | 162 | /* Receive signals from a task. */ |
paul@469 | 163 | |
paul@469 | 164 | long ExternalPager::signal(unsigned long sig, unsigned long val) |
paul@469 | 165 | { |
paul@484 | 166 | notify_all(NOTIFY_TASK_SIGNAL, (notify_values_t) {sig, val}); |
paul@480 | 167 | |
paul@469 | 168 | return L4_EOK; |
paul@469 | 169 | } |
paul@469 | 170 | |
paul@485 | 171 | /* Subscribe to notifications. */ |
paul@485 | 172 | |
paul@485 | 173 | long ExternalPager::subscribe(l4_cap_idx_t notifier, notify_flags_t flags) |
paul@485 | 174 | { |
paul@485 | 175 | return NotificationSupport::subscribe(notifier, flags); |
paul@485 | 176 | } |
paul@485 | 177 | |
paul@485 | 178 | /* Unsubscribe from notifications. */ |
paul@485 | 179 | |
paul@488 | 180 | long ExternalPager::unsubscribe(l4_cap_idx_t notifier) |
paul@485 | 181 | { |
paul@488 | 182 | return NotificationSupport::unsubscribe(notifier); |
paul@485 | 183 | } |
paul@485 | 184 | |
paul@366 | 185 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@366 | 186 | */ |