paul@367 | 1 | /* |
paul@367 | 2 | * A region mapper for deployment in a new task. |
paul@367 | 3 | * |
paul@595 | 4 | * Copyright (C) 2022, 2023, 2024 Paul Boddie <paul@boddie.org.uk> |
paul@367 | 5 | * |
paul@367 | 6 | * This program is free software; you can redistribute it and/or |
paul@367 | 7 | * modify it under the terms of the GNU General Public License as |
paul@367 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@367 | 9 | * the License, or (at your option) any later version. |
paul@367 | 10 | * |
paul@367 | 11 | * This program is distributed in the hope that it will be useful, |
paul@367 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@367 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@367 | 14 | * GNU General Public License for more details. |
paul@367 | 15 | * |
paul@367 | 16 | * You should have received a copy of the GNU General Public License |
paul@367 | 17 | * along with this program; if not, write to the Free Software |
paul@367 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@367 | 19 | * Boston, MA 02110-1301, USA |
paul@367 | 20 | */ |
paul@367 | 21 | |
paul@367 | 22 | #include <l4/re/env.h> |
paul@493 | 23 | #include <l4/sys/err.h> |
paul@367 | 24 | |
paul@523 | 25 | #include <stdio.h> |
paul@523 | 26 | #include <string.h> |
paul@523 | 27 | |
paul@367 | 28 | #include <exec/common.h> |
paul@367 | 29 | #include <exec/internal_pager.h> |
paul@367 | 30 | #include <exec/stack.h> |
paul@535 | 31 | #include <resource/resource_server.h> |
paul@367 | 32 | |
paul@367 | 33 | |
paul@367 | 34 | |
paul@431 | 35 | /* Initialise a pager for programs, reserving space below the allocated regions |
paul@431 | 36 | for the region mapper itself. */ |
paul@431 | 37 | |
paul@595 | 38 | static InternalPager exec_pager(MEM_INTERNAL_PAGER_LIMIT); |
paul@367 | 39 | |
paul@367 | 40 | |
paul@367 | 41 | |
paul@367 | 42 | int main(int argc, char *argv[]) |
paul@367 | 43 | { |
paul@523 | 44 | int debug = (argc > 1) && !strcmp(argv[1], "-v"); |
paul@523 | 45 | |
paul@367 | 46 | /* Initialise pager regions from region descriptions obtained via the |
paul@367 | 47 | auxiliary data. */ |
paul@367 | 48 | |
paul@367 | 49 | l4re_aux_t *l4re_aux = exec_get_l4re_aux(argc, argv); |
paul@367 | 50 | |
paul@523 | 51 | if (debug) |
paul@523 | 52 | printf("aux = {%s, %lx, %lx, %lx}\n", l4re_aux->binary, l4re_aux->kip_ds, l4re_aux->dbg_lvl, l4re_aux->ldr_flags); |
paul@368 | 53 | |
paul@367 | 54 | /* Skip past the auxiliary structure itself. */ |
paul@367 | 55 | |
paul@368 | 56 | struct exec_region *region = (struct exec_region *) *((l4_addr_t *) (l4re_aux + 1)); |
paul@367 | 57 | |
paul@367 | 58 | if (!region) |
paul@367 | 59 | { |
paul@367 | 60 | printf("Could not find regions.\n"); |
paul@367 | 61 | return 1; |
paul@367 | 62 | } |
paul@367 | 63 | |
paul@367 | 64 | for (; region && (region->ds != L4_INVALID_CAP); region++) |
paul@367 | 65 | { |
paul@523 | 66 | if (debug) |
paul@523 | 67 | printf("Adding region: {%lx, %llx, %lx, %lx}\n", region->start, region->start + region->size, region->flags, region->ds); |
paul@523 | 68 | |
paul@515 | 69 | MappedRegion r(region->start, region->start + region->size, region->flags, region->ds); |
paul@431 | 70 | exec_pager.add(r); |
paul@367 | 71 | } |
paul@367 | 72 | |
paul@367 | 73 | /* Start the pager. */ |
paul@367 | 74 | |
paul@523 | 75 | if (debug) |
paul@523 | 76 | { |
paul@523 | 77 | printf("Initialising pager...\n"); |
paul@523 | 78 | printf("Pager capability: %lx\n", l4re_env_get_cap(ENV_INTERNAL_PAGER_NAME)); |
paul@523 | 79 | printf("Main thread: %lx\n", l4re_env()->main_thread); |
paul@523 | 80 | } |
paul@376 | 81 | |
paul@492 | 82 | /* Initialise the server, enabling notifications. */ |
paul@492 | 83 | |
paul@493 | 84 | ResourceServer server(&exec_pager); |
paul@506 | 85 | long err = server.bind(ENV_INTERNAL_PAGER_NAME); |
paul@492 | 86 | |
paul@493 | 87 | if (err) |
paul@493 | 88 | { |
paul@493 | 89 | printf("Could not bind server: %s\n", l4sys_errtostr(err)); |
paul@493 | 90 | return 1; |
paul@493 | 91 | } |
paul@492 | 92 | |
paul@618 | 93 | if (debug) |
paul@618 | 94 | printf("Starting pager...\n"); |
paul@618 | 95 | |
paul@493 | 96 | server.start(true); |
paul@367 | 97 | |
paul@618 | 98 | if (debug) |
paul@618 | 99 | printf("Ending pager...\n"); |
paul@367 | 100 | return 0; |
paul@367 | 101 | } |
paul@367 | 102 | |
paul@367 | 103 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@367 | 104 | */ |