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/re/l4aux.h> 24 #include <l4/sys/err.h> 25 #include <l4/util/elf.h> 26 #include <l4/util/util.h> 27 28 #include <exec/elf.h> 29 #include <exec/process.h> 30 #include <fsclient/client.h> 31 #include <ipc/mem_ipc.h> 32 #include <ipc/server.h> 33 #include <mem/memory_utils.h> 34 #include <systypes/fcntl.h> 35 36 #include <map> 37 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <string.h> 41 42 #include <pthread-l4.h> 43 #include <pthread.h> 44 45 #include "pager_object_interface.h" 46 #include "pager_object_server.h" 47 48 49 50 /* A simple system pager also acting as a region mapper. */ 51 52 typedef std::map<l4_addr_t, MappedRegion> MappedRegions; 53 54 class ExecPager : public PagerObject 55 { 56 protected: 57 MappedRegions _regions; 58 59 public: 60 virtual void add(MappedRegion region) 61 { 62 _regions[region.map_start] = region; 63 } 64 65 /* Notification methods. */ 66 67 virtual long exception(l4_exc_regs_t regs, 68 l4_snd_fpage_t *region); 69 70 virtual long page_fault(l4_umword_t pfa, l4_umword_t pc, 71 l4_snd_fpage_t *region); 72 73 /* Region manager/mapper methods. */ 74 75 virtual long attach(address_t *start, offset_t size, map_flags_t flags, 76 l4_cap_idx_t ds, address_t offset, unsigned char align); 77 78 }; 79 80 /* Handle a general exception. */ 81 82 long ExecPager::exception(l4_exc_regs_t regs, l4_snd_fpage_t *region) 83 { 84 (void) region; 85 86 printf("exception(...) -> pfa = %lx, pc = %lx\n", l4_utcb_exc_pfa(®s), l4_utcb_exc_pc(®s)); 87 88 printf("r15 = %lx\n", regs.r15); 89 printf("r14 = %lx\n", regs.r14); 90 printf("r13 = %lx\n", regs.r13); 91 printf("r12 = %lx\n", regs.r12); 92 printf("r11 = %lx\n", regs.r11); 93 printf("r10 = %lx\n", regs.r10); 94 printf("r9 = %lx\n", regs.r9); 95 printf("r8 = %lx\n", regs.r8); 96 printf("rdi = %lx\n", regs.rdi); 97 printf("rsi = %lx\n", regs.rsi); 98 printf("rbp = %lx\n", regs.rbp); 99 printf("pfa = %lx\n", regs.pfa); 100 printf("rbx = %lx\n", regs.rbx); 101 printf("rdx = %lx\n", regs.rdx); 102 printf("rcx = %lx\n", regs.rcx); 103 printf("rax = %lx\n", regs.rax); 104 printf("trapno = %lx\n", regs.trapno); 105 printf("err = %lx\n", regs.err); 106 printf("ip = %lx\n", regs.ip); 107 printf("flags = %lx\n", regs.flags); 108 printf("sp = %lx\n", regs.sp); 109 printf("ss = %lx\n", regs.ss); 110 printf("fs_base = %lx\n", regs.fs_base); 111 printf("gs_base = %lx\n", regs.gs_base); 112 113 return L4_EOK; 114 } 115 116 #define DEBUG 0 117 118 /* Handle a page fault using any configured regions. */ 119 120 long ExecPager::page_fault(l4_umword_t pfa, l4_umword_t pc, l4_snd_fpage_t *region) 121 { 122 l4_umword_t addr = pfa & ~7UL, flags = pfa & 7; 123 124 #if DEBUG 125 printf("page_fault(%lx, %lx) -> %lx (%lx) -> ", pfa, pc, addr, flags); 126 #endif 127 128 MappedRegions::iterator it = _regions.upper_bound(addr); 129 130 if (it != _regions.begin()) 131 it--; 132 else 133 { 134 printf("not mapped!\n"); 135 return -L4_ENOMEM; 136 } 137 138 MappedRegion &r = it->second; 139 140 if ((addr >= r.map_start) && (addr < r.map_start + r.size)) 141 { 142 l4_addr_t page_addr = trunc(addr, L4_PAGESIZE); 143 144 region->fpage = l4_fpage(r.start + (page_addr - r.map_start), L4_PAGESHIFT, r.flags); 145 region->snd_base = page_addr; 146 147 #if DEBUG 148 printf("%lx...%lx from %lx...%lx offset %lx size %d rights %x\n", 149 r.map_start, region->snd_base, 150 r.start, l4_fpage_memaddr(region->fpage), 151 addr - r.map_start, 152 l4_fpage_size(region->fpage), 153 l4_fpage_rights(region->fpage)); 154 printf("%lx -> ", addr); 155 156 for (unsigned int i = 0; i < sizeof(l4_umword_t); i++) 157 printf("%02x", *((unsigned char *)(r.start + (addr - r.map_start) + i))); 158 159 printf("\n"); 160 #endif 161 162 if (r.flags & L4RE_RM_F_W) 163 l4_touch_rw((const void *) (r.start + (page_addr - r.map_start)), L4_PAGESIZE); 164 else 165 l4_touch_ro((const void *) (r.start + (page_addr - r.map_start)), L4_PAGESIZE); 166 167 return L4_EOK; 168 } 169 170 #if DEBUG 171 printf("not mapped!\n"); 172 #endif 173 174 return -L4_ENOMEM; 175 } 176 177 /* Attach a region for provision when page faults occur. This is required in 178 the initialisation of a program by the C library which requires a region 179 mapper. */ 180 181 long ExecPager::attach(address_t *start, offset_t size, map_flags_t flags, 182 l4_cap_idx_t ds, address_t offset, unsigned char align) 183 { 184 #if DEBUG 185 printf("attach(%lx, %ld, %lx, ..., %lx, %d)\n", *start, size, flags, offset, align); 186 #endif 187 188 if (align < L4_PAGESHIFT) 189 align = L4_PAGESHIFT; 190 191 offset_t increment = 1UL << align; 192 offset_t region_size = round(size, increment); 193 194 /* Either attempt to find an address for the specified region, starting from 195 any indicated address. */ 196 197 if (flags & L4RE_RM_F_SEARCH_ADDR) 198 { 199 address_t region_start = trunc(*start, increment); 200 MappedRegions::iterator it = _regions.upper_bound(*start); 201 202 if (!region_start) 203 region_start += increment; 204 205 #if DEBUG 206 printf("-> search from %lx -> %lx...\n", *start, region_start); 207 #endif 208 209 /* Before last known region. */ 210 211 while (it != _regions.end()) 212 { 213 MappedRegions::iterator next = it; 214 MappedRegion &r = it->second; 215 address_t start_limit; 216 address_t end_limit = r.map_start; 217 218 /* Consider any preceding region. If no such region exists, choose an 219 address at the start of memory. */ 220 221 if (it == _regions.begin()) 222 start_limit = L4_PAGESIZE; 223 else 224 { 225 it--; 226 MappedRegion &pr = it->second; 227 start_limit = pr.map_start + pr.size; 228 it = next; 229 } 230 231 /* Test against the limits. */ 232 233 if (region_start < start_limit) 234 region_start = round(start_limit, increment); 235 236 /* Investigate subsequent regions if not enough space exists between the 237 preceding region (or start of memory) and the current region. */ 238 239 if ((region_start + region_size) > end_limit) 240 { 241 it++; 242 if (it == _regions.end()) 243 return -L4_ENOMEM; 244 } 245 else 246 break; 247 } 248 249 /* Attach the provided dataspace. 250 NOTE: This is only done in this implementation to support the paging 251 mechanism. In a region mapper residing within the actual task, the 252 dataspace's map operation would be invoked to obtain mappings. */ 253 254 l4_addr_t ds_start; 255 256 long err = ipc_attach_dataspace(ds, size, (void **) &ds_start); 257 258 if (err) 259 return err; 260 261 l4_touch_rw((const void *) ds_start, size); 262 263 #if DEBUG 264 printf("-> added region for %lx size %ld (%d)\n", region_start, region_size, page_order(region_size)); 265 #endif 266 267 add(MappedRegion(ds_start, region_size, flags & L4RE_DS_F_RIGHTS_MASK, region_start)); 268 269 *start = region_start; 270 return L4_EOK; 271 } 272 273 /* Or attempt to add the specified region at a specific address. */ 274 275 else 276 { 277 // NOTE: To be implemented. 278 279 return -L4_ENOMEM; 280 } 281 } 282 283 284 285 static ExecPager exec_pager; 286 287 static void init_pager(ipc_server_config_type *config) 288 { 289 ipc_server_init_config(config); 290 291 config->expected_items = PagerObject_expected_items; 292 config->handler = (ipc_server_handler_type) handle_PagerObject; 293 config->handler_obj = static_cast<PagerObject *>(&exec_pager); 294 } 295 296 static long start_pager(ipc_server_config_type *config, pthread_t thread) 297 { 298 config->config_thread = 1; 299 config->thread = pthread_l4_cap(thread); 300 301 printf("Starting pager thread...\n"); 302 return ipc_server_start_config(config); 303 } 304 305 306 307 int main(int argc, char *argv[]) 308 { 309 long err; 310 311 if (argc < 2) 312 { 313 printf("Need a program to run.\n"); 314 return 1; 315 } 316 317 /* Obtain the payload as a dataspace. */ 318 319 file_t *file = client_open(argv[1], O_RDONLY); 320 321 if (file == NULL) 322 { 323 printf("Could not read file: %s\n", argv[1]); 324 return 1; 325 } 326 327 /* Obtain metadata from the file. */ 328 329 char buf[4096]; 330 offset_t nread; 331 332 nread = client_read(file, buf, EI_NIDENT); 333 if ((nread < EI_NIDENT) || memcmp(buf, "\x7f" "ELF", 4)) 334 { 335 printf("Not an ELF payload: %s\n", argv[1]); 336 return 1; 337 } 338 339 Payload *payload = get_payload(buf); 340 341 if (payload == NULL) 342 { 343 printf("Unrecognised object size.\n"); 344 return 1; 345 } 346 347 client_seek(file, 0, SEEK_SET); 348 nread = client_read(file, buf, payload->header_extent()); 349 350 if (nread < payload->header_extent()) 351 { 352 printf("Header incomplete.\n"); 353 return 1; 354 } 355 356 printf("Program start: %lx\n", payload->entry_point()); 357 358 client_seek(file, 0, SEEK_SET); 359 nread = client_read(file, buf, payload->program_header_extent()); 360 361 if (nread < payload->program_header_extent()) 362 { 363 printf("Program headers incomplete.\n"); 364 return 1; 365 } 366 367 /* Make appropriate segments, although program segments could be made 368 interoperable with these. */ 369 370 Segment *segments[payload->segments() + 1]; 371 372 for (unsigned int i = 0; i < payload->segments(); i++) 373 { 374 ProgramSegment *ps = payload->segment(i); 375 376 printf("Segment(0x%lx, 0x%lx, 0x%x, 0x%lx, 0x%lx): %s\n", 377 ps->region_address(), 378 ps->region_size(), 379 ps->region_flags(), 380 ps->file_offset(), 381 ps->file_contents(), 382 ps->loadable() ? "loadable" : "other"); 383 384 if (ps->loadable()) 385 segments[i] = ps->segment(); 386 else 387 segments[i] = NULL; 388 } 389 390 /* Copy the payload regions to new dataspaces. */ 391 392 address_t program_start = payload->entry_point(); 393 offset_t initial_stack_size = 16 * L4_PAGESIZE; 394 395 Segment stack(Utcb_area_start - initial_stack_size, initial_stack_size, L4_FPAGE_RW); 396 397 segments[payload->segments()] = &stack; 398 399 for (unsigned int i = 0; i < payload->segments() + 1; i++) 400 { 401 Segment *segment = segments[i]; 402 403 if (segment == NULL) 404 continue; 405 406 if (segment->file_contents()) 407 { 408 flags_t flags; 409 410 if ((segment->region_flags() & L4RE_RM_F_RW) == L4RE_RM_F_RW) 411 flags = O_RDWR; 412 else if (segment->region_flags() & L4RE_RM_F_W) 413 flags = O_WRONLY; 414 else 415 flags = O_RDONLY; 416 417 file_t *file = client_open(argv[1], flags); 418 419 if (file == NULL) 420 { 421 printf("Could not open file for segment.\n"); 422 return 1; 423 } 424 425 err = segment->fill(file); 426 427 if (err) 428 { 429 printf("Could not fill segment from file.\n"); 430 return 1; 431 } 432 } 433 else 434 { 435 err = segment->allocate(); 436 437 if (err) 438 { 439 printf("Could not allocate segment.\n"); 440 return 1; 441 } 442 } 443 } 444 445 /* Start the pager. */ 446 447 ipc_server_config_type config; 448 pthread_t pager_thread; 449 pthread_attr_t attr; 450 451 pthread_attr_init(&attr); 452 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); 453 454 init_pager(&config); 455 456 for (unsigned int i = 0; i < payload->segments() + 1; i++) 457 { 458 Segment *segment = segments[i]; 459 460 if (segment == NULL) 461 continue; 462 463 exec_pager.add(segment->region()); 464 } 465 466 err = pthread_create(&pager_thread, &attr, ipc_server_start_mainloop, &config); 467 468 if (err) 469 { 470 printf("Could not start pager thread.\n"); 471 return 1; 472 } 473 474 err = start_pager(&config, pager_thread); 475 476 if (err) 477 { 478 printf("Could not start pager.\n"); 479 return 1; 480 } 481 482 /* Configure the environment for the task, specifying the pager (and exception 483 handler plus region mapper). */ 484 485 Process process; 486 487 err = process.configure(config.server); 488 489 if (err) 490 { 491 printf("Could not configure task.\n"); 492 return 1; 493 } 494 495 /* Populate a thread stack with argument and environment details. */ 496 497 Stack st(stack); 498 499 /* NOTE: Environment vector is currently not defined. */ 500 501 char *envp[] = {NULL}; 502 503 st.populate(argc - 1, argv + 1, envp); 504 505 /* Start the new thread in the given stack. */ 506 507 printf("Run thread...\n"); 508 509 err = process.thread_start(program_start, st); 510 511 if (err) 512 { 513 printf("Could not run thread.\n"); 514 return 1; 515 } 516 517 printf("Finished.\n"); 518 while (1); 519 520 return 0; 521 } 522 523 /* vim: tabstop=2 expandtab shiftwidth=2 524 */