process.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include "linux/sched.h"
  6. #include "linux/slab.h"
  7. #include "linux/ptrace.h"
  8. #include "linux/proc_fs.h"
  9. #include "linux/file.h"
  10. #include "linux/errno.h"
  11. #include "linux/init.h"
  12. #include "asm/uaccess.h"
  13. #include "asm/atomic.h"
  14. #include "kern_util.h"
  15. #include "as-layout.h"
  16. #include "skas.h"
  17. #include "os.h"
  18. #include "tlb.h"
  19. #include "kern.h"
  20. #include "mode.h"
  21. #include "registers.h"
  22. void switch_to_skas(void *prev, void *next)
  23. {
  24. struct task_struct *from, *to;
  25. from = prev;
  26. to = next;
  27. /* XXX need to check runqueues[cpu].idle */
  28. if(current->pid == 0)
  29. switch_timers(0);
  30. switch_threads(&from->thread.mode.skas.switch_buf,
  31. &to->thread.mode.skas.switch_buf);
  32. arch_switch_to_skas(current->thread.prev_sched, current);
  33. if(current->pid == 0)
  34. switch_timers(1);
  35. }
  36. extern void schedule_tail(struct task_struct *prev);
  37. /* This is called magically, by its address being stuffed in a jmp_buf
  38. * and being longjmp-d to.
  39. */
  40. void new_thread_handler(void)
  41. {
  42. int (*fn)(void *), n;
  43. void *arg;
  44. if(current->thread.prev_sched != NULL)
  45. schedule_tail(current->thread.prev_sched);
  46. current->thread.prev_sched = NULL;
  47. fn = current->thread.request.u.thread.proc;
  48. arg = current->thread.request.u.thread.arg;
  49. /* The return value is 1 if the kernel thread execs a process,
  50. * 0 if it just exits
  51. */
  52. n = run_kernel_thread(fn, arg, &current->thread.exec_buf);
  53. if(n == 1){
  54. /* Handle any immediate reschedules or signals */
  55. interrupt_end();
  56. userspace(&current->thread.regs.regs);
  57. }
  58. else do_exit(0);
  59. }
  60. void release_thread_skas(struct task_struct *task)
  61. {
  62. }
  63. /* Called magically, see new_thread_handler above */
  64. void fork_handler(void)
  65. {
  66. force_flush_all();
  67. if(current->thread.prev_sched == NULL)
  68. panic("blech");
  69. schedule_tail(current->thread.prev_sched);
  70. /* XXX: if interrupt_end() calls schedule, this call to
  71. * arch_switch_to_skas isn't needed. We could want to apply this to
  72. * improve performance. -bb */
  73. arch_switch_to_skas(current->thread.prev_sched, current);
  74. current->thread.prev_sched = NULL;
  75. /* Handle any immediate reschedules or signals */
  76. interrupt_end();
  77. userspace(&current->thread.regs.regs);
  78. }
  79. int copy_thread_skas(int nr, unsigned long clone_flags, unsigned long sp,
  80. unsigned long stack_top, struct task_struct * p,
  81. struct pt_regs *regs)
  82. {
  83. void (*handler)(void);
  84. if(current->thread.forking){
  85. memcpy(&p->thread.regs.regs.skas, &regs->regs.skas,
  86. sizeof(p->thread.regs.regs.skas));
  87. REGS_SET_SYSCALL_RETURN(p->thread.regs.regs.skas.regs, 0);
  88. if(sp != 0) REGS_SP(p->thread.regs.regs.skas.regs) = sp;
  89. handler = fork_handler;
  90. arch_copy_thread(&current->thread.arch, &p->thread.arch);
  91. }
  92. else {
  93. init_thread_registers(&p->thread.regs.regs);
  94. p->thread.request.u.thread = current->thread.request.u.thread;
  95. handler = new_thread_handler;
  96. }
  97. new_thread(task_stack_page(p), &p->thread.mode.skas.switch_buf,
  98. handler);
  99. return(0);
  100. }
  101. int new_mm(unsigned long stack)
  102. {
  103. int fd;
  104. fd = os_open_file("/proc/mm", of_cloexec(of_write(OPENFLAGS())), 0);
  105. if(fd < 0)
  106. return(fd);
  107. if(skas_needs_stub)
  108. map_stub_pages(fd, CONFIG_STUB_CODE, CONFIG_STUB_DATA, stack);
  109. return(fd);
  110. }
  111. void init_idle_skas(void)
  112. {
  113. cpu_tasks[current_thread->cpu].pid = os_getpid();
  114. default_idle();
  115. }
  116. extern void start_kernel(void);
  117. static int start_kernel_proc(void *unused)
  118. {
  119. int pid;
  120. block_signals();
  121. pid = os_getpid();
  122. cpu_tasks[0].pid = pid;
  123. cpu_tasks[0].task = current;
  124. #ifdef CONFIG_SMP
  125. cpu_online_map = cpumask_of_cpu(0);
  126. #endif
  127. start_kernel();
  128. return(0);
  129. }
  130. extern int userspace_pid[];
  131. extern char cpu0_irqstack[];
  132. int start_uml_skas(void)
  133. {
  134. stack_protections((unsigned long) &cpu0_irqstack);
  135. set_sigstack(cpu0_irqstack, THREAD_SIZE);
  136. if(proc_mm)
  137. userspace_pid[0] = start_userspace(0);
  138. init_new_thread_signals();
  139. init_task.thread.request.u.thread.proc = start_kernel_proc;
  140. init_task.thread.request.u.thread.arg = NULL;
  141. return(start_idle_thread(task_stack_page(&init_task),
  142. &init_task.thread.mode.skas.switch_buf));
  143. }
  144. int external_pid_skas(struct task_struct *task)
  145. {
  146. /* FIXME: Need to look up userspace_pid by cpu */
  147. return(userspace_pid[0]);
  148. }
  149. int thread_pid_skas(struct task_struct *task)
  150. {
  151. /* FIXME: Need to look up userspace_pid by cpu */
  152. return(userspace_pid[0]);
  153. }
  154. void kill_off_processes_skas(void)
  155. {
  156. if(proc_mm)
  157. /*
  158. * FIXME: need to loop over userspace_pids in
  159. * kill_off_processes_skas
  160. */
  161. os_kill_ptraced_process(userspace_pid[0], 1);
  162. else {
  163. struct task_struct *p;
  164. int pid, me;
  165. me = os_getpid();
  166. for_each_process(p){
  167. if(p->mm == NULL)
  168. continue;
  169. pid = p->mm->context.skas.id.u.pid;
  170. os_kill_ptraced_process(pid, 1);
  171. }
  172. }
  173. }
  174. unsigned long current_stub_stack(void)
  175. {
  176. if(current->mm == NULL)
  177. return(0);
  178. return(current->mm->context.skas.id.stack);
  179. }