paul@308 | 1 | /* |
paul@308 | 2 | * Support for executing code in new tasks and threads. |
paul@308 | 3 | * |
paul@308 | 4 | * Copyright (C) 2022 Paul Boddie <paul@boddie.org.uk> |
paul@308 | 5 | * |
paul@308 | 6 | * This program is free software; you can redistribute it and/or |
paul@308 | 7 | * modify it under the terms of the GNU General Public License as |
paul@308 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@308 | 9 | * the License, or (at your option) any later version. |
paul@308 | 10 | * |
paul@308 | 11 | * This program is distributed in the hope that it will be useful, |
paul@308 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@308 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@308 | 14 | * GNU General Public License for more details. |
paul@308 | 15 | * |
paul@308 | 16 | * You should have received a copy of the GNU General Public License |
paul@308 | 17 | * along with this program; if not, write to the Free Software |
paul@308 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@308 | 19 | * Boston, MA 02110-1301, USA |
paul@308 | 20 | */ |
paul@308 | 21 | |
paul@308 | 22 | #include <l4/re/env.h> |
paul@308 | 23 | #include <l4/sys/err.h> |
paul@312 | 24 | #include <l4/util/util.h> |
paul@308 | 25 | |
paul@327 | 26 | #include <exec/elf.h> |
paul@349 | 27 | #include <exec/memory.h> |
paul@324 | 28 | #include <exec/process.h> |
paul@308 | 29 | #include <ipc/mem_ipc.h> |
paul@308 | 30 | #include <ipc/server.h> |
paul@311 | 31 | #include <mem/memory_utils.h> |
paul@308 | 32 | |
paul@312 | 33 | #include <map> |
paul@312 | 34 | |
paul@308 | 35 | #include <stdio.h> |
paul@308 | 36 | #include <stdlib.h> |
paul@308 | 37 | #include <string.h> |
paul@308 | 38 | |
paul@308 | 39 | #include <pthread-l4.h> |
paul@308 | 40 | #include <pthread.h> |
paul@308 | 41 | |
paul@312 | 42 | #include "pager_object_interface.h" |
paul@312 | 43 | #include "pager_object_server.h" |
paul@308 | 44 | |
paul@308 | 45 | |
paul@308 | 46 | |
paul@312 | 47 | /* A simple system pager also acting as a region mapper. */ |
paul@312 | 48 | |
paul@320 | 49 | typedef std::map<l4_addr_t, MappedRegion> MappedRegions; |
paul@316 | 50 | |
paul@312 | 51 | class ExecPager : public PagerObject |
paul@312 | 52 | { |
paul@312 | 53 | protected: |
paul@320 | 54 | MappedRegions _regions; |
paul@312 | 55 | |
paul@312 | 56 | public: |
paul@320 | 57 | virtual void add(MappedRegion region) |
paul@312 | 58 | { |
paul@312 | 59 | _regions[region.map_start] = region; |
paul@312 | 60 | } |
paul@312 | 61 | |
paul@312 | 62 | /* Notification methods. */ |
paul@308 | 63 | |
paul@308 | 64 | virtual long exception(l4_exc_regs_t regs, |
paul@308 | 65 | l4_snd_fpage_t *region); |
paul@308 | 66 | |
paul@308 | 67 | virtual long page_fault(l4_umword_t pfa, l4_umword_t pc, |
paul@308 | 68 | l4_snd_fpage_t *region); |
paul@312 | 69 | |
paul@312 | 70 | /* Region manager/mapper methods. */ |
paul@312 | 71 | |
paul@312 | 72 | virtual long attach(address_t *start, offset_t size, map_flags_t flags, |
paul@312 | 73 | l4_cap_idx_t ds, address_t offset, unsigned char align); |
paul@312 | 74 | |
paul@308 | 75 | }; |
paul@308 | 76 | |
paul@312 | 77 | /* Handle a general exception. */ |
paul@312 | 78 | |
paul@308 | 79 | long ExecPager::exception(l4_exc_regs_t regs, l4_snd_fpage_t *region) |
paul@308 | 80 | { |
paul@308 | 81 | (void) region; |
paul@308 | 82 | |
paul@308 | 83 | printf("exception(...) -> pfa = %lx, pc = %lx\n", l4_utcb_exc_pfa(®s), l4_utcb_exc_pc(®s)); |
paul@312 | 84 | |
paul@312 | 85 | printf("r15 = %lx\n", regs.r15); |
paul@312 | 86 | printf("r14 = %lx\n", regs.r14); |
paul@312 | 87 | printf("r13 = %lx\n", regs.r13); |
paul@312 | 88 | printf("r12 = %lx\n", regs.r12); |
paul@312 | 89 | printf("r11 = %lx\n", regs.r11); |
paul@312 | 90 | printf("r10 = %lx\n", regs.r10); |
paul@312 | 91 | printf("r9 = %lx\n", regs.r9); |
paul@312 | 92 | printf("r8 = %lx\n", regs.r8); |
paul@312 | 93 | printf("rdi = %lx\n", regs.rdi); |
paul@312 | 94 | printf("rsi = %lx\n", regs.rsi); |
paul@312 | 95 | printf("rbp = %lx\n", regs.rbp); |
paul@312 | 96 | printf("pfa = %lx\n", regs.pfa); |
paul@312 | 97 | printf("rbx = %lx\n", regs.rbx); |
paul@312 | 98 | printf("rdx = %lx\n", regs.rdx); |
paul@312 | 99 | printf("rcx = %lx\n", regs.rcx); |
paul@312 | 100 | printf("rax = %lx\n", regs.rax); |
paul@312 | 101 | printf("trapno = %lx\n", regs.trapno); |
paul@312 | 102 | printf("err = %lx\n", regs.err); |
paul@312 | 103 | printf("ip = %lx\n", regs.ip); |
paul@312 | 104 | printf("flags = %lx\n", regs.flags); |
paul@312 | 105 | printf("sp = %lx\n", regs.sp); |
paul@312 | 106 | printf("ss = %lx\n", regs.ss); |
paul@312 | 107 | printf("fs_base = %lx\n", regs.fs_base); |
paul@312 | 108 | printf("gs_base = %lx\n", regs.gs_base); |
paul@312 | 109 | |
paul@308 | 110 | return L4_EOK; |
paul@308 | 111 | } |
paul@308 | 112 | |
paul@315 | 113 | #define DEBUG 0 |
paul@315 | 114 | |
paul@312 | 115 | /* Handle a page fault using any configured regions. */ |
paul@312 | 116 | |
paul@308 | 117 | long ExecPager::page_fault(l4_umword_t pfa, l4_umword_t pc, l4_snd_fpage_t *region) |
paul@308 | 118 | { |
paul@308 | 119 | l4_umword_t addr = pfa & ~7UL, flags = pfa & 7; |
paul@308 | 120 | |
paul@315 | 121 | #if DEBUG |
paul@312 | 122 | printf("page_fault(%lx, %lx) -> %lx (%lx) -> ", pfa, pc, addr, flags); |
paul@312 | 123 | #endif |
paul@312 | 124 | |
paul@320 | 125 | MappedRegions::iterator it = _regions.upper_bound(addr); |
paul@312 | 126 | |
paul@312 | 127 | if (it != _regions.begin()) |
paul@312 | 128 | it--; |
paul@312 | 129 | else |
paul@312 | 130 | { |
paul@312 | 131 | printf("not mapped!\n"); |
paul@312 | 132 | return -L4_ENOMEM; |
paul@312 | 133 | } |
paul@312 | 134 | |
paul@320 | 135 | MappedRegion &r = it->second; |
paul@308 | 136 | |
paul@329 | 137 | if ((addr >= r.map_start) && (addr < r.map_start + r.size)) |
paul@308 | 138 | { |
paul@312 | 139 | l4_addr_t page_addr = trunc(addr, L4_PAGESIZE); |
paul@312 | 140 | |
paul@312 | 141 | region->fpage = l4_fpage(r.start + (page_addr - r.map_start), L4_PAGESHIFT, r.flags); |
paul@312 | 142 | region->snd_base = page_addr; |
paul@312 | 143 | |
paul@315 | 144 | #if DEBUG |
paul@329 | 145 | printf("%lx...%lx from %lx...%lx offset %lx size %d rights %x\n", |
paul@312 | 146 | r.map_start, region->snd_base, |
paul@312 | 147 | r.start, l4_fpage_memaddr(region->fpage), |
paul@329 | 148 | addr - r.map_start, |
paul@312 | 149 | l4_fpage_size(region->fpage), |
paul@312 | 150 | l4_fpage_rights(region->fpage)); |
paul@336 | 151 | |
paul@312 | 152 | printf("%lx -> ", addr); |
paul@312 | 153 | |
paul@312 | 154 | for (unsigned int i = 0; i < sizeof(l4_umword_t); i++) |
paul@312 | 155 | printf("%02x", *((unsigned char *)(r.start + (addr - r.map_start) + i))); |
paul@312 | 156 | |
paul@312 | 157 | printf("\n"); |
paul@312 | 158 | #endif |
paul@308 | 159 | |
paul@329 | 160 | if (r.flags & L4RE_RM_F_W) |
paul@329 | 161 | l4_touch_rw((const void *) (r.start + (page_addr - r.map_start)), L4_PAGESIZE); |
paul@329 | 162 | else |
paul@329 | 163 | l4_touch_ro((const void *) (r.start + (page_addr - r.map_start)), L4_PAGESIZE); |
paul@329 | 164 | |
paul@308 | 165 | return L4_EOK; |
paul@308 | 166 | } |
paul@308 | 167 | |
paul@315 | 168 | #if DEBUG |
paul@312 | 169 | printf("not mapped!\n"); |
paul@312 | 170 | #endif |
paul@312 | 171 | |
paul@308 | 172 | return -L4_ENOMEM; |
paul@308 | 173 | } |
paul@308 | 174 | |
paul@312 | 175 | /* Attach a region for provision when page faults occur. This is required in |
paul@312 | 176 | the initialisation of a program by the C library which requires a region |
paul@312 | 177 | mapper. */ |
paul@312 | 178 | |
paul@312 | 179 | long ExecPager::attach(address_t *start, offset_t size, map_flags_t flags, |
paul@312 | 180 | l4_cap_idx_t ds, address_t offset, unsigned char align) |
paul@312 | 181 | { |
paul@315 | 182 | #if DEBUG |
paul@312 | 183 | printf("attach(%lx, %ld, %lx, ..., %lx, %d)\n", *start, size, flags, offset, align); |
paul@312 | 184 | #endif |
paul@312 | 185 | |
paul@312 | 186 | if (align < L4_PAGESHIFT) |
paul@312 | 187 | align = L4_PAGESHIFT; |
paul@312 | 188 | |
paul@312 | 189 | offset_t increment = 1UL << align; |
paul@312 | 190 | offset_t region_size = round(size, increment); |
paul@312 | 191 | |
paul@312 | 192 | /* Either attempt to find an address for the specified region, starting from |
paul@312 | 193 | any indicated address. */ |
paul@312 | 194 | |
paul@312 | 195 | if (flags & L4RE_RM_F_SEARCH_ADDR) |
paul@312 | 196 | { |
paul@312 | 197 | address_t region_start = trunc(*start, increment); |
paul@320 | 198 | MappedRegions::iterator it = _regions.upper_bound(*start); |
paul@312 | 199 | |
paul@312 | 200 | if (!region_start) |
paul@312 | 201 | region_start += increment; |
paul@312 | 202 | |
paul@315 | 203 | #if DEBUG |
paul@312 | 204 | printf("-> search from %lx -> %lx...\n", *start, region_start); |
paul@312 | 205 | #endif |
paul@312 | 206 | |
paul@312 | 207 | /* Before last known region. */ |
paul@312 | 208 | |
paul@312 | 209 | while (it != _regions.end()) |
paul@312 | 210 | { |
paul@320 | 211 | MappedRegions::iterator next = it; |
paul@320 | 212 | MappedRegion &r = it->second; |
paul@312 | 213 | address_t start_limit; |
paul@312 | 214 | address_t end_limit = r.map_start; |
paul@312 | 215 | |
paul@312 | 216 | /* Consider any preceding region. If no such region exists, choose an |
paul@312 | 217 | address at the start of memory. */ |
paul@312 | 218 | |
paul@312 | 219 | if (it == _regions.begin()) |
paul@312 | 220 | start_limit = L4_PAGESIZE; |
paul@312 | 221 | else |
paul@312 | 222 | { |
paul@312 | 223 | it--; |
paul@320 | 224 | MappedRegion &pr = it->second; |
paul@329 | 225 | start_limit = pr.map_start + pr.size; |
paul@312 | 226 | it = next; |
paul@312 | 227 | } |
paul@312 | 228 | |
paul@312 | 229 | /* Test against the limits. */ |
paul@312 | 230 | |
paul@312 | 231 | if (region_start < start_limit) |
paul@312 | 232 | region_start = round(start_limit, increment); |
paul@312 | 233 | |
paul@312 | 234 | /* Investigate subsequent regions if not enough space exists between the |
paul@312 | 235 | preceding region (or start of memory) and the current region. */ |
paul@312 | 236 | |
paul@312 | 237 | if ((region_start + region_size) > end_limit) |
paul@312 | 238 | { |
paul@312 | 239 | it++; |
paul@312 | 240 | if (it == _regions.end()) |
paul@312 | 241 | return -L4_ENOMEM; |
paul@312 | 242 | } |
paul@312 | 243 | else |
paul@312 | 244 | break; |
paul@312 | 245 | } |
paul@312 | 246 | |
paul@312 | 247 | /* Attach the provided dataspace. |
paul@312 | 248 | NOTE: This is only done in this implementation to support the paging |
paul@312 | 249 | mechanism. In a region mapper residing within the actual task, the |
paul@312 | 250 | dataspace's map operation would be invoked to obtain mappings. */ |
paul@312 | 251 | |
paul@312 | 252 | l4_addr_t ds_start; |
paul@312 | 253 | |
paul@312 | 254 | long err = ipc_attach_dataspace(ds, size, (void **) &ds_start); |
paul@312 | 255 | |
paul@312 | 256 | if (err) |
paul@312 | 257 | return err; |
paul@312 | 258 | |
paul@312 | 259 | l4_touch_rw((const void *) ds_start, size); |
paul@312 | 260 | |
paul@315 | 261 | #if DEBUG |
paul@312 | 262 | printf("-> added region for %lx size %ld (%d)\n", region_start, region_size, page_order(region_size)); |
paul@312 | 263 | #endif |
paul@312 | 264 | |
paul@329 | 265 | add(MappedRegion(ds_start, region_size, flags & L4RE_DS_F_RIGHTS_MASK, region_start)); |
paul@312 | 266 | |
paul@312 | 267 | *start = region_start; |
paul@312 | 268 | return L4_EOK; |
paul@312 | 269 | } |
paul@312 | 270 | |
paul@312 | 271 | /* Or attempt to add the specified region at a specific address. */ |
paul@312 | 272 | |
paul@312 | 273 | else |
paul@312 | 274 | { |
paul@312 | 275 | // NOTE: To be implemented. |
paul@312 | 276 | |
paul@336 | 277 | #if DEBUG |
paul@336 | 278 | printf("-> region of size %ld (%d) not added!\n", region_size, page_order(region_size)); |
paul@336 | 279 | #endif |
paul@336 | 280 | |
paul@312 | 281 | return -L4_ENOMEM; |
paul@312 | 282 | } |
paul@312 | 283 | } |
paul@312 | 284 | |
paul@308 | 285 | |
paul@308 | 286 | |
paul@308 | 287 | static ExecPager exec_pager; |
paul@308 | 288 | |
paul@308 | 289 | |
paul@308 | 290 | |
paul@308 | 291 | int main(int argc, char *argv[]) |
paul@308 | 292 | { |
paul@308 | 293 | long err; |
paul@308 | 294 | |
paul@308 | 295 | if (argc < 2) |
paul@308 | 296 | { |
paul@308 | 297 | printf("Need a program to run.\n"); |
paul@308 | 298 | return 1; |
paul@308 | 299 | } |
paul@308 | 300 | |
paul@349 | 301 | /* Initialise the memory of the new task. */ |
paul@323 | 302 | |
paul@349 | 303 | offset_t initial_stack_size = 16 * L4_PAGESIZE; |
paul@349 | 304 | ExplicitSegment stack(Utcb_area_start - initial_stack_size, initial_stack_size, L4_FPAGE_RW); |
paul@349 | 305 | Payload *payload; |
paul@323 | 306 | |
paul@349 | 307 | if (exec_get_payload(argv[1], &payload)) |
paul@323 | 308 | { |
paul@349 | 309 | printf("Could not initialise program.\n"); |
paul@325 | 310 | return 1; |
paul@325 | 311 | } |
paul@325 | 312 | |
paul@349 | 313 | if (stack.allocate()) |
paul@316 | 314 | { |
paul@349 | 315 | printf("Could not allocate stack.\n"); |
paul@349 | 316 | return 1; |
paul@308 | 317 | } |
paul@308 | 318 | |
paul@354 | 319 | /* Initialise pager regions. */ |
paul@312 | 320 | |
paul@349 | 321 | for (unsigned int i = 0; i < payload->segments(); i++) |
paul@325 | 322 | { |
paul@349 | 323 | if (payload->segment(i)->loadable()) |
paul@349 | 324 | exec_pager.add(payload->segment(i)->region()); |
paul@349 | 325 | } |
paul@325 | 326 | |
paul@349 | 327 | exec_pager.add(stack.region()); |
paul@308 | 328 | |
paul@354 | 329 | /* Start the pager. */ |
paul@354 | 330 | |
paul@354 | 331 | pthread_t pager_thread; |
paul@354 | 332 | pthread_attr_t attr; |
paul@354 | 333 | |
paul@354 | 334 | pthread_attr_init(&attr); |
paul@354 | 335 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
paul@354 | 336 | |
paul@354 | 337 | ipc_server_config_type config; |
paul@354 | 338 | ipc_server_init_for(&config, PagerObject, &exec_pager); |
paul@354 | 339 | |
paul@308 | 340 | err = pthread_create(&pager_thread, &attr, ipc_server_start_mainloop, &config); |
paul@308 | 341 | |
paul@308 | 342 | if (err) |
paul@308 | 343 | { |
paul@308 | 344 | printf("Could not start pager thread.\n"); |
paul@308 | 345 | return 1; |
paul@308 | 346 | } |
paul@308 | 347 | |
paul@354 | 348 | printf("Starting pager thread...\n"); |
paul@354 | 349 | err = ipc_server_start_config_thread(&config, pthread_l4_cap(pager_thread)); |
paul@308 | 350 | |
paul@308 | 351 | if (err) |
paul@308 | 352 | { |
paul@308 | 353 | printf("Could not start pager.\n"); |
paul@308 | 354 | return 1; |
paul@308 | 355 | } |
paul@308 | 356 | |
paul@319 | 357 | /* Configure the environment for the task, specifying the pager (and exception |
paul@319 | 358 | handler plus region mapper). */ |
paul@308 | 359 | |
paul@321 | 360 | Process process; |
paul@321 | 361 | |
paul@321 | 362 | err = process.configure(config.server); |
paul@308 | 363 | |
paul@308 | 364 | if (err) |
paul@308 | 365 | { |
paul@321 | 366 | printf("Could not configure task.\n"); |
paul@308 | 367 | return 1; |
paul@308 | 368 | } |
paul@308 | 369 | |
paul@321 | 370 | /* Populate a thread stack with argument and environment details. */ |
paul@321 | 371 | |
paul@321 | 372 | Stack st(stack); |
paul@321 | 373 | |
paul@321 | 374 | /* NOTE: Environment vector is currently not defined. */ |
paul@321 | 375 | |
paul@321 | 376 | char *envp[] = {NULL}; |
paul@321 | 377 | |
paul@321 | 378 | st.populate(argc - 1, argv + 1, envp); |
paul@321 | 379 | |
paul@321 | 380 | /* Start the new thread in the given stack. */ |
paul@308 | 381 | |
paul@308 | 382 | printf("Run thread...\n"); |
paul@308 | 383 | |
paul@349 | 384 | err = process.thread_start(payload->entry_point(), st); |
paul@308 | 385 | |
paul@308 | 386 | if (err) |
paul@308 | 387 | { |
paul@308 | 388 | printf("Could not run thread.\n"); |
paul@308 | 389 | return 1; |
paul@308 | 390 | } |
paul@308 | 391 | |
paul@308 | 392 | printf("Finished.\n"); |
paul@308 | 393 | while (1); |
paul@308 | 394 | |
paul@308 | 395 | return 0; |
paul@308 | 396 | } |
paul@308 | 397 | |
paul@308 | 398 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@308 | 399 | */ |