process.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2015 Thomas Meyer ([email protected])
  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. int is_skas_winch(int pid, int fd, void *data)
  26. {
  27. return pid == getpgrp();
  28. }
  29. static const char *ptrace_reg_name(int idx)
  30. {
  31. #define R(n) case HOST_##n: return #n
  32. switch (idx) {
  33. #ifdef __x86_64__
  34. R(BX);
  35. R(CX);
  36. R(DI);
  37. R(SI);
  38. R(DX);
  39. R(BP);
  40. R(AX);
  41. R(R8);
  42. R(R9);
  43. R(R10);
  44. R(R11);
  45. R(R12);
  46. R(R13);
  47. R(R14);
  48. R(R15);
  49. R(ORIG_AX);
  50. R(CS);
  51. R(SS);
  52. R(EFLAGS);
  53. #elif defined(__i386__)
  54. R(IP);
  55. R(SP);
  56. R(EFLAGS);
  57. R(AX);
  58. R(BX);
  59. R(CX);
  60. R(DX);
  61. R(SI);
  62. R(DI);
  63. R(BP);
  64. R(CS);
  65. R(SS);
  66. R(DS);
  67. R(FS);
  68. R(ES);
  69. R(GS);
  70. R(ORIG_AX);
  71. #endif
  72. }
  73. return "";
  74. }
  75. static int ptrace_dump_regs(int pid)
  76. {
  77. unsigned long regs[MAX_REG_NR];
  78. int i;
  79. if (ptrace(PTRACE_GETREGS, pid, 0, regs) < 0)
  80. return -errno;
  81. printk(UM_KERN_ERR "Stub registers -\n");
  82. for (i = 0; i < ARRAY_SIZE(regs); i++) {
  83. const char *regname = ptrace_reg_name(i);
  84. printk(UM_KERN_ERR "\t%s\t(%2d): %lx\n", regname, i, regs[i]);
  85. }
  86. return 0;
  87. }
  88. /*
  89. * Signals that are OK to receive in the stub - we'll just continue it.
  90. * SIGWINCH will happen when UML is inside a detached screen.
  91. */
  92. #define STUB_SIG_MASK ((1 << SIGALRM) | (1 << SIGWINCH))
  93. /* Signals that the stub will finish with - anything else is an error */
  94. #define STUB_DONE_MASK (1 << SIGTRAP)
  95. void wait_stub_done(int pid)
  96. {
  97. int n, status, err;
  98. while (1) {
  99. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED | __WALL));
  100. if ((n < 0) || !WIFSTOPPED(status))
  101. goto bad_wait;
  102. if (((1 << WSTOPSIG(status)) & STUB_SIG_MASK) == 0)
  103. break;
  104. err = ptrace(PTRACE_CONT, pid, 0, 0);
  105. if (err) {
  106. printk(UM_KERN_ERR "wait_stub_done : continue failed, "
  107. "errno = %d\n", errno);
  108. fatal_sigsegv();
  109. }
  110. }
  111. if (((1 << WSTOPSIG(status)) & STUB_DONE_MASK) != 0)
  112. return;
  113. bad_wait:
  114. err = ptrace_dump_regs(pid);
  115. if (err)
  116. printk(UM_KERN_ERR "Failed to get registers from stub, "
  117. "errno = %d\n", -err);
  118. printk(UM_KERN_ERR "wait_stub_done : failed to wait for SIGTRAP, "
  119. "pid = %d, n = %d, errno = %d, status = 0x%x\n", pid, n, errno,
  120. status);
  121. fatal_sigsegv();
  122. }
  123. extern unsigned long current_stub_stack(void);
  124. static void get_skas_faultinfo(int pid, struct faultinfo *fi, unsigned long *aux_fp_regs)
  125. {
  126. int err;
  127. err = get_fp_registers(pid, aux_fp_regs);
  128. if (err < 0) {
  129. printk(UM_KERN_ERR "save_fp_registers returned %d\n",
  130. err);
  131. fatal_sigsegv();
  132. }
  133. err = ptrace(PTRACE_CONT, pid, 0, SIGSEGV);
  134. if (err) {
  135. printk(UM_KERN_ERR "Failed to continue stub, pid = %d, "
  136. "errno = %d\n", pid, errno);
  137. fatal_sigsegv();
  138. }
  139. wait_stub_done(pid);
  140. /*
  141. * faultinfo is prepared by the stub_segv_handler at start of
  142. * the stub stack page. We just have to copy it.
  143. */
  144. memcpy(fi, (void *)current_stub_stack(), sizeof(*fi));
  145. err = put_fp_registers(pid, aux_fp_regs);
  146. if (err < 0) {
  147. printk(UM_KERN_ERR "put_fp_registers returned %d\n",
  148. err);
  149. fatal_sigsegv();
  150. }
  151. }
  152. static void handle_segv(int pid, struct uml_pt_regs *regs, unsigned long *aux_fp_regs)
  153. {
  154. get_skas_faultinfo(pid, &regs->faultinfo, aux_fp_regs);
  155. segv(regs->faultinfo, 0, 1, NULL);
  156. }
  157. /*
  158. * To use the same value of using_sysemu as the caller, ask it that value
  159. * (in local_using_sysemu
  160. */
  161. static void handle_trap(int pid, struct uml_pt_regs *regs,
  162. int local_using_sysemu)
  163. {
  164. int err, status;
  165. if ((UPT_IP(regs) >= STUB_START) && (UPT_IP(regs) < STUB_END))
  166. fatal_sigsegv();
  167. if (!local_using_sysemu)
  168. {
  169. err = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_NR_OFFSET,
  170. __NR_getpid);
  171. if (err < 0) {
  172. printk(UM_KERN_ERR "handle_trap - nullifying syscall "
  173. "failed, errno = %d\n", errno);
  174. fatal_sigsegv();
  175. }
  176. err = ptrace(PTRACE_SYSCALL, pid, 0, 0);
  177. if (err < 0) {
  178. printk(UM_KERN_ERR "handle_trap - continuing to end of "
  179. "syscall failed, errno = %d\n", errno);
  180. fatal_sigsegv();
  181. }
  182. CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED | __WALL));
  183. if ((err < 0) || !WIFSTOPPED(status) ||
  184. (WSTOPSIG(status) != SIGTRAP + 0x80)) {
  185. err = ptrace_dump_regs(pid);
  186. if (err)
  187. printk(UM_KERN_ERR "Failed to get registers "
  188. "from process, errno = %d\n", -err);
  189. printk(UM_KERN_ERR "handle_trap - failed to wait at "
  190. "end of syscall, errno = %d, status = %d\n",
  191. errno, status);
  192. fatal_sigsegv();
  193. }
  194. }
  195. handle_syscall(regs);
  196. }
  197. extern char __syscall_stub_start[];
  198. /**
  199. * userspace_tramp() - userspace trampoline
  200. * @stack: pointer to the new userspace stack page, can be NULL, if? FIXME:
  201. *
  202. * The userspace trampoline is used to setup a new userspace process in start_userspace() after it was clone()'ed.
  203. * This function will run on a temporary stack page.
  204. * It ptrace()'es itself, then
  205. * Two pages are mapped into the userspace address space:
  206. * - STUB_CODE (with EXEC), which contains the skas stub code
  207. * - 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.
  208. * Also for the userspace process a SIGSEGV handler is installed to catch pagefaults in the userspace process.
  209. * And last the process stops itself to give control to the UML kernel for this userspace process.
  210. *
  211. * Return: Always zero, otherwise the current userspace process is ended with non null exit() call
  212. */
  213. static int userspace_tramp(void *stack)
  214. {
  215. void *addr;
  216. int fd;
  217. unsigned long long offset;
  218. ptrace(PTRACE_TRACEME, 0, 0, 0);
  219. signal(SIGTERM, SIG_DFL);
  220. signal(SIGWINCH, SIG_IGN);
  221. fd = phys_mapping(uml_to_phys(__syscall_stub_start), &offset);
  222. addr = mmap64((void *) STUB_CODE, UM_KERN_PAGE_SIZE,
  223. PROT_EXEC, MAP_FIXED | MAP_PRIVATE, fd, offset);
  224. if (addr == MAP_FAILED) {
  225. printk(UM_KERN_ERR "mapping mmap stub at 0x%lx failed, "
  226. "errno = %d\n", STUB_CODE, errno);
  227. exit(1);
  228. }
  229. if (stack != NULL) {
  230. fd = phys_mapping(uml_to_phys(stack), &offset);
  231. addr = mmap((void *) STUB_DATA,
  232. UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE,
  233. MAP_FIXED | MAP_SHARED, fd, offset);
  234. if (addr == MAP_FAILED) {
  235. printk(UM_KERN_ERR "mapping segfault stack "
  236. "at 0x%lx failed, errno = %d\n",
  237. STUB_DATA, errno);
  238. exit(1);
  239. }
  240. }
  241. if (stack != NULL) {
  242. struct sigaction sa;
  243. unsigned long v = STUB_CODE +
  244. (unsigned long) stub_segv_handler -
  245. (unsigned long) __syscall_stub_start;
  246. set_sigstack((void *) STUB_DATA, UM_KERN_PAGE_SIZE);
  247. sigemptyset(&sa.sa_mask);
  248. sa.sa_flags = SA_ONSTACK | SA_NODEFER | SA_SIGINFO;
  249. sa.sa_sigaction = (void *) v;
  250. sa.sa_restorer = NULL;
  251. if (sigaction(SIGSEGV, &sa, NULL) < 0) {
  252. printk(UM_KERN_ERR "userspace_tramp - setting SIGSEGV "
  253. "handler failed - errno = %d\n", errno);
  254. exit(1);
  255. }
  256. }
  257. kill(os_getpid(), SIGSTOP);
  258. return 0;
  259. }
  260. int userspace_pid[NR_CPUS];
  261. int kill_userspace_mm[NR_CPUS];
  262. /**
  263. * start_userspace() - prepare a new userspace process
  264. * @stub_stack: pointer to the stub stack. Can be NULL, if? FIXME:
  265. *
  266. * Setups a new temporary stack page that is used while userspace_tramp() runs
  267. * Clones the kernel process into a new userspace process, with FDs only.
  268. *
  269. * Return: When positive: the process id of the new userspace process,
  270. * when negative: an error number.
  271. * FIXME: can PIDs become negative?!
  272. */
  273. int start_userspace(unsigned long stub_stack)
  274. {
  275. void *stack;
  276. unsigned long sp;
  277. int pid, status, n, flags, err;
  278. /* setup a temporary stack page */
  279. stack = mmap(NULL, UM_KERN_PAGE_SIZE,
  280. PROT_READ | PROT_WRITE | PROT_EXEC,
  281. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  282. if (stack == MAP_FAILED) {
  283. err = -errno;
  284. printk(UM_KERN_ERR "start_userspace : mmap failed, "
  285. "errno = %d\n", errno);
  286. return err;
  287. }
  288. /* set stack pointer to the end of the stack page, so it can grow downwards */
  289. sp = (unsigned long)stack + UM_KERN_PAGE_SIZE;
  290. flags = CLONE_FILES | SIGCHLD;
  291. /* clone into new userspace process */
  292. pid = clone(userspace_tramp, (void *) sp, flags, (void *) stub_stack);
  293. if (pid < 0) {
  294. err = -errno;
  295. printk(UM_KERN_ERR "start_userspace : clone failed, "
  296. "errno = %d\n", errno);
  297. return err;
  298. }
  299. do {
  300. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED | __WALL));
  301. if (n < 0) {
  302. err = -errno;
  303. printk(UM_KERN_ERR "start_userspace : wait failed, "
  304. "errno = %d\n", errno);
  305. goto out_kill;
  306. }
  307. } while (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGALRM));
  308. if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP)) {
  309. err = -EINVAL;
  310. printk(UM_KERN_ERR "start_userspace : expected SIGSTOP, got "
  311. "status = %d\n", status);
  312. goto out_kill;
  313. }
  314. if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL,
  315. (void *) PTRACE_O_TRACESYSGOOD) < 0) {
  316. err = -errno;
  317. printk(UM_KERN_ERR "start_userspace : PTRACE_OLDSETOPTIONS "
  318. "failed, errno = %d\n", errno);
  319. goto out_kill;
  320. }
  321. if (munmap(stack, UM_KERN_PAGE_SIZE) < 0) {
  322. err = -errno;
  323. printk(UM_KERN_ERR "start_userspace : munmap failed, "
  324. "errno = %d\n", errno);
  325. goto out_kill;
  326. }
  327. return pid;
  328. out_kill:
  329. os_kill_ptraced_process(pid, 1);
  330. return err;
  331. }
  332. void userspace(struct uml_pt_regs *regs, unsigned long *aux_fp_regs)
  333. {
  334. int err, status, op, pid = userspace_pid[0];
  335. /* To prevent races if using_sysemu changes under us.*/
  336. int local_using_sysemu;
  337. siginfo_t si;
  338. /* Handle any immediate reschedules or signals */
  339. interrupt_end();
  340. while (1) {
  341. if (kill_userspace_mm[0])
  342. fatal_sigsegv();
  343. /*
  344. * This can legitimately fail if the process loads a
  345. * bogus value into a segment register. It will
  346. * segfault and PTRACE_GETREGS will read that value
  347. * out of the process. However, PTRACE_SETREGS will
  348. * fail. In this case, there is nothing to do but
  349. * just kill the process.
  350. */
  351. if (ptrace(PTRACE_SETREGS, pid, 0, regs->gp)) {
  352. printk(UM_KERN_ERR "userspace - ptrace set regs "
  353. "failed, errno = %d\n", errno);
  354. fatal_sigsegv();
  355. }
  356. if (put_fp_registers(pid, regs->fp)) {
  357. printk(UM_KERN_ERR "userspace - ptrace set fp regs "
  358. "failed, errno = %d\n", errno);
  359. fatal_sigsegv();
  360. }
  361. /* Now we set local_using_sysemu to be used for one loop */
  362. local_using_sysemu = get_using_sysemu();
  363. op = SELECT_PTRACE_OPERATION(local_using_sysemu,
  364. singlestepping(NULL));
  365. if (ptrace(op, pid, 0, 0)) {
  366. printk(UM_KERN_ERR "userspace - ptrace continue "
  367. "failed, op = %d, errno = %d\n", op, errno);
  368. fatal_sigsegv();
  369. }
  370. CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED | __WALL));
  371. if (err < 0) {
  372. printk(UM_KERN_ERR "userspace - wait failed, "
  373. "errno = %d\n", errno);
  374. fatal_sigsegv();
  375. }
  376. regs->is_user = 1;
  377. if (ptrace(PTRACE_GETREGS, pid, 0, regs->gp)) {
  378. printk(UM_KERN_ERR "userspace - PTRACE_GETREGS failed, "
  379. "errno = %d\n", errno);
  380. fatal_sigsegv();
  381. }
  382. if (get_fp_registers(pid, regs->fp)) {
  383. printk(UM_KERN_ERR "userspace - get_fp_registers failed, "
  384. "errno = %d\n", errno);
  385. fatal_sigsegv();
  386. }
  387. UPT_SYSCALL_NR(regs) = -1; /* Assume: It's not a syscall */
  388. if (WIFSTOPPED(status)) {
  389. int sig = WSTOPSIG(status);
  390. /* These signal handlers need the si argument.
  391. * The SIGIO and SIGALARM handlers which constitute the
  392. * majority of invocations, do not use it.
  393. */
  394. switch (sig) {
  395. case SIGSEGV:
  396. case SIGTRAP:
  397. case SIGILL:
  398. case SIGBUS:
  399. case SIGFPE:
  400. case SIGWINCH:
  401. ptrace(PTRACE_GETSIGINFO, pid, 0, (struct siginfo *)&si);
  402. break;
  403. }
  404. switch (sig) {
  405. case SIGSEGV:
  406. if (PTRACE_FULL_FAULTINFO) {
  407. get_skas_faultinfo(pid,
  408. &regs->faultinfo, aux_fp_regs);
  409. (*sig_info[SIGSEGV])(SIGSEGV, (struct siginfo *)&si,
  410. regs);
  411. }
  412. else handle_segv(pid, regs, aux_fp_regs);
  413. break;
  414. case SIGTRAP + 0x80:
  415. handle_trap(pid, regs, local_using_sysemu);
  416. break;
  417. case SIGTRAP:
  418. relay_signal(SIGTRAP, (struct siginfo *)&si, regs);
  419. break;
  420. case SIGALRM:
  421. break;
  422. case SIGIO:
  423. case SIGILL:
  424. case SIGBUS:
  425. case SIGFPE:
  426. case SIGWINCH:
  427. block_signals_trace();
  428. (*sig_info[sig])(sig, (struct siginfo *)&si, regs);
  429. unblock_signals_trace();
  430. break;
  431. default:
  432. printk(UM_KERN_ERR "userspace - child stopped "
  433. "with signal %d\n", sig);
  434. fatal_sigsegv();
  435. }
  436. pid = userspace_pid[0];
  437. interrupt_end();
  438. /* Avoid -ERESTARTSYS handling in host */
  439. if (PT_SYSCALL_NR_OFFSET != PT_SYSCALL_RET_OFFSET)
  440. PT_SYSCALL_NR(regs->gp) = -1;
  441. }
  442. }
  443. }
  444. static unsigned long thread_regs[MAX_REG_NR];
  445. static unsigned long thread_fp_regs[FP_SIZE];
  446. static int __init init_thread_regs(void)
  447. {
  448. get_safe_registers(thread_regs, thread_fp_regs);
  449. /* Set parent's instruction pointer to start of clone-stub */
  450. thread_regs[REGS_IP_INDEX] = STUB_CODE +
  451. (unsigned long) stub_clone_handler -
  452. (unsigned long) __syscall_stub_start;
  453. thread_regs[REGS_SP_INDEX] = STUB_DATA + UM_KERN_PAGE_SIZE -
  454. sizeof(void *);
  455. #ifdef __SIGNAL_FRAMESIZE
  456. thread_regs[REGS_SP_INDEX] -= __SIGNAL_FRAMESIZE;
  457. #endif
  458. return 0;
  459. }
  460. __initcall(init_thread_regs);
  461. int copy_context_skas0(unsigned long new_stack, int pid)
  462. {
  463. int err;
  464. unsigned long current_stack = current_stub_stack();
  465. struct stub_data *data = (struct stub_data *) current_stack;
  466. struct stub_data *child_data = (struct stub_data *) new_stack;
  467. unsigned long long new_offset;
  468. int new_fd = phys_mapping(uml_to_phys((void *)new_stack), &new_offset);
  469. /*
  470. * prepare offset and fd of child's stack as argument for parent's
  471. * and child's mmap2 calls
  472. */
  473. *data = ((struct stub_data) {
  474. .offset = MMAP_OFFSET(new_offset),
  475. .fd = new_fd,
  476. .parent_err = -ESRCH,
  477. .child_err = 0,
  478. });
  479. *child_data = ((struct stub_data) {
  480. .child_err = -ESRCH,
  481. });
  482. err = ptrace_setregs(pid, thread_regs);
  483. if (err < 0) {
  484. err = -errno;
  485. printk(UM_KERN_ERR "copy_context_skas0 : PTRACE_SETREGS "
  486. "failed, pid = %d, errno = %d\n", pid, -err);
  487. return err;
  488. }
  489. err = put_fp_registers(pid, thread_fp_regs);
  490. if (err < 0) {
  491. printk(UM_KERN_ERR "copy_context_skas0 : put_fp_registers "
  492. "failed, pid = %d, err = %d\n", pid, err);
  493. return err;
  494. }
  495. /*
  496. * Wait, until parent has finished its work: read child's pid from
  497. * parent's stack, and check, if bad result.
  498. */
  499. err = ptrace(PTRACE_CONT, pid, 0, 0);
  500. if (err) {
  501. err = -errno;
  502. printk(UM_KERN_ERR "Failed to continue new process, pid = %d, "
  503. "errno = %d\n", pid, errno);
  504. return err;
  505. }
  506. wait_stub_done(pid);
  507. pid = data->parent_err;
  508. if (pid < 0) {
  509. printk(UM_KERN_ERR "copy_context_skas0 - stub-parent reports "
  510. "error %d\n", -pid);
  511. return pid;
  512. }
  513. /*
  514. * Wait, until child has finished too: read child's result from
  515. * child's stack and check it.
  516. */
  517. wait_stub_done(pid);
  518. if (child_data->child_err != STUB_DATA) {
  519. printk(UM_KERN_ERR "copy_context_skas0 - stub-child %d reports "
  520. "error %ld\n", pid, data->child_err);
  521. err = data->child_err;
  522. goto out_kill;
  523. }
  524. if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL,
  525. (void *)PTRACE_O_TRACESYSGOOD) < 0) {
  526. err = -errno;
  527. printk(UM_KERN_ERR "copy_context_skas0 : PTRACE_OLDSETOPTIONS "
  528. "failed, errno = %d\n", errno);
  529. goto out_kill;
  530. }
  531. return pid;
  532. out_kill:
  533. os_kill_ptraced_process(pid, 1);
  534. return err;
  535. }
  536. void new_thread(void *stack, jmp_buf *buf, void (*handler)(void))
  537. {
  538. (*buf)[0].JB_IP = (unsigned long) handler;
  539. (*buf)[0].JB_SP = (unsigned long) stack + UM_THREAD_SIZE -
  540. sizeof(void *);
  541. }
  542. #define INIT_JMP_NEW_THREAD 0
  543. #define INIT_JMP_CALLBACK 1
  544. #define INIT_JMP_HALT 2
  545. #define INIT_JMP_REBOOT 3
  546. void switch_threads(jmp_buf *me, jmp_buf *you)
  547. {
  548. if (UML_SETJMP(me) == 0)
  549. UML_LONGJMP(you, 1);
  550. }
  551. static jmp_buf initial_jmpbuf;
  552. /* XXX Make these percpu */
  553. static void (*cb_proc)(void *arg);
  554. static void *cb_arg;
  555. static jmp_buf *cb_back;
  556. int start_idle_thread(void *stack, jmp_buf *switch_buf)
  557. {
  558. int n;
  559. set_handler(SIGWINCH);
  560. /*
  561. * Can't use UML_SETJMP or UML_LONGJMP here because they save
  562. * and restore signals, with the possible side-effect of
  563. * trying to handle any signals which came when they were
  564. * blocked, which can't be done on this stack.
  565. * Signals must be blocked when jumping back here and restored
  566. * after returning to the jumper.
  567. */
  568. n = setjmp(initial_jmpbuf);
  569. switch (n) {
  570. case INIT_JMP_NEW_THREAD:
  571. (*switch_buf)[0].JB_IP = (unsigned long) uml_finishsetup;
  572. (*switch_buf)[0].JB_SP = (unsigned long) stack +
  573. UM_THREAD_SIZE - sizeof(void *);
  574. break;
  575. case INIT_JMP_CALLBACK:
  576. (*cb_proc)(cb_arg);
  577. longjmp(*cb_back, 1);
  578. break;
  579. case INIT_JMP_HALT:
  580. kmalloc_ok = 0;
  581. return 0;
  582. case INIT_JMP_REBOOT:
  583. kmalloc_ok = 0;
  584. return 1;
  585. default:
  586. printk(UM_KERN_ERR "Bad sigsetjmp return in "
  587. "start_idle_thread - %d\n", n);
  588. fatal_sigsegv();
  589. }
  590. longjmp(*switch_buf, 1);
  591. /* unreachable */
  592. printk(UM_KERN_ERR "impossible long jump!");
  593. fatal_sigsegv();
  594. return 0;
  595. }
  596. void initial_thread_cb_skas(void (*proc)(void *), void *arg)
  597. {
  598. jmp_buf here;
  599. cb_proc = proc;
  600. cb_arg = arg;
  601. cb_back = &here;
  602. block_signals_trace();
  603. if (UML_SETJMP(&here) == 0)
  604. UML_LONGJMP(&initial_jmpbuf, INIT_JMP_CALLBACK);
  605. unblock_signals_trace();
  606. cb_proc = NULL;
  607. cb_arg = NULL;
  608. cb_back = NULL;
  609. }
  610. void halt_skas(void)
  611. {
  612. block_signals_trace();
  613. UML_LONGJMP(&initial_jmpbuf, INIT_JMP_HALT);
  614. }
  615. static bool noreboot;
  616. static int __init noreboot_cmd_param(char *str, int *add)
  617. {
  618. noreboot = true;
  619. return 0;
  620. }
  621. __uml_setup("noreboot", noreboot_cmd_param,
  622. "noreboot\n"
  623. " Rather than rebooting, exit always, akin to QEMU's -no-reboot option.\n"
  624. " This is useful if you're using CONFIG_PANIC_TIMEOUT in order to catch\n"
  625. " crashes in CI\n");
  626. void reboot_skas(void)
  627. {
  628. block_signals_trace();
  629. UML_LONGJMP(&initial_jmpbuf, noreboot ? INIT_JMP_HALT : INIT_JMP_REBOOT);
  630. }
  631. void __switch_mm(struct mm_id *mm_idp)
  632. {
  633. userspace_pid[0] = mm_idp->u.pid;
  634. kill_userspace_mm[0] = mm_idp->kill;
  635. }