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 155 printf("%lx -> ", addr); 156 157 for (unsigned int i = 0; i < sizeof(l4_umword_t); i++) 158 printf("%02x", *((unsigned char *)(r.start + (addr - r.map_start) + i))); 159 160 printf("\n"); 161 #endif 162 163 if (r.flags & L4RE_RM_F_W) 164 l4_touch_rw((const void *) (r.start + (page_addr - r.map_start)), L4_PAGESIZE); 165 else 166 l4_touch_ro((const void *) (r.start + (page_addr - r.map_start)), L4_PAGESIZE); 167 168 return L4_EOK; 169 } 170 171 #if DEBUG 172 printf("not mapped!\n"); 173 #endif 174 175 return -L4_ENOMEM; 176 } 177 178 /* Attach a region for provision when page faults occur. This is required in 179 the initialisation of a program by the C library which requires a region 180 mapper. */ 181 182 long ExecPager::attach(address_t *start, offset_t size, map_flags_t flags, 183 l4_cap_idx_t ds, address_t offset, unsigned char align) 184 { 185 #if DEBUG 186 printf("attach(%lx, %ld, %lx, ..., %lx, %d)\n", *start, size, flags, offset, align); 187 #endif 188 189 if (align < L4_PAGESHIFT) 190 align = L4_PAGESHIFT; 191 192 offset_t increment = 1UL << align; 193 offset_t region_size = round(size, increment); 194 195 /* Either attempt to find an address for the specified region, starting from 196 any indicated address. */ 197 198 if (flags & L4RE_RM_F_SEARCH_ADDR) 199 { 200 address_t region_start = trunc(*start, increment); 201 MappedRegions::iterator it = _regions.upper_bound(*start); 202 203 if (!region_start) 204 region_start += increment; 205 206 #if DEBUG 207 printf("-> search from %lx -> %lx...\n", *start, region_start); 208 #endif 209 210 /* Before last known region. */ 211 212 while (it != _regions.end()) 213 { 214 MappedRegions::iterator next = it; 215 MappedRegion &r = it->second; 216 address_t start_limit; 217 address_t end_limit = r.map_start; 218 219 /* Consider any preceding region. If no such region exists, choose an 220 address at the start of memory. */ 221 222 if (it == _regions.begin()) 223 start_limit = L4_PAGESIZE; 224 else 225 { 226 it--; 227 MappedRegion &pr = it->second; 228 start_limit = pr.map_start + pr.size; 229 it = next; 230 } 231 232 /* Test against the limits. */ 233 234 if (region_start < start_limit) 235 region_start = round(start_limit, increment); 236 237 /* Investigate subsequent regions if not enough space exists between the 238 preceding region (or start of memory) and the current region. */ 239 240 if ((region_start + region_size) > end_limit) 241 { 242 it++; 243 if (it == _regions.end()) 244 return -L4_ENOMEM; 245 } 246 else 247 break; 248 } 249 250 /* Attach the provided dataspace. 251 NOTE: This is only done in this implementation to support the paging 252 mechanism. In a region mapper residing within the actual task, the 253 dataspace's map operation would be invoked to obtain mappings. */ 254 255 l4_addr_t ds_start; 256 257 long err = ipc_attach_dataspace(ds, size, (void **) &ds_start); 258 259 if (err) 260 return err; 261 262 l4_touch_rw((const void *) ds_start, size); 263 264 #if DEBUG 265 printf("-> added region for %lx size %ld (%d)\n", region_start, region_size, page_order(region_size)); 266 #endif 267 268 add(MappedRegion(ds_start, region_size, flags & L4RE_DS_F_RIGHTS_MASK, region_start)); 269 270 *start = region_start; 271 return L4_EOK; 272 } 273 274 /* Or attempt to add the specified region at a specific address. */ 275 276 else 277 { 278 // NOTE: To be implemented. 279 280 #if DEBUG 281 printf("-> region of size %ld (%d) not added!\n", region_size, page_order(region_size)); 282 #endif 283 284 return -L4_ENOMEM; 285 } 286 } 287 288 289 290 static ExecPager exec_pager; 291 292 static void init_pager(ipc_server_config_type *config) 293 { 294 ipc_server_init_config(config); 295 296 config->expected_items = PagerObject_expected_items; 297 config->handler = (ipc_server_handler_type) handle_PagerObject; 298 config->handler_obj = static_cast<PagerObject *>(&exec_pager); 299 } 300 301 static long start_pager(ipc_server_config_type *config, pthread_t thread) 302 { 303 config->config_thread = 1; 304 config->thread = pthread_l4_cap(thread); 305 306 printf("Starting pager thread...\n"); 307 return ipc_server_start_config(config); 308 } 309 310 311 312 int main(int argc, char *argv[]) 313 { 314 long err; 315 316 if (argc < 2) 317 { 318 printf("Need a program to run.\n"); 319 return 1; 320 } 321 322 /* Obtain the payload as a dataspace. */ 323 324 file_t *file = client_open(argv[1], O_RDONLY); 325 326 if (file == NULL) 327 { 328 printf("Could not read file: %s\n", argv[1]); 329 return 1; 330 } 331 332 /* Obtain metadata from the file. */ 333 334 char *buf = (char *) client_mmap(file, 0, file->size, 0, 0, L4RE_DS_F_R); 335 336 if ((file->size < EI_NIDENT) || memcmp(buf, "\x7f" "ELF", 4)) 337 { 338 printf("Not an ELF payload: %s\n", argv[1]); 339 return 1; 340 } 341 342 Payload *payload = get_payload(buf); 343 344 if (payload == NULL) 345 { 346 printf("Unrecognised object size.\n"); 347 return 1; 348 } 349 350 if (file->size < 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 if (file->size < payload->program_header_extent()) 359 { 360 printf("Program headers incomplete.\n"); 361 return 1; 362 } 363 364 /* Make appropriate segments, although program segments could be made 365 interoperable with these. */ 366 367 Segment *segments[payload->segments() + 1]; 368 369 for (unsigned int i = 0; i < payload->segments(); i++) 370 { 371 ProgramSegment *ps = payload->segment(i); 372 373 printf("Segment(0x%lx, 0x%lx, 0x%x, 0x%lx, 0x%lx): %s\n", 374 ps->region_address(), 375 ps->region_size(), 376 ps->region_flags(), 377 ps->file_offset(), 378 ps->file_contents(), 379 ps->loadable() ? "loadable" : "other"); 380 381 if (ps->loadable()) 382 segments[i] = ps->segment(); 383 else 384 segments[i] = NULL; 385 } 386 387 /* Copy the payload regions to new dataspaces. */ 388 389 address_t program_start = payload->entry_point(); 390 offset_t initial_stack_size = 16 * L4_PAGESIZE; 391 392 Segment stack(Utcb_area_start - initial_stack_size, initial_stack_size, L4_FPAGE_RW); 393 394 segments[payload->segments()] = &stack; 395 396 for (unsigned int i = 0; i < payload->segments() + 1; i++) 397 { 398 Segment *segment = segments[i]; 399 400 if (segment == NULL) 401 continue; 402 403 if (segment->file_contents()) 404 { 405 file_t *file = client_open(argv[1], file_opening_flags(segment->region_flags())); 406 407 if (file == NULL) 408 { 409 printf("Could not open file for segment.\n"); 410 return 1; 411 } 412 413 err = segment->fill(file); 414 415 if (err) 416 { 417 printf("Could not fill segment from file.\n"); 418 return 1; 419 } 420 } 421 else 422 { 423 err = segment->allocate(); 424 425 if (err) 426 { 427 printf("Could not allocate segment.\n"); 428 return 1; 429 } 430 } 431 } 432 433 /* Start the pager. */ 434 435 ipc_server_config_type config; 436 pthread_t pager_thread; 437 pthread_attr_t attr; 438 439 pthread_attr_init(&attr); 440 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); 441 442 init_pager(&config); 443 444 for (unsigned int i = 0; i < payload->segments() + 1; i++) 445 { 446 Segment *segment = segments[i]; 447 448 if (segment == NULL) 449 continue; 450 451 exec_pager.add(segment->region()); 452 } 453 454 err = pthread_create(&pager_thread, &attr, ipc_server_start_mainloop, &config); 455 456 if (err) 457 { 458 printf("Could not start pager thread.\n"); 459 return 1; 460 } 461 462 err = start_pager(&config, pager_thread); 463 464 if (err) 465 { 466 printf("Could not start pager.\n"); 467 return 1; 468 } 469 470 /* Configure the environment for the task, specifying the pager (and exception 471 handler plus region mapper). */ 472 473 Process process; 474 475 err = process.configure(config.server); 476 477 if (err) 478 { 479 printf("Could not configure task.\n"); 480 return 1; 481 } 482 483 /* Populate a thread stack with argument and environment details. */ 484 485 Stack st(stack); 486 487 /* NOTE: Environment vector is currently not defined. */ 488 489 char *envp[] = {NULL}; 490 491 st.populate(argc - 1, argv + 1, envp); 492 493 /* Start the new thread in the given stack. */ 494 495 printf("Run thread...\n"); 496 497 err = process.thread_start(program_start, st); 498 499 if (err) 500 { 501 printf("Could not run thread.\n"); 502 return 1; 503 } 504 505 printf("Finished.\n"); 506 while (1); 507 508 return 0; 509 } 510 511 /* vim: tabstop=2 expandtab shiftwidth=2 512 */