blob: 0917c7f25720be91372bacddb1a3032323b8996f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1995 Linus Torvalds
3 *
4 * Pentium III FXSR, SSE support
5 * Gareth Hughes <gareth@valinux.com>, May 2000
6 */
7
8/*
9 * This file handles the architecture-dependent parts of process handling..
10 */
11
Zwane Mwaikambof3705132005-06-25 14:54:50 -070012#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/errno.h>
14#include <linux/sched.h>
Ingo Molnar29930022017-02-08 18:51:36 +010015#include <linux/sched/task.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010016#include <linux/sched/task_stack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/fs.h>
18#include <linux/kernel.h>
19#include <linux/mm.h>
20#include <linux/elfcore.h>
21#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/stddef.h>
23#include <linux/slab.h>
24#include <linux/vmalloc.h>
25#include <linux/user.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/delay.h>
28#include <linux/reboot.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/mc146818rtc.h>
Paul Gortmaker186f4362016-07-13 20:18:56 -040030#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/kallsyms.h>
32#include <linux/ptrace.h>
Andi Kleenc16b63e02006-09-26 10:52:28 +020033#include <linux/personality.h>
Jeremy Fitzhardinge7c3576d2007-05-02 19:27:16 +020034#include <linux/percpu.h>
Erik Bosman529e25f2008-04-14 00:24:18 +020035#include <linux/prctl.h>
Frederic Weisbecker8b96f012008-12-06 03:40:00 +010036#include <linux/ftrace.h>
Jaswinder Singh Rajputbefa9e72009-01-04 16:18:56 +053037#include <linux/uaccess.h>
38#include <linux/io.h>
39#include <linux/kdebug.h>
Kyle Huey79170fd2017-03-20 01:16:24 -070040#include <linux/syscalls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/ldt.h>
43#include <asm/processor.h>
Thomas Gleixner63e81802021-10-15 03:16:20 +020044#include <asm/fpu/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/desc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/err.h>
48
Zwane Mwaikambof3705132005-06-25 14:54:50 -070049#include <asm/tlbflush.h>
50#include <asm/cpu.h>
K.Prasad66cb5912009-06-01 23:44:55 +053051#include <asm/debugreg.h>
David Howellsf05e7982012-03-28 18:11:12 +010052#include <asm/switch_to.h>
Brian Gerstba3e1272015-07-29 01:41:21 -040053#include <asm/vm86.h>
Reinette Chatre8dd97c62020-05-05 15:36:12 -070054#include <asm/resctrl.h>
Kyle Huey79170fd2017-03-20 01:16:24 -070055#include <asm/proto.h>
Zwane Mwaikambof3705132005-06-25 14:54:50 -070056
Thomas Gleixnerff167012018-11-25 19:33:47 +010057#include "process.h"
58
Dmitry Safonov44e21532020-06-29 15:48:46 +010059void __show_regs(struct pt_regs *regs, enum show_regs_mode mode,
60 const char *log_lvl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
62 unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L;
Alan Sternbb1995d2007-07-21 17:10:42 +020063 unsigned long d0, d1, d2, d3, d6, d7;
Peter Zijlstra3c88c692019-05-07 23:25:54 +020064 unsigned short gs;
Pavel Emelyanov9d975eb2007-10-19 20:35:03 +020065
Brian Gerst3a24a602022-03-25 11:39:52 -040066 savesegment(gs, gs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Dmitry Safonov44e21532020-06-29 15:48:46 +010068 show_ip(regs, log_lvl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Dmitry Safonov44e21532020-06-29 15:48:46 +010070 printk("%sEAX: %08lx EBX: %08lx ECX: %08lx EDX: %08lx\n",
71 log_lvl, regs->ax, regs->bx, regs->cx, regs->dx);
72 printk("%sESI: %08lx EDI: %08lx EBP: %08lx ESP: %08lx\n",
73 log_lvl, regs->si, regs->di, regs->bp, regs->sp);
74 printk("%sDS: %04x ES: %04x FS: %04x GS: %04x SS: %04x EFLAGS: %08lx\n",
75 log_lvl, (u16)regs->ds, (u16)regs->es, (u16)regs->fs, gs, regs->ss, regs->flags);
Pavel Emelyanov9d975eb2007-10-19 20:35:03 +020076
Jann Horn9fe62992018-08-31 21:41:51 +020077 if (mode != SHOW_REGS_ALL)
Pavel Emelyanov9d975eb2007-10-19 20:35:03 +020078 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Zachary Amsden4bb0d3e2005-09-03 15:56:36 -070080 cr0 = read_cr0();
81 cr2 = read_cr2();
Andy Lutomirski6c690ee2017-06-12 10:26:14 -070082 cr3 = __read_cr3();
Andy Lutomirski1ef55be12016-09-29 12:48:12 -070083 cr4 = __read_cr4();
Dmitry Safonov44e21532020-06-29 15:48:46 +010084 printk("%sCR0: %08lx CR2: %08lx CR3: %08lx CR4: %08lx\n",
85 log_lvl, cr0, cr2, cr3, cr4);
Alan Sternbb1995d2007-07-21 17:10:42 +020086
87 get_debugreg(d0, 0);
88 get_debugreg(d1, 1);
89 get_debugreg(d2, 2);
90 get_debugreg(d3, 3);
Alan Sternbb1995d2007-07-21 17:10:42 +020091 get_debugreg(d6, 6);
92 get_debugreg(d7, 7);
Dave Jones4338774c2013-06-18 12:09:11 -040093
94 /* Only print out debug registers if they are in their non-default state. */
95 if ((d0 == 0) && (d1 == 0) && (d2 == 0) && (d3 == 0) &&
96 (d6 == DR6_RESERVED) && (d7 == 0x400))
97 return;
98
Dmitry Safonov44e21532020-06-29 15:48:46 +010099 printk("%sDR0: %08lx DR1: %08lx DR2: %08lx DR3: %08lx\n",
100 log_lvl, d0, d1, d2, d3);
101 printk("%sDR6: %08lx DR7: %08lx\n",
102 log_lvl, d6, d7);
Pavel Emelyanov9d975eb2007-10-19 20:35:03 +0200103}
Alan Sternbb1995d2007-07-21 17:10:42 +0200104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105void release_thread(struct task_struct *dead_task)
106{
Zachary Amsden26849272006-01-06 00:11:59 -0800107 BUG_ON(dead_task->mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 release_vm86_irqs(dead_task);
109}
110
Ingo Molnar513ad842008-02-21 05:18:40 +0100111void
112start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
113{
Brian Gerst3a24a602022-03-25 11:39:52 -0400114 loadsegment(gs, 0);
Ingo Molnar513ad842008-02-21 05:18:40 +0100115 regs->fs = 0;
Ingo Molnar513ad842008-02-21 05:18:40 +0100116 regs->ds = __USER_DS;
117 regs->es = __USER_DS;
118 regs->ss = __USER_DS;
119 regs->cs = __USER_CS;
120 regs->ip = new_ip;
121 regs->sp = new_sp;
Al Viro6783eaa22012-08-02 23:05:11 +0400122 regs->flags = X86_EFLAGS_IF;
Ingo Molnar513ad842008-02-21 05:18:40 +0100123}
124EXPORT_SYMBOL_GPL(start_thread);
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127/*
Kamalesh Babulalea70ef32011-04-28 14:32:08 +0530128 * switch_to(x,y) should switch tasks from x to y.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 *
130 * We fsave/fwait so that an exception goes off at the right time
131 * (as a call from the fsave or fwait in effect) rather than to
132 * the wrong process. Lazy FP saving no longer makes any sense
133 * with modern CPU's, and this simplifies a lot of things (SMP
134 * and UP become the same).
135 *
136 * NOTE! We used to use the x86 hardware context switching. The
137 * reason for not using it any more becomes apparent when you
138 * try to recover gracefully from saved state that is no longer
139 * valid (stale segment register values in particular). With the
140 * hardware task-switch, there is no way to fix up bad state in
141 * a reasonable manner.
142 *
143 * The fact that Intel documents the hardware task-switching to
144 * be slow is a fairly red herring - this code is not noticeably
145 * faster. However, there _is_ some room for improvement here,
146 * so the performance issues may eventually be a valid point.
147 * More important, however, is the fact that this allows us much
148 * more flexibility.
149 *
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100150 * The return value (in %ax) will be the "prev" task after
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 * the task-switch, and shows up in ret_from_fork in entry.S,
152 * for example.
153 */
Andi Kleen35ea79032013-08-05 15:02:39 -0700154__visible __notrace_funcgraph struct task_struct *
Frederic Weisbecker8b96f012008-12-06 03:40:00 +0100155__switch_to(struct task_struct *prev_p, struct task_struct *next_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 struct thread_struct *prev = &prev_p->thread,
Ingo Molnar384a23f2015-04-23 17:43:27 +0200158 *next = &next_p->thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 /* never put a printk in __switch_to... printk() calls wake_up*() indirectly */
162
Linus Torvalds24b8a232023-10-18 20:41:58 +0200163 if (!test_tsk_thread_flag(prev_p, TIF_NEED_FPU_LOAD))
164 switch_fpu_prepare(prev_p, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 /*
Jeremy Fitzhardinge464d1a72007-02-13 13:26:20 +0100167 * Save away %gs. No need to save %fs, as it was saved on the
Jeremy Fitzhardingef95d47c2006-12-07 02:14:02 +0100168 * stack on entry. No need to save %es and %ds, as those are
169 * always kernel segments while inside the kernel. Doing this
170 * before setting the new TLS descriptors avoids the situation
171 * where we temporarily have non-reloadable segments in %fs
172 * and %gs. This could be an issue if the NMI handler ever
173 * used %fs or %gs (it does not today), or if the kernel is
174 * running inside of a hypervisor layer.
Zachary Amsdene7a2ff52005-09-03 15:56:39 -0700175 */
Brian Gerst3a24a602022-03-25 11:39:52 -0400176 savesegment(gs, prev->gs);
Zachary Amsdene7a2ff52005-09-03 15:56:39 -0700177
178 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 * Load the per-thread Thread-Local Storage descriptor.
180 */
181 load_TLS(next, cpu);
182
Thomas Gleixnerff167012018-11-25 19:33:47 +0100183 switch_to_extra(prev_p, next_p);
Andrea Arcangeliffaa8bd2005-06-27 14:36:36 -0700184
Zachary Amsden9226d122007-02-13 13:26:21 +0100185 /*
186 * Leave lazy mode, flushing any hypercalls made here.
187 * This must be done before restoring TLS segments so
Sebastian Andrzej Siewior6dd677a2019-04-03 18:41:31 +0200188 * the GDT and LDT are properly updated.
Zachary Amsden9226d122007-02-13 13:26:21 +0100189 */
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -0800190 arch_end_context_switch(next_p);
Zachary Amsden9226d122007-02-13 13:26:21 +0100191
Andy Lutomirskib27559a2015-03-06 17:50:18 -0800192 /*
Thomas Gleixnerc063a212022-09-15 13:11:04 +0200193 * Reload esp0 and pcpu_hot.top_of_stack. This changes
Andy Lutomirskibd7dc5a2017-11-02 00:59:09 -0700194 * current_thread_info(). Refresh the SYSENTER configuration in
195 * case prev or next is vm86.
Andy Lutomirskib27559a2015-03-06 17:50:18 -0800196 */
Joerg Roedel252e1a02018-07-18 11:40:51 +0200197 update_task_stack(next_p);
Andy Lutomirskibd7dc5a2017-11-02 00:59:09 -0700198 refresh_sysenter_cs(next);
Thomas Gleixnerc063a212022-09-15 13:11:04 +0200199 this_cpu_write(pcpu_hot.top_of_stack,
Andy Lutomirskia7fcf282015-03-06 17:50:19 -0800200 (unsigned long)task_stack_page(next_p) +
201 THREAD_SIZE);
Steven Rostedt198d2082014-02-06 09:41:31 -0500202
Zachary Amsden9226d122007-02-13 13:26:21 +0100203 /*
204 * Restore %gs if needed (which is common)
205 */
206 if (prev->gs | next->gs)
Brian Gerst3a24a602022-03-25 11:39:52 -0400207 loadsegment(gs, next->gs);
Zachary Amsden9226d122007-02-13 13:26:21 +0100208
Thomas Gleixnere57ef2e2022-09-15 13:11:01 +0200209 raw_cpu_write(pcpu_hot.current_task, next_p);
Zachary Amsden9226d122007-02-13 13:26:21 +0100210
Linus Torvalds24b8a232023-10-18 20:41:58 +0200211 switch_fpu_finish(next_p);
Sebastian Andrzej Siewior27221462019-04-03 18:41:36 +0200212
Fenghua Yu4f341a52016-10-28 15:04:48 -0700213 /* Load the Intel cache allocation PQR MSR. */
Linus Torvalds7fef0992023-03-07 13:06:29 -0800214 resctrl_sched_in(next_p);
Fenghua Yu4f341a52016-10-28 15:04:48 -0700215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return prev_p;
217}
Kyle Huey79170fd2017-03-20 01:16:24 -0700218
219SYSCALL_DEFINE2(arch_prctl, int, option, unsigned long, arg2)
220{
Thomas Gleixnerf5c0b4f2022-05-12 14:04:08 +0200221 return do_arch_prctl_common(option, arg2);
Kyle Huey79170fd2017-03-20 01:16:24 -0700222}