2 * Copyright (C) 2002- 2004 Jeff Dike (jdike@addtoit.com)
3 * Licensed under the GPL
12 #include "ptrace_user.h"
17 #include <sys/syscall.h>
18 #include <asm/types.h>
20 #include "sysdep/ptrace.h"
21 #include "kern_util.h"
23 #include "stub-data.h"
25 #include "sysdep/sigcontext.h"
26 #include "sysdep/stub.h"
29 #include "skas_ptrace.h"
30 #include "chan_user.h"
31 #include "registers.h"
33 #include "uml-config.h"
36 #include "kern_constants.h"
37 #include "as-layout.h"
39 int is_skas_winch(int pid
, int fd
, void *data
)
41 if(pid
!= os_getpgrp())
44 register_winch_irq(-1, fd
, -1, data
);
48 static int ptrace_dump_regs(int pid
)
50 unsigned long regs
[MAX_REG_NR
];
53 if(ptrace(PTRACE_GETREGS
, pid
, 0, regs
) < 0)
56 printk("Stub registers -\n");
57 for(i
= 0; i
< ARRAY_SIZE(regs
); i
++)
58 printk("\t%d - %lx\n", i
, regs
[i
]);
65 * Signals that are OK to receive in the stub - we'll just continue it.
66 * SIGWINCH will happen when UML is inside a detached screen.
68 #define STUB_SIG_MASK ((1 << SIGVTALRM) | (1 << SIGWINCH))
70 /* Signals that the stub will finish with - anything else is an error */
71 #define STUB_DONE_MASK ((1 << SIGUSR1) | (1 << SIGTRAP))
73 void wait_stub_done(int pid
)
78 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
79 if((n
< 0) || !WIFSTOPPED(status
))
82 if(((1 << WSTOPSIG(status
)) & STUB_SIG_MASK
) == 0)
85 err
= ptrace(PTRACE_CONT
, pid
, 0, 0);
87 panic("wait_stub_done : continue failed, errno = %d\n",
91 if(((1 << WSTOPSIG(status
)) & STUB_DONE_MASK
) != 0)
95 err
= ptrace_dump_regs(pid
);
97 printk("Failed to get registers from stub, errno = %d\n", -err
);
98 panic("wait_stub_done : failed to wait for SIGUSR1/SIGTRAP, pid = %d, "
99 "n = %d, errno = %d, status = 0x%x\n", pid
, n
, errno
, status
);
102 extern unsigned long current_stub_stack(void);
104 void get_skas_faultinfo(int pid
, struct faultinfo
* fi
)
108 if(ptrace_faultinfo
){
109 err
= ptrace(PTRACE_FAULTINFO
, pid
, 0, fi
);
111 panic("get_skas_faultinfo - PTRACE_FAULTINFO failed, "
112 "errno = %d\n", errno
);
114 /* Special handling for i386, which has different structs */
115 if (sizeof(struct ptrace_faultinfo
) < sizeof(struct faultinfo
))
116 memset((char *)fi
+ sizeof(struct ptrace_faultinfo
), 0,
117 sizeof(struct faultinfo
) -
118 sizeof(struct ptrace_faultinfo
));
121 err
= ptrace(PTRACE_CONT
, pid
, 0, SIGSEGV
);
123 panic("Failed to continue stub, pid = %d, errno = %d\n",
127 /* faultinfo is prepared by the stub-segv-handler at start of
128 * the stub stack page. We just have to copy it.
130 memcpy(fi
, (void *)current_stub_stack(), sizeof(*fi
));
134 static void handle_segv(int pid
, union uml_pt_regs
* regs
)
136 get_skas_faultinfo(pid
, ®s
->skas
.faultinfo
);
137 segv(regs
->skas
.faultinfo
, 0, 1, NULL
);
140 /*To use the same value of using_sysemu as the caller, ask it that value (in local_using_sysemu)*/
141 static void handle_trap(int pid
, union uml_pt_regs
*regs
, int local_using_sysemu
)
145 /* Mark this as a syscall */
146 UPT_SYSCALL_NR(regs
) = PT_SYSCALL_NR(regs
->skas
.regs
);
148 if (!local_using_sysemu
)
150 err
= ptrace(PTRACE_POKEUSR
, pid
, PT_SYSCALL_NR_OFFSET
,
153 panic("handle_trap - nullifying syscall failed errno = %d\n",
156 err
= ptrace(PTRACE_SYSCALL
, pid
, 0, 0);
158 panic("handle_trap - continuing to end of syscall failed, "
159 "errno = %d\n", errno
);
161 CATCH_EINTR(err
= waitpid(pid
, &status
, WUNTRACED
));
162 if((err
< 0) || !WIFSTOPPED(status
) ||
163 (WSTOPSIG(status
) != SIGTRAP
+ 0x80)){
164 err
= ptrace_dump_regs(pid
);
166 printk("Failed to get registers from process, "
167 "errno = %d\n", -err
);
168 panic("handle_trap - failed to wait at end of syscall, "
169 "errno = %d, status = %d\n", errno
, status
);
173 handle_syscall(regs
);
176 extern int __syscall_stub_start
;
178 static int userspace_tramp(void *stack
)
183 ptrace(PTRACE_TRACEME
, 0, 0, 0);
185 init_new_thread_signals();
186 err
= set_interval(1);
188 panic("userspace_tramp - setting timer failed, errno = %d\n",
192 /* This has a pte, but it can't be mapped in with the usual
193 * tlb_flush mechanism because this is part of that mechanism
197 fd
= phys_mapping(to_phys(&__syscall_stub_start
), &offset
);
198 addr
= mmap64((void *) UML_CONFIG_STUB_CODE
, UM_KERN_PAGE_SIZE
,
199 PROT_EXEC
, MAP_FIXED
| MAP_PRIVATE
, fd
, offset
);
200 if(addr
== MAP_FAILED
){
201 printk("mapping mmap stub failed, errno = %d\n",
207 fd
= phys_mapping(to_phys(stack
), &offset
);
208 addr
= mmap((void *) UML_CONFIG_STUB_DATA
,
209 UM_KERN_PAGE_SIZE
, PROT_READ
| PROT_WRITE
,
210 MAP_FIXED
| MAP_SHARED
, fd
, offset
);
211 if(addr
== MAP_FAILED
){
212 printk("mapping segfault stack failed, "
213 "errno = %d\n", errno
);
218 if(!ptrace_faultinfo
&& (stack
!= NULL
)){
221 unsigned long v
= UML_CONFIG_STUB_CODE
+
222 (unsigned long) stub_segv_handler
-
223 (unsigned long) &__syscall_stub_start
;
225 set_sigstack((void *) UML_CONFIG_STUB_DATA
, UM_KERN_PAGE_SIZE
);
226 sigemptyset(&sa
.sa_mask
);
227 sigaddset(&sa
.sa_mask
, SIGIO
);
228 sigaddset(&sa
.sa_mask
, SIGWINCH
);
229 sigaddset(&sa
.sa_mask
, SIGALRM
);
230 sigaddset(&sa
.sa_mask
, SIGVTALRM
);
231 sigaddset(&sa
.sa_mask
, SIGUSR1
);
232 sa
.sa_flags
= SA_ONSTACK
;
233 sa
.sa_handler
= (void *) v
;
234 sa
.sa_restorer
= NULL
;
235 if(sigaction(SIGSEGV
, &sa
, NULL
) < 0)
236 panic("userspace_tramp - setting SIGSEGV handler "
237 "failed - errno = %d\n", errno
);
240 os_stop_process(os_getpid());
244 /* Each element set once, and only accessed by a single processor anyway */
247 int userspace_pid
[NR_CPUS
];
249 int start_userspace(unsigned long stub_stack
)
253 int pid
, status
, n
, flags
;
255 stack
= mmap(NULL
, PAGE_SIZE
, PROT_READ
| PROT_WRITE
| PROT_EXEC
,
256 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
257 if(stack
== MAP_FAILED
)
258 panic("start_userspace : mmap failed, errno = %d", errno
);
259 sp
= (unsigned long) stack
+ PAGE_SIZE
- sizeof(void *);
261 flags
= CLONE_FILES
| SIGCHLD
;
262 if(proc_mm
) flags
|= CLONE_VM
;
263 pid
= clone(userspace_tramp
, (void *) sp
, flags
, (void *) stub_stack
);
265 panic("start_userspace : clone failed, errno = %d", errno
);
268 CATCH_EINTR(n
= waitpid(pid
, &status
, WUNTRACED
));
270 panic("start_userspace : wait failed, errno = %d",
272 } while(WIFSTOPPED(status
) && (WSTOPSIG(status
) == SIGVTALRM
));
274 if(!WIFSTOPPED(status
) || (WSTOPSIG(status
) != SIGSTOP
))
275 panic("start_userspace : expected SIGSTOP, got status = %d",
278 if (ptrace(PTRACE_OLDSETOPTIONS
, pid
, NULL
, (void *)PTRACE_O_TRACESYSGOOD
) < 0)
279 panic("start_userspace : PTRACE_OLDSETOPTIONS failed, errno=%d\n",
282 if(munmap(stack
, PAGE_SIZE
) < 0)
283 panic("start_userspace : munmap failed, errno = %d\n", errno
);
288 void userspace(union uml_pt_regs
*regs
)
290 int err
, status
, op
, pid
= userspace_pid
[0];
291 /* To prevent races if using_sysemu changes under us.*/
292 int local_using_sysemu
;
295 restore_registers(pid
, regs
);
297 /* Now we set local_using_sysemu to be used for one loop */
298 local_using_sysemu
= get_using_sysemu();
300 op
= SELECT_PTRACE_OPERATION(local_using_sysemu
,
301 singlestepping(NULL
));
303 err
= ptrace(op
, pid
, 0, 0);
305 panic("userspace - could not resume userspace process, "
306 "pid=%d, ptrace operation = %d, errno = %d\n",
309 CATCH_EINTR(err
= waitpid(pid
, &status
, WUNTRACED
));
311 panic("userspace - waitpid failed, errno = %d\n",
314 regs
->skas
.is_user
= 1;
315 save_registers(pid
, regs
);
316 UPT_SYSCALL_NR(regs
) = -1; /* Assume: It's not a syscall */
318 if(WIFSTOPPED(status
)){
319 int sig
= WSTOPSIG(status
);
322 if(PTRACE_FULL_FAULTINFO
|| !ptrace_faultinfo
){
323 get_skas_faultinfo(pid
, ®s
->skas
.faultinfo
);
324 (*sig_info
[SIGSEGV
])(SIGSEGV
, regs
);
326 else handle_segv(pid
, regs
);
329 handle_trap(pid
, regs
, local_using_sysemu
);
332 relay_signal(SIGTRAP
, regs
);
341 (*sig_info
[sig
])(sig
, regs
);
345 printk("userspace - child stopped with signal "
348 pid
= userspace_pid
[0];
351 /* Avoid -ERESTARTSYS handling in host */
352 if(PT_SYSCALL_NR_OFFSET
!= PT_SYSCALL_RET_OFFSET
)
353 PT_SYSCALL_NR(regs
->skas
.regs
) = -1;
358 static unsigned long thread_regs
[MAX_REG_NR
];
359 static unsigned long thread_fp_regs
[HOST_FP_SIZE
];
361 static int __init
init_thread_regs(void)
363 get_safe_registers(thread_regs
, thread_fp_regs
);
364 /* Set parent's instruction pointer to start of clone-stub */
365 thread_regs
[REGS_IP_INDEX
] = UML_CONFIG_STUB_CODE
+
366 (unsigned long) stub_clone_handler
-
367 (unsigned long) &__syscall_stub_start
;
368 thread_regs
[REGS_SP_INDEX
] = UML_CONFIG_STUB_DATA
+ PAGE_SIZE
-
370 #ifdef __SIGNAL_FRAMESIZE
371 thread_regs
[REGS_SP_INDEX
] -= __SIGNAL_FRAMESIZE
;
376 __initcall(init_thread_regs
);
378 int copy_context_skas0(unsigned long new_stack
, int pid
)
381 unsigned long current_stack
= current_stub_stack();
382 struct stub_data
*data
= (struct stub_data
*) current_stack
;
383 struct stub_data
*child_data
= (struct stub_data
*) new_stack
;
385 int new_fd
= phys_mapping(to_phys((void *)new_stack
), &new_offset
);
387 /* prepare offset and fd of child's stack as argument for parent's
388 * and child's mmap2 calls
390 *data
= ((struct stub_data
) { .offset
= MMAP_OFFSET(new_offset
),
392 .timer
= ((struct itimerval
)
393 { { 0, 1000000 / hz() },
394 { 0, 1000000 / hz() }})});
395 err
= ptrace_setregs(pid
, thread_regs
);
397 panic("copy_context_skas0 : PTRACE_SETREGS failed, "
398 "pid = %d, errno = %d\n", pid
, -err
);
400 err
= ptrace_setfpregs(pid
, thread_fp_regs
);
402 panic("copy_context_skas0 : PTRACE_SETFPREGS failed, "
403 "pid = %d, errno = %d\n", pid
, -err
);
405 /* set a well known return code for detection of child write failure */
406 child_data
->err
= 12345678;
408 /* Wait, until parent has finished its work: read child's pid from
409 * parent's stack, and check, if bad result.
411 err
= ptrace(PTRACE_CONT
, pid
, 0, 0);
413 panic("Failed to continue new process, pid = %d, "
414 "errno = %d\n", pid
, errno
);
419 panic("copy_context_skas0 - stub-parent reports error %d\n",
422 /* Wait, until child has finished too: read child's result from
423 * child's stack and check it.
426 if (child_data
->err
!= UML_CONFIG_STUB_DATA
)
427 panic("copy_context_skas0 - stub-child reports error %ld\n",
430 if (ptrace(PTRACE_OLDSETOPTIONS
, pid
, NULL
,
431 (void *)PTRACE_O_TRACESYSGOOD
) < 0)
432 panic("copy_context_skas0 : PTRACE_OLDSETOPTIONS failed, "
433 "errno = %d\n", errno
);
439 * This is used only, if stub pages are needed, while proc_mm is
440 * available. Opening /proc/mm creates a new mm_context, which lacks
441 * the stub-pages. Thus, we map them using /proc/mm-fd
443 void map_stub_pages(int fd
, unsigned long code
,
444 unsigned long data
, unsigned long stack
)
446 struct proc_mm_op mmop
;
449 int code_fd
= phys_mapping(to_phys((void *) &__syscall_stub_start
),
452 mmop
= ((struct proc_mm_op
) { .op
= MM_MMAP
,
458 .flags
= MAP_FIXED
| MAP_PRIVATE
,
460 .offset
= code_offset
462 CATCH_EINTR(n
= write(fd
, &mmop
, sizeof(mmop
)));
463 if(n
!= sizeof(mmop
)){
465 printk("mmap args - addr = 0x%lx, fd = %d, offset = %llx\n",
466 code
, code_fd
, (unsigned long long) code_offset
);
467 panic("map_stub_pages : /proc/mm map for code failed, "
473 int map_fd
= phys_mapping(to_phys((void *)stack
), &map_offset
);
474 mmop
= ((struct proc_mm_op
)
480 .prot
= PROT_READ
| PROT_WRITE
,
481 .flags
= MAP_FIXED
| MAP_SHARED
,
485 CATCH_EINTR(n
= write(fd
, &mmop
, sizeof(mmop
)));
486 if(n
!= sizeof(mmop
))
487 panic("map_stub_pages : /proc/mm map for data failed, "
488 "err = %d\n", errno
);
492 void new_thread(void *stack
, jmp_buf *buf
, void (*handler
)(void))
494 (*buf
)[0].JB_IP
= (unsigned long) handler
;
495 (*buf
)[0].JB_SP
= (unsigned long) stack
+ UM_THREAD_SIZE
-
499 #define INIT_JMP_NEW_THREAD 0
500 #define INIT_JMP_CALLBACK 1
501 #define INIT_JMP_HALT 2
502 #define INIT_JMP_REBOOT 3
504 void switch_threads(jmp_buf *me
, jmp_buf *you
)
506 if(UML_SETJMP(me
) == 0)
510 static jmp_buf initial_jmpbuf
;
512 /* XXX Make these percpu */
513 static void (*cb_proc
)(void *arg
);
515 static jmp_buf *cb_back
;
517 int start_idle_thread(void *stack
, jmp_buf *switch_buf
)
521 set_handler(SIGWINCH
, (__sighandler_t
) sig_handler
,
522 SA_ONSTACK
| SA_RESTART
, SIGUSR1
, SIGIO
, SIGALRM
,
526 * Can't use UML_SETJMP or UML_LONGJMP here because they save
527 * and restore signals, with the possible side-effect of
528 * trying to handle any signals which came when they were
529 * blocked, which can't be done on this stack.
530 * Signals must be blocked when jumping back here and restored
531 * after returning to the jumper.
533 n
= setjmp(initial_jmpbuf
);
535 case INIT_JMP_NEW_THREAD
:
536 (*switch_buf
)[0].JB_IP
= (unsigned long) new_thread_handler
;
537 (*switch_buf
)[0].JB_SP
= (unsigned long) stack
+
538 UM_THREAD_SIZE
- sizeof(void *);
540 case INIT_JMP_CALLBACK
:
542 longjmp(*cb_back
, 1);
547 case INIT_JMP_REBOOT
:
551 panic("Bad sigsetjmp return in start_idle_thread - %d\n", n
);
553 longjmp(*switch_buf
, 1);
556 void initial_thread_cb_skas(void (*proc
)(void *), void *arg
)
565 if(UML_SETJMP(&here
) == 0)
566 UML_LONGJMP(&initial_jmpbuf
, INIT_JMP_CALLBACK
);
577 UML_LONGJMP(&initial_jmpbuf
, INIT_JMP_HALT
);
580 void reboot_skas(void)
583 UML_LONGJMP(&initial_jmpbuf
, INIT_JMP_REBOOT
);
586 void switch_mm_skas(struct mm_id
*mm_idp
)
590 /* FIXME: need cpu pid in switch_mm_skas */
592 err
= ptrace(PTRACE_SWITCH_MM
, userspace_pid
[0], 0,
595 panic("switch_mm_skas - PTRACE_SWITCH_MM failed, "
596 "errno = %d\n", errno
);
598 else userspace_pid
[0] = mm_idp
->u
.pid
;