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@492 | 23 | #include <l4/sys/task.h> |
paul@366 | 24 | #include <l4/util/util.h> |
paul@366 | 25 | |
paul@492 | 26 | #include <ipc/cap_alloc.h> |
paul@366 | 27 | #include <ipc/mem_ipc.h> |
paul@366 | 28 | #include <mem/memory_utils.h> |
paul@472 | 29 | #include <systypes/base.h> |
paul@366 | 30 | |
paul@366 | 31 | #include <stdio.h> |
paul@366 | 32 | |
paul@366 | 33 | #include "external_pager.h" |
paul@489 | 34 | #include "parent_pager_object_server.h" |
paul@366 | 35 | |
paul@366 | 36 | |
paul@366 | 37 | |
paul@431 | 38 | #define DEBUG 0 |
paul@431 | 39 | |
paul@431 | 40 | |
paul@431 | 41 | |
paul@366 | 42 | /* A simple system pager also acting as a region mapper. */ |
paul@366 | 43 | |
paul@431 | 44 | ExternalPager::ExternalPager(address_t start, address_t end) |
paul@484 | 45 | : ExecPager(start, end), NotificationSupport() |
paul@366 | 46 | { |
paul@366 | 47 | } |
paul@366 | 48 | |
paul@489 | 49 | ipc_server_default_config_type ExternalPager::config() |
paul@489 | 50 | { |
paul@492 | 51 | return config_ParentPagerObject; |
paul@492 | 52 | } |
paul@492 | 53 | |
paul@492 | 54 | |
paul@492 | 55 | |
paul@492 | 56 | /* Close the pager. */ |
paul@492 | 57 | |
paul@492 | 58 | void ExternalPager::close() |
paul@492 | 59 | { |
paul@492 | 60 | printf("Pager closing...\n"); |
paul@492 | 61 | |
paul@492 | 62 | /* Unmap all regions. */ |
paul@492 | 63 | |
paul@492 | 64 | MappedRegions::iterator it; |
paul@492 | 65 | |
paul@492 | 66 | for (it = _regions.begin(); it != _regions.end(); it++) |
paul@492 | 67 | { |
paul@492 | 68 | MappedRegion &r = it->second; |
paul@492 | 69 | |
paul@492 | 70 | ipc_detach_dataspace((void *) r.ds_start); |
paul@492 | 71 | } |
paul@489 | 72 | } |
paul@489 | 73 | |
paul@492 | 74 | |
paul@492 | 75 | |
paul@492 | 76 | /* Manage the task and thread capabilities. */ |
paul@492 | 77 | |
paul@492 | 78 | void ExternalPager::set_task(l4_cap_idx_t cap) |
paul@492 | 79 | { |
paul@492 | 80 | _task = cap; |
paul@492 | 81 | } |
paul@492 | 82 | |
paul@492 | 83 | void ExternalPager::set_server(l4_cap_idx_t cap) |
paul@492 | 84 | { |
paul@492 | 85 | _server = cap; |
paul@492 | 86 | } |
paul@492 | 87 | |
paul@492 | 88 | void ExternalPager::set_gate(l4_cap_idx_t cap) |
paul@492 | 89 | { |
paul@492 | 90 | _ipc_gate = cap; |
paul@492 | 91 | } |
paul@492 | 92 | |
paul@492 | 93 | |
paul@492 | 94 | |
paul@366 | 95 | /* Handle a general exception. */ |
paul@366 | 96 | |
paul@366 | 97 | long ExternalPager::exception(l4_exc_regs_t regs, l4_snd_fpage_t *region) |
paul@366 | 98 | { |
paul@366 | 99 | (void) region; |
paul@366 | 100 | |
paul@431 | 101 | printf("ExternalPager::exception(...) -> pfa = %lx, pc = %lx\n", l4_utcb_exc_pfa(®s), l4_utcb_exc_pc(®s)); |
paul@366 | 102 | |
paul@431 | 103 | #if DEBUG |
paul@376 | 104 | while (1) |
paul@376 | 105 | l4_sleep_forever(); |
paul@431 | 106 | #endif |
paul@376 | 107 | |
paul@366 | 108 | return L4_EOK; |
paul@366 | 109 | } |
paul@366 | 110 | |
paul@366 | 111 | /* Handle a page fault using any configured regions. */ |
paul@366 | 112 | |
paul@366 | 113 | long ExternalPager::page_fault(l4_umword_t pfa, l4_umword_t pc, l4_snd_fpage_t *region) |
paul@366 | 114 | { |
paul@366 | 115 | l4_umword_t addr = pfa & ~7UL, flags = pfa & 7; |
paul@366 | 116 | |
paul@366 | 117 | #if DEBUG |
paul@366 | 118 | printf("page_fault(%lx, %lx) -> %lx (%lx) -> ", pfa, pc, addr, flags); |
paul@366 | 119 | #endif |
paul@366 | 120 | |
paul@472 | 121 | /* Find the first region whose start address is beyond the fault address, |
paul@472 | 122 | testing if any immediately preceding region contains the fault address. */ |
paul@472 | 123 | |
paul@366 | 124 | MappedRegions::iterator it = _regions.upper_bound(addr); |
paul@366 | 125 | |
paul@366 | 126 | if (it != _regions.begin()) |
paul@366 | 127 | it--; |
paul@366 | 128 | else |
paul@366 | 129 | { |
paul@431 | 130 | printf("not mapped at %lx for pc %lx\n", addr, pc); |
paul@366 | 131 | return -L4_ENOMEM; |
paul@366 | 132 | } |
paul@366 | 133 | |
paul@472 | 134 | /* Obtain the region and test if it contains the fault address. */ |
paul@472 | 135 | |
paul@366 | 136 | MappedRegion &r = it->second; |
paul@366 | 137 | |
paul@431 | 138 | if ((addr >= r.start) && (addr < r.start + r.size)) |
paul@366 | 139 | { |
paul@366 | 140 | l4_addr_t page_addr = trunc(addr, L4_PAGESIZE); |
paul@472 | 141 | map_flags_t map_flags = map_flags_for_fault(flags); |
paul@376 | 142 | |
paul@431 | 143 | region->fpage = l4_fpage(r.ds_start + (page_addr - r.start), L4_PAGESHIFT, map_flags & r.flags); |
paul@366 | 144 | region->snd_base = page_addr; |
paul@366 | 145 | |
paul@366 | 146 | #if DEBUG |
paul@376 | 147 | printf("%lx...%lx from %lx...%lx offset %lx size %d rights %x ds %lx\n", |
paul@431 | 148 | r.start, region->snd_base, |
paul@431 | 149 | r.ds_start, l4_fpage_memaddr(region->fpage), |
paul@431 | 150 | addr - r.start, |
paul@366 | 151 | l4_fpage_size(region->fpage), |
paul@376 | 152 | l4_fpage_rights(region->fpage), |
paul@376 | 153 | r.ds); |
paul@366 | 154 | |
paul@366 | 155 | printf("%lx -> ", addr); |
paul@366 | 156 | |
paul@366 | 157 | for (unsigned int i = 0; i < sizeof(l4_umword_t); i++) |
paul@431 | 158 | printf("%02x", *((unsigned char *)(r.ds_start + (addr - r.start) + i))); |
paul@366 | 159 | |
paul@366 | 160 | printf("\n"); |
paul@366 | 161 | #endif |
paul@366 | 162 | |
paul@366 | 163 | if (r.flags & L4RE_RM_F_W) |
paul@431 | 164 | l4_touch_rw((const void *) (r.ds_start + (page_addr - r.start)), L4_PAGESIZE); |
paul@366 | 165 | else |
paul@431 | 166 | l4_touch_ro((const void *) (r.ds_start + (page_addr - r.start)), L4_PAGESIZE); |
paul@366 | 167 | |
paul@366 | 168 | return L4_EOK; |
paul@366 | 169 | } |
paul@366 | 170 | |
paul@366 | 171 | #if DEBUG |
paul@366 | 172 | printf("not mapped!\n"); |
paul@366 | 173 | #endif |
paul@366 | 174 | |
paul@366 | 175 | return -L4_ENOMEM; |
paul@366 | 176 | } |
paul@366 | 177 | |
paul@366 | 178 | /* Attach a region for provision when page faults occur. This is required in |
paul@366 | 179 | the initialisation of a program by the C library which requires a region |
paul@366 | 180 | mapper. */ |
paul@366 | 181 | |
paul@419 | 182 | long ExternalPager::attach(address_t *start, address_t size, map_flags_t flags, |
paul@366 | 183 | l4_cap_idx_t ds, address_t offset, |
paul@366 | 184 | unsigned char align) |
paul@366 | 185 | { |
paul@469 | 186 | long err = ExecPager::find(start, &size, flags, offset, align); |
paul@366 | 187 | |
paul@431 | 188 | if (!err) |
paul@431 | 189 | { |
paul@366 | 190 | /* Attach the provided dataspace. |
paul@472 | 191 | |
paul@472 | 192 | This is only done in this implementation to support the paging |
paul@472 | 193 | mechanism. In a region mapper residing within the actual task, the |
paul@472 | 194 | dataspace's map operation would be invoked to obtain mappings. */ |
paul@366 | 195 | |
paul@366 | 196 | l4_addr_t ds_start; |
paul@431 | 197 | err = ipc_attach_dataspace(ds, size, (void **) &ds_start); |
paul@366 | 198 | |
paul@366 | 199 | if (err) |
paul@366 | 200 | return err; |
paul@366 | 201 | |
paul@366 | 202 | l4_touch_rw((const void *) ds_start, size); |
paul@366 | 203 | |
paul@431 | 204 | MappedRegion r(*start, size, flags & L4RE_DS_F_RIGHTS_MASK, ds, ds_start); |
paul@366 | 205 | add(r); |
paul@366 | 206 | } |
paul@366 | 207 | |
paul@431 | 208 | return err; |
paul@366 | 209 | } |
paul@366 | 210 | |
paul@492 | 211 | |
paul@492 | 212 | |
paul@469 | 213 | /* Receive signals from a task. */ |
paul@469 | 214 | |
paul@469 | 215 | long ExternalPager::signal(unsigned long sig, unsigned long val) |
paul@469 | 216 | { |
paul@484 | 217 | notify_all(NOTIFY_TASK_SIGNAL, (notify_values_t) {sig, val}); |
paul@480 | 218 | |
paul@492 | 219 | /* Handle the termination event. */ |
paul@492 | 220 | |
paul@492 | 221 | if (sig == 0) |
paul@492 | 222 | { |
paul@492 | 223 | /* Once the program exits, the IPC gate connecting the program with its |
paul@492 | 224 | internal pager can be released. */ |
paul@492 | 225 | |
paul@492 | 226 | if (l4_is_valid_cap(_ipc_gate)) |
paul@492 | 227 | { |
paul@492 | 228 | l4_task_unmap(L4RE_THIS_TASK_CAP, l4_obj_fpage(_ipc_gate, 0, L4_CAP_FPAGE_RWSD), L4_FP_ALL_SPACES); |
paul@492 | 229 | _ipc_gate = L4_INVALID_CAP; |
paul@492 | 230 | } |
paul@492 | 231 | |
paul@492 | 232 | /* Upon exit of the program's internal pager, release the capabilities |
paul@492 | 233 | associated with this server. */ |
paul@492 | 234 | |
paul@492 | 235 | else if (l4_is_valid_cap(_task)) |
paul@492 | 236 | { |
paul@492 | 237 | /* NOTE: Capability indexes to be obtained from the process creation |
paul@492 | 238 | activity. */ |
paul@492 | 239 | |
paul@492 | 240 | l4_task_unmap(_task, l4_obj_fpage(0x13UL << L4_CAP_SHIFT, 0, L4_CAP_FPAGE_RWSD), L4_FP_ALL_SPACES); |
paul@492 | 241 | l4_task_unmap(_task, l4_obj_fpage(0x19UL << L4_CAP_SHIFT, 0, L4_CAP_FPAGE_RWSD), L4_FP_ALL_SPACES); |
paul@492 | 242 | } |
paul@492 | 243 | } |
paul@492 | 244 | |
paul@469 | 245 | return L4_EOK; |
paul@469 | 246 | } |
paul@469 | 247 | |
paul@492 | 248 | |
paul@492 | 249 | |
paul@485 | 250 | /* Subscribe to notifications. */ |
paul@485 | 251 | |
paul@485 | 252 | long ExternalPager::subscribe(l4_cap_idx_t notifier, notify_flags_t flags) |
paul@485 | 253 | { |
paul@485 | 254 | return NotificationSupport::subscribe(notifier, flags); |
paul@485 | 255 | } |
paul@485 | 256 | |
paul@485 | 257 | /* Unsubscribe from notifications. */ |
paul@485 | 258 | |
paul@488 | 259 | long ExternalPager::unsubscribe(l4_cap_idx_t notifier) |
paul@485 | 260 | { |
paul@488 | 261 | return NotificationSupport::unsubscribe(notifier); |
paul@485 | 262 | } |
paul@485 | 263 | |
paul@366 | 264 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@366 | 265 | */ |