process.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8. #include <signal.h>
  9. #include <setjmp.h>
  10. #include <sched.h>
  11. #include <sys/wait.h>
  12. #include <sys/mman.h>
  13. #include <sys/user.h>
  14. #include <asm/unistd.h>
  15. #include "user.h"
  16. #include "ptrace_user.h"
  17. #include "time_user.h"
  18. #include "sysdep/ptrace.h"
  19. #include "user_util.h"
  20. #include "kern_util.h"
  21. #include "skas.h"
  22. #include "sysdep/sigcontext.h"
  23. #include "os.h"
  24. #include "proc_mm.h"
  25. #include "skas_ptrace.h"
  26. #include "chan_user.h"
  27. #include "signal_user.h"
  28. #include "registers.h"
  29. int is_skas_winch(int pid, int fd, void *data)
  30. {
  31. if(pid != os_getpid())
  32. return(0);
  33. register_winch_irq(-1, fd, -1, data);
  34. return(1);
  35. }
  36. static void handle_segv(int pid)
  37. {
  38. struct ptrace_faultinfo fault;
  39. int err;
  40. err = ptrace(PTRACE_FAULTINFO, pid, 0, &fault);
  41. if(err)
  42. panic("handle_segv - PTRACE_FAULTINFO failed, errno = %d\n",
  43. errno);
  44. segv(fault.addr, 0, FAULT_WRITE(fault.is_write), 1, NULL);
  45. }
  46. /*To use the same value of using_sysemu as the caller, ask it that value (in local_using_sysemu)*/
  47. static void handle_trap(int pid, union uml_pt_regs *regs, int local_using_sysemu)
  48. {
  49. int err, status;
  50. /* Mark this as a syscall */
  51. UPT_SYSCALL_NR(regs) = PT_SYSCALL_NR(regs->skas.regs);
  52. if (!local_using_sysemu)
  53. {
  54. err = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET, __NR_getpid);
  55. if(err < 0)
  56. panic("handle_trap - nullifying syscall failed errno = %d\n",
  57. errno);
  58. err = ptrace(PTRACE_SYSCALL, pid, 0, 0);
  59. if(err < 0)
  60. panic("handle_trap - continuing to end of syscall failed, "
  61. "errno = %d\n", errno);
  62. CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED));
  63. if((err < 0) || !WIFSTOPPED(status) ||
  64. (WSTOPSIG(status) != SIGTRAP + 0x80))
  65. panic("handle_trap - failed to wait at end of syscall, "
  66. "errno = %d, status = %d\n", errno, status);
  67. }
  68. handle_syscall(regs);
  69. }
  70. static int userspace_tramp(void *arg)
  71. {
  72. init_new_thread_signals(0);
  73. enable_timer();
  74. ptrace(PTRACE_TRACEME, 0, 0, 0);
  75. os_stop_process(os_getpid());
  76. return(0);
  77. }
  78. /* Each element set once, and only accessed by a single processor anyway */
  79. #undef NR_CPUS
  80. #define NR_CPUS 1
  81. int userspace_pid[NR_CPUS];
  82. void start_userspace(int cpu)
  83. {
  84. void *stack;
  85. unsigned long sp;
  86. int pid, status, n;
  87. stack = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
  88. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  89. if(stack == MAP_FAILED)
  90. panic("start_userspace : mmap failed, errno = %d", errno);
  91. sp = (unsigned long) stack + PAGE_SIZE - sizeof(void *);
  92. pid = clone(userspace_tramp, (void *) sp,
  93. CLONE_FILES | CLONE_VM | SIGCHLD, NULL);
  94. if(pid < 0)
  95. panic("start_userspace : clone failed, errno = %d", errno);
  96. do {
  97. CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
  98. if(n < 0)
  99. panic("start_userspace : wait failed, errno = %d",
  100. errno);
  101. } while(WIFSTOPPED(status) && (WSTOPSIG(status) == SIGVTALRM));
  102. if(!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
  103. panic("start_userspace : expected SIGSTOP, got status = %d",
  104. status);
  105. if (ptrace(PTRACE_OLDSETOPTIONS, pid, NULL, (void *)PTRACE_O_TRACESYSGOOD) < 0)
  106. panic("start_userspace : PTRACE_SETOPTIONS failed, errno=%d\n",
  107. errno);
  108. if(munmap(stack, PAGE_SIZE) < 0)
  109. panic("start_userspace : munmap failed, errno = %d\n", errno);
  110. userspace_pid[cpu] = pid;
  111. }
  112. void userspace(union uml_pt_regs *regs)
  113. {
  114. int err, status, op, pid = userspace_pid[0];
  115. int local_using_sysemu; /*To prevent races if using_sysemu changes under us.*/
  116. while(1){
  117. restore_registers(pid, regs);
  118. /* Now we set local_using_sysemu to be used for one loop */
  119. local_using_sysemu = get_using_sysemu();
  120. op = SELECT_PTRACE_OPERATION(local_using_sysemu, singlestepping(NULL));
  121. err = ptrace(op, pid, 0, 0);
  122. if(err)
  123. panic("userspace - could not resume userspace process, "
  124. "pid=%d, ptrace operation = %d, errno = %d\n",
  125. op, errno);
  126. CATCH_EINTR(err = waitpid(pid, &status, WUNTRACED));
  127. if(err < 0)
  128. panic("userspace - waitpid failed, errno = %d\n",
  129. errno);
  130. regs->skas.is_user = 1;
  131. save_registers(pid, regs);
  132. UPT_SYSCALL_NR(regs) = -1; /* Assume: It's not a syscall */
  133. if(WIFSTOPPED(status)){
  134. switch(WSTOPSIG(status)){
  135. case SIGSEGV:
  136. handle_segv(pid);
  137. break;
  138. case SIGTRAP + 0x80:
  139. handle_trap(pid, regs, local_using_sysemu);
  140. break;
  141. case SIGTRAP:
  142. relay_signal(SIGTRAP, regs);
  143. break;
  144. case SIGIO:
  145. case SIGVTALRM:
  146. case SIGILL:
  147. case SIGBUS:
  148. case SIGFPE:
  149. case SIGWINCH:
  150. user_signal(WSTOPSIG(status), regs);
  151. break;
  152. default:
  153. printk("userspace - child stopped with signal "
  154. "%d\n", WSTOPSIG(status));
  155. }
  156. interrupt_end();
  157. /* Avoid -ERESTARTSYS handling in host */
  158. PT_SYSCALL_NR(regs->skas.regs) = -1;
  159. }
  160. }
  161. }
  162. void new_thread(void *stack, void **switch_buf_ptr, void **fork_buf_ptr,
  163. void (*handler)(int))
  164. {
  165. unsigned long flags;
  166. sigjmp_buf switch_buf, fork_buf;
  167. *switch_buf_ptr = &switch_buf;
  168. *fork_buf_ptr = &fork_buf;
  169. /* Somewhat subtle - siglongjmp restores the signal mask before doing
  170. * the longjmp. This means that when jumping from one stack to another
  171. * when the target stack has interrupts enabled, an interrupt may occur
  172. * on the source stack. This is bad when starting up a process because
  173. * it's not supposed to get timer ticks until it has been scheduled.
  174. * So, we disable interrupts around the sigsetjmp to ensure that
  175. * they can't happen until we get back here where they are safe.
  176. */
  177. flags = get_signals();
  178. block_signals();
  179. if(sigsetjmp(fork_buf, 1) == 0)
  180. new_thread_proc(stack, handler);
  181. remove_sigstack();
  182. set_signals(flags);
  183. }
  184. void thread_wait(void *sw, void *fb)
  185. {
  186. sigjmp_buf buf, **switch_buf = sw, *fork_buf;
  187. *switch_buf = &buf;
  188. fork_buf = fb;
  189. if(sigsetjmp(buf, 1) == 0)
  190. siglongjmp(*fork_buf, 1);
  191. }
  192. void switch_threads(void *me, void *next)
  193. {
  194. sigjmp_buf my_buf, **me_ptr = me, *next_buf = next;
  195. *me_ptr = &my_buf;
  196. if(sigsetjmp(my_buf, 1) == 0)
  197. siglongjmp(*next_buf, 1);
  198. }
  199. static sigjmp_buf initial_jmpbuf;
  200. /* XXX Make these percpu */
  201. static void (*cb_proc)(void *arg);
  202. static void *cb_arg;
  203. static sigjmp_buf *cb_back;
  204. int start_idle_thread(void *stack, void *switch_buf_ptr, void **fork_buf_ptr)
  205. {
  206. sigjmp_buf **switch_buf = switch_buf_ptr;
  207. int n;
  208. *fork_buf_ptr = &initial_jmpbuf;
  209. n = sigsetjmp(initial_jmpbuf, 1);
  210. if(n == 0)
  211. new_thread_proc((void *) stack, new_thread_handler);
  212. else if(n == 1)
  213. remove_sigstack();
  214. else if(n == 2){
  215. (*cb_proc)(cb_arg);
  216. siglongjmp(*cb_back, 1);
  217. }
  218. else if(n == 3){
  219. kmalloc_ok = 0;
  220. return(0);
  221. }
  222. else if(n == 4){
  223. kmalloc_ok = 0;
  224. return(1);
  225. }
  226. siglongjmp(**switch_buf, 1);
  227. }
  228. void remove_sigstack(void)
  229. {
  230. stack_t stack = ((stack_t) { .ss_flags = SS_DISABLE,
  231. .ss_sp = NULL,
  232. .ss_size = 0 });
  233. if(sigaltstack(&stack, NULL) != 0)
  234. panic("disabling signal stack failed, errno = %d\n", errno);
  235. }
  236. void initial_thread_cb_skas(void (*proc)(void *), void *arg)
  237. {
  238. sigjmp_buf here;
  239. cb_proc = proc;
  240. cb_arg = arg;
  241. cb_back = &here;
  242. block_signals();
  243. if(sigsetjmp(here, 1) == 0)
  244. siglongjmp(initial_jmpbuf, 2);
  245. unblock_signals();
  246. cb_proc = NULL;
  247. cb_arg = NULL;
  248. cb_back = NULL;
  249. }
  250. void halt_skas(void)
  251. {
  252. block_signals();
  253. siglongjmp(initial_jmpbuf, 3);
  254. }
  255. void reboot_skas(void)
  256. {
  257. block_signals();
  258. siglongjmp(initial_jmpbuf, 4);
  259. }
  260. void switch_mm_skas(int mm_fd)
  261. {
  262. int err;
  263. #warning need cpu pid in switch_mm_skas
  264. err = ptrace(PTRACE_SWITCH_MM, userspace_pid[0], 0, mm_fd);
  265. if(err)
  266. panic("switch_mm_skas - PTRACE_SWITCH_MM failed, errno = %d\n",
  267. errno);
  268. }
  269. void kill_off_processes_skas(void)
  270. {
  271. #warning need to loop over userspace_pids in kill_off_processes_skas
  272. os_kill_ptraced_process(userspace_pid[0], 1);
  273. }
  274. /*
  275. * Overrides for Emacs so that we follow Linus's tabbing style.
  276. * Emacs will notice this stuff at the end of the file and automatically
  277. * adjust the settings for this buffer only. This must remain at the end
  278. * of the file.
  279. * ---------------------------------------------------------------------------
  280. * Local variables:
  281. * c-file-style: "linux"
  282. * End:
  283. */