diff options
Diffstat (limited to 'arch/um/kernel/skas/process.c')
-rw-r--r-- | arch/um/kernel/skas/process.c | 216 |
1 files changed, 183 insertions, 33 deletions
diff --git a/arch/um/kernel/skas/process.c b/arch/um/kernel/skas/process.c index 773cd2b525fc..ba671dab8878 100644 --- a/arch/um/kernel/skas/process.c +++ b/arch/um/kernel/skas/process.c | |||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | * Copyright (C) 2002 Jeff Dike (jdike@karaya.com) | 2 | * Copyright (C) 2002- 2004 Jeff Dike (jdike@addtoit.com) |
3 | * Licensed under the GPL | 3 | * Licensed under the GPL |
4 | */ | 4 | */ |
5 | 5 | ||
@@ -13,7 +13,9 @@ | |||
13 | #include <sys/wait.h> | 13 | #include <sys/wait.h> |
14 | #include <sys/mman.h> | 14 | #include <sys/mman.h> |
15 | #include <sys/user.h> | 15 | #include <sys/user.h> |
16 | #include <sys/time.h> | ||
16 | #include <asm/unistd.h> | 17 | #include <asm/unistd.h> |
18 | #include <asm/types.h> | ||
17 | #include "user.h" | 19 | #include "user.h" |
18 | #include "ptrace_user.h" | 20 | #include "ptrace_user.h" |
19 | #include "time_user.h" | 21 | #include "time_user.h" |
@@ -21,13 +23,18 @@ | |||
21 | #include "user_util.h" | 23 | #include "user_util.h" |
22 | #include "kern_util.h" | 24 | #include "kern_util.h" |
23 | #include "skas.h" | 25 | #include "skas.h" |
26 | #include "stub-data.h" | ||
27 | #include "mm_id.h" | ||
24 | #include "sysdep/sigcontext.h" | 28 | #include "sysdep/sigcontext.h" |
29 | #include "sysdep/stub.h" | ||
25 | #include "os.h" | 30 | #include "os.h" |
26 | #include "proc_mm.h" | 31 | #include "proc_mm.h" |
27 | #include "skas_ptrace.h" | 32 | #include "skas_ptrace.h" |
28 | #include "chan_user.h" | 33 | #include "chan_user.h" |
29 | #include "signal_user.h" | 34 | #include "signal_user.h" |
30 | #include "registers.h" | 35 | #include "registers.h" |
36 | #include "mem.h" | ||
37 | #include "uml-config.h" | ||
31 | #include "process.h" | 38 | #include "process.h" |
32 | 39 | ||
33 | int is_skas_winch(int pid, int fd, void *data) | 40 | int is_skas_winch(int pid, int fd, void *data) |
@@ -39,20 +46,55 @@ int is_skas_winch(int pid, int fd, void *data) | |||
39 | return(1); | 46 | return(1); |
40 | } | 47 | } |
41 | 48 | ||
42 | void get_skas_faultinfo(int pid, struct faultinfo * fi) | 49 | void wait_stub_done(int pid, int sig, char * fname) |
43 | { | 50 | { |
44 | int err; | 51 | int n, status, err; |
45 | 52 | ||
46 | err = ptrace(PTRACE_FAULTINFO, pid, 0, fi); | 53 | do { |
47 | if(err) | 54 | if ( sig != -1 ) { |
48 | panic("get_skas_faultinfo - PTRACE_FAULTINFO failed, " | 55 | err = ptrace(PTRACE_CONT, pid, 0, sig); |
49 | "errno = %d\n", errno); | 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 | |||
66 | if((n < 0) || !WIFSTOPPED(status) || | ||
67 | (WSTOPSIG(status) != SIGUSR1 && WSTOPSIG(status != SIGTRAP))){ | ||
68 | panic("%s : failed to wait for SIGUSR1/SIGTRAP, " | ||
69 | "pid = %d, n = %d, errno = %d, status = 0x%x\n", | ||
70 | fname, pid, n, errno, status); | ||
71 | } | ||
72 | } | ||
50 | 73 | ||
51 | /* Special handling for i386, which has different structs */ | 74 | void get_skas_faultinfo(int pid, struct faultinfo * fi) |
52 | if (sizeof(struct ptrace_faultinfo) < sizeof(struct faultinfo)) | 75 | { |
53 | memset((char *)fi + sizeof(struct ptrace_faultinfo), 0, | 76 | int err; |
54 | sizeof(struct faultinfo) - | 77 | |
55 | sizeof(struct ptrace_faultinfo)); | 78 | if(ptrace_faultinfo){ |
79 | err = ptrace(PTRACE_FAULTINFO, pid, 0, fi); | ||
80 | if(err) | ||
81 | panic("get_skas_faultinfo - PTRACE_FAULTINFO failed, " | ||
82 | "errno = %d\n", errno); | ||
83 | |||
84 | /* Special handling for i386, which has different structs */ | ||
85 | if (sizeof(struct ptrace_faultinfo) < sizeof(struct faultinfo)) | ||
86 | memset((char *)fi + sizeof(struct ptrace_faultinfo), 0, | ||
87 | sizeof(struct faultinfo) - | ||
88 | sizeof(struct ptrace_faultinfo)); | ||
89 | } | ||
90 | else { | ||
91 | wait_stub_done(pid, SIGSEGV, "get_skas_faultinfo"); | ||
92 | |||
93 | /* faultinfo is prepared by the stub-segv-handler at start of | ||
94 | * the stub stack page. We just have to copy it. | ||
95 | */ | ||
96 | memcpy(fi, (void *)current_stub_stack(), sizeof(*fi)); | ||
97 | } | ||
56 | } | 98 | } |
57 | 99 | ||
58 | static void handle_segv(int pid, union uml_pt_regs * regs) | 100 | static void handle_segv(int pid, union uml_pt_regs * regs) |
@@ -91,11 +133,56 @@ static void handle_trap(int pid, union uml_pt_regs *regs, int local_using_sysemu | |||
91 | handle_syscall(regs); | 133 | handle_syscall(regs); |
92 | } | 134 | } |
93 | 135 | ||
94 | static int userspace_tramp(void *arg) | 136 | extern int __syscall_stub_start; |
137 | |||
138 | static int userspace_tramp(void *stack) | ||
95 | { | 139 | { |
96 | init_new_thread_signals(0); | 140 | void *addr; |
97 | enable_timer(); | 141 | |
98 | ptrace(PTRACE_TRACEME, 0, 0, 0); | 142 | ptrace(PTRACE_TRACEME, 0, 0, 0); |
143 | |||
144 | init_new_thread_signals(1); | ||
145 | enable_timer(); | ||
146 | |||
147 | if(!proc_mm){ | ||
148 | /* This has a pte, but it can't be mapped in with the usual | ||
149 | * tlb_flush mechanism because this is part of that mechanism | ||
150 | */ | ||
151 | int fd; | ||
152 | __u64 offset; | ||
153 | |||
154 | fd = phys_mapping(to_phys(&__syscall_stub_start), &offset); | ||
155 | addr = mmap64((void *) UML_CONFIG_STUB_CODE, page_size(), | ||
156 | PROT_EXEC, MAP_FIXED | MAP_PRIVATE, fd, offset); | ||
157 | if(addr == MAP_FAILED){ | ||
158 | printk("mapping mmap stub failed, errno = %d\n", | ||
159 | errno); | ||
160 | exit(1); | ||
161 | } | ||
162 | |||
163 | if(stack != NULL){ | ||
164 | fd = phys_mapping(to_phys(stack), &offset); | ||
165 | addr = mmap((void *) UML_CONFIG_STUB_DATA, page_size(), | ||
166 | PROT_READ | PROT_WRITE, | ||
167 | MAP_FIXED | MAP_SHARED, fd, offset); | ||
168 | if(addr == MAP_FAILED){ | ||
169 | printk("mapping segfault stack failed, " | ||
170 | "errno = %d\n", errno); | ||
171 | exit(1); | ||
172 | } | ||
173 | } | ||
174 | } | ||
175 | if(!ptrace_faultinfo && (stack != NULL)){ | ||
176 | unsigned long v = UML_CONFIG_STUB_CODE + | ||
177 | (unsigned long) stub_segv_handler - | ||
178 | (unsigned long) &__syscall_stub_start; | ||
179 | |||
180 | set_sigstack((void *) UML_CONFIG_STUB_DATA, page_size()); | ||
181 | set_handler(SIGSEGV, (void *) v, SA_ONSTACK, | ||
182 | SIGIO, SIGWINCH, SIGALRM, SIGVTALRM, | ||
183 | SIGUSR1, -1); | ||
184 | } | ||
185 | |||
99 | os_stop_process(os_getpid()); | 186 | os_stop_process(os_getpid()); |
100 | return(0); | 187 | return(0); |
101 | } | 188 | } |
@@ -105,11 +192,11 @@ static int userspace_tramp(void *arg) | |||
105 | #define NR_CPUS 1 | 192 | #define NR_CPUS 1 |
106 | int userspace_pid[NR_CPUS]; | 193 | int userspace_pid[NR_CPUS]; |
107 | 194 | ||
108 | void start_userspace(int cpu) | 195 | int start_userspace(unsigned long stub_stack) |
109 | { | 196 | { |
110 | void *stack; | 197 | void *stack; |
111 | unsigned long sp; | 198 | unsigned long sp; |
112 | int pid, status, n; | 199 | int pid, status, n, flags; |
113 | 200 | ||
114 | stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC, | 201 | stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC, |
115 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | 202 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
@@ -117,8 +204,9 @@ void start_userspace(int cpu) | |||
117 | panic("start_userspace : mmap failed, errno = %d", errno); | 204 | panic("start_userspace : mmap failed, errno = %d", errno); |
118 | sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *); | 205 | sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *); |
119 | 206 | ||
120 | pid = clone(userspace_tramp, (void *) sp, | 207 | flags = CLONE_FILES | SIGCHLD; |
121 | CLONE_FILES | CLONE_VM | SIGCHLD, NULL); | 208 | if(proc_mm) flags |= CLONE_VM; |
209 | pid = clone(userspace_tramp, (void *) sp, flags, (void *) stub_stack); | ||
122 | if(pid < 0) | 210 | if(pid < 0) |
123 | panic("start_userspace : clone failed, errno = %d", errno); | 211 | panic("start_userspace : clone failed, errno = %d", errno); |
124 | 212 | ||
@@ -140,7 +228,7 @@ void start_userspace(int cpu) | |||
140 | if(munmap(stack, PAGE_SIZE) < 0) | 228 | if(munmap(stack, PAGE_SIZE) < 0) |
141 | panic("start_userspace : munmap failed, errno = %d\n", errno); | 229 | panic("start_userspace : munmap failed, errno = %d\n", errno); |
142 | 230 | ||
143 | userspace_pid[cpu] = pid; | 231 | return(pid); |
144 | } | 232 | } |
145 | 233 | ||
146 | void userspace(union uml_pt_regs *regs) | 234 | void userspace(union uml_pt_regs *regs) |
@@ -174,7 +262,9 @@ void userspace(union uml_pt_regs *regs) | |||
174 | if(WIFSTOPPED(status)){ | 262 | if(WIFSTOPPED(status)){ |
175 | switch(WSTOPSIG(status)){ | 263 | switch(WSTOPSIG(status)){ |
176 | case SIGSEGV: | 264 | case SIGSEGV: |
177 | handle_segv(pid, regs); | 265 | if(PTRACE_FULL_FAULTINFO || !ptrace_faultinfo) |
266 | user_signal(SIGSEGV, regs, pid); | ||
267 | else handle_segv(pid, regs); | ||
178 | break; | 268 | break; |
179 | case SIGTRAP + 0x80: | 269 | case SIGTRAP + 0x80: |
180 | handle_trap(pid, regs, local_using_sysemu); | 270 | handle_trap(pid, regs, local_using_sysemu); |
@@ -194,6 +284,7 @@ void userspace(union uml_pt_regs *regs) | |||
194 | printk("userspace - child stopped with signal " | 284 | printk("userspace - child stopped with signal " |
195 | "%d\n", WSTOPSIG(status)); | 285 | "%d\n", WSTOPSIG(status)); |
196 | } | 286 | } |
287 | pid = userspace_pid[0]; | ||
197 | interrupt_end(); | 288 | interrupt_end(); |
198 | 289 | ||
199 | /* Avoid -ERESTARTSYS handling in host */ | 290 | /* Avoid -ERESTARTSYS handling in host */ |
@@ -207,6 +298,67 @@ void userspace(union uml_pt_regs *regs) | |||
207 | #define INIT_JMP_HALT 3 | 298 | #define INIT_JMP_HALT 3 |
208 | #define INIT_JMP_REBOOT 4 | 299 | #define INIT_JMP_REBOOT 4 |
209 | 300 | ||
301 | |||
302 | int copy_context_skas0(unsigned long new_stack, int pid) | ||
303 | { | ||
304 | int err; | ||
305 | unsigned long regs[MAX_REG_NR]; | ||
306 | unsigned long current_stack = current_stub_stack(); | ||
307 | struct stub_data *data = (struct stub_data *) current_stack; | ||
308 | struct stub_data *child_data = (struct stub_data *) new_stack; | ||
309 | __u64 new_offset; | ||
310 | int new_fd = phys_mapping(to_phys((void *)new_stack), &new_offset); | ||
311 | |||
312 | /* prepare offset and fd of child's stack as argument for parent's | ||
313 | * and child's mmap2 calls | ||
314 | */ | ||
315 | *data = ((struct stub_data) { .offset = MMAP_OFFSET(new_offset), | ||
316 | .fd = new_fd, | ||
317 | .timer = ((struct itimerval) | ||
318 | { { 0, 1000000 / hz() }, | ||
319 | { 0, 1000000 / hz() }})}); | ||
320 | get_safe_registers(regs); | ||
321 | |||
322 | /* Set parent's instruction pointer to start of clone-stub */ | ||
323 | regs[REGS_IP_INDEX] = UML_CONFIG_STUB_CODE + | ||
324 | (unsigned long) stub_clone_handler - | ||
325 | (unsigned long) &__syscall_stub_start; | ||
326 | regs[REGS_SP_INDEX] = UML_CONFIG_STUB_DATA + PAGE_SIZE - | ||
327 | sizeof(void *); | ||
328 | err = ptrace_setregs(pid, regs); | ||
329 | if(err < 0) | ||
330 | panic("copy_context_skas0 : PTRACE_SETREGS failed, " | ||
331 | "pid = %d, errno = %d\n", pid, errno); | ||
332 | |||
333 | /* set a well known return code for detection of child write failure */ | ||
334 | child_data->err = 12345678; | ||
335 | |||
336 | /* Wait, until parent has finished its work: read child's pid from | ||
337 | * parent's stack, and check, if bad result. | ||
338 | */ | ||
339 | wait_stub_done(pid, 0, "copy_context_skas0"); | ||
340 | |||
341 | pid = data->err; | ||
342 | if(pid < 0) | ||
343 | panic("copy_context_skas0 - stub-parent reports error %d\n", | ||
344 | pid); | ||
345 | |||
346 | /* Wait, until child has finished too: read child's result from | ||
347 | * child's stack and check it. | ||
348 | */ | ||
349 | wait_stub_done(pid, -1, "copy_context_skas0"); | ||
350 | if (child_data->err != UML_CONFIG_STUB_DATA) | ||
351 | panic("copy_context_skas0 - stub-child reports error %d\n", | ||
352 | child_data->err); | ||
353 | |||
354 | if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL, | ||
355 | (void *)PTRACE_O_TRACESYSGOOD) < 0) | ||
356 | panic("copy_context_skas0 : PTRACE_SETOPTIONS failed, " | ||
357 | "errno = %d\n", errno); | ||
358 | |||
359 | return pid; | ||
360 | } | ||
361 | |||
210 | void new_thread(void *stack, void **switch_buf_ptr, void **fork_buf_ptr, | 362 | void new_thread(void *stack, void **switch_buf_ptr, void **fork_buf_ptr, |
211 | void (*handler)(int)) | 363 | void (*handler)(int)) |
212 | { | 364 | { |
@@ -334,21 +486,19 @@ void reboot_skas(void) | |||
334 | siglongjmp(initial_jmpbuf, INIT_JMP_REBOOT); | 486 | siglongjmp(initial_jmpbuf, INIT_JMP_REBOOT); |
335 | } | 487 | } |
336 | 488 | ||
337 | void switch_mm_skas(int mm_fd) | 489 | void switch_mm_skas(struct mm_id *mm_idp) |
338 | { | 490 | { |
339 | int err; | 491 | int err; |
340 | 492 | ||
341 | #warning need cpu pid in switch_mm_skas | 493 | #warning need cpu pid in switch_mm_skas |
342 | err = ptrace(PTRACE_SWITCH_MM, userspace_pid[0], 0, mm_fd); | 494 | if(proc_mm){ |
343 | if(err) | 495 | err = ptrace(PTRACE_SWITCH_MM, userspace_pid[0], 0, |
344 | panic("switch_mm_skas - PTRACE_SWITCH_MM failed, errno = %d\n", | 496 | mm_idp->u.mm_fd); |
345 | errno); | 497 | if(err) |
346 | } | 498 | panic("switch_mm_skas - PTRACE_SWITCH_MM failed, " |
347 | 499 | "errno = %d\n", errno); | |
348 | void kill_off_processes_skas(void) | 500 | } |
349 | { | 501 | else userspace_pid[0] = mm_idp->u.pid; |
350 | #warning need to loop over userspace_pids in kill_off_processes_skas | ||
351 | os_kill_ptraced_process(userspace_pid[0], 1); | ||
352 | } | 502 | } |
353 | 503 | ||
354 | /* | 504 | /* |