process_kern.c 11 KB

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