process_64.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. /*
  2. * Copyright (C) 1995 Linus Torvalds
  3. *
  4. * Pentium III FXSR, SSE support
  5. * Gareth Hughes <gareth@valinux.com>, May 2000
  6. *
  7. * X86-64 port
  8. * Andi Kleen.
  9. *
  10. * CPU hotplug support - ashok.raj@intel.com
  11. */
  12. /*
  13. * This file handles the architecture-dependent parts of process handling..
  14. */
  15. #include <linux/cpu.h>
  16. #include <linux/errno.h>
  17. #include <linux/sched.h>
  18. #include <linux/fs.h>
  19. #include <linux/kernel.h>
  20. #include <linux/mm.h>
  21. #include <linux/elfcore.h>
  22. #include <linux/smp.h>
  23. #include <linux/slab.h>
  24. #include <linux/user.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/delay.h>
  27. #include <linux/export.h>
  28. #include <linux/ptrace.h>
  29. #include <linux/notifier.h>
  30. #include <linux/kprobes.h>
  31. #include <linux/kdebug.h>
  32. #include <linux/prctl.h>
  33. #include <linux/uaccess.h>
  34. #include <linux/io.h>
  35. #include <linux/ftrace.h>
  36. #include <asm/pgtable.h>
  37. #include <asm/processor.h>
  38. #include <asm/fpu/internal.h>
  39. #include <asm/mmu_context.h>
  40. #include <asm/prctl.h>
  41. #include <asm/desc.h>
  42. #include <asm/proto.h>
  43. #include <asm/ia32.h>
  44. #include <asm/idle.h>
  45. #include <asm/syscalls.h>
  46. #include <asm/debugreg.h>
  47. #include <asm/switch_to.h>
  48. #include <asm/xen/hypervisor.h>
  49. __visible DEFINE_PER_CPU(unsigned long, rsp_scratch);
  50. /* Prints also some state that isn't saved in the pt_regs */
  51. void __show_regs(struct pt_regs *regs, int all)
  52. {
  53. unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L, fs, gs, shadowgs;
  54. unsigned long d0, d1, d2, d3, d6, d7;
  55. unsigned int fsindex, gsindex;
  56. unsigned int ds, cs, es;
  57. printk(KERN_DEFAULT "RIP: %04lx:[<%016lx>] ", regs->cs & 0xffff, regs->ip);
  58. printk_address(regs->ip);
  59. printk(KERN_DEFAULT "RSP: %04lx:%016lx EFLAGS: %08lx\n", regs->ss,
  60. regs->sp, regs->flags);
  61. printk(KERN_DEFAULT "RAX: %016lx RBX: %016lx RCX: %016lx\n",
  62. regs->ax, regs->bx, regs->cx);
  63. printk(KERN_DEFAULT "RDX: %016lx RSI: %016lx RDI: %016lx\n",
  64. regs->dx, regs->si, regs->di);
  65. printk(KERN_DEFAULT "RBP: %016lx R08: %016lx R09: %016lx\n",
  66. regs->bp, regs->r8, regs->r9);
  67. printk(KERN_DEFAULT "R10: %016lx R11: %016lx R12: %016lx\n",
  68. regs->r10, regs->r11, regs->r12);
  69. printk(KERN_DEFAULT "R13: %016lx R14: %016lx R15: %016lx\n",
  70. regs->r13, regs->r14, regs->r15);
  71. asm("movl %%ds,%0" : "=r" (ds));
  72. asm("movl %%cs,%0" : "=r" (cs));
  73. asm("movl %%es,%0" : "=r" (es));
  74. asm("movl %%fs,%0" : "=r" (fsindex));
  75. asm("movl %%gs,%0" : "=r" (gsindex));
  76. rdmsrl(MSR_FS_BASE, fs);
  77. rdmsrl(MSR_GS_BASE, gs);
  78. rdmsrl(MSR_KERNEL_GS_BASE, shadowgs);
  79. if (!all)
  80. return;
  81. cr0 = read_cr0();
  82. cr2 = read_cr2();
  83. cr3 = read_cr3();
  84. cr4 = __read_cr4();
  85. printk(KERN_DEFAULT "FS: %016lx(%04x) GS:%016lx(%04x) knlGS:%016lx\n",
  86. fs, fsindex, gs, gsindex, shadowgs);
  87. printk(KERN_DEFAULT "CS: %04x DS: %04x ES: %04x CR0: %016lx\n", cs, ds,
  88. es, cr0);
  89. printk(KERN_DEFAULT "CR2: %016lx CR3: %016lx CR4: %016lx\n", cr2, cr3,
  90. cr4);
  91. get_debugreg(d0, 0);
  92. get_debugreg(d1, 1);
  93. get_debugreg(d2, 2);
  94. get_debugreg(d3, 3);
  95. get_debugreg(d6, 6);
  96. get_debugreg(d7, 7);
  97. /* Only print out debug registers if they are in their non-default state. */
  98. if ((d0 == 0) && (d1 == 0) && (d2 == 0) && (d3 == 0) &&
  99. (d6 == DR6_RESERVED) && (d7 == 0x400))
  100. return;
  101. printk(KERN_DEFAULT "DR0: %016lx DR1: %016lx DR2: %016lx\n", d0, d1, d2);
  102. printk(KERN_DEFAULT "DR3: %016lx DR6: %016lx DR7: %016lx\n", d3, d6, d7);
  103. if (boot_cpu_has(X86_FEATURE_OSPKE))
  104. printk(KERN_DEFAULT "PKRU: %08x\n", read_pkru());
  105. }
  106. void release_thread(struct task_struct *dead_task)
  107. {
  108. if (dead_task->mm) {
  109. #ifdef CONFIG_MODIFY_LDT_SYSCALL
  110. if (dead_task->mm->context.ldt) {
  111. pr_warn("WARNING: dead process %s still has LDT? <%p/%d>\n",
  112. dead_task->comm,
  113. dead_task->mm->context.ldt->entries,
  114. dead_task->mm->context.ldt->size);
  115. BUG();
  116. }
  117. #endif
  118. }
  119. }
  120. int copy_thread_tls(unsigned long clone_flags, unsigned long sp,
  121. unsigned long arg, struct task_struct *p, unsigned long tls)
  122. {
  123. int err;
  124. struct pt_regs *childregs;
  125. struct fork_frame *fork_frame;
  126. struct inactive_task_frame *frame;
  127. struct task_struct *me = current;
  128. p->thread.sp0 = (unsigned long)task_stack_page(p) + THREAD_SIZE;
  129. childregs = task_pt_regs(p);
  130. fork_frame = container_of(childregs, struct fork_frame, regs);
  131. frame = &fork_frame->frame;
  132. frame->bp = 0;
  133. frame->ret_addr = (unsigned long) ret_from_fork;
  134. p->thread.sp = (unsigned long) fork_frame;
  135. p->thread.io_bitmap_ptr = NULL;
  136. savesegment(gs, p->thread.gsindex);
  137. p->thread.gsbase = p->thread.gsindex ? 0 : me->thread.gsbase;
  138. savesegment(fs, p->thread.fsindex);
  139. p->thread.fsbase = p->thread.fsindex ? 0 : me->thread.fsbase;
  140. savesegment(es, p->thread.es);
  141. savesegment(ds, p->thread.ds);
  142. memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps));
  143. if (unlikely(p->flags & PF_KTHREAD)) {
  144. /* kernel thread */
  145. memset(childregs, 0, sizeof(struct pt_regs));
  146. frame->bx = sp; /* function */
  147. frame->r12 = arg;
  148. return 0;
  149. }
  150. frame->bx = 0;
  151. *childregs = *current_pt_regs();
  152. childregs->ax = 0;
  153. if (sp)
  154. childregs->sp = sp;
  155. err = -ENOMEM;
  156. if (unlikely(test_tsk_thread_flag(me, TIF_IO_BITMAP))) {
  157. p->thread.io_bitmap_ptr = kmemdup(me->thread.io_bitmap_ptr,
  158. IO_BITMAP_BYTES, GFP_KERNEL);
  159. if (!p->thread.io_bitmap_ptr) {
  160. p->thread.io_bitmap_max = 0;
  161. return -ENOMEM;
  162. }
  163. set_tsk_thread_flag(p, TIF_IO_BITMAP);
  164. }
  165. /*
  166. * Set a new TLS for the child thread?
  167. */
  168. if (clone_flags & CLONE_SETTLS) {
  169. #ifdef CONFIG_IA32_EMULATION
  170. if (in_ia32_syscall())
  171. err = do_set_thread_area(p, -1,
  172. (struct user_desc __user *)tls, 0);
  173. else
  174. #endif
  175. err = do_arch_prctl(p, ARCH_SET_FS, tls);
  176. if (err)
  177. goto out;
  178. }
  179. err = 0;
  180. out:
  181. if (err && p->thread.io_bitmap_ptr) {
  182. kfree(p->thread.io_bitmap_ptr);
  183. p->thread.io_bitmap_max = 0;
  184. }
  185. return err;
  186. }
  187. static void
  188. start_thread_common(struct pt_regs *regs, unsigned long new_ip,
  189. unsigned long new_sp,
  190. unsigned int _cs, unsigned int _ss, unsigned int _ds)
  191. {
  192. loadsegment(fs, 0);
  193. loadsegment(es, _ds);
  194. loadsegment(ds, _ds);
  195. load_gs_index(0);
  196. regs->ip = new_ip;
  197. regs->sp = new_sp;
  198. regs->cs = _cs;
  199. regs->ss = _ss;
  200. regs->flags = X86_EFLAGS_IF;
  201. force_iret();
  202. }
  203. void
  204. start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
  205. {
  206. start_thread_common(regs, new_ip, new_sp,
  207. __USER_CS, __USER_DS, 0);
  208. }
  209. #ifdef CONFIG_COMPAT
  210. void compat_start_thread(struct pt_regs *regs, u32 new_ip, u32 new_sp)
  211. {
  212. start_thread_common(regs, new_ip, new_sp,
  213. test_thread_flag(TIF_X32)
  214. ? __USER_CS : __USER32_CS,
  215. __USER_DS, __USER_DS);
  216. }
  217. #endif
  218. /*
  219. * switch_to(x,y) should switch tasks from x to y.
  220. *
  221. * This could still be optimized:
  222. * - fold all the options into a flag word and test it with a single test.
  223. * - could test fs/gs bitsliced
  224. *
  225. * Kprobes not supported here. Set the probe on schedule instead.
  226. * Function graph tracer not supported too.
  227. */
  228. __visible __notrace_funcgraph struct task_struct *
  229. __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
  230. {
  231. struct thread_struct *prev = &prev_p->thread;
  232. struct thread_struct *next = &next_p->thread;
  233. struct fpu *prev_fpu = &prev->fpu;
  234. struct fpu *next_fpu = &next->fpu;
  235. int cpu = smp_processor_id();
  236. struct tss_struct *tss = &per_cpu(cpu_tss, cpu);
  237. unsigned prev_fsindex, prev_gsindex;
  238. fpu_switch_t fpu_switch;
  239. fpu_switch = switch_fpu_prepare(prev_fpu, next_fpu, cpu);
  240. /* We must save %fs and %gs before load_TLS() because
  241. * %fs and %gs may be cleared by load_TLS().
  242. *
  243. * (e.g. xen_load_tls())
  244. */
  245. savesegment(fs, prev_fsindex);
  246. savesegment(gs, prev_gsindex);
  247. /*
  248. * Load TLS before restoring any segments so that segment loads
  249. * reference the correct GDT entries.
  250. */
  251. load_TLS(next, cpu);
  252. /*
  253. * Leave lazy mode, flushing any hypercalls made here. This
  254. * must be done after loading TLS entries in the GDT but before
  255. * loading segments that might reference them, and and it must
  256. * be done before fpu__restore(), so the TS bit is up to
  257. * date.
  258. */
  259. arch_end_context_switch(next_p);
  260. /* Switch DS and ES.
  261. *
  262. * Reading them only returns the selectors, but writing them (if
  263. * nonzero) loads the full descriptor from the GDT or LDT. The
  264. * LDT for next is loaded in switch_mm, and the GDT is loaded
  265. * above.
  266. *
  267. * We therefore need to write new values to the segment
  268. * registers on every context switch unless both the new and old
  269. * values are zero.
  270. *
  271. * Note that we don't need to do anything for CS and SS, as
  272. * those are saved and restored as part of pt_regs.
  273. */
  274. savesegment(es, prev->es);
  275. if (unlikely(next->es | prev->es))
  276. loadsegment(es, next->es);
  277. savesegment(ds, prev->ds);
  278. if (unlikely(next->ds | prev->ds))
  279. loadsegment(ds, next->ds);
  280. /*
  281. * Switch FS and GS.
  282. *
  283. * These are even more complicated than DS and ES: they have
  284. * 64-bit bases are that controlled by arch_prctl. The bases
  285. * don't necessarily match the selectors, as user code can do
  286. * any number of things to cause them to be inconsistent.
  287. *
  288. * We don't promise to preserve the bases if the selectors are
  289. * nonzero. We also don't promise to preserve the base if the
  290. * selector is zero and the base doesn't match whatever was
  291. * most recently passed to ARCH_SET_FS/GS. (If/when the
  292. * FSGSBASE instructions are enabled, we'll need to offer
  293. * stronger guarantees.)
  294. *
  295. * As an invariant,
  296. * (fsbase != 0 && fsindex != 0) || (gsbase != 0 && gsindex != 0) is
  297. * impossible.
  298. */
  299. if (next->fsindex) {
  300. /* Loading a nonzero value into FS sets the index and base. */
  301. loadsegment(fs, next->fsindex);
  302. } else {
  303. if (next->fsbase) {
  304. /* Next index is zero but next base is nonzero. */
  305. if (prev_fsindex)
  306. loadsegment(fs, 0);
  307. wrmsrl(MSR_FS_BASE, next->fsbase);
  308. } else {
  309. /* Next base and index are both zero. */
  310. if (static_cpu_has_bug(X86_BUG_NULL_SEG)) {
  311. /*
  312. * We don't know the previous base and can't
  313. * find out without RDMSR. Forcibly clear it.
  314. */
  315. loadsegment(fs, __USER_DS);
  316. loadsegment(fs, 0);
  317. } else {
  318. /*
  319. * If the previous index is zero and ARCH_SET_FS
  320. * didn't change the base, then the base is
  321. * also zero and we don't need to do anything.
  322. */
  323. if (prev->fsbase || prev_fsindex)
  324. loadsegment(fs, 0);
  325. }
  326. }
  327. }
  328. /*
  329. * Save the old state and preserve the invariant.
  330. * NB: if prev_fsindex == 0, then we can't reliably learn the base
  331. * without RDMSR because Intel user code can zero it without telling
  332. * us and AMD user code can program any 32-bit value without telling
  333. * us.
  334. */
  335. if (prev_fsindex)
  336. prev->fsbase = 0;
  337. prev->fsindex = prev_fsindex;
  338. if (next->gsindex) {
  339. /* Loading a nonzero value into GS sets the index and base. */
  340. load_gs_index(next->gsindex);
  341. } else {
  342. if (next->gsbase) {
  343. /* Next index is zero but next base is nonzero. */
  344. if (prev_gsindex)
  345. load_gs_index(0);
  346. wrmsrl(MSR_KERNEL_GS_BASE, next->gsbase);
  347. } else {
  348. /* Next base and index are both zero. */
  349. if (static_cpu_has_bug(X86_BUG_NULL_SEG)) {
  350. /*
  351. * We don't know the previous base and can't
  352. * find out without RDMSR. Forcibly clear it.
  353. *
  354. * This contains a pointless SWAPGS pair.
  355. * Fixing it would involve an explicit check
  356. * for Xen or a new pvop.
  357. */
  358. load_gs_index(__USER_DS);
  359. load_gs_index(0);
  360. } else {
  361. /*
  362. * If the previous index is zero and ARCH_SET_GS
  363. * didn't change the base, then the base is
  364. * also zero and we don't need to do anything.
  365. */
  366. if (prev->gsbase || prev_gsindex)
  367. load_gs_index(0);
  368. }
  369. }
  370. }
  371. /*
  372. * Save the old state and preserve the invariant.
  373. * NB: if prev_gsindex == 0, then we can't reliably learn the base
  374. * without RDMSR because Intel user code can zero it without telling
  375. * us and AMD user code can program any 32-bit value without telling
  376. * us.
  377. */
  378. if (prev_gsindex)
  379. prev->gsbase = 0;
  380. prev->gsindex = prev_gsindex;
  381. switch_fpu_finish(next_fpu, fpu_switch);
  382. /*
  383. * Switch the PDA and FPU contexts.
  384. */
  385. this_cpu_write(current_task, next_p);
  386. /* Reload esp0 and ss1. This changes current_thread_info(). */
  387. load_sp0(tss, next);
  388. /*
  389. * Now maybe reload the debug registers and handle I/O bitmaps
  390. */
  391. if (unlikely(task_thread_info(next_p)->flags & _TIF_WORK_CTXSW_NEXT ||
  392. task_thread_info(prev_p)->flags & _TIF_WORK_CTXSW_PREV))
  393. __switch_to_xtra(prev_p, next_p, tss);
  394. #ifdef CONFIG_XEN
  395. /*
  396. * On Xen PV, IOPL bits in pt_regs->flags have no effect, and
  397. * current_pt_regs()->flags may not match the current task's
  398. * intended IOPL. We need to switch it manually.
  399. */
  400. if (unlikely(static_cpu_has(X86_FEATURE_XENPV) &&
  401. prev->iopl != next->iopl))
  402. xen_set_iopl_mask(next->iopl);
  403. #endif
  404. if (static_cpu_has_bug(X86_BUG_SYSRET_SS_ATTRS)) {
  405. /*
  406. * AMD CPUs have a misfeature: SYSRET sets the SS selector but
  407. * does not update the cached descriptor. As a result, if we
  408. * do SYSRET while SS is NULL, we'll end up in user mode with
  409. * SS apparently equal to __USER_DS but actually unusable.
  410. *
  411. * The straightforward workaround would be to fix it up just
  412. * before SYSRET, but that would slow down the system call
  413. * fast paths. Instead, we ensure that SS is never NULL in
  414. * system call context. We do this by replacing NULL SS
  415. * selectors at every context switch. SYSCALL sets up a valid
  416. * SS, so the only way to get NULL is to re-enter the kernel
  417. * from CPL 3 through an interrupt. Since that can't happen
  418. * in the same task as a running syscall, we are guaranteed to
  419. * context switch between every interrupt vector entry and a
  420. * subsequent SYSRET.
  421. *
  422. * We read SS first because SS reads are much faster than
  423. * writes. Out of caution, we force SS to __KERNEL_DS even if
  424. * it previously had a different non-NULL value.
  425. */
  426. unsigned short ss_sel;
  427. savesegment(ss, ss_sel);
  428. if (ss_sel != __KERNEL_DS)
  429. loadsegment(ss, __KERNEL_DS);
  430. }
  431. return prev_p;
  432. }
  433. void set_personality_64bit(void)
  434. {
  435. /* inherit personality from parent */
  436. /* Make sure to be in 64bit mode */
  437. clear_thread_flag(TIF_IA32);
  438. clear_thread_flag(TIF_ADDR32);
  439. clear_thread_flag(TIF_X32);
  440. /* Ensure the corresponding mm is not marked. */
  441. if (current->mm)
  442. current->mm->context.ia32_compat = 0;
  443. /* TBD: overwrites user setup. Should have two bits.
  444. But 64bit processes have always behaved this way,
  445. so it's not too bad. The main problem is just that
  446. 32bit childs are affected again. */
  447. current->personality &= ~READ_IMPLIES_EXEC;
  448. }
  449. void set_personality_ia32(bool x32)
  450. {
  451. /* inherit personality from parent */
  452. /* Make sure to be in 32bit mode */
  453. set_thread_flag(TIF_ADDR32);
  454. /* Mark the associated mm as containing 32-bit tasks. */
  455. if (x32) {
  456. clear_thread_flag(TIF_IA32);
  457. set_thread_flag(TIF_X32);
  458. if (current->mm)
  459. current->mm->context.ia32_compat = TIF_X32;
  460. current->personality &= ~READ_IMPLIES_EXEC;
  461. /* in_compat_syscall() uses the presence of the x32
  462. syscall bit flag to determine compat status */
  463. current->thread.status &= ~TS_COMPAT;
  464. } else {
  465. set_thread_flag(TIF_IA32);
  466. clear_thread_flag(TIF_X32);
  467. if (current->mm)
  468. current->mm->context.ia32_compat = TIF_IA32;
  469. current->personality |= force_personality32;
  470. /* Prepare the first "return" to user space */
  471. current->thread.status |= TS_COMPAT;
  472. }
  473. }
  474. EXPORT_SYMBOL_GPL(set_personality_ia32);
  475. long do_arch_prctl(struct task_struct *task, int code, unsigned long addr)
  476. {
  477. int ret = 0;
  478. int doit = task == current;
  479. int cpu;
  480. switch (code) {
  481. case ARCH_SET_GS:
  482. if (addr >= TASK_SIZE_MAX)
  483. return -EPERM;
  484. cpu = get_cpu();
  485. task->thread.gsindex = 0;
  486. task->thread.gsbase = addr;
  487. if (doit) {
  488. load_gs_index(0);
  489. ret = wrmsrl_safe(MSR_KERNEL_GS_BASE, addr);
  490. }
  491. put_cpu();
  492. break;
  493. case ARCH_SET_FS:
  494. /* Not strictly needed for fs, but do it for symmetry
  495. with gs */
  496. if (addr >= TASK_SIZE_MAX)
  497. return -EPERM;
  498. cpu = get_cpu();
  499. task->thread.fsindex = 0;
  500. task->thread.fsbase = addr;
  501. if (doit) {
  502. /* set the selector to 0 to not confuse __switch_to */
  503. loadsegment(fs, 0);
  504. ret = wrmsrl_safe(MSR_FS_BASE, addr);
  505. }
  506. put_cpu();
  507. break;
  508. case ARCH_GET_FS: {
  509. unsigned long base;
  510. if (doit)
  511. rdmsrl(MSR_FS_BASE, base);
  512. else
  513. base = task->thread.fsbase;
  514. ret = put_user(base, (unsigned long __user *)addr);
  515. break;
  516. }
  517. case ARCH_GET_GS: {
  518. unsigned long base;
  519. if (doit)
  520. rdmsrl(MSR_KERNEL_GS_BASE, base);
  521. else
  522. base = task->thread.gsbase;
  523. ret = put_user(base, (unsigned long __user *)addr);
  524. break;
  525. }
  526. default:
  527. ret = -EINVAL;
  528. break;
  529. }
  530. return ret;
  531. }
  532. long sys_arch_prctl(int code, unsigned long addr)
  533. {
  534. return do_arch_prctl(current, code, addr);
  535. }
  536. unsigned long KSTK_ESP(struct task_struct *task)
  537. {
  538. return task_pt_regs(task)->sp;
  539. }