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@308 | 24 | #include <l4/sys/factory.h> |
paul@308 | 25 | #include <l4/sys/task.h> |
paul@308 | 26 | #include <l4/sys/thread.h> |
paul@308 | 27 | |
paul@308 | 28 | #include <fsclient/client.h> |
paul@308 | 29 | #include <ipc/cap_alloc.h> |
paul@308 | 30 | #include <ipc/mem_ipc.h> |
paul@308 | 31 | #include <ipc/server.h> |
paul@311 | 32 | #include <mem/memory_utils.h> |
paul@308 | 33 | #include <systypes/fcntl.h> |
paul@308 | 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@308 | 42 | #include "system_pager_interface.h" |
paul@308 | 43 | #include "system_pager_server.h" |
paul@308 | 44 | |
paul@308 | 45 | |
paul@308 | 46 | |
paul@308 | 47 | /* A simple system pager. */ |
paul@308 | 48 | |
paul@308 | 49 | class ExecPager : public SystemPager |
paul@308 | 50 | { |
paul@308 | 51 | public: |
paul@308 | 52 | l4_addr_t buf, buf_start; |
paul@308 | 53 | unsigned int buf_log2size; |
paul@308 | 54 | |
paul@308 | 55 | virtual long exception(l4_exc_regs_t regs, |
paul@308 | 56 | l4_snd_fpage_t *region); |
paul@308 | 57 | |
paul@308 | 58 | virtual long page_fault(l4_umword_t pfa, l4_umword_t pc, |
paul@308 | 59 | l4_snd_fpage_t *region); |
paul@308 | 60 | }; |
paul@308 | 61 | |
paul@308 | 62 | long ExecPager::exception(l4_exc_regs_t regs, l4_snd_fpage_t *region) |
paul@308 | 63 | { |
paul@308 | 64 | (void) region; |
paul@308 | 65 | |
paul@308 | 66 | printf("exception(...) -> pfa = %lx, pc = %lx\n", l4_utcb_exc_pfa(®s), l4_utcb_exc_pc(®s)); |
paul@308 | 67 | return L4_EOK; |
paul@308 | 68 | } |
paul@308 | 69 | |
paul@308 | 70 | long ExecPager::page_fault(l4_umword_t pfa, l4_umword_t pc, l4_snd_fpage_t *region) |
paul@308 | 71 | { |
paul@308 | 72 | l4_umword_t addr = pfa & ~7UL, flags = pfa & 7; |
paul@308 | 73 | |
paul@308 | 74 | printf("page_fault(%lx, %lx) -> %lx (%lx)...\n", pfa, pc, addr, flags); |
paul@308 | 75 | |
paul@308 | 76 | if ((addr >= buf_start) && (addr < buf_start + (1UL << buf_log2size))) |
paul@308 | 77 | { |
paul@308 | 78 | region->fpage = l4_fpage(buf, buf_log2size, L4_FPAGE_RX); |
paul@308 | 79 | region->snd_base = buf_start; |
paul@308 | 80 | |
paul@308 | 81 | return L4_EOK; |
paul@308 | 82 | } |
paul@308 | 83 | |
paul@308 | 84 | return -L4_ENOMEM; |
paul@308 | 85 | } |
paul@308 | 86 | |
paul@308 | 87 | |
paul@308 | 88 | |
paul@308 | 89 | static ExecPager exec_pager; |
paul@308 | 90 | |
paul@308 | 91 | static void init_pager(ipc_server_config_type *config, l4_addr_t buf, |
paul@308 | 92 | unsigned int buf_log2size, l4_addr_t buf_start) |
paul@308 | 93 | { |
paul@308 | 94 | exec_pager.buf = buf; |
paul@308 | 95 | exec_pager.buf_log2size = buf_log2size; |
paul@308 | 96 | exec_pager.buf_start = buf_start; |
paul@308 | 97 | |
paul@308 | 98 | ipc_server_init_config(config); |
paul@308 | 99 | |
paul@308 | 100 | config->expected_items = SystemPager_expected_items; |
paul@308 | 101 | config->handler = (ipc_server_handler_type) handle_SystemPager; |
paul@308 | 102 | config->handler_obj = static_cast<SystemPager *>(&exec_pager); |
paul@308 | 103 | } |
paul@308 | 104 | |
paul@308 | 105 | static long start_pager(ipc_server_config_type *config, pthread_t thread) |
paul@308 | 106 | { |
paul@308 | 107 | config->config_thread = 1; |
paul@308 | 108 | config->thread = pthread_l4_cap(thread); |
paul@308 | 109 | |
paul@308 | 110 | printf("Starting pager thread...\n"); |
paul@308 | 111 | return ipc_server_start_config(config); |
paul@308 | 112 | } |
paul@308 | 113 | |
paul@308 | 114 | |
paul@308 | 115 | |
paul@308 | 116 | /* UTCB properties. |
paul@308 | 117 | See: moe/server/src/loader.cc */ |
paul@308 | 118 | |
paul@308 | 119 | enum ipc_exec_utcb |
paul@308 | 120 | { |
paul@308 | 121 | Default_max_threads = 16, |
paul@308 | 122 | #ifdef ARCH_mips |
paul@308 | 123 | Utcb_area_start = 0x73000000, |
paul@308 | 124 | #else |
paul@308 | 125 | Utcb_area_start = 0xb3000000, |
paul@308 | 126 | #endif |
paul@308 | 127 | }; |
paul@308 | 128 | |
paul@308 | 129 | |
paul@308 | 130 | |
paul@311 | 131 | static offset_t find_region_size(offset_t size) |
paul@308 | 132 | { |
paul@311 | 133 | return 1 << page_order(size); |
paul@308 | 134 | } |
paul@308 | 135 | |
paul@311 | 136 | |
paul@311 | 137 | |
paul@311 | 138 | /* Capability mapping definitions for the new task. */ |
paul@311 | 139 | |
paul@311 | 140 | struct mapped_cap |
paul@311 | 141 | { |
paul@311 | 142 | l4_cap_idx_t cap; |
paul@311 | 143 | unsigned char rights; |
paul@311 | 144 | l4_umword_t spot; |
paul@311 | 145 | }; |
paul@311 | 146 | |
paul@311 | 147 | static long map_capabilities(l4_cap_idx_t task, struct mapped_cap mapped_caps[]) |
paul@308 | 148 | { |
paul@311 | 149 | long err = L4_EOK; |
paul@311 | 150 | int i = 0; |
paul@311 | 151 | |
paul@311 | 152 | while (l4_is_valid_cap(mapped_caps[i].cap) && !err) |
paul@311 | 153 | { |
paul@311 | 154 | err = l4_error(l4_task_map(task, L4RE_THIS_TASK_CAP, |
paul@311 | 155 | l4_obj_fpage(mapped_caps[i].cap, 0, mapped_caps[i].rights), |
paul@311 | 156 | l4_map_obj_control(mapped_caps[i].spot, L4_MAP_ITEM_MAP))); |
paul@311 | 157 | i++; |
paul@311 | 158 | } |
paul@311 | 159 | |
paul@311 | 160 | return err; |
paul@308 | 161 | } |
paul@308 | 162 | |
paul@311 | 163 | |
paul@311 | 164 | |
paul@308 | 165 | int main(int argc, char *argv[]) |
paul@308 | 166 | { |
paul@308 | 167 | long err; |
paul@308 | 168 | |
paul@308 | 169 | if (argc < 2) |
paul@308 | 170 | { |
paul@308 | 171 | printf("Need a program to run.\n"); |
paul@308 | 172 | return 1; |
paul@308 | 173 | } |
paul@308 | 174 | |
paul@308 | 175 | /* Allocate capabilities for the task and thread. */ |
paul@308 | 176 | |
paul@311 | 177 | l4_cap_idx_t caps[2]; |
paul@308 | 178 | |
paul@311 | 179 | err = ipc_cap_alloc_many(caps, 2); |
paul@308 | 180 | |
paul@311 | 181 | if (err) |
paul@308 | 182 | { |
paul@311 | 183 | printf("Could not allocate capabilities.\n"); |
paul@308 | 184 | return 1; |
paul@308 | 185 | } |
paul@308 | 186 | |
paul@311 | 187 | l4_cap_idx_t &task = caps[0]; |
paul@311 | 188 | l4_cap_idx_t &thread = caps[1]; |
paul@308 | 189 | |
paul@308 | 190 | /* Obtain the payload as a dataspace. */ |
paul@308 | 191 | |
paul@308 | 192 | file_t *file = client_open(argv[1], O_RDONLY); |
paul@308 | 193 | |
paul@308 | 194 | if (file == NULL) |
paul@308 | 195 | { |
paul@308 | 196 | printf("Could not read file: %s\n", argv[1]); |
paul@308 | 197 | return 1; |
paul@308 | 198 | } |
paul@308 | 199 | |
paul@308 | 200 | /* Copy the entire payload to a new dataspace. */ |
paul@308 | 201 | |
paul@308 | 202 | char *buf; |
paul@308 | 203 | offset_t nread; |
paul@308 | 204 | offset_t region_size = find_region_size(4000000); |
paul@308 | 205 | l4re_ds_t region_ds; |
paul@308 | 206 | |
paul@308 | 207 | err = ipc_allocate_align(region_size, L4RE_RM_F_SEARCH_ADDR | L4RE_RM_F_RWX, |
paul@311 | 208 | page_order(4000000), (void **) &buf, ®ion_ds); |
paul@308 | 209 | |
paul@308 | 210 | if (err) |
paul@308 | 211 | { |
paul@308 | 212 | printf("Could not reserve memory.\n"); |
paul@308 | 213 | return 1; |
paul@308 | 214 | } |
paul@308 | 215 | |
paul@308 | 216 | printf("Reading from file into %p.\n", buf); |
paul@308 | 217 | |
paul@308 | 218 | nread = client_read(file, buf, 4000000); |
paul@308 | 219 | |
paul@308 | 220 | printf("Read %ld from file.\n", nread); |
paul@308 | 221 | |
paul@308 | 222 | if (memcmp(buf + 0xae3, "\x31\xed", 2)) |
paul@308 | 223 | { |
paul@308 | 224 | printf("Did not find expected instructions at start.\n"); |
paul@308 | 225 | return 1; |
paul@308 | 226 | } |
paul@308 | 227 | |
paul@308 | 228 | /* UTCB location and size. */ |
paul@308 | 229 | |
paul@308 | 230 | l4_addr_t utcb_start = Utcb_area_start; |
paul@311 | 231 | int utcb_log2size = page_order(Default_max_threads * L4_UTCB_OFFSET); |
paul@308 | 232 | |
paul@308 | 233 | /* Round up to at least one page. */ |
paul@308 | 234 | |
paul@308 | 235 | if (utcb_log2size < L4_PAGESHIFT) |
paul@308 | 236 | utcb_log2size = L4_PAGESHIFT; |
paul@308 | 237 | |
paul@308 | 238 | /* KIP allocation. */ |
paul@308 | 239 | |
paul@308 | 240 | l4_addr_t kip_start = (l4_addr_t) l4re_kip(); |
paul@308 | 241 | |
paul@308 | 242 | printf("KIP at %lx.\n", kip_start); |
paul@308 | 243 | |
paul@308 | 244 | /* Create a new task and thread. */ |
paul@308 | 245 | |
paul@308 | 246 | l4_fpage_t utcb_fpage = l4_fpage(utcb_start, utcb_log2size, 0); |
paul@308 | 247 | |
paul@308 | 248 | err = l4_error(l4_factory_create_task(l4re_env()->factory, task, utcb_fpage)); |
paul@308 | 249 | |
paul@308 | 250 | if (err) |
paul@308 | 251 | { |
paul@308 | 252 | printf("Could not create task.\n"); |
paul@308 | 253 | return 1; |
paul@308 | 254 | } |
paul@308 | 255 | |
paul@308 | 256 | err = l4_error(l4_factory_create_thread(l4re_env()->factory, thread)); |
paul@308 | 257 | |
paul@308 | 258 | if (err) |
paul@308 | 259 | { |
paul@308 | 260 | printf("Could not create thread.\n"); |
paul@308 | 261 | return 1; |
paul@308 | 262 | } |
paul@308 | 263 | |
paul@308 | 264 | /* Start the pager. */ |
paul@308 | 265 | |
paul@308 | 266 | ipc_server_config_type config; |
paul@308 | 267 | pthread_t pager_thread; |
paul@308 | 268 | pthread_attr_t attr; |
paul@308 | 269 | |
paul@308 | 270 | pthread_attr_init(&attr); |
paul@308 | 271 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
paul@308 | 272 | |
paul@311 | 273 | init_pager(&config, (l4_addr_t) buf, page_order(file->size), 0x1000000); |
paul@308 | 274 | |
paul@308 | 275 | err = pthread_create(&pager_thread, &attr, ipc_server_start_mainloop, &config); |
paul@308 | 276 | |
paul@308 | 277 | if (err) |
paul@308 | 278 | { |
paul@308 | 279 | printf("Could not start pager thread.\n"); |
paul@308 | 280 | return 1; |
paul@308 | 281 | } |
paul@308 | 282 | |
paul@308 | 283 | err = start_pager(&config, pager_thread); |
paul@308 | 284 | |
paul@308 | 285 | if (err) |
paul@308 | 286 | { |
paul@308 | 287 | printf("Could not start pager.\n"); |
paul@308 | 288 | return 1; |
paul@308 | 289 | } |
paul@308 | 290 | |
paul@308 | 291 | /* Map the pager capability into the region manager/mapper slot. */ |
paul@308 | 292 | |
paul@308 | 293 | err = l4_error(l4_task_map(task, L4RE_THIS_TASK_CAP, |
paul@308 | 294 | l4_obj_fpage(config.server, 0, L4_CAP_FPAGE_RWS), |
paul@308 | 295 | l4_map_obj_control(0x10 << L4_CAP_SHIFT, L4_MAP_ITEM_MAP))); |
paul@308 | 296 | |
paul@308 | 297 | if (err) |
paul@308 | 298 | { |
paul@308 | 299 | printf("Could not map pager capability into task.\n"); |
paul@308 | 300 | return 1; |
paul@308 | 301 | } |
paul@308 | 302 | |
paul@308 | 303 | /* Map the KIP into the task. */ |
paul@308 | 304 | |
paul@308 | 305 | err = l4_error(l4_task_map(task, L4RE_THIS_TASK_CAP, |
paul@308 | 306 | l4_fpage(kip_start, L4_PAGESHIFT, L4_FPAGE_RX), |
paul@308 | 307 | kip_start)); |
paul@308 | 308 | |
paul@308 | 309 | if (err) |
paul@308 | 310 | { |
paul@308 | 311 | printf("Could not map KIP into task.\n"); |
paul@308 | 312 | return 1; |
paul@308 | 313 | } |
paul@308 | 314 | |
paul@311 | 315 | /* Define capability mappings for the new task. */ |
paul@308 | 316 | |
paul@311 | 317 | struct mapped_cap mapped_caps[] = { |
paul@311 | 318 | {task, L4_CAP_FPAGE_RWS, L4_BASE_TASK_CAP}, |
paul@311 | 319 | {thread, L4_CAP_FPAGE_RWS, L4_BASE_THREAD_CAP}, |
paul@311 | 320 | {l4re_env()->factory, L4_CAP_FPAGE_RWS, L4_BASE_FACTORY_CAP}, |
paul@311 | 321 | {l4re_env()->log, L4_CAP_FPAGE_RWS, L4_BASE_LOG_CAP}, |
paul@311 | 322 | {l4re_env()->scheduler, L4_CAP_FPAGE_RWS, L4_BASE_SCHEDULER_CAP}, |
paul@311 | 323 | {L4_INVALID_CAP, 0, 0}, |
paul@311 | 324 | }; |
paul@308 | 325 | |
paul@311 | 326 | err = map_capabilities(task, mapped_caps); |
paul@308 | 327 | |
paul@308 | 328 | if (err) |
paul@308 | 329 | { |
paul@311 | 330 | printf("Could not capabilities into task.\n"); |
paul@308 | 331 | return 1; |
paul@308 | 332 | } |
paul@308 | 333 | |
paul@308 | 334 | /* Configure the thread with the region manager acting as pager and exception |
paul@308 | 335 | handler. The UTCB will be situated at an address supported by a dataspace |
paul@308 | 336 | attached to the new task. */ |
paul@308 | 337 | |
paul@308 | 338 | printf("Configure thread...\n"); |
paul@308 | 339 | |
paul@308 | 340 | l4_thread_control_start(); |
paul@308 | 341 | l4_thread_control_pager(0x10 << L4_CAP_SHIFT); |
paul@308 | 342 | l4_thread_control_exc_handler(0x10 << L4_CAP_SHIFT); |
paul@308 | 343 | l4_thread_control_bind((l4_utcb_t *) utcb_start, task); |
paul@308 | 344 | err = l4_error(l4_thread_control_commit(thread)); |
paul@308 | 345 | |
paul@308 | 346 | if (err) |
paul@308 | 347 | { |
paul@308 | 348 | printf("Could not configure thread.\n"); |
paul@308 | 349 | return 1; |
paul@308 | 350 | } |
paul@308 | 351 | |
paul@308 | 352 | /* Map the payload into the new task. */ |
paul@308 | 353 | |
paul@311 | 354 | printf("Map %p with size %ld (2 ** %d).\n", buf, file->size, page_order(file->size)); |
paul@308 | 355 | |
paul@311 | 356 | l4_fpage_t payload_fpage = l4_fpage((l4_addr_t) buf, page_order(file->size), L4_FPAGE_RX); |
paul@308 | 357 | |
paul@308 | 358 | err = l4_error(l4_task_map(task, L4RE_THIS_TASK_CAP, payload_fpage, 0x1000000)); |
paul@308 | 359 | |
paul@308 | 360 | if (err) |
paul@308 | 361 | { |
paul@308 | 362 | printf("Could not map payload into task.\n"); |
paul@308 | 363 | return 1; |
paul@308 | 364 | } |
paul@308 | 365 | |
paul@308 | 366 | /* Start the new thread. */ |
paul@308 | 367 | |
paul@308 | 368 | printf("Schedule thread...\n"); |
paul@308 | 369 | |
paul@308 | 370 | err = l4_error(l4_thread_ex_regs(thread, 0x1000ae3, 0x2000000 /* stack top */, 0)); |
paul@308 | 371 | |
paul@308 | 372 | if (err) |
paul@308 | 373 | { |
paul@308 | 374 | printf("Could not set thread registers.\n"); |
paul@308 | 375 | return 1; |
paul@308 | 376 | } |
paul@308 | 377 | |
paul@308 | 378 | printf("Run thread...\n"); |
paul@308 | 379 | |
paul@308 | 380 | l4_sched_param_t sp = l4_sched_param(L4RE_MAIN_THREAD_PRIO); |
paul@308 | 381 | |
paul@308 | 382 | err = l4_error(l4_scheduler_run_thread(l4re_env()->scheduler, thread, &sp)); |
paul@308 | 383 | |
paul@308 | 384 | if (err) |
paul@308 | 385 | { |
paul@308 | 386 | printf("Could not run thread.\n"); |
paul@308 | 387 | return 1; |
paul@308 | 388 | } |
paul@308 | 389 | |
paul@308 | 390 | printf("Finished.\n"); |
paul@308 | 391 | while (1); |
paul@308 | 392 | |
paul@308 | 393 | return 0; |
paul@308 | 394 | } |
paul@308 | 395 | |
paul@308 | 396 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@308 | 397 | */ |