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@499 | 71 | printf("Free %lx\n", r.ds); |
paul@499 | 72 | ipc_cap_free_um(r.ds); |
paul@499 | 73 | } |
paul@499 | 74 | |
paul@499 | 75 | if (l4_is_valid_cap(_task)) |
paul@499 | 76 | { |
paul@499 | 77 | ipc_cap_free_um(_task); |
paul@499 | 78 | _task = L4_INVALID_CAP; |
paul@492 | 79 | } |
paul@489 | 80 | } |
paul@489 | 81 | |
paul@492 | 82 | |
paul@492 | 83 | |
paul@492 | 84 | /* Manage the task and thread capabilities. */ |
paul@492 | 85 | |
paul@499 | 86 | void ExternalPager::add_thread(l4_cap_idx_t cap, l4_cap_idx_t rcap) |
paul@492 | 87 | { |
paul@499 | 88 | _threads.push_back(cap); |
paul@499 | 89 | _rthreads.push_back(rcap); |
paul@492 | 90 | } |
paul@492 | 91 | |
paul@492 | 92 | void ExternalPager::set_gate(l4_cap_idx_t cap) |
paul@492 | 93 | { |
paul@492 | 94 | _ipc_gate = cap; |
paul@492 | 95 | } |
paul@492 | 96 | |
paul@499 | 97 | void ExternalPager::set_pager(l4_cap_idx_t cap, l4_cap_idx_t rcap) |
paul@499 | 98 | { |
paul@499 | 99 | _pager = cap; |
paul@499 | 100 | _rpager = rcap; |
paul@499 | 101 | } |
paul@499 | 102 | |
paul@499 | 103 | void ExternalPager::set_parent(l4_cap_idx_t cap, l4_cap_idx_t rcap) |
paul@499 | 104 | { |
paul@499 | 105 | _parent = cap; |
paul@499 | 106 | _rparent = rcap; |
paul@499 | 107 | } |
paul@499 | 108 | |
paul@499 | 109 | void ExternalPager::set_task(l4_cap_idx_t cap) |
paul@499 | 110 | { |
paul@499 | 111 | _task = cap; |
paul@499 | 112 | } |
paul@499 | 113 | |
paul@492 | 114 | |
paul@492 | 115 | |
paul@366 | 116 | /* Handle a general exception. */ |
paul@366 | 117 | |
paul@366 | 118 | long ExternalPager::exception(l4_exc_regs_t regs, l4_snd_fpage_t *region) |
paul@366 | 119 | { |
paul@366 | 120 | (void) region; |
paul@366 | 121 | |
paul@431 | 122 | printf("ExternalPager::exception(...) -> pfa = %lx, pc = %lx\n", l4_utcb_exc_pfa(®s), l4_utcb_exc_pc(®s)); |
paul@366 | 123 | |
paul@431 | 124 | #if DEBUG |
paul@376 | 125 | while (1) |
paul@376 | 126 | l4_sleep_forever(); |
paul@431 | 127 | #endif |
paul@376 | 128 | |
paul@366 | 129 | return L4_EOK; |
paul@366 | 130 | } |
paul@366 | 131 | |
paul@366 | 132 | /* Handle a page fault using any configured regions. */ |
paul@366 | 133 | |
paul@366 | 134 | long ExternalPager::page_fault(l4_umword_t pfa, l4_umword_t pc, l4_snd_fpage_t *region) |
paul@366 | 135 | { |
paul@366 | 136 | l4_umword_t addr = pfa & ~7UL, flags = pfa & 7; |
paul@366 | 137 | |
paul@366 | 138 | #if DEBUG |
paul@366 | 139 | printf("page_fault(%lx, %lx) -> %lx (%lx) -> ", pfa, pc, addr, flags); |
paul@366 | 140 | #endif |
paul@366 | 141 | |
paul@472 | 142 | /* Find the first region whose start address is beyond the fault address, |
paul@472 | 143 | testing if any immediately preceding region contains the fault address. */ |
paul@472 | 144 | |
paul@366 | 145 | MappedRegions::iterator it = _regions.upper_bound(addr); |
paul@366 | 146 | |
paul@366 | 147 | if (it != _regions.begin()) |
paul@366 | 148 | it--; |
paul@366 | 149 | else |
paul@366 | 150 | { |
paul@431 | 151 | printf("not mapped at %lx for pc %lx\n", addr, pc); |
paul@366 | 152 | return -L4_ENOMEM; |
paul@366 | 153 | } |
paul@366 | 154 | |
paul@472 | 155 | /* Obtain the region and test if it contains the fault address. */ |
paul@472 | 156 | |
paul@366 | 157 | MappedRegion &r = it->second; |
paul@366 | 158 | |
paul@431 | 159 | if ((addr >= r.start) && (addr < r.start + r.size)) |
paul@366 | 160 | { |
paul@366 | 161 | l4_addr_t page_addr = trunc(addr, L4_PAGESIZE); |
paul@472 | 162 | map_flags_t map_flags = map_flags_for_fault(flags); |
paul@376 | 163 | |
paul@431 | 164 | region->fpage = l4_fpage(r.ds_start + (page_addr - r.start), L4_PAGESHIFT, map_flags & r.flags); |
paul@366 | 165 | region->snd_base = page_addr; |
paul@366 | 166 | |
paul@366 | 167 | #if DEBUG |
paul@376 | 168 | printf("%lx...%lx from %lx...%lx offset %lx size %d rights %x ds %lx\n", |
paul@431 | 169 | r.start, region->snd_base, |
paul@431 | 170 | r.ds_start, l4_fpage_memaddr(region->fpage), |
paul@431 | 171 | addr - r.start, |
paul@366 | 172 | l4_fpage_size(region->fpage), |
paul@376 | 173 | l4_fpage_rights(region->fpage), |
paul@376 | 174 | r.ds); |
paul@366 | 175 | |
paul@366 | 176 | printf("%lx -> ", addr); |
paul@366 | 177 | |
paul@366 | 178 | for (unsigned int i = 0; i < sizeof(l4_umword_t); i++) |
paul@431 | 179 | printf("%02x", *((unsigned char *)(r.ds_start + (addr - r.start) + i))); |
paul@366 | 180 | |
paul@366 | 181 | printf("\n"); |
paul@366 | 182 | #endif |
paul@366 | 183 | |
paul@366 | 184 | if (r.flags & L4RE_RM_F_W) |
paul@431 | 185 | l4_touch_rw((const void *) (r.ds_start + (page_addr - r.start)), L4_PAGESIZE); |
paul@366 | 186 | else |
paul@431 | 187 | l4_touch_ro((const void *) (r.ds_start + (page_addr - r.start)), L4_PAGESIZE); |
paul@366 | 188 | |
paul@366 | 189 | return L4_EOK; |
paul@366 | 190 | } |
paul@366 | 191 | |
paul@366 | 192 | #if DEBUG |
paul@366 | 193 | printf("not mapped!\n"); |
paul@366 | 194 | #endif |
paul@366 | 195 | |
paul@366 | 196 | return -L4_ENOMEM; |
paul@366 | 197 | } |
paul@366 | 198 | |
paul@366 | 199 | /* Attach a region for provision when page faults occur. This is required in |
paul@366 | 200 | the initialisation of a program by the C library which requires a region |
paul@366 | 201 | mapper. */ |
paul@366 | 202 | |
paul@419 | 203 | long ExternalPager::attach(address_t *start, address_t size, map_flags_t flags, |
paul@366 | 204 | l4_cap_idx_t ds, address_t offset, |
paul@366 | 205 | unsigned char align) |
paul@366 | 206 | { |
paul@469 | 207 | long err = ExecPager::find(start, &size, flags, offset, align); |
paul@366 | 208 | |
paul@499 | 209 | /* Without an error, attach the dataspace. */ |
paul@499 | 210 | |
paul@431 | 211 | if (!err) |
paul@431 | 212 | { |
paul@499 | 213 | /* Attach the provided dataspace within this task. |
paul@472 | 214 | |
paul@472 | 215 | This is only done in this implementation to support the paging |
paul@472 | 216 | mechanism. In a region mapper residing within the actual task, the |
paul@472 | 217 | dataspace's map operation would be invoked to obtain mappings. */ |
paul@366 | 218 | |
paul@366 | 219 | l4_addr_t ds_start; |
paul@431 | 220 | err = ipc_attach_dataspace(ds, size, (void **) &ds_start); |
paul@366 | 221 | |
paul@366 | 222 | if (err) |
paul@366 | 223 | return err; |
paul@366 | 224 | |
paul@366 | 225 | l4_touch_rw((const void *) ds_start, size); |
paul@366 | 226 | |
paul@431 | 227 | MappedRegion r(*start, size, flags & L4RE_DS_F_RIGHTS_MASK, ds, ds_start); |
paul@366 | 228 | add(r); |
paul@366 | 229 | } |
paul@366 | 230 | |
paul@499 | 231 | /* Discard the imported dataspace if its region cannot be accommodated. */ |
paul@499 | 232 | |
paul@499 | 233 | else |
paul@499 | 234 | ipc_cap_free_um(ds); |
paul@499 | 235 | |
paul@431 | 236 | return err; |
paul@366 | 237 | } |
paul@366 | 238 | |
paul@492 | 239 | |
paul@492 | 240 | |
paul@469 | 241 | /* Receive signals from a task. */ |
paul@469 | 242 | |
paul@469 | 243 | long ExternalPager::signal(unsigned long sig, unsigned long val) |
paul@469 | 244 | { |
paul@484 | 245 | notify_all(NOTIFY_TASK_SIGNAL, (notify_values_t) {sig, val}); |
paul@480 | 246 | |
paul@492 | 247 | /* Handle the termination event. */ |
paul@492 | 248 | |
paul@492 | 249 | if (sig == 0) |
paul@492 | 250 | { |
paul@492 | 251 | /* Once the program exits, the IPC gate connecting the program with its |
paul@492 | 252 | internal pager can be released. */ |
paul@492 | 253 | |
paul@492 | 254 | if (l4_is_valid_cap(_ipc_gate)) |
paul@492 | 255 | { |
paul@492 | 256 | l4_task_unmap(L4RE_THIS_TASK_CAP, l4_obj_fpage(_ipc_gate, 0, L4_CAP_FPAGE_RWSD), L4_FP_ALL_SPACES); |
paul@492 | 257 | _ipc_gate = L4_INVALID_CAP; |
paul@492 | 258 | } |
paul@492 | 259 | |
paul@492 | 260 | /* Upon exit of the program's internal pager, release the capabilities |
paul@492 | 261 | associated with this server. */ |
paul@492 | 262 | |
paul@492 | 263 | else if (l4_is_valid_cap(_task)) |
paul@492 | 264 | { |
paul@492 | 265 | /* NOTE: Capability indexes to be obtained from the process creation |
paul@492 | 266 | activity. */ |
paul@492 | 267 | |
paul@499 | 268 | /* Log. */ |
paul@499 | 269 | |
paul@499 | 270 | l4_task_unmap(_task, l4_obj_fpage(0x05UL << L4_CAP_SHIFT, 0, L4_CAP_FPAGE_RWSD), L4_FP_ALL_SPACES); |
paul@499 | 271 | |
paul@493 | 272 | /* Parent and pager/region mapper. */ |
paul@493 | 273 | |
paul@499 | 274 | l4_task_unmap(_task, l4_obj_fpage(_rparent, 0, L4_CAP_FPAGE_RWSD), L4_FP_ALL_SPACES); |
paul@499 | 275 | l4_task_unmap(_task, l4_obj_fpage(_rpager, 0, L4_CAP_FPAGE_RWSD), L4_FP_ALL_SPACES); |
paul@499 | 276 | |
paul@499 | 277 | /* Threads. For some reason, these cannot be released by the process, so |
paul@499 | 278 | they are also unmapped on its behalf. */ |
paul@499 | 279 | |
paul@499 | 280 | std::vector<l4_cap_idx_t>::iterator it; |
paul@499 | 281 | |
paul@499 | 282 | for (it = _threads.begin(); it != _threads.end(); it++) |
paul@499 | 283 | ipc_cap_free_um(*it); |
paul@499 | 284 | |
paul@499 | 285 | for (it = _rthreads.begin(); it != _rthreads.end(); it++) |
paul@499 | 286 | l4_task_unmap(_task, l4_obj_fpage(*it, 0, L4_CAP_FPAGE_RWSD), L4_FP_ALL_SPACES); |
paul@492 | 287 | } |
paul@492 | 288 | } |
paul@492 | 289 | |
paul@469 | 290 | return L4_EOK; |
paul@469 | 291 | } |
paul@469 | 292 | |
paul@492 | 293 | |
paul@492 | 294 | |
paul@485 | 295 | /* Subscribe to notifications. */ |
paul@485 | 296 | |
paul@485 | 297 | long ExternalPager::subscribe(l4_cap_idx_t notifier, notify_flags_t flags) |
paul@485 | 298 | { |
paul@485 | 299 | return NotificationSupport::subscribe(notifier, flags); |
paul@485 | 300 | } |
paul@485 | 301 | |
paul@485 | 302 | /* Unsubscribe from notifications. */ |
paul@485 | 303 | |
paul@488 | 304 | long ExternalPager::unsubscribe(l4_cap_idx_t notifier) |
paul@485 | 305 | { |
paul@488 | 306 | return NotificationSupport::unsubscribe(notifier); |
paul@485 | 307 | } |
paul@485 | 308 | |
paul@366 | 309 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@366 | 310 | */ |