process_64.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 1995 Linus Torvalds
  4. *
  5. * Pentium III FXSR, SSE support
  6. * Gareth Hughes <[email protected]>, May 2000
  7. *
  8. * X86-64 port
  9. * Andi Kleen.
  10. *
  11. * CPU hotplug support - [email protected]
  12. */
  13. /*
  14. * This file handles the architecture-dependent parts of process handling..
  15. */
  16. #include <linux/cpu.h>
  17. #include <linux/errno.h>
  18. #include <linux/sched.h>
  19. #include <linux/sched/task.h>
  20. #include <linux/sched/task_stack.h>
  21. #include <linux/fs.h>
  22. #include <linux/kernel.h>
  23. #include <linux/mm.h>
  24. #include <linux/elfcore.h>
  25. #include <linux/smp.h>
  26. #include <linux/slab.h>
  27. #include <linux/user.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/delay.h>
  30. #include <linux/export.h>
  31. #include <linux/ptrace.h>
  32. #include <linux/notifier.h>
  33. #include <linux/kprobes.h>
  34. #include <linux/kdebug.h>
  35. #include <linux/prctl.h>
  36. #include <linux/uaccess.h>
  37. #include <linux/io.h>
  38. #include <linux/ftrace.h>
  39. #include <linux/syscalls.h>
  40. #include <asm/processor.h>
  41. #include <asm/pkru.h>
  42. #include <asm/fpu/sched.h>
  43. #include <asm/mmu_context.h>
  44. #include <asm/prctl.h>
  45. #include <asm/desc.h>
  46. #include <asm/proto.h>
  47. #include <asm/ia32.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/resctrl.h>
  53. #include <asm/unistd.h>
  54. #include <asm/fsgsbase.h>
  55. #ifdef CONFIG_IA32_EMULATION
  56. /* Not included via unistd.h */
  57. #include <asm/unistd_32_ia32.h>
  58. #endif
  59. #include "process.h"
  60. /* Prints also some state that isn't saved in the pt_regs */
  61. void __show_regs(struct pt_regs *regs, enum show_regs_mode mode,
  62. const char *log_lvl)
  63. {
  64. unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L, fs, gs, shadowgs;
  65. unsigned long d0, d1, d2, d3, d6, d7;
  66. unsigned int fsindex, gsindex;
  67. unsigned int ds, es;
  68. show_iret_regs(regs, log_lvl);
  69. if (regs->orig_ax != -1)
  70. pr_cont(" ORIG_RAX: %016lx\n", regs->orig_ax);
  71. else
  72. pr_cont("\n");
  73. printk("%sRAX: %016lx RBX: %016lx RCX: %016lx\n",
  74. log_lvl, regs->ax, regs->bx, regs->cx);
  75. printk("%sRDX: %016lx RSI: %016lx RDI: %016lx\n",
  76. log_lvl, regs->dx, regs->si, regs->di);
  77. printk("%sRBP: %016lx R08: %016lx R09: %016lx\n",
  78. log_lvl, regs->bp, regs->r8, regs->r9);
  79. printk("%sR10: %016lx R11: %016lx R12: %016lx\n",
  80. log_lvl, regs->r10, regs->r11, regs->r12);
  81. printk("%sR13: %016lx R14: %016lx R15: %016lx\n",
  82. log_lvl, regs->r13, regs->r14, regs->r15);
  83. if (mode == SHOW_REGS_SHORT)
  84. return;
  85. if (mode == SHOW_REGS_USER) {
  86. rdmsrl(MSR_FS_BASE, fs);
  87. rdmsrl(MSR_KERNEL_GS_BASE, shadowgs);
  88. printk("%sFS: %016lx GS: %016lx\n",
  89. log_lvl, fs, shadowgs);
  90. return;
  91. }
  92. asm("movl %%ds,%0" : "=r" (ds));
  93. asm("movl %%es,%0" : "=r" (es));
  94. asm("movl %%fs,%0" : "=r" (fsindex));
  95. asm("movl %%gs,%0" : "=r" (gsindex));
  96. rdmsrl(MSR_FS_BASE, fs);
  97. rdmsrl(MSR_GS_BASE, gs);
  98. rdmsrl(MSR_KERNEL_GS_BASE, shadowgs);
  99. cr0 = read_cr0();
  100. cr2 = read_cr2();
  101. cr3 = __read_cr3();
  102. cr4 = __read_cr4();
  103. printk("%sFS: %016lx(%04x) GS:%016lx(%04x) knlGS:%016lx\n",
  104. log_lvl, fs, fsindex, gs, gsindex, shadowgs);
  105. printk("%sCS: %04lx DS: %04x ES: %04x CR0: %016lx\n",
  106. log_lvl, regs->cs, ds, es, cr0);
  107. printk("%sCR2: %016lx CR3: %016lx CR4: %016lx\n",
  108. log_lvl, cr2, cr3, cr4);
  109. get_debugreg(d0, 0);
  110. get_debugreg(d1, 1);
  111. get_debugreg(d2, 2);
  112. get_debugreg(d3, 3);
  113. get_debugreg(d6, 6);
  114. get_debugreg(d7, 7);
  115. /* Only print out debug registers if they are in their non-default state. */
  116. if (!((d0 == 0) && (d1 == 0) && (d2 == 0) && (d3 == 0) &&
  117. (d6 == DR6_RESERVED) && (d7 == 0x400))) {
  118. printk("%sDR0: %016lx DR1: %016lx DR2: %016lx\n",
  119. log_lvl, d0, d1, d2);
  120. printk("%sDR3: %016lx DR6: %016lx DR7: %016lx\n",
  121. log_lvl, d3, d6, d7);
  122. }
  123. if (cpu_feature_enabled(X86_FEATURE_OSPKE))
  124. printk("%sPKRU: %08x\n", log_lvl, read_pkru());
  125. }
  126. void release_thread(struct task_struct *dead_task)
  127. {
  128. WARN_ON(dead_task->mm);
  129. }
  130. enum which_selector {
  131. FS,
  132. GS
  133. };
  134. /*
  135. * Out of line to be protected from kprobes and tracing. If this would be
  136. * traced or probed than any access to a per CPU variable happens with
  137. * the wrong GS.
  138. *
  139. * It is not used on Xen paravirt. When paravirt support is needed, it
  140. * needs to be renamed with native_ prefix.
  141. */
  142. static noinstr unsigned long __rdgsbase_inactive(void)
  143. {
  144. unsigned long gsbase;
  145. lockdep_assert_irqs_disabled();
  146. if (!static_cpu_has(X86_FEATURE_XENPV)) {
  147. native_swapgs();
  148. gsbase = rdgsbase();
  149. native_swapgs();
  150. } else {
  151. instrumentation_begin();
  152. rdmsrl(MSR_KERNEL_GS_BASE, gsbase);
  153. instrumentation_end();
  154. }
  155. return gsbase;
  156. }
  157. /*
  158. * Out of line to be protected from kprobes and tracing. If this would be
  159. * traced or probed than any access to a per CPU variable happens with
  160. * the wrong GS.
  161. *
  162. * It is not used on Xen paravirt. When paravirt support is needed, it
  163. * needs to be renamed with native_ prefix.
  164. */
  165. static noinstr void __wrgsbase_inactive(unsigned long gsbase)
  166. {
  167. lockdep_assert_irqs_disabled();
  168. if (!static_cpu_has(X86_FEATURE_XENPV)) {
  169. native_swapgs();
  170. wrgsbase(gsbase);
  171. native_swapgs();
  172. } else {
  173. instrumentation_begin();
  174. wrmsrl(MSR_KERNEL_GS_BASE, gsbase);
  175. instrumentation_end();
  176. }
  177. }
  178. /*
  179. * Saves the FS or GS base for an outgoing thread if FSGSBASE extensions are
  180. * not available. The goal is to be reasonably fast on non-FSGSBASE systems.
  181. * It's forcibly inlined because it'll generate better code and this function
  182. * is hot.
  183. */
  184. static __always_inline void save_base_legacy(struct task_struct *prev_p,
  185. unsigned short selector,
  186. enum which_selector which)
  187. {
  188. if (likely(selector == 0)) {
  189. /*
  190. * On Intel (without X86_BUG_NULL_SEG), the segment base could
  191. * be the pre-existing saved base or it could be zero. On AMD
  192. * (with X86_BUG_NULL_SEG), the segment base could be almost
  193. * anything.
  194. *
  195. * This branch is very hot (it's hit twice on almost every
  196. * context switch between 64-bit programs), and avoiding
  197. * the RDMSR helps a lot, so we just assume that whatever
  198. * value is already saved is correct. This matches historical
  199. * Linux behavior, so it won't break existing applications.
  200. *
  201. * To avoid leaking state, on non-X86_BUG_NULL_SEG CPUs, if we
  202. * report that the base is zero, it needs to actually be zero:
  203. * see the corresponding logic in load_seg_legacy.
  204. */
  205. } else {
  206. /*
  207. * If the selector is 1, 2, or 3, then the base is zero on
  208. * !X86_BUG_NULL_SEG CPUs and could be anything on
  209. * X86_BUG_NULL_SEG CPUs. In the latter case, Linux
  210. * has never attempted to preserve the base across context
  211. * switches.
  212. *
  213. * If selector > 3, then it refers to a real segment, and
  214. * saving the base isn't necessary.
  215. */
  216. if (which == FS)
  217. prev_p->thread.fsbase = 0;
  218. else
  219. prev_p->thread.gsbase = 0;
  220. }
  221. }
  222. static __always_inline void save_fsgs(struct task_struct *task)
  223. {
  224. savesegment(fs, task->thread.fsindex);
  225. savesegment(gs, task->thread.gsindex);
  226. if (static_cpu_has(X86_FEATURE_FSGSBASE)) {
  227. /*
  228. * If FSGSBASE is enabled, we can't make any useful guesses
  229. * about the base, and user code expects us to save the current
  230. * value. Fortunately, reading the base directly is efficient.
  231. */
  232. task->thread.fsbase = rdfsbase();
  233. task->thread.gsbase = __rdgsbase_inactive();
  234. } else {
  235. save_base_legacy(task, task->thread.fsindex, FS);
  236. save_base_legacy(task, task->thread.gsindex, GS);
  237. }
  238. }
  239. /*
  240. * While a process is running,current->thread.fsbase and current->thread.gsbase
  241. * may not match the corresponding CPU registers (see save_base_legacy()).
  242. */
  243. void current_save_fsgs(void)
  244. {
  245. unsigned long flags;
  246. /* Interrupts need to be off for FSGSBASE */
  247. local_irq_save(flags);
  248. save_fsgs(current);
  249. local_irq_restore(flags);
  250. }
  251. #if IS_ENABLED(CONFIG_KVM)
  252. EXPORT_SYMBOL_GPL(current_save_fsgs);
  253. #endif
  254. static __always_inline void loadseg(enum which_selector which,
  255. unsigned short sel)
  256. {
  257. if (which == FS)
  258. loadsegment(fs, sel);
  259. else
  260. load_gs_index(sel);
  261. }
  262. static __always_inline void load_seg_legacy(unsigned short prev_index,
  263. unsigned long prev_base,
  264. unsigned short next_index,
  265. unsigned long next_base,
  266. enum which_selector which)
  267. {
  268. if (likely(next_index <= 3)) {
  269. /*
  270. * The next task is using 64-bit TLS, is not using this
  271. * segment at all, or is having fun with arcane CPU features.
  272. */
  273. if (next_base == 0) {
  274. /*
  275. * Nasty case: on AMD CPUs, we need to forcibly zero
  276. * the base.
  277. */
  278. if (static_cpu_has_bug(X86_BUG_NULL_SEG)) {
  279. loadseg(which, __USER_DS);
  280. loadseg(which, next_index);
  281. } else {
  282. /*
  283. * We could try to exhaustively detect cases
  284. * under which we can skip the segment load,
  285. * but there's really only one case that matters
  286. * for performance: if both the previous and
  287. * next states are fully zeroed, we can skip
  288. * the load.
  289. *
  290. * (This assumes that prev_base == 0 has no
  291. * false positives. This is the case on
  292. * Intel-style CPUs.)
  293. */
  294. if (likely(prev_index | next_index | prev_base))
  295. loadseg(which, next_index);
  296. }
  297. } else {
  298. if (prev_index != next_index)
  299. loadseg(which, next_index);
  300. wrmsrl(which == FS ? MSR_FS_BASE : MSR_KERNEL_GS_BASE,
  301. next_base);
  302. }
  303. } else {
  304. /*
  305. * The next task is using a real segment. Loading the selector
  306. * is sufficient.
  307. */
  308. loadseg(which, next_index);
  309. }
  310. }
  311. /*
  312. * Store prev's PKRU value and load next's PKRU value if they differ. PKRU
  313. * is not XSTATE managed on context switch because that would require a
  314. * lookup in the task's FPU xsave buffer and require to keep that updated
  315. * in various places.
  316. */
  317. static __always_inline void x86_pkru_load(struct thread_struct *prev,
  318. struct thread_struct *next)
  319. {
  320. if (!cpu_feature_enabled(X86_FEATURE_OSPKE))
  321. return;
  322. /* Stash the prev task's value: */
  323. prev->pkru = rdpkru();
  324. /*
  325. * PKRU writes are slightly expensive. Avoid them when not
  326. * strictly necessary:
  327. */
  328. if (prev->pkru != next->pkru)
  329. wrpkru(next->pkru);
  330. }
  331. static __always_inline void x86_fsgsbase_load(struct thread_struct *prev,
  332. struct thread_struct *next)
  333. {
  334. if (static_cpu_has(X86_FEATURE_FSGSBASE)) {
  335. /* Update the FS and GS selectors if they could have changed. */
  336. if (unlikely(prev->fsindex || next->fsindex))
  337. loadseg(FS, next->fsindex);
  338. if (unlikely(prev->gsindex || next->gsindex))
  339. loadseg(GS, next->gsindex);
  340. /* Update the bases. */
  341. wrfsbase(next->fsbase);
  342. __wrgsbase_inactive(next->gsbase);
  343. } else {
  344. load_seg_legacy(prev->fsindex, prev->fsbase,
  345. next->fsindex, next->fsbase, FS);
  346. load_seg_legacy(prev->gsindex, prev->gsbase,
  347. next->gsindex, next->gsbase, GS);
  348. }
  349. }
  350. unsigned long x86_fsgsbase_read_task(struct task_struct *task,
  351. unsigned short selector)
  352. {
  353. unsigned short idx = selector >> 3;
  354. unsigned long base;
  355. if (likely((selector & SEGMENT_TI_MASK) == 0)) {
  356. if (unlikely(idx >= GDT_ENTRIES))
  357. return 0;
  358. /*
  359. * There are no user segments in the GDT with nonzero bases
  360. * other than the TLS segments.
  361. */
  362. if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
  363. return 0;
  364. idx -= GDT_ENTRY_TLS_MIN;
  365. base = get_desc_base(&task->thread.tls_array[idx]);
  366. } else {
  367. #ifdef CONFIG_MODIFY_LDT_SYSCALL
  368. struct ldt_struct *ldt;
  369. /*
  370. * If performance here mattered, we could protect the LDT
  371. * with RCU. This is a slow path, though, so we can just
  372. * take the mutex.
  373. */
  374. mutex_lock(&task->mm->context.lock);
  375. ldt = task->mm->context.ldt;
  376. if (unlikely(!ldt || idx >= ldt->nr_entries))
  377. base = 0;
  378. else
  379. base = get_desc_base(ldt->entries + idx);
  380. mutex_unlock(&task->mm->context.lock);
  381. #else
  382. base = 0;
  383. #endif
  384. }
  385. return base;
  386. }
  387. unsigned long x86_gsbase_read_cpu_inactive(void)
  388. {
  389. unsigned long gsbase;
  390. if (boot_cpu_has(X86_FEATURE_FSGSBASE)) {
  391. unsigned long flags;
  392. local_irq_save(flags);
  393. gsbase = __rdgsbase_inactive();
  394. local_irq_restore(flags);
  395. } else {
  396. rdmsrl(MSR_KERNEL_GS_BASE, gsbase);
  397. }
  398. return gsbase;
  399. }
  400. void x86_gsbase_write_cpu_inactive(unsigned long gsbase)
  401. {
  402. if (boot_cpu_has(X86_FEATURE_FSGSBASE)) {
  403. unsigned long flags;
  404. local_irq_save(flags);
  405. __wrgsbase_inactive(gsbase);
  406. local_irq_restore(flags);
  407. } else {
  408. wrmsrl(MSR_KERNEL_GS_BASE, gsbase);
  409. }
  410. }
  411. unsigned long x86_fsbase_read_task(struct task_struct *task)
  412. {
  413. unsigned long fsbase;
  414. if (task == current)
  415. fsbase = x86_fsbase_read_cpu();
  416. else if (boot_cpu_has(X86_FEATURE_FSGSBASE) ||
  417. (task->thread.fsindex == 0))
  418. fsbase = task->thread.fsbase;
  419. else
  420. fsbase = x86_fsgsbase_read_task(task, task->thread.fsindex);
  421. return fsbase;
  422. }
  423. unsigned long x86_gsbase_read_task(struct task_struct *task)
  424. {
  425. unsigned long gsbase;
  426. if (task == current)
  427. gsbase = x86_gsbase_read_cpu_inactive();
  428. else if (boot_cpu_has(X86_FEATURE_FSGSBASE) ||
  429. (task->thread.gsindex == 0))
  430. gsbase = task->thread.gsbase;
  431. else
  432. gsbase = x86_fsgsbase_read_task(task, task->thread.gsindex);
  433. return gsbase;
  434. }
  435. void x86_fsbase_write_task(struct task_struct *task, unsigned long fsbase)
  436. {
  437. WARN_ON_ONCE(task == current);
  438. task->thread.fsbase = fsbase;
  439. }
  440. void x86_gsbase_write_task(struct task_struct *task, unsigned long gsbase)
  441. {
  442. WARN_ON_ONCE(task == current);
  443. task->thread.gsbase = gsbase;
  444. }
  445. static void
  446. start_thread_common(struct pt_regs *regs, unsigned long new_ip,
  447. unsigned long new_sp,
  448. unsigned int _cs, unsigned int _ss, unsigned int _ds)
  449. {
  450. WARN_ON_ONCE(regs != current_pt_regs());
  451. if (static_cpu_has(X86_BUG_NULL_SEG)) {
  452. /* Loading zero below won't clear the base. */
  453. loadsegment(fs, __USER_DS);
  454. load_gs_index(__USER_DS);
  455. }
  456. loadsegment(fs, 0);
  457. loadsegment(es, _ds);
  458. loadsegment(ds, _ds);
  459. load_gs_index(0);
  460. regs->ip = new_ip;
  461. regs->sp = new_sp;
  462. regs->cs = _cs;
  463. regs->ss = _ss;
  464. regs->flags = X86_EFLAGS_IF;
  465. }
  466. void
  467. start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
  468. {
  469. start_thread_common(regs, new_ip, new_sp,
  470. __USER_CS, __USER_DS, 0);
  471. }
  472. EXPORT_SYMBOL_GPL(start_thread);
  473. #ifdef CONFIG_COMPAT
  474. void compat_start_thread(struct pt_regs *regs, u32 new_ip, u32 new_sp, bool x32)
  475. {
  476. start_thread_common(regs, new_ip, new_sp,
  477. x32 ? __USER_CS : __USER32_CS,
  478. __USER_DS, __USER_DS);
  479. }
  480. #endif
  481. /*
  482. * switch_to(x,y) should switch tasks from x to y.
  483. *
  484. * This could still be optimized:
  485. * - fold all the options into a flag word and test it with a single test.
  486. * - could test fs/gs bitsliced
  487. *
  488. * Kprobes not supported here. Set the probe on schedule instead.
  489. * Function graph tracer not supported too.
  490. */
  491. __no_kmsan_checks
  492. __visible __notrace_funcgraph struct task_struct *
  493. __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
  494. {
  495. struct thread_struct *prev = &prev_p->thread;
  496. struct thread_struct *next = &next_p->thread;
  497. struct fpu *prev_fpu = &prev->fpu;
  498. int cpu = smp_processor_id();
  499. WARN_ON_ONCE(IS_ENABLED(CONFIG_DEBUG_ENTRY) &&
  500. this_cpu_read(hardirq_stack_inuse));
  501. if (!test_thread_flag(TIF_NEED_FPU_LOAD))
  502. switch_fpu_prepare(prev_fpu, cpu);
  503. /* We must save %fs and %gs before load_TLS() because
  504. * %fs and %gs may be cleared by load_TLS().
  505. *
  506. * (e.g. xen_load_tls())
  507. */
  508. save_fsgs(prev_p);
  509. /*
  510. * Load TLS before restoring any segments so that segment loads
  511. * reference the correct GDT entries.
  512. */
  513. load_TLS(next, cpu);
  514. /*
  515. * Leave lazy mode, flushing any hypercalls made here. This
  516. * must be done after loading TLS entries in the GDT but before
  517. * loading segments that might reference them.
  518. */
  519. arch_end_context_switch(next_p);
  520. /* Switch DS and ES.
  521. *
  522. * Reading them only returns the selectors, but writing them (if
  523. * nonzero) loads the full descriptor from the GDT or LDT. The
  524. * LDT for next is loaded in switch_mm, and the GDT is loaded
  525. * above.
  526. *
  527. * We therefore need to write new values to the segment
  528. * registers on every context switch unless both the new and old
  529. * values are zero.
  530. *
  531. * Note that we don't need to do anything for CS and SS, as
  532. * those are saved and restored as part of pt_regs.
  533. */
  534. savesegment(es, prev->es);
  535. if (unlikely(next->es | prev->es))
  536. loadsegment(es, next->es);
  537. savesegment(ds, prev->ds);
  538. if (unlikely(next->ds | prev->ds))
  539. loadsegment(ds, next->ds);
  540. x86_fsgsbase_load(prev, next);
  541. x86_pkru_load(prev, next);
  542. /*
  543. * Switch the PDA and FPU contexts.
  544. */
  545. this_cpu_write(current_task, next_p);
  546. this_cpu_write(cpu_current_top_of_stack, task_top_of_stack(next_p));
  547. switch_fpu_finish();
  548. /* Reload sp0. */
  549. update_task_stack(next_p);
  550. switch_to_extra(prev_p, next_p);
  551. if (static_cpu_has_bug(X86_BUG_SYSRET_SS_ATTRS)) {
  552. /*
  553. * AMD CPUs have a misfeature: SYSRET sets the SS selector but
  554. * does not update the cached descriptor. As a result, if we
  555. * do SYSRET while SS is NULL, we'll end up in user mode with
  556. * SS apparently equal to __USER_DS but actually unusable.
  557. *
  558. * The straightforward workaround would be to fix it up just
  559. * before SYSRET, but that would slow down the system call
  560. * fast paths. Instead, we ensure that SS is never NULL in
  561. * system call context. We do this by replacing NULL SS
  562. * selectors at every context switch. SYSCALL sets up a valid
  563. * SS, so the only way to get NULL is to re-enter the kernel
  564. * from CPL 3 through an interrupt. Since that can't happen
  565. * in the same task as a running syscall, we are guaranteed to
  566. * context switch between every interrupt vector entry and a
  567. * subsequent SYSRET.
  568. *
  569. * We read SS first because SS reads are much faster than
  570. * writes. Out of caution, we force SS to __KERNEL_DS even if
  571. * it previously had a different non-NULL value.
  572. */
  573. unsigned short ss_sel;
  574. savesegment(ss, ss_sel);
  575. if (ss_sel != __KERNEL_DS)
  576. loadsegment(ss, __KERNEL_DS);
  577. }
  578. /* Load the Intel cache allocation PQR MSR. */
  579. resctrl_sched_in(next_p);
  580. return prev_p;
  581. }
  582. void set_personality_64bit(void)
  583. {
  584. /* inherit personality from parent */
  585. /* Make sure to be in 64bit mode */
  586. clear_thread_flag(TIF_ADDR32);
  587. /* Pretend that this comes from a 64bit execve */
  588. task_pt_regs(current)->orig_ax = __NR_execve;
  589. current_thread_info()->status &= ~TS_COMPAT;
  590. if (current->mm)
  591. current->mm->context.flags = MM_CONTEXT_HAS_VSYSCALL;
  592. /* TBD: overwrites user setup. Should have two bits.
  593. But 64bit processes have always behaved this way,
  594. so it's not too bad. The main problem is just that
  595. 32bit children are affected again. */
  596. current->personality &= ~READ_IMPLIES_EXEC;
  597. }
  598. static void __set_personality_x32(void)
  599. {
  600. #ifdef CONFIG_X86_X32_ABI
  601. if (current->mm)
  602. current->mm->context.flags = 0;
  603. current->personality &= ~READ_IMPLIES_EXEC;
  604. /*
  605. * in_32bit_syscall() uses the presence of the x32 syscall bit
  606. * flag to determine compat status. The x86 mmap() code relies on
  607. * the syscall bitness so set x32 syscall bit right here to make
  608. * in_32bit_syscall() work during exec().
  609. *
  610. * Pretend to come from a x32 execve.
  611. */
  612. task_pt_regs(current)->orig_ax = __NR_x32_execve | __X32_SYSCALL_BIT;
  613. current_thread_info()->status &= ~TS_COMPAT;
  614. #endif
  615. }
  616. static void __set_personality_ia32(void)
  617. {
  618. #ifdef CONFIG_IA32_EMULATION
  619. if (current->mm) {
  620. /*
  621. * uprobes applied to this MM need to know this and
  622. * cannot use user_64bit_mode() at that time.
  623. */
  624. current->mm->context.flags = MM_CONTEXT_UPROBE_IA32;
  625. }
  626. current->personality |= force_personality32;
  627. /* Prepare the first "return" to user space */
  628. task_pt_regs(current)->orig_ax = __NR_ia32_execve;
  629. current_thread_info()->status |= TS_COMPAT;
  630. #endif
  631. }
  632. void set_personality_ia32(bool x32)
  633. {
  634. /* Make sure to be in 32bit mode */
  635. set_thread_flag(TIF_ADDR32);
  636. if (x32)
  637. __set_personality_x32();
  638. else
  639. __set_personality_ia32();
  640. }
  641. EXPORT_SYMBOL_GPL(set_personality_ia32);
  642. #ifdef CONFIG_CHECKPOINT_RESTORE
  643. static long prctl_map_vdso(const struct vdso_image *image, unsigned long addr)
  644. {
  645. int ret;
  646. ret = map_vdso_once(image, addr);
  647. if (ret)
  648. return ret;
  649. return (long)image->size;
  650. }
  651. #endif
  652. long do_arch_prctl_64(struct task_struct *task, int option, unsigned long arg2)
  653. {
  654. int ret = 0;
  655. switch (option) {
  656. case ARCH_SET_GS: {
  657. if (unlikely(arg2 >= TASK_SIZE_MAX))
  658. return -EPERM;
  659. preempt_disable();
  660. /*
  661. * ARCH_SET_GS has always overwritten the index
  662. * and the base. Zero is the most sensible value
  663. * to put in the index, and is the only value that
  664. * makes any sense if FSGSBASE is unavailable.
  665. */
  666. if (task == current) {
  667. loadseg(GS, 0);
  668. x86_gsbase_write_cpu_inactive(arg2);
  669. /*
  670. * On non-FSGSBASE systems, save_base_legacy() expects
  671. * that we also fill in thread.gsbase.
  672. */
  673. task->thread.gsbase = arg2;
  674. } else {
  675. task->thread.gsindex = 0;
  676. x86_gsbase_write_task(task, arg2);
  677. }
  678. preempt_enable();
  679. break;
  680. }
  681. case ARCH_SET_FS: {
  682. /*
  683. * Not strictly needed for %fs, but do it for symmetry
  684. * with %gs
  685. */
  686. if (unlikely(arg2 >= TASK_SIZE_MAX))
  687. return -EPERM;
  688. preempt_disable();
  689. /*
  690. * Set the selector to 0 for the same reason
  691. * as %gs above.
  692. */
  693. if (task == current) {
  694. loadseg(FS, 0);
  695. x86_fsbase_write_cpu(arg2);
  696. /*
  697. * On non-FSGSBASE systems, save_base_legacy() expects
  698. * that we also fill in thread.fsbase.
  699. */
  700. task->thread.fsbase = arg2;
  701. } else {
  702. task->thread.fsindex = 0;
  703. x86_fsbase_write_task(task, arg2);
  704. }
  705. preempt_enable();
  706. break;
  707. }
  708. case ARCH_GET_FS: {
  709. unsigned long base = x86_fsbase_read_task(task);
  710. ret = put_user(base, (unsigned long __user *)arg2);
  711. break;
  712. }
  713. case ARCH_GET_GS: {
  714. unsigned long base = x86_gsbase_read_task(task);
  715. ret = put_user(base, (unsigned long __user *)arg2);
  716. break;
  717. }
  718. #ifdef CONFIG_CHECKPOINT_RESTORE
  719. # ifdef CONFIG_X86_X32_ABI
  720. case ARCH_MAP_VDSO_X32:
  721. return prctl_map_vdso(&vdso_image_x32, arg2);
  722. # endif
  723. # if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  724. case ARCH_MAP_VDSO_32:
  725. return prctl_map_vdso(&vdso_image_32, arg2);
  726. # endif
  727. case ARCH_MAP_VDSO_64:
  728. return prctl_map_vdso(&vdso_image_64, arg2);
  729. #endif
  730. default:
  731. ret = -EINVAL;
  732. break;
  733. }
  734. return ret;
  735. }
  736. SYSCALL_DEFINE2(arch_prctl, int, option, unsigned long, arg2)
  737. {
  738. long ret;
  739. ret = do_arch_prctl_64(current, option, arg2);
  740. if (ret == -EINVAL)
  741. ret = do_arch_prctl_common(option, arg2);
  742. return ret;
  743. }
  744. #ifdef CONFIG_IA32_EMULATION
  745. COMPAT_SYSCALL_DEFINE2(arch_prctl, int, option, unsigned long, arg2)
  746. {
  747. return do_arch_prctl_common(option, arg2);
  748. }
  749. #endif
  750. unsigned long KSTK_ESP(struct task_struct *task)
  751. {
  752. return task_pt_regs(task)->sp;
  753. }