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