1 /* 2 * Copyright (C) 2002- 2004 Jeff Dike (jdike@addtoit.com) 3 * Licensed under the GPL 4 */ 5 6 #include <stdlib.h> 7 #include <string.h> 8 #include <unistd.h> 9 #include <errno.h> 10 #include <signal.h> 11 #include <setjmp.h> 12 #include <sched.h> 13 #include <sys/wait.h> 14 #include <sys/mman.h> 15 #include <sys/user.h> 16 #include <sys/time.h> 17 #include <asm/unistd.h> 18 #include <asm/types.h> 19 #include "user.h" 20 #include "ptrace_user.h" 21 #include "time_user.h" 22 #include "sysdep/ptrace.h" 23 #include "user_util.h" 24 #include "kern_util.h" 25 #include "skas.h" 26 #include "stub-data.h" 27 #include "mm_id.h" 28 #include "sysdep/sigcontext.h" 29 #include "sysdep/stub.h" 30 #include "os.h" 31 #include "proc_mm.h" 32 #include "skas_ptrace.h" 33 #include "chan_user.h" 34 #include "signal_user.h" 35 #include "registers.h" 36 #include "mem.h" 37 #include "uml-config.h" 38 #include "process.h" 39 40 int is_skas_winch(int pid, int fd, void *data) 41 { 42 if(pid != os_getpgrp()) 43 return(0); 44 45 register_winch_irq(-1, fd, -1, data); 46 return(1); 47 } 48 49 void wait_stub_done(int pid, int sig, char * fname) 50 { 51 int n, status, err; 52 53 do { 54 if ( sig != -1 ) { 55 err = ptrace(PTRACE_CONT, pid, 0, sig); 56 if(err) 57 panic("%s : continue failed, errno = %d\n", 58 fname, errno); 59 } 60 sig = 0; 61 62 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED)); 63 } while((n >= 0) && WIFSTOPPED(status) && 64 ((WSTOPSIG(status) == SIGVTALRM) || 65 /* running UML inside a detached screen can cause 66 * SIGWINCHes 67 */ 68 (WSTOPSIG(status) == SIGWINCH))); 69 70 if((n < 0) || !WIFSTOPPED(status) || 71 (WSTOPSIG(status) != SIGUSR1 && WSTOPSIG(status) != SIGTRAP)){ 72 unsigned long regs[FRAME_SIZE]; 73 if(ptrace(PTRACE_GETREGS, pid, 0, regs) < 0) 74 printk("Failed to get registers from stub, " 75 "errno = %d\n", errno); 76 else { 77 int i; 78 79 printk("Stub registers -\n"); 80 for(i = 0; i < FRAME_SIZE; i++) 81 printk("\t%d - %lx\n", i, regs[i]); 82 } 83 panic("%s : failed to wait for SIGUSR1/SIGTRAP, " 84 "pid = %d, n = %d, errno = %d, status = 0x%x\n", 85 fname, pid, n, errno, status); 86 } 87 } 88 89 void get_skas_faultinfo(int pid, struct faultinfo * fi) 90 { 91 int err; 92 93 if(ptrace_faultinfo){ 94 err = ptrace(PTRACE_FAULTINFO, pid, 0, fi); 95 if(err) 96 panic("get_skas_faultinfo - PTRACE_FAULTINFO failed, " 97 "errno = %d\n", errno); 98 99 /* Special handling for i386, which has different structs */ 100 if (sizeof(struct ptrace_faultinfo) < sizeof(struct faultinfo)) 101 memset((char *)fi + sizeof(struct ptrace_faultinfo), 0, 102 sizeof(struct faultinfo) - 103 sizeof(struct ptrace_faultinfo)); 104 } 105 else { 106 wait_stub_done(pid, SIGSEGV, "get_skas_faultinfo"); 107 108 /* faultinfo is prepared by the stub-segv-handler at start of 109 * the stub stack page. We just have to copy it. 110 */ 111 memcpy(fi, (void *)current_stub_stack(), sizeof(*fi)); 112 } 113 } 114 115 static void handle_segv(int pid, union uml_pt_regs * regs) 116 { 117 get_skas_faultinfo(pid, ®s->skas.faultinfo); 118 segv(regs->skas.faultinfo, 0, 1, NULL); 119 } 120 121 /*To use the same value of using_sysemu as the caller, ask it that value (in local_using_sysemu)*/ 122 static void handle_trap(int pid, union uml_pt_regs *regs, int local_using_sysemu) 123 { 124 int err, status; 125 126 /* Mark this as a syscall */ 127 UPT_SYSCALL_NR(regs) = PT_SYSCALL_NR(regs->skas.regs); 128 129 if (!local_using_sysemu) 130 { 131 err = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET, __NR_getpid); 132 if(err < 0) 133 panic("handle_trap - nullifying syscall failed errno = %d\n", 134 errno); 135 136 err = ptrace(PTRACE_SYSCALL, pid, 0, 0); 137 if(err < 0) 138 panic("handle_trap - continuing to end of syscall failed, " 139 "errno = %d\n", errno); 140 141 CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED)); 142 if((err < 0) || !WIFSTOPPED(status) || 143 (WSTOPSIG(status) != SIGTRAP + 0x80)) 144 panic("handle_trap - failed to wait at end of syscall, " 145 "errno = %d, status = %d\n", errno, status); 146 } 147 148 handle_syscall(regs); 149 } 150 151 extern int __syscall_stub_start; 152 int stub_code_fd = -1; 153 __u64 stub_code_offset; 154 155 static int userspace_tramp(void *stack) 156 { 157 void *addr; 158 159 ptrace(PTRACE_TRACEME, 0, 0, 0); 160 161 init_new_thread_signals(1); 162 enable_timer(); 163 164 if(!proc_mm){ 165 /* This has a pte, but it can't be mapped in with the usual 166 * tlb_flush mechanism because this is part of that mechanism 167 */ 168 addr = mmap64((void *) UML_CONFIG_STUB_CODE, page_size(), 169 PROT_EXEC, MAP_FIXED | MAP_PRIVATE, 170 stub_code_fd, stub_code_offset); 171 if(addr == MAP_FAILED){ 172 printk("mapping stub code failed, errno = %d\n", 173 errno); 174 exit(1); 175 } 176 177 if(stack != NULL){ 178 int fd; 179 __u64 offset; 180 181 fd = phys_mapping(to_phys(stack), &offset); 182 addr = mmap((void *) UML_CONFIG_STUB_DATA, page_size(), 183 PROT_READ | PROT_WRITE, 184 MAP_FIXED | MAP_SHARED, fd, offset); 185 if(addr == MAP_FAILED){ 186 printk("mapping stub stack failed, " 187 "errno = %d\n", errno); 188 exit(1); 189 } 190 } 191 } 192 if(!ptrace_faultinfo){ 193 unsigned long v = UML_CONFIG_STUB_CODE + 194 (unsigned long) stub_segv_handler - 195 (unsigned long) &__syscall_stub_start; 196 197 set_sigstack((void *) UML_CONFIG_STUB_DATA, page_size()); 198 set_handler(SIGSEGV, (void *) v, SA_ONSTACK, 199 SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, 200 SIGUSR1, -1); 201 } 202 203 os_stop_process(os_getpid()); 204 return(0); 205 } 206 207 /* Each element set once, and only accessed by a single processor anyway */ 208 #undef NR_CPUS 209 #define NR_CPUS 1 210 int userspace_pid[NR_CPUS]; 211 212 int start_userspace(unsigned long stub_stack) 213 { 214 void *stack; 215 unsigned long sp; 216 int pid, status, n, flags; 217 218 if ( stub_code_fd == -1 ) 219 stub_code_fd = phys_mapping(to_phys(&__syscall_stub_start), 220 &stub_code_offset); 221 222 stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC, 223 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 224 if(stack == MAP_FAILED) 225 panic("start_userspace : mmap failed, errno = %d", errno); 226 sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *); 227 228 flags = CLONE_FILES | SIGCHLD; 229 if(proc_mm) flags |= CLONE_VM; 230 pid = clone(userspace_tramp, (void *) sp, flags, (void *) stub_stack); 231 if(pid < 0) 232 panic("start_userspace : clone failed, errno = %d", errno); 233 234 do { 235 CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED)); 236 if(n < 0) 237 panic("start_userspace : wait failed, errno = %d", 238 errno); 239 } while(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGVTALRM)); 240 241 if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP)) 242 panic("start_userspace : expected SIGSTOP, got status = %d", 243 status); 244 245 if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL, (void *)PTRACE_O_TRACESYSGOOD) < 0) 246 panic("start_userspace : PTRACE_SETOPTIONS failed, errno=%d\n", 247 errno); 248 249 if(munmap(stack, PAGE_SIZE) < 0) 250 panic("start_userspace : munmap failed, errno = %d\n", errno); 251 252 return(pid); 253 } 254 255 void userspace(union uml_pt_regs *regs) 256 { 257 int err, status, op, pid = userspace_pid[0]; 258 int local_using_sysemu; /*To prevent races if using_sysemu changes under us.*/ 259 260 while(1){ 261 restore_registers(pid, regs); 262 263 /* Now we set local_using_sysemu to be used for one loop */ 264 local_using_sysemu = get_using_sysemu(); 265 266 op = SELECT_PTRACE_OPERATION(local_using_sysemu, singlestepping(NULL)); 267 268 err = ptrace(op, pid, 0, 0); 269 if(err) 270 panic("userspace - could not resume userspace process, " 271 "pid=%d, ptrace operation = %d, errno = %d\n", 272 op, errno); 273 274 CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED)); 275 if(err < 0) 276 panic("userspace - waitpid failed, errno = %d\n", 277 errno); 278 279 regs->skas.is_user = 1; 280 save_registers(pid, regs); 281 UPT_SYSCALL_NR(regs) = -1; /* Assume: It's not a syscall */ 282 283 if(WIFSTOPPED(status)){ 284 switch(WSTOPSIG(status)){ 285 case SIGSEGV: 286 if(PTRACE_FULL_FAULTINFO || !ptrace_faultinfo) 287 user_signal(SIGSEGV, regs, pid); 288 else handle_segv(pid, regs); 289 break; 290 case SIGTRAP + 0x80: 291 handle_trap(pid, regs, local_using_sysemu); 292 break; 293 case SIGTRAP: 294 relay_signal(SIGTRAP, regs); 295 break; 296 case SIGIO: 297 case SIGVTALRM: 298 case SIGILL: 299 case SIGBUS: 300 case SIGFPE: 301 case SIGWINCH: 302 user_signal(WSTOPSIG(status), regs, pid); 303 break; 304 default: 305 printk("userspace - child stopped with signal " 306 "%d\n", WSTOPSIG(status)); 307 } 308 pid = userspace_pid[0]; 309 interrupt_end(); 310 311 /* Avoid -ERESTARTSYS handling in host */ 312 PT_SYSCALL_NR(regs->skas.regs) = -1; 313 } 314 } 315 } 316 #define INIT_JMP_NEW_THREAD 0 317 #define INIT_JMP_REMOVE_SIGSTACK 1 318 #define INIT_JMP_CALLBACK 2 319 #define INIT_JMP_HALT 3 320 #define INIT_JMP_REBOOT 4 321 322 323 int copy_context_skas0(unsigned long new_stack, int pid) 324 { 325 int err; 326 unsigned long regs[MAX_REG_NR]; 327 unsigned long current_stack = current_stub_stack(); 328 struct stub_data *data = (struct stub_data *) current_stack; 329 struct stub_data *child_data = (struct stub_data *) new_stack; 330 __u64 new_offset; 331 int new_fd = phys_mapping(to_phys((void *)new_stack), &new_offset); 332 333 /* prepare offset and fd of child's stack as argument for parent's 334 * and child's mmap2 calls 335 */ 336 *data = ((struct stub_data) { .offset = MMAP_OFFSET(new_offset), 337 .fd = new_fd, 338 .timer = ((struct itimerval) 339 { { 0, 1000000 / hz() }, 340 { 0, 1000000 / hz() }})}); 341 get_safe_registers(regs); 342 343 /* Set parent's instruction pointer to start of clone-stub */ 344 regs[REGS_IP_INDEX] = UML_CONFIG_STUB_CODE + 345 (unsigned long) stub_clone_handler - 346 (unsigned long) &__syscall_stub_start; 347 regs[REGS_SP_INDEX] = UML_CONFIG_STUB_DATA + PAGE_SIZE - 348 sizeof(void *); 349 err = ptrace_setregs(pid, regs); 350 if(err < 0) 351 panic("copy_context_skas0 : PTRACE_SETREGS failed, " 352 "pid = %d, errno = %d\n", pid, errno); 353 354 /* set a well known return code for detection of child write failure */ 355 child_data->err = 12345678; 356 357 /* Wait, until parent has finished its work: read child's pid from 358 * parent's stack, and check, if bad result. 359 */ 360 wait_stub_done(pid, 0, "copy_context_skas0"); 361 362 pid = data->err; 363 if(pid < 0) 364 panic("copy_context_skas0 - stub-parent reports error %d\n", 365 pid); 366 367 /* Wait, until child has finished too: read child's result from 368 * child's stack and check it. 369 */ 370 wait_stub_done(pid, -1, "copy_context_skas0"); 371 if (child_data->err != UML_CONFIG_STUB_DATA) 372 panic("copy_context_skas0 - stub-child reports error %d\n", 373 child_data->err); 374 375 if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL, 376 (void *)PTRACE_O_TRACESYSGOOD) < 0) 377 panic("copy_context_skas0 : PTRACE_SETOPTIONS failed, " 378 "errno = %d\n", errno); 379 380 return pid; 381 } 382 383 /* 384 * This is used only, if stub pages are needed, while proc_mm is 385 * availabl. Opening /proc/mm creates a new mm_context, which lacks 386 * the stub-pages. Thus, we map them using /proc/mm-fd 387 */ 388 void map_stub_pages(int fd, unsigned long code, 389 unsigned long data, unsigned long stack) 390 { 391 struct proc_mm_op mmop; 392 int n; 393 394 mmop = ((struct proc_mm_op) { .op = MM_MMAP, 395 .u = 396 { .mmap = 397 { .addr = code, 398 .len = PAGE_SIZE, 399 .prot = PROT_EXEC, 400 .flags = MAP_FIXED | MAP_PRIVATE, 401 .fd = stub_code_fd, 402 .offset = stub_code_offset 403 } } }); 404 n = os_write_file(fd, &mmop, sizeof(mmop)); 405 if(n != sizeof(mmop)) 406 panic("map_stub_pages : /proc/mm map for code failed, " 407 "err = %d\n", -n); 408 409 if ( stack ) { 410 __u64 map_offset; 411 int map_fd = phys_mapping(to_phys((void *)stack), &map_offset); 412 mmop = ((struct proc_mm_op) 413 { .op = MM_MMAP, 414 .u = 415 { .mmap = 416 { .addr = data, 417 .len = PAGE_SIZE, 418 .prot = PROT_READ | PROT_WRITE, 419 .flags = MAP_FIXED | MAP_SHARED, 420 .fd = map_fd, 421 .offset = map_offset 422 } } }); 423 n = os_write_file(fd, &mmop, sizeof(mmop)); 424 if(n != sizeof(mmop)) 425 panic("map_stub_pages : /proc/mm map for data failed, " 426 "err = %d\n", -n); 427 } 428 } 429 430 void new_thread(void *stack, void **switch_buf_ptr, void **fork_buf_ptr, 431 void (*handler)(int)) 432 { 433 unsigned long flags; 434 sigjmp_buf switch_buf, fork_buf; 435 436 *switch_buf_ptr = &switch_buf; 437 *fork_buf_ptr = &fork_buf; 438 439 /* Somewhat subtle - siglongjmp restores the signal mask before doing 440 * the longjmp. This means that when jumping from one stack to another 441 * when the target stack has interrupts enabled, an interrupt may occur 442 * on the source stack. This is bad when starting up a process because 443 * it's not supposed to get timer ticks until it has been scheduled. 444 * So, we disable interrupts around the sigsetjmp to ensure that 445 * they can't happen until we get back here where they are safe. 446 */ 447 flags = get_signals(); 448 block_signals(); 449 if(sigsetjmp(fork_buf, 1) == 0) 450 new_thread_proc(stack, handler); 451 452 remove_sigstack(); 453 454 set_signals(flags); 455 } 456 457 void thread_wait(void *sw, void *fb) 458 { 459 sigjmp_buf buf, **switch_buf = sw, *fork_buf; 460 461 *switch_buf = &buf; 462 fork_buf = fb; 463 if(sigsetjmp(buf, 1) == 0) 464 siglongjmp(*fork_buf, INIT_JMP_REMOVE_SIGSTACK); 465 } 466 467 void switch_threads(void *me, void *next) 468 { 469 sigjmp_buf my_buf, **me_ptr = me, *next_buf = next; 470 471 *me_ptr = &my_buf; 472 if(sigsetjmp(my_buf, 1) == 0) 473 siglongjmp(*next_buf, 1); 474 } 475 476 static sigjmp_buf initial_jmpbuf; 477 478 /* XXX Make these percpu */ 479 static void (*cb_proc)(void *arg); 480 static void *cb_arg; 481 static sigjmp_buf *cb_back; 482 483 int start_idle_thread(void *stack, void *switch_buf_ptr, void **fork_buf_ptr) 484 { 485 sigjmp_buf **switch_buf = switch_buf_ptr; 486 int n; 487 488 set_handler(SIGWINCH, (__sighandler_t) sig_handler, 489 SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGALRM, 490 SIGVTALRM, -1); 491 492 *fork_buf_ptr = &initial_jmpbuf; 493 n = sigsetjmp(initial_jmpbuf, 1); 494 switch(n){ 495 case INIT_JMP_NEW_THREAD: 496 new_thread_proc((void *) stack, new_thread_handler); 497 break; 498 case INIT_JMP_REMOVE_SIGSTACK: 499 remove_sigstack(); 500 break; 501 case INIT_JMP_CALLBACK: 502 (*cb_proc)(cb_arg); 503 siglongjmp(*cb_back, 1); 504 break; 505 case INIT_JMP_HALT: 506 kmalloc_ok = 0; 507 return(0); 508 case INIT_JMP_REBOOT: 509 kmalloc_ok = 0; 510 return(1); 511 default: 512 panic("Bad sigsetjmp return in start_idle_thread - %d\n", n); 513 } 514 siglongjmp(**switch_buf, 1); 515 } 516 517 void remove_sigstack(void) 518 { 519 stack_t stack = ((stack_t) { .ss_flags = SS_DISABLE, 520 .ss_sp = NULL, 521 .ss_size = 0 }); 522 523 if(sigaltstack(&stack, NULL) != 0) 524 panic("disabling signal stack failed, errno = %d\n", errno); 525 } 526 527 void initial_thread_cb_skas(void (*proc)(void *), void *arg) 528 { 529 sigjmp_buf here; 530 531 cb_proc = proc; 532 cb_arg = arg; 533 cb_back = &here; 534 535 block_signals(); 536 if(sigsetjmp(here, 1) == 0) 537 siglongjmp(initial_jmpbuf, INIT_JMP_CALLBACK); 538 unblock_signals(); 539 540 cb_proc = NULL; 541 cb_arg = NULL; 542 cb_back = NULL; 543 } 544 545 void halt_skas(void) 546 { 547 block_signals(); 548 siglongjmp(initial_jmpbuf, INIT_JMP_HALT); 549 } 550 551 void reboot_skas(void) 552 { 553 block_signals(); 554 siglongjmp(initial_jmpbuf, INIT_JMP_REBOOT); 555 } 556 557 void switch_mm_skas(struct mm_id *mm_idp) 558 { 559 int err; 560 561 #warning need cpu pid in switch_mm_skas 562 if(proc_mm){ 563 err = ptrace(PTRACE_SWITCH_MM, userspace_pid[0], 0, 564 mm_idp->u.mm_fd); 565 if(err) 566 panic("switch_mm_skas - PTRACE_SWITCH_MM failed, " 567 "errno = %d\n", errno); 568 } 569 else userspace_pid[0] = mm_idp->u.pid; 570 } 571 572 /* 573 * Overrides for Emacs so that we follow Linus's tabbing style. 574 * Emacs will notice this stuff at the end of the file and automatically 575 * adjust the settings for this buffer only. This must remain at the end 576 * of the file. 577 * --------------------------------------------------------------------------- 578 * Local variables: 579 * c-file-style: "linux" 580 * End: 581 */ 582