process_kern.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include "linux/sched.h"
  6. #include "linux/signal.h"
  7. #include "linux/kernel.h"
  8. #include "linux/interrupt.h"
  9. #include "linux/ptrace.h"
  10. #include "asm/system.h"
  11. #include "asm/pgalloc.h"
  12. #include "asm/ptrace.h"
  13. #include "asm/tlbflush.h"
  14. #include "irq_user.h"
  15. #include "signal_user.h"
  16. #include "kern_util.h"
  17. #include "user_util.h"
  18. #include "os.h"
  19. #include "kern.h"
  20. #include "sigcontext.h"
  21. #include "time_user.h"
  22. #include "mem_user.h"
  23. #include "tlb.h"
  24. #include "mode.h"
  25. #include "mode_kern.h"
  26. #include "init.h"
  27. #include "tt.h"
  28. void switch_to_tt(void *prev, void *next)
  29. {
  30. struct task_struct *from, *to, *prev_sched;
  31. unsigned long flags;
  32. int err, vtalrm, alrm, prof, cpu;
  33. char c;
  34. from = prev;
  35. to = next;
  36. cpu = from->thread_info->cpu;
  37. if(cpu == 0)
  38. forward_interrupts(to->thread.mode.tt.extern_pid);
  39. #ifdef CONFIG_SMP
  40. forward_ipi(cpu_data[cpu].ipi_pipe[0], to->thread.mode.tt.extern_pid);
  41. #endif
  42. local_irq_save(flags);
  43. vtalrm = change_sig(SIGVTALRM, 0);
  44. alrm = change_sig(SIGALRM, 0);
  45. prof = change_sig(SIGPROF, 0);
  46. forward_pending_sigio(to->thread.mode.tt.extern_pid);
  47. c = 0;
  48. err = os_write_file(to->thread.mode.tt.switch_pipe[1], &c, sizeof(c));
  49. if(err != sizeof(c))
  50. panic("write of switch_pipe failed, err = %d", -err);
  51. if(from->thread.mode.tt.switch_pipe[0] == -1)
  52. os_kill_process(os_getpid(), 0);
  53. err = os_read_file(from->thread.mode.tt.switch_pipe[0], &c, sizeof(c));
  54. if(err != sizeof(c))
  55. panic("read of switch_pipe failed, errno = %d", -err);
  56. /* If the process that we have just scheduled away from has exited,
  57. * then it needs to be killed here. The reason is that, even though
  58. * it will kill itself when it next runs, that may be too late. Its
  59. * stack will be freed, possibly before then, and if that happens,
  60. * we have a use-after-free situation. So, it gets killed here
  61. * in case it has not already killed itself.
  62. */
  63. prev_sched = current->thread.prev_sched;
  64. if(prev_sched->thread.mode.tt.switch_pipe[0] == -1)
  65. os_kill_process(prev_sched->thread.mode.tt.extern_pid, 1);
  66. change_sig(SIGVTALRM, vtalrm);
  67. change_sig(SIGALRM, alrm);
  68. change_sig(SIGPROF, prof);
  69. arch_switch();
  70. flush_tlb_all();
  71. local_irq_restore(flags);
  72. }
  73. void release_thread_tt(struct task_struct *task)
  74. {
  75. int pid = task->thread.mode.tt.extern_pid;
  76. /*
  77. * We first have to kill the other process, before
  78. * closing its switch_pipe. Else it might wake up
  79. * and receive "EOF" before we could kill it.
  80. */
  81. if(os_getpid() != pid)
  82. os_kill_process(pid, 0);
  83. os_close_file(task->thread.mode.tt.switch_pipe[0]);
  84. os_close_file(task->thread.mode.tt.switch_pipe[1]);
  85. /* use switch_pipe as flag: thread is released */
  86. task->thread.mode.tt.switch_pipe[0] = -1;
  87. }
  88. void suspend_new_thread(int fd)
  89. {
  90. int err;
  91. char c;
  92. os_stop_process(os_getpid());
  93. err = os_read_file(fd, &c, sizeof(c));
  94. if(err != sizeof(c))
  95. panic("read failed in suspend_new_thread, err = %d", -err);
  96. }
  97. void schedule_tail(task_t *prev);
  98. static void new_thread_handler(int sig)
  99. {
  100. unsigned long disable;
  101. int (*fn)(void *);
  102. void *arg;
  103. fn = current->thread.request.u.thread.proc;
  104. arg = current->thread.request.u.thread.arg;
  105. UPT_SC(&current->thread.regs.regs) = (void *) (&sig + 1);
  106. disable = (1 << (SIGVTALRM - 1)) | (1 << (SIGALRM - 1)) |
  107. (1 << (SIGIO - 1)) | (1 << (SIGPROF - 1));
  108. SC_SIGMASK(UPT_SC(&current->thread.regs.regs)) &= ~disable;
  109. suspend_new_thread(current->thread.mode.tt.switch_pipe[0]);
  110. force_flush_all();
  111. if(current->thread.prev_sched != NULL)
  112. schedule_tail(current->thread.prev_sched);
  113. current->thread.prev_sched = NULL;
  114. init_new_thread_signals(1);
  115. enable_timer();
  116. free_page(current->thread.temp_stack);
  117. set_cmdline("(kernel thread)");
  118. change_sig(SIGUSR1, 1);
  119. change_sig(SIGVTALRM, 1);
  120. change_sig(SIGPROF, 1);
  121. local_irq_enable();
  122. if(!run_kernel_thread(fn, arg, &current->thread.exec_buf))
  123. do_exit(0);
  124. /* XXX No set_user_mode here because a newly execed process will
  125. * immediately segfault on its non-existent IP, coming straight back
  126. * to the signal handler, which will call set_user_mode on its way
  127. * out. This should probably change since it's confusing.
  128. */
  129. }
  130. static int new_thread_proc(void *stack)
  131. {
  132. /* local_irq_disable is needed to block out signals until this thread is
  133. * properly scheduled. Otherwise, the tracing thread will get mighty
  134. * upset about any signals that arrive before that.
  135. * This has the complication that it sets the saved signal mask in
  136. * the sigcontext to block signals. This gets restored when this
  137. * thread (or a descendant, since they get a copy of this sigcontext)
  138. * returns to userspace.
  139. * So, this is compensated for elsewhere.
  140. * XXX There is still a small window until local_irq_disable() actually
  141. * finishes where signals are possible - shouldn't be a problem in
  142. * practice since SIGIO hasn't been forwarded here yet, and the
  143. * local_irq_disable should finish before a SIGVTALRM has time to be
  144. * delivered.
  145. */
  146. local_irq_disable();
  147. init_new_thread_stack(stack, new_thread_handler);
  148. os_usr1_process(os_getpid());
  149. change_sig(SIGUSR1, 1);
  150. return(0);
  151. }
  152. /* Signal masking - signals are blocked at the start of fork_tramp. They
  153. * are re-enabled when finish_fork_handler is entered by fork_tramp hitting
  154. * itself with a SIGUSR1. set_user_mode has to be run with SIGUSR1 off,
  155. * so it is blocked before it's called. They are re-enabled on sigreturn
  156. * despite the fact that they were blocked when the SIGUSR1 was issued because
  157. * copy_thread copies the parent's sigcontext, including the signal mask
  158. * onto the signal frame.
  159. */
  160. void finish_fork_handler(int sig)
  161. {
  162. UPT_SC(&current->thread.regs.regs) = (void *) (&sig + 1);
  163. suspend_new_thread(current->thread.mode.tt.switch_pipe[0]);
  164. force_flush_all();
  165. if(current->thread.prev_sched != NULL)
  166. schedule_tail(current->thread.prev_sched);
  167. current->thread.prev_sched = NULL;
  168. enable_timer();
  169. change_sig(SIGVTALRM, 1);
  170. local_irq_enable();
  171. if(current->mm != current->parent->mm)
  172. protect_memory(uml_reserved, high_physmem - uml_reserved, 1,
  173. 1, 0, 1);
  174. task_protections((unsigned long) current_thread);
  175. free_page(current->thread.temp_stack);
  176. local_irq_disable();
  177. change_sig(SIGUSR1, 0);
  178. set_user_mode(current);
  179. }
  180. int fork_tramp(void *stack)
  181. {
  182. local_irq_disable();
  183. arch_init_thread();
  184. init_new_thread_stack(stack, finish_fork_handler);
  185. os_usr1_process(os_getpid());
  186. change_sig(SIGUSR1, 1);
  187. return(0);
  188. }
  189. int copy_thread_tt(int nr, unsigned long clone_flags, unsigned long sp,
  190. unsigned long stack_top, struct task_struct * p,
  191. struct pt_regs *regs)
  192. {
  193. int (*tramp)(void *);
  194. int new_pid, err;
  195. unsigned long stack;
  196. if(current->thread.forking)
  197. tramp = fork_tramp;
  198. else {
  199. tramp = new_thread_proc;
  200. p->thread.request.u.thread = current->thread.request.u.thread;
  201. }
  202. err = os_pipe(p->thread.mode.tt.switch_pipe, 1, 1);
  203. if(err < 0){
  204. printk("copy_thread : pipe failed, err = %d\n", -err);
  205. return(err);
  206. }
  207. stack = alloc_stack(0, 0);
  208. if(stack == 0){
  209. printk(KERN_ERR "copy_thread : failed to allocate "
  210. "temporary stack\n");
  211. return(-ENOMEM);
  212. }
  213. clone_flags &= CLONE_VM;
  214. p->thread.temp_stack = stack;
  215. new_pid = start_fork_tramp(p->thread_info, stack, clone_flags, tramp);
  216. if(new_pid < 0){
  217. printk(KERN_ERR "copy_thread : clone failed - errno = %d\n",
  218. -new_pid);
  219. return(new_pid);
  220. }
  221. if(current->thread.forking){
  222. sc_to_sc(UPT_SC(&p->thread.regs.regs), UPT_SC(&regs->regs));
  223. SC_SET_SYSCALL_RETURN(UPT_SC(&p->thread.regs.regs), 0);
  224. if(sp != 0)
  225. SC_SP(UPT_SC(&p->thread.regs.regs)) = sp;
  226. }
  227. p->thread.mode.tt.extern_pid = new_pid;
  228. current->thread.request.op = OP_FORK;
  229. current->thread.request.u.fork.pid = new_pid;
  230. os_usr1_process(os_getpid());
  231. /* Enable the signal and then disable it to ensure that it is handled
  232. * here, and nowhere else.
  233. */
  234. change_sig(SIGUSR1, 1);
  235. change_sig(SIGUSR1, 0);
  236. err = 0;
  237. return(err);
  238. }
  239. void reboot_tt(void)
  240. {
  241. current->thread.request.op = OP_REBOOT;
  242. os_usr1_process(os_getpid());
  243. change_sig(SIGUSR1, 1);
  244. }
  245. void halt_tt(void)
  246. {
  247. current->thread.request.op = OP_HALT;
  248. os_usr1_process(os_getpid());
  249. change_sig(SIGUSR1, 1);
  250. }
  251. void kill_off_processes_tt(void)
  252. {
  253. struct task_struct *p;
  254. int me;
  255. me = os_getpid();
  256. for_each_process(p){
  257. if(p->thread.mode.tt.extern_pid != me)
  258. os_kill_process(p->thread.mode.tt.extern_pid, 0);
  259. }
  260. if(init_task.thread.mode.tt.extern_pid != me)
  261. os_kill_process(init_task.thread.mode.tt.extern_pid, 0);
  262. }
  263. void initial_thread_cb_tt(void (*proc)(void *), void *arg)
  264. {
  265. if(os_getpid() == tracing_pid){
  266. (*proc)(arg);
  267. }
  268. else {
  269. current->thread.request.op = OP_CB;
  270. current->thread.request.u.cb.proc = proc;
  271. current->thread.request.u.cb.arg = arg;
  272. os_usr1_process(os_getpid());
  273. change_sig(SIGUSR1, 1);
  274. change_sig(SIGUSR1, 0);
  275. }
  276. }
  277. int do_proc_op(void *t, int proc_id)
  278. {
  279. struct task_struct *task;
  280. struct thread_struct *thread;
  281. int op, pid;
  282. task = t;
  283. thread = &task->thread;
  284. op = thread->request.op;
  285. switch(op){
  286. case OP_NONE:
  287. case OP_TRACE_ON:
  288. break;
  289. case OP_EXEC:
  290. pid = thread->request.u.exec.pid;
  291. do_exec(thread->mode.tt.extern_pid, pid);
  292. thread->mode.tt.extern_pid = pid;
  293. cpu_tasks[task->thread_info->cpu].pid = pid;
  294. break;
  295. case OP_FORK:
  296. attach_process(thread->request.u.fork.pid);
  297. break;
  298. case OP_CB:
  299. (*thread->request.u.cb.proc)(thread->request.u.cb.arg);
  300. break;
  301. case OP_REBOOT:
  302. case OP_HALT:
  303. break;
  304. default:
  305. tracer_panic("Bad op in do_proc_op");
  306. break;
  307. }
  308. thread->request.op = OP_NONE;
  309. return(op);
  310. }
  311. void init_idle_tt(void)
  312. {
  313. default_idle();
  314. }
  315. extern void start_kernel(void);
  316. static int start_kernel_proc(void *unused)
  317. {
  318. int pid;
  319. block_signals();
  320. pid = os_getpid();
  321. cpu_tasks[0].pid = pid;
  322. cpu_tasks[0].task = current;
  323. #ifdef CONFIG_SMP
  324. cpu_online_map = cpumask_of_cpu(0);
  325. #endif
  326. if(debug) os_stop_process(pid);
  327. start_kernel();
  328. return(0);
  329. }
  330. void set_tracing(void *task, int tracing)
  331. {
  332. ((struct task_struct *) task)->thread.mode.tt.tracing = tracing;
  333. }
  334. int is_tracing(void *t)
  335. {
  336. return (((struct task_struct *) t)->thread.mode.tt.tracing);
  337. }
  338. int set_user_mode(void *t)
  339. {
  340. struct task_struct *task;
  341. task = t ? t : current;
  342. if(task->thread.mode.tt.tracing)
  343. return(1);
  344. task->thread.request.op = OP_TRACE_ON;
  345. os_usr1_process(os_getpid());
  346. return(0);
  347. }
  348. void set_init_pid(int pid)
  349. {
  350. int err;
  351. init_task.thread.mode.tt.extern_pid = pid;
  352. err = os_pipe(init_task.thread.mode.tt.switch_pipe, 1, 1);
  353. if(err)
  354. panic("Can't create switch pipe for init_task, errno = %d",
  355. -err);
  356. }
  357. int start_uml_tt(void)
  358. {
  359. void *sp;
  360. int pages;
  361. pages = (1 << CONFIG_KERNEL_STACK_ORDER);
  362. sp = (void *) ((unsigned long) init_task.thread_info) +
  363. pages * PAGE_SIZE - sizeof(unsigned long);
  364. return(tracer(start_kernel_proc, sp));
  365. }
  366. int external_pid_tt(struct task_struct *task)
  367. {
  368. return(task->thread.mode.tt.extern_pid);
  369. }
  370. int thread_pid_tt(struct task_struct *task)
  371. {
  372. return(task->thread.mode.tt.extern_pid);
  373. }
  374. int is_valid_pid(int pid)
  375. {
  376. struct task_struct *task;
  377. read_lock(&tasklist_lock);
  378. for_each_process(task){
  379. if(task->thread.mode.tt.extern_pid == pid){
  380. read_unlock(&tasklist_lock);
  381. return(1);
  382. }
  383. }
  384. read_unlock(&tasklist_lock);
  385. return(0);
  386. }