process.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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 "user_util.h"
  35. #include "kern_util.h"
  36. #include "as-layout.h"
  37. #include "kern.h"
  38. #include "signal_kern.h"
  39. #include "init.h"
  40. #include "irq_user.h"
  41. #include "mem_user.h"
  42. #include "tlb.h"
  43. #include "frame_kern.h"
  44. #include "sigcontext.h"
  45. #include "os.h"
  46. #include "mode.h"
  47. #include "mode_kern.h"
  48. #include "choose-mode.h"
  49. #include "um_malloc.h"
  50. /* This is a per-cpu array. A processor only modifies its entry and it only
  51. * cares about its entry, so it's OK if another processor is modifying its
  52. * entry.
  53. */
  54. struct cpu_task cpu_tasks[NR_CPUS] = { [0 ... NR_CPUS - 1] = { -1, NULL } };
  55. int external_pid(void *t)
  56. {
  57. struct task_struct *task = t ? t : current;
  58. return(CHOOSE_MODE_PROC(external_pid_tt, external_pid_skas, task));
  59. }
  60. int pid_to_processor_id(int pid)
  61. {
  62. int i;
  63. for(i = 0; i < ncpus; i++){
  64. if(cpu_tasks[i].pid == pid) return(i);
  65. }
  66. return(-1);
  67. }
  68. void free_stack(unsigned long stack, int order)
  69. {
  70. free_pages(stack, order);
  71. }
  72. unsigned long alloc_stack(int order, int atomic)
  73. {
  74. unsigned long page;
  75. gfp_t flags = GFP_KERNEL;
  76. if (atomic)
  77. flags = GFP_ATOMIC;
  78. page = __get_free_pages(flags, order);
  79. if(page == 0)
  80. return(0);
  81. stack_protections(page);
  82. return(page);
  83. }
  84. int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
  85. {
  86. int pid;
  87. current->thread.request.u.thread.proc = fn;
  88. current->thread.request.u.thread.arg = arg;
  89. pid = do_fork(CLONE_VM | CLONE_UNTRACED | flags, 0,
  90. &current->thread.regs, 0, NULL, NULL);
  91. if(pid < 0)
  92. panic("do_fork failed in kernel_thread, errno = %d", pid);
  93. return(pid);
  94. }
  95. void set_current(void *t)
  96. {
  97. struct task_struct *task = t;
  98. cpu_tasks[task_thread_info(task)->cpu] = ((struct cpu_task)
  99. { external_pid(task), task });
  100. }
  101. void *_switch_to(void *prev, void *next, void *last)
  102. {
  103. struct task_struct *from = prev;
  104. struct task_struct *to= next;
  105. to->thread.prev_sched = from;
  106. set_current(to);
  107. do {
  108. current->thread.saved_task = NULL ;
  109. CHOOSE_MODE_PROC(switch_to_tt, switch_to_skas, prev, next);
  110. if(current->thread.saved_task)
  111. show_regs(&(current->thread.regs));
  112. next= current->thread.saved_task;
  113. prev= current;
  114. } while(current->thread.saved_task);
  115. return(current->thread.prev_sched);
  116. }
  117. void interrupt_end(void)
  118. {
  119. if(need_resched()) schedule();
  120. if(test_tsk_thread_flag(current, TIF_SIGPENDING)) do_signal();
  121. }
  122. void release_thread(struct task_struct *task)
  123. {
  124. CHOOSE_MODE(release_thread_tt(task), release_thread_skas(task));
  125. }
  126. void exit_thread(void)
  127. {
  128. unprotect_stack((unsigned long) current_thread);
  129. }
  130. void *get_current(void)
  131. {
  132. return(current);
  133. }
  134. int copy_thread(int nr, unsigned long clone_flags, unsigned long sp,
  135. unsigned long stack_top, struct task_struct * p,
  136. struct pt_regs *regs)
  137. {
  138. int ret;
  139. p->thread = (struct thread_struct) INIT_THREAD;
  140. ret = CHOOSE_MODE_PROC(copy_thread_tt, copy_thread_skas, nr,
  141. clone_flags, sp, stack_top, p, regs);
  142. if (ret || !current->thread.forking)
  143. goto out;
  144. clear_flushed_tls(p);
  145. /*
  146. * Set a new TLS for the child thread?
  147. */
  148. if (clone_flags & CLONE_SETTLS)
  149. ret = arch_copy_tls(p);
  150. out:
  151. return ret;
  152. }
  153. void initial_thread_cb(void (*proc)(void *), void *arg)
  154. {
  155. int save_kmalloc_ok = kmalloc_ok;
  156. kmalloc_ok = 0;
  157. CHOOSE_MODE_PROC(initial_thread_cb_tt, initial_thread_cb_skas, proc,
  158. arg);
  159. kmalloc_ok = save_kmalloc_ok;
  160. }
  161. unsigned long stack_sp(unsigned long page)
  162. {
  163. return(page + PAGE_SIZE - sizeof(void *));
  164. }
  165. int current_pid(void)
  166. {
  167. return(current->pid);
  168. }
  169. void default_idle(void)
  170. {
  171. CHOOSE_MODE(uml_idle_timer(), (void) 0);
  172. while(1){
  173. /* endless idle loop with no priority at all */
  174. /*
  175. * although we are an idle CPU, we do not want to
  176. * get into the scheduler unnecessarily.
  177. */
  178. if(need_resched())
  179. schedule();
  180. idle_sleep(10);
  181. }
  182. }
  183. void cpu_idle(void)
  184. {
  185. CHOOSE_MODE(init_idle_tt(), init_idle_skas());
  186. }
  187. int page_size(void)
  188. {
  189. return(PAGE_SIZE);
  190. }
  191. void *um_virt_to_phys(struct task_struct *task, unsigned long addr,
  192. pte_t *pte_out)
  193. {
  194. pgd_t *pgd;
  195. pud_t *pud;
  196. pmd_t *pmd;
  197. pte_t *pte;
  198. pte_t ptent;
  199. if(task->mm == NULL)
  200. return(ERR_PTR(-EINVAL));
  201. pgd = pgd_offset(task->mm, addr);
  202. if(!pgd_present(*pgd))
  203. return(ERR_PTR(-EINVAL));
  204. pud = pud_offset(pgd, addr);
  205. if(!pud_present(*pud))
  206. return(ERR_PTR(-EINVAL));
  207. pmd = pmd_offset(pud, addr);
  208. if(!pmd_present(*pmd))
  209. return(ERR_PTR(-EINVAL));
  210. pte = pte_offset_kernel(pmd, addr);
  211. ptent = *pte;
  212. if(!pte_present(ptent))
  213. return(ERR_PTR(-EINVAL));
  214. if(pte_out != NULL)
  215. *pte_out = ptent;
  216. return((void *) (pte_val(ptent) & PAGE_MASK) + (addr & ~PAGE_MASK));
  217. }
  218. char *current_cmd(void)
  219. {
  220. #if defined(CONFIG_SMP) || defined(CONFIG_HIGHMEM)
  221. return("(Unknown)");
  222. #else
  223. void *addr = um_virt_to_phys(current, current->mm->arg_start, NULL);
  224. return IS_ERR(addr) ? "(Unknown)": __va((unsigned long) addr);
  225. #endif
  226. }
  227. void force_sigbus(void)
  228. {
  229. printk(KERN_ERR "Killing pid %d because of a lack of memory\n",
  230. current->pid);
  231. lock_kernel();
  232. sigaddset(&current->pending.signal, SIGBUS);
  233. recalc_sigpending();
  234. current->flags |= PF_SIGNALED;
  235. do_exit(SIGBUS | 0x80);
  236. }
  237. void dump_thread(struct pt_regs *regs, struct user *u)
  238. {
  239. }
  240. void enable_hlt(void)
  241. {
  242. panic("enable_hlt");
  243. }
  244. EXPORT_SYMBOL(enable_hlt);
  245. void disable_hlt(void)
  246. {
  247. panic("disable_hlt");
  248. }
  249. EXPORT_SYMBOL(disable_hlt);
  250. void *um_kmalloc(int size)
  251. {
  252. return kmalloc(size, GFP_KERNEL);
  253. }
  254. void *um_kmalloc_atomic(int size)
  255. {
  256. return kmalloc(size, GFP_ATOMIC);
  257. }
  258. void *um_vmalloc(int size)
  259. {
  260. return vmalloc(size);
  261. }
  262. void *um_vmalloc_atomic(int size)
  263. {
  264. return __vmalloc(size, GFP_ATOMIC | __GFP_HIGHMEM, PAGE_KERNEL);
  265. }
  266. int __cant_sleep(void) {
  267. return in_atomic() || irqs_disabled() || in_interrupt();
  268. /* Is in_interrupt() really needed? */
  269. }
  270. unsigned long get_fault_addr(void)
  271. {
  272. return((unsigned long) current->thread.fault_addr);
  273. }
  274. EXPORT_SYMBOL(get_fault_addr);
  275. void not_implemented(void)
  276. {
  277. printk(KERN_DEBUG "Something isn't implemented in here\n");
  278. }
  279. EXPORT_SYMBOL(not_implemented);
  280. int user_context(unsigned long sp)
  281. {
  282. unsigned long stack;
  283. stack = sp & (PAGE_MASK << CONFIG_KERNEL_STACK_ORDER);
  284. return(stack != (unsigned long) current_thread);
  285. }
  286. extern exitcall_t __uml_exitcall_begin, __uml_exitcall_end;
  287. void do_uml_exitcalls(void)
  288. {
  289. exitcall_t *call;
  290. call = &__uml_exitcall_end;
  291. while (--call >= &__uml_exitcall_begin)
  292. (*call)();
  293. }
  294. char *uml_strdup(char *string)
  295. {
  296. return kstrdup(string, GFP_KERNEL);
  297. }
  298. int copy_to_user_proc(void __user *to, void *from, int size)
  299. {
  300. return(copy_to_user(to, from, size));
  301. }
  302. int copy_from_user_proc(void *to, void __user *from, int size)
  303. {
  304. return(copy_from_user(to, from, size));
  305. }
  306. int clear_user_proc(void __user *buf, int size)
  307. {
  308. return(clear_user(buf, size));
  309. }
  310. int strlen_user_proc(char __user *str)
  311. {
  312. return(strlen_user(str));
  313. }
  314. int smp_sigio_handler(void)
  315. {
  316. #ifdef CONFIG_SMP
  317. int cpu = current_thread->cpu;
  318. IPI_handler(cpu);
  319. if(cpu != 0)
  320. return(1);
  321. #endif
  322. return(0);
  323. }
  324. int cpu(void)
  325. {
  326. return(current_thread->cpu);
  327. }
  328. static atomic_t using_sysemu = ATOMIC_INIT(0);
  329. int sysemu_supported;
  330. void set_using_sysemu(int value)
  331. {
  332. if (value > sysemu_supported)
  333. return;
  334. atomic_set(&using_sysemu, value);
  335. }
  336. int get_using_sysemu(void)
  337. {
  338. return atomic_read(&using_sysemu);
  339. }
  340. static int proc_read_sysemu(char *buf, char **start, off_t offset, int size,int *eof, void *data)
  341. {
  342. if (snprintf(buf, size, "%d\n", get_using_sysemu()) < size) /*No overflow*/
  343. *eof = 1;
  344. return strlen(buf);
  345. }
  346. static int proc_write_sysemu(struct file *file,const char __user *buf, unsigned long count,void *data)
  347. {
  348. char tmp[2];
  349. if (copy_from_user(tmp, buf, 1))
  350. return -EFAULT;
  351. if (tmp[0] >= '0' && tmp[0] <= '2')
  352. set_using_sysemu(tmp[0] - '0');
  353. return count; /*We use the first char, but pretend to write everything*/
  354. }
  355. int __init make_proc_sysemu(void)
  356. {
  357. struct proc_dir_entry *ent;
  358. if (!sysemu_supported)
  359. return 0;
  360. ent = create_proc_entry("sysemu", 0600, &proc_root);
  361. if (ent == NULL)
  362. {
  363. printk(KERN_WARNING "Failed to register /proc/sysemu\n");
  364. return(0);
  365. }
  366. ent->read_proc = proc_read_sysemu;
  367. ent->write_proc = proc_write_sysemu;
  368. return 0;
  369. }
  370. late_initcall(make_proc_sysemu);
  371. int singlestepping(void * t)
  372. {
  373. struct task_struct *task = t ? t : current;
  374. if ( ! (task->ptrace & PT_DTRACE) )
  375. return(0);
  376. if (task->thread.singlestep_syscall)
  377. return(1);
  378. return 2;
  379. }
  380. /*
  381. * Only x86 and x86_64 have an arch_align_stack().
  382. * All other arches have "#define arch_align_stack(x) (x)"
  383. * in their asm/system.h
  384. * As this is included in UML from asm-um/system-generic.h,
  385. * we can use it to behave as the subarch does.
  386. */
  387. #ifndef arch_align_stack
  388. unsigned long arch_align_stack(unsigned long sp)
  389. {
  390. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  391. sp -= get_random_int() % 8192;
  392. return sp & ~0xf;
  393. }
  394. #endif