process.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. * Copyright (C) 2002- 2004 Jeff Dike (jdike@addtoit.com)
  3. * Licensed under the GPL
  4. */
  5. #include <stdlib.h>
  6. #include <string.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 <sys/time.h>
  16. #include <asm/unistd.h>
  17. #include <asm/types.h>
  18. #include "user.h"
  19. #include "ptrace_user.h"
  20. #include "time_user.h"
  21. #include "sysdep/ptrace.h"
  22. #include "user_util.h"
  23. #include "kern_util.h"
  24. #include "skas.h"
  25. #include "stub-data.h"
  26. #include "mm_id.h"
  27. #include "sysdep/sigcontext.h"
  28. #include "sysdep/stub.h"
  29. #include "os.h"
  30. #include "proc_mm.h"
  31. #include "skas_ptrace.h"
  32. #include "chan_user.h"
  33. #include "signal_user.h"
  34. #include "registers.h"
  35. #include "mem.h"
  36. #include "uml-config.h"
  37. #include "process.h"
  38. int is_skas_winch(int pid, int fd, void *data)
  39. {
  40. if(pid != os_getpgrp())
  41. return(0);
  42. register_winch_irq(-1, fd, -1, data);
  43. return(1);
  44. }
  45. void wait_stub_done(int pid, int sig, char * fname)
  46. {
  47. int n, status, err;
  48. do {
  49. if ( sig != -1 ) {
  50. err = ptrace(PTRACE_CONT, pid, 0, sig);
  51. if(err)
  52. panic("%s : continue failed, errno = %d\n",
  53. fname, errno);
  54. }
  55. sig = 0;
  56. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  57. } while((n >= 0) && WIFSTOPPED(status) &&
  58. (WSTOPSIG(status) == SIGVTALRM));
  59. if((n < 0) || !WIFSTOPPED(status) ||
  60. (WSTOPSIG(status) != SIGUSR1 && WSTOPSIG(status) != SIGTRAP)){
  61. panic("%s : failed to wait for SIGUSR1/SIGTRAP, "
  62. "pid = %d, n = %d, errno = %d, status = 0x%x\n",
  63. fname, pid, n, errno, status);
  64. }
  65. }
  66. void get_skas_faultinfo(int pid, struct faultinfo * fi)
  67. {
  68. int err;
  69. if(ptrace_faultinfo){
  70. err = ptrace(PTRACE_FAULTINFO, pid, 0, fi);
  71. if(err)
  72. panic("get_skas_faultinfo - PTRACE_FAULTINFO failed, "
  73. "errno = %d\n", errno);
  74. /* Special handling for i386, which has different structs */
  75. if (sizeof(struct ptrace_faultinfo) < sizeof(struct faultinfo))
  76. memset((char *)fi + sizeof(struct ptrace_faultinfo), 0,
  77. sizeof(struct faultinfo) -
  78. sizeof(struct ptrace_faultinfo));
  79. }
  80. else {
  81. wait_stub_done(pid, SIGSEGV, "get_skas_faultinfo");
  82. /* faultinfo is prepared by the stub-segv-handler at start of
  83. * the stub stack page. We just have to copy it.
  84. */
  85. memcpy(fi, (void *)current_stub_stack(), sizeof(*fi));
  86. }
  87. }
  88. static void handle_segv(int pid, union uml_pt_regs * regs)
  89. {
  90. get_skas_faultinfo(pid, &regs->skas.faultinfo);
  91. segv(regs->skas.faultinfo, 0, 1, NULL);
  92. }
  93. /*To use the same value of using_sysemu as the caller, ask it that value (in local_using_sysemu)*/
  94. static void handle_trap(int pid, union uml_pt_regs *regs, int local_using_sysemu)
  95. {
  96. int err, status;
  97. /* Mark this as a syscall */
  98. UPT_SYSCALL_NR(regs) = PT_SYSCALL_NR(regs->skas.regs);
  99. if (!local_using_sysemu)
  100. {
  101. err = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET, __NR_getpid);
  102. if(err < 0)
  103. panic("handle_trap - nullifying syscall failed errno = %d\n",
  104. errno);
  105. err = ptrace(PTRACE_SYSCALL, pid, 0, 0);
  106. if(err < 0)
  107. panic("handle_trap - continuing to end of syscall failed, "
  108. "errno = %d\n", errno);
  109. CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED));
  110. if((err < 0) || !WIFSTOPPED(status) ||
  111. (WSTOPSIG(status) != SIGTRAP + 0x80))
  112. panic("handle_trap - failed to wait at end of syscall, "
  113. "errno = %d, status = %d\n", errno, status);
  114. }
  115. handle_syscall(regs);
  116. }
  117. extern int __syscall_stub_start;
  118. static int userspace_tramp(void *stack)
  119. {
  120. void *addr;
  121. ptrace(PTRACE_TRACEME, 0, 0, 0);
  122. init_new_thread_signals(1);
  123. enable_timer();
  124. if(!proc_mm){
  125. /* This has a pte, but it can't be mapped in with the usual
  126. * tlb_flush mechanism because this is part of that mechanism
  127. */
  128. int fd;
  129. __u64 offset;
  130. fd = phys_mapping(to_phys(&__syscall_stub_start), &offset);
  131. addr = mmap64((void *) UML_CONFIG_STUB_CODE, page_size(),
  132. PROT_EXEC, MAP_FIXED | MAP_PRIVATE, fd, offset);
  133. if(addr == MAP_FAILED){
  134. printk("mapping mmap stub failed, errno = %d\n",
  135. errno);
  136. exit(1);
  137. }
  138. if(stack != NULL){
  139. fd = phys_mapping(to_phys(stack), &offset);
  140. addr = mmap((void *) UML_CONFIG_STUB_DATA, page_size(),
  141. PROT_READ | PROT_WRITE,
  142. MAP_FIXED | MAP_SHARED, fd, offset);
  143. if(addr == MAP_FAILED){
  144. printk("mapping segfault stack failed, "
  145. "errno = %d\n", errno);
  146. exit(1);
  147. }
  148. }
  149. }
  150. if(!ptrace_faultinfo && (stack != NULL)){
  151. unsigned long v = UML_CONFIG_STUB_CODE +
  152. (unsigned long) stub_segv_handler -
  153. (unsigned long) &__syscall_stub_start;
  154. set_sigstack((void *) UML_CONFIG_STUB_DATA, page_size());
  155. set_handler(SIGSEGV, (void *) v, SA_ONSTACK,
  156. SIGIO, SIGWINCH, SIGALRM, SIGVTALRM,
  157. SIGUSR1, -1);
  158. }
  159. os_stop_process(os_getpid());
  160. return(0);
  161. }
  162. /* Each element set once, and only accessed by a single processor anyway */
  163. #undef NR_CPUS
  164. #define NR_CPUS 1
  165. int userspace_pid[NR_CPUS];
  166. int start_userspace(unsigned long stub_stack)
  167. {
  168. void *stack;
  169. unsigned long sp;
  170. int pid, status, n, flags;
  171. stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
  172. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  173. if(stack == MAP_FAILED)
  174. panic("start_userspace : mmap failed, errno = %d", errno);
  175. sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *);
  176. flags = CLONE_FILES | SIGCHLD;
  177. if(proc_mm) flags |= CLONE_VM;
  178. pid = clone(userspace_tramp, (void *) sp, flags, (void *) stub_stack);
  179. if(pid < 0)
  180. panic("start_userspace : clone failed, errno = %d", errno);
  181. do {
  182. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  183. if(n < 0)
  184. panic("start_userspace : wait failed, errno = %d",
  185. errno);
  186. } while(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGVTALRM));
  187. if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
  188. panic("start_userspace : expected SIGSTOP, got status = %d",
  189. status);
  190. if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL, (void *)PTRACE_O_TRACESYSGOOD) < 0)
  191. panic("start_userspace : PTRACE_SETOPTIONS failed, errno=%d\n",
  192. errno);
  193. if(munmap(stack, PAGE_SIZE) < 0)
  194. panic("start_userspace : munmap failed, errno = %d\n", errno);
  195. return(pid);
  196. }
  197. void userspace(union uml_pt_regs *regs)
  198. {
  199. int err, status, op, pid = userspace_pid[0];
  200. int local_using_sysemu; /*To prevent races if using_sysemu changes under us.*/
  201. while(1){
  202. restore_registers(pid, regs);
  203. /* Now we set local_using_sysemu to be used for one loop */
  204. local_using_sysemu = get_using_sysemu();
  205. op = SELECT_PTRACE_OPERATION(local_using_sysemu, singlestepping(NULL));
  206. err = ptrace(op, pid, 0, 0);
  207. if(err)
  208. panic("userspace - could not resume userspace process, "
  209. "pid=%d, ptrace operation = %d, errno = %d\n",
  210. op, errno);
  211. CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED));
  212. if(err < 0)
  213. panic("userspace - waitpid failed, errno = %d\n",
  214. errno);
  215. regs->skas.is_user = 1;
  216. save_registers(pid, regs);
  217. UPT_SYSCALL_NR(regs) = -1; /* Assume: It's not a syscall */
  218. if(WIFSTOPPED(status)){
  219. switch(WSTOPSIG(status)){
  220. case SIGSEGV:
  221. if(PTRACE_FULL_FAULTINFO || !ptrace_faultinfo)
  222. user_signal(SIGSEGV, regs, pid);
  223. else handle_segv(pid, regs);
  224. break;
  225. case SIGTRAP + 0x80:
  226. handle_trap(pid, regs, local_using_sysemu);
  227. break;
  228. case SIGTRAP:
  229. relay_signal(SIGTRAP, regs);
  230. break;
  231. case SIGIO:
  232. case SIGVTALRM:
  233. case SIGILL:
  234. case SIGBUS:
  235. case SIGFPE:
  236. case SIGWINCH:
  237. user_signal(WSTOPSIG(status), regs, pid);
  238. break;
  239. default:
  240. printk("userspace - child stopped with signal "
  241. "%d\n", WSTOPSIG(status));
  242. }
  243. pid = userspace_pid[0];
  244. interrupt_end();
  245. /* Avoid -ERESTARTSYS handling in host */
  246. PT_SYSCALL_NR(regs->skas.regs) = -1;
  247. }
  248. }
  249. }
  250. #define INIT_JMP_NEW_THREAD 0
  251. #define INIT_JMP_REMOVE_SIGSTACK 1
  252. #define INIT_JMP_CALLBACK 2
  253. #define INIT_JMP_HALT 3
  254. #define INIT_JMP_REBOOT 4
  255. int copy_context_skas0(unsigned long new_stack, int pid)
  256. {
  257. int err;
  258. unsigned long regs[MAX_REG_NR];
  259. unsigned long current_stack = current_stub_stack();
  260. struct stub_data *data = (struct stub_data *) current_stack;
  261. struct stub_data *child_data = (struct stub_data *) new_stack;
  262. __u64 new_offset;
  263. int new_fd = phys_mapping(to_phys((void *)new_stack), &new_offset);
  264. /* prepare offset and fd of child's stack as argument for parent's
  265. * and child's mmap2 calls
  266. */
  267. *data = ((struct stub_data) { .offset = MMAP_OFFSET(new_offset),
  268. .fd = new_fd,
  269. .timer = ((struct itimerval)
  270. { { 0, 1000000 / hz() },
  271. { 0, 1000000 / hz() }})});
  272. get_safe_registers(regs);
  273. /* Set parent's instruction pointer to start of clone-stub */
  274. regs[REGS_IP_INDEX] = UML_CONFIG_STUB_CODE +
  275. (unsigned long) stub_clone_handler -
  276. (unsigned long) &__syscall_stub_start;
  277. regs[REGS_SP_INDEX] = UML_CONFIG_STUB_DATA + PAGE_SIZE -
  278. sizeof(void *);
  279. err = ptrace_setregs(pid, regs);
  280. if(err < 0)
  281. panic("copy_context_skas0 : PTRACE_SETREGS failed, "
  282. "pid = %d, errno = %d\n", pid, errno);
  283. /* set a well known return code for detection of child write failure */
  284. child_data->err = 12345678;
  285. /* Wait, until parent has finished its work: read child's pid from
  286. * parent's stack, and check, if bad result.
  287. */
  288. wait_stub_done(pid, 0, "copy_context_skas0");
  289. pid = data->err;
  290. if(pid < 0)
  291. panic("copy_context_skas0 - stub-parent reports error %d\n",
  292. pid);
  293. /* Wait, until child has finished too: read child's result from
  294. * child's stack and check it.
  295. */
  296. wait_stub_done(pid, -1, "copy_context_skas0");
  297. if (child_data->err != UML_CONFIG_STUB_DATA)
  298. panic("copy_context_skas0 - stub-child reports error %d\n",
  299. child_data->err);
  300. if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL,
  301. (void *)PTRACE_O_TRACESYSGOOD) < 0)
  302. panic("copy_context_skas0 : PTRACE_SETOPTIONS failed, "
  303. "errno = %d\n", errno);
  304. return pid;
  305. }
  306. void new_thread(void *stack, void **switch_buf_ptr, void **fork_buf_ptr,
  307. void (*handler)(int))
  308. {
  309. unsigned long flags;
  310. sigjmp_buf switch_buf, fork_buf;
  311. *switch_buf_ptr = &switch_buf;
  312. *fork_buf_ptr = &fork_buf;
  313. /* Somewhat subtle - siglongjmp restores the signal mask before doing
  314. * the longjmp. This means that when jumping from one stack to another
  315. * when the target stack has interrupts enabled, an interrupt may occur
  316. * on the source stack. This is bad when starting up a process because
  317. * it's not supposed to get timer ticks until it has been scheduled.
  318. * So, we disable interrupts around the sigsetjmp to ensure that
  319. * they can't happen until we get back here where they are safe.
  320. */
  321. flags = get_signals();
  322. block_signals();
  323. if(sigsetjmp(fork_buf, 1) == 0)
  324. new_thread_proc(stack, handler);
  325. remove_sigstack();
  326. set_signals(flags);
  327. }
  328. void thread_wait(void *sw, void *fb)
  329. {
  330. sigjmp_buf buf, **switch_buf = sw, *fork_buf;
  331. *switch_buf = &buf;
  332. fork_buf = fb;
  333. if(sigsetjmp(buf, 1) == 0)
  334. siglongjmp(*fork_buf, INIT_JMP_REMOVE_SIGSTACK);
  335. }
  336. void switch_threads(void *me, void *next)
  337. {
  338. sigjmp_buf my_buf, **me_ptr = me, *next_buf = next;
  339. *me_ptr = &my_buf;
  340. if(sigsetjmp(my_buf, 1) == 0)
  341. siglongjmp(*next_buf, 1);
  342. }
  343. static sigjmp_buf initial_jmpbuf;
  344. /* XXX Make these percpu */
  345. static void (*cb_proc)(void *arg);
  346. static void *cb_arg;
  347. static sigjmp_buf *cb_back;
  348. int start_idle_thread(void *stack, void *switch_buf_ptr, void **fork_buf_ptr)
  349. {
  350. sigjmp_buf **switch_buf = switch_buf_ptr;
  351. int n;
  352. set_handler(SIGWINCH, (__sighandler_t) sig_handler,
  353. SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGALRM,
  354. SIGVTALRM, -1);
  355. *fork_buf_ptr = &initial_jmpbuf;
  356. n = sigsetjmp(initial_jmpbuf, 1);
  357. switch(n){
  358. case INIT_JMP_NEW_THREAD:
  359. new_thread_proc((void *) stack, new_thread_handler);
  360. break;
  361. case INIT_JMP_REMOVE_SIGSTACK:
  362. remove_sigstack();
  363. break;
  364. case INIT_JMP_CALLBACK:
  365. (*cb_proc)(cb_arg);
  366. siglongjmp(*cb_back, 1);
  367. break;
  368. case INIT_JMP_HALT:
  369. kmalloc_ok = 0;
  370. return(0);
  371. case INIT_JMP_REBOOT:
  372. kmalloc_ok = 0;
  373. return(1);
  374. default:
  375. panic("Bad sigsetjmp return in start_idle_thread - %d\n", n);
  376. }
  377. siglongjmp(**switch_buf, 1);
  378. }
  379. void remove_sigstack(void)
  380. {
  381. stack_t stack = ((stack_t) { .ss_flags = SS_DISABLE,
  382. .ss_sp = NULL,
  383. .ss_size = 0 });
  384. if(sigaltstack(&stack, NULL) != 0)
  385. panic("disabling signal stack failed, errno = %d\n", errno);
  386. }
  387. void initial_thread_cb_skas(void (*proc)(void *), void *arg)
  388. {
  389. sigjmp_buf here;
  390. cb_proc = proc;
  391. cb_arg = arg;
  392. cb_back = &here;
  393. block_signals();
  394. if(sigsetjmp(here, 1) == 0)
  395. siglongjmp(initial_jmpbuf, INIT_JMP_CALLBACK);
  396. unblock_signals();
  397. cb_proc = NULL;
  398. cb_arg = NULL;
  399. cb_back = NULL;
  400. }
  401. void halt_skas(void)
  402. {
  403. block_signals();
  404. siglongjmp(initial_jmpbuf, INIT_JMP_HALT);
  405. }
  406. void reboot_skas(void)
  407. {
  408. block_signals();
  409. siglongjmp(initial_jmpbuf, INIT_JMP_REBOOT);
  410. }
  411. void switch_mm_skas(struct mm_id *mm_idp)
  412. {
  413. int err;
  414. #warning need cpu pid in switch_mm_skas
  415. if(proc_mm){
  416. err = ptrace(PTRACE_SWITCH_MM, userspace_pid[0], 0,
  417. mm_idp->u.mm_fd);
  418. if(err)
  419. panic("switch_mm_skas - PTRACE_SWITCH_MM failed, "
  420. "errno = %d\n", errno);
  421. }
  422. else userspace_pid[0] = mm_idp->u.pid;
  423. }
  424. /*
  425. * Overrides for Emacs so that we follow Linus's tabbing style.
  426. * Emacs will notice this stuff at the end of the file and automatically
  427. * adjust the settings for this buffer only. This must remain at the end
  428. * of the file.
  429. * ---------------------------------------------------------------------------
  430. * Local variables:
  431. * c-file-style: "linux"
  432. * End:
  433. */