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