process_64.c 26 KB

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