process.c 16 KB

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