process_kern.c 8.8 KB

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