process_kern.c 11 KB

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