Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2002 Jeff Dike (jdike@karaya.com) |
| 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
| 6 | #include <stdlib.h> |
| 7 | #include <unistd.h> |
| 8 | #include <errno.h> |
| 9 | #include <signal.h> |
| 10 | #include <setjmp.h> |
| 11 | #include <sched.h> |
| 12 | #include <sys/wait.h> |
| 13 | #include <sys/mman.h> |
| 14 | #include <sys/user.h> |
| 15 | #include <asm/unistd.h> |
| 16 | #include "user.h" |
| 17 | #include "ptrace_user.h" |
| 18 | #include "time_user.h" |
| 19 | #include "sysdep/ptrace.h" |
| 20 | #include "user_util.h" |
| 21 | #include "kern_util.h" |
| 22 | #include "skas.h" |
| 23 | #include "sysdep/sigcontext.h" |
| 24 | #include "os.h" |
| 25 | #include "proc_mm.h" |
| 26 | #include "skas_ptrace.h" |
| 27 | #include "chan_user.h" |
| 28 | #include "signal_user.h" |
| 29 | #include "registers.h" |
| 30 | |
| 31 | int is_skas_winch(int pid, int fd, void *data) |
| 32 | { |
| 33 | if(pid != os_getpid()) |
| 34 | return(0); |
| 35 | |
| 36 | register_winch_irq(-1, fd, -1, data); |
| 37 | return(1); |
| 38 | } |
| 39 | |
| 40 | static void handle_segv(int pid) |
| 41 | { |
| 42 | struct ptrace_faultinfo fault; |
| 43 | int err; |
| 44 | |
| 45 | err = ptrace(PTRACE_FAULTINFO, pid, 0, &fault); |
| 46 | if(err) |
| 47 | panic("handle_segv - PTRACE_FAULTINFO failed, errno = %d\n", |
| 48 | errno); |
| 49 | |
| 50 | segv(fault.addr, 0, FAULT_WRITE(fault.is_write), 1, NULL); |
| 51 | } |
| 52 | |
| 53 | /*To use the same value of using_sysemu as the caller, ask it that value (in local_using_sysemu)*/ |
| 54 | static void handle_trap(int pid, union uml_pt_regs *regs, int local_using_sysemu) |
| 55 | { |
| 56 | int err, status; |
| 57 | |
| 58 | /* Mark this as a syscall */ |
| 59 | UPT_SYSCALL_NR(regs) = PT_SYSCALL_NR(regs->skas.regs); |
| 60 | |
| 61 | if (!local_using_sysemu) |
| 62 | { |
| 63 | err = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET, __NR_getpid); |
| 64 | if(err < 0) |
| 65 | panic("handle_trap - nullifying syscall failed errno = %d\n", |
| 66 | errno); |
| 67 | |
| 68 | err = ptrace(PTRACE_SYSCALL, pid, 0, 0); |
| 69 | if(err < 0) |
| 70 | panic("handle_trap - continuing to end of syscall failed, " |
| 71 | "errno = %d\n", errno); |
| 72 | |
| 73 | CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED)); |
| 74 | if((err < 0) || !WIFSTOPPED(status) || |
| 75 | (WSTOPSIG(status) != SIGTRAP + 0x80)) |
| 76 | panic("handle_trap - failed to wait at end of syscall, " |
| 77 | "errno = %d, status = %d\n", errno, status); |
| 78 | } |
| 79 | |
| 80 | handle_syscall(regs); |
| 81 | } |
| 82 | |
| 83 | static int userspace_tramp(void *arg) |
| 84 | { |
| 85 | init_new_thread_signals(0); |
| 86 | enable_timer(); |
| 87 | ptrace(PTRACE_TRACEME, 0, 0, 0); |
| 88 | os_stop_process(os_getpid()); |
| 89 | return(0); |
| 90 | } |
| 91 | |
| 92 | /* Each element set once, and only accessed by a single processor anyway */ |
| 93 | #undef NR_CPUS |
| 94 | #define NR_CPUS 1 |
| 95 | int userspace_pid[NR_CPUS]; |
| 96 | |
| 97 | void start_userspace(int cpu) |
| 98 | { |
| 99 | void *stack; |
| 100 | unsigned long sp; |
| 101 | int pid, status, n; |
| 102 | |
| 103 | stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC, |
| 104 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 105 | if(stack == MAP_FAILED) |
| 106 | panic("start_userspace : mmap failed, errno = %d", errno); |
| 107 | sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *); |
| 108 | |
| 109 | pid = clone(userspace_tramp, (void *) sp, |
| 110 | CLONE_FILES | CLONE_VM | SIGCHLD, NULL); |
| 111 | if(pid < 0) |
| 112 | panic("start_userspace : clone failed, errno = %d", errno); |
| 113 | |
| 114 | do { |
| 115 | CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED)); |
| 116 | if(n < 0) |
| 117 | panic("start_userspace : wait failed, errno = %d", |
| 118 | errno); |
| 119 | } while(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGVTALRM)); |
| 120 | |
| 121 | if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP)) |
| 122 | panic("start_userspace : expected SIGSTOP, got status = %d", |
| 123 | status); |
| 124 | |
| 125 | if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL, (void *)PTRACE_O_TRACESYSGOOD) < 0) |
| 126 | panic("start_userspace : PTRACE_SETOPTIONS failed, errno=%d\n", |
| 127 | errno); |
| 128 | |
| 129 | if(munmap(stack, PAGE_SIZE) < 0) |
| 130 | panic("start_userspace : munmap failed, errno = %d\n", errno); |
| 131 | |
| 132 | userspace_pid[cpu] = pid; |
| 133 | } |
| 134 | |
| 135 | void userspace(union uml_pt_regs *regs) |
| 136 | { |
| 137 | int err, status, op, pid = userspace_pid[0]; |
| 138 | int local_using_sysemu; /*To prevent races if using_sysemu changes under us.*/ |
| 139 | |
| 140 | while(1){ |
| 141 | restore_registers(pid, regs); |
| 142 | |
| 143 | /* Now we set local_using_sysemu to be used for one loop */ |
| 144 | local_using_sysemu = get_using_sysemu(); |
| 145 | |
| 146 | op = SELECT_PTRACE_OPERATION(local_using_sysemu, singlestepping(NULL)); |
| 147 | |
| 148 | err = ptrace(op, pid, 0, 0); |
| 149 | if(err) |
| 150 | panic("userspace - could not resume userspace process, " |
| 151 | "pid=%d, ptrace operation = %d, errno = %d\n", |
| 152 | op, errno); |
| 153 | |
| 154 | CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED)); |
| 155 | if(err < 0) |
| 156 | panic("userspace - waitpid failed, errno = %d\n", |
| 157 | errno); |
| 158 | |
| 159 | regs->skas.is_user = 1; |
| 160 | save_registers(pid, regs); |
| 161 | UPT_SYSCALL_NR(regs) = -1; /* Assume: It's not a syscall */ |
| 162 | |
| 163 | if(WIFSTOPPED(status)){ |
| 164 | switch(WSTOPSIG(status)){ |
| 165 | case SIGSEGV: |
| 166 | handle_segv(pid); |
| 167 | break; |
| 168 | case SIGTRAP + 0x80: |
| 169 | handle_trap(pid, regs, local_using_sysemu); |
| 170 | break; |
| 171 | case SIGTRAP: |
| 172 | relay_signal(SIGTRAP, regs); |
| 173 | break; |
| 174 | case SIGIO: |
| 175 | case SIGVTALRM: |
| 176 | case SIGILL: |
| 177 | case SIGBUS: |
| 178 | case SIGFPE: |
| 179 | case SIGWINCH: |
| 180 | user_signal(WSTOPSIG(status), regs); |
| 181 | break; |
| 182 | default: |
| 183 | printk("userspace - child stopped with signal " |
| 184 | "%d\n", WSTOPSIG(status)); |
| 185 | } |
| 186 | interrupt_end(); |
| 187 | |
| 188 | /* Avoid -ERESTARTSYS handling in host */ |
| 189 | PT_SYSCALL_NR(regs->skas.regs) = -1; |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | void new_thread(void *stack, void **switch_buf_ptr, void **fork_buf_ptr, |
| 195 | void (*handler)(int)) |
| 196 | { |
| 197 | unsigned long flags; |
| 198 | sigjmp_buf switch_buf, fork_buf; |
| 199 | |
| 200 | *switch_buf_ptr = &switch_buf; |
| 201 | *fork_buf_ptr = &fork_buf; |
| 202 | |
| 203 | /* Somewhat subtle - siglongjmp restores the signal mask before doing |
| 204 | * the longjmp. This means that when jumping from one stack to another |
| 205 | * when the target stack has interrupts enabled, an interrupt may occur |
| 206 | * on the source stack. This is bad when starting up a process because |
| 207 | * it's not supposed to get timer ticks until it has been scheduled. |
| 208 | * So, we disable interrupts around the sigsetjmp to ensure that |
| 209 | * they can't happen until we get back here where they are safe. |
| 210 | */ |
| 211 | flags = get_signals(); |
| 212 | block_signals(); |
| 213 | if(sigsetjmp(fork_buf, 1) == 0) |
| 214 | new_thread_proc(stack, handler); |
| 215 | |
| 216 | remove_sigstack(); |
| 217 | |
| 218 | set_signals(flags); |
| 219 | } |
| 220 | |
| 221 | void thread_wait(void *sw, void *fb) |
| 222 | { |
| 223 | sigjmp_buf buf, **switch_buf = sw, *fork_buf; |
| 224 | |
| 225 | *switch_buf = &buf; |
| 226 | fork_buf = fb; |
| 227 | if(sigsetjmp(buf, 1) == 0) |
| 228 | siglongjmp(*fork_buf, 1); |
| 229 | } |
| 230 | |
| 231 | void switch_threads(void *me, void *next) |
| 232 | { |
| 233 | sigjmp_buf my_buf, **me_ptr = me, *next_buf = next; |
| 234 | |
| 235 | *me_ptr = &my_buf; |
| 236 | if(sigsetjmp(my_buf, 1) == 0) |
| 237 | siglongjmp(*next_buf, 1); |
| 238 | } |
| 239 | |
| 240 | static sigjmp_buf initial_jmpbuf; |
| 241 | |
| 242 | /* XXX Make these percpu */ |
| 243 | static void (*cb_proc)(void *arg); |
| 244 | static void *cb_arg; |
| 245 | static sigjmp_buf *cb_back; |
| 246 | |
| 247 | int start_idle_thread(void *stack, void *switch_buf_ptr, void **fork_buf_ptr) |
| 248 | { |
| 249 | sigjmp_buf **switch_buf = switch_buf_ptr; |
| 250 | int n; |
| 251 | |
| 252 | *fork_buf_ptr = &initial_jmpbuf; |
| 253 | n = sigsetjmp(initial_jmpbuf, 1); |
| 254 | if(n == 0) |
| 255 | new_thread_proc((void *) stack, new_thread_handler); |
| 256 | else if(n == 1) |
| 257 | remove_sigstack(); |
| 258 | else if(n == 2){ |
| 259 | (*cb_proc)(cb_arg); |
| 260 | siglongjmp(*cb_back, 1); |
| 261 | } |
| 262 | else if(n == 3){ |
| 263 | kmalloc_ok = 0; |
| 264 | return(0); |
| 265 | } |
| 266 | else if(n == 4){ |
| 267 | kmalloc_ok = 0; |
| 268 | return(1); |
| 269 | } |
| 270 | siglongjmp(**switch_buf, 1); |
| 271 | } |
| 272 | |
| 273 | void remove_sigstack(void) |
| 274 | { |
| 275 | stack_t stack = ((stack_t) { .ss_flags = SS_DISABLE, |
| 276 | .ss_sp = NULL, |
| 277 | .ss_size = 0 }); |
| 278 | |
| 279 | if(sigaltstack(&stack, NULL) != 0) |
| 280 | panic("disabling signal stack failed, errno = %d\n", errno); |
| 281 | } |
| 282 | |
| 283 | void initial_thread_cb_skas(void (*proc)(void *), void *arg) |
| 284 | { |
| 285 | sigjmp_buf here; |
| 286 | |
| 287 | cb_proc = proc; |
| 288 | cb_arg = arg; |
| 289 | cb_back = &here; |
| 290 | |
| 291 | block_signals(); |
| 292 | if(sigsetjmp(here, 1) == 0) |
| 293 | siglongjmp(initial_jmpbuf, 2); |
| 294 | unblock_signals(); |
| 295 | |
| 296 | cb_proc = NULL; |
| 297 | cb_arg = NULL; |
| 298 | cb_back = NULL; |
| 299 | } |
| 300 | |
| 301 | void halt_skas(void) |
| 302 | { |
| 303 | block_signals(); |
| 304 | siglongjmp(initial_jmpbuf, 3); |
| 305 | } |
| 306 | |
| 307 | void reboot_skas(void) |
| 308 | { |
| 309 | block_signals(); |
| 310 | siglongjmp(initial_jmpbuf, 4); |
| 311 | } |
| 312 | |
| 313 | void switch_mm_skas(int mm_fd) |
| 314 | { |
| 315 | int err; |
| 316 | |
| 317 | #warning need cpu pid in switch_mm_skas |
| 318 | err = ptrace(PTRACE_SWITCH_MM, userspace_pid[0], 0, mm_fd); |
| 319 | if(err) |
| 320 | panic("switch_mm_skas - PTRACE_SWITCH_MM failed, errno = %d\n", |
| 321 | errno); |
| 322 | } |
| 323 | |
| 324 | void kill_off_processes_skas(void) |
| 325 | { |
| 326 | #warning need to loop over userspace_pids in kill_off_processes_skas |
| 327 | os_kill_ptraced_process(userspace_pid[0], 1); |
| 328 | } |
| 329 | |
| 330 | /* |
| 331 | * Overrides for Emacs so that we follow Linus's tabbing style. |
| 332 | * Emacs will notice this stuff at the end of the file and automatically |
| 333 | * adjust the settings for this buffer only. This must remain at the end |
| 334 | * of the file. |
| 335 | * --------------------------------------------------------------------------- |
| 336 | * Local variables: |
| 337 | * c-file-style: "linux" |
| 338 | * End: |
| 339 | */ |