process_kern.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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 "2_5compat.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. struct task_struct *get_task(int pid, int require)
  56. {
  57. struct task_struct *ret;
  58. read_lock(&tasklist_lock);
  59. ret = find_task_by_pid(pid);
  60. read_unlock(&tasklist_lock);
  61. if(require && (ret == NULL)) panic("get_task couldn't find a task\n");
  62. return(ret);
  63. }
  64. int external_pid(void *t)
  65. {
  66. struct task_struct *task = t ? t : current;
  67. return(CHOOSE_MODE_PROC(external_pid_tt, external_pid_skas, task));
  68. }
  69. int pid_to_processor_id(int pid)
  70. {
  71. int i;
  72. for(i = 0; i < ncpus; i++){
  73. if(cpu_tasks[i].pid == pid) return(i);
  74. }
  75. return(-1);
  76. }
  77. void free_stack(unsigned long stack, int order)
  78. {
  79. free_pages(stack, order);
  80. }
  81. unsigned long alloc_stack(int order, int atomic)
  82. {
  83. unsigned long page;
  84. int flags = GFP_KERNEL;
  85. if(atomic) flags |= GFP_ATOMIC;
  86. page = __get_free_pages(flags, order);
  87. if(page == 0)
  88. return(0);
  89. stack_protections(page);
  90. return(page);
  91. }
  92. int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
  93. {
  94. int pid;
  95. current->thread.request.u.thread.proc = fn;
  96. current->thread.request.u.thread.arg = arg;
  97. pid = do_fork(CLONE_VM | CLONE_UNTRACED | flags, 0, NULL, 0, NULL,
  98. NULL);
  99. if(pid < 0)
  100. panic("do_fork failed in kernel_thread, errno = %d", pid);
  101. return(pid);
  102. }
  103. void switch_mm(struct mm_struct *prev, struct mm_struct *next,
  104. struct task_struct *tsk)
  105. {
  106. int cpu = smp_processor_id();
  107. if (prev != next)
  108. cpu_clear(cpu, prev->cpu_vm_mask);
  109. cpu_set(cpu, next->cpu_vm_mask);
  110. }
  111. void set_current(void *t)
  112. {
  113. struct task_struct *task = t;
  114. cpu_tasks[task->thread_info->cpu] = ((struct cpu_task)
  115. { external_pid(task), task });
  116. }
  117. void *_switch_to(void *prev, void *next, void *last)
  118. {
  119. return(CHOOSE_MODE(switch_to_tt(prev, next),
  120. switch_to_skas(prev, next)));
  121. }
  122. void interrupt_end(void)
  123. {
  124. if(need_resched()) schedule();
  125. if(test_tsk_thread_flag(current, TIF_SIGPENDING)) do_signal();
  126. }
  127. void release_thread(struct task_struct *task)
  128. {
  129. CHOOSE_MODE(release_thread_tt(task), release_thread_skas(task));
  130. }
  131. void exit_thread(void)
  132. {
  133. CHOOSE_MODE(exit_thread_tt(), exit_thread_skas());
  134. unprotect_stack((unsigned long) current_thread);
  135. }
  136. void *get_current(void)
  137. {
  138. return(current);
  139. }
  140. int copy_thread(int nr, unsigned long clone_flags, unsigned long sp,
  141. unsigned long stack_top, struct task_struct * p,
  142. struct pt_regs *regs)
  143. {
  144. p->thread = (struct thread_struct) INIT_THREAD;
  145. return(CHOOSE_MODE_PROC(copy_thread_tt, copy_thread_skas, nr,
  146. clone_flags, sp, stack_top, p, regs));
  147. }
  148. void initial_thread_cb(void (*proc)(void *), void *arg)
  149. {
  150. int save_kmalloc_ok = kmalloc_ok;
  151. kmalloc_ok = 0;
  152. CHOOSE_MODE_PROC(initial_thread_cb_tt, initial_thread_cb_skas, proc,
  153. arg);
  154. kmalloc_ok = save_kmalloc_ok;
  155. }
  156. unsigned long stack_sp(unsigned long page)
  157. {
  158. return(page + PAGE_SIZE - sizeof(void *));
  159. }
  160. int current_pid(void)
  161. {
  162. return(current->pid);
  163. }
  164. void default_idle(void)
  165. {
  166. uml_idle_timer();
  167. atomic_inc(&init_mm.mm_count);
  168. current->mm = &init_mm;
  169. current->active_mm = &init_mm;
  170. while(1){
  171. /* endless idle loop with no priority at all */
  172. SET_PRI(current);
  173. /*
  174. * although we are an idle CPU, we do not want to
  175. * get into the scheduler unnecessarily.
  176. */
  177. if(need_resched())
  178. schedule();
  179. idle_sleep(10);
  180. }
  181. }
  182. void cpu_idle(void)
  183. {
  184. CHOOSE_MODE(init_idle_tt(), init_idle_skas());
  185. }
  186. int page_size(void)
  187. {
  188. return(PAGE_SIZE);
  189. }
  190. unsigned long page_mask(void)
  191. {
  192. return(PAGE_MASK);
  193. }
  194. void *um_virt_to_phys(struct task_struct *task, unsigned long addr,
  195. pte_t *pte_out)
  196. {
  197. pgd_t *pgd;
  198. pud_t *pud;
  199. pmd_t *pmd;
  200. pte_t *pte;
  201. if(task->mm == NULL)
  202. return(ERR_PTR(-EINVAL));
  203. pgd = pgd_offset(task->mm, addr);
  204. if(!pgd_present(*pgd))
  205. return(ERR_PTR(-EINVAL));
  206. pud = pud_offset(pgd, addr);
  207. if(!pud_present(*pud))
  208. return(ERR_PTR(-EINVAL));
  209. pmd = pmd_offset(pud, addr);
  210. if(!pmd_present(*pmd))
  211. return(ERR_PTR(-EINVAL));
  212. pte = pte_offset_kernel(pmd, addr);
  213. if(!pte_present(*pte))
  214. return(ERR_PTR(-EINVAL));
  215. if(pte_out != NULL)
  216. *pte_out = *pte;
  217. return((void *) (pte_val(*pte) & PAGE_MASK) + (addr & ~PAGE_MASK));
  218. }
  219. char *current_cmd(void)
  220. {
  221. #if defined(CONFIG_SMP) || defined(CONFIG_HIGHMEM)
  222. return("(Unknown)");
  223. #else
  224. void *addr = um_virt_to_phys(current, current->mm->arg_start, NULL);
  225. return IS_ERR(addr) ? "(Unknown)": __va((unsigned long) addr);
  226. #endif
  227. }
  228. void force_sigbus(void)
  229. {
  230. printk(KERN_ERR "Killing pid %d because of a lack of memory\n",
  231. current->pid);
  232. lock_kernel();
  233. sigaddset(&current->pending.signal, SIGBUS);
  234. recalc_sigpending();
  235. current->flags |= PF_SIGNALED;
  236. do_exit(SIGBUS | 0x80);
  237. }
  238. void dump_thread(struct pt_regs *regs, struct user *u)
  239. {
  240. }
  241. void enable_hlt(void)
  242. {
  243. panic("enable_hlt");
  244. }
  245. EXPORT_SYMBOL(enable_hlt);
  246. void disable_hlt(void)
  247. {
  248. panic("disable_hlt");
  249. }
  250. EXPORT_SYMBOL(disable_hlt);
  251. void *um_kmalloc(int size)
  252. {
  253. return(kmalloc(size, GFP_KERNEL));
  254. }
  255. void *um_kmalloc_atomic(int size)
  256. {
  257. return(kmalloc(size, GFP_ATOMIC));
  258. }
  259. void *um_vmalloc(int size)
  260. {
  261. return(vmalloc(size));
  262. }
  263. unsigned long get_fault_addr(void)
  264. {
  265. return((unsigned long) current->thread.fault_addr);
  266. }
  267. EXPORT_SYMBOL(get_fault_addr);
  268. void not_implemented(void)
  269. {
  270. printk(KERN_DEBUG "Something isn't implemented in here\n");
  271. }
  272. EXPORT_SYMBOL(not_implemented);
  273. int user_context(unsigned long sp)
  274. {
  275. unsigned long stack;
  276. stack = sp & (PAGE_MASK << CONFIG_KERNEL_STACK_ORDER);
  277. return(stack != (unsigned long) current_thread);
  278. }
  279. extern void remove_umid_dir(void);
  280. __uml_exitcall(remove_umid_dir);
  281. extern exitcall_t __uml_exitcall_begin, __uml_exitcall_end;
  282. void do_uml_exitcalls(void)
  283. {
  284. exitcall_t *call;
  285. call = &__uml_exitcall_end;
  286. while (--call >= &__uml_exitcall_begin)
  287. (*call)();
  288. }
  289. char *uml_strdup(char *string)
  290. {
  291. char *new;
  292. new = kmalloc(strlen(string) + 1, GFP_KERNEL);
  293. if(new == NULL) return(NULL);
  294. strcpy(new, string);
  295. return(new);
  296. }
  297. void *get_init_task(void)
  298. {
  299. return(&init_thread_union.thread_info.task);
  300. }
  301. int copy_to_user_proc(void __user *to, void *from, int size)
  302. {
  303. return(copy_to_user(to, from, size));
  304. }
  305. int copy_from_user_proc(void *to, void __user *from, int size)
  306. {
  307. return(copy_from_user(to, from, size));
  308. }
  309. int clear_user_proc(void __user *buf, int size)
  310. {
  311. return(clear_user(buf, size));
  312. }
  313. int strlen_user_proc(char __user *str)
  314. {
  315. return(strlen_user(str));
  316. }
  317. int smp_sigio_handler(void)
  318. {
  319. #ifdef CONFIG_SMP
  320. int cpu = current_thread->cpu;
  321. IPI_handler(cpu);
  322. if(cpu != 0)
  323. return(1);
  324. #endif
  325. return(0);
  326. }
  327. int um_in_interrupt(void)
  328. {
  329. return(in_interrupt());
  330. }
  331. int cpu(void)
  332. {
  333. return(current_thread->cpu);
  334. }
  335. static atomic_t using_sysemu = ATOMIC_INIT(0);
  336. int sysemu_supported;
  337. void set_using_sysemu(int value)
  338. {
  339. if (value > sysemu_supported)
  340. return;
  341. atomic_set(&using_sysemu, value);
  342. }
  343. int get_using_sysemu(void)
  344. {
  345. return atomic_read(&using_sysemu);
  346. }
  347. static int proc_read_sysemu(char *buf, char **start, off_t offset, int size,int *eof, void *data)
  348. {
  349. if (snprintf(buf, size, "%d\n", get_using_sysemu()) < size) /*No overflow*/
  350. *eof = 1;
  351. return strlen(buf);
  352. }
  353. static int proc_write_sysemu(struct file *file,const char *buf, unsigned long count,void *data)
  354. {
  355. char tmp[2];
  356. if (copy_from_user(tmp, buf, 1))
  357. return -EFAULT;
  358. if (tmp[0] >= '0' && tmp[0] <= '2')
  359. set_using_sysemu(tmp[0] - '0');
  360. return count; /*We use the first char, but pretend to write everything*/
  361. }
  362. int __init make_proc_sysemu(void)
  363. {
  364. struct proc_dir_entry *ent;
  365. if (!sysemu_supported)
  366. return 0;
  367. ent = create_proc_entry("sysemu", 0600, &proc_root);
  368. if (ent == NULL)
  369. {
  370. printk("Failed to register /proc/sysemu\n");
  371. return(0);
  372. }
  373. ent->read_proc = proc_read_sysemu;
  374. ent->write_proc = proc_write_sysemu;
  375. return 0;
  376. }
  377. late_initcall(make_proc_sysemu);
  378. int singlestepping(void * t)
  379. {
  380. struct task_struct *task = t ? t : current;
  381. if ( ! (task->ptrace & PT_DTRACE) )
  382. return(0);
  383. if (task->thread.singlestep_syscall)
  384. return(1);
  385. return 2;
  386. }
  387. unsigned long arch_align_stack(unsigned long sp)
  388. {
  389. if (randomize_va_space)
  390. sp -= get_random_int() % 8192;
  391. return sp & ~0xf;
  392. }
  393. /*
  394. * Overrides for Emacs so that we follow Linus's tabbing style.
  395. * Emacs will notice this stuff at the end of the file and automatically
  396. * adjust the settings for this buffer only. This must remain at the end
  397. * of the file.
  398. * ---------------------------------------------------------------------------
  399. * Local variables:
  400. * c-file-style: "linux"
  401. * End:
  402. */