process_32.c 8.6 KB

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