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