Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * 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 Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 12 | #include <linux/cpu.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/errno.h> |
| 14 | #include <linux/sched.h> |
Ingo Molnar | 2993002 | 2017-02-08 18:51:36 +0100 | [diff] [blame] | 15 | #include <linux/sched/task.h> |
Ingo Molnar | 68db0cf | 2017-02-08 18:51:37 +0100 | [diff] [blame] | 16 | #include <linux/sched/task_stack.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/stddef.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include <linux/vmalloc.h> |
| 25 | #include <linux/user.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/interrupt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/delay.h> |
| 28 | #include <linux/reboot.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/mc146818rtc.h> |
Paul Gortmaker | 186f436 | 2016-07-13 20:18:56 -0400 | [diff] [blame] | 30 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/kallsyms.h> |
| 32 | #include <linux/ptrace.h> |
Andi Kleen | c16b63e0 | 2006-09-26 10:52:28 +0200 | [diff] [blame] | 33 | #include <linux/personality.h> |
Jeremy Fitzhardinge | 7c3576d | 2007-05-02 19:27:16 +0200 | [diff] [blame] | 34 | #include <linux/percpu.h> |
Erik Bosman | 529e25f | 2008-04-14 00:24:18 +0200 | [diff] [blame] | 35 | #include <linux/prctl.h> |
Frederic Weisbecker | 8b96f01 | 2008-12-06 03:40:00 +0100 | [diff] [blame] | 36 | #include <linux/ftrace.h> |
Jaswinder Singh Rajput | befa9e7 | 2009-01-04 16:18:56 +0530 | [diff] [blame] | 37 | #include <linux/uaccess.h> |
| 38 | #include <linux/io.h> |
| 39 | #include <linux/kdebug.h> |
Kyle Huey | 79170fd | 2017-03-20 01:16:24 -0700 | [diff] [blame] | 40 | #include <linux/syscalls.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <asm/ldt.h> |
| 43 | #include <asm/processor.h> |
Thomas Gleixner | 63e8180 | 2021-10-15 03:16:20 +0200 | [diff] [blame] | 44 | #include <asm/fpu/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <asm/desc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include <linux/err.h> |
| 48 | |
Zwane Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 49 | #include <asm/tlbflush.h> |
| 50 | #include <asm/cpu.h> |
K.Prasad | 66cb591 | 2009-06-01 23:44:55 +0530 | [diff] [blame] | 51 | #include <asm/debugreg.h> |
David Howells | f05e798 | 2012-03-28 18:11:12 +0100 | [diff] [blame] | 52 | #include <asm/switch_to.h> |
Brian Gerst | ba3e127 | 2015-07-29 01:41:21 -0400 | [diff] [blame] | 53 | #include <asm/vm86.h> |
Reinette Chatre | 8dd97c6 | 2020-05-05 15:36:12 -0700 | [diff] [blame] | 54 | #include <asm/resctrl.h> |
Kyle Huey | 79170fd | 2017-03-20 01:16:24 -0700 | [diff] [blame] | 55 | #include <asm/proto.h> |
Zwane Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 56 | |
Thomas Gleixner | ff16701 | 2018-11-25 19:33:47 +0100 | [diff] [blame] | 57 | #include "process.h" |
| 58 | |
Dmitry Safonov | 44e2153 | 2020-06-29 15:48:46 +0100 | [diff] [blame] | 59 | void __show_regs(struct pt_regs *regs, enum show_regs_mode mode, |
| 60 | const char *log_lvl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | { |
| 62 | unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L; |
Alan Stern | bb1995d | 2007-07-21 17:10:42 +0200 | [diff] [blame] | 63 | unsigned long d0, d1, d2, d3, d6, d7; |
Peter Zijlstra | 3c88c69 | 2019-05-07 23:25:54 +0200 | [diff] [blame] | 64 | unsigned short gs; |
Pavel Emelyanov | 9d975eb | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 65 | |
Brian Gerst | 3a24a60 | 2022-03-25 11:39:52 -0400 | [diff] [blame] | 66 | savesegment(gs, gs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Dmitry Safonov | 44e2153 | 2020-06-29 15:48:46 +0100 | [diff] [blame] | 68 | show_ip(regs, log_lvl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
Dmitry Safonov | 44e2153 | 2020-06-29 15:48:46 +0100 | [diff] [blame] | 70 | 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 Emelyanov | 9d975eb | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 76 | |
Jann Horn | 9fe6299 | 2018-08-31 21:41:51 +0200 | [diff] [blame] | 77 | if (mode != SHOW_REGS_ALL) |
Pavel Emelyanov | 9d975eb | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 78 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
Zachary Amsden | 4bb0d3e | 2005-09-03 15:56:36 -0700 | [diff] [blame] | 80 | cr0 = read_cr0(); |
| 81 | cr2 = read_cr2(); |
Andy Lutomirski | 6c690ee | 2017-06-12 10:26:14 -0700 | [diff] [blame] | 82 | cr3 = __read_cr3(); |
Andy Lutomirski | 1ef55be1 | 2016-09-29 12:48:12 -0700 | [diff] [blame] | 83 | cr4 = __read_cr4(); |
Dmitry Safonov | 44e2153 | 2020-06-29 15:48:46 +0100 | [diff] [blame] | 84 | printk("%sCR0: %08lx CR2: %08lx CR3: %08lx CR4: %08lx\n", |
| 85 | log_lvl, cr0, cr2, cr3, cr4); |
Alan Stern | bb1995d | 2007-07-21 17:10:42 +0200 | [diff] [blame] | 86 | |
| 87 | get_debugreg(d0, 0); |
| 88 | get_debugreg(d1, 1); |
| 89 | get_debugreg(d2, 2); |
| 90 | get_debugreg(d3, 3); |
Alan Stern | bb1995d | 2007-07-21 17:10:42 +0200 | [diff] [blame] | 91 | get_debugreg(d6, 6); |
| 92 | get_debugreg(d7, 7); |
Dave Jones | 4338774c | 2013-06-18 12:09:11 -0400 | [diff] [blame] | 93 | |
| 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 Safonov | 44e2153 | 2020-06-29 15:48:46 +0100 | [diff] [blame] | 99 | 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 Emelyanov | 9d975eb | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 103 | } |
Alan Stern | bb1995d | 2007-07-21 17:10:42 +0200 | [diff] [blame] | 104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | void release_thread(struct task_struct *dead_task) |
| 106 | { |
Zachary Amsden | 2684927 | 2006-01-06 00:11:59 -0800 | [diff] [blame] | 107 | BUG_ON(dead_task->mm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | release_vm86_irqs(dead_task); |
| 109 | } |
| 110 | |
Ingo Molnar | 513ad84 | 2008-02-21 05:18:40 +0100 | [diff] [blame] | 111 | void |
| 112 | start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp) |
| 113 | { |
Brian Gerst | 3a24a60 | 2022-03-25 11:39:52 -0400 | [diff] [blame] | 114 | loadsegment(gs, 0); |
Ingo Molnar | 513ad84 | 2008-02-21 05:18:40 +0100 | [diff] [blame] | 115 | regs->fs = 0; |
Ingo Molnar | 513ad84 | 2008-02-21 05:18:40 +0100 | [diff] [blame] | 116 | 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 Viro | 6783eaa2 | 2012-08-02 23:05:11 +0400 | [diff] [blame] | 122 | regs->flags = X86_EFLAGS_IF; |
Ingo Molnar | 513ad84 | 2008-02-21 05:18:40 +0100 | [diff] [blame] | 123 | } |
| 124 | EXPORT_SYMBOL_GPL(start_thread); |
| 125 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
| 127 | /* |
Kamalesh Babulal | ea70ef3 | 2011-04-28 14:32:08 +0530 | [diff] [blame] | 128 | * switch_to(x,y) should switch tasks from x to y. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | * |
| 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 Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 150 | * The return value (in %ax) will be the "prev" task after |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | * the task-switch, and shows up in ret_from_fork in entry.S, |
| 152 | * for example. |
| 153 | */ |
Andi Kleen | 35ea7903 | 2013-08-05 15:02:39 -0700 | [diff] [blame] | 154 | __visible __notrace_funcgraph struct task_struct * |
Frederic Weisbecker | 8b96f01 | 2008-12-06 03:40:00 +0100 | [diff] [blame] | 155 | __switch_to(struct task_struct *prev_p, struct task_struct *next_p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | { |
| 157 | struct thread_struct *prev = &prev_p->thread, |
Ingo Molnar | 384a23f | 2015-04-23 17:43:27 +0200 | [diff] [blame] | 158 | *next = &next_p->thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | int cpu = smp_processor_id(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
| 161 | /* never put a printk in __switch_to... printk() calls wake_up*() indirectly */ |
| 162 | |
Linus Torvalds | 24b8a23 | 2023-10-18 20:41:58 +0200 | [diff] [blame] | 163 | if (!test_tsk_thread_flag(prev_p, TIF_NEED_FPU_LOAD)) |
| 164 | switch_fpu_prepare(prev_p, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
| 166 | /* |
Jeremy Fitzhardinge | 464d1a7 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 167 | * Save away %gs. No need to save %fs, as it was saved on the |
Jeremy Fitzhardinge | f95d47c | 2006-12-07 02:14:02 +0100 | [diff] [blame] | 168 | * 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 Amsden | e7a2ff5 | 2005-09-03 15:56:39 -0700 | [diff] [blame] | 175 | */ |
Brian Gerst | 3a24a60 | 2022-03-25 11:39:52 -0400 | [diff] [blame] | 176 | savesegment(gs, prev->gs); |
Zachary Amsden | e7a2ff5 | 2005-09-03 15:56:39 -0700 | [diff] [blame] | 177 | |
| 178 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | * Load the per-thread Thread-Local Storage descriptor. |
| 180 | */ |
| 181 | load_TLS(next, cpu); |
| 182 | |
Thomas Gleixner | ff16701 | 2018-11-25 19:33:47 +0100 | [diff] [blame] | 183 | switch_to_extra(prev_p, next_p); |
Andrea Arcangeli | ffaa8bd | 2005-06-27 14:36:36 -0700 | [diff] [blame] | 184 | |
Zachary Amsden | 9226d12 | 2007-02-13 13:26:21 +0100 | [diff] [blame] | 185 | /* |
| 186 | * Leave lazy mode, flushing any hypercalls made here. |
| 187 | * This must be done before restoring TLS segments so |
Sebastian Andrzej Siewior | 6dd677a | 2019-04-03 18:41:31 +0200 | [diff] [blame] | 188 | * the GDT and LDT are properly updated. |
Zachary Amsden | 9226d12 | 2007-02-13 13:26:21 +0100 | [diff] [blame] | 189 | */ |
Jeremy Fitzhardinge | 224101e | 2009-02-18 11:18:57 -0800 | [diff] [blame] | 190 | arch_end_context_switch(next_p); |
Zachary Amsden | 9226d12 | 2007-02-13 13:26:21 +0100 | [diff] [blame] | 191 | |
Andy Lutomirski | b27559a | 2015-03-06 17:50:18 -0800 | [diff] [blame] | 192 | /* |
Thomas Gleixner | c063a21 | 2022-09-15 13:11:04 +0200 | [diff] [blame] | 193 | * Reload esp0 and pcpu_hot.top_of_stack. This changes |
Andy Lutomirski | bd7dc5a | 2017-11-02 00:59:09 -0700 | [diff] [blame] | 194 | * current_thread_info(). Refresh the SYSENTER configuration in |
| 195 | * case prev or next is vm86. |
Andy Lutomirski | b27559a | 2015-03-06 17:50:18 -0800 | [diff] [blame] | 196 | */ |
Joerg Roedel | 252e1a0 | 2018-07-18 11:40:51 +0200 | [diff] [blame] | 197 | update_task_stack(next_p); |
Andy Lutomirski | bd7dc5a | 2017-11-02 00:59:09 -0700 | [diff] [blame] | 198 | refresh_sysenter_cs(next); |
Thomas Gleixner | c063a21 | 2022-09-15 13:11:04 +0200 | [diff] [blame] | 199 | this_cpu_write(pcpu_hot.top_of_stack, |
Andy Lutomirski | a7fcf28 | 2015-03-06 17:50:19 -0800 | [diff] [blame] | 200 | (unsigned long)task_stack_page(next_p) + |
| 201 | THREAD_SIZE); |
Steven Rostedt | 198d208 | 2014-02-06 09:41:31 -0500 | [diff] [blame] | 202 | |
Zachary Amsden | 9226d12 | 2007-02-13 13:26:21 +0100 | [diff] [blame] | 203 | /* |
| 204 | * Restore %gs if needed (which is common) |
| 205 | */ |
| 206 | if (prev->gs | next->gs) |
Brian Gerst | 3a24a60 | 2022-03-25 11:39:52 -0400 | [diff] [blame] | 207 | loadsegment(gs, next->gs); |
Zachary Amsden | 9226d12 | 2007-02-13 13:26:21 +0100 | [diff] [blame] | 208 | |
Thomas Gleixner | e57ef2e | 2022-09-15 13:11:01 +0200 | [diff] [blame] | 209 | raw_cpu_write(pcpu_hot.current_task, next_p); |
Zachary Amsden | 9226d12 | 2007-02-13 13:26:21 +0100 | [diff] [blame] | 210 | |
Linus Torvalds | 24b8a23 | 2023-10-18 20:41:58 +0200 | [diff] [blame] | 211 | switch_fpu_finish(next_p); |
Sebastian Andrzej Siewior | 2722146 | 2019-04-03 18:41:36 +0200 | [diff] [blame] | 212 | |
Fenghua Yu | 4f341a5 | 2016-10-28 15:04:48 -0700 | [diff] [blame] | 213 | /* Load the Intel cache allocation PQR MSR. */ |
Linus Torvalds | 7fef099 | 2023-03-07 13:06:29 -0800 | [diff] [blame] | 214 | resctrl_sched_in(next_p); |
Fenghua Yu | 4f341a5 | 2016-10-28 15:04:48 -0700 | [diff] [blame] | 215 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | return prev_p; |
| 217 | } |
Kyle Huey | 79170fd | 2017-03-20 01:16:24 -0700 | [diff] [blame] | 218 | |
| 219 | SYSCALL_DEFINE2(arch_prctl, int, option, unsigned long, arg2) |
| 220 | { |
Thomas Gleixner | f5c0b4f | 2022-05-12 14:04:08 +0200 | [diff] [blame] | 221 | return do_arch_prctl_common(option, arg2); |
Kyle Huey | 79170fd | 2017-03-20 01:16:24 -0700 | [diff] [blame] | 222 | } |