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