process_kern.c 8.8 KB

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