process_32.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. #include "process.h"
  57. void __show_regs(struct pt_regs *regs, enum show_regs_mode mode)
  58. {
  59. unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L;
  60. unsigned long d0, d1, d2, d3, d6, d7;
  61. unsigned long sp;
  62. unsigned short ss, gs;
  63. if (user_mode(regs)) {
  64. sp = regs->sp;
  65. ss = regs->ss;
  66. gs = get_user_gs(regs);
  67. } else {
  68. sp = kernel_stack_pointer(regs);
  69. savesegment(ss, ss);
  70. savesegment(gs, gs);
  71. }
  72. show_ip(regs, KERN_DEFAULT);
  73. printk(KERN_DEFAULT "EAX: %08lx EBX: %08lx ECX: %08lx EDX: %08lx\n",
  74. regs->ax, regs->bx, regs->cx, regs->dx);
  75. printk(KERN_DEFAULT "ESI: %08lx EDI: %08lx EBP: %08lx ESP: %08lx\n",
  76. regs->si, regs->di, regs->bp, sp);
  77. printk(KERN_DEFAULT "DS: %04x ES: %04x FS: %04x GS: %04x SS: %04x EFLAGS: %08lx\n",
  78. (u16)regs->ds, (u16)regs->es, (u16)regs->fs, gs, ss, regs->flags);
  79. if (mode != SHOW_REGS_ALL)
  80. return;
  81. cr0 = read_cr0();
  82. cr2 = read_cr2();
  83. cr3 = __read_cr3();
  84. cr4 = __read_cr4();
  85. printk(KERN_DEFAULT "CR0: %08lx CR2: %08lx CR3: %08lx CR4: %08lx\n",
  86. cr0, cr2, cr3, cr4);
  87. get_debugreg(d0, 0);
  88. get_debugreg(d1, 1);
  89. get_debugreg(d2, 2);
  90. get_debugreg(d3, 3);
  91. get_debugreg(d6, 6);
  92. get_debugreg(d7, 7);
  93. /* Only print out debug registers if they are in their non-default state. */
  94. if ((d0 == 0) && (d1 == 0) && (d2 == 0) && (d3 == 0) &&
  95. (d6 == DR6_RESERVED) && (d7 == 0x400))
  96. return;
  97. printk(KERN_DEFAULT "DR0: %08lx DR1: %08lx DR2: %08lx DR3: %08lx\n",
  98. d0, d1, d2, d3);
  99. printk(KERN_DEFAULT "DR6: %08lx DR7: %08lx\n",
  100. d6, d7);
  101. }
  102. void release_thread(struct task_struct *dead_task)
  103. {
  104. BUG_ON(dead_task->mm);
  105. release_vm86_irqs(dead_task);
  106. }
  107. int copy_thread_tls(unsigned long clone_flags, unsigned long sp,
  108. unsigned long arg, struct task_struct *p, unsigned long tls)
  109. {
  110. struct pt_regs *childregs = task_pt_regs(p);
  111. struct fork_frame *fork_frame = container_of(childregs, struct fork_frame, regs);
  112. struct inactive_task_frame *frame = &fork_frame->frame;
  113. struct task_struct *tsk;
  114. int err;
  115. /*
  116. * For a new task use the RESET flags value since there is no before.
  117. * All the status flags are zero; DF and all the system flags must also
  118. * be 0, specifically IF must be 0 because we context switch to the new
  119. * task with interrupts disabled.
  120. */
  121. frame->flags = X86_EFLAGS_FIXED;
  122. frame->bp = 0;
  123. frame->ret_addr = (unsigned long) ret_from_fork;
  124. p->thread.sp = (unsigned long) fork_frame;
  125. p->thread.sp0 = (unsigned long) (childregs+1);
  126. memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps));
  127. if (unlikely(p->flags & PF_KTHREAD)) {
  128. /* kernel thread */
  129. memset(childregs, 0, sizeof(struct pt_regs));
  130. frame->bx = sp; /* function */
  131. frame->di = arg;
  132. p->thread.io_bitmap_ptr = NULL;
  133. return 0;
  134. }
  135. frame->bx = 0;
  136. *childregs = *current_pt_regs();
  137. childregs->ax = 0;
  138. if (sp)
  139. childregs->sp = sp;
  140. task_user_gs(p) = get_user_gs(current_pt_regs());
  141. p->thread.io_bitmap_ptr = NULL;
  142. tsk = current;
  143. err = -ENOMEM;
  144. if (unlikely(test_tsk_thread_flag(tsk, TIF_IO_BITMAP))) {
  145. p->thread.io_bitmap_ptr = kmemdup(tsk->thread.io_bitmap_ptr,
  146. IO_BITMAP_BYTES, GFP_KERNEL);
  147. if (!p->thread.io_bitmap_ptr) {
  148. p->thread.io_bitmap_max = 0;
  149. return -ENOMEM;
  150. }
  151. set_tsk_thread_flag(p, TIF_IO_BITMAP);
  152. }
  153. err = 0;
  154. /*
  155. * Set a new TLS for the child thread?
  156. */
  157. if (clone_flags & CLONE_SETTLS)
  158. err = do_set_thread_area(p, -1,
  159. (struct user_desc __user *)tls, 0);
  160. if (err && p->thread.io_bitmap_ptr) {
  161. kfree(p->thread.io_bitmap_ptr);
  162. p->thread.io_bitmap_max = 0;
  163. }
  164. return err;
  165. }
  166. void
  167. start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
  168. {
  169. set_user_gs(regs, 0);
  170. regs->fs = 0;
  171. regs->ds = __USER_DS;
  172. regs->es = __USER_DS;
  173. regs->ss = __USER_DS;
  174. regs->cs = __USER_CS;
  175. regs->ip = new_ip;
  176. regs->sp = new_sp;
  177. regs->flags = X86_EFLAGS_IF;
  178. force_iret();
  179. }
  180. EXPORT_SYMBOL_GPL(start_thread);
  181. /*
  182. * switch_to(x,y) should switch tasks from x to y.
  183. *
  184. * We fsave/fwait so that an exception goes off at the right time
  185. * (as a call from the fsave or fwait in effect) rather than to
  186. * the wrong process. Lazy FP saving no longer makes any sense
  187. * with modern CPU's, and this simplifies a lot of things (SMP
  188. * and UP become the same).
  189. *
  190. * NOTE! We used to use the x86 hardware context switching. The
  191. * reason for not using it any more becomes apparent when you
  192. * try to recover gracefully from saved state that is no longer
  193. * valid (stale segment register values in particular). With the
  194. * hardware task-switch, there is no way to fix up bad state in
  195. * a reasonable manner.
  196. *
  197. * The fact that Intel documents the hardware task-switching to
  198. * be slow is a fairly red herring - this code is not noticeably
  199. * faster. However, there _is_ some room for improvement here,
  200. * so the performance issues may eventually be a valid point.
  201. * More important, however, is the fact that this allows us much
  202. * more flexibility.
  203. *
  204. * The return value (in %ax) will be the "prev" task after
  205. * the task-switch, and shows up in ret_from_fork in entry.S,
  206. * for example.
  207. */
  208. __visible __notrace_funcgraph struct task_struct *
  209. __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
  210. {
  211. struct thread_struct *prev = &prev_p->thread,
  212. *next = &next_p->thread;
  213. struct fpu *prev_fpu = &prev->fpu;
  214. struct fpu *next_fpu = &next->fpu;
  215. int cpu = smp_processor_id();
  216. /* never put a printk in __switch_to... printk() calls wake_up*() indirectly */
  217. switch_fpu_prepare(prev_fpu, cpu);
  218. /*
  219. * Save away %gs. No need to save %fs, as it was saved on the
  220. * stack on entry. No need to save %es and %ds, as those are
  221. * always kernel segments while inside the kernel. Doing this
  222. * before setting the new TLS descriptors avoids the situation
  223. * where we temporarily have non-reloadable segments in %fs
  224. * and %gs. This could be an issue if the NMI handler ever
  225. * used %fs or %gs (it does not today), or if the kernel is
  226. * running inside of a hypervisor layer.
  227. */
  228. lazy_save_gs(prev->gs);
  229. /*
  230. * Load the per-thread Thread-Local Storage descriptor.
  231. */
  232. load_TLS(next, cpu);
  233. /*
  234. * Restore IOPL if needed. In normal use, the flags restore
  235. * in the switch assembly will handle this. But if the kernel
  236. * is running virtualized at a non-zero CPL, the popf will
  237. * not restore flags, so it must be done in a separate step.
  238. */
  239. if (get_kernel_rpl() && unlikely(prev->iopl != next->iopl))
  240. set_iopl_mask(next->iopl);
  241. switch_to_extra(prev_p, next_p);
  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_task_stack(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. }