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