1 /* 2 * Support for executing code in new tasks and threads. 3 * 4 * Copyright (C) 2022 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <l4/re/env.h> 23 #include <l4/sys/err.h> 24 #include <l4/sys/factory.h> 25 #include <l4/sys/task.h> 26 #include <l4/sys/thread.h> 27 #include <l4/util/elf.h> 28 #include <l4/util/util.h> 29 30 #include <l4/libloader/adjust_stack> 31 32 #include <fsclient/client.h> 33 #include <ipc/cap_alloc.h> 34 #include <ipc/mem_ipc.h> 35 #include <ipc/server.h> 36 #include <mem/memory_utils.h> 37 #include <systypes/fcntl.h> 38 39 #include <map> 40 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <string.h> 44 45 #include <pthread-l4.h> 46 #include <pthread.h> 47 48 #include "pager_object_interface.h" 49 #include "pager_object_server.h" 50 51 52 53 /* UTCB properties. 54 See: moe/server/src/loader.cc */ 55 56 enum ipc_exec_utcb 57 { 58 Default_max_threads = 16, 59 #ifdef ARCH_mips 60 Utcb_area_start = 0x73000000, 61 #else 62 Utcb_area_start = 0xb3000000, 63 #endif 64 }; 65 66 static l4_fpage_t get_utcb_fpage() 67 { 68 /* UTCB location and size. */ 69 70 int utcb_log2size = page_order(Default_max_threads * L4_UTCB_OFFSET); 71 72 /* Round up to at least one page. */ 73 74 if (utcb_log2size < L4_PAGESHIFT) 75 utcb_log2size = L4_PAGESHIFT; 76 77 return l4_fpage(Utcb_area_start, utcb_log2size, 0); 78 } 79 80 81 82 /* Mapped region data structures. */ 83 84 class MappedRegion 85 { 86 public: 87 l4_addr_t start; 88 unsigned int log2size; 89 l4_umword_t flags; 90 l4_addr_t map_start; 91 92 explicit MappedRegion() 93 : start(0), log2size(0), flags(0), map_start(0) 94 { 95 } 96 97 explicit MappedRegion(l4_addr_t start, unsigned int log2size, 98 l4_umword_t flags, l4_addr_t map_start) 99 : start(start), log2size(log2size), flags(flags), map_start(map_start) 100 { 101 } 102 }; 103 104 105 106 /* Program segment abstraction. */ 107 108 class Segment 109 { 110 protected: 111 MappedRegion _region; 112 113 public: 114 /* Allocated memory. */ 115 116 char *buf; 117 l4re_ds_t ds; 118 119 /* Segment base and corresponding region base. */ 120 121 l4_addr_t base, region_base; 122 123 /* Segment size and corresponding region size. */ 124 125 offset_t size, region_size; 126 127 /* Offset of segment content within the region. */ 128 129 offset_t region_offset; 130 131 /* Access flags. */ 132 133 l4re_rm_flags_t flags; 134 135 /* File access details. */ 136 137 offset_t file_offset, file_contents; 138 139 explicit Segment(offset_t base, offset_t size, l4re_rm_flags_t flags, 140 offset_t file_offset = 0, offset_t file_contents = 0) 141 : base(base), size(size), flags(flags), file_offset(file_offset), file_contents(file_contents) 142 { 143 region_base = trunc(base, L4_PAGESIZE); 144 region_offset = base - region_base; 145 region_size = round(size, L4_PAGESIZE); 146 } 147 148 long allocate(); 149 150 long fill(file_t *file); 151 152 MappedRegion ®ion(); 153 154 l4_addr_t region_address(char *address); 155 156 l4_addr_t region_address(l4_addr_t address); 157 }; 158 159 long Segment::allocate() 160 { 161 return ipc_allocate_align(size, L4RE_RM_F_SEARCH_ADDR | flags, 162 L4_PAGESHIFT, (void **) &buf, &ds); 163 } 164 165 long Segment::fill(file_t *file) 166 { 167 if (!file_contents) 168 return L4_EOK; 169 170 memset(buf, 0, region_size); 171 client_seek(file, file_offset, SEEK_SET); 172 offset_t nread = client_read(file, buf + region_offset, file_contents); 173 174 if (nread < file_contents) 175 return -L4_EIO; 176 else 177 return L4_EOK; 178 } 179 180 MappedRegion &Segment::region() 181 { 182 _region = MappedRegion((l4_addr_t) buf, page_order(region_size), flags, region_base); 183 return _region; 184 } 185 186 l4_addr_t Segment::region_address(char *address) 187 { 188 return (l4_addr_t) ((address - buf) + (char *) region_base); 189 } 190 191 l4_addr_t Segment::region_address(l4_addr_t address) 192 { 193 return (address - (l4_addr_t) buf) + region_base; 194 } 195 196 197 198 /* A simple system pager also acting as a region mapper. */ 199 200 typedef std::map<l4_addr_t, MappedRegion> MappedRegions; 201 202 class ExecPager : public PagerObject 203 { 204 protected: 205 MappedRegions _regions; 206 207 public: 208 virtual void add(MappedRegion region) 209 { 210 _regions[region.map_start] = region; 211 } 212 213 /* Notification methods. */ 214 215 virtual long exception(l4_exc_regs_t regs, 216 l4_snd_fpage_t *region); 217 218 virtual long page_fault(l4_umword_t pfa, l4_umword_t pc, 219 l4_snd_fpage_t *region); 220 221 /* Region manager/mapper methods. */ 222 223 virtual long attach(address_t *start, offset_t size, map_flags_t flags, 224 l4_cap_idx_t ds, address_t offset, unsigned char align); 225 226 }; 227 228 /* Handle a general exception. */ 229 230 long ExecPager::exception(l4_exc_regs_t regs, l4_snd_fpage_t *region) 231 { 232 (void) region; 233 234 printf("exception(...) -> pfa = %lx, pc = %lx\n", l4_utcb_exc_pfa(®s), l4_utcb_exc_pc(®s)); 235 236 printf("r15 = %lx\n", regs.r15); 237 printf("r14 = %lx\n", regs.r14); 238 printf("r13 = %lx\n", regs.r13); 239 printf("r12 = %lx\n", regs.r12); 240 printf("r11 = %lx\n", regs.r11); 241 printf("r10 = %lx\n", regs.r10); 242 printf("r9 = %lx\n", regs.r9); 243 printf("r8 = %lx\n", regs.r8); 244 printf("rdi = %lx\n", regs.rdi); 245 printf("rsi = %lx\n", regs.rsi); 246 printf("rbp = %lx\n", regs.rbp); 247 printf("pfa = %lx\n", regs.pfa); 248 printf("rbx = %lx\n", regs.rbx); 249 printf("rdx = %lx\n", regs.rdx); 250 printf("rcx = %lx\n", regs.rcx); 251 printf("rax = %lx\n", regs.rax); 252 printf("trapno = %lx\n", regs.trapno); 253 printf("err = %lx\n", regs.err); 254 printf("ip = %lx\n", regs.ip); 255 printf("flags = %lx\n", regs.flags); 256 printf("sp = %lx\n", regs.sp); 257 printf("ss = %lx\n", regs.ss); 258 printf("fs_base = %lx\n", regs.fs_base); 259 printf("gs_base = %lx\n", regs.gs_base); 260 261 return L4_EOK; 262 } 263 264 #define DEBUG 0 265 266 /* Handle a page fault using any configured regions. */ 267 268 long ExecPager::page_fault(l4_umword_t pfa, l4_umword_t pc, l4_snd_fpage_t *region) 269 { 270 l4_umword_t addr = pfa & ~7UL, flags = pfa & 7; 271 272 #if DEBUG 273 printf("page_fault(%lx, %lx) -> %lx (%lx) -> ", pfa, pc, addr, flags); 274 #endif 275 276 MappedRegions::iterator it = _regions.upper_bound(addr); 277 278 if (it != _regions.begin()) 279 it--; 280 else 281 { 282 printf("not mapped!\n"); 283 return -L4_ENOMEM; 284 } 285 286 MappedRegion &r = it->second; 287 288 if ((addr >= r.map_start) && (addr < r.map_start + (1UL << r.log2size))) 289 { 290 l4_addr_t page_addr = trunc(addr, L4_PAGESIZE); 291 292 region->fpage = l4_fpage(r.start + (page_addr - r.map_start), L4_PAGESHIFT, r.flags); 293 region->snd_base = page_addr; 294 295 #if DEBUG 296 printf("%lx...%lx from %lx...%lx size %d rights %x\n", 297 r.map_start, region->snd_base, 298 r.start, l4_fpage_memaddr(region->fpage), 299 l4_fpage_size(region->fpage), 300 l4_fpage_rights(region->fpage)); 301 printf("%lx -> ", addr); 302 303 for (unsigned int i = 0; i < sizeof(l4_umword_t); i++) 304 printf("%02x", *((unsigned char *)(r.start + (addr - r.map_start) + i))); 305 306 printf("\n"); 307 #endif 308 309 return L4_EOK; 310 } 311 312 #if DEBUG 313 printf("not mapped!\n"); 314 #endif 315 316 return -L4_ENOMEM; 317 } 318 319 /* Attach a region for provision when page faults occur. This is required in 320 the initialisation of a program by the C library which requires a region 321 mapper. */ 322 323 long ExecPager::attach(address_t *start, offset_t size, map_flags_t flags, 324 l4_cap_idx_t ds, address_t offset, unsigned char align) 325 { 326 #if DEBUG 327 printf("attach(%lx, %ld, %lx, ..., %lx, %d)\n", *start, size, flags, offset, align); 328 #endif 329 330 if (align < L4_PAGESHIFT) 331 align = L4_PAGESHIFT; 332 333 offset_t increment = 1UL << align; 334 offset_t region_size = round(size, increment); 335 336 /* Either attempt to find an address for the specified region, starting from 337 any indicated address. */ 338 339 if (flags & L4RE_RM_F_SEARCH_ADDR) 340 { 341 address_t region_start = trunc(*start, increment); 342 MappedRegions::iterator it = _regions.upper_bound(*start); 343 344 if (!region_start) 345 region_start += increment; 346 347 #if DEBUG 348 printf("-> search from %lx -> %lx...\n", *start, region_start); 349 #endif 350 351 /* Before last known region. */ 352 353 while (it != _regions.end()) 354 { 355 MappedRegions::iterator next = it; 356 MappedRegion &r = it->second; 357 address_t start_limit; 358 address_t end_limit = r.map_start; 359 360 /* Consider any preceding region. If no such region exists, choose an 361 address at the start of memory. */ 362 363 if (it == _regions.begin()) 364 start_limit = L4_PAGESIZE; 365 else 366 { 367 it--; 368 MappedRegion &pr = it->second; 369 start_limit = pr.map_start + (1UL << pr.log2size); 370 it = next; 371 } 372 373 /* Test against the limits. */ 374 375 if (region_start < start_limit) 376 region_start = round(start_limit, increment); 377 378 /* Investigate subsequent regions if not enough space exists between the 379 preceding region (or start of memory) and the current region. */ 380 381 if ((region_start + region_size) > end_limit) 382 { 383 it++; 384 if (it == _regions.end()) 385 return -L4_ENOMEM; 386 } 387 else 388 break; 389 } 390 391 /* Attach the provided dataspace. 392 NOTE: This is only done in this implementation to support the paging 393 mechanism. In a region mapper residing within the actual task, the 394 dataspace's map operation would be invoked to obtain mappings. */ 395 396 l4_addr_t ds_start; 397 398 long err = ipc_attach_dataspace(ds, size, (void **) &ds_start); 399 400 if (err) 401 return err; 402 403 l4_touch_rw((const void *) ds_start, size); 404 405 #if DEBUG 406 printf("-> added region for %lx size %ld (%d)\n", region_start, region_size, page_order(region_size)); 407 #endif 408 409 add(MappedRegion(ds_start, page_order(region_size), flags & L4RE_DS_F_RIGHTS_MASK, region_start)); 410 411 *start = region_start; 412 return L4_EOK; 413 } 414 415 /* Or attempt to add the specified region at a specific address. */ 416 417 else 418 { 419 // NOTE: To be implemented. 420 421 return -L4_ENOMEM; 422 } 423 } 424 425 426 427 /* Capability mapping definitions for the new task. */ 428 429 struct mapped_cap 430 { 431 l4_umword_t index; 432 l4_cap_idx_t cap; 433 unsigned char rights; 434 }; 435 436 static long map_capabilities(l4_cap_idx_t task, struct mapped_cap mapped_caps[]) 437 { 438 long err = L4_EOK; 439 int i = 0; 440 441 while (l4_is_valid_cap(mapped_caps[i].cap) && !err) 442 { 443 err = l4_error(l4_task_map(task, L4RE_THIS_TASK_CAP, 444 l4_obj_fpage(mapped_caps[i].cap, 0, mapped_caps[i].rights), 445 l4_map_obj_control(mapped_caps[i].index, L4_MAP_ITEM_MAP))); 446 i++; 447 } 448 449 return err; 450 } 451 452 453 454 /* A stack abstraction. */ 455 456 class Stack 457 { 458 struct auxv_entry 459 { 460 l4_umword_t key, value; 461 }; 462 463 protected: 464 Segment &_segment; 465 466 /* Next element pointer. */ 467 468 l4_umword_t *_element; 469 470 /* Stack section properties. */ 471 472 l4_addr_t _caps; 473 char *_arg_top, *_env_top; 474 char *_auxv_end; 475 int _env_entries; 476 477 public: 478 /* Start address and environment structure. */ 479 480 l4_addr_t start; 481 l4re_env_t *env; 482 l4_fpage_t utcb_fpage; 483 484 /* Initialise a stack in a memory segment. */ 485 486 explicit Stack(Segment &segment, l4_fpage_t utcb_fpage) 487 : _segment(segment), utcb_fpage(utcb_fpage) 488 { 489 _element = (l4_umword_t *) (segment.buf + segment.size); 490 } 491 492 /* Push any additional initial capabilities. */ 493 494 void push_cap_entries(l4re_env_cap_entry_t *entries) 495 { 496 l4re_env_cap_entry_t *entry = (l4re_env_cap_entry_t *) _element; 497 498 *(--entry) = l4re_env_cap_entry_t(); 499 500 while ((entries != NULL) && (entries->cap != L4_INVALID_CAP)) 501 *(--entry) = *entries; 502 503 _caps = (l4_addr_t) entry; 504 _element = (l4_umword_t *) entry; 505 } 506 507 /* Push environment values in reverse order. */ 508 509 void push_string(char *s) 510 { 511 char *arg = (char *) _element; 512 char *arg_last = arg; 513 514 arg -= round(strlen(s) + 1, sizeof(l4_umword_t)); 515 516 memset(arg, 0, arg_last - arg); 517 memcpy(arg, s, strlen(s)); 518 519 _element = (l4_umword_t *) arg; 520 } 521 522 /* Push environment values in reverse order. */ 523 524 void push_env(char *envp[]) 525 { 526 _env_top = (char *) _element; 527 _env_entries = 0; 528 529 for (; *envp != NULL; envp++, _env_entries++) 530 push_string(*envp); 531 } 532 533 /* Push argument values in reverse order. */ 534 535 void push_args(int argc, char *argv[]) 536 { 537 _arg_top = (char *) _element; 538 539 for (int i = 0; i < argc; i++) 540 push_string(argv[i]); 541 } 542 543 /* Loader flags, debugging flags, and the KIP capability index. 544 See: generate_l4aux in Remote_app_model */ 545 546 void push_l4re_flags() 547 { 548 *(--_element) = 0; 549 *(--_element) = 0; 550 *(--_element) = 0x14 << L4_CAP_SHIFT; 551 } 552 553 void push_l4re_env() 554 { 555 env = (l4re_env_t *) _element; 556 env--; 557 558 env->factory = L4_BASE_FACTORY_CAP; 559 env->main_thread = L4_BASE_THREAD_CAP; 560 env->log = L4_BASE_LOG_CAP; 561 env->scheduler = L4_BASE_SCHEDULER_CAP; 562 env->rm = 0x11 << L4_CAP_SHIFT; 563 env->mem_alloc = 0x12 << L4_CAP_SHIFT; 564 env->first_free_cap = 0x15; 565 env->caps = (l4re_env_cap_entry_t *) (_segment.region_address(_caps)); 566 env->utcb_area = utcb_fpage; 567 env->first_free_utcb = l4_fpage_memaddr(utcb_fpage) + L4_UTCB_OFFSET; 568 569 _element = (l4_umword_t *) env; 570 } 571 572 /* Push the auxiliary vector. */ 573 574 void push_auxv() 575 { 576 _auxv_end = (char *) _element; 577 578 struct auxv_entry *auxv_base = (struct auxv_entry *) _element; 579 580 /* AUXV NULL. */ 581 582 *(--auxv_base) = {0, 0}; 583 584 /* L4Re global environment pointer. */ 585 586 *(--auxv_base) = {0xf1, _segment.region_address((char *) env)}; 587 588 /* Apparently required entries. 589 NOTE: The user/group identifiers should be obtained from the broader 590 environment. */ 591 592 *(--auxv_base) = {AT_PAGESZ, L4_PAGESIZE}; 593 *(--auxv_base) = {AT_UID, 0}; 594 *(--auxv_base) = {AT_EUID, 0}; 595 *(--auxv_base) = {AT_GID, 0}; 596 *(--auxv_base) = {AT_EGID, 0}; 597 598 _element = (l4_umword_t *) auxv_base; 599 } 600 601 /* Fill the stack in reverse with an address, returning the size of the 602 value. */ 603 604 offset_t write_address(char *arg, char **addr, char *s) 605 { 606 offset_t size = round(strlen(s) + 1, sizeof(l4_umword_t)); 607 608 *addr = (char *) _segment.region_address(arg - size); 609 return size; 610 } 611 612 /* Populate stack with environment pointers, employing a pointer ordering 613 that is the reverse of the value ordering. */ 614 615 void push_envp(char *envp[]) 616 { 617 /* Write the terminating element. */ 618 619 *(--_element) = 0; 620 621 /* Reserve space and fill the stack from the top inwards. */ 622 623 char **ep = (char **) (_element - _env_entries); 624 char *arg = _env_top; 625 626 for (; *envp != NULL; envp++, ep++) 627 arg -= write_address(arg, ep, *envp); 628 629 _element -= _env_entries; 630 } 631 632 /* Populate stack with argument pointers and count, employing a pointer 633 ordering that is the reverse of the value ordering. */ 634 635 void push_argv(int argc, char *argv[]) 636 { 637 /* Write the terminating element. */ 638 639 *(--_element) = 0; 640 641 /* Reserve space and fill the stack from the top inwards. */ 642 643 char **ap = (char **) (_element - argc); 644 char *arg = _arg_top; 645 646 for (int i = 0; i < argc; i++) 647 arg -= write_address(arg, &ap[i], argv[i]); 648 649 /* Write the count. */ 650 651 _element -= argc; 652 653 *(--_element) = argc; 654 } 655 656 /* Adjust the stack alignment and return the stack address. */ 657 658 l4_addr_t align_stack() 659 { 660 char *current = (char *) _element; 661 char *adjusted = Ldr::adjust_sp(current, NULL); 662 663 if (adjusted != current) 664 memmove(adjusted, (const void *) current, _auxv_end - current); 665 666 _element = (l4_umword_t *) adjusted; 667 668 return _segment.region_address(adjusted); 669 } 670 671 /* Populate stack with additional capabilities. */ 672 673 void populate(int argc, char *argv[], char *envp[]) 674 { 675 push_cap_entries(NULL); 676 677 /* Populate stack with environment and argument values. */ 678 679 push_env(envp); 680 push_args(argc, argv); 681 682 /* Push L4Re flags, environment and auxiliary vector. */ 683 684 push_l4re_flags(); 685 push_l4re_env(); 686 push_auxv(); 687 688 /* Push environment and argument pointers. */ 689 690 push_envp(envp); 691 push_argv(argc, argv); 692 693 /* Adjust the stack alignment. */ 694 695 start = align_stack(); 696 } 697 }; 698 699 700 701 class Environment 702 { 703 protected: 704 Stack &_st; 705 l4_cap_idx_t task = L4_INVALID_CAP, thread = L4_INVALID_CAP; 706 l4_cap_idx_t _pager_cap = 0x10 << L4_CAP_SHIFT; 707 708 public: 709 explicit Environment(Stack &st) 710 : _st(st) 711 { 712 } 713 714 long create_task() 715 { 716 task = ipc_cap_alloc(); 717 718 if (l4_is_invalid_cap(task)) 719 return -L4_ENOMEM; 720 721 return l4_error(l4_factory_create_task(l4re_env()->factory, task, _st.utcb_fpage)); 722 } 723 724 long create_thread() 725 { 726 thread = ipc_cap_alloc(); 727 728 if (l4_is_invalid_cap(thread)) 729 return -L4_ENOMEM; 730 731 return l4_error(l4_factory_create_thread(l4re_env()->factory, thread)); 732 } 733 734 /* Configure the task environment. */ 735 736 long configure(l4_cap_idx_t server) 737 { 738 /* Map the KIP into the task. */ 739 740 l4_addr_t kip_start = (l4_addr_t) l4re_kip(); 741 742 long err = l4_error(l4_task_map(task, L4RE_THIS_TASK_CAP, 743 l4_fpage(kip_start, L4_PAGESHIFT, L4_FPAGE_RX), 744 kip_start)); 745 746 if (err) 747 return err; 748 749 /* Define capability mappings for the new task. */ 750 751 l4re_env_t *env = _st.env; 752 753 struct mapped_cap mapped_caps[] = { 754 {L4_BASE_TASK_CAP, task, L4_CAP_FPAGE_RWS}, 755 {_pager_cap, server, L4_CAP_FPAGE_RWS}, 756 {env->rm, server, L4_CAP_FPAGE_RWS}, 757 {env->main_thread, thread, L4_CAP_FPAGE_RWS}, 758 {env->factory, l4re_env()->factory, L4_CAP_FPAGE_RWS}, 759 {env->log, l4re_env()->log, L4_CAP_FPAGE_RWS}, 760 {env->scheduler, l4re_env()->scheduler, L4_CAP_FPAGE_RWS}, 761 {env->mem_alloc, l4re_env()->mem_alloc, L4_CAP_FPAGE_RWS}, 762 {0, L4_INVALID_CAP, 0}, 763 }; 764 765 return map_capabilities(task, mapped_caps); 766 } 767 768 /* Initialise the thread. */ 769 770 long thread_init(l4_addr_t program_start) 771 { 772 l4_thread_control_start(); 773 l4_thread_control_pager(_pager_cap); 774 l4_thread_control_exc_handler(_pager_cap); 775 l4_thread_control_bind((l4_utcb_t *) l4_fpage_memaddr(_st.utcb_fpage), task); 776 777 long err = l4_error(l4_thread_control_commit(thread)); 778 779 if (err) 780 return err; 781 782 return l4_error(l4_thread_ex_regs(thread, program_start, _st.start, 0)); 783 } 784 785 /* Start the thread. */ 786 787 long thread_start() 788 { 789 l4_sched_param_t sp = l4_sched_param(L4RE_MAIN_THREAD_PRIO); 790 791 return l4_error(l4_scheduler_run_thread(l4re_env()->scheduler, thread, &sp)); 792 } 793 }; 794 795 796 797 static ExecPager exec_pager; 798 799 static void init_pager(ipc_server_config_type *config) 800 { 801 ipc_server_init_config(config); 802 803 config->expected_items = PagerObject_expected_items; 804 config->handler = (ipc_server_handler_type) handle_PagerObject; 805 config->handler_obj = static_cast<PagerObject *>(&exec_pager); 806 } 807 808 static long start_pager(ipc_server_config_type *config, pthread_t thread) 809 { 810 config->config_thread = 1; 811 config->thread = pthread_l4_cap(thread); 812 813 printf("Starting pager thread...\n"); 814 return ipc_server_start_config(config); 815 } 816 817 818 819 int main(int argc, char *argv[]) 820 { 821 long err; 822 823 if (argc < 2) 824 { 825 printf("Need a program to run.\n"); 826 return 1; 827 } 828 829 /* Obtain the payload as a dataspace. */ 830 831 file_t *file = client_open(argv[1], O_RDONLY); 832 833 if (file == NULL) 834 { 835 printf("Could not read file: %s\n", argv[1]); 836 return 1; 837 } 838 839 /* Copy the payload regions to new dataspaces. 840 NOTE: This should be directed by the ELF metadata. */ 841 842 address_t program_start = 0x1000b43; 843 offset_t initial_stack_size = 16 * L4_PAGESIZE; 844 845 Segment program(0x1000000, 0x28326, L4_FPAGE_RWX, 0, 0x28326); 846 Segment data(0x1029360, 0x8068, L4_FPAGE_RW, 0x28360, 0x2058); 847 Segment stack(Utcb_area_start - initial_stack_size, initial_stack_size, L4_FPAGE_RW); 848 Segment *segments[] = {&program, &data, &stack, NULL}; 849 Segment **segment; 850 851 for (segment = segments; *segment != NULL; segment++) 852 { 853 err = (*segment)->allocate(); 854 855 if (err) 856 { 857 printf("Could not reserve memory.\n"); 858 return 1; 859 } 860 861 err = (*segment)->fill(file); 862 863 if (err) 864 { 865 printf("Could not fill segment from file.\n"); 866 return 1; 867 } 868 } 869 870 /* Populate the stack with argument and environment details. */ 871 872 Stack st(stack, get_utcb_fpage()); 873 Environment environment(st); 874 875 /* NOTE: Environment vector is currently not defined. */ 876 877 char *envp[] = {NULL}; 878 879 st.populate(argc - 1, argv + 1, envp); 880 881 /* Create a new task and thread. */ 882 883 err = environment.create_task(); 884 885 if (err) 886 { 887 printf("Could not create task.\n"); 888 return 1; 889 } 890 891 err = environment.create_thread(); 892 893 if (err) 894 { 895 printf("Could not create thread.\n"); 896 return 1; 897 } 898 899 err = environment.thread_init(program_start); 900 901 if (err) 902 { 903 printf("Could not configure thread.\n"); 904 return 1; 905 } 906 907 /* Start the pager. */ 908 909 ipc_server_config_type config; 910 pthread_t pager_thread; 911 pthread_attr_t attr; 912 913 pthread_attr_init(&attr); 914 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); 915 916 init_pager(&config); 917 918 for (segment = segments; *segment != NULL; segment++) 919 exec_pager.add((*segment)->region()); 920 921 err = pthread_create(&pager_thread, &attr, ipc_server_start_mainloop, &config); 922 923 if (err) 924 { 925 printf("Could not start pager thread.\n"); 926 return 1; 927 } 928 929 err = start_pager(&config, pager_thread); 930 931 if (err) 932 { 933 printf("Could not start pager.\n"); 934 return 1; 935 } 936 937 /* Configure the environment for the task, specifying the pager (and exception 938 handler plus region mapper). */ 939 940 err = environment.configure(config.server); 941 942 if (err) 943 { 944 printf("Could not map capabilities into task.\n"); 945 return 1; 946 } 947 948 /* Start the new thread. */ 949 950 printf("Run thread...\n"); 951 952 err = environment.thread_start(); 953 954 if (err) 955 { 956 printf("Could not run thread.\n"); 957 return 1; 958 } 959 960 printf("Finished.\n"); 961 while (1); 962 963 return 0; 964 } 965 966 /* vim: tabstop=2 expandtab shiftwidth=2 967 */