process_kern.c 12 KB

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