process.c 15 KB

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