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