process.c 7.8 KB

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