process_kern.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 "signal_user.h"
  17. #include "skas.h"
  18. #include "os.h"
  19. #include "user_util.h"
  20. #include "tlb.h"
  21. #include "kern.h"
  22. #include "mode.h"
  23. #include "proc_mm.h"
  24. #include "registers.h"
  25. void switch_to_skas(void *prev, void *next)
  26. {
  27. struct task_struct *from, *to;
  28. from = prev;
  29. to = next;
  30. /* XXX need to check runqueues[cpu].idle */
  31. if(current->pid == 0)
  32. switch_timers(0);
  33. switch_threads(&from->thread.mode.skas.switch_buf,
  34. to->thread.mode.skas.switch_buf);
  35. if(current->pid == 0)
  36. switch_timers(1);
  37. }
  38. extern void schedule_tail(struct task_struct *prev);
  39. void new_thread_handler(int sig)
  40. {
  41. int (*fn)(void *), n;
  42. void *arg;
  43. fn = current->thread.request.u.thread.proc;
  44. arg = current->thread.request.u.thread.arg;
  45. change_sig(SIGUSR1, 1);
  46. thread_wait(&current->thread.mode.skas.switch_buf,
  47. current->thread.mode.skas.fork_buf);
  48. if(current->thread.prev_sched != NULL)
  49. schedule_tail(current->thread.prev_sched);
  50. current->thread.prev_sched = NULL;
  51. /* The return value is 1 if the kernel thread execs a process,
  52. * 0 if it just exits
  53. */
  54. n = run_kernel_thread(fn, arg, &current->thread.exec_buf);
  55. if(n == 1){
  56. /* Handle any immediate reschedules or signals */
  57. interrupt_end();
  58. userspace(&current->thread.regs.regs);
  59. }
  60. else do_exit(0);
  61. }
  62. void new_thread_proc(void *stack, void (*handler)(int sig))
  63. {
  64. init_new_thread_stack(stack, handler);
  65. os_usr1_process(os_getpid());
  66. }
  67. void release_thread_skas(struct task_struct *task)
  68. {
  69. }
  70. void fork_handler(int sig)
  71. {
  72. change_sig(SIGUSR1, 1);
  73. thread_wait(&current->thread.mode.skas.switch_buf,
  74. current->thread.mode.skas.fork_buf);
  75. force_flush_all();
  76. if(current->thread.prev_sched == NULL)
  77. panic("blech");
  78. schedule_tail(current->thread.prev_sched);
  79. current->thread.prev_sched = NULL;
  80. /* Handle any immediate reschedules or signals */
  81. interrupt_end();
  82. userspace(&current->thread.regs.regs);
  83. }
  84. int copy_thread_skas(int nr, unsigned long clone_flags, unsigned long sp,
  85. unsigned long stack_top, struct task_struct * p,
  86. struct pt_regs *regs)
  87. {
  88. void (*handler)(int);
  89. if(current->thread.forking){
  90. memcpy(&p->thread.regs.regs.skas, &regs->regs.skas,
  91. sizeof(p->thread.regs.regs.skas));
  92. REGS_SET_SYSCALL_RETURN(p->thread.regs.regs.skas.regs, 0);
  93. if(sp != 0) REGS_SP(p->thread.regs.regs.skas.regs) = sp;
  94. handler = fork_handler;
  95. }
  96. else {
  97. init_thread_registers(&p->thread.regs.regs);
  98. p->thread.request.u.thread = current->thread.request.u.thread;
  99. handler = new_thread_handler;
  100. }
  101. new_thread(p->thread_info, &p->thread.mode.skas.switch_buf,
  102. &p->thread.mode.skas.fork_buf, handler);
  103. return(0);
  104. }
  105. extern void map_stub_pages(int fd, unsigned long code,
  106. unsigned long data, unsigned long stack);
  107. int new_mm(int from, unsigned long stack)
  108. {
  109. struct proc_mm_op copy;
  110. int n, fd;
  111. fd = os_open_file("/proc/mm", of_cloexec(of_write(OPENFLAGS())), 0);
  112. if(fd < 0)
  113. return(fd);
  114. if(from != -1){
  115. copy = ((struct proc_mm_op) { .op = MM_COPY_SEGMENTS,
  116. .u =
  117. { .copy_segments = from } } );
  118. n = os_write_file(fd, &copy, sizeof(copy));
  119. if(n != sizeof(copy))
  120. printk("new_mm : /proc/mm copy_segments failed, "
  121. "err = %d\n", -n);
  122. }
  123. if(!ptrace_faultinfo)
  124. map_stub_pages(fd, CONFIG_STUB_CODE, CONFIG_STUB_DATA, stack);
  125. return(fd);
  126. }
  127. void init_idle_skas(void)
  128. {
  129. cpu_tasks[current_thread->cpu].pid = os_getpid();
  130. default_idle();
  131. }
  132. extern void start_kernel(void);
  133. static int start_kernel_proc(void *unused)
  134. {
  135. int pid;
  136. block_signals();
  137. pid = os_getpid();
  138. cpu_tasks[0].pid = pid;
  139. cpu_tasks[0].task = current;
  140. #ifdef CONFIG_SMP
  141. cpu_online_map = cpumask_of_cpu(0);
  142. #endif
  143. start_kernel();
  144. return(0);
  145. }
  146. extern int userspace_pid[];
  147. int start_uml_skas(void)
  148. {
  149. if(proc_mm)
  150. userspace_pid[0] = start_userspace(0);
  151. init_new_thread_signals(1);
  152. init_task.thread.request.u.thread.proc = start_kernel_proc;
  153. init_task.thread.request.u.thread.arg = NULL;
  154. return(start_idle_thread(init_task.thread_info,
  155. &init_task.thread.mode.skas.switch_buf,
  156. &init_task.thread.mode.skas.fork_buf));
  157. }
  158. int external_pid_skas(struct task_struct *task)
  159. {
  160. #warning Need to look up userspace_pid by cpu
  161. return(userspace_pid[0]);
  162. }
  163. int thread_pid_skas(struct task_struct *task)
  164. {
  165. #warning Need to look up userspace_pid by cpu
  166. return(userspace_pid[0]);
  167. }
  168. void kill_off_processes_skas(void)
  169. {
  170. if(proc_mm)
  171. #warning need to loop over userspace_pids in kill_off_processes_skas
  172. os_kill_ptraced_process(userspace_pid[0], 1);
  173. else {
  174. struct task_struct *p;
  175. int pid, me;
  176. me = os_getpid();
  177. for_each_process(p){
  178. if(p->mm == NULL)
  179. continue;
  180. pid = p->mm->context.skas.id.u.pid;
  181. os_kill_ptraced_process(pid, 1);
  182. }
  183. }
  184. }
  185. unsigned long current_stub_stack(void)
  186. {
  187. if(current->mm == NULL)
  188. return(0);
  189. return(current->mm->context.skas.id.stack);
  190. }