process_kern.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 "skas.h"
  16. #include "os.h"
  17. #include "user_util.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. void new_thread_handler(int sig)
  38. {
  39. int (*fn)(void *), n;
  40. void *arg;
  41. fn = current->thread.request.u.thread.proc;
  42. arg = current->thread.request.u.thread.arg;
  43. os_usr1_signal(1);
  44. thread_wait(&current->thread.mode.skas.switch_buf,
  45. current->thread.mode.skas.fork_buf);
  46. if(current->thread.prev_sched != NULL)
  47. schedule_tail(current->thread.prev_sched);
  48. current->thread.prev_sched = NULL;
  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 new_thread_proc(void *stack, void (*handler)(int sig))
  61. {
  62. init_new_thread_stack(stack, handler);
  63. os_usr1_process(os_getpid());
  64. }
  65. void release_thread_skas(struct task_struct *task)
  66. {
  67. }
  68. void fork_handler(int sig)
  69. {
  70. os_usr1_signal(1);
  71. thread_wait(&current->thread.mode.skas.switch_buf,
  72. current->thread.mode.skas.fork_buf);
  73. force_flush_all();
  74. if(current->thread.prev_sched == NULL)
  75. panic("blech");
  76. schedule_tail(current->thread.prev_sched);
  77. /* XXX: if interrupt_end() calls schedule, this call to
  78. * arch_switch_to_skas isn't needed. We could want to apply this to
  79. * improve performance. -bb */
  80. arch_switch_to_skas(current->thread.prev_sched, current);
  81. current->thread.prev_sched = NULL;
  82. /* Handle any immediate reschedules or signals */
  83. interrupt_end();
  84. userspace(&current->thread.regs.regs);
  85. }
  86. int copy_thread_skas(int nr, unsigned long clone_flags, unsigned long sp,
  87. unsigned long stack_top, struct task_struct * p,
  88. struct pt_regs *regs)
  89. {
  90. void (*handler)(int);
  91. if(current->thread.forking){
  92. memcpy(&p->thread.regs.regs.skas, &regs->regs.skas,
  93. sizeof(p->thread.regs.regs.skas));
  94. REGS_SET_SYSCALL_RETURN(p->thread.regs.regs.skas.regs, 0);
  95. if(sp != 0) REGS_SP(p->thread.regs.regs.skas.regs) = sp;
  96. handler = fork_handler;
  97. arch_copy_thread(&current->thread.arch, &p->thread.arch);
  98. }
  99. else {
  100. init_thread_registers(&p->thread.regs.regs);
  101. p->thread.request.u.thread = current->thread.request.u.thread;
  102. handler = new_thread_handler;
  103. }
  104. new_thread(task_stack_page(p), &p->thread.mode.skas.switch_buf,
  105. &p->thread.mode.skas.fork_buf, handler);
  106. return(0);
  107. }
  108. int new_mm(unsigned long stack)
  109. {
  110. int fd;
  111. fd = os_open_file("/proc/mm", of_cloexec(of_write(OPENFLAGS())), 0);
  112. if(fd < 0)
  113. return(fd);
  114. if(skas_needs_stub)
  115. map_stub_pages(fd, CONFIG_STUB_CODE, CONFIG_STUB_DATA, stack);
  116. return(fd);
  117. }
  118. void init_idle_skas(void)
  119. {
  120. cpu_tasks[current_thread->cpu].pid = os_getpid();
  121. default_idle();
  122. }
  123. extern void start_kernel(void);
  124. static int start_kernel_proc(void *unused)
  125. {
  126. int pid;
  127. block_signals();
  128. pid = os_getpid();
  129. cpu_tasks[0].pid = pid;
  130. cpu_tasks[0].task = current;
  131. #ifdef CONFIG_SMP
  132. cpu_online_map = cpumask_of_cpu(0);
  133. #endif
  134. start_kernel();
  135. return(0);
  136. }
  137. extern int userspace_pid[];
  138. int start_uml_skas(void)
  139. {
  140. if(proc_mm)
  141. userspace_pid[0] = start_userspace(0);
  142. init_new_thread_signals(1);
  143. init_task.thread.request.u.thread.proc = start_kernel_proc;
  144. init_task.thread.request.u.thread.arg = NULL;
  145. return(start_idle_thread(task_stack_page(&init_task),
  146. &init_task.thread.mode.skas.switch_buf,
  147. &init_task.thread.mode.skas.fork_buf));
  148. }
  149. int external_pid_skas(struct task_struct *task)
  150. {
  151. #warning Need to look up userspace_pid by cpu
  152. return(userspace_pid[0]);
  153. }
  154. int thread_pid_skas(struct task_struct *task)
  155. {
  156. #warning Need to look up userspace_pid by cpu
  157. return(userspace_pid[0]);
  158. }
  159. void kill_off_processes_skas(void)
  160. {
  161. if(proc_mm)
  162. #warning need to loop over userspace_pids in kill_off_processes_skas
  163. os_kill_ptraced_process(userspace_pid[0], 1);
  164. else {
  165. struct task_struct *p;
  166. int pid, me;
  167. me = os_getpid();
  168. for_each_process(p){
  169. if(p->mm == NULL)
  170. continue;
  171. pid = p->mm->context.skas.id.u.pid;
  172. os_kill_ptraced_process(pid, 1);
  173. }
  174. }
  175. }
  176. unsigned long current_stub_stack(void)
  177. {
  178. if(current->mm == NULL)
  179. return(0);
  180. return(current->mm->context.skas.id.stack);
  181. }