process.c 16 KB

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