Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 1 | /* |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 2 | * Copyright (C) 2002- 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
| 6 | #include <stdlib.h> |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 7 | #include <unistd.h> |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 8 | #include <sched.h> |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 9 | #include <errno.h> |
| 10 | #include <string.h> |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 11 | #include <sys/mman.h> |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 12 | #include <sys/ptrace.h> |
| 13 | #include <sys/wait.h> |
| 14 | #include <asm/unistd.h> |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 15 | #include "as-layout.h" |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 16 | #include "chan_user.h" |
| 17 | #include "kern_constants.h" |
| 18 | #include "mem.h" |
| 19 | #include "os.h" |
| 20 | #include "process.h" |
| 21 | #include "proc_mm.h" |
| 22 | #include "ptrace_user.h" |
| 23 | #include "registers.h" |
| 24 | #include "skas.h" |
| 25 | #include "skas_ptrace.h" |
| 26 | #include "user.h" |
| 27 | #include "sysdep/stub.h" |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 28 | |
| 29 | int is_skas_winch(int pid, int fd, void *data) |
| 30 | { |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 31 | if (pid != getpgrp()) |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 32 | return 0; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 33 | |
Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 34 | register_winch_irq(-1, fd, -1, data, 0); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 35 | return 1; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 36 | } |
| 37 | |
Jeff Dike | f30c2c9 | 2007-05-06 14:51:29 -0700 | [diff] [blame] | 38 | static int ptrace_dump_regs(int pid) |
| 39 | { |
| 40 | unsigned long regs[MAX_REG_NR]; |
| 41 | int i; |
| 42 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 43 | if (ptrace(PTRACE_GETREGS, pid, 0, regs) < 0) |
Jeff Dike | f30c2c9 | 2007-05-06 14:51:29 -0700 | [diff] [blame] | 44 | return -errno; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 45 | |
| 46 | printk(UM_KERN_ERR "Stub registers -\n"); |
| 47 | for (i = 0; i < ARRAY_SIZE(regs); i++) |
| 48 | printk(UM_KERN_ERR "\t%d - %lx\n", i, regs[i]); |
Jeff Dike | f30c2c9 | 2007-05-06 14:51:29 -0700 | [diff] [blame] | 49 | |
| 50 | return 0; |
| 51 | } |
| 52 | |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 53 | /* |
| 54 | * Signals that are OK to receive in the stub - we'll just continue it. |
| 55 | * SIGWINCH will happen when UML is inside a detached screen. |
| 56 | */ |
| 57 | #define STUB_SIG_MASK ((1 << SIGVTALRM) | (1 << SIGWINCH)) |
| 58 | |
| 59 | /* Signals that the stub will finish with - anything else is an error */ |
| 60 | #define STUB_DONE_MASK ((1 << SIGUSR1) | (1 << SIGTRAP)) |
| 61 | |
| 62 | void wait_stub_done(int pid) |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 63 | { |
| 64 | int n, status, err; |
| 65 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 66 | while (1) { |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 67 | CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED)); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 68 | if ((n < 0) || !WIFSTOPPED(status)) |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 69 | goto bad_wait; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 70 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 71 | if (((1 << WSTOPSIG(status)) & STUB_SIG_MASK) == 0) |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 72 | break; |
| 73 | |
| 74 | err = ptrace(PTRACE_CONT, pid, 0, 0); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 75 | if (err) |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 76 | panic("wait_stub_done : continue failed, errno = %d\n", |
| 77 | errno); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 78 | } |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 79 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 80 | if (((1 << WSTOPSIG(status)) & STUB_DONE_MASK) != 0) |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 81 | return; |
| 82 | |
| 83 | bad_wait: |
| 84 | err = ptrace_dump_regs(pid); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 85 | if (err) |
| 86 | printk(UM_KERN_ERR "Failed to get registers from stub, " |
| 87 | "errno = %d\n", -err); |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 88 | panic("wait_stub_done : failed to wait for SIGUSR1/SIGTRAP, pid = %d, " |
| 89 | "n = %d, errno = %d, status = 0x%x\n", pid, n, errno, status); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | extern unsigned long current_stub_stack(void); |
| 93 | |
| 94 | void get_skas_faultinfo(int pid, struct faultinfo * fi) |
| 95 | { |
| 96 | int err; |
| 97 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 98 | if (ptrace_faultinfo) { |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 99 | err = ptrace(PTRACE_FAULTINFO, pid, 0, fi); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 100 | if (err) |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 101 | panic("get_skas_faultinfo - PTRACE_FAULTINFO failed, " |
| 102 | "errno = %d\n", errno); |
| 103 | |
| 104 | /* Special handling for i386, which has different structs */ |
| 105 | if (sizeof(struct ptrace_faultinfo) < sizeof(struct faultinfo)) |
| 106 | memset((char *)fi + sizeof(struct ptrace_faultinfo), 0, |
| 107 | sizeof(struct faultinfo) - |
| 108 | sizeof(struct ptrace_faultinfo)); |
| 109 | } |
| 110 | else { |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 111 | err = ptrace(PTRACE_CONT, pid, 0, SIGSEGV); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 112 | if (err) |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 113 | panic("Failed to continue stub, pid = %d, errno = %d\n", |
| 114 | pid, errno); |
| 115 | wait_stub_done(pid); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 116 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 117 | /* |
| 118 | * faultinfo is prepared by the stub-segv-handler at start of |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 119 | * the stub stack page. We just have to copy it. |
| 120 | */ |
| 121 | memcpy(fi, (void *)current_stub_stack(), sizeof(*fi)); |
| 122 | } |
| 123 | } |
| 124 | |
Jeff Dike | 77bf440 | 2007-10-16 01:26:58 -0700 | [diff] [blame] | 125 | static void handle_segv(int pid, struct uml_pt_regs * regs) |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 126 | { |
Jeff Dike | 77bf440 | 2007-10-16 01:26:58 -0700 | [diff] [blame] | 127 | get_skas_faultinfo(pid, ®s->faultinfo); |
| 128 | segv(regs->faultinfo, 0, 1, NULL); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 131 | /* |
| 132 | * To use the same value of using_sysemu as the caller, ask it that value |
| 133 | * (in local_using_sysemu |
| 134 | */ |
| 135 | static void handle_trap(int pid, struct uml_pt_regs *regs, |
| 136 | int local_using_sysemu) |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 137 | { |
| 138 | int err, status; |
| 139 | |
| 140 | /* Mark this as a syscall */ |
Jeff Dike | 18baddd | 2007-10-16 01:27:07 -0700 | [diff] [blame] | 141 | UPT_SYSCALL_NR(regs) = PT_SYSCALL_NR(regs->gp); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 142 | |
| 143 | if (!local_using_sysemu) |
| 144 | { |
| 145 | err = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET, |
| 146 | __NR_getpid); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 147 | if (err < 0) |
| 148 | panic("handle_trap - nullifying syscall failed, " |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 149 | "errno = %d\n", errno); |
| 150 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 151 | err = ptrace(PTRACE_SYSCALL, pid, 0, 0); |
| 152 | if (err < 0) |
| 153 | panic("handle_trap - continuing to end of syscall " |
| 154 | "failed, errno = %d\n", errno); |
| 155 | |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 156 | CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED)); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 157 | if ((err < 0) || !WIFSTOPPED(status) || |
| 158 | (WSTOPSIG(status) != SIGTRAP + 0x80)) { |
Jeff Dike | f30c2c9 | 2007-05-06 14:51:29 -0700 | [diff] [blame] | 159 | err = ptrace_dump_regs(pid); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 160 | if (err) |
| 161 | printk(UM_KERN_ERR "Failed to get registers " |
| 162 | "from process, errno = %d\n", -err); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 163 | panic("handle_trap - failed to wait at end of syscall, " |
| 164 | "errno = %d, status = %d\n", errno, status); |
Jeff Dike | f30c2c9 | 2007-05-06 14:51:29 -0700 | [diff] [blame] | 165 | } |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | handle_syscall(regs); |
| 169 | } |
| 170 | |
| 171 | extern int __syscall_stub_start; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 172 | |
| 173 | static int userspace_tramp(void *stack) |
| 174 | { |
| 175 | void *addr; |
Jeff Dike | 537ae94 | 2006-09-25 23:33:05 -0700 | [diff] [blame] | 176 | int err; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 177 | |
| 178 | ptrace(PTRACE_TRACEME, 0, 0, 0); |
| 179 | |
Jeff Dike | e64bd13 | 2006-07-10 04:45:07 -0700 | [diff] [blame] | 180 | init_new_thread_signals(); |
Jeff Dike | a2f018b | 2007-10-16 01:27:22 -0700 | [diff] [blame] | 181 | err = set_interval(); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 182 | if (err) |
Jeff Dike | 537ae94 | 2006-09-25 23:33:05 -0700 | [diff] [blame] | 183 | panic("userspace_tramp - setting timer failed, errno = %d\n", |
| 184 | err); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 185 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 186 | if (!proc_mm) { |
| 187 | /* |
| 188 | * This has a pte, but it can't be mapped in with the usual |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 189 | * tlb_flush mechanism because this is part of that mechanism |
| 190 | */ |
Jeff Dike | 09ee011 | 2006-01-18 17:42:48 -0800 | [diff] [blame] | 191 | int fd; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 192 | unsigned long long offset; |
Jeff Dike | 09ee011 | 2006-01-18 17:42:48 -0800 | [diff] [blame] | 193 | fd = phys_mapping(to_phys(&__syscall_stub_start), &offset); |
Jeff Dike | 1ffb916 | 2007-05-06 14:51:22 -0700 | [diff] [blame] | 194 | addr = mmap64((void *) UML_CONFIG_STUB_CODE, UM_KERN_PAGE_SIZE, |
Jeff Dike | 09ee011 | 2006-01-18 17:42:48 -0800 | [diff] [blame] | 195 | PROT_EXEC, MAP_FIXED | MAP_PRIVATE, fd, offset); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 196 | if (addr == MAP_FAILED) { |
| 197 | printk(UM_KERN_ERR "mapping mmap stub failed, " |
| 198 | "errno = %d\n", errno); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 199 | exit(1); |
| 200 | } |
| 201 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 202 | if (stack != NULL) { |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 203 | fd = phys_mapping(to_phys(stack), &offset); |
Jeff Dike | 1ffb916 | 2007-05-06 14:51:22 -0700 | [diff] [blame] | 204 | addr = mmap((void *) UML_CONFIG_STUB_DATA, |
| 205 | UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE, |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 206 | MAP_FIXED | MAP_SHARED, fd, offset); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 207 | if (addr == MAP_FAILED) { |
| 208 | printk(UM_KERN_ERR "mapping segfault stack " |
| 209 | "failed, errno = %d\n", errno); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 210 | exit(1); |
| 211 | } |
| 212 | } |
| 213 | } |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 214 | if (!ptrace_faultinfo && (stack != NULL)) { |
Jeff Dike | 4b84c69 | 2006-09-25 23:33:04 -0700 | [diff] [blame] | 215 | struct sigaction sa; |
| 216 | |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 217 | unsigned long v = UML_CONFIG_STUB_CODE + |
| 218 | (unsigned long) stub_segv_handler - |
| 219 | (unsigned long) &__syscall_stub_start; |
| 220 | |
Jeff Dike | 1ffb916 | 2007-05-06 14:51:22 -0700 | [diff] [blame] | 221 | set_sigstack((void *) UML_CONFIG_STUB_DATA, UM_KERN_PAGE_SIZE); |
Jeff Dike | 4b84c69 | 2006-09-25 23:33:04 -0700 | [diff] [blame] | 222 | sigemptyset(&sa.sa_mask); |
| 223 | sigaddset(&sa.sa_mask, SIGIO); |
| 224 | sigaddset(&sa.sa_mask, SIGWINCH); |
Jeff Dike | 4b84c69 | 2006-09-25 23:33:04 -0700 | [diff] [blame] | 225 | sigaddset(&sa.sa_mask, SIGVTALRM); |
| 226 | sigaddset(&sa.sa_mask, SIGUSR1); |
| 227 | sa.sa_flags = SA_ONSTACK; |
| 228 | sa.sa_handler = (void *) v; |
| 229 | sa.sa_restorer = NULL; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 230 | if (sigaction(SIGSEGV, &sa, NULL) < 0) |
Jeff Dike | 4b84c69 | 2006-09-25 23:33:04 -0700 | [diff] [blame] | 231 | panic("userspace_tramp - setting SIGSEGV handler " |
| 232 | "failed - errno = %d\n", errno); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 235 | kill(os_getpid(), SIGSTOP); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 236 | return 0; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /* Each element set once, and only accessed by a single processor anyway */ |
| 240 | #undef NR_CPUS |
| 241 | #define NR_CPUS 1 |
| 242 | int userspace_pid[NR_CPUS]; |
| 243 | |
| 244 | int start_userspace(unsigned long stub_stack) |
| 245 | { |
| 246 | void *stack; |
| 247 | unsigned long sp; |
| 248 | int pid, status, n, flags; |
| 249 | |
Jeff Dike | c539ab73 | 2007-06-16 10:16:09 -0700 | [diff] [blame] | 250 | stack = mmap(NULL, UM_KERN_PAGE_SIZE, |
| 251 | PROT_READ | PROT_WRITE | PROT_EXEC, |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 252 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 253 | if (stack == MAP_FAILED) |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 254 | panic("start_userspace : mmap failed, errno = %d", errno); |
Jeff Dike | c539ab73 | 2007-06-16 10:16:09 -0700 | [diff] [blame] | 255 | sp = (unsigned long) stack + UM_KERN_PAGE_SIZE - sizeof(void *); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 256 | |
| 257 | flags = CLONE_FILES | SIGCHLD; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 258 | if (proc_mm) |
| 259 | flags |= CLONE_VM; |
| 260 | |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 261 | pid = clone(userspace_tramp, (void *) sp, flags, (void *) stub_stack); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 262 | if (pid < 0) |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 263 | panic("start_userspace : clone failed, errno = %d", errno); |
| 264 | |
| 265 | do { |
| 266 | CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED)); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 267 | if (n < 0) |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 268 | panic("start_userspace : wait failed, errno = %d", |
| 269 | errno); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 270 | } while (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGVTALRM)); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 271 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 272 | if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP)) |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 273 | panic("start_userspace : expected SIGSTOP, got status = %d", |
| 274 | status); |
| 275 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 276 | if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL, |
| 277 | (void *) PTRACE_O_TRACESYSGOOD) < 0) |
| 278 | panic("start_userspace : PTRACE_OLDSETOPTIONS failed, " |
| 279 | "errno = %d\n", errno); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 280 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 281 | if (munmap(stack, UM_KERN_PAGE_SIZE) < 0) |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 282 | panic("start_userspace : munmap failed, errno = %d\n", errno); |
| 283 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 284 | return pid; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 285 | } |
| 286 | |
Jeff Dike | 77bf440 | 2007-10-16 01:26:58 -0700 | [diff] [blame] | 287 | void userspace(struct uml_pt_regs *regs) |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 288 | { |
Jeff Dike | d2753a6 | 2007-10-16 01:27:25 -0700 | [diff] [blame] | 289 | struct itimerval timer; |
| 290 | unsigned long long nsecs, now; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 291 | int err, status, op, pid = userspace_pid[0]; |
Jeff Dike | 2ea5bc5 | 2007-05-10 22:22:32 -0700 | [diff] [blame] | 292 | /* To prevent races if using_sysemu changes under us.*/ |
| 293 | int local_using_sysemu; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 294 | |
Jeff Dike | d2753a6 | 2007-10-16 01:27:25 -0700 | [diff] [blame] | 295 | if (getitimer(ITIMER_VIRTUAL, &timer)) |
| 296 | printk("Failed to get itimer, errno = %d\n", errno); |
| 297 | nsecs = timer.it_value.tv_sec * BILLION + |
| 298 | timer.it_value.tv_usec * 1000; |
| 299 | nsecs += os_nsecs(); |
| 300 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 301 | while (1) { |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 302 | restore_registers(pid, regs); |
| 303 | |
| 304 | /* Now we set local_using_sysemu to be used for one loop */ |
| 305 | local_using_sysemu = get_using_sysemu(); |
| 306 | |
Jeff Dike | 2ea5bc5 | 2007-05-10 22:22:32 -0700 | [diff] [blame] | 307 | op = SELECT_PTRACE_OPERATION(local_using_sysemu, |
| 308 | singlestepping(NULL)); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 309 | |
| 310 | err = ptrace(op, pid, 0, 0); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 311 | if (err) |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 312 | panic("userspace - could not resume userspace process, " |
| 313 | "pid=%d, ptrace operation = %d, errno = %d\n", |
Paolo 'Blaisorblade' Giarrusso | 802e307 | 2006-04-10 22:53:32 -0700 | [diff] [blame] | 314 | pid, op, errno); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 315 | |
| 316 | CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED)); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 317 | if (err < 0) |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 318 | panic("userspace - waitpid failed, errno = %d\n", |
| 319 | errno); |
| 320 | |
Jeff Dike | 77bf440 | 2007-10-16 01:26:58 -0700 | [diff] [blame] | 321 | regs->is_user = 1; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 322 | save_registers(pid, regs); |
| 323 | UPT_SYSCALL_NR(regs) = -1; /* Assume: It's not a syscall */ |
| 324 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 325 | if (WIFSTOPPED(status)) { |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 326 | int sig = WSTOPSIG(status); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 327 | switch(sig) { |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 328 | case SIGSEGV: |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 329 | if (PTRACE_FULL_FAULTINFO || |
| 330 | !ptrace_faultinfo) { |
| 331 | get_skas_faultinfo(pid, |
| 332 | ®s->faultinfo); |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 333 | (*sig_info[SIGSEGV])(SIGSEGV, regs); |
| 334 | } |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 335 | else handle_segv(pid, regs); |
| 336 | break; |
| 337 | case SIGTRAP + 0x80: |
| 338 | handle_trap(pid, regs, local_using_sysemu); |
| 339 | break; |
| 340 | case SIGTRAP: |
| 341 | relay_signal(SIGTRAP, regs); |
| 342 | break; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 343 | case SIGVTALRM: |
Jeff Dike | d2753a6 | 2007-10-16 01:27:25 -0700 | [diff] [blame] | 344 | now = os_nsecs(); |
| 345 | if(now < nsecs) |
| 346 | break; |
| 347 | block_signals(); |
| 348 | (*sig_info[sig])(sig, regs); |
| 349 | unblock_signals(); |
| 350 | nsecs = timer.it_value.tv_sec * BILLION + |
| 351 | timer.it_value.tv_usec * 1000; |
| 352 | nsecs += os_nsecs(); |
| 353 | break; |
| 354 | case SIGIO: |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 355 | case SIGILL: |
| 356 | case SIGBUS: |
| 357 | case SIGFPE: |
| 358 | case SIGWINCH: |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 359 | block_signals(); |
| 360 | (*sig_info[sig])(sig, regs); |
| 361 | unblock_signals(); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 362 | break; |
| 363 | default: |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 364 | printk(UM_KERN_ERR "userspace - child stopped " |
| 365 | "with signal %d\n", sig); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 366 | } |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 367 | pid = userspace_pid[0]; |
| 368 | interrupt_end(); |
| 369 | |
| 370 | /* Avoid -ERESTARTSYS handling in host */ |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 371 | if (PT_SYSCALL_NR_OFFSET != PT_SYSCALL_RET_OFFSET) |
Jeff Dike | 18baddd | 2007-10-16 01:27:07 -0700 | [diff] [blame] | 372 | PT_SYSCALL_NR(regs->gp) = -1; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | } |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 376 | |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 377 | static unsigned long thread_regs[MAX_REG_NR]; |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 378 | |
| 379 | static int __init init_thread_regs(void) |
| 380 | { |
Jeff Dike | 42daba3 | 2007-10-16 01:26:47 -0700 | [diff] [blame] | 381 | get_safe_registers(thread_regs); |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 382 | /* Set parent's instruction pointer to start of clone-stub */ |
| 383 | thread_regs[REGS_IP_INDEX] = UML_CONFIG_STUB_CODE + |
| 384 | (unsigned long) stub_clone_handler - |
| 385 | (unsigned long) &__syscall_stub_start; |
Jeff Dike | c539ab73 | 2007-06-16 10:16:09 -0700 | [diff] [blame] | 386 | thread_regs[REGS_SP_INDEX] = UML_CONFIG_STUB_DATA + UM_KERN_PAGE_SIZE - |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 387 | sizeof(void *); |
| 388 | #ifdef __SIGNAL_FRAMESIZE |
| 389 | thread_regs[REGS_SP_INDEX] -= __SIGNAL_FRAMESIZE; |
| 390 | #endif |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | __initcall(init_thread_regs); |
| 395 | |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 396 | int copy_context_skas0(unsigned long new_stack, int pid) |
| 397 | { |
Jeff Dike | d2753a6 | 2007-10-16 01:27:25 -0700 | [diff] [blame] | 398 | struct timeval tv = { .tv_sec = 0, .tv_usec = 1000000 / UM_HZ }; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 399 | int err; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 400 | unsigned long current_stack = current_stub_stack(); |
| 401 | struct stub_data *data = (struct stub_data *) current_stack; |
| 402 | struct stub_data *child_data = (struct stub_data *) new_stack; |
Jeff Dike | 0a7675a | 2007-10-16 01:27:05 -0700 | [diff] [blame] | 403 | unsigned long long new_offset; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 404 | int new_fd = phys_mapping(to_phys((void *)new_stack), &new_offset); |
| 405 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 406 | /* |
| 407 | * prepare offset and fd of child's stack as argument for parent's |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 408 | * and child's mmap2 calls |
| 409 | */ |
| 410 | *data = ((struct stub_data) { .offset = MMAP_OFFSET(new_offset), |
| 411 | .fd = new_fd, |
| 412 | .timer = ((struct itimerval) |
Jeff Dike | d2753a6 | 2007-10-16 01:27:25 -0700 | [diff] [blame] | 413 | { .it_value = tv, |
| 414 | .it_interval = tv }) }); |
| 415 | |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 416 | err = ptrace_setregs(pid, thread_regs); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 417 | if (err < 0) |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 418 | panic("copy_context_skas0 : PTRACE_SETREGS failed, " |
Jeff Dike | 2ace87b | 2006-05-01 12:16:00 -0700 | [diff] [blame] | 419 | "pid = %d, errno = %d\n", pid, -err); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 420 | |
| 421 | /* set a well known return code for detection of child write failure */ |
| 422 | child_data->err = 12345678; |
| 423 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 424 | /* |
| 425 | * Wait, until parent has finished its work: read child's pid from |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 426 | * parent's stack, and check, if bad result. |
| 427 | */ |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 428 | err = ptrace(PTRACE_CONT, pid, 0, 0); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 429 | if (err) |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 430 | panic("Failed to continue new process, pid = %d, " |
| 431 | "errno = %d\n", pid, errno); |
| 432 | wait_stub_done(pid); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 433 | |
| 434 | pid = data->err; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 435 | if (pid < 0) |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 436 | panic("copy_context_skas0 - stub-parent reports error %d\n", |
Jeff Dike | 2ace87b | 2006-05-01 12:16:00 -0700 | [diff] [blame] | 437 | -pid); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 438 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 439 | /* |
| 440 | * Wait, until child has finished too: read child's result from |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 441 | * child's stack and check it. |
| 442 | */ |
Jeff Dike | 16dd07b | 2007-05-06 14:51:48 -0700 | [diff] [blame] | 443 | wait_stub_done(pid); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 444 | if (child_data->err != UML_CONFIG_STUB_DATA) |
Paolo 'Blaisorblade' Giarrusso | 802e307 | 2006-04-10 22:53:32 -0700 | [diff] [blame] | 445 | panic("copy_context_skas0 - stub-child reports error %ld\n", |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 446 | child_data->err); |
| 447 | |
| 448 | if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL, |
| 449 | (void *)PTRACE_O_TRACESYSGOOD) < 0) |
| 450 | panic("copy_context_skas0 : PTRACE_OLDSETOPTIONS failed, " |
| 451 | "errno = %d\n", errno); |
| 452 | |
| 453 | return pid; |
| 454 | } |
| 455 | |
| 456 | /* |
| 457 | * This is used only, if stub pages are needed, while proc_mm is |
Jeff Dike | ef0470c | 2007-05-06 14:51:33 -0700 | [diff] [blame] | 458 | * available. Opening /proc/mm creates a new mm_context, which lacks |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 459 | * the stub-pages. Thus, we map them using /proc/mm-fd |
| 460 | */ |
| 461 | void map_stub_pages(int fd, unsigned long code, |
| 462 | unsigned long data, unsigned long stack) |
| 463 | { |
| 464 | struct proc_mm_op mmop; |
| 465 | int n; |
Jeff Dike | 0a7675a | 2007-10-16 01:27:05 -0700 | [diff] [blame] | 466 | unsigned long long code_offset; |
Jeff Dike | 09ee011 | 2006-01-18 17:42:48 -0800 | [diff] [blame] | 467 | int code_fd = phys_mapping(to_phys((void *) &__syscall_stub_start), |
| 468 | &code_offset); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 469 | |
| 470 | mmop = ((struct proc_mm_op) { .op = MM_MMAP, |
| 471 | .u = |
| 472 | { .mmap = |
| 473 | { .addr = code, |
Jeff Dike | c539ab73 | 2007-06-16 10:16:09 -0700 | [diff] [blame] | 474 | .len = UM_KERN_PAGE_SIZE, |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 475 | .prot = PROT_EXEC, |
| 476 | .flags = MAP_FIXED | MAP_PRIVATE, |
Jeff Dike | 09ee011 | 2006-01-18 17:42:48 -0800 | [diff] [blame] | 477 | .fd = code_fd, |
| 478 | .offset = code_offset |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 479 | } } }); |
Jeff Dike | a61f334 | 2007-05-06 14:51:35 -0700 | [diff] [blame] | 480 | CATCH_EINTR(n = write(fd, &mmop, sizeof(mmop))); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 481 | if (n != sizeof(mmop)) { |
Jeff Dike | a61f334 | 2007-05-06 14:51:35 -0700 | [diff] [blame] | 482 | n = errno; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 483 | printk(UM_KERN_ERR "mmap args - addr = 0x%lx, fd = %d, " |
| 484 | "offset = %llx\n", code, code_fd, |
| 485 | (unsigned long long) code_offset); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 486 | panic("map_stub_pages : /proc/mm map for code failed, " |
Jeff Dike | a61f334 | 2007-05-06 14:51:35 -0700 | [diff] [blame] | 487 | "err = %d\n", n); |
Jeff Dike | b4cf95c | 2007-03-06 01:42:19 -0800 | [diff] [blame] | 488 | } |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 489 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 490 | if (stack) { |
Jeff Dike | 0a7675a | 2007-10-16 01:27:05 -0700 | [diff] [blame] | 491 | unsigned long long map_offset; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 492 | int map_fd = phys_mapping(to_phys((void *)stack), &map_offset); |
| 493 | mmop = ((struct proc_mm_op) |
| 494 | { .op = MM_MMAP, |
| 495 | .u = |
| 496 | { .mmap = |
| 497 | { .addr = data, |
Jeff Dike | c539ab73 | 2007-06-16 10:16:09 -0700 | [diff] [blame] | 498 | .len = UM_KERN_PAGE_SIZE, |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 499 | .prot = PROT_READ | PROT_WRITE, |
| 500 | .flags = MAP_FIXED | MAP_SHARED, |
| 501 | .fd = map_fd, |
| 502 | .offset = map_offset |
| 503 | } } }); |
Jeff Dike | a61f334 | 2007-05-06 14:51:35 -0700 | [diff] [blame] | 504 | CATCH_EINTR(n = write(fd, &mmop, sizeof(mmop))); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 505 | if (n != sizeof(mmop)) |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 506 | panic("map_stub_pages : /proc/mm map for data failed, " |
Jeff Dike | a61f334 | 2007-05-06 14:51:35 -0700 | [diff] [blame] | 507 | "err = %d\n", errno); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | |
Jeff Dike | 3c91735 | 2006-09-27 01:50:40 -0700 | [diff] [blame] | 511 | void new_thread(void *stack, jmp_buf *buf, void (*handler)(void)) |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 512 | { |
Jeff Dike | 3c91735 | 2006-09-27 01:50:40 -0700 | [diff] [blame] | 513 | (*buf)[0].JB_IP = (unsigned long) handler; |
Jeff Dike | e1a79c4 | 2007-05-10 22:22:31 -0700 | [diff] [blame] | 514 | (*buf)[0].JB_SP = (unsigned long) stack + UM_THREAD_SIZE - |
| 515 | sizeof(void *); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 516 | } |
| 517 | |
Jeff Dike | e2216fe | 2006-02-07 12:58:43 -0800 | [diff] [blame] | 518 | #define INIT_JMP_NEW_THREAD 0 |
Jeff Dike | 3c91735 | 2006-09-27 01:50:40 -0700 | [diff] [blame] | 519 | #define INIT_JMP_CALLBACK 1 |
| 520 | #define INIT_JMP_HALT 2 |
| 521 | #define INIT_JMP_REBOOT 3 |
Jeff Dike | e2216fe | 2006-02-07 12:58:43 -0800 | [diff] [blame] | 522 | |
Jeff Dike | 3c91735 | 2006-09-27 01:50:40 -0700 | [diff] [blame] | 523 | void switch_threads(jmp_buf *me, jmp_buf *you) |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 524 | { |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 525 | if (UML_SETJMP(me) == 0) |
Jeff Dike | 3c91735 | 2006-09-27 01:50:40 -0700 | [diff] [blame] | 526 | UML_LONGJMP(you, 1); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 527 | } |
| 528 | |
Jeff Dike | ad28e02 | 2006-04-18 22:21:41 -0700 | [diff] [blame] | 529 | static jmp_buf initial_jmpbuf; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 530 | |
| 531 | /* XXX Make these percpu */ |
| 532 | static void (*cb_proc)(void *arg); |
| 533 | static void *cb_arg; |
Jeff Dike | ad28e02 | 2006-04-18 22:21:41 -0700 | [diff] [blame] | 534 | static jmp_buf *cb_back; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 535 | |
Jeff Dike | 3c91735 | 2006-09-27 01:50:40 -0700 | [diff] [blame] | 536 | int start_idle_thread(void *stack, jmp_buf *switch_buf) |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 537 | { |
Jeff Dike | a5df0d1 | 2006-07-14 00:24:02 -0700 | [diff] [blame] | 538 | int n; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 539 | |
| 540 | set_handler(SIGWINCH, (__sighandler_t) sig_handler, |
Jeff Dike | 61b63c5 | 2007-10-16 01:27:27 -0700 | [diff] [blame^] | 541 | SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGVTALRM, -1); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 542 | |
Jeff Dike | 77f6af7 | 2007-05-06 14:51:40 -0700 | [diff] [blame] | 543 | /* |
| 544 | * Can't use UML_SETJMP or UML_LONGJMP here because they save |
| 545 | * and restore signals, with the possible side-effect of |
| 546 | * trying to handle any signals which came when they were |
| 547 | * blocked, which can't be done on this stack. |
| 548 | * Signals must be blocked when jumping back here and restored |
| 549 | * after returning to the jumper. |
| 550 | */ |
| 551 | n = setjmp(initial_jmpbuf); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 552 | switch(n) { |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 553 | case INIT_JMP_NEW_THREAD: |
Jeff Dike | 3c91735 | 2006-09-27 01:50:40 -0700 | [diff] [blame] | 554 | (*switch_buf)[0].JB_IP = (unsigned long) new_thread_handler; |
| 555 | (*switch_buf)[0].JB_SP = (unsigned long) stack + |
Jeff Dike | e1a79c4 | 2007-05-10 22:22:31 -0700 | [diff] [blame] | 556 | UM_THREAD_SIZE - sizeof(void *); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 557 | break; |
| 558 | case INIT_JMP_CALLBACK: |
| 559 | (*cb_proc)(cb_arg); |
Jeff Dike | 77f6af7 | 2007-05-06 14:51:40 -0700 | [diff] [blame] | 560 | longjmp(*cb_back, 1); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 561 | break; |
| 562 | case INIT_JMP_HALT: |
| 563 | kmalloc_ok = 0; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 564 | return 0; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 565 | case INIT_JMP_REBOOT: |
| 566 | kmalloc_ok = 0; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 567 | return 1; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 568 | default: |
| 569 | panic("Bad sigsetjmp return in start_idle_thread - %d\n", n); |
| 570 | } |
Jeff Dike | 77f6af7 | 2007-05-06 14:51:40 -0700 | [diff] [blame] | 571 | longjmp(*switch_buf, 1); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | void initial_thread_cb_skas(void (*proc)(void *), void *arg) |
| 575 | { |
Jeff Dike | ad28e02 | 2006-04-18 22:21:41 -0700 | [diff] [blame] | 576 | jmp_buf here; |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 577 | |
| 578 | cb_proc = proc; |
| 579 | cb_arg = arg; |
| 580 | cb_back = &here; |
| 581 | |
| 582 | block_signals(); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 583 | if (UML_SETJMP(&here) == 0) |
Jeff Dike | ad28e02 | 2006-04-18 22:21:41 -0700 | [diff] [blame] | 584 | UML_LONGJMP(&initial_jmpbuf, INIT_JMP_CALLBACK); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 585 | unblock_signals(); |
| 586 | |
| 587 | cb_proc = NULL; |
| 588 | cb_arg = NULL; |
| 589 | cb_back = NULL; |
| 590 | } |
| 591 | |
| 592 | void halt_skas(void) |
| 593 | { |
| 594 | block_signals(); |
Jeff Dike | ad28e02 | 2006-04-18 22:21:41 -0700 | [diff] [blame] | 595 | UML_LONGJMP(&initial_jmpbuf, INIT_JMP_HALT); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | void reboot_skas(void) |
| 599 | { |
| 600 | block_signals(); |
Jeff Dike | ad28e02 | 2006-04-18 22:21:41 -0700 | [diff] [blame] | 601 | UML_LONGJMP(&initial_jmpbuf, INIT_JMP_REBOOT); |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 602 | } |
| 603 | |
Jeff Dike | 77bf440 | 2007-10-16 01:26:58 -0700 | [diff] [blame] | 604 | void __switch_mm(struct mm_id *mm_idp) |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 605 | { |
| 606 | int err; |
| 607 | |
Jeff Dike | 77bf440 | 2007-10-16 01:26:58 -0700 | [diff] [blame] | 608 | /* FIXME: need cpu pid in __switch_mm */ |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 609 | if (proc_mm) { |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 610 | err = ptrace(PTRACE_SWITCH_MM, userspace_pid[0], 0, |
| 611 | mm_idp->u.mm_fd); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 612 | if (err) |
Jeff Dike | 77bf440 | 2007-10-16 01:26:58 -0700 | [diff] [blame] | 613 | panic("__switch_mm - PTRACE_SWITCH_MM failed, " |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 614 | "errno = %d\n", errno); |
| 615 | } |
| 616 | else userspace_pid[0] = mm_idp->u.pid; |
| 617 | } |