process_kern.c 11 KB

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