process_kern.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 "time_user.h"
  16. #include "skas.h"
  17. #include "os.h"
  18. #include "user_util.h"
  19. #include "tlb.h"
  20. #include "kern.h"
  21. #include "mode.h"
  22. #include "registers.h"
  23. void switch_to_skas(void *prev, void *next)
  24. {
  25. struct task_struct *from, *to;
  26. from = prev;
  27. to = next;
  28. /* XXX need to check runqueues[cpu].idle */
  29. if(current->pid == 0)
  30. switch_timers(0);
  31. switch_threads(&from->thread.mode.skas.switch_buf,
  32. to->thread.mode.skas.switch_buf);
  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. change_sig(SIGUSR1, 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. change_sig(SIGUSR1, 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. current->thread.prev_sched = NULL;
  78. /* Handle any immediate reschedules or signals */
  79. interrupt_end();
  80. userspace(&current->thread.regs.regs);
  81. }
  82. int copy_thread_skas(int nr, unsigned long clone_flags, unsigned long sp,
  83. unsigned long stack_top, struct task_struct * p,
  84. struct pt_regs *regs)
  85. {
  86. void (*handler)(int);
  87. if(current->thread.forking){
  88. memcpy(&p->thread.regs.regs.skas, &regs->regs.skas,
  89. sizeof(p->thread.regs.regs.skas));
  90. REGS_SET_SYSCALL_RETURN(p->thread.regs.regs.skas.regs, 0);
  91. if(sp != 0) REGS_SP(p->thread.regs.regs.skas.regs) = sp;
  92. handler = fork_handler;
  93. }
  94. else {
  95. init_thread_registers(&p->thread.regs.regs);
  96. p->thread.request.u.thread = current->thread.request.u.thread;
  97. handler = new_thread_handler;
  98. }
  99. new_thread(task_stack_page(p), &p->thread.mode.skas.switch_buf,
  100. &p->thread.mode.skas.fork_buf, handler);
  101. return(0);
  102. }
  103. extern void map_stub_pages(int fd, unsigned long code,
  104. unsigned long data, unsigned long stack);
  105. int new_mm(unsigned long stack)
  106. {
  107. int fd;
  108. fd = os_open_file("/proc/mm", of_cloexec(of_write(OPENFLAGS())), 0);
  109. if(fd < 0)
  110. return(fd);
  111. if(skas_needs_stub)
  112. map_stub_pages(fd, CONFIG_STUB_CODE, CONFIG_STUB_DATA, stack);
  113. return(fd);
  114. }
  115. void init_idle_skas(void)
  116. {
  117. cpu_tasks[current_thread->cpu].pid = os_getpid();
  118. default_idle();
  119. }
  120. extern void start_kernel(void);
  121. static int start_kernel_proc(void *unused)
  122. {
  123. int pid;
  124. block_signals();
  125. pid = os_getpid();
  126. cpu_tasks[0].pid = pid;
  127. cpu_tasks[0].task = current;
  128. #ifdef CONFIG_SMP
  129. cpu_online_map = cpumask_of_cpu(0);
  130. #endif
  131. start_kernel();
  132. return(0);
  133. }
  134. extern int userspace_pid[];
  135. int start_uml_skas(void)
  136. {
  137. if(proc_mm)
  138. userspace_pid[0] = start_userspace(0);
  139. init_new_thread_signals(1);
  140. init_task.thread.request.u.thread.proc = start_kernel_proc;
  141. init_task.thread.request.u.thread.arg = NULL;
  142. return(start_idle_thread(task_stack_page(&init_task),
  143. &init_task.thread.mode.skas.switch_buf,
  144. &init_task.thread.mode.skas.fork_buf));
  145. }
  146. int external_pid_skas(struct task_struct *task)
  147. {
  148. #warning Need to look up userspace_pid by cpu
  149. return(userspace_pid[0]);
  150. }
  151. int thread_pid_skas(struct task_struct *task)
  152. {
  153. #warning Need to look up userspace_pid by cpu
  154. return(userspace_pid[0]);
  155. }
  156. void kill_off_processes_skas(void)
  157. {
  158. if(proc_mm)
  159. #warning need to loop over userspace_pids in kill_off_processes_skas
  160. os_kill_ptraced_process(userspace_pid[0], 1);
  161. else {
  162. struct task_struct *p;
  163. int pid, me;
  164. me = os_getpid();
  165. for_each_process(p){
  166. if(p->mm == NULL)
  167. continue;
  168. pid = p->mm->context.skas.id.u.pid;
  169. os_kill_ptraced_process(pid, 1);
  170. }
  171. }
  172. }
  173. unsigned long current_stub_stack(void)
  174. {
  175. if(current->mm == NULL)
  176. return(0);
  177. return(current->mm->context.skas.id.stack);
  178. }