process_kern.c 9.2 KB

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