process.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. /* 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. int start_uml_skas(void)
  132. {
  133. if(proc_mm)
  134. userspace_pid[0] = start_userspace(0);
  135. init_new_thread_signals();
  136. init_task.thread.request.u.thread.proc = start_kernel_proc;
  137. init_task.thread.request.u.thread.arg = NULL;
  138. return(start_idle_thread(task_stack_page(&init_task),
  139. &init_task.thread.mode.skas.switch_buf));
  140. }
  141. int external_pid_skas(struct task_struct *task)
  142. {
  143. #warning Need to look up userspace_pid by cpu
  144. return(userspace_pid[0]);
  145. }
  146. int thread_pid_skas(struct task_struct *task)
  147. {
  148. #warning Need to look up userspace_pid by cpu
  149. return(userspace_pid[0]);
  150. }
  151. void kill_off_processes_skas(void)
  152. {
  153. if(proc_mm)
  154. #warning need to loop over userspace_pids in kill_off_processes_skas
  155. os_kill_ptraced_process(userspace_pid[0], 1);
  156. else {
  157. struct task_struct *p;
  158. int pid, me;
  159. me = os_getpid();
  160. for_each_process(p){
  161. if(p->mm == NULL)
  162. continue;
  163. pid = p->mm->context.skas.id.u.pid;
  164. os_kill_ptraced_process(pid, 1);
  165. }
  166. }
  167. }
  168. unsigned long current_stub_stack(void)
  169. {
  170. if(current->mm == NULL)
  171. return(0);
  172. return(current->mm->context.skas.id.stack);
  173. }