process_kern.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. to->thread.prev_sched = from;
  34. set_current(to);
  35. switch_threads(&from->thread.mode.skas.switch_buf,
  36. to->thread.mode.skas.switch_buf);
  37. if(current->pid == 0)
  38. switch_timers(1);
  39. return(current->thread.prev_sched);
  40. }
  41. extern void schedule_tail(struct task_struct *prev);
  42. void new_thread_handler(int sig)
  43. {
  44. int (*fn)(void *), n;
  45. void *arg;
  46. fn = current->thread.request.u.thread.proc;
  47. arg = current->thread.request.u.thread.arg;
  48. change_sig(SIGUSR1, 1);
  49. thread_wait(&current->thread.mode.skas.switch_buf,
  50. current->thread.mode.skas.fork_buf);
  51. if(current->thread.prev_sched != NULL)
  52. schedule_tail(current->thread.prev_sched);
  53. current->thread.prev_sched = NULL;
  54. /* The return value is 1 if the kernel thread execs a process,
  55. * 0 if it just exits
  56. */
  57. n = run_kernel_thread(fn, arg, &current->thread.exec_buf);
  58. if(n == 1)
  59. userspace(&current->thread.regs.regs);
  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. 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,
  89. &current->thread.regs.regs.skas,
  90. sizeof(p->thread.regs.regs.skas));
  91. REGS_SET_SYSCALL_RETURN(p->thread.regs.regs.skas.regs, 0);
  92. if(sp != 0) REGS_SP(p->thread.regs.regs.skas.regs) = sp;
  93. handler = fork_handler;
  94. }
  95. else {
  96. init_thread_registers(&p->thread.regs.regs);
  97. p->thread.request.u.thread = current->thread.request.u.thread;
  98. handler = new_thread_handler;
  99. }
  100. new_thread(p->thread_info, &p->thread.mode.skas.switch_buf,
  101. &p->thread.mode.skas.fork_buf, handler);
  102. return(0);
  103. }
  104. int new_mm(int from)
  105. {
  106. struct proc_mm_op copy;
  107. int n, fd;
  108. fd = os_open_file("/proc/mm", of_cloexec(of_write(OPENFLAGS())), 0);
  109. if(fd < 0)
  110. return(fd);
  111. if(from != -1){
  112. copy = ((struct proc_mm_op) { .op = MM_COPY_SEGMENTS,
  113. .u =
  114. { .copy_segments = from } } );
  115. n = os_write_file(fd, &copy, sizeof(copy));
  116. if(n != sizeof(copy))
  117. printk("new_mm : /proc/mm copy_segments failed, "
  118. "err = %d\n", -n);
  119. }
  120. return(fd);
  121. }
  122. void init_idle_skas(void)
  123. {
  124. cpu_tasks[current_thread->cpu].pid = os_getpid();
  125. default_idle();
  126. }
  127. extern void start_kernel(void);
  128. static int start_kernel_proc(void *unused)
  129. {
  130. int pid;
  131. block_signals();
  132. pid = os_getpid();
  133. cpu_tasks[0].pid = pid;
  134. cpu_tasks[0].task = current;
  135. #ifdef CONFIG_SMP
  136. cpu_online_map = cpumask_of_cpu(0);
  137. #endif
  138. start_kernel();
  139. return(0);
  140. }
  141. int start_uml_skas(void)
  142. {
  143. start_userspace(0);
  144. init_new_thread_signals(1);
  145. uml_idle_timer();
  146. init_task.thread.request.u.thread.proc = start_kernel_proc;
  147. init_task.thread.request.u.thread.arg = NULL;
  148. return(start_idle_thread(init_task.thread_info,
  149. &init_task.thread.mode.skas.switch_buf,
  150. &init_task.thread.mode.skas.fork_buf));
  151. }
  152. int external_pid_skas(struct task_struct *task)
  153. {
  154. #warning Need to look up userspace_pid by cpu
  155. return(userspace_pid[0]);
  156. }
  157. int thread_pid_skas(struct task_struct *task)
  158. {
  159. #warning Need to look up userspace_pid by cpu
  160. return(userspace_pid[0]);
  161. }
  162. /*
  163. * Overrides for Emacs so that we follow Linus's tabbing style.
  164. * Emacs will notice this stuff at the end of the file and automatically
  165. * adjust the settings for this buffer only. This must remain at the end
  166. * of the file.
  167. * ---------------------------------------------------------------------------
  168. * Local variables:
  169. * c-file-style: "linux"
  170. * End:
  171. */