process.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. /*
  2. * Copyright (C) 2015 Thomas Meyer ([email protected])
  3. * Copyright (C) 2002- 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  4. * Licensed under the GPL
  5. */
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <sched.h>
  9. #include <errno.h>
  10. #include <string.h>
  11. #include <sys/mman.h>
  12. #include <sys/wait.h>
  13. #include <asm/unistd.h>
  14. #include <as-layout.h>
  15. #include <init.h>
  16. #include <kern_util.h>
  17. #include <mem.h>
  18. #include <os.h>
  19. #include <ptrace_user.h>
  20. #include <registers.h>
  21. #include <skas.h>
  22. #include <sysdep/stub.h>
  23. int is_skas_winch(int pid, int fd, void *data)
  24. {
  25. return pid == getpgrp();
  26. }
  27. static int ptrace_dump_regs(int pid)
  28. {
  29. unsigned long regs[MAX_REG_NR];
  30. int i;
  31. if (ptrace(PTRACE_GETREGS, pid, 0, regs) < 0)
  32. return -errno;
  33. printk(UM_KERN_ERR "Stub registers -\n");
  34. for (i = 0; i < ARRAY_SIZE(regs); i++)
  35. printk(UM_KERN_ERR "\t%d - %lx\n", i, regs[i]);
  36. return 0;
  37. }
  38. /*
  39. * Signals that are OK to receive in the stub - we'll just continue it.
  40. * SIGWINCH will happen when UML is inside a detached screen.
  41. */
  42. #define STUB_SIG_MASK ((1 << SIGALRM) | (1 << SIGWINCH))
  43. /* Signals that the stub will finish with - anything else is an error */
  44. #define STUB_DONE_MASK (1 << SIGTRAP)
  45. void wait_stub_done(int pid)
  46. {
  47. int n, status, err;
  48. while (1) {
  49. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED | __WALL));
  50. if ((n < 0) || !WIFSTOPPED(status))
  51. goto bad_wait;
  52. if (((1 << WSTOPSIG(status)) & STUB_SIG_MASK) == 0)
  53. break;
  54. err = ptrace(PTRACE_CONT, pid, 0, 0);
  55. if (err) {
  56. printk(UM_KERN_ERR "wait_stub_done : continue failed, "
  57. "errno = %d\n", errno);
  58. fatal_sigsegv();
  59. }
  60. }
  61. if (((1 << WSTOPSIG(status)) & STUB_DONE_MASK) != 0)
  62. return;
  63. bad_wait:
  64. err = ptrace_dump_regs(pid);
  65. if (err)
  66. printk(UM_KERN_ERR "Failed to get registers from stub, "
  67. "errno = %d\n", -err);
  68. printk(UM_KERN_ERR "wait_stub_done : failed to wait for SIGTRAP, "
  69. "pid = %d, n = %d, errno = %d, status = 0x%x\n", pid, n, errno,
  70. status);
  71. fatal_sigsegv();
  72. }
  73. extern unsigned long current_stub_stack(void);
  74. static void get_skas_faultinfo(int pid, struct faultinfo *fi, unsigned long *aux_fp_regs)
  75. {
  76. int err;
  77. err = get_fp_registers(pid, aux_fp_regs);
  78. if (err < 0) {
  79. printk(UM_KERN_ERR "save_fp_registers returned %d\n",
  80. err);
  81. fatal_sigsegv();
  82. }
  83. err = ptrace(PTRACE_CONT, pid, 0, SIGSEGV);
  84. if (err) {
  85. printk(UM_KERN_ERR "Failed to continue stub, pid = %d, "
  86. "errno = %d\n", pid, errno);
  87. fatal_sigsegv();
  88. }
  89. wait_stub_done(pid);
  90. /*
  91. * faultinfo is prepared by the stub-segv-handler at start of
  92. * the stub stack page. We just have to copy it.
  93. */
  94. memcpy(fi, (void *)current_stub_stack(), sizeof(*fi));
  95. err = put_fp_registers(pid, aux_fp_regs);
  96. if (err < 0) {
  97. printk(UM_KERN_ERR "put_fp_registers returned %d\n",
  98. err);
  99. fatal_sigsegv();
  100. }
  101. }
  102. static void handle_segv(int pid, struct uml_pt_regs *regs, unsigned long *aux_fp_regs)
  103. {
  104. get_skas_faultinfo(pid, &regs->faultinfo, aux_fp_regs);
  105. segv(regs->faultinfo, 0, 1, NULL);
  106. }
  107. /*
  108. * To use the same value of using_sysemu as the caller, ask it that value
  109. * (in local_using_sysemu
  110. */
  111. static void handle_trap(int pid, struct uml_pt_regs *regs,
  112. int local_using_sysemu)
  113. {
  114. int err, status;
  115. if ((UPT_IP(regs) >= STUB_START) && (UPT_IP(regs) < STUB_END))
  116. fatal_sigsegv();
  117. if (!local_using_sysemu)
  118. {
  119. err = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_NR_OFFSET,
  120. __NR_getpid);
  121. if (err < 0) {
  122. printk(UM_KERN_ERR "handle_trap - nullifying syscall "
  123. "failed, errno = %d\n", errno);
  124. fatal_sigsegv();
  125. }
  126. err = ptrace(PTRACE_SYSCALL, pid, 0, 0);
  127. if (err < 0) {
  128. printk(UM_KERN_ERR "handle_trap - continuing to end of "
  129. "syscall failed, errno = %d\n", errno);
  130. fatal_sigsegv();
  131. }
  132. CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED | __WALL));
  133. if ((err < 0) || !WIFSTOPPED(status) ||
  134. (WSTOPSIG(status) != SIGTRAP + 0x80)) {
  135. err = ptrace_dump_regs(pid);
  136. if (err)
  137. printk(UM_KERN_ERR "Failed to get registers "
  138. "from process, errno = %d\n", -err);
  139. printk(UM_KERN_ERR "handle_trap - failed to wait at "
  140. "end of syscall, errno = %d, status = %d\n",
  141. errno, status);
  142. fatal_sigsegv();
  143. }
  144. }
  145. handle_syscall(regs);
  146. }
  147. extern char __syscall_stub_start[];
  148. static int userspace_tramp(void *stack)
  149. {
  150. void *addr;
  151. int fd;
  152. unsigned long long offset;
  153. ptrace(PTRACE_TRACEME, 0, 0, 0);
  154. signal(SIGTERM, SIG_DFL);
  155. signal(SIGWINCH, SIG_IGN);
  156. /*
  157. * This has a pte, but it can't be mapped in with the usual
  158. * tlb_flush mechanism because this is part of that mechanism
  159. */
  160. fd = phys_mapping(to_phys(__syscall_stub_start), &offset);
  161. addr = mmap64((void *) STUB_CODE, UM_KERN_PAGE_SIZE,
  162. PROT_EXEC, MAP_FIXED | MAP_PRIVATE, fd, offset);
  163. if (addr == MAP_FAILED) {
  164. printk(UM_KERN_ERR "mapping mmap stub at 0x%lx failed, "
  165. "errno = %d\n", STUB_CODE, errno);
  166. exit(1);
  167. }
  168. if (stack != NULL) {
  169. fd = phys_mapping(to_phys(stack), &offset);
  170. addr = mmap((void *) STUB_DATA,
  171. UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE,
  172. MAP_FIXED | MAP_SHARED, fd, offset);
  173. if (addr == MAP_FAILED) {
  174. printk(UM_KERN_ERR "mapping segfault stack "
  175. "at 0x%lx failed, errno = %d\n",
  176. STUB_DATA, errno);
  177. exit(1);
  178. }
  179. }
  180. if (stack != NULL) {
  181. struct sigaction sa;
  182. unsigned long v = STUB_CODE +
  183. (unsigned long) stub_segv_handler -
  184. (unsigned long) __syscall_stub_start;
  185. set_sigstack((void *) STUB_DATA, UM_KERN_PAGE_SIZE);
  186. sigemptyset(&sa.sa_mask);
  187. sa.sa_flags = SA_ONSTACK | SA_NODEFER | SA_SIGINFO;
  188. sa.sa_sigaction = (void *) v;
  189. sa.sa_restorer = NULL;
  190. if (sigaction(SIGSEGV, &sa, NULL) < 0) {
  191. printk(UM_KERN_ERR "userspace_tramp - setting SIGSEGV "
  192. "handler failed - errno = %d\n", errno);
  193. exit(1);
  194. }
  195. }
  196. kill(os_getpid(), SIGSTOP);
  197. return 0;
  198. }
  199. /* Each element set once, and only accessed by a single processor anyway */
  200. #undef NR_CPUS
  201. #define NR_CPUS 1
  202. int userspace_pid[NR_CPUS];
  203. int start_userspace(unsigned long stub_stack)
  204. {
  205. void *stack;
  206. unsigned long sp;
  207. int pid, status, n, flags, err;
  208. stack = mmap(NULL, UM_KERN_PAGE_SIZE,
  209. PROT_READ | PROT_WRITE | PROT_EXEC,
  210. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  211. if (stack == MAP_FAILED) {
  212. err = -errno;
  213. printk(UM_KERN_ERR "start_userspace : mmap failed, "
  214. "errno = %d\n", errno);
  215. return err;
  216. }
  217. sp = (unsigned long) stack + UM_KERN_PAGE_SIZE - sizeof(void *);
  218. flags = CLONE_FILES | SIGCHLD;
  219. pid = clone(userspace_tramp, (void *) sp, flags, (void *) stub_stack);
  220. if (pid < 0) {
  221. err = -errno;
  222. printk(UM_KERN_ERR "start_userspace : clone failed, "
  223. "errno = %d\n", errno);
  224. return err;
  225. }
  226. do {
  227. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED | __WALL));
  228. if (n < 0) {
  229. err = -errno;
  230. printk(UM_KERN_ERR "start_userspace : wait failed, "
  231. "errno = %d\n", errno);
  232. goto out_kill;
  233. }
  234. } while (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGALRM));
  235. if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP)) {
  236. err = -EINVAL;
  237. printk(UM_KERN_ERR "start_userspace : expected SIGSTOP, got "
  238. "status = %d\n", status);
  239. goto out_kill;
  240. }
  241. if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL,
  242. (void *) PTRACE_O_TRACESYSGOOD) < 0) {
  243. err = -errno;
  244. printk(UM_KERN_ERR "start_userspace : PTRACE_OLDSETOPTIONS "
  245. "failed, errno = %d\n", errno);
  246. goto out_kill;
  247. }
  248. if (munmap(stack, UM_KERN_PAGE_SIZE) < 0) {
  249. err = -errno;
  250. printk(UM_KERN_ERR "start_userspace : munmap failed, "
  251. "errno = %d\n", errno);
  252. goto out_kill;
  253. }
  254. return pid;
  255. out_kill:
  256. os_kill_ptraced_process(pid, 1);
  257. return err;
  258. }
  259. void userspace(struct uml_pt_regs *regs, unsigned long *aux_fp_regs)
  260. {
  261. int err, status, op, pid = userspace_pid[0];
  262. /* To prevent races if using_sysemu changes under us.*/
  263. int local_using_sysemu;
  264. siginfo_t si;
  265. /* Handle any immediate reschedules or signals */
  266. interrupt_end();
  267. while (1) {
  268. /*
  269. * This can legitimately fail if the process loads a
  270. * bogus value into a segment register. It will
  271. * segfault and PTRACE_GETREGS will read that value
  272. * out of the process. However, PTRACE_SETREGS will
  273. * fail. In this case, there is nothing to do but
  274. * just kill the process.
  275. */
  276. if (ptrace(PTRACE_SETREGS, pid, 0, regs->gp))
  277. fatal_sigsegv();
  278. if (put_fp_registers(pid, regs->fp))
  279. fatal_sigsegv();
  280. /* Now we set local_using_sysemu to be used for one loop */
  281. local_using_sysemu = get_using_sysemu();
  282. op = SELECT_PTRACE_OPERATION(local_using_sysemu,
  283. singlestepping(NULL));
  284. if (ptrace(op, pid, 0, 0)) {
  285. printk(UM_KERN_ERR "userspace - ptrace continue "
  286. "failed, op = %d, errno = %d\n", op, errno);
  287. fatal_sigsegv();
  288. }
  289. CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED | __WALL));
  290. if (err < 0) {
  291. printk(UM_KERN_ERR "userspace - wait failed, "
  292. "errno = %d\n", errno);
  293. fatal_sigsegv();
  294. }
  295. regs->is_user = 1;
  296. if (ptrace(PTRACE_GETREGS, pid, 0, regs->gp)) {
  297. printk(UM_KERN_ERR "userspace - PTRACE_GETREGS failed, "
  298. "errno = %d\n", errno);
  299. fatal_sigsegv();
  300. }
  301. if (get_fp_registers(pid, regs->fp)) {
  302. printk(UM_KERN_ERR "userspace - get_fp_registers failed, "
  303. "errno = %d\n", errno);
  304. fatal_sigsegv();
  305. }
  306. UPT_SYSCALL_NR(regs) = -1; /* Assume: It's not a syscall */
  307. if (WIFSTOPPED(status)) {
  308. int sig = WSTOPSIG(status);
  309. ptrace(PTRACE_GETSIGINFO, pid, 0, (struct siginfo *)&si);
  310. switch (sig) {
  311. case SIGSEGV:
  312. if (PTRACE_FULL_FAULTINFO) {
  313. get_skas_faultinfo(pid,
  314. &regs->faultinfo, aux_fp_regs);
  315. (*sig_info[SIGSEGV])(SIGSEGV, (struct siginfo *)&si,
  316. regs);
  317. }
  318. else handle_segv(pid, regs, aux_fp_regs);
  319. break;
  320. case SIGTRAP + 0x80:
  321. handle_trap(pid, regs, local_using_sysemu);
  322. break;
  323. case SIGTRAP:
  324. relay_signal(SIGTRAP, (struct siginfo *)&si, regs);
  325. break;
  326. case SIGALRM:
  327. break;
  328. case SIGIO:
  329. case SIGILL:
  330. case SIGBUS:
  331. case SIGFPE:
  332. case SIGWINCH:
  333. block_signals();
  334. (*sig_info[sig])(sig, (struct siginfo *)&si, regs);
  335. unblock_signals();
  336. break;
  337. default:
  338. printk(UM_KERN_ERR "userspace - child stopped "
  339. "with signal %d\n", sig);
  340. fatal_sigsegv();
  341. }
  342. pid = userspace_pid[0];
  343. interrupt_end();
  344. /* Avoid -ERESTARTSYS handling in host */
  345. if (PT_SYSCALL_NR_OFFSET != PT_SYSCALL_RET_OFFSET)
  346. PT_SYSCALL_NR(regs->gp) = -1;
  347. }
  348. }
  349. }
  350. static unsigned long thread_regs[MAX_REG_NR];
  351. static unsigned long thread_fp_regs[FP_SIZE];
  352. static int __init init_thread_regs(void)
  353. {
  354. get_safe_registers(thread_regs, thread_fp_regs);
  355. /* Set parent's instruction pointer to start of clone-stub */
  356. thread_regs[REGS_IP_INDEX] = STUB_CODE +
  357. (unsigned long) stub_clone_handler -
  358. (unsigned long) __syscall_stub_start;
  359. thread_regs[REGS_SP_INDEX] = STUB_DATA + UM_KERN_PAGE_SIZE -
  360. sizeof(void *);
  361. #ifdef __SIGNAL_FRAMESIZE
  362. thread_regs[REGS_SP_INDEX] -= __SIGNAL_FRAMESIZE;
  363. #endif
  364. return 0;
  365. }
  366. __initcall(init_thread_regs);
  367. int copy_context_skas0(unsigned long new_stack, int pid)
  368. {
  369. int err;
  370. unsigned long current_stack = current_stub_stack();
  371. struct stub_data *data = (struct stub_data *) current_stack;
  372. struct stub_data *child_data = (struct stub_data *) new_stack;
  373. unsigned long long new_offset;
  374. int new_fd = phys_mapping(to_phys((void *)new_stack), &new_offset);
  375. /*
  376. * prepare offset and fd of child's stack as argument for parent's
  377. * and child's mmap2 calls
  378. */
  379. *data = ((struct stub_data) {
  380. .offset = MMAP_OFFSET(new_offset),
  381. .fd = new_fd
  382. });
  383. err = ptrace_setregs(pid, thread_regs);
  384. if (err < 0) {
  385. err = -errno;
  386. printk(UM_KERN_ERR "copy_context_skas0 : PTRACE_SETREGS "
  387. "failed, pid = %d, errno = %d\n", pid, -err);
  388. return err;
  389. }
  390. err = put_fp_registers(pid, thread_fp_regs);
  391. if (err < 0) {
  392. printk(UM_KERN_ERR "copy_context_skas0 : put_fp_registers "
  393. "failed, pid = %d, err = %d\n", pid, err);
  394. return err;
  395. }
  396. /* set a well known return code for detection of child write failure */
  397. child_data->err = 12345678;
  398. /*
  399. * Wait, until parent has finished its work: read child's pid from
  400. * parent's stack, and check, if bad result.
  401. */
  402. err = ptrace(PTRACE_CONT, pid, 0, 0);
  403. if (err) {
  404. err = -errno;
  405. printk(UM_KERN_ERR "Failed to continue new process, pid = %d, "
  406. "errno = %d\n", pid, errno);
  407. return err;
  408. }
  409. wait_stub_done(pid);
  410. pid = data->err;
  411. if (pid < 0) {
  412. printk(UM_KERN_ERR "copy_context_skas0 - stub-parent reports "
  413. "error %d\n", -pid);
  414. return pid;
  415. }
  416. /*
  417. * Wait, until child has finished too: read child's result from
  418. * child's stack and check it.
  419. */
  420. wait_stub_done(pid);
  421. if (child_data->err != STUB_DATA) {
  422. printk(UM_KERN_ERR "copy_context_skas0 - stub-child reports "
  423. "error %ld\n", child_data->err);
  424. err = child_data->err;
  425. goto out_kill;
  426. }
  427. if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL,
  428. (void *)PTRACE_O_TRACESYSGOOD) < 0) {
  429. err = -errno;
  430. printk(UM_KERN_ERR "copy_context_skas0 : PTRACE_OLDSETOPTIONS "
  431. "failed, errno = %d\n", errno);
  432. goto out_kill;
  433. }
  434. return pid;
  435. out_kill:
  436. os_kill_ptraced_process(pid, 1);
  437. return err;
  438. }
  439. void new_thread(void *stack, jmp_buf *buf, void (*handler)(void))
  440. {
  441. (*buf)[0].JB_IP = (unsigned long) handler;
  442. (*buf)[0].JB_SP = (unsigned long) stack + UM_THREAD_SIZE -
  443. sizeof(void *);
  444. }
  445. #define INIT_JMP_NEW_THREAD 0
  446. #define INIT_JMP_CALLBACK 1
  447. #define INIT_JMP_HALT 2
  448. #define INIT_JMP_REBOOT 3
  449. void switch_threads(jmp_buf *me, jmp_buf *you)
  450. {
  451. if (UML_SETJMP(me) == 0)
  452. UML_LONGJMP(you, 1);
  453. }
  454. static jmp_buf initial_jmpbuf;
  455. /* XXX Make these percpu */
  456. static void (*cb_proc)(void *arg);
  457. static void *cb_arg;
  458. static jmp_buf *cb_back;
  459. int start_idle_thread(void *stack, jmp_buf *switch_buf)
  460. {
  461. int n;
  462. set_handler(SIGWINCH);
  463. /*
  464. * Can't use UML_SETJMP or UML_LONGJMP here because they save
  465. * and restore signals, with the possible side-effect of
  466. * trying to handle any signals which came when they were
  467. * blocked, which can't be done on this stack.
  468. * Signals must be blocked when jumping back here and restored
  469. * after returning to the jumper.
  470. */
  471. n = setjmp(initial_jmpbuf);
  472. switch (n) {
  473. case INIT_JMP_NEW_THREAD:
  474. (*switch_buf)[0].JB_IP = (unsigned long) uml_finishsetup;
  475. (*switch_buf)[0].JB_SP = (unsigned long) stack +
  476. UM_THREAD_SIZE - sizeof(void *);
  477. break;
  478. case INIT_JMP_CALLBACK:
  479. (*cb_proc)(cb_arg);
  480. longjmp(*cb_back, 1);
  481. break;
  482. case INIT_JMP_HALT:
  483. kmalloc_ok = 0;
  484. return 0;
  485. case INIT_JMP_REBOOT:
  486. kmalloc_ok = 0;
  487. return 1;
  488. default:
  489. printk(UM_KERN_ERR "Bad sigsetjmp return in "
  490. "start_idle_thread - %d\n", n);
  491. fatal_sigsegv();
  492. }
  493. longjmp(*switch_buf, 1);
  494. /* unreachable */
  495. printk(UM_KERN_ERR "impossible long jump!");
  496. fatal_sigsegv();
  497. return 0;
  498. }
  499. void initial_thread_cb_skas(void (*proc)(void *), void *arg)
  500. {
  501. jmp_buf here;
  502. cb_proc = proc;
  503. cb_arg = arg;
  504. cb_back = &here;
  505. block_signals();
  506. if (UML_SETJMP(&here) == 0)
  507. UML_LONGJMP(&initial_jmpbuf, INIT_JMP_CALLBACK);
  508. unblock_signals();
  509. cb_proc = NULL;
  510. cb_arg = NULL;
  511. cb_back = NULL;
  512. }
  513. void halt_skas(void)
  514. {
  515. block_signals();
  516. UML_LONGJMP(&initial_jmpbuf, INIT_JMP_HALT);
  517. }
  518. void reboot_skas(void)
  519. {
  520. block_signals();
  521. UML_LONGJMP(&initial_jmpbuf, INIT_JMP_REBOOT);
  522. }
  523. void __switch_mm(struct mm_id *mm_idp)
  524. {
  525. userspace_pid[0] = mm_idp->u.pid;
  526. }