process_kern.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 set_current(void *t)
  104. {
  105. struct task_struct *task = t;
  106. cpu_tasks[task->thread_info->cpu] = ((struct cpu_task)
  107. { external_pid(task), task });
  108. }
  109. void *_switch_to(void *prev, void *next, void *last)
  110. {
  111. return(CHOOSE_MODE(switch_to_tt(prev, next),
  112. switch_to_skas(prev, next)));
  113. }
  114. void interrupt_end(void)
  115. {
  116. if(need_resched()) schedule();
  117. if(test_tsk_thread_flag(current, TIF_SIGPENDING)) do_signal();
  118. }
  119. void release_thread(struct task_struct *task)
  120. {
  121. CHOOSE_MODE(release_thread_tt(task), release_thread_skas(task));
  122. }
  123. void exit_thread(void)
  124. {
  125. CHOOSE_MODE(exit_thread_tt(), exit_thread_skas());
  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. uml_idle_timer();
  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. SET_PRI(current);
  165. /*
  166. * although we are an idle CPU, we do not want to
  167. * get into the scheduler unnecessarily.
  168. */
  169. if(need_resched())
  170. schedule();
  171. idle_sleep(10);
  172. }
  173. }
  174. void cpu_idle(void)
  175. {
  176. CHOOSE_MODE(init_idle_tt(), init_idle_skas());
  177. }
  178. int page_size(void)
  179. {
  180. return(PAGE_SIZE);
  181. }
  182. unsigned long page_mask(void)
  183. {
  184. return(PAGE_MASK);
  185. }
  186. void *um_virt_to_phys(struct task_struct *task, unsigned long addr,
  187. pte_t *pte_out)
  188. {
  189. pgd_t *pgd;
  190. pud_t *pud;
  191. pmd_t *pmd;
  192. pte_t *pte;
  193. if(task->mm == NULL)
  194. return(ERR_PTR(-EINVAL));
  195. pgd = pgd_offset(task->mm, addr);
  196. if(!pgd_present(*pgd))
  197. return(ERR_PTR(-EINVAL));
  198. pud = pud_offset(pgd, addr);
  199. if(!pud_present(*pud))
  200. return(ERR_PTR(-EINVAL));
  201. pmd = pmd_offset(pud, addr);
  202. if(!pmd_present(*pmd))
  203. return(ERR_PTR(-EINVAL));
  204. pte = pte_offset_kernel(pmd, addr);
  205. if(!pte_present(*pte))
  206. return(ERR_PTR(-EINVAL));
  207. if(pte_out != NULL)
  208. *pte_out = *pte;
  209. return((void *) (pte_val(*pte) & PAGE_MASK) + (addr & ~PAGE_MASK));
  210. }
  211. char *current_cmd(void)
  212. {
  213. #if defined(CONFIG_SMP) || defined(CONFIG_HIGHMEM)
  214. return("(Unknown)");
  215. #else
  216. void *addr = um_virt_to_phys(current, current->mm->arg_start, NULL);
  217. return IS_ERR(addr) ? "(Unknown)": __va((unsigned long) addr);
  218. #endif
  219. }
  220. void force_sigbus(void)
  221. {
  222. printk(KERN_ERR "Killing pid %d because of a lack of memory\n",
  223. current->pid);
  224. lock_kernel();
  225. sigaddset(&current->pending.signal, SIGBUS);
  226. recalc_sigpending();
  227. current->flags |= PF_SIGNALED;
  228. do_exit(SIGBUS | 0x80);
  229. }
  230. void dump_thread(struct pt_regs *regs, struct user *u)
  231. {
  232. }
  233. void enable_hlt(void)
  234. {
  235. panic("enable_hlt");
  236. }
  237. EXPORT_SYMBOL(enable_hlt);
  238. void disable_hlt(void)
  239. {
  240. panic("disable_hlt");
  241. }
  242. EXPORT_SYMBOL(disable_hlt);
  243. void *um_kmalloc(int size)
  244. {
  245. return(kmalloc(size, GFP_KERNEL));
  246. }
  247. void *um_kmalloc_atomic(int size)
  248. {
  249. return(kmalloc(size, GFP_ATOMIC));
  250. }
  251. void *um_vmalloc(int size)
  252. {
  253. return(vmalloc(size));
  254. }
  255. unsigned long get_fault_addr(void)
  256. {
  257. return((unsigned long) current->thread.fault_addr);
  258. }
  259. EXPORT_SYMBOL(get_fault_addr);
  260. void not_implemented(void)
  261. {
  262. printk(KERN_DEBUG "Something isn't implemented in here\n");
  263. }
  264. EXPORT_SYMBOL(not_implemented);
  265. int user_context(unsigned long sp)
  266. {
  267. unsigned long stack;
  268. stack = sp & (PAGE_MASK << CONFIG_KERNEL_STACK_ORDER);
  269. return(stack != (unsigned long) current_thread);
  270. }
  271. extern void remove_umid_dir(void);
  272. __uml_exitcall(remove_umid_dir);
  273. extern exitcall_t __uml_exitcall_begin, __uml_exitcall_end;
  274. void do_uml_exitcalls(void)
  275. {
  276. exitcall_t *call;
  277. call = &__uml_exitcall_end;
  278. while (--call >= &__uml_exitcall_begin)
  279. (*call)();
  280. }
  281. char *uml_strdup(char *string)
  282. {
  283. char *new;
  284. new = kmalloc(strlen(string) + 1, GFP_KERNEL);
  285. if(new == NULL) return(NULL);
  286. strcpy(new, string);
  287. return(new);
  288. }
  289. void *get_init_task(void)
  290. {
  291. return(&init_thread_union.thread_info.task);
  292. }
  293. int copy_to_user_proc(void __user *to, void *from, int size)
  294. {
  295. return(copy_to_user(to, from, size));
  296. }
  297. int copy_from_user_proc(void *to, void __user *from, int size)
  298. {
  299. return(copy_from_user(to, from, size));
  300. }
  301. int clear_user_proc(void __user *buf, int size)
  302. {
  303. return(clear_user(buf, size));
  304. }
  305. int strlen_user_proc(char __user *str)
  306. {
  307. return(strlen_user(str));
  308. }
  309. int smp_sigio_handler(void)
  310. {
  311. #ifdef CONFIG_SMP
  312. int cpu = current_thread->cpu;
  313. IPI_handler(cpu);
  314. if(cpu != 0)
  315. return(1);
  316. #endif
  317. return(0);
  318. }
  319. int um_in_interrupt(void)
  320. {
  321. return(in_interrupt());
  322. }
  323. int cpu(void)
  324. {
  325. return(current_thread->cpu);
  326. }
  327. static atomic_t using_sysemu = ATOMIC_INIT(0);
  328. int sysemu_supported;
  329. void set_using_sysemu(int value)
  330. {
  331. if (value > sysemu_supported)
  332. return;
  333. atomic_set(&using_sysemu, value);
  334. }
  335. int get_using_sysemu(void)
  336. {
  337. return atomic_read(&using_sysemu);
  338. }
  339. static int proc_read_sysemu(char *buf, char **start, off_t offset, int size,int *eof, void *data)
  340. {
  341. if (snprintf(buf, size, "%d\n", get_using_sysemu()) < size) /*No overflow*/
  342. *eof = 1;
  343. return strlen(buf);
  344. }
  345. static int proc_write_sysemu(struct file *file,const char *buf, unsigned long count,void *data)
  346. {
  347. char tmp[2];
  348. if (copy_from_user(tmp, buf, 1))
  349. return -EFAULT;
  350. if (tmp[0] >= '0' && tmp[0] <= '2')
  351. set_using_sysemu(tmp[0] - '0');
  352. return count; /*We use the first char, but pretend to write everything*/
  353. }
  354. int __init make_proc_sysemu(void)
  355. {
  356. struct proc_dir_entry *ent;
  357. if (!sysemu_supported)
  358. return 0;
  359. ent = create_proc_entry("sysemu", 0600, &proc_root);
  360. if (ent == NULL)
  361. {
  362. printk("Failed to register /proc/sysemu\n");
  363. return(0);
  364. }
  365. ent->read_proc = proc_read_sysemu;
  366. ent->write_proc = proc_write_sysemu;
  367. return 0;
  368. }
  369. late_initcall(make_proc_sysemu);
  370. int singlestepping(void * t)
  371. {
  372. struct task_struct *task = t ? t : current;
  373. if ( ! (task->ptrace & PT_DTRACE) )
  374. return(0);
  375. if (task->thread.singlestep_syscall)
  376. return(1);
  377. return 2;
  378. }
  379. unsigned long arch_align_stack(unsigned long sp)
  380. {
  381. if (randomize_va_space)
  382. sp -= get_random_int() % 8192;
  383. return sp & ~0xf;
  384. }
  385. /*
  386. * Overrides for Emacs so that we follow Linus's tabbing style.
  387. * Emacs will notice this stuff at the end of the file and automatically
  388. * adjust the settings for this buffer only. This must remain at the end
  389. * of the file.
  390. * ---------------------------------------------------------------------------
  391. * Local variables:
  392. * c-file-style: "linux"
  393. * End:
  394. */