process_kern.c 12 KB

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