process_kern.c 9.6 KB

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