process_32.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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/cpu.h>
  11. #include <linux/errno.h>
  12. #include <linux/sched.h>
  13. #include <linux/fs.h>
  14. #include <linux/kernel.h>
  15. #include <linux/mm.h>
  16. #include <linux/elfcore.h>
  17. #include <linux/smp.h>
  18. #include <linux/stddef.h>
  19. #include <linux/slab.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/user.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/delay.h>
  24. #include <linux/reboot.h>
  25. #include <linux/mc146818rtc.h>
  26. #include <linux/export.h>
  27. #include <linux/kallsyms.h>
  28. #include <linux/ptrace.h>
  29. #include <linux/personality.h>
  30. #include <linux/percpu.h>
  31. #include <linux/prctl.h>
  32. #include <linux/ftrace.h>
  33. #include <linux/uaccess.h>
  34. #include <linux/io.h>
  35. #include <linux/kdebug.h>
  36. #include <asm/pgtable.h>
  37. #include <asm/ldt.h>
  38. #include <asm/processor.h>
  39. #include <asm/fpu/internal.h>
  40. #include <asm/desc.h>
  41. #ifdef CONFIG_MATH_EMULATION
  42. #include <asm/math_emu.h>
  43. #endif
  44. #include <linux/err.h>
  45. #include <asm/tlbflush.h>
  46. #include <asm/cpu.h>
  47. #include <asm/idle.h>
  48. #include <asm/syscalls.h>
  49. #include <asm/debugreg.h>
  50. #include <asm/switch_to.h>
  51. #include <asm/vm86.h>
  52. void __show_regs(struct pt_regs *regs, int all)
  53. {
  54. unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L;
  55. unsigned long d0, d1, d2, d3, d6, d7;
  56. unsigned long sp;
  57. unsigned short ss, gs;
  58. if (user_mode(regs)) {
  59. sp = regs->sp;
  60. ss = regs->ss & 0xffff;
  61. gs = get_user_gs(regs);
  62. } else {
  63. sp = kernel_stack_pointer(regs);
  64. savesegment(ss, ss);
  65. savesegment(gs, gs);
  66. }
  67. printk(KERN_DEFAULT "EIP: %04x:[<%08lx>] EFLAGS: %08lx CPU: %d\n",
  68. (u16)regs->cs, regs->ip, regs->flags,
  69. smp_processor_id());
  70. print_symbol("EIP is at %s\n", regs->ip);
  71. printk(KERN_DEFAULT "EAX: %08lx EBX: %08lx ECX: %08lx EDX: %08lx\n",
  72. regs->ax, regs->bx, regs->cx, regs->dx);
  73. printk(KERN_DEFAULT "ESI: %08lx EDI: %08lx EBP: %08lx ESP: %08lx\n",
  74. regs->si, regs->di, regs->bp, sp);
  75. printk(KERN_DEFAULT " DS: %04x ES: %04x FS: %04x GS: %04x SS: %04x\n",
  76. (u16)regs->ds, (u16)regs->es, (u16)regs->fs, gs, ss);
  77. if (!all)
  78. return;
  79. cr0 = read_cr0();
  80. cr2 = read_cr2();
  81. cr3 = read_cr3();
  82. cr4 = __read_cr4();
  83. printk(KERN_DEFAULT "CR0: %08lx CR2: %08lx CR3: %08lx CR4: %08lx\n",
  84. cr0, cr2, cr3, cr4);
  85. get_debugreg(d0, 0);
  86. get_debugreg(d1, 1);
  87. get_debugreg(d2, 2);
  88. get_debugreg(d3, 3);
  89. get_debugreg(d6, 6);
  90. get_debugreg(d7, 7);
  91. /* Only print out debug registers if they are in their non-default state. */
  92. if ((d0 == 0) && (d1 == 0) && (d2 == 0) && (d3 == 0) &&
  93. (d6 == DR6_RESERVED) && (d7 == 0x400))
  94. return;
  95. printk(KERN_DEFAULT "DR0: %08lx DR1: %08lx DR2: %08lx DR3: %08lx\n",
  96. d0, d1, d2, d3);
  97. printk(KERN_DEFAULT "DR6: %08lx DR7: %08lx\n",
  98. d6, d7);
  99. }
  100. void release_thread(struct task_struct *dead_task)
  101. {
  102. BUG_ON(dead_task->mm);
  103. release_vm86_irqs(dead_task);
  104. }
  105. int copy_thread_tls(unsigned long clone_flags, unsigned long sp,
  106. unsigned long arg, struct task_struct *p, unsigned long tls)
  107. {
  108. struct pt_regs *childregs = task_pt_regs(p);
  109. struct fork_frame *fork_frame = container_of(childregs, struct fork_frame, regs);
  110. struct inactive_task_frame *frame = &fork_frame->frame;
  111. struct task_struct *tsk;
  112. int err;
  113. frame->bp = 0;
  114. frame->ret_addr = (unsigned long) ret_from_fork;
  115. p->thread.sp = (unsigned long) fork_frame;
  116. p->thread.sp0 = (unsigned long) (childregs+1);
  117. memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps));
  118. if (unlikely(p->flags & PF_KTHREAD)) {
  119. /* kernel thread */
  120. memset(childregs, 0, sizeof(struct pt_regs));
  121. frame->bx = sp; /* function */
  122. frame->di = arg;
  123. p->thread.io_bitmap_ptr = NULL;
  124. return 0;
  125. }
  126. frame->bx = 0;
  127. *childregs = *current_pt_regs();
  128. childregs->ax = 0;
  129. if (sp)
  130. childregs->sp = sp;
  131. task_user_gs(p) = get_user_gs(current_pt_regs());
  132. p->thread.io_bitmap_ptr = NULL;
  133. tsk = current;
  134. err = -ENOMEM;
  135. if (unlikely(test_tsk_thread_flag(tsk, TIF_IO_BITMAP))) {
  136. p->thread.io_bitmap_ptr = kmemdup(tsk->thread.io_bitmap_ptr,
  137. IO_BITMAP_BYTES, GFP_KERNEL);
  138. if (!p->thread.io_bitmap_ptr) {
  139. p->thread.io_bitmap_max = 0;
  140. return -ENOMEM;
  141. }
  142. set_tsk_thread_flag(p, TIF_IO_BITMAP);
  143. }
  144. err = 0;
  145. /*
  146. * Set a new TLS for the child thread?
  147. */
  148. if (clone_flags & CLONE_SETTLS)
  149. err = do_set_thread_area(p, -1,
  150. (struct user_desc __user *)tls, 0);
  151. if (err && p->thread.io_bitmap_ptr) {
  152. kfree(p->thread.io_bitmap_ptr);
  153. p->thread.io_bitmap_max = 0;
  154. }
  155. return err;
  156. }
  157. void
  158. start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
  159. {
  160. set_user_gs(regs, 0);
  161. regs->fs = 0;
  162. regs->ds = __USER_DS;
  163. regs->es = __USER_DS;
  164. regs->ss = __USER_DS;
  165. regs->cs = __USER_CS;
  166. regs->ip = new_ip;
  167. regs->sp = new_sp;
  168. regs->flags = X86_EFLAGS_IF;
  169. force_iret();
  170. }
  171. EXPORT_SYMBOL_GPL(start_thread);
  172. /*
  173. * switch_to(x,y) should switch tasks from x to y.
  174. *
  175. * We fsave/fwait so that an exception goes off at the right time
  176. * (as a call from the fsave or fwait in effect) rather than to
  177. * the wrong process. Lazy FP saving no longer makes any sense
  178. * with modern CPU's, and this simplifies a lot of things (SMP
  179. * and UP become the same).
  180. *
  181. * NOTE! We used to use the x86 hardware context switching. The
  182. * reason for not using it any more becomes apparent when you
  183. * try to recover gracefully from saved state that is no longer
  184. * valid (stale segment register values in particular). With the
  185. * hardware task-switch, there is no way to fix up bad state in
  186. * a reasonable manner.
  187. *
  188. * The fact that Intel documents the hardware task-switching to
  189. * be slow is a fairly red herring - this code is not noticeably
  190. * faster. However, there _is_ some room for improvement here,
  191. * so the performance issues may eventually be a valid point.
  192. * More important, however, is the fact that this allows us much
  193. * more flexibility.
  194. *
  195. * The return value (in %ax) will be the "prev" task after
  196. * the task-switch, and shows up in ret_from_fork in entry.S,
  197. * for example.
  198. */
  199. __visible __notrace_funcgraph struct task_struct *
  200. __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
  201. {
  202. struct thread_struct *prev = &prev_p->thread,
  203. *next = &next_p->thread;
  204. struct fpu *prev_fpu = &prev->fpu;
  205. struct fpu *next_fpu = &next->fpu;
  206. int cpu = smp_processor_id();
  207. struct tss_struct *tss = &per_cpu(cpu_tss, cpu);
  208. fpu_switch_t fpu_switch;
  209. /* never put a printk in __switch_to... printk() calls wake_up*() indirectly */
  210. fpu_switch = switch_fpu_prepare(prev_fpu, next_fpu, cpu);
  211. /*
  212. * Save away %gs. No need to save %fs, as it was saved on the
  213. * stack on entry. No need to save %es and %ds, as those are
  214. * always kernel segments while inside the kernel. Doing this
  215. * before setting the new TLS descriptors avoids the situation
  216. * where we temporarily have non-reloadable segments in %fs
  217. * and %gs. This could be an issue if the NMI handler ever
  218. * used %fs or %gs (it does not today), or if the kernel is
  219. * running inside of a hypervisor layer.
  220. */
  221. lazy_save_gs(prev->gs);
  222. /*
  223. * Load the per-thread Thread-Local Storage descriptor.
  224. */
  225. load_TLS(next, cpu);
  226. /*
  227. * Restore IOPL if needed. In normal use, the flags restore
  228. * in the switch assembly will handle this. But if the kernel
  229. * is running virtualized at a non-zero CPL, the popf will
  230. * not restore flags, so it must be done in a separate step.
  231. */
  232. if (get_kernel_rpl() && unlikely(prev->iopl != next->iopl))
  233. set_iopl_mask(next->iopl);
  234. /*
  235. * Now maybe handle debug registers and/or IO bitmaps
  236. */
  237. if (unlikely(task_thread_info(prev_p)->flags & _TIF_WORK_CTXSW_PREV ||
  238. task_thread_info(next_p)->flags & _TIF_WORK_CTXSW_NEXT))
  239. __switch_to_xtra(prev_p, next_p, tss);
  240. /*
  241. * Leave lazy mode, flushing any hypercalls made here.
  242. * This must be done before restoring TLS segments so
  243. * the GDT and LDT are properly updated, and must be
  244. * done before fpu__restore(), so the TS bit is up
  245. * to date.
  246. */
  247. arch_end_context_switch(next_p);
  248. /*
  249. * Reload esp0 and cpu_current_top_of_stack. This changes
  250. * current_thread_info().
  251. */
  252. load_sp0(tss, next);
  253. this_cpu_write(cpu_current_top_of_stack,
  254. (unsigned long)task_stack_page(next_p) +
  255. THREAD_SIZE);
  256. /*
  257. * Restore %gs if needed (which is common)
  258. */
  259. if (prev->gs | next->gs)
  260. lazy_load_gs(next->gs);
  261. switch_fpu_finish(next_fpu, fpu_switch);
  262. this_cpu_write(current_task, next_p);
  263. return prev_p;
  264. }