process_32.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. * Copyright (C) 1995 Linus Torvalds
  3. *
  4. * Pentium III FXSR, SSE support
  5. * Gareth Hughes <gareth@valinux.com>, May 2000
  6. */
  7. /*
  8. * This file handles the architecture-dependent parts of process handling..
  9. */
  10. #include <linux/stackprotector.h>
  11. #include <linux/cpu.h>
  12. #include <linux/errno.h>
  13. #include <linux/sched.h>
  14. #include <linux/fs.h>
  15. #include <linux/kernel.h>
  16. #include <linux/mm.h>
  17. #include <linux/elfcore.h>
  18. #include <linux/smp.h>
  19. #include <linux/stddef.h>
  20. #include <linux/slab.h>
  21. #include <linux/vmalloc.h>
  22. #include <linux/user.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/utsname.h>
  25. #include <linux/delay.h>
  26. #include <linux/reboot.h>
  27. #include <linux/init.h>
  28. #include <linux/mc146818rtc.h>
  29. #include <linux/module.h>
  30. #include <linux/kallsyms.h>
  31. #include <linux/ptrace.h>
  32. #include <linux/personality.h>
  33. #include <linux/tick.h>
  34. #include <linux/percpu.h>
  35. #include <linux/prctl.h>
  36. #include <linux/dmi.h>
  37. #include <linux/ftrace.h>
  38. #include <linux/uaccess.h>
  39. #include <linux/io.h>
  40. #include <linux/kdebug.h>
  41. #include <asm/pgtable.h>
  42. #include <asm/system.h>
  43. #include <asm/ldt.h>
  44. #include <asm/processor.h>
  45. #include <asm/i387.h>
  46. #include <asm/desc.h>
  47. #ifdef CONFIG_MATH_EMULATION
  48. #include <asm/math_emu.h>
  49. #endif
  50. #include <linux/err.h>
  51. #include <asm/tlbflush.h>
  52. #include <asm/cpu.h>
  53. #include <asm/idle.h>
  54. #include <asm/syscalls.h>
  55. #include <asm/ds.h>
  56. asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
  57. DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
  58. EXPORT_PER_CPU_SYMBOL(current_task);
  59. /*
  60. * Return saved PC of a blocked thread.
  61. */
  62. unsigned long thread_saved_pc(struct task_struct *tsk)
  63. {
  64. return ((unsigned long *)tsk->thread.sp)[3];
  65. }
  66. #ifndef CONFIG_SMP
  67. static inline void play_dead(void)
  68. {
  69. BUG();
  70. }
  71. #endif
  72. /*
  73. * The idle thread. There's no useful work to be
  74. * done, so just try to conserve power and have a
  75. * low exit latency (ie sit in a loop waiting for
  76. * somebody to say that they'd like to reschedule)
  77. */
  78. void cpu_idle(void)
  79. {
  80. int cpu = smp_processor_id();
  81. /*
  82. * If we're the non-boot CPU, nothing set the stack canary up
  83. * for us. CPU0 already has it initialized but no harm in
  84. * doing it again. This is a good place for updating it, as
  85. * we wont ever return from this function (so the invalid
  86. * canaries already on the stack wont ever trigger).
  87. */
  88. boot_init_stack_canary();
  89. current_thread_info()->status |= TS_POLLING;
  90. /* endless idle loop with no priority at all */
  91. while (1) {
  92. tick_nohz_stop_sched_tick(1);
  93. while (!need_resched()) {
  94. check_pgt_cache();
  95. rmb();
  96. if (cpu_is_offline(cpu))
  97. play_dead();
  98. local_irq_disable();
  99. /* Don't trace irqs off for idle */
  100. stop_critical_timings();
  101. pm_idle();
  102. start_critical_timings();
  103. }
  104. tick_nohz_restart_sched_tick();
  105. preempt_enable_no_resched();
  106. schedule();
  107. preempt_disable();
  108. }
  109. }
  110. void __show_regs(struct pt_regs *regs, int all)
  111. {
  112. unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L;
  113. unsigned long d0, d1, d2, d3, d6, d7;
  114. unsigned long sp;
  115. unsigned short ss, gs;
  116. const char *board;
  117. if (user_mode_vm(regs)) {
  118. sp = regs->sp;
  119. ss = regs->ss & 0xffff;
  120. gs = get_user_gs(regs);
  121. } else {
  122. sp = (unsigned long) (&regs->sp);
  123. savesegment(ss, ss);
  124. savesegment(gs, gs);
  125. }
  126. printk("\n");
  127. board = dmi_get_system_info(DMI_PRODUCT_NAME);
  128. if (!board)
  129. board = "";
  130. printk("Pid: %d, comm: %s %s (%s %.*s) %s\n",
  131. task_pid_nr(current), current->comm,
  132. print_tainted(), init_utsname()->release,
  133. (int)strcspn(init_utsname()->version, " "),
  134. init_utsname()->version, board);
  135. printk("EIP: %04x:[<%08lx>] EFLAGS: %08lx CPU: %d\n",
  136. (u16)regs->cs, regs->ip, regs->flags,
  137. smp_processor_id());
  138. print_symbol("EIP is at %s\n", regs->ip);
  139. printk("EAX: %08lx EBX: %08lx ECX: %08lx EDX: %08lx\n",
  140. regs->ax, regs->bx, regs->cx, regs->dx);
  141. printk("ESI: %08lx EDI: %08lx EBP: %08lx ESP: %08lx\n",
  142. regs->si, regs->di, regs->bp, sp);
  143. printk(" DS: %04x ES: %04x FS: %04x GS: %04x SS: %04x\n",
  144. (u16)regs->ds, (u16)regs->es, (u16)regs->fs, gs, ss);
  145. if (!all)
  146. return;
  147. cr0 = read_cr0();
  148. cr2 = read_cr2();
  149. cr3 = read_cr3();
  150. cr4 = read_cr4_safe();
  151. printk("CR0: %08lx CR2: %08lx CR3: %08lx CR4: %08lx\n",
  152. cr0, cr2, cr3, cr4);
  153. get_debugreg(d0, 0);
  154. get_debugreg(d1, 1);
  155. get_debugreg(d2, 2);
  156. get_debugreg(d3, 3);
  157. printk("DR0: %08lx DR1: %08lx DR2: %08lx DR3: %08lx\n",
  158. d0, d1, d2, d3);
  159. get_debugreg(d6, 6);
  160. get_debugreg(d7, 7);
  161. printk("DR6: %08lx DR7: %08lx\n",
  162. d6, d7);
  163. }
  164. void show_regs(struct pt_regs *regs)
  165. {
  166. __show_regs(regs, 1);
  167. show_trace(NULL, regs, &regs->sp, regs->bp);
  168. }
  169. /*
  170. * This gets run with %bx containing the
  171. * function to call, and %dx containing
  172. * the "args".
  173. */
  174. extern void kernel_thread_helper(void);
  175. /*
  176. * Create a kernel thread
  177. */
  178. int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
  179. {
  180. struct pt_regs regs;
  181. memset(&regs, 0, sizeof(regs));
  182. regs.bx = (unsigned long) fn;
  183. regs.dx = (unsigned long) arg;
  184. regs.ds = __USER_DS;
  185. regs.es = __USER_DS;
  186. regs.fs = __KERNEL_PERCPU;
  187. regs.gs = __KERNEL_STACK_CANARY;
  188. regs.orig_ax = -1;
  189. regs.ip = (unsigned long) kernel_thread_helper;
  190. regs.cs = __KERNEL_CS | get_kernel_rpl();
  191. regs.flags = X86_EFLAGS_IF | X86_EFLAGS_SF | X86_EFLAGS_PF | 0x2;
  192. /* Ok, create the new process.. */
  193. return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
  194. }
  195. EXPORT_SYMBOL(kernel_thread);
  196. void release_thread(struct task_struct *dead_task)
  197. {
  198. BUG_ON(dead_task->mm);
  199. release_vm86_irqs(dead_task);
  200. }
  201. /*
  202. * This gets called before we allocate a new thread and copy
  203. * the current task into it.
  204. */
  205. void prepare_to_copy(struct task_struct *tsk)
  206. {
  207. unlazy_fpu(tsk);
  208. }
  209. int copy_thread(unsigned long clone_flags, unsigned long sp,
  210. unsigned long unused,
  211. struct task_struct *p, struct pt_regs *regs)
  212. {
  213. struct pt_regs *childregs;
  214. struct task_struct *tsk;
  215. int err;
  216. childregs = task_pt_regs(p);
  217. *childregs = *regs;
  218. childregs->ax = 0;
  219. childregs->sp = sp;
  220. p->thread.sp = (unsigned long) childregs;
  221. p->thread.sp0 = (unsigned long) (childregs+1);
  222. p->thread.ip = (unsigned long) ret_from_fork;
  223. task_user_gs(p) = get_user_gs(regs);
  224. tsk = current;
  225. if (unlikely(test_tsk_thread_flag(tsk, TIF_IO_BITMAP))) {
  226. p->thread.io_bitmap_ptr = kmemdup(tsk->thread.io_bitmap_ptr,
  227. IO_BITMAP_BYTES, GFP_KERNEL);
  228. if (!p->thread.io_bitmap_ptr) {
  229. p->thread.io_bitmap_max = 0;
  230. return -ENOMEM;
  231. }
  232. set_tsk_thread_flag(p, TIF_IO_BITMAP);
  233. }
  234. err = 0;
  235. /*
  236. * Set a new TLS for the child thread?
  237. */
  238. if (clone_flags & CLONE_SETTLS)
  239. err = do_set_thread_area(p, -1,
  240. (struct user_desc __user *)childregs->si, 0);
  241. if (err && p->thread.io_bitmap_ptr) {
  242. kfree(p->thread.io_bitmap_ptr);
  243. p->thread.io_bitmap_max = 0;
  244. }
  245. clear_tsk_thread_flag(p, TIF_DS_AREA_MSR);
  246. p->thread.ds_ctx = NULL;
  247. clear_tsk_thread_flag(p, TIF_DEBUGCTLMSR);
  248. p->thread.debugctlmsr = 0;
  249. return err;
  250. }
  251. void
  252. start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
  253. {
  254. set_user_gs(regs, 0);
  255. regs->fs = 0;
  256. set_fs(USER_DS);
  257. regs->ds = __USER_DS;
  258. regs->es = __USER_DS;
  259. regs->ss = __USER_DS;
  260. regs->cs = __USER_CS;
  261. regs->ip = new_ip;
  262. regs->sp = new_sp;
  263. /*
  264. * Free the old FP and other extended state
  265. */
  266. free_thread_xstate(current);
  267. }
  268. EXPORT_SYMBOL_GPL(start_thread);
  269. /*
  270. * switch_to(x,yn) should switch tasks from x to y.
  271. *
  272. * We fsave/fwait so that an exception goes off at the right time
  273. * (as a call from the fsave or fwait in effect) rather than to
  274. * the wrong process. Lazy FP saving no longer makes any sense
  275. * with modern CPU's, and this simplifies a lot of things (SMP
  276. * and UP become the same).
  277. *
  278. * NOTE! We used to use the x86 hardware context switching. The
  279. * reason for not using it any more becomes apparent when you
  280. * try to recover gracefully from saved state that is no longer
  281. * valid (stale segment register values in particular). With the
  282. * hardware task-switch, there is no way to fix up bad state in
  283. * a reasonable manner.
  284. *
  285. * The fact that Intel documents the hardware task-switching to
  286. * be slow is a fairly red herring - this code is not noticeably
  287. * faster. However, there _is_ some room for improvement here,
  288. * so the performance issues may eventually be a valid point.
  289. * More important, however, is the fact that this allows us much
  290. * more flexibility.
  291. *
  292. * The return value (in %ax) will be the "prev" task after
  293. * the task-switch, and shows up in ret_from_fork in entry.S,
  294. * for example.
  295. */
  296. __notrace_funcgraph struct task_struct *
  297. __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
  298. {
  299. struct thread_struct *prev = &prev_p->thread,
  300. *next = &next_p->thread;
  301. int cpu = smp_processor_id();
  302. struct tss_struct *tss = &per_cpu(init_tss, cpu);
  303. /* never put a printk in __switch_to... printk() calls wake_up*() indirectly */
  304. __unlazy_fpu(prev_p);
  305. /* we're going to use this soon, after a few expensive things */
  306. if (next_p->fpu_counter > 5)
  307. prefetch(next->xstate);
  308. /*
  309. * Reload esp0.
  310. */
  311. load_sp0(tss, next);
  312. /*
  313. * Save away %gs. No need to save %fs, as it was saved on the
  314. * stack on entry. No need to save %es and %ds, as those are
  315. * always kernel segments while inside the kernel. Doing this
  316. * before setting the new TLS descriptors avoids the situation
  317. * where we temporarily have non-reloadable segments in %fs
  318. * and %gs. This could be an issue if the NMI handler ever
  319. * used %fs or %gs (it does not today), or if the kernel is
  320. * running inside of a hypervisor layer.
  321. */
  322. lazy_save_gs(prev->gs);
  323. /*
  324. * Load the per-thread Thread-Local Storage descriptor.
  325. */
  326. load_TLS(next, cpu);
  327. /*
  328. * Restore IOPL if needed. In normal use, the flags restore
  329. * in the switch assembly will handle this. But if the kernel
  330. * is running virtualized at a non-zero CPL, the popf will
  331. * not restore flags, so it must be done in a separate step.
  332. */
  333. if (get_kernel_rpl() && unlikely(prev->iopl != next->iopl))
  334. set_iopl_mask(next->iopl);
  335. /*
  336. * Now maybe handle debug registers and/or IO bitmaps
  337. */
  338. if (unlikely(task_thread_info(prev_p)->flags & _TIF_WORK_CTXSW_PREV ||
  339. task_thread_info(next_p)->flags & _TIF_WORK_CTXSW_NEXT))
  340. __switch_to_xtra(prev_p, next_p, tss);
  341. /*
  342. * Leave lazy mode, flushing any hypercalls made here.
  343. * This must be done before restoring TLS segments so
  344. * the GDT and LDT are properly updated, and must be
  345. * done before math_state_restore, so the TS bit is up
  346. * to date.
  347. */
  348. arch_end_context_switch(next_p);
  349. /* If the task has used fpu the last 5 timeslices, just do a full
  350. * restore of the math state immediately to avoid the trap; the
  351. * chances of needing FPU soon are obviously high now
  352. *
  353. * tsk_used_math() checks prevent calling math_state_restore(),
  354. * which can sleep in the case of !tsk_used_math()
  355. */
  356. if (tsk_used_math(next_p) && next_p->fpu_counter > 5)
  357. math_state_restore();
  358. /*
  359. * Restore %gs if needed (which is common)
  360. */
  361. if (prev->gs | next->gs)
  362. lazy_load_gs(next->gs);
  363. percpu_write(current_task, next_p);
  364. return prev_p;
  365. }
  366. int sys_clone(struct pt_regs *regs)
  367. {
  368. unsigned long clone_flags;
  369. unsigned long newsp;
  370. int __user *parent_tidptr, *child_tidptr;
  371. clone_flags = regs->bx;
  372. newsp = regs->cx;
  373. parent_tidptr = (int __user *)regs->dx;
  374. child_tidptr = (int __user *)regs->di;
  375. if (!newsp)
  376. newsp = regs->sp;
  377. return do_fork(clone_flags, newsp, regs, 0, parent_tidptr, child_tidptr);
  378. }
  379. /*
  380. * sys_execve() executes a new program.
  381. */
  382. int sys_execve(struct pt_regs *regs)
  383. {
  384. int error;
  385. char *filename;
  386. filename = getname((char __user *) regs->bx);
  387. error = PTR_ERR(filename);
  388. if (IS_ERR(filename))
  389. goto out;
  390. error = do_execve(filename,
  391. (char __user * __user *) regs->cx,
  392. (char __user * __user *) regs->dx,
  393. regs);
  394. if (error == 0) {
  395. /* Make sure we don't return using sysenter.. */
  396. set_thread_flag(TIF_IRET);
  397. }
  398. putname(filename);
  399. out:
  400. return error;
  401. }
  402. #define top_esp (THREAD_SIZE - sizeof(unsigned long))
  403. #define top_ebp (THREAD_SIZE - 2*sizeof(unsigned long))
  404. unsigned long get_wchan(struct task_struct *p)
  405. {
  406. unsigned long bp, sp, ip;
  407. unsigned long stack_page;
  408. int count = 0;
  409. if (!p || p == current || p->state == TASK_RUNNING)
  410. return 0;
  411. stack_page = (unsigned long)task_stack_page(p);
  412. sp = p->thread.sp;
  413. if (!stack_page || sp < stack_page || sp > top_esp+stack_page)
  414. return 0;
  415. /* include/asm-i386/system.h:switch_to() pushes bp last. */
  416. bp = *(unsigned long *) sp;
  417. do {
  418. if (bp < stack_page || bp > top_ebp+stack_page)
  419. return 0;
  420. ip = *(unsigned long *) (bp+4);
  421. if (!in_sched_functions(ip))
  422. return ip;
  423. bp = *(unsigned long *) bp;
  424. } while (count++ < 16);
  425. return 0;
  426. }