process.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
  4. * Copyright (C) 2002- 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  5. */
  6. #include <stdlib.h>
  7. #include <stdbool.h>
  8. #include <unistd.h>
  9. #include <sched.h>
  10. #include <errno.h>
  11. #include <string.h>
  12. #include <sys/mman.h>
  13. #include <sys/wait.h>
  14. #include <asm/unistd.h>
  15. #include <as-layout.h>
  16. #include <init.h>
  17. #include <kern_util.h>
  18. #include <mem.h>
  19. #include <os.h>
  20. #include <ptrace_user.h>
  21. #include <registers.h>
  22. #include <skas.h>
  23. #include <sysdep/stub.h>
  24. #include <linux/threads.h>
  25. #include <timetravel.h>
  26. #include "../internal.h"
  27. int is_skas_winch(int pid, int fd, void *data)
  28. {
  29. return pid == getpgrp();
  30. }
  31. static const char *ptrace_reg_name(int idx)
  32. {
  33. #define R(n) case HOST_##n: return #n
  34. switch (idx) {
  35. #ifdef __x86_64__
  36. R(BX);
  37. R(CX);
  38. R(DI);
  39. R(SI);
  40. R(DX);
  41. R(BP);
  42. R(AX);
  43. R(R8);
  44. R(R9);
  45. R(R10);
  46. R(R11);
  47. R(R12);
  48. R(R13);
  49. R(R14);
  50. R(R15);
  51. R(ORIG_AX);
  52. R(CS);
  53. R(SS);
  54. R(EFLAGS);
  55. #elif defined(__i386__)
  56. R(IP);
  57. R(SP);
  58. R(EFLAGS);
  59. R(AX);
  60. R(BX);
  61. R(CX);
  62. R(DX);
  63. R(SI);
  64. R(DI);
  65. R(BP);
  66. R(CS);
  67. R(SS);
  68. R(DS);
  69. R(FS);
  70. R(ES);
  71. R(GS);
  72. R(ORIG_AX);
  73. #endif
  74. }
  75. return "";
  76. }
  77. static int ptrace_dump_regs(int pid)
  78. {
  79. unsigned long regs[MAX_REG_NR];
  80. int i;
  81. if (ptrace(PTRACE_GETREGS, pid, 0, regs) < 0)
  82. return -errno;
  83. printk(UM_KERN_ERR "Stub registers -\n");
  84. for (i = 0; i < ARRAY_SIZE(regs); i++) {
  85. const char *regname = ptrace_reg_name(i);
  86. printk(UM_KERN_ERR "\t%s\t(%2d): %lx\n", regname, i, regs[i]);
  87. }
  88. return 0;
  89. }
  90. /*
  91. * Signals that are OK to receive in the stub - we'll just continue it.
  92. * SIGWINCH will happen when UML is inside a detached screen.
  93. */
  94. #define STUB_SIG_MASK ((1 << SIGALRM) | (1 << SIGWINCH))
  95. /* Signals that the stub will finish with - anything else is an error */
  96. #define STUB_DONE_MASK (1 << SIGTRAP)
  97. void wait_stub_done(int pid)
  98. {
  99. int n, status, err;
  100. while (1) {
  101. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED | __WALL));
  102. if ((n < 0) || !WIFSTOPPED(status))
  103. goto bad_wait;
  104. if (((1 << WSTOPSIG(status)) & STUB_SIG_MASK) == 0)
  105. break;
  106. err = ptrace(PTRACE_CONT, pid, 0, 0);
  107. if (err) {
  108. printk(UM_KERN_ERR "%s : continue failed, errno = %d\n",
  109. __func__, errno);
  110. fatal_sigsegv();
  111. }
  112. }
  113. if (((1 << WSTOPSIG(status)) & STUB_DONE_MASK) != 0)
  114. return;
  115. bad_wait:
  116. err = ptrace_dump_regs(pid);
  117. if (err)
  118. printk(UM_KERN_ERR "Failed to get registers from stub, errno = %d\n",
  119. -err);
  120. printk(UM_KERN_ERR "%s : failed to wait for SIGTRAP, pid = %d, n = %d, errno = %d, status = 0x%x\n",
  121. __func__, pid, n, errno, status);
  122. fatal_sigsegv();
  123. }
  124. extern unsigned long current_stub_stack(void);
  125. static void get_skas_faultinfo(int pid, struct faultinfo *fi, unsigned long *aux_fp_regs)
  126. {
  127. int err;
  128. err = get_fp_registers(pid, aux_fp_regs);
  129. if (err < 0) {
  130. printk(UM_KERN_ERR "save_fp_registers returned %d\n",
  131. err);
  132. fatal_sigsegv();
  133. }
  134. err = ptrace(PTRACE_CONT, pid, 0, SIGSEGV);
  135. if (err) {
  136. printk(UM_KERN_ERR "Failed to continue stub, pid = %d, "
  137. "errno = %d\n", pid, errno);
  138. fatal_sigsegv();
  139. }
  140. wait_stub_done(pid);
  141. /*
  142. * faultinfo is prepared by the stub_segv_handler at start of
  143. * the stub stack page. We just have to copy it.
  144. */
  145. memcpy(fi, (void *)current_stub_stack(), sizeof(*fi));
  146. err = put_fp_registers(pid, aux_fp_regs);
  147. if (err < 0) {
  148. printk(UM_KERN_ERR "put_fp_registers returned %d\n",
  149. err);
  150. fatal_sigsegv();
  151. }
  152. }
  153. static void handle_segv(int pid, struct uml_pt_regs *regs, unsigned long *aux_fp_regs)
  154. {
  155. get_skas_faultinfo(pid, &regs->faultinfo, aux_fp_regs);
  156. segv(regs->faultinfo, 0, 1, NULL);
  157. }
  158. static void handle_trap(int pid, struct uml_pt_regs *regs)
  159. {
  160. if ((UPT_IP(regs) >= STUB_START) && (UPT_IP(regs) < STUB_END))
  161. fatal_sigsegv();
  162. handle_syscall(regs);
  163. }
  164. extern char __syscall_stub_start[];
  165. /**
  166. * userspace_tramp() - userspace trampoline
  167. * @stack: pointer to the new userspace stack page
  168. *
  169. * The userspace trampoline is used to setup a new userspace process in start_userspace() after it was clone()'ed.
  170. * This function will run on a temporary stack page.
  171. * It ptrace()'es itself, then
  172. * Two pages are mapped into the userspace address space:
  173. * - STUB_CODE (with EXEC), which contains the skas stub code
  174. * - STUB_DATA (with R/W), which contains a data page that is used to transfer certain data between the UML userspace process and the UML kernel.
  175. * Also for the userspace process a SIGSEGV handler is installed to catch pagefaults in the userspace process.
  176. * And last the process stops itself to give control to the UML kernel for this userspace process.
  177. *
  178. * Return: Always zero, otherwise the current userspace process is ended with non null exit() call
  179. */
  180. static int userspace_tramp(void *stack)
  181. {
  182. struct sigaction sa;
  183. void *addr;
  184. int fd;
  185. unsigned long long offset;
  186. unsigned long segv_handler = STUB_CODE +
  187. (unsigned long) stub_segv_handler -
  188. (unsigned long) __syscall_stub_start;
  189. ptrace(PTRACE_TRACEME, 0, 0, 0);
  190. signal(SIGTERM, SIG_DFL);
  191. signal(SIGWINCH, SIG_IGN);
  192. fd = phys_mapping(uml_to_phys(__syscall_stub_start), &offset);
  193. addr = mmap64((void *) STUB_CODE, UM_KERN_PAGE_SIZE,
  194. PROT_EXEC, MAP_FIXED | MAP_PRIVATE, fd, offset);
  195. if (addr == MAP_FAILED) {
  196. os_info("mapping mmap stub at 0x%lx failed, errno = %d\n",
  197. STUB_CODE, errno);
  198. exit(1);
  199. }
  200. fd = phys_mapping(uml_to_phys(stack), &offset);
  201. addr = mmap((void *) STUB_DATA,
  202. STUB_DATA_PAGES * UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE,
  203. MAP_FIXED | MAP_SHARED, fd, offset);
  204. if (addr == MAP_FAILED) {
  205. os_info("mapping segfault stack at 0x%lx failed, errno = %d\n",
  206. STUB_DATA, errno);
  207. exit(1);
  208. }
  209. set_sigstack((void *) STUB_DATA, STUB_DATA_PAGES * UM_KERN_PAGE_SIZE);
  210. sigemptyset(&sa.sa_mask);
  211. sa.sa_flags = SA_ONSTACK | SA_NODEFER | SA_SIGINFO;
  212. sa.sa_sigaction = (void *) segv_handler;
  213. sa.sa_restorer = NULL;
  214. if (sigaction(SIGSEGV, &sa, NULL) < 0) {
  215. os_info("%s - setting SIGSEGV handler failed - errno = %d\n",
  216. __func__, errno);
  217. exit(1);
  218. }
  219. kill(os_getpid(), SIGSTOP);
  220. return 0;
  221. }
  222. int userspace_pid[NR_CPUS];
  223. /**
  224. * start_userspace() - prepare a new userspace process
  225. * @stub_stack: pointer to the stub stack.
  226. *
  227. * Setups a new temporary stack page that is used while userspace_tramp() runs
  228. * Clones the kernel process into a new userspace process, with FDs only.
  229. *
  230. * Return: When positive: the process id of the new userspace process,
  231. * when negative: an error number.
  232. * FIXME: can PIDs become negative?!
  233. */
  234. int start_userspace(unsigned long stub_stack)
  235. {
  236. void *stack;
  237. unsigned long sp;
  238. int pid, status, n, flags, err;
  239. /* setup a temporary stack page */
  240. stack = mmap(NULL, UM_KERN_PAGE_SIZE,
  241. PROT_READ | PROT_WRITE | PROT_EXEC,
  242. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  243. if (stack == MAP_FAILED) {
  244. err = -errno;
  245. printk(UM_KERN_ERR "%s : mmap failed, errno = %d\n",
  246. __func__, errno);
  247. return err;
  248. }
  249. /* set stack pointer to the end of the stack page, so it can grow downwards */
  250. sp = (unsigned long)stack + UM_KERN_PAGE_SIZE;
  251. flags = CLONE_FILES | SIGCHLD;
  252. /* clone into new userspace process */
  253. pid = clone(userspace_tramp, (void *) sp, flags, (void *) stub_stack);
  254. if (pid < 0) {
  255. err = -errno;
  256. printk(UM_KERN_ERR "%s : clone failed, errno = %d\n",
  257. __func__, errno);
  258. return err;
  259. }
  260. do {
  261. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED | __WALL));
  262. if (n < 0) {
  263. err = -errno;
  264. printk(UM_KERN_ERR "%s : wait failed, errno = %d\n",
  265. __func__, errno);
  266. goto out_kill;
  267. }
  268. } while (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGALRM));
  269. if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP)) {
  270. err = -EINVAL;
  271. printk(UM_KERN_ERR "%s : expected SIGSTOP, got status = %d\n",
  272. __func__, status);
  273. goto out_kill;
  274. }
  275. if (ptrace(PTRACE_SETOPTIONS, pid, NULL,
  276. (void *) PTRACE_O_TRACESYSGOOD) < 0) {
  277. err = -errno;
  278. printk(UM_KERN_ERR "%s : PTRACE_SETOPTIONS failed, errno = %d\n",
  279. __func__, errno);
  280. goto out_kill;
  281. }
  282. if (munmap(stack, UM_KERN_PAGE_SIZE) < 0) {
  283. err = -errno;
  284. printk(UM_KERN_ERR "%s : munmap failed, errno = %d\n",
  285. __func__, errno);
  286. goto out_kill;
  287. }
  288. return pid;
  289. out_kill:
  290. os_kill_ptraced_process(pid, 1);
  291. return err;
  292. }
  293. void userspace(struct uml_pt_regs *regs, unsigned long *aux_fp_regs)
  294. {
  295. int err, status, op, pid = userspace_pid[0];
  296. siginfo_t si;
  297. /* Handle any immediate reschedules or signals */
  298. interrupt_end();
  299. while (1) {
  300. time_travel_print_bc_msg();
  301. current_mm_sync();
  302. /* Flush out any pending syscalls */
  303. err = syscall_stub_flush(current_mm_id());
  304. if (err) {
  305. if (err == -ENOMEM)
  306. report_enomem();
  307. printk(UM_KERN_ERR "%s - Error flushing stub syscalls: %d",
  308. __func__, -err);
  309. fatal_sigsegv();
  310. }
  311. /*
  312. * This can legitimately fail if the process loads a
  313. * bogus value into a segment register. It will
  314. * segfault and PTRACE_GETREGS will read that value
  315. * out of the process. However, PTRACE_SETREGS will
  316. * fail. In this case, there is nothing to do but
  317. * just kill the process.
  318. */
  319. if (ptrace(PTRACE_SETREGS, pid, 0, regs->gp)) {
  320. printk(UM_KERN_ERR "%s - ptrace set regs failed, errno = %d\n",
  321. __func__, errno);
  322. fatal_sigsegv();
  323. }
  324. if (put_fp_registers(pid, regs->fp)) {
  325. printk(UM_KERN_ERR "%s - ptrace set fp regs failed, errno = %d\n",
  326. __func__, errno);
  327. fatal_sigsegv();
  328. }
  329. if (singlestepping())
  330. op = PTRACE_SYSEMU_SINGLESTEP;
  331. else
  332. op = PTRACE_SYSEMU;
  333. if (ptrace(op, pid, 0, 0)) {
  334. printk(UM_KERN_ERR "%s - ptrace continue failed, op = %d, errno = %d\n",
  335. __func__, op, errno);
  336. fatal_sigsegv();
  337. }
  338. CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED | __WALL));
  339. if (err < 0) {
  340. printk(UM_KERN_ERR "%s - wait failed, errno = %d\n",
  341. __func__, errno);
  342. fatal_sigsegv();
  343. }
  344. regs->is_user = 1;
  345. if (ptrace(PTRACE_GETREGS, pid, 0, regs->gp)) {
  346. printk(UM_KERN_ERR "%s - PTRACE_GETREGS failed, errno = %d\n",
  347. __func__, errno);
  348. fatal_sigsegv();
  349. }
  350. if (get_fp_registers(pid, regs->fp)) {
  351. printk(UM_KERN_ERR "%s - get_fp_registers failed, errno = %d\n",
  352. __func__, errno);
  353. fatal_sigsegv();
  354. }
  355. UPT_SYSCALL_NR(regs) = -1; /* Assume: It's not a syscall */
  356. if (WIFSTOPPED(status)) {
  357. int sig = WSTOPSIG(status);
  358. /* These signal handlers need the si argument.
  359. * The SIGIO and SIGALARM handlers which constitute the
  360. * majority of invocations, do not use it.
  361. */
  362. switch (sig) {
  363. case SIGSEGV:
  364. case SIGTRAP:
  365. case SIGILL:
  366. case SIGBUS:
  367. case SIGFPE:
  368. case SIGWINCH:
  369. ptrace(PTRACE_GETSIGINFO, pid, 0, (struct siginfo *)&si);
  370. break;
  371. }
  372. switch (sig) {
  373. case SIGSEGV:
  374. if (PTRACE_FULL_FAULTINFO) {
  375. get_skas_faultinfo(pid,
  376. &regs->faultinfo, aux_fp_regs);
  377. (*sig_info[SIGSEGV])(SIGSEGV, (struct siginfo *)&si,
  378. regs);
  379. }
  380. else handle_segv(pid, regs, aux_fp_regs);
  381. break;
  382. case SIGTRAP + 0x80:
  383. handle_trap(pid, regs);
  384. break;
  385. case SIGTRAP:
  386. relay_signal(SIGTRAP, (struct siginfo *)&si, regs);
  387. break;
  388. case SIGALRM:
  389. break;
  390. case SIGIO:
  391. case SIGILL:
  392. case SIGBUS:
  393. case SIGFPE:
  394. case SIGWINCH:
  395. block_signals_trace();
  396. (*sig_info[sig])(sig, (struct siginfo *)&si, regs);
  397. unblock_signals_trace();
  398. break;
  399. default:
  400. printk(UM_KERN_ERR "%s - child stopped with signal %d\n",
  401. __func__, sig);
  402. fatal_sigsegv();
  403. }
  404. pid = userspace_pid[0];
  405. interrupt_end();
  406. /* Avoid -ERESTARTSYS handling in host */
  407. if (PT_SYSCALL_NR_OFFSET != PT_SYSCALL_RET_OFFSET)
  408. PT_SYSCALL_NR(regs->gp) = -1;
  409. }
  410. }
  411. }
  412. void new_thread(void *stack, jmp_buf *buf, void (*handler)(void))
  413. {
  414. (*buf)[0].JB_IP = (unsigned long) handler;
  415. (*buf)[0].JB_SP = (unsigned long) stack + UM_THREAD_SIZE -
  416. sizeof(void *);
  417. }
  418. #define INIT_JMP_NEW_THREAD 0
  419. #define INIT_JMP_CALLBACK 1
  420. #define INIT_JMP_HALT 2
  421. #define INIT_JMP_REBOOT 3
  422. void switch_threads(jmp_buf *me, jmp_buf *you)
  423. {
  424. if (UML_SETJMP(me) == 0)
  425. UML_LONGJMP(you, 1);
  426. }
  427. static jmp_buf initial_jmpbuf;
  428. /* XXX Make these percpu */
  429. static void (*cb_proc)(void *arg);
  430. static void *cb_arg;
  431. static jmp_buf *cb_back;
  432. int start_idle_thread(void *stack, jmp_buf *switch_buf)
  433. {
  434. int n;
  435. set_handler(SIGWINCH);
  436. /*
  437. * Can't use UML_SETJMP or UML_LONGJMP here because they save
  438. * and restore signals, with the possible side-effect of
  439. * trying to handle any signals which came when they were
  440. * blocked, which can't be done on this stack.
  441. * Signals must be blocked when jumping back here and restored
  442. * after returning to the jumper.
  443. */
  444. n = setjmp(initial_jmpbuf);
  445. switch (n) {
  446. case INIT_JMP_NEW_THREAD:
  447. (*switch_buf)[0].JB_IP = (unsigned long) uml_finishsetup;
  448. (*switch_buf)[0].JB_SP = (unsigned long) stack +
  449. UM_THREAD_SIZE - sizeof(void *);
  450. break;
  451. case INIT_JMP_CALLBACK:
  452. (*cb_proc)(cb_arg);
  453. longjmp(*cb_back, 1);
  454. break;
  455. case INIT_JMP_HALT:
  456. kmalloc_ok = 0;
  457. return 0;
  458. case INIT_JMP_REBOOT:
  459. kmalloc_ok = 0;
  460. return 1;
  461. default:
  462. printk(UM_KERN_ERR "Bad sigsetjmp return in %s - %d\n",
  463. __func__, n);
  464. fatal_sigsegv();
  465. }
  466. longjmp(*switch_buf, 1);
  467. /* unreachable */
  468. printk(UM_KERN_ERR "impossible long jump!");
  469. fatal_sigsegv();
  470. return 0;
  471. }
  472. void initial_thread_cb_skas(void (*proc)(void *), void *arg)
  473. {
  474. jmp_buf here;
  475. cb_proc = proc;
  476. cb_arg = arg;
  477. cb_back = &here;
  478. block_signals_trace();
  479. if (UML_SETJMP(&here) == 0)
  480. UML_LONGJMP(&initial_jmpbuf, INIT_JMP_CALLBACK);
  481. unblock_signals_trace();
  482. cb_proc = NULL;
  483. cb_arg = NULL;
  484. cb_back = NULL;
  485. }
  486. void halt_skas(void)
  487. {
  488. block_signals_trace();
  489. UML_LONGJMP(&initial_jmpbuf, INIT_JMP_HALT);
  490. }
  491. static bool noreboot;
  492. static int __init noreboot_cmd_param(char *str, int *add)
  493. {
  494. noreboot = true;
  495. return 0;
  496. }
  497. __uml_setup("noreboot", noreboot_cmd_param,
  498. "noreboot\n"
  499. " Rather than rebooting, exit always, akin to QEMU's -no-reboot option.\n"
  500. " This is useful if you're using CONFIG_PANIC_TIMEOUT in order to catch\n"
  501. " crashes in CI\n");
  502. void reboot_skas(void)
  503. {
  504. block_signals_trace();
  505. UML_LONGJMP(&initial_jmpbuf, noreboot ? INIT_JMP_HALT : INIT_JMP_REBOOT);
  506. }
  507. void __switch_mm(struct mm_id *mm_idp)
  508. {
  509. userspace_pid[0] = mm_idp->pid;
  510. }