process_64.c 18 KB

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