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@366 | 28 | #include <exec/external_pager.h> |
paul@324 | 29 | #include <exec/process.h> |
paul@308 | 30 | #include <ipc/server.h> |
paul@312 | 31 | |
paul@308 | 32 | #include <stdio.h> |
paul@308 | 33 | |
paul@308 | 34 | #include <pthread-l4.h> |
paul@308 | 35 | #include <pthread.h> |
paul@308 | 36 | |
paul@312 | 37 | #include "pager_object_server.h" |
paul@308 | 38 | |
paul@308 | 39 | |
paul@308 | 40 | |
paul@366 | 41 | static ExternalPager exec_pager; |
paul@308 | 42 | |
paul@369 | 43 | static const offset_t initial_stack_size = 16 * L4_PAGESIZE; |
paul@369 | 44 | |
paul@369 | 45 | |
paul@369 | 46 | |
paul@369 | 47 | /* Start the system pager in a separate thread. */ |
paul@369 | 48 | |
paul@369 | 49 | static long start_pager(ipc_server_config_type &config) |
paul@369 | 50 | { |
paul@369 | 51 | pthread_t pager_thread; |
paul@369 | 52 | pthread_attr_t attr; |
paul@369 | 53 | |
paul@369 | 54 | pthread_attr_init(&attr); |
paul@369 | 55 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
paul@369 | 56 | |
paul@369 | 57 | ipc_server_init_for(&config, PagerObject, &exec_pager); |
paul@369 | 58 | |
paul@369 | 59 | long err = pthread_create(&pager_thread, &attr, ipc_server_start_mainloop, &config); |
paul@369 | 60 | |
paul@369 | 61 | if (err) |
paul@369 | 62 | return err; |
paul@369 | 63 | |
paul@369 | 64 | return ipc_server_start_config_thread(&config, pthread_l4_cap(pager_thread)); |
paul@369 | 65 | } |
paul@369 | 66 | |
paul@308 | 67 | |
paul@308 | 68 | |
paul@308 | 69 | int main(int argc, char *argv[]) |
paul@308 | 70 | { |
paul@308 | 71 | long err; |
paul@308 | 72 | |
paul@308 | 73 | if (argc < 2) |
paul@308 | 74 | { |
paul@308 | 75 | printf("Need a program to run.\n"); |
paul@308 | 76 | return 1; |
paul@308 | 77 | } |
paul@308 | 78 | |
paul@349 | 79 | /* Initialise the memory of the new task. */ |
paul@323 | 80 | |
paul@349 | 81 | ExplicitSegment stack(Utcb_area_start - initial_stack_size, initial_stack_size, L4_FPAGE_RW); |
paul@349 | 82 | Payload *payload; |
paul@323 | 83 | |
paul@365 | 84 | if (exec_get_payload(argv[1], &payload, true)) |
paul@323 | 85 | { |
paul@349 | 86 | printf("Could not initialise program.\n"); |
paul@325 | 87 | return 1; |
paul@325 | 88 | } |
paul@325 | 89 | |
paul@365 | 90 | if (stack.allocate(true)) |
paul@316 | 91 | { |
paul@349 | 92 | printf("Could not allocate stack.\n"); |
paul@349 | 93 | return 1; |
paul@308 | 94 | } |
paul@308 | 95 | |
paul@354 | 96 | /* Initialise pager regions. */ |
paul@312 | 97 | |
paul@349 | 98 | for (unsigned int i = 0; i < payload->segments(); i++) |
paul@325 | 99 | { |
paul@349 | 100 | if (payload->segment(i)->loadable()) |
paul@349 | 101 | exec_pager.add(payload->segment(i)->region()); |
paul@349 | 102 | } |
paul@325 | 103 | |
paul@349 | 104 | exec_pager.add(stack.region()); |
paul@308 | 105 | |
paul@369 | 106 | /* Start the pager in a separate thread. */ |
paul@354 | 107 | |
paul@354 | 108 | ipc_server_config_type config; |
paul@308 | 109 | |
paul@354 | 110 | printf("Starting pager thread...\n"); |
paul@369 | 111 | |
paul@369 | 112 | err = start_pager(config); |
paul@308 | 113 | |
paul@308 | 114 | if (err) |
paul@308 | 115 | { |
paul@308 | 116 | printf("Could not start pager.\n"); |
paul@308 | 117 | return 1; |
paul@308 | 118 | } |
paul@308 | 119 | |
paul@319 | 120 | /* Configure the environment for the task, specifying the pager (and exception |
paul@319 | 121 | handler plus region mapper). */ |
paul@308 | 122 | |
paul@321 | 123 | Process process; |
paul@321 | 124 | |
paul@365 | 125 | err = process.configure_task(); |
paul@308 | 126 | |
paul@308 | 127 | if (err) |
paul@308 | 128 | { |
paul@321 | 129 | printf("Could not configure task.\n"); |
paul@308 | 130 | return 1; |
paul@308 | 131 | } |
paul@308 | 132 | |
paul@365 | 133 | err = process.configure_thread(config.server); |
paul@365 | 134 | |
paul@365 | 135 | if (err) |
paul@365 | 136 | { |
paul@365 | 137 | printf("Could not configure thread.\n"); |
paul@365 | 138 | return 1; |
paul@365 | 139 | } |
paul@365 | 140 | |
paul@321 | 141 | /* Populate a thread stack with argument and environment details. */ |
paul@321 | 142 | |
paul@369 | 143 | Stack program_st(stack); |
paul@321 | 144 | |
paul@321 | 145 | /* NOTE: Environment vector is currently not defined. */ |
paul@321 | 146 | |
paul@321 | 147 | char *envp[] = {NULL}; |
paul@321 | 148 | |
paul@369 | 149 | program_st.populate(argc - 1, argv + 1, envp); |
paul@321 | 150 | |
paul@321 | 151 | /* Start the new thread in the given stack. */ |
paul@308 | 152 | |
paul@308 | 153 | printf("Run thread...\n"); |
paul@308 | 154 | |
paul@369 | 155 | err = process.thread_start(payload->entry_point(), program_st); |
paul@308 | 156 | |
paul@308 | 157 | if (err) |
paul@308 | 158 | { |
paul@308 | 159 | printf("Could not run thread.\n"); |
paul@308 | 160 | return 1; |
paul@308 | 161 | } |
paul@308 | 162 | |
paul@308 | 163 | printf("Finished.\n"); |
paul@369 | 164 | while (1) |
paul@369 | 165 | l4_sleep_forever(); |
paul@308 | 166 | |
paul@308 | 167 | return 0; |
paul@308 | 168 | } |
paul@308 | 169 | |
paul@308 | 170 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@308 | 171 | */ |