process.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Copyright 2003 PathScale, Inc.
  4. * Licensed under the GPL
  5. */
  6. #include "linux/stddef.h"
  7. #include "linux/err.h"
  8. #include "linux/hardirq.h"
  9. #include "linux/mm.h"
  10. #include "linux/personality.h"
  11. #include "linux/proc_fs.h"
  12. #include "linux/ptrace.h"
  13. #include "linux/random.h"
  14. #include "linux/sched.h"
  15. #include "linux/threads.h"
  16. #include "asm/pgtable.h"
  17. #include "asm/uaccess.h"
  18. #include "as-layout.h"
  19. #include "kern_util.h"
  20. #include "os.h"
  21. #include "skas.h"
  22. #include "tlb.h"
  23. /*
  24. * This is a per-cpu array. A processor only modifies its entry and it only
  25. * cares about its entry, so it's OK if another processor is modifying its
  26. * entry.
  27. */
  28. struct cpu_task cpu_tasks[NR_CPUS] = { [0 ... NR_CPUS - 1] = { -1, NULL } };
  29. static inline int external_pid(struct task_struct *task)
  30. {
  31. /* FIXME: Need to look up userspace_pid by cpu */
  32. return userspace_pid[0];
  33. }
  34. int pid_to_processor_id(int pid)
  35. {
  36. int i;
  37. for(i = 0; i < ncpus; i++) {
  38. if (cpu_tasks[i].pid == pid)
  39. return i;
  40. }
  41. return -1;
  42. }
  43. void free_stack(unsigned long stack, int order)
  44. {
  45. free_pages(stack, order);
  46. }
  47. unsigned long alloc_stack(int order, int atomic)
  48. {
  49. unsigned long page;
  50. gfp_t flags = GFP_KERNEL;
  51. if (atomic)
  52. flags = GFP_ATOMIC;
  53. page = __get_free_pages(flags, order);
  54. if (page == 0)
  55. return 0;
  56. return page;
  57. }
  58. int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
  59. {
  60. int pid;
  61. current->thread.request.u.thread.proc = fn;
  62. current->thread.request.u.thread.arg = arg;
  63. pid = do_fork(CLONE_VM | CLONE_UNTRACED | flags, 0,
  64. &current->thread.regs, 0, NULL, NULL);
  65. return pid;
  66. }
  67. static inline void set_current(struct task_struct *task)
  68. {
  69. cpu_tasks[task_thread_info(task)->cpu] = ((struct cpu_task)
  70. { external_pid(task), task });
  71. }
  72. extern void arch_switch_to(struct task_struct *from, struct task_struct *to);
  73. void *_switch_to(void *prev, void *next, void *last)
  74. {
  75. struct task_struct *from = prev;
  76. struct task_struct *to= next;
  77. to->thread.prev_sched = from;
  78. set_current(to);
  79. do {
  80. current->thread.saved_task = NULL;
  81. switch_threads(&from->thread.switch_buf,
  82. &to->thread.switch_buf);
  83. arch_switch_to(current->thread.prev_sched, current);
  84. if (current->thread.saved_task)
  85. show_regs(&(current->thread.regs));
  86. next= current->thread.saved_task;
  87. prev= current;
  88. } while(current->thread.saved_task);
  89. return current->thread.prev_sched;
  90. }
  91. void interrupt_end(void)
  92. {
  93. if (need_resched())
  94. schedule();
  95. if (test_tsk_thread_flag(current, TIF_SIGPENDING))
  96. do_signal();
  97. }
  98. void exit_thread(void)
  99. {
  100. }
  101. void *get_current(void)
  102. {
  103. return current;
  104. }
  105. extern void schedule_tail(struct task_struct *prev);
  106. /*
  107. * This is called magically, by its address being stuffed in a jmp_buf
  108. * and being longjmp-d to.
  109. */
  110. void new_thread_handler(void)
  111. {
  112. int (*fn)(void *), n;
  113. void *arg;
  114. if (current->thread.prev_sched != NULL)
  115. schedule_tail(current->thread.prev_sched);
  116. current->thread.prev_sched = NULL;
  117. fn = current->thread.request.u.thread.proc;
  118. arg = current->thread.request.u.thread.arg;
  119. /*
  120. * The return value is 1 if the kernel thread execs a process,
  121. * 0 if it just exits
  122. */
  123. n = run_kernel_thread(fn, arg, &current->thread.exec_buf);
  124. if (n == 1) {
  125. /* Handle any immediate reschedules or signals */
  126. interrupt_end();
  127. userspace(&current->thread.regs.regs);
  128. }
  129. else do_exit(0);
  130. }
  131. /* Called magically, see new_thread_handler above */
  132. void fork_handler(void)
  133. {
  134. force_flush_all();
  135. if (current->thread.prev_sched == NULL)
  136. panic("blech");
  137. schedule_tail(current->thread.prev_sched);
  138. /*
  139. * XXX: if interrupt_end() calls schedule, this call to
  140. * arch_switch_to isn't needed. We could want to apply this to
  141. * improve performance. -bb
  142. */
  143. arch_switch_to(current->thread.prev_sched, current);
  144. current->thread.prev_sched = NULL;
  145. /* Handle any immediate reschedules or signals */
  146. interrupt_end();
  147. userspace(&current->thread.regs.regs);
  148. }
  149. int copy_thread(int nr, unsigned long clone_flags, unsigned long sp,
  150. unsigned long stack_top, struct task_struct * p,
  151. struct pt_regs *regs)
  152. {
  153. void (*handler)(void);
  154. int ret = 0;
  155. p->thread = (struct thread_struct) INIT_THREAD;
  156. if (current->thread.forking) {
  157. memcpy(&p->thread.regs.regs, &regs->regs,
  158. sizeof(p->thread.regs.regs));
  159. REGS_SET_SYSCALL_RETURN(p->thread.regs.regs.gp, 0);
  160. if (sp != 0)
  161. REGS_SP(p->thread.regs.regs.gp) = sp;
  162. handler = fork_handler;
  163. arch_copy_thread(&current->thread.arch, &p->thread.arch);
  164. }
  165. else {
  166. init_thread_registers(&p->thread.regs.regs);
  167. p->thread.request.u.thread = current->thread.request.u.thread;
  168. handler = new_thread_handler;
  169. }
  170. new_thread(task_stack_page(p), &p->thread.switch_buf, handler);
  171. if (current->thread.forking) {
  172. clear_flushed_tls(p);
  173. /*
  174. * Set a new TLS for the child thread?
  175. */
  176. if (clone_flags & CLONE_SETTLS)
  177. ret = arch_copy_tls(p);
  178. }
  179. return ret;
  180. }
  181. void initial_thread_cb(void (*proc)(void *), void *arg)
  182. {
  183. int save_kmalloc_ok = kmalloc_ok;
  184. kmalloc_ok = 0;
  185. initial_thread_cb_skas(proc, arg);
  186. kmalloc_ok = save_kmalloc_ok;
  187. }
  188. void default_idle(void)
  189. {
  190. while(1) {
  191. /* endless idle loop with no priority at all */
  192. /*
  193. * although we are an idle CPU, we do not want to
  194. * get into the scheduler unnecessarily.
  195. */
  196. if (need_resched())
  197. schedule();
  198. switch_timers(1);
  199. idle_sleep(10);
  200. switch_timers(0);
  201. }
  202. }
  203. void cpu_idle(void)
  204. {
  205. cpu_tasks[current_thread->cpu].pid = os_getpid();
  206. default_idle();
  207. }
  208. void *um_virt_to_phys(struct task_struct *task, unsigned long addr,
  209. pte_t *pte_out)
  210. {
  211. pgd_t *pgd;
  212. pud_t *pud;
  213. pmd_t *pmd;
  214. pte_t *pte;
  215. pte_t ptent;
  216. if (task->mm == NULL)
  217. return ERR_PTR(-EINVAL);
  218. pgd = pgd_offset(task->mm, addr);
  219. if (!pgd_present(*pgd))
  220. return ERR_PTR(-EINVAL);
  221. pud = pud_offset(pgd, addr);
  222. if (!pud_present(*pud))
  223. return ERR_PTR(-EINVAL);
  224. pmd = pmd_offset(pud, addr);
  225. if (!pmd_present(*pmd))
  226. return ERR_PTR(-EINVAL);
  227. pte = pte_offset_kernel(pmd, addr);
  228. ptent = *pte;
  229. if (!pte_present(ptent))
  230. return ERR_PTR(-EINVAL);
  231. if (pte_out != NULL)
  232. *pte_out = ptent;
  233. return (void *) (pte_val(ptent) & PAGE_MASK) + (addr & ~PAGE_MASK);
  234. }
  235. char *current_cmd(void)
  236. {
  237. #if defined(CONFIG_SMP) || defined(CONFIG_HIGHMEM)
  238. return "(Unknown)";
  239. #else
  240. void *addr = um_virt_to_phys(current, current->mm->arg_start, NULL);
  241. return IS_ERR(addr) ? "(Unknown)": __va((unsigned long) addr);
  242. #endif
  243. }
  244. void dump_thread(struct pt_regs *regs, struct user *u)
  245. {
  246. }
  247. int __cant_sleep(void) {
  248. return in_atomic() || irqs_disabled() || in_interrupt();
  249. /* Is in_interrupt() really needed? */
  250. }
  251. int user_context(unsigned long sp)
  252. {
  253. unsigned long stack;
  254. stack = sp & (PAGE_MASK << CONFIG_KERNEL_STACK_ORDER);
  255. return stack != (unsigned long) current_thread;
  256. }
  257. extern exitcall_t __uml_exitcall_begin, __uml_exitcall_end;
  258. void do_uml_exitcalls(void)
  259. {
  260. exitcall_t *call;
  261. call = &__uml_exitcall_end;
  262. while (--call >= &__uml_exitcall_begin)
  263. (*call)();
  264. }
  265. char *uml_strdup(char *string)
  266. {
  267. return kstrdup(string, GFP_KERNEL);
  268. }
  269. int copy_to_user_proc(void __user *to, void *from, int size)
  270. {
  271. return copy_to_user(to, from, size);
  272. }
  273. int copy_from_user_proc(void *to, void __user *from, int size)
  274. {
  275. return copy_from_user(to, from, size);
  276. }
  277. int clear_user_proc(void __user *buf, int size)
  278. {
  279. return clear_user(buf, size);
  280. }
  281. int strlen_user_proc(char __user *str)
  282. {
  283. return strlen_user(str);
  284. }
  285. int smp_sigio_handler(void)
  286. {
  287. #ifdef CONFIG_SMP
  288. int cpu = current_thread->cpu;
  289. IPI_handler(cpu);
  290. if (cpu != 0)
  291. return 1;
  292. #endif
  293. return 0;
  294. }
  295. int cpu(void)
  296. {
  297. return current_thread->cpu;
  298. }
  299. static atomic_t using_sysemu = ATOMIC_INIT(0);
  300. int sysemu_supported;
  301. void set_using_sysemu(int value)
  302. {
  303. if (value > sysemu_supported)
  304. return;
  305. atomic_set(&using_sysemu, value);
  306. }
  307. int get_using_sysemu(void)
  308. {
  309. return atomic_read(&using_sysemu);
  310. }
  311. static int proc_read_sysemu(char *buf, char **start, off_t offset, int size,int *eof, void *data)
  312. {
  313. if (snprintf(buf, size, "%d\n", get_using_sysemu()) < size)
  314. /* No overflow */
  315. *eof = 1;
  316. return strlen(buf);
  317. }
  318. static int proc_write_sysemu(struct file *file,const char __user *buf, unsigned long count,void *data)
  319. {
  320. char tmp[2];
  321. if (copy_from_user(tmp, buf, 1))
  322. return -EFAULT;
  323. if (tmp[0] >= '0' && tmp[0] <= '2')
  324. set_using_sysemu(tmp[0] - '0');
  325. /* We use the first char, but pretend to write everything */
  326. return count;
  327. }
  328. int __init make_proc_sysemu(void)
  329. {
  330. struct proc_dir_entry *ent;
  331. if (!sysemu_supported)
  332. return 0;
  333. ent = create_proc_entry("sysemu", 0600, &proc_root);
  334. if (ent == NULL)
  335. {
  336. printk(KERN_WARNING "Failed to register /proc/sysemu\n");
  337. return 0;
  338. }
  339. ent->read_proc = proc_read_sysemu;
  340. ent->write_proc = proc_write_sysemu;
  341. return 0;
  342. }
  343. late_initcall(make_proc_sysemu);
  344. int singlestepping(void * t)
  345. {
  346. struct task_struct *task = t ? t : current;
  347. if ( ! (task->ptrace & PT_DTRACE) )
  348. return 0;
  349. if (task->thread.singlestep_syscall)
  350. return 1;
  351. return 2;
  352. }
  353. /*
  354. * Only x86 and x86_64 have an arch_align_stack().
  355. * All other arches have "#define arch_align_stack(x) (x)"
  356. * in their asm/system.h
  357. * As this is included in UML from asm-um/system-generic.h,
  358. * we can use it to behave as the subarch does.
  359. */
  360. #ifndef arch_align_stack
  361. unsigned long arch_align_stack(unsigned long sp)
  362. {
  363. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  364. sp -= get_random_int() % 8192;
  365. return sp & ~0xf;
  366. }
  367. #endif