Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 2 | /* |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 3 | * Derived from "arch/i386/kernel/process.c" |
| 4 | * Copyright (C) 1995 Linus Torvalds |
| 5 | * |
| 6 | * Updated and modified by Cort Dougan (cort@cs.nmt.edu) and |
| 7 | * Paul Mackerras (paulus@cs.anu.edu.au) |
| 8 | * |
| 9 | * PowerPC version |
| 10 | * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 11 | */ |
| 12 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 13 | #include <linux/errno.h> |
| 14 | #include <linux/sched.h> |
Ingo Molnar | b17b015 | 2017-02-08 18:51:35 +0100 | [diff] [blame] | 15 | #include <linux/sched/debug.h> |
Ingo Molnar | 2993002 | 2017-02-08 18:51:36 +0100 | [diff] [blame] | 16 | #include <linux/sched/task.h> |
Ingo Molnar | 68db0cf | 2017-02-08 18:51:37 +0100 | [diff] [blame] | 17 | #include <linux/sched/task_stack.h> |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 18 | #include <linux/kernel.h> |
| 19 | #include <linux/mm.h> |
| 20 | #include <linux/smp.h> |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 21 | #include <linux/stddef.h> |
| 22 | #include <linux/unistd.h> |
| 23 | #include <linux/ptrace.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/user.h> |
| 26 | #include <linux/elf.h> |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 27 | #include <linux/prctl.h> |
| 28 | #include <linux/init_task.h> |
Paul Gortmaker | 4b16f8e | 2011-07-22 18:24:23 -0400 | [diff] [blame] | 29 | #include <linux/export.h> |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 30 | #include <linux/kallsyms.h> |
| 31 | #include <linux/mqueue.h> |
| 32 | #include <linux/hardirq.h> |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 33 | #include <linux/utsname.h> |
Steven Rostedt | 6794c78 | 2009-02-09 21:10:27 -0800 | [diff] [blame] | 34 | #include <linux/ftrace.h> |
Martin Schwidefsky | 79741dd | 2008-12-31 15:11:38 +0100 | [diff] [blame] | 35 | #include <linux/kernel_stat.h> |
Anton Blanchard | d839088 | 2009-02-22 01:50:03 +0000 | [diff] [blame] | 36 | #include <linux/personality.h> |
| 37 | #include <linux/random.h> |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 38 | #include <linux/hw_breakpoint.h> |
Anton Blanchard | 7b051f6 | 2014-10-13 20:27:15 +1100 | [diff] [blame] | 39 | #include <linux/uaccess.h> |
Daniel Axtens | 7f92bc5 | 2016-01-06 11:45:51 +1100 | [diff] [blame] | 40 | #include <linux/elf-randomize.h> |
Ram Pai | 06bb53b | 2018-01-18 17:50:31 -0800 | [diff] [blame] | 41 | #include <linux/pkeys.h> |
Christophe Leroy | fb2d950 | 2018-10-06 16:51:14 +0000 | [diff] [blame] | 42 | #include <linux/seq_buf.h> |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 43 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 44 | #include <asm/io.h> |
| 45 | #include <asm/processor.h> |
| 46 | #include <asm/mmu.h> |
| 47 | #include <asm/prom.h> |
Michael Ellerman | 76032de | 2005-11-07 13:12:03 +1100 | [diff] [blame] | 48 | #include <asm/machdep.h> |
Paul Mackerras | c6622f6 | 2006-02-24 10:06:59 +1100 | [diff] [blame] | 49 | #include <asm/time.h> |
David Howells | ae3a197 | 2012-03-28 18:30:02 +0100 | [diff] [blame] | 50 | #include <asm/runlatch.h> |
Arnd Bergmann | a7f3184 | 2006-03-23 00:00:08 +0100 | [diff] [blame] | 51 | #include <asm/syscalls.h> |
David Howells | ae3a197 | 2012-03-28 18:30:02 +0100 | [diff] [blame] | 52 | #include <asm/switch_to.h> |
Michael Neuling | fb09692 | 2013-02-13 16:21:37 +0000 | [diff] [blame] | 53 | #include <asm/tm.h> |
David Howells | ae3a197 | 2012-03-28 18:30:02 +0100 | [diff] [blame] | 54 | #include <asm/debug.h> |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 55 | #ifdef CONFIG_PPC64 |
| 56 | #include <asm/firmware.h> |
Madhavan Srinivasan | c2e480b | 2017-12-20 09:25:42 +0530 | [diff] [blame] | 57 | #include <asm/hw_irq.h> |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 58 | #endif |
Anton Blanchard | 7cedd60 | 2014-02-04 16:08:51 +1100 | [diff] [blame] | 59 | #include <asm/code-patching.h> |
Daniel Axtens | 7f92bc5 | 2016-01-06 11:45:51 +1100 | [diff] [blame] | 60 | #include <asm/exec.h> |
Michael Ellerman | 5d31a96 | 2016-03-24 22:04:04 +1100 | [diff] [blame] | 61 | #include <asm/livepatch.h> |
Kevin Hao | b92a226 | 2016-07-23 14:42:40 +0530 | [diff] [blame] | 62 | #include <asm/cpu_has_feature.h> |
Daniel Axtens | 0545d54 | 2016-09-06 15:32:43 +1000 | [diff] [blame] | 63 | #include <asm/asm-prototypes.h> |
Christophe Leroy | c9386bf | 2018-10-09 16:46:25 +1100 | [diff] [blame] | 64 | #include <asm/stacktrace.h> |
Michael Neuling | c1fe190 | 2019-04-01 17:03:12 +1100 | [diff] [blame] | 65 | #include <asm/hw_breakpoint.h> |
Michael Ellerman | 5d31a96 | 2016-03-24 22:04:04 +1100 | [diff] [blame] | 66 | |
Luis Machado | d6a61bf | 2008-07-24 02:10:41 +1000 | [diff] [blame] | 67 | #include <linux/kprobes.h> |
| 68 | #include <linux/kdebug.h> |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 69 | |
Michael Neuling | 8b3c34c | 2013-02-13 16:21:32 +0000 | [diff] [blame] | 70 | /* Transactional Memory debug */ |
| 71 | #ifdef TM_DEBUG_SW |
| 72 | #define TM_DEBUG(x...) printk(KERN_INFO x) |
| 73 | #else |
| 74 | #define TM_DEBUG(x...) do { } while(0) |
| 75 | #endif |
| 76 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 77 | extern unsigned long _get_SP(void); |
| 78 | |
Paul Mackerras | d31626f | 2014-01-13 15:56:29 +1100 | [diff] [blame] | 79 | #ifdef CONFIG_PPC_TRANSACTIONAL_MEM |
Michael Ellerman | 5482053 | 2017-10-12 21:17:18 +1100 | [diff] [blame] | 80 | /* |
| 81 | * Are we running in "Suspend disabled" mode? If so we have to block any |
| 82 | * sigreturn that would get us into suspended state, and we also warn in some |
| 83 | * other paths that we should never reach with suspend disabled. |
| 84 | */ |
| 85 | bool tm_suspend_disabled __ro_after_init = false; |
| 86 | |
Anton Blanchard | b86fd2b | 2015-10-29 11:43:58 +1100 | [diff] [blame] | 87 | static void check_if_tm_restore_required(struct task_struct *tsk) |
Paul Mackerras | d31626f | 2014-01-13 15:56:29 +1100 | [diff] [blame] | 88 | { |
| 89 | /* |
| 90 | * If we are saving the current thread's registers, and the |
| 91 | * thread is in a transactional state, set the TIF_RESTORE_TM |
| 92 | * bit so that we know to restore the registers before |
| 93 | * returning to userspace. |
| 94 | */ |
| 95 | if (tsk == current && tsk->thread.regs && |
| 96 | MSR_TM_ACTIVE(tsk->thread.regs->msr) && |
| 97 | !test_thread_flag(TIF_RESTORE_TM)) { |
Anshuman Khandual | 829023d | 2015-07-06 16:24:10 +0530 | [diff] [blame] | 98 | tsk->thread.ckpt_regs.msr = tsk->thread.regs->msr; |
Paul Mackerras | d31626f | 2014-01-13 15:56:29 +1100 | [diff] [blame] | 99 | set_thread_flag(TIF_RESTORE_TM); |
| 100 | } |
Paul Mackerras | d31626f | 2014-01-13 15:56:29 +1100 | [diff] [blame] | 101 | } |
Cyril Bur | dc16b55 | 2016-09-23 16:18:08 +1000 | [diff] [blame] | 102 | |
Paul Mackerras | d31626f | 2014-01-13 15:56:29 +1100 | [diff] [blame] | 103 | #else |
Anton Blanchard | b86fd2b | 2015-10-29 11:43:58 +1100 | [diff] [blame] | 104 | static inline void check_if_tm_restore_required(struct task_struct *tsk) { } |
Paul Mackerras | d31626f | 2014-01-13 15:56:29 +1100 | [diff] [blame] | 105 | #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */ |
| 106 | |
Anton Blanchard | 3eb5d58 | 2015-10-29 11:44:06 +1100 | [diff] [blame] | 107 | bool strict_msr_control; |
| 108 | EXPORT_SYMBOL(strict_msr_control); |
| 109 | |
| 110 | static int __init enable_strict_msr_control(char *str) |
| 111 | { |
| 112 | strict_msr_control = true; |
| 113 | pr_info("Enabling strict facility control\n"); |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | early_param("ppc_strict_facility_enable", enable_strict_msr_control); |
| 118 | |
Nicholas Piggin | e2b36d5 | 2019-05-02 15:21:07 +1000 | [diff] [blame] | 119 | /* notrace because it's called by restore_math */ |
| 120 | unsigned long notrace msr_check_and_set(unsigned long bits) |
Anton Blanchard | a0e72cf | 2015-10-29 11:44:04 +1100 | [diff] [blame] | 121 | { |
| 122 | unsigned long oldmsr = mfmsr(); |
| 123 | unsigned long newmsr; |
| 124 | |
| 125 | newmsr = oldmsr | bits; |
| 126 | |
| 127 | #ifdef CONFIG_VSX |
| 128 | if (cpu_has_feature(CPU_FTR_VSX) && (bits & MSR_FP)) |
| 129 | newmsr |= MSR_VSX; |
| 130 | #endif |
| 131 | |
| 132 | if (oldmsr != newmsr) |
| 133 | mtmsr_isync(newmsr); |
Cyril Bur | 3cee070 | 2016-09-23 16:18:10 +1000 | [diff] [blame] | 134 | |
| 135 | return newmsr; |
Anton Blanchard | a0e72cf | 2015-10-29 11:44:04 +1100 | [diff] [blame] | 136 | } |
Simon Guo | d1c7211 | 2018-05-23 15:01:44 +0800 | [diff] [blame] | 137 | EXPORT_SYMBOL_GPL(msr_check_and_set); |
Anton Blanchard | a0e72cf | 2015-10-29 11:44:04 +1100 | [diff] [blame] | 138 | |
Nicholas Piggin | e2b36d5 | 2019-05-02 15:21:07 +1000 | [diff] [blame] | 139 | /* notrace because it's called by restore_math */ |
| 140 | void notrace __msr_check_and_clear(unsigned long bits) |
Anton Blanchard | a0e72cf | 2015-10-29 11:44:04 +1100 | [diff] [blame] | 141 | { |
| 142 | unsigned long oldmsr = mfmsr(); |
| 143 | unsigned long newmsr; |
| 144 | |
| 145 | newmsr = oldmsr & ~bits; |
| 146 | |
| 147 | #ifdef CONFIG_VSX |
| 148 | if (cpu_has_feature(CPU_FTR_VSX) && (bits & MSR_FP)) |
| 149 | newmsr &= ~MSR_VSX; |
| 150 | #endif |
| 151 | |
| 152 | if (oldmsr != newmsr) |
| 153 | mtmsr_isync(newmsr); |
| 154 | } |
Anton Blanchard | 3eb5d58 | 2015-10-29 11:44:06 +1100 | [diff] [blame] | 155 | EXPORT_SYMBOL(__msr_check_and_clear); |
Anton Blanchard | a0e72cf | 2015-10-29 11:44:04 +1100 | [diff] [blame] | 156 | |
Kevin Hao | 037f0ee | 2013-07-14 17:02:05 +0800 | [diff] [blame] | 157 | #ifdef CONFIG_PPC_FPU |
Mathieu Malaterre | 1cdf039 | 2018-02-25 18:22:23 +0100 | [diff] [blame] | 158 | static void __giveup_fpu(struct task_struct *tsk) |
Cyril Bur | 8792468 | 2016-02-29 17:53:49 +1100 | [diff] [blame] | 159 | { |
Anton Blanchard | 8eb9803 | 2016-05-29 22:03:50 +1000 | [diff] [blame] | 160 | unsigned long msr; |
| 161 | |
Cyril Bur | 8792468 | 2016-02-29 17:53:49 +1100 | [diff] [blame] | 162 | save_fpu(tsk); |
Anton Blanchard | 8eb9803 | 2016-05-29 22:03:50 +1000 | [diff] [blame] | 163 | msr = tsk->thread.regs->msr; |
Mark Cave-Ayland | fe1ef6b | 2019-02-08 14:33:19 +0000 | [diff] [blame] | 164 | msr &= ~(MSR_FP|MSR_FE0|MSR_FE1); |
Cyril Bur | 8792468 | 2016-02-29 17:53:49 +1100 | [diff] [blame] | 165 | #ifdef CONFIG_VSX |
| 166 | if (cpu_has_feature(CPU_FTR_VSX)) |
Anton Blanchard | 8eb9803 | 2016-05-29 22:03:50 +1000 | [diff] [blame] | 167 | msr &= ~MSR_VSX; |
Cyril Bur | 8792468 | 2016-02-29 17:53:49 +1100 | [diff] [blame] | 168 | #endif |
Anton Blanchard | 8eb9803 | 2016-05-29 22:03:50 +1000 | [diff] [blame] | 169 | tsk->thread.regs->msr = msr; |
Cyril Bur | 8792468 | 2016-02-29 17:53:49 +1100 | [diff] [blame] | 170 | } |
| 171 | |
Anton Blanchard | 98da581 | 2015-10-29 11:44:01 +1100 | [diff] [blame] | 172 | void giveup_fpu(struct task_struct *tsk) |
| 173 | { |
Anton Blanchard | 98da581 | 2015-10-29 11:44:01 +1100 | [diff] [blame] | 174 | check_if_tm_restore_required(tsk); |
| 175 | |
Anton Blanchard | a0e72cf | 2015-10-29 11:44:04 +1100 | [diff] [blame] | 176 | msr_check_and_set(MSR_FP); |
Anton Blanchard | 98da581 | 2015-10-29 11:44:01 +1100 | [diff] [blame] | 177 | __giveup_fpu(tsk); |
Anton Blanchard | a0e72cf | 2015-10-29 11:44:04 +1100 | [diff] [blame] | 178 | msr_check_and_clear(MSR_FP); |
Anton Blanchard | 98da581 | 2015-10-29 11:44:01 +1100 | [diff] [blame] | 179 | } |
| 180 | EXPORT_SYMBOL(giveup_fpu); |
| 181 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 182 | /* |
| 183 | * Make sure the floating-point register state in the |
| 184 | * the thread_struct is up to date for task tsk. |
| 185 | */ |
| 186 | void flush_fp_to_thread(struct task_struct *tsk) |
| 187 | { |
| 188 | if (tsk->thread.regs) { |
| 189 | /* |
| 190 | * We need to disable preemption here because if we didn't, |
| 191 | * another process could get scheduled after the regs->msr |
| 192 | * test but before we have finished saving the FP registers |
| 193 | * to the thread_struct. That process could take over the |
| 194 | * FPU, and then when we get scheduled again we would store |
| 195 | * bogus values for the remaining FP registers. |
| 196 | */ |
| 197 | preempt_disable(); |
| 198 | if (tsk->thread.regs->msr & MSR_FP) { |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 199 | /* |
| 200 | * This should only ever be called for current or |
| 201 | * for a stopped child process. Since we save away |
Anton Blanchard | af1bbc3 | 2015-10-29 11:43:57 +1100 | [diff] [blame] | 202 | * the FP register state on context switch, |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 203 | * there is something wrong if a stopped child appears |
| 204 | * to still have its FP state in the CPU registers. |
| 205 | */ |
| 206 | BUG_ON(tsk != current); |
Anton Blanchard | b86fd2b | 2015-10-29 11:43:58 +1100 | [diff] [blame] | 207 | giveup_fpu(tsk); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 208 | } |
| 209 | preempt_enable(); |
| 210 | } |
| 211 | } |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 212 | EXPORT_SYMBOL_GPL(flush_fp_to_thread); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 213 | |
| 214 | void enable_kernel_fp(void) |
| 215 | { |
Cyril Bur | e909fb8 | 2016-09-23 16:18:11 +1000 | [diff] [blame] | 216 | unsigned long cpumsr; |
| 217 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 218 | WARN_ON(preemptible()); |
| 219 | |
Cyril Bur | e909fb8 | 2016-09-23 16:18:11 +1000 | [diff] [blame] | 220 | cpumsr = msr_check_and_set(MSR_FP); |
Anton Blanchard | 611b0e5 | 2015-10-29 11:43:59 +1100 | [diff] [blame] | 221 | |
Anton Blanchard | d64d02c | 2015-12-10 20:04:05 +1100 | [diff] [blame] | 222 | if (current->thread.regs && (current->thread.regs->msr & MSR_FP)) { |
| 223 | check_if_tm_restore_required(current); |
Cyril Bur | e909fb8 | 2016-09-23 16:18:11 +1000 | [diff] [blame] | 224 | /* |
| 225 | * If a thread has already been reclaimed then the |
| 226 | * checkpointed registers are on the CPU but have definitely |
| 227 | * been saved by the reclaim code. Don't need to and *cannot* |
| 228 | * giveup as this would save to the 'live' structure not the |
| 229 | * checkpointed structure. |
| 230 | */ |
Breno Leitao | 5c784c8 | 2018-08-16 14:21:07 -0300 | [diff] [blame] | 231 | if (!MSR_TM_ACTIVE(cpumsr) && |
| 232 | MSR_TM_ACTIVE(current->thread.regs->msr)) |
Cyril Bur | e909fb8 | 2016-09-23 16:18:11 +1000 | [diff] [blame] | 233 | return; |
Anton Blanchard | a0e72cf | 2015-10-29 11:44:04 +1100 | [diff] [blame] | 234 | __giveup_fpu(current); |
Anton Blanchard | d64d02c | 2015-12-10 20:04:05 +1100 | [diff] [blame] | 235 | } |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 236 | } |
| 237 | EXPORT_SYMBOL(enable_kernel_fp); |
Anton Blanchard | d1e1cf2 | 2015-10-29 11:44:11 +1100 | [diff] [blame] | 238 | #endif /* CONFIG_PPC_FPU */ |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 239 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 240 | #ifdef CONFIG_ALTIVEC |
Cyril Bur | 6f515d8 | 2016-02-29 17:53:50 +1100 | [diff] [blame] | 241 | static void __giveup_altivec(struct task_struct *tsk) |
| 242 | { |
Anton Blanchard | 8eb9803 | 2016-05-29 22:03:50 +1000 | [diff] [blame] | 243 | unsigned long msr; |
| 244 | |
Cyril Bur | 6f515d8 | 2016-02-29 17:53:50 +1100 | [diff] [blame] | 245 | save_altivec(tsk); |
Anton Blanchard | 8eb9803 | 2016-05-29 22:03:50 +1000 | [diff] [blame] | 246 | msr = tsk->thread.regs->msr; |
| 247 | msr &= ~MSR_VEC; |
Cyril Bur | 6f515d8 | 2016-02-29 17:53:50 +1100 | [diff] [blame] | 248 | #ifdef CONFIG_VSX |
| 249 | if (cpu_has_feature(CPU_FTR_VSX)) |
Anton Blanchard | 8eb9803 | 2016-05-29 22:03:50 +1000 | [diff] [blame] | 250 | msr &= ~MSR_VSX; |
Cyril Bur | 6f515d8 | 2016-02-29 17:53:50 +1100 | [diff] [blame] | 251 | #endif |
Anton Blanchard | 8eb9803 | 2016-05-29 22:03:50 +1000 | [diff] [blame] | 252 | tsk->thread.regs->msr = msr; |
Cyril Bur | 6f515d8 | 2016-02-29 17:53:50 +1100 | [diff] [blame] | 253 | } |
| 254 | |
Anton Blanchard | 98da581 | 2015-10-29 11:44:01 +1100 | [diff] [blame] | 255 | void giveup_altivec(struct task_struct *tsk) |
| 256 | { |
Anton Blanchard | 98da581 | 2015-10-29 11:44:01 +1100 | [diff] [blame] | 257 | check_if_tm_restore_required(tsk); |
| 258 | |
Anton Blanchard | a0e72cf | 2015-10-29 11:44:04 +1100 | [diff] [blame] | 259 | msr_check_and_set(MSR_VEC); |
Anton Blanchard | 98da581 | 2015-10-29 11:44:01 +1100 | [diff] [blame] | 260 | __giveup_altivec(tsk); |
Anton Blanchard | a0e72cf | 2015-10-29 11:44:04 +1100 | [diff] [blame] | 261 | msr_check_and_clear(MSR_VEC); |
Anton Blanchard | 98da581 | 2015-10-29 11:44:01 +1100 | [diff] [blame] | 262 | } |
| 263 | EXPORT_SYMBOL(giveup_altivec); |
| 264 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 265 | void enable_kernel_altivec(void) |
| 266 | { |
Cyril Bur | e909fb8 | 2016-09-23 16:18:11 +1000 | [diff] [blame] | 267 | unsigned long cpumsr; |
| 268 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 269 | WARN_ON(preemptible()); |
| 270 | |
Cyril Bur | e909fb8 | 2016-09-23 16:18:11 +1000 | [diff] [blame] | 271 | cpumsr = msr_check_and_set(MSR_VEC); |
Anton Blanchard | 611b0e5 | 2015-10-29 11:43:59 +1100 | [diff] [blame] | 272 | |
Anton Blanchard | d64d02c | 2015-12-10 20:04:05 +1100 | [diff] [blame] | 273 | if (current->thread.regs && (current->thread.regs->msr & MSR_VEC)) { |
| 274 | check_if_tm_restore_required(current); |
Cyril Bur | e909fb8 | 2016-09-23 16:18:11 +1000 | [diff] [blame] | 275 | /* |
| 276 | * If a thread has already been reclaimed then the |
| 277 | * checkpointed registers are on the CPU but have definitely |
| 278 | * been saved by the reclaim code. Don't need to and *cannot* |
| 279 | * giveup as this would save to the 'live' structure not the |
| 280 | * checkpointed structure. |
| 281 | */ |
Breno Leitao | 5c784c8 | 2018-08-16 14:21:07 -0300 | [diff] [blame] | 282 | if (!MSR_TM_ACTIVE(cpumsr) && |
| 283 | MSR_TM_ACTIVE(current->thread.regs->msr)) |
Cyril Bur | e909fb8 | 2016-09-23 16:18:11 +1000 | [diff] [blame] | 284 | return; |
Anton Blanchard | a0e72cf | 2015-10-29 11:44:04 +1100 | [diff] [blame] | 285 | __giveup_altivec(current); |
Anton Blanchard | d64d02c | 2015-12-10 20:04:05 +1100 | [diff] [blame] | 286 | } |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 287 | } |
| 288 | EXPORT_SYMBOL(enable_kernel_altivec); |
| 289 | |
| 290 | /* |
| 291 | * Make sure the VMX/Altivec register state in the |
| 292 | * the thread_struct is up to date for task tsk. |
| 293 | */ |
| 294 | void flush_altivec_to_thread(struct task_struct *tsk) |
| 295 | { |
| 296 | if (tsk->thread.regs) { |
| 297 | preempt_disable(); |
| 298 | if (tsk->thread.regs->msr & MSR_VEC) { |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 299 | BUG_ON(tsk != current); |
Anton Blanchard | b86fd2b | 2015-10-29 11:43:58 +1100 | [diff] [blame] | 300 | giveup_altivec(tsk); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 301 | } |
| 302 | preempt_enable(); |
| 303 | } |
| 304 | } |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 305 | EXPORT_SYMBOL_GPL(flush_altivec_to_thread); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 306 | #endif /* CONFIG_ALTIVEC */ |
| 307 | |
Michael Neuling | ce48b21 | 2008-06-25 14:07:18 +1000 | [diff] [blame] | 308 | #ifdef CONFIG_VSX |
Cyril Bur | bf6a4d5 | 2016-02-29 17:53:51 +1100 | [diff] [blame] | 309 | static void __giveup_vsx(struct task_struct *tsk) |
Anton Blanchard | a7d623d | 2015-10-29 11:44:02 +1100 | [diff] [blame] | 310 | { |
Benjamin Herrenschmidt | dc80108 | 2017-08-16 16:01:17 +1000 | [diff] [blame] | 311 | unsigned long msr = tsk->thread.regs->msr; |
| 312 | |
| 313 | /* |
| 314 | * We should never be ssetting MSR_VSX without also setting |
| 315 | * MSR_FP and MSR_VEC |
| 316 | */ |
| 317 | WARN_ON((msr & MSR_VSX) && !((msr & MSR_FP) && (msr & MSR_VEC))); |
| 318 | |
| 319 | /* __giveup_fpu will clear MSR_VSX */ |
| 320 | if (msr & MSR_FP) |
Anton Blanchard | a7d623d | 2015-10-29 11:44:02 +1100 | [diff] [blame] | 321 | __giveup_fpu(tsk); |
Benjamin Herrenschmidt | dc80108 | 2017-08-16 16:01:17 +1000 | [diff] [blame] | 322 | if (msr & MSR_VEC) |
Anton Blanchard | a7d623d | 2015-10-29 11:44:02 +1100 | [diff] [blame] | 323 | __giveup_altivec(tsk); |
Cyril Bur | bf6a4d5 | 2016-02-29 17:53:51 +1100 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | static void giveup_vsx(struct task_struct *tsk) |
| 327 | { |
| 328 | check_if_tm_restore_required(tsk); |
| 329 | |
| 330 | msr_check_and_set(MSR_FP|MSR_VEC|MSR_VSX); |
Anton Blanchard | a7d623d | 2015-10-29 11:44:02 +1100 | [diff] [blame] | 331 | __giveup_vsx(tsk); |
Anton Blanchard | a0e72cf | 2015-10-29 11:44:04 +1100 | [diff] [blame] | 332 | msr_check_and_clear(MSR_FP|MSR_VEC|MSR_VSX); |
Anton Blanchard | a7d623d | 2015-10-29 11:44:02 +1100 | [diff] [blame] | 333 | } |
Cyril Bur | bf6a4d5 | 2016-02-29 17:53:51 +1100 | [diff] [blame] | 334 | |
Michael Neuling | ce48b21 | 2008-06-25 14:07:18 +1000 | [diff] [blame] | 335 | void enable_kernel_vsx(void) |
| 336 | { |
Cyril Bur | e909fb8 | 2016-09-23 16:18:11 +1000 | [diff] [blame] | 337 | unsigned long cpumsr; |
| 338 | |
Michael Neuling | ce48b21 | 2008-06-25 14:07:18 +1000 | [diff] [blame] | 339 | WARN_ON(preemptible()); |
| 340 | |
Cyril Bur | e909fb8 | 2016-09-23 16:18:11 +1000 | [diff] [blame] | 341 | cpumsr = msr_check_and_set(MSR_FP|MSR_VEC|MSR_VSX); |
Anton Blanchard | 611b0e5 | 2015-10-29 11:43:59 +1100 | [diff] [blame] | 342 | |
Benjamin Herrenschmidt | 5a69aec | 2017-08-16 16:01:14 +1000 | [diff] [blame] | 343 | if (current->thread.regs && |
| 344 | (current->thread.regs->msr & (MSR_VSX|MSR_VEC|MSR_FP))) { |
Anton Blanchard | d64d02c | 2015-12-10 20:04:05 +1100 | [diff] [blame] | 345 | check_if_tm_restore_required(current); |
Cyril Bur | e909fb8 | 2016-09-23 16:18:11 +1000 | [diff] [blame] | 346 | /* |
| 347 | * If a thread has already been reclaimed then the |
| 348 | * checkpointed registers are on the CPU but have definitely |
| 349 | * been saved by the reclaim code. Don't need to and *cannot* |
| 350 | * giveup as this would save to the 'live' structure not the |
| 351 | * checkpointed structure. |
| 352 | */ |
Breno Leitao | 5c784c8 | 2018-08-16 14:21:07 -0300 | [diff] [blame] | 353 | if (!MSR_TM_ACTIVE(cpumsr) && |
| 354 | MSR_TM_ACTIVE(current->thread.regs->msr)) |
Cyril Bur | e909fb8 | 2016-09-23 16:18:11 +1000 | [diff] [blame] | 355 | return; |
Anton Blanchard | a0e72cf | 2015-10-29 11:44:04 +1100 | [diff] [blame] | 356 | __giveup_vsx(current); |
Anton Blanchard | 611b0e5 | 2015-10-29 11:43:59 +1100 | [diff] [blame] | 357 | } |
Michael Neuling | ce48b21 | 2008-06-25 14:07:18 +1000 | [diff] [blame] | 358 | } |
| 359 | EXPORT_SYMBOL(enable_kernel_vsx); |
Michael Neuling | ce48b21 | 2008-06-25 14:07:18 +1000 | [diff] [blame] | 360 | |
| 361 | void flush_vsx_to_thread(struct task_struct *tsk) |
| 362 | { |
| 363 | if (tsk->thread.regs) { |
| 364 | preempt_disable(); |
Benjamin Herrenschmidt | 5a69aec | 2017-08-16 16:01:14 +1000 | [diff] [blame] | 365 | if (tsk->thread.regs->msr & (MSR_VSX|MSR_VEC|MSR_FP)) { |
Michael Neuling | ce48b21 | 2008-06-25 14:07:18 +1000 | [diff] [blame] | 366 | BUG_ON(tsk != current); |
Michael Neuling | ce48b21 | 2008-06-25 14:07:18 +1000 | [diff] [blame] | 367 | giveup_vsx(tsk); |
| 368 | } |
| 369 | preempt_enable(); |
| 370 | } |
| 371 | } |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 372 | EXPORT_SYMBOL_GPL(flush_vsx_to_thread); |
Michael Neuling | ce48b21 | 2008-06-25 14:07:18 +1000 | [diff] [blame] | 373 | #endif /* CONFIG_VSX */ |
| 374 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 375 | #ifdef CONFIG_SPE |
Anton Blanchard | 98da581 | 2015-10-29 11:44:01 +1100 | [diff] [blame] | 376 | void giveup_spe(struct task_struct *tsk) |
| 377 | { |
Anton Blanchard | 98da581 | 2015-10-29 11:44:01 +1100 | [diff] [blame] | 378 | check_if_tm_restore_required(tsk); |
| 379 | |
Anton Blanchard | a0e72cf | 2015-10-29 11:44:04 +1100 | [diff] [blame] | 380 | msr_check_and_set(MSR_SPE); |
Anton Blanchard | 98da581 | 2015-10-29 11:44:01 +1100 | [diff] [blame] | 381 | __giveup_spe(tsk); |
Anton Blanchard | a0e72cf | 2015-10-29 11:44:04 +1100 | [diff] [blame] | 382 | msr_check_and_clear(MSR_SPE); |
Anton Blanchard | 98da581 | 2015-10-29 11:44:01 +1100 | [diff] [blame] | 383 | } |
| 384 | EXPORT_SYMBOL(giveup_spe); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 385 | |
| 386 | void enable_kernel_spe(void) |
| 387 | { |
| 388 | WARN_ON(preemptible()); |
| 389 | |
Anton Blanchard | a0e72cf | 2015-10-29 11:44:04 +1100 | [diff] [blame] | 390 | msr_check_and_set(MSR_SPE); |
Anton Blanchard | 611b0e5 | 2015-10-29 11:43:59 +1100 | [diff] [blame] | 391 | |
Anton Blanchard | d64d02c | 2015-12-10 20:04:05 +1100 | [diff] [blame] | 392 | if (current->thread.regs && (current->thread.regs->msr & MSR_SPE)) { |
| 393 | check_if_tm_restore_required(current); |
Anton Blanchard | a0e72cf | 2015-10-29 11:44:04 +1100 | [diff] [blame] | 394 | __giveup_spe(current); |
Anton Blanchard | d64d02c | 2015-12-10 20:04:05 +1100 | [diff] [blame] | 395 | } |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 396 | } |
| 397 | EXPORT_SYMBOL(enable_kernel_spe); |
| 398 | |
| 399 | void flush_spe_to_thread(struct task_struct *tsk) |
| 400 | { |
| 401 | if (tsk->thread.regs) { |
| 402 | preempt_disable(); |
| 403 | if (tsk->thread.regs->msr & MSR_SPE) { |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 404 | BUG_ON(tsk != current); |
yu liu | 685659e | 2011-06-14 18:34:25 -0500 | [diff] [blame] | 405 | tsk->thread.spefscr = mfspr(SPRN_SPEFSCR); |
Kumar Gala | 0ee6c15 | 2007-08-28 21:15:53 -0500 | [diff] [blame] | 406 | giveup_spe(tsk); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 407 | } |
| 408 | preempt_enable(); |
| 409 | } |
| 410 | } |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 411 | #endif /* CONFIG_SPE */ |
| 412 | |
Anton Blanchard | c208505 | 2015-10-29 11:44:08 +1100 | [diff] [blame] | 413 | static unsigned long msr_all_available; |
| 414 | |
| 415 | static int __init init_msr_all_available(void) |
| 416 | { |
| 417 | #ifdef CONFIG_PPC_FPU |
| 418 | msr_all_available |= MSR_FP; |
| 419 | #endif |
| 420 | #ifdef CONFIG_ALTIVEC |
| 421 | if (cpu_has_feature(CPU_FTR_ALTIVEC)) |
| 422 | msr_all_available |= MSR_VEC; |
| 423 | #endif |
| 424 | #ifdef CONFIG_VSX |
| 425 | if (cpu_has_feature(CPU_FTR_VSX)) |
| 426 | msr_all_available |= MSR_VSX; |
| 427 | #endif |
| 428 | #ifdef CONFIG_SPE |
| 429 | if (cpu_has_feature(CPU_FTR_SPE)) |
| 430 | msr_all_available |= MSR_SPE; |
| 431 | #endif |
| 432 | |
| 433 | return 0; |
| 434 | } |
| 435 | early_initcall(init_msr_all_available); |
| 436 | |
| 437 | void giveup_all(struct task_struct *tsk) |
| 438 | { |
| 439 | unsigned long usermsr; |
| 440 | |
| 441 | if (!tsk->thread.regs) |
| 442 | return; |
| 443 | |
Gustavo Romero | 8205d5d | 2019-09-04 00:55:27 -0400 | [diff] [blame] | 444 | check_if_tm_restore_required(tsk); |
| 445 | |
Anton Blanchard | c208505 | 2015-10-29 11:44:08 +1100 | [diff] [blame] | 446 | usermsr = tsk->thread.regs->msr; |
| 447 | |
| 448 | if ((usermsr & msr_all_available) == 0) |
| 449 | return; |
| 450 | |
| 451 | msr_check_and_set(msr_all_available); |
| 452 | |
Benjamin Herrenschmidt | 96c79b6 | 2017-08-16 16:01:18 +1000 | [diff] [blame] | 453 | WARN_ON((usermsr & MSR_VSX) && !((usermsr & MSR_FP) && (usermsr & MSR_VEC))); |
| 454 | |
Anton Blanchard | c208505 | 2015-10-29 11:44:08 +1100 | [diff] [blame] | 455 | #ifdef CONFIG_PPC_FPU |
| 456 | if (usermsr & MSR_FP) |
| 457 | __giveup_fpu(tsk); |
| 458 | #endif |
| 459 | #ifdef CONFIG_ALTIVEC |
| 460 | if (usermsr & MSR_VEC) |
| 461 | __giveup_altivec(tsk); |
| 462 | #endif |
Anton Blanchard | c208505 | 2015-10-29 11:44:08 +1100 | [diff] [blame] | 463 | #ifdef CONFIG_SPE |
| 464 | if (usermsr & MSR_SPE) |
| 465 | __giveup_spe(tsk); |
| 466 | #endif |
| 467 | |
| 468 | msr_check_and_clear(msr_all_available); |
| 469 | } |
| 470 | EXPORT_SYMBOL(giveup_all); |
| 471 | |
Nicholas Piggin | 6cc0c16 | 2020-02-26 03:35:37 +1000 | [diff] [blame] | 472 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 473 | #ifdef CONFIG_PPC_FPU |
Nicholas Piggin | 01eb018 | 2020-06-24 09:41:38 +1000 | [diff] [blame] | 474 | static bool should_restore_fp(void) |
Nicholas Piggin | 6cc0c16 | 2020-02-26 03:35:37 +1000 | [diff] [blame] | 475 | { |
Nicholas Piggin | 01eb018 | 2020-06-24 09:41:38 +1000 | [diff] [blame] | 476 | if (current->thread.load_fp) { |
Nicholas Piggin | 6cc0c16 | 2020-02-26 03:35:37 +1000 | [diff] [blame] | 477 | current->thread.load_fp++; |
Nicholas Piggin | 01eb018 | 2020-06-24 09:41:38 +1000 | [diff] [blame] | 478 | return true; |
Nicholas Piggin | 6cc0c16 | 2020-02-26 03:35:37 +1000 | [diff] [blame] | 479 | } |
Nicholas Piggin | 01eb018 | 2020-06-24 09:41:38 +1000 | [diff] [blame] | 480 | return false; |
| 481 | } |
| 482 | |
| 483 | static void do_restore_fp(void) |
| 484 | { |
| 485 | load_fp_state(¤t->thread.fp_state); |
Nicholas Piggin | 6cc0c16 | 2020-02-26 03:35:37 +1000 | [diff] [blame] | 486 | } |
| 487 | #else |
Nicholas Piggin | 01eb018 | 2020-06-24 09:41:38 +1000 | [diff] [blame] | 488 | static bool should_restore_fp(void) { return false; } |
| 489 | static void do_restore_fp(void) { } |
Nicholas Piggin | 6cc0c16 | 2020-02-26 03:35:37 +1000 | [diff] [blame] | 490 | #endif /* CONFIG_PPC_FPU */ |
| 491 | |
| 492 | #ifdef CONFIG_ALTIVEC |
Nicholas Piggin | 01eb018 | 2020-06-24 09:41:38 +1000 | [diff] [blame] | 493 | static bool should_restore_altivec(void) |
Nicholas Piggin | 6cc0c16 | 2020-02-26 03:35:37 +1000 | [diff] [blame] | 494 | { |
Nicholas Piggin | 01eb018 | 2020-06-24 09:41:38 +1000 | [diff] [blame] | 495 | if (cpu_has_feature(CPU_FTR_ALTIVEC) && (current->thread.load_vec)) { |
| 496 | current->thread.load_vec++; |
| 497 | return true; |
Nicholas Piggin | 6cc0c16 | 2020-02-26 03:35:37 +1000 | [diff] [blame] | 498 | } |
Nicholas Piggin | 01eb018 | 2020-06-24 09:41:38 +1000 | [diff] [blame] | 499 | return false; |
| 500 | } |
| 501 | |
| 502 | static void do_restore_altivec(void) |
| 503 | { |
| 504 | load_vr_state(¤t->thread.vr_state); |
| 505 | current->thread.used_vr = 1; |
Nicholas Piggin | 6cc0c16 | 2020-02-26 03:35:37 +1000 | [diff] [blame] | 506 | } |
| 507 | #else |
Nicholas Piggin | 01eb018 | 2020-06-24 09:41:38 +1000 | [diff] [blame] | 508 | static bool should_restore_altivec(void) { return false; } |
| 509 | static void do_restore_altivec(void) { } |
Nicholas Piggin | 6cc0c16 | 2020-02-26 03:35:37 +1000 | [diff] [blame] | 510 | #endif /* CONFIG_ALTIVEC */ |
| 511 | |
| 512 | #ifdef CONFIG_VSX |
Nicholas Piggin | 01eb018 | 2020-06-24 09:41:38 +1000 | [diff] [blame] | 513 | static bool should_restore_vsx(void) |
Nicholas Piggin | 6cc0c16 | 2020-02-26 03:35:37 +1000 | [diff] [blame] | 514 | { |
Nicholas Piggin | 01eb018 | 2020-06-24 09:41:38 +1000 | [diff] [blame] | 515 | if (cpu_has_feature(CPU_FTR_VSX)) |
| 516 | return true; |
| 517 | return false; |
| 518 | } |
| 519 | static void do_restore_vsx(void) |
| 520 | { |
| 521 | current->thread.used_vsr = 1; |
Nicholas Piggin | 6cc0c16 | 2020-02-26 03:35:37 +1000 | [diff] [blame] | 522 | } |
| 523 | #else |
Nicholas Piggin | 01eb018 | 2020-06-24 09:41:38 +1000 | [diff] [blame] | 524 | static bool should_restore_vsx(void) { return false; } |
| 525 | static void do_restore_vsx(void) { } |
Nicholas Piggin | 6cc0c16 | 2020-02-26 03:35:37 +1000 | [diff] [blame] | 526 | #endif /* CONFIG_VSX */ |
| 527 | |
Nicholas Piggin | e2b36d5 | 2019-05-02 15:21:07 +1000 | [diff] [blame] | 528 | /* |
| 529 | * The exception exit path calls restore_math() with interrupts hard disabled |
| 530 | * but the soft irq state not "reconciled". ftrace code that calls |
| 531 | * local_irq_save/restore causes warnings. |
| 532 | * |
| 533 | * Rather than complicate the exit path, just don't trace restore_math. This |
| 534 | * could be done by having ftrace entry code check for this un-reconciled |
| 535 | * condition where MSR[EE]=0 and PACA_IRQ_HARD_DIS is not set, and |
| 536 | * temporarily fix it up for the duration of the ftrace call. |
| 537 | */ |
| 538 | void notrace restore_math(struct pt_regs *regs) |
Cyril Bur | 70fe3d9 | 2016-02-29 17:53:47 +1100 | [diff] [blame] | 539 | { |
| 540 | unsigned long msr; |
Nicholas Piggin | 01eb018 | 2020-06-24 09:41:38 +1000 | [diff] [blame] | 541 | unsigned long new_msr = 0; |
Cyril Bur | 70fe3d9 | 2016-02-29 17:53:47 +1100 | [diff] [blame] | 542 | |
| 543 | msr = regs->msr; |
Cyril Bur | 70fe3d9 | 2016-02-29 17:53:47 +1100 | [diff] [blame] | 544 | |
| 545 | /* |
Nicholas Piggin | 01eb018 | 2020-06-24 09:41:38 +1000 | [diff] [blame] | 546 | * new_msr tracks the facilities that are to be restored. Only reload |
| 547 | * if the bit is not set in the user MSR (if it is set, the registers |
| 548 | * are live for the user thread). |
Cyril Bur | 70fe3d9 | 2016-02-29 17:53:47 +1100 | [diff] [blame] | 549 | */ |
Nicholas Piggin | 01eb018 | 2020-06-24 09:41:38 +1000 | [diff] [blame] | 550 | if ((!(msr & MSR_FP)) && should_restore_fp()) |
Michael Ellerman | b91eb51 | 2020-08-25 19:34:24 +1000 | [diff] [blame] | 551 | new_msr |= MSR_FP; |
Cyril Bur | 70fe3d9 | 2016-02-29 17:53:47 +1100 | [diff] [blame] | 552 | |
Nicholas Piggin | 01eb018 | 2020-06-24 09:41:38 +1000 | [diff] [blame] | 553 | if ((!(msr & MSR_VEC)) && should_restore_altivec()) |
| 554 | new_msr |= MSR_VEC; |
Cyril Bur | 70fe3d9 | 2016-02-29 17:53:47 +1100 | [diff] [blame] | 555 | |
Nicholas Piggin | 01eb018 | 2020-06-24 09:41:38 +1000 | [diff] [blame] | 556 | if ((!(msr & MSR_VSX)) && should_restore_vsx()) { |
| 557 | if (((msr | new_msr) & (MSR_FP | MSR_VEC)) == (MSR_FP | MSR_VEC)) |
| 558 | new_msr |= MSR_VSX; |
Cyril Bur | 70fe3d9 | 2016-02-29 17:53:47 +1100 | [diff] [blame] | 559 | } |
| 560 | |
Nicholas Piggin | 01eb018 | 2020-06-24 09:41:38 +1000 | [diff] [blame] | 561 | if (new_msr) { |
Michael Ellerman | b91eb51 | 2020-08-25 19:34:24 +1000 | [diff] [blame] | 562 | unsigned long fpexc_mode = 0; |
| 563 | |
Nicholas Piggin | 01eb018 | 2020-06-24 09:41:38 +1000 | [diff] [blame] | 564 | msr_check_and_set(new_msr); |
Cyril Bur | 70fe3d9 | 2016-02-29 17:53:47 +1100 | [diff] [blame] | 565 | |
Michael Ellerman | b91eb51 | 2020-08-25 19:34:24 +1000 | [diff] [blame] | 566 | if (new_msr & MSR_FP) { |
Nicholas Piggin | 01eb018 | 2020-06-24 09:41:38 +1000 | [diff] [blame] | 567 | do_restore_fp(); |
| 568 | |
Michael Ellerman | b91eb51 | 2020-08-25 19:34:24 +1000 | [diff] [blame] | 569 | // This also covers VSX, because VSX implies FP |
| 570 | fpexc_mode = current->thread.fpexc_mode; |
| 571 | } |
| 572 | |
Nicholas Piggin | 01eb018 | 2020-06-24 09:41:38 +1000 | [diff] [blame] | 573 | if (new_msr & MSR_VEC) |
| 574 | do_restore_altivec(); |
| 575 | |
| 576 | if (new_msr & MSR_VSX) |
| 577 | do_restore_vsx(); |
| 578 | |
| 579 | msr_check_and_clear(new_msr); |
| 580 | |
Michael Ellerman | b91eb51 | 2020-08-25 19:34:24 +1000 | [diff] [blame] | 581 | regs->msr |= new_msr | fpexc_mode; |
Nicholas Piggin | 01eb018 | 2020-06-24 09:41:38 +1000 | [diff] [blame] | 582 | } |
Cyril Bur | 70fe3d9 | 2016-02-29 17:53:47 +1100 | [diff] [blame] | 583 | } |
Christophe Leroy | 60d62bf | 2020-08-17 05:46:45 +0000 | [diff] [blame^] | 584 | #endif /* CONFIG_PPC_BOOK3S_64 */ |
Cyril Bur | 70fe3d9 | 2016-02-29 17:53:47 +1100 | [diff] [blame] | 585 | |
Mathieu Malaterre | 1cdf039 | 2018-02-25 18:22:23 +0100 | [diff] [blame] | 586 | static void save_all(struct task_struct *tsk) |
Cyril Bur | de2a20a | 2016-02-29 17:53:48 +1100 | [diff] [blame] | 587 | { |
| 588 | unsigned long usermsr; |
| 589 | |
| 590 | if (!tsk->thread.regs) |
| 591 | return; |
| 592 | |
| 593 | usermsr = tsk->thread.regs->msr; |
| 594 | |
| 595 | if ((usermsr & msr_all_available) == 0) |
| 596 | return; |
| 597 | |
| 598 | msr_check_and_set(msr_all_available); |
| 599 | |
Benjamin Herrenschmidt | 96c79b6 | 2017-08-16 16:01:18 +1000 | [diff] [blame] | 600 | WARN_ON((usermsr & MSR_VSX) && !((usermsr & MSR_FP) && (usermsr & MSR_VEC))); |
Cyril Bur | de2a20a | 2016-02-29 17:53:48 +1100 | [diff] [blame] | 601 | |
Benjamin Herrenschmidt | 96c79b6 | 2017-08-16 16:01:18 +1000 | [diff] [blame] | 602 | if (usermsr & MSR_FP) |
| 603 | save_fpu(tsk); |
| 604 | |
| 605 | if (usermsr & MSR_VEC) |
| 606 | save_altivec(tsk); |
Cyril Bur | de2a20a | 2016-02-29 17:53:48 +1100 | [diff] [blame] | 607 | |
| 608 | if (usermsr & MSR_SPE) |
| 609 | __giveup_spe(tsk); |
| 610 | |
| 611 | msr_check_and_clear(msr_all_available); |
Ram Pai | c76662e | 2018-07-17 06:51:05 -0700 | [diff] [blame] | 612 | thread_pkey_regs_save(&tsk->thread); |
Cyril Bur | de2a20a | 2016-02-29 17:53:48 +1100 | [diff] [blame] | 613 | } |
| 614 | |
Anton Blanchard | 579e633 | 2015-10-29 11:44:09 +1100 | [diff] [blame] | 615 | void flush_all_to_thread(struct task_struct *tsk) |
| 616 | { |
| 617 | if (tsk->thread.regs) { |
| 618 | preempt_disable(); |
| 619 | BUG_ON(tsk != current); |
Anton Blanchard | 579e633 | 2015-10-29 11:44:09 +1100 | [diff] [blame] | 620 | #ifdef CONFIG_SPE |
| 621 | if (tsk->thread.regs->msr & MSR_SPE) |
| 622 | tsk->thread.spefscr = mfspr(SPRN_SPEFSCR); |
| 623 | #endif |
Felipe Rechia | e901378 | 2018-10-24 10:57:22 -0300 | [diff] [blame] | 624 | save_all(tsk); |
Anton Blanchard | 579e633 | 2015-10-29 11:44:09 +1100 | [diff] [blame] | 625 | |
| 626 | preempt_enable(); |
| 627 | } |
| 628 | } |
| 629 | EXPORT_SYMBOL(flush_all_to_thread); |
| 630 | |
Dave Kleikamp | 3bffb65 | 2010-02-08 11:51:18 +0000 | [diff] [blame] | 631 | #ifdef CONFIG_PPC_ADV_DEBUG_REGS |
| 632 | void do_send_trap(struct pt_regs *regs, unsigned long address, |
Eric W. Biederman | 4735504 | 2018-01-16 16:12:38 -0600 | [diff] [blame] | 633 | unsigned long error_code, int breakpt) |
Dave Kleikamp | 3bffb65 | 2010-02-08 11:51:18 +0000 | [diff] [blame] | 634 | { |
Eric W. Biederman | 4735504 | 2018-01-16 16:12:38 -0600 | [diff] [blame] | 635 | current->thread.trap_nr = TRAP_HWBKPT; |
Dave Kleikamp | 3bffb65 | 2010-02-08 11:51:18 +0000 | [diff] [blame] | 636 | if (notify_die(DIE_DABR_MATCH, "dabr_match", regs, error_code, |
| 637 | 11, SIGSEGV) == NOTIFY_STOP) |
| 638 | return; |
| 639 | |
| 640 | /* Deliver the signal to userspace */ |
Eric W. Biederman | f71dd7d | 2018-01-22 14:37:25 -0600 | [diff] [blame] | 641 | force_sig_ptrace_errno_trap(breakpt, /* breakpoint or watchpoint id */ |
| 642 | (void __user *)address); |
Dave Kleikamp | 3bffb65 | 2010-02-08 11:51:18 +0000 | [diff] [blame] | 643 | } |
| 644 | #else /* !CONFIG_PPC_ADV_DEBUG_REGS */ |
Ravi Bangoria | 5b905d7 | 2020-09-02 09:59:42 +0530 | [diff] [blame] | 645 | |
| 646 | static void do_break_handler(struct pt_regs *regs) |
| 647 | { |
| 648 | struct arch_hw_breakpoint null_brk = {0}; |
| 649 | struct arch_hw_breakpoint *info; |
| 650 | struct ppc_inst instr = ppc_inst(0); |
| 651 | int type = 0; |
| 652 | int size = 0; |
| 653 | unsigned long ea; |
| 654 | int i; |
| 655 | |
| 656 | /* |
| 657 | * If underneath hw supports only one watchpoint, we know it |
| 658 | * caused exception. 8xx also falls into this category. |
| 659 | */ |
| 660 | if (nr_wp_slots() == 1) { |
| 661 | __set_breakpoint(0, &null_brk); |
| 662 | current->thread.hw_brk[0] = null_brk; |
| 663 | current->thread.hw_brk[0].flags |= HW_BRK_FLAG_DISABLED; |
| 664 | return; |
| 665 | } |
| 666 | |
| 667 | /* Otherwise findout which DAWR caused exception and disable it. */ |
| 668 | wp_get_instr_detail(regs, &instr, &type, &size, &ea); |
| 669 | |
| 670 | for (i = 0; i < nr_wp_slots(); i++) { |
| 671 | info = ¤t->thread.hw_brk[i]; |
| 672 | if (!info->address) |
| 673 | continue; |
| 674 | |
| 675 | if (wp_check_constraints(regs, instr, ea, type, size, info)) { |
| 676 | __set_breakpoint(i, &null_brk); |
| 677 | current->thread.hw_brk[i] = null_brk; |
| 678 | current->thread.hw_brk[i].flags |= HW_BRK_FLAG_DISABLED; |
| 679 | } |
| 680 | } |
| 681 | } |
| 682 | |
Michael Neuling | 9422de3 | 2012-12-20 14:06:44 +0000 | [diff] [blame] | 683 | void do_break (struct pt_regs *regs, unsigned long address, |
Luis Machado | d6a61bf | 2008-07-24 02:10:41 +1000 | [diff] [blame] | 684 | unsigned long error_code) |
| 685 | { |
Ananth N Mavinakayanahalli | 41ab526 | 2012-08-23 21:27:09 +0000 | [diff] [blame] | 686 | current->thread.trap_nr = TRAP_HWBKPT; |
Luis Machado | d6a61bf | 2008-07-24 02:10:41 +1000 | [diff] [blame] | 687 | if (notify_die(DIE_DABR_MATCH, "dabr_match", regs, error_code, |
| 688 | 11, SIGSEGV) == NOTIFY_STOP) |
| 689 | return; |
| 690 | |
Michael Neuling | 9422de3 | 2012-12-20 14:06:44 +0000 | [diff] [blame] | 691 | if (debugger_break_match(regs)) |
Luis Machado | d6a61bf | 2008-07-24 02:10:41 +1000 | [diff] [blame] | 692 | return; |
| 693 | |
Ravi Bangoria | 5b905d7 | 2020-09-02 09:59:42 +0530 | [diff] [blame] | 694 | /* |
| 695 | * We reach here only when watchpoint exception is generated by ptrace |
| 696 | * event (or hw is buggy!). Now if CONFIG_HAVE_HW_BREAKPOINT is set, |
| 697 | * watchpoint is already handled by hw_breakpoint_handler() so we don't |
| 698 | * have to do anything. But when CONFIG_HAVE_HW_BREAKPOINT is not set, |
| 699 | * we need to manually handle the watchpoint here. |
| 700 | */ |
| 701 | if (!IS_ENABLED(CONFIG_HAVE_HW_BREAKPOINT)) |
| 702 | do_break_handler(regs); |
| 703 | |
Luis Machado | d6a61bf | 2008-07-24 02:10:41 +1000 | [diff] [blame] | 704 | /* Deliver the signal to userspace */ |
Eric W. Biederman | 2e1661d2 | 2019-05-23 11:04:24 -0500 | [diff] [blame] | 705 | force_sig_fault(SIGTRAP, TRAP_HWBKPT, (void __user *)address); |
Luis Machado | d6a61bf | 2008-07-24 02:10:41 +1000 | [diff] [blame] | 706 | } |
Dave Kleikamp | 3bffb65 | 2010-02-08 11:51:18 +0000 | [diff] [blame] | 707 | #endif /* CONFIG_PPC_ADV_DEBUG_REGS */ |
Luis Machado | d6a61bf | 2008-07-24 02:10:41 +1000 | [diff] [blame] | 708 | |
Ravi Bangoria | 4a8a937 | 2020-05-14 16:47:31 +0530 | [diff] [blame] | 709 | static DEFINE_PER_CPU(struct arch_hw_breakpoint, current_brk[HBP_NUM_MAX]); |
Michael Ellerman | a2ceff5 | 2008-03-28 19:11:48 +1100 | [diff] [blame] | 710 | |
Dave Kleikamp | 3bffb65 | 2010-02-08 11:51:18 +0000 | [diff] [blame] | 711 | #ifdef CONFIG_PPC_ADV_DEBUG_REGS |
| 712 | /* |
| 713 | * Set the debug registers back to their default "safe" values. |
| 714 | */ |
| 715 | static void set_debug_reg_defaults(struct thread_struct *thread) |
| 716 | { |
Bharat Bhushan | 51ae8d4 | 2013-07-04 11:45:46 +0530 | [diff] [blame] | 717 | thread->debug.iac1 = thread->debug.iac2 = 0; |
Dave Kleikamp | 3bffb65 | 2010-02-08 11:51:18 +0000 | [diff] [blame] | 718 | #if CONFIG_PPC_ADV_DEBUG_IACS > 2 |
Bharat Bhushan | 51ae8d4 | 2013-07-04 11:45:46 +0530 | [diff] [blame] | 719 | thread->debug.iac3 = thread->debug.iac4 = 0; |
Dave Kleikamp | 3bffb65 | 2010-02-08 11:51:18 +0000 | [diff] [blame] | 720 | #endif |
Bharat Bhushan | 51ae8d4 | 2013-07-04 11:45:46 +0530 | [diff] [blame] | 721 | thread->debug.dac1 = thread->debug.dac2 = 0; |
Dave Kleikamp | 3bffb65 | 2010-02-08 11:51:18 +0000 | [diff] [blame] | 722 | #if CONFIG_PPC_ADV_DEBUG_DVCS > 0 |
Bharat Bhushan | 51ae8d4 | 2013-07-04 11:45:46 +0530 | [diff] [blame] | 723 | thread->debug.dvc1 = thread->debug.dvc2 = 0; |
Dave Kleikamp | 3bffb65 | 2010-02-08 11:51:18 +0000 | [diff] [blame] | 724 | #endif |
Bharat Bhushan | 51ae8d4 | 2013-07-04 11:45:46 +0530 | [diff] [blame] | 725 | thread->debug.dbcr0 = 0; |
Dave Kleikamp | 3bffb65 | 2010-02-08 11:51:18 +0000 | [diff] [blame] | 726 | #ifdef CONFIG_BOOKE |
| 727 | /* |
| 728 | * Force User/Supervisor bits to b11 (user-only MSR[PR]=1) |
| 729 | */ |
Bharat Bhushan | 51ae8d4 | 2013-07-04 11:45:46 +0530 | [diff] [blame] | 730 | thread->debug.dbcr1 = DBCR1_IAC1US | DBCR1_IAC2US | |
Dave Kleikamp | 3bffb65 | 2010-02-08 11:51:18 +0000 | [diff] [blame] | 731 | DBCR1_IAC3US | DBCR1_IAC4US; |
| 732 | /* |
| 733 | * Force Data Address Compare User/Supervisor bits to be User-only |
| 734 | * (0b11 MSR[PR]=1) and set all other bits in DBCR2 register to be 0. |
| 735 | */ |
Bharat Bhushan | 51ae8d4 | 2013-07-04 11:45:46 +0530 | [diff] [blame] | 736 | thread->debug.dbcr2 = DBCR2_DAC1US | DBCR2_DAC2US; |
Dave Kleikamp | 3bffb65 | 2010-02-08 11:51:18 +0000 | [diff] [blame] | 737 | #else |
Bharat Bhushan | 51ae8d4 | 2013-07-04 11:45:46 +0530 | [diff] [blame] | 738 | thread->debug.dbcr1 = 0; |
Dave Kleikamp | 3bffb65 | 2010-02-08 11:51:18 +0000 | [diff] [blame] | 739 | #endif |
| 740 | } |
| 741 | |
Scott Wood | f5f9721 | 2013-11-22 15:52:29 -0600 | [diff] [blame] | 742 | static void prime_debug_regs(struct debug_reg *debug) |
Dave Kleikamp | 3bffb65 | 2010-02-08 11:51:18 +0000 | [diff] [blame] | 743 | { |
Scott Wood | 6cecf76 | 2013-05-13 14:14:53 +0000 | [diff] [blame] | 744 | /* |
| 745 | * We could have inherited MSR_DE from userspace, since |
| 746 | * it doesn't get cleared on exception entry. Make sure |
| 747 | * MSR_DE is clear before we enable any debug events. |
| 748 | */ |
| 749 | mtmsr(mfmsr() & ~MSR_DE); |
| 750 | |
Scott Wood | f5f9721 | 2013-11-22 15:52:29 -0600 | [diff] [blame] | 751 | mtspr(SPRN_IAC1, debug->iac1); |
| 752 | mtspr(SPRN_IAC2, debug->iac2); |
Dave Kleikamp | 3bffb65 | 2010-02-08 11:51:18 +0000 | [diff] [blame] | 753 | #if CONFIG_PPC_ADV_DEBUG_IACS > 2 |
Scott Wood | f5f9721 | 2013-11-22 15:52:29 -0600 | [diff] [blame] | 754 | mtspr(SPRN_IAC3, debug->iac3); |
| 755 | mtspr(SPRN_IAC4, debug->iac4); |
Dave Kleikamp | 3bffb65 | 2010-02-08 11:51:18 +0000 | [diff] [blame] | 756 | #endif |
Scott Wood | f5f9721 | 2013-11-22 15:52:29 -0600 | [diff] [blame] | 757 | mtspr(SPRN_DAC1, debug->dac1); |
| 758 | mtspr(SPRN_DAC2, debug->dac2); |
Dave Kleikamp | 3bffb65 | 2010-02-08 11:51:18 +0000 | [diff] [blame] | 759 | #if CONFIG_PPC_ADV_DEBUG_DVCS > 0 |
Scott Wood | f5f9721 | 2013-11-22 15:52:29 -0600 | [diff] [blame] | 760 | mtspr(SPRN_DVC1, debug->dvc1); |
| 761 | mtspr(SPRN_DVC2, debug->dvc2); |
Dave Kleikamp | 3bffb65 | 2010-02-08 11:51:18 +0000 | [diff] [blame] | 762 | #endif |
Scott Wood | f5f9721 | 2013-11-22 15:52:29 -0600 | [diff] [blame] | 763 | mtspr(SPRN_DBCR0, debug->dbcr0); |
| 764 | mtspr(SPRN_DBCR1, debug->dbcr1); |
Dave Kleikamp | 3bffb65 | 2010-02-08 11:51:18 +0000 | [diff] [blame] | 765 | #ifdef CONFIG_BOOKE |
Scott Wood | f5f9721 | 2013-11-22 15:52:29 -0600 | [diff] [blame] | 766 | mtspr(SPRN_DBCR2, debug->dbcr2); |
Dave Kleikamp | 3bffb65 | 2010-02-08 11:51:18 +0000 | [diff] [blame] | 767 | #endif |
| 768 | } |
| 769 | /* |
| 770 | * Unless neither the old or new thread are making use of the |
| 771 | * debug registers, set the debug registers from the values |
| 772 | * stored in the new thread. |
| 773 | */ |
Scott Wood | f5f9721 | 2013-11-22 15:52:29 -0600 | [diff] [blame] | 774 | void switch_booke_debug_regs(struct debug_reg *new_debug) |
Dave Kleikamp | 3bffb65 | 2010-02-08 11:51:18 +0000 | [diff] [blame] | 775 | { |
Bharat Bhushan | 51ae8d4 | 2013-07-04 11:45:46 +0530 | [diff] [blame] | 776 | if ((current->thread.debug.dbcr0 & DBCR0_IDM) |
Scott Wood | f5f9721 | 2013-11-22 15:52:29 -0600 | [diff] [blame] | 777 | || (new_debug->dbcr0 & DBCR0_IDM)) |
| 778 | prime_debug_regs(new_debug); |
Dave Kleikamp | 3bffb65 | 2010-02-08 11:51:18 +0000 | [diff] [blame] | 779 | } |
Bharat Bhushan | 3743c9b | 2013-07-04 12:27:44 +0530 | [diff] [blame] | 780 | EXPORT_SYMBOL_GPL(switch_booke_debug_regs); |
Dave Kleikamp | 3bffb65 | 2010-02-08 11:51:18 +0000 | [diff] [blame] | 781 | #else /* !CONFIG_PPC_ADV_DEBUG_REGS */ |
K.Prasad | e0780b7 | 2011-02-10 04:44:35 +0000 | [diff] [blame] | 782 | #ifndef CONFIG_HAVE_HW_BREAKPOINT |
Ravi Bangoria | 303e6a9 | 2020-05-14 16:47:34 +0530 | [diff] [blame] | 783 | static void set_breakpoint(int i, struct arch_hw_breakpoint *brk) |
Christophe Leroy | b5ac51d | 2018-07-05 16:25:05 +0000 | [diff] [blame] | 784 | { |
| 785 | preempt_disable(); |
Ravi Bangoria | 303e6a9 | 2020-05-14 16:47:34 +0530 | [diff] [blame] | 786 | __set_breakpoint(i, brk); |
Christophe Leroy | b5ac51d | 2018-07-05 16:25:05 +0000 | [diff] [blame] | 787 | preempt_enable(); |
| 788 | } |
| 789 | |
Dave Kleikamp | 3bffb65 | 2010-02-08 11:51:18 +0000 | [diff] [blame] | 790 | static void set_debug_reg_defaults(struct thread_struct *thread) |
| 791 | { |
Ravi Bangoria | 303e6a9 | 2020-05-14 16:47:34 +0530 | [diff] [blame] | 792 | int i; |
| 793 | struct arch_hw_breakpoint null_brk = {0}; |
| 794 | |
| 795 | for (i = 0; i < nr_wp_slots(); i++) { |
| 796 | thread->hw_brk[i] = null_brk; |
| 797 | if (ppc_breakpoint_available()) |
| 798 | set_breakpoint(i, &thread->hw_brk[i]); |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | static inline bool hw_brk_match(struct arch_hw_breakpoint *a, |
| 803 | struct arch_hw_breakpoint *b) |
| 804 | { |
| 805 | if (a->address != b->address) |
| 806 | return false; |
| 807 | if (a->type != b->type) |
| 808 | return false; |
| 809 | if (a->len != b->len) |
| 810 | return false; |
| 811 | /* no need to check hw_len. it's calculated from address and len */ |
| 812 | return true; |
| 813 | } |
| 814 | |
| 815 | static void switch_hw_breakpoint(struct task_struct *new) |
| 816 | { |
| 817 | int i; |
| 818 | |
| 819 | for (i = 0; i < nr_wp_slots(); i++) { |
| 820 | if (likely(hw_brk_match(this_cpu_ptr(¤t_brk[i]), |
| 821 | &new->thread.hw_brk[i]))) |
| 822 | continue; |
| 823 | |
| 824 | __set_breakpoint(i, &new->thread.hw_brk[i]); |
| 825 | } |
Dave Kleikamp | 3bffb65 | 2010-02-08 11:51:18 +0000 | [diff] [blame] | 826 | } |
K.Prasad | e0780b7 | 2011-02-10 04:44:35 +0000 | [diff] [blame] | 827 | #endif /* !CONFIG_HAVE_HW_BREAKPOINT */ |
Dave Kleikamp | 3bffb65 | 2010-02-08 11:51:18 +0000 | [diff] [blame] | 828 | #endif /* CONFIG_PPC_ADV_DEBUG_REGS */ |
| 829 | |
Dave Kleikamp | 172ae2e | 2010-02-08 11:50:57 +0000 | [diff] [blame] | 830 | #ifdef CONFIG_PPC_ADV_DEBUG_REGS |
Michael Neuling | 9422de3 | 2012-12-20 14:06:44 +0000 | [diff] [blame] | 831 | static inline int __set_dabr(unsigned long dabr, unsigned long dabrx) |
| 832 | { |
Benjamin Herrenschmidt | c6c9eac | 2009-09-08 14:16:58 +0000 | [diff] [blame] | 833 | mtspr(SPRN_DAC1, dabr); |
Christophe Leroy | 04d476b | 2020-08-17 05:46:41 +0000 | [diff] [blame] | 834 | if (IS_ENABLED(CONFIG_PPC_47x)) |
| 835 | isync(); |
Michael Neuling | 9422de3 | 2012-12-20 14:06:44 +0000 | [diff] [blame] | 836 | return 0; |
| 837 | } |
Benjamin Herrenschmidt | c6c9eac | 2009-09-08 14:16:58 +0000 | [diff] [blame] | 838 | #elif defined(CONFIG_PPC_BOOK3S) |
Michael Neuling | 9422de3 | 2012-12-20 14:06:44 +0000 | [diff] [blame] | 839 | static inline int __set_dabr(unsigned long dabr, unsigned long dabrx) |
| 840 | { |
Michael Ellerman | cab0af9 | 2005-11-03 15:30:49 +1100 | [diff] [blame] | 841 | mtspr(SPRN_DABR, dabr); |
Michael Neuling | 82a9f16 | 2013-05-16 20:27:31 +0000 | [diff] [blame] | 842 | if (cpu_has_feature(CPU_FTR_DABRX)) |
| 843 | mtspr(SPRN_DABRX, dabrx); |
Michael Ellerman | cab0af9 | 2005-11-03 15:30:49 +1100 | [diff] [blame] | 844 | return 0; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 845 | } |
Michael Neuling | 9422de3 | 2012-12-20 14:06:44 +0000 | [diff] [blame] | 846 | #else |
| 847 | static inline int __set_dabr(unsigned long dabr, unsigned long dabrx) |
| 848 | { |
| 849 | return -EINVAL; |
| 850 | } |
| 851 | #endif |
| 852 | |
| 853 | static inline int set_dabr(struct arch_hw_breakpoint *brk) |
| 854 | { |
| 855 | unsigned long dabr, dabrx; |
| 856 | |
| 857 | dabr = brk->address | (brk->type & HW_BRK_TYPE_DABR); |
| 858 | dabrx = ((brk->type >> 3) & 0x7); |
| 859 | |
| 860 | if (ppc_md.set_dabr) |
| 861 | return ppc_md.set_dabr(dabr, dabrx); |
| 862 | |
| 863 | return __set_dabr(dabr, dabrx); |
| 864 | } |
| 865 | |
Christophe Leroy | 39413ae | 2019-11-26 17:43:29 +0000 | [diff] [blame] | 866 | static inline int set_breakpoint_8xx(struct arch_hw_breakpoint *brk) |
| 867 | { |
| 868 | unsigned long lctrl1 = LCTRL1_CTE_GT | LCTRL1_CTF_LT | LCTRL1_CRWE_RW | |
| 869 | LCTRL1_CRWF_RW; |
| 870 | unsigned long lctrl2 = LCTRL2_LW0EN | LCTRL2_LW0LADC | LCTRL2_SLW0EN; |
Ravi Bangoria | e68ef12 | 2020-05-14 16:47:37 +0530 | [diff] [blame] | 871 | unsigned long start_addr = ALIGN_DOWN(brk->address, HW_BREAKPOINT_SIZE); |
| 872 | unsigned long end_addr = ALIGN(brk->address + brk->len, HW_BREAKPOINT_SIZE); |
Christophe Leroy | 39413ae | 2019-11-26 17:43:29 +0000 | [diff] [blame] | 873 | |
| 874 | if (start_addr == 0) |
| 875 | lctrl2 |= LCTRL2_LW0LA_F; |
Ravi Bangoria | e68ef12 | 2020-05-14 16:47:37 +0530 | [diff] [blame] | 876 | else if (end_addr == 0) |
Christophe Leroy | 39413ae | 2019-11-26 17:43:29 +0000 | [diff] [blame] | 877 | lctrl2 |= LCTRL2_LW0LA_E; |
| 878 | else |
| 879 | lctrl2 |= LCTRL2_LW0LA_EandF; |
| 880 | |
| 881 | mtspr(SPRN_LCTRL2, 0); |
| 882 | |
| 883 | if ((brk->type & HW_BRK_TYPE_RDWR) == 0) |
| 884 | return 0; |
| 885 | |
| 886 | if ((brk->type & HW_BRK_TYPE_RDWR) == HW_BRK_TYPE_READ) |
| 887 | lctrl1 |= LCTRL1_CRWE_RO | LCTRL1_CRWF_RO; |
| 888 | if ((brk->type & HW_BRK_TYPE_RDWR) == HW_BRK_TYPE_WRITE) |
| 889 | lctrl1 |= LCTRL1_CRWE_WO | LCTRL1_CRWF_WO; |
| 890 | |
| 891 | mtspr(SPRN_CMPE, start_addr - 1); |
Ravi Bangoria | e68ef12 | 2020-05-14 16:47:37 +0530 | [diff] [blame] | 892 | mtspr(SPRN_CMPF, end_addr); |
Christophe Leroy | 39413ae | 2019-11-26 17:43:29 +0000 | [diff] [blame] | 893 | mtspr(SPRN_LCTRL1, lctrl1); |
| 894 | mtspr(SPRN_LCTRL2, lctrl2); |
| 895 | |
| 896 | return 0; |
| 897 | } |
| 898 | |
Ravi Bangoria | 4a8a937 | 2020-05-14 16:47:31 +0530 | [diff] [blame] | 899 | void __set_breakpoint(int nr, struct arch_hw_breakpoint *brk) |
Michael Neuling | 9422de3 | 2012-12-20 14:06:44 +0000 | [diff] [blame] | 900 | { |
Ravi Bangoria | 4a8a937 | 2020-05-14 16:47:31 +0530 | [diff] [blame] | 901 | memcpy(this_cpu_ptr(¤t_brk[nr]), brk, sizeof(*brk)); |
Michael Neuling | 9422de3 | 2012-12-20 14:06:44 +0000 | [diff] [blame] | 902 | |
Michael Neuling | c1fe190 | 2019-04-01 17:03:12 +1100 | [diff] [blame] | 903 | if (dawr_enabled()) |
Nicholas Piggin | 252988c | 2018-04-01 15:50:36 +1000 | [diff] [blame] | 904 | // Power8 or later |
Ravi Bangoria | 4a8a937 | 2020-05-14 16:47:31 +0530 | [diff] [blame] | 905 | set_dawr(nr, brk); |
Christophe Leroy | 39413ae | 2019-11-26 17:43:29 +0000 | [diff] [blame] | 906 | else if (IS_ENABLED(CONFIG_PPC_8xx)) |
| 907 | set_breakpoint_8xx(brk); |
Nicholas Piggin | 252988c | 2018-04-01 15:50:36 +1000 | [diff] [blame] | 908 | else if (!cpu_has_feature(CPU_FTR_ARCH_207S)) |
| 909 | // Power7 or earlier |
Paul Gortmaker | 04c32a5 | 2014-04-29 15:25:16 -0400 | [diff] [blame] | 910 | set_dabr(brk); |
Nicholas Piggin | 252988c | 2018-04-01 15:50:36 +1000 | [diff] [blame] | 911 | else |
| 912 | // Shouldn't happen due to higher level checks |
| 913 | WARN_ON_ONCE(1); |
Michael Neuling | 9422de3 | 2012-12-20 14:06:44 +0000 | [diff] [blame] | 914 | } |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 915 | |
Michael Neuling | 404b27d | 2018-03-27 15:37:17 +1100 | [diff] [blame] | 916 | /* Check if we have DAWR or DABR hardware */ |
| 917 | bool ppc_breakpoint_available(void) |
| 918 | { |
Michael Neuling | c1fe190 | 2019-04-01 17:03:12 +1100 | [diff] [blame] | 919 | if (dawr_enabled()) |
| 920 | return true; /* POWER8 DAWR or POWER9 forced DAWR */ |
Michael Neuling | 404b27d | 2018-03-27 15:37:17 +1100 | [diff] [blame] | 921 | if (cpu_has_feature(CPU_FTR_ARCH_207S)) |
| 922 | return false; /* POWER9 with DAWR disabled */ |
| 923 | /* DABR: Everything but POWER8 and POWER9 */ |
| 924 | return true; |
| 925 | } |
| 926 | EXPORT_SYMBOL_GPL(ppc_breakpoint_available); |
| 927 | |
Michael Neuling | fb09692 | 2013-02-13 16:21:37 +0000 | [diff] [blame] | 928 | #ifdef CONFIG_PPC_TRANSACTIONAL_MEM |
Cyril Bur | 5d176f7 | 2016-09-14 18:02:16 +1000 | [diff] [blame] | 929 | |
| 930 | static inline bool tm_enabled(struct task_struct *tsk) |
| 931 | { |
| 932 | return tsk && tsk->thread.regs && (tsk->thread.regs->msr & MSR_TM); |
| 933 | } |
| 934 | |
Cyril Bur | edd00b8 | 2018-02-01 12:07:46 +1100 | [diff] [blame] | 935 | static void tm_reclaim_thread(struct thread_struct *thr, uint8_t cause) |
Paul Mackerras | d31626f | 2014-01-13 15:56:29 +1100 | [diff] [blame] | 936 | { |
Michael Neuling | 7f821fc | 2015-11-19 15:44:45 +1100 | [diff] [blame] | 937 | /* |
| 938 | * Use the current MSR TM suspended bit to track if we have |
| 939 | * checkpointed state outstanding. |
| 940 | * On signal delivery, we'd normally reclaim the checkpointed |
| 941 | * state to obtain stack pointer (see:get_tm_stackpointer()). |
| 942 | * This will then directly return to userspace without going |
| 943 | * through __switch_to(). However, if the stack frame is bad, |
| 944 | * we need to exit this thread which calls __switch_to() which |
| 945 | * will again attempt to reclaim the already saved tm state. |
| 946 | * Hence we need to check that we've not already reclaimed |
| 947 | * this state. |
| 948 | * We do this using the current MSR, rather tracking it in |
| 949 | * some specific thread_struct bit, as it has the additional |
Michael Ellerman | 027dfac | 2016-06-01 16:34:37 +1000 | [diff] [blame] | 950 | * benefit of checking for a potential TM bad thing exception. |
Michael Neuling | 7f821fc | 2015-11-19 15:44:45 +1100 | [diff] [blame] | 951 | */ |
| 952 | if (!MSR_TM_SUSPENDED(mfmsr())) |
| 953 | return; |
| 954 | |
Cyril Bur | 91381b9 | 2017-11-02 14:09:04 +1100 | [diff] [blame] | 955 | giveup_all(container_of(thr, struct task_struct, thread)); |
| 956 | |
Cyril Bur | eb5c3f1 | 2017-11-02 14:09:05 +1100 | [diff] [blame] | 957 | tm_reclaim(thr, cause); |
| 958 | |
Michael Neuling | f48e91e | 2017-05-08 17:16:26 +1000 | [diff] [blame] | 959 | /* |
| 960 | * If we are in a transaction and FP is off then we can't have |
| 961 | * used FP inside that transaction. Hence the checkpointed |
| 962 | * state is the same as the live state. We need to copy the |
| 963 | * live state to the checkpointed state so that when the |
| 964 | * transaction is restored, the checkpointed state is correct |
| 965 | * and the aborted transaction sees the correct state. We use |
| 966 | * ckpt_regs.msr here as that's what tm_reclaim will use to |
| 967 | * determine if it's going to write the checkpointed state or |
| 968 | * not. So either this will write the checkpointed registers, |
| 969 | * or reclaim will. Similarly for VMX. |
| 970 | */ |
| 971 | if ((thr->ckpt_regs.msr & MSR_FP) == 0) |
| 972 | memcpy(&thr->ckfp_state, &thr->fp_state, |
| 973 | sizeof(struct thread_fp_state)); |
| 974 | if ((thr->ckpt_regs.msr & MSR_VEC) == 0) |
| 975 | memcpy(&thr->ckvr_state, &thr->vr_state, |
| 976 | sizeof(struct thread_vr_state)); |
Paul Mackerras | d31626f | 2014-01-13 15:56:29 +1100 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | void tm_reclaim_current(uint8_t cause) |
| 980 | { |
| 981 | tm_enable(); |
Cyril Bur | edd00b8 | 2018-02-01 12:07:46 +1100 | [diff] [blame] | 982 | tm_reclaim_thread(¤t->thread, cause); |
Paul Mackerras | d31626f | 2014-01-13 15:56:29 +1100 | [diff] [blame] | 983 | } |
| 984 | |
Michael Neuling | fb09692 | 2013-02-13 16:21:37 +0000 | [diff] [blame] | 985 | static inline void tm_reclaim_task(struct task_struct *tsk) |
| 986 | { |
| 987 | /* We have to work out if we're switching from/to a task that's in the |
| 988 | * middle of a transaction. |
| 989 | * |
| 990 | * In switching we need to maintain a 2nd register state as |
| 991 | * oldtask->thread.ckpt_regs. We tm_reclaim(oldproc); this saves the |
Cyril Bur | 000ec28 | 2016-09-23 16:18:25 +1000 | [diff] [blame] | 992 | * checkpointed (tbegin) state in ckpt_regs, ckfp_state and |
| 993 | * ckvr_state |
Michael Neuling | fb09692 | 2013-02-13 16:21:37 +0000 | [diff] [blame] | 994 | * |
| 995 | * We also context switch (save) TFHAR/TEXASR/TFIAR in here. |
| 996 | */ |
| 997 | struct thread_struct *thr = &tsk->thread; |
| 998 | |
| 999 | if (!thr->regs) |
| 1000 | return; |
| 1001 | |
| 1002 | if (!MSR_TM_ACTIVE(thr->regs->msr)) |
| 1003 | goto out_and_saveregs; |
| 1004 | |
Michael Neuling | 92fb869 | 2017-10-12 21:17:19 +1100 | [diff] [blame] | 1005 | WARN_ON(tm_suspend_disabled); |
| 1006 | |
Michael Neuling | fb09692 | 2013-02-13 16:21:37 +0000 | [diff] [blame] | 1007 | TM_DEBUG("--- tm_reclaim on pid %d (NIP=%lx, " |
| 1008 | "ccr=%lx, msr=%lx, trap=%lx)\n", |
| 1009 | tsk->pid, thr->regs->nip, |
| 1010 | thr->regs->ccr, thr->regs->msr, |
| 1011 | thr->regs->trap); |
| 1012 | |
Cyril Bur | edd00b8 | 2018-02-01 12:07:46 +1100 | [diff] [blame] | 1013 | tm_reclaim_thread(thr, TM_CAUSE_RESCHED); |
Michael Neuling | fb09692 | 2013-02-13 16:21:37 +0000 | [diff] [blame] | 1014 | |
| 1015 | TM_DEBUG("--- tm_reclaim on pid %d complete\n", |
| 1016 | tsk->pid); |
| 1017 | |
| 1018 | out_and_saveregs: |
| 1019 | /* Always save the regs here, even if a transaction's not active. |
| 1020 | * This context-switches a thread's TM info SPRs. We do it here to |
| 1021 | * be consistent with the restore path (in recheckpoint) which |
| 1022 | * cannot happen later in _switch(). |
| 1023 | */ |
| 1024 | tm_save_sprs(thr); |
| 1025 | } |
| 1026 | |
Cyril Bur | eb5c3f1 | 2017-11-02 14:09:05 +1100 | [diff] [blame] | 1027 | extern void __tm_recheckpoint(struct thread_struct *thread); |
Michael Neuling | e6b8fd0 | 2014-04-04 20:19:48 +1100 | [diff] [blame] | 1028 | |
Cyril Bur | eb5c3f1 | 2017-11-02 14:09:05 +1100 | [diff] [blame] | 1029 | void tm_recheckpoint(struct thread_struct *thread) |
Michael Neuling | e6b8fd0 | 2014-04-04 20:19:48 +1100 | [diff] [blame] | 1030 | { |
| 1031 | unsigned long flags; |
| 1032 | |
Cyril Bur | 5d176f7 | 2016-09-14 18:02:16 +1000 | [diff] [blame] | 1033 | if (!(thread->regs->msr & MSR_TM)) |
| 1034 | return; |
| 1035 | |
Michael Neuling | e6b8fd0 | 2014-04-04 20:19:48 +1100 | [diff] [blame] | 1036 | /* We really can't be interrupted here as the TEXASR registers can't |
| 1037 | * change and later in the trecheckpoint code, we have a userspace R1. |
| 1038 | * So let's hard disable over this region. |
| 1039 | */ |
| 1040 | local_irq_save(flags); |
| 1041 | hard_irq_disable(); |
| 1042 | |
| 1043 | /* The TM SPRs are restored here, so that TEXASR.FS can be set |
| 1044 | * before the trecheckpoint and no explosion occurs. |
| 1045 | */ |
| 1046 | tm_restore_sprs(thread); |
| 1047 | |
Cyril Bur | eb5c3f1 | 2017-11-02 14:09:05 +1100 | [diff] [blame] | 1048 | __tm_recheckpoint(thread); |
Michael Neuling | e6b8fd0 | 2014-04-04 20:19:48 +1100 | [diff] [blame] | 1049 | |
| 1050 | local_irq_restore(flags); |
| 1051 | } |
| 1052 | |
Michael Neuling | bc2a940 | 2013-02-13 16:21:40 +0000 | [diff] [blame] | 1053 | static inline void tm_recheckpoint_new_task(struct task_struct *new) |
Michael Neuling | fb09692 | 2013-02-13 16:21:37 +0000 | [diff] [blame] | 1054 | { |
Michael Neuling | fb09692 | 2013-02-13 16:21:37 +0000 | [diff] [blame] | 1055 | if (!cpu_has_feature(CPU_FTR_TM)) |
| 1056 | return; |
| 1057 | |
| 1058 | /* Recheckpoint the registers of the thread we're about to switch to. |
| 1059 | * |
| 1060 | * If the task was using FP, we non-lazily reload both the original and |
| 1061 | * the speculative FP register states. This is because the kernel |
| 1062 | * doesn't see if/when a TM rollback occurs, so if we take an FP |
Cyril Bur | dc31066 | 2016-09-23 16:18:24 +1000 | [diff] [blame] | 1063 | * unavailable later, we are unable to determine which set of FP regs |
Michael Neuling | fb09692 | 2013-02-13 16:21:37 +0000 | [diff] [blame] | 1064 | * need to be restored. |
| 1065 | */ |
Cyril Bur | 5d176f7 | 2016-09-14 18:02:16 +1000 | [diff] [blame] | 1066 | if (!tm_enabled(new)) |
Michael Neuling | fb09692 | 2013-02-13 16:21:37 +0000 | [diff] [blame] | 1067 | return; |
| 1068 | |
Michael Neuling | e6b8fd0 | 2014-04-04 20:19:48 +1100 | [diff] [blame] | 1069 | if (!MSR_TM_ACTIVE(new->thread.regs->msr)){ |
| 1070 | tm_restore_sprs(&new->thread); |
Michael Neuling | fb09692 | 2013-02-13 16:21:37 +0000 | [diff] [blame] | 1071 | return; |
Michael Neuling | e6b8fd0 | 2014-04-04 20:19:48 +1100 | [diff] [blame] | 1072 | } |
Michael Neuling | fb09692 | 2013-02-13 16:21:37 +0000 | [diff] [blame] | 1073 | /* Recheckpoint to restore original checkpointed register state. */ |
Cyril Bur | eb5c3f1 | 2017-11-02 14:09:05 +1100 | [diff] [blame] | 1074 | TM_DEBUG("*** tm_recheckpoint of pid %d (new->msr 0x%lx)\n", |
| 1075 | new->pid, new->thread.regs->msr); |
Michael Neuling | fb09692 | 2013-02-13 16:21:37 +0000 | [diff] [blame] | 1076 | |
Cyril Bur | eb5c3f1 | 2017-11-02 14:09:05 +1100 | [diff] [blame] | 1077 | tm_recheckpoint(&new->thread); |
Michael Neuling | fb09692 | 2013-02-13 16:21:37 +0000 | [diff] [blame] | 1078 | |
Cyril Bur | dc31066 | 2016-09-23 16:18:24 +1000 | [diff] [blame] | 1079 | /* |
| 1080 | * The checkpointed state has been restored but the live state has |
| 1081 | * not, ensure all the math functionality is turned off to trigger |
| 1082 | * restore_math() to reload. |
| 1083 | */ |
| 1084 | new->thread.regs->msr &= ~(MSR_FP | MSR_VEC | MSR_VSX); |
Michael Neuling | fb09692 | 2013-02-13 16:21:37 +0000 | [diff] [blame] | 1085 | |
| 1086 | TM_DEBUG("*** tm_recheckpoint of pid %d complete " |
| 1087 | "(kernel msr 0x%lx)\n", |
| 1088 | new->pid, mfmsr()); |
| 1089 | } |
| 1090 | |
Cyril Bur | dc31066 | 2016-09-23 16:18:24 +1000 | [diff] [blame] | 1091 | static inline void __switch_to_tm(struct task_struct *prev, |
| 1092 | struct task_struct *new) |
Michael Neuling | fb09692 | 2013-02-13 16:21:37 +0000 | [diff] [blame] | 1093 | { |
| 1094 | if (cpu_has_feature(CPU_FTR_TM)) { |
Cyril Bur | 5d176f7 | 2016-09-14 18:02:16 +1000 | [diff] [blame] | 1095 | if (tm_enabled(prev) || tm_enabled(new)) |
| 1096 | tm_enable(); |
| 1097 | |
| 1098 | if (tm_enabled(prev)) { |
| 1099 | prev->thread.load_tm++; |
| 1100 | tm_reclaim_task(prev); |
| 1101 | if (!MSR_TM_ACTIVE(prev->thread.regs->msr) && prev->thread.load_tm == 0) |
| 1102 | prev->thread.regs->msr &= ~MSR_TM; |
| 1103 | } |
| 1104 | |
Cyril Bur | dc31066 | 2016-09-23 16:18:24 +1000 | [diff] [blame] | 1105 | tm_recheckpoint_new_task(new); |
Michael Neuling | fb09692 | 2013-02-13 16:21:37 +0000 | [diff] [blame] | 1106 | } |
| 1107 | } |
Paul Mackerras | d31626f | 2014-01-13 15:56:29 +1100 | [diff] [blame] | 1108 | |
| 1109 | /* |
| 1110 | * This is called if we are on the way out to userspace and the |
| 1111 | * TIF_RESTORE_TM flag is set. It checks if we need to reload |
| 1112 | * FP and/or vector state and does so if necessary. |
| 1113 | * If userspace is inside a transaction (whether active or |
| 1114 | * suspended) and FP/VMX/VSX instructions have ever been enabled |
| 1115 | * inside that transaction, then we have to keep them enabled |
| 1116 | * and keep the FP/VMX/VSX state loaded while ever the transaction |
| 1117 | * continues. The reason is that if we didn't, and subsequently |
| 1118 | * got a FP/VMX/VSX unavailable interrupt inside a transaction, |
| 1119 | * we don't know whether it's the same transaction, and thus we |
| 1120 | * don't know which of the checkpointed state and the transactional |
| 1121 | * state to use. |
| 1122 | */ |
| 1123 | void restore_tm_state(struct pt_regs *regs) |
| 1124 | { |
| 1125 | unsigned long msr_diff; |
| 1126 | |
Cyril Bur | dc31066 | 2016-09-23 16:18:24 +1000 | [diff] [blame] | 1127 | /* |
| 1128 | * This is the only moment we should clear TIF_RESTORE_TM as |
| 1129 | * it is here that ckpt_regs.msr and pt_regs.msr become the same |
| 1130 | * again, anything else could lead to an incorrect ckpt_msr being |
| 1131 | * saved and therefore incorrect signal contexts. |
| 1132 | */ |
Paul Mackerras | d31626f | 2014-01-13 15:56:29 +1100 | [diff] [blame] | 1133 | clear_thread_flag(TIF_RESTORE_TM); |
| 1134 | if (!MSR_TM_ACTIVE(regs->msr)) |
| 1135 | return; |
| 1136 | |
Anshuman Khandual | 829023d | 2015-07-06 16:24:10 +0530 | [diff] [blame] | 1137 | msr_diff = current->thread.ckpt_regs.msr & ~regs->msr; |
Paul Mackerras | d31626f | 2014-01-13 15:56:29 +1100 | [diff] [blame] | 1138 | msr_diff &= MSR_FP | MSR_VEC | MSR_VSX; |
Cyril Bur | 70fe3d9 | 2016-02-29 17:53:47 +1100 | [diff] [blame] | 1139 | |
Cyril Bur | dc16b55 | 2016-09-23 16:18:08 +1000 | [diff] [blame] | 1140 | /* Ensure that restore_math() will restore */ |
| 1141 | if (msr_diff & MSR_FP) |
| 1142 | current->thread.load_fp = 1; |
Valentin Rothberg | 39715bf | 2016-10-05 07:57:26 +0200 | [diff] [blame] | 1143 | #ifdef CONFIG_ALTIVEC |
Cyril Bur | dc16b55 | 2016-09-23 16:18:08 +1000 | [diff] [blame] | 1144 | if (cpu_has_feature(CPU_FTR_ALTIVEC) && msr_diff & MSR_VEC) |
| 1145 | current->thread.load_vec = 1; |
| 1146 | #endif |
Cyril Bur | 70fe3d9 | 2016-02-29 17:53:47 +1100 | [diff] [blame] | 1147 | restore_math(regs); |
| 1148 | |
Paul Mackerras | d31626f | 2014-01-13 15:56:29 +1100 | [diff] [blame] | 1149 | regs->msr |= msr_diff; |
| 1150 | } |
| 1151 | |
Michael Neuling | fb09692 | 2013-02-13 16:21:37 +0000 | [diff] [blame] | 1152 | #else |
| 1153 | #define tm_recheckpoint_new_task(new) |
Cyril Bur | dc31066 | 2016-09-23 16:18:24 +1000 | [diff] [blame] | 1154 | #define __switch_to_tm(prev, new) |
Michael Neuling | fb09692 | 2013-02-13 16:21:37 +0000 | [diff] [blame] | 1155 | #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */ |
Michael Neuling | 9422de3 | 2012-12-20 14:06:44 +0000 | [diff] [blame] | 1156 | |
Anton Blanchard | 152d523 | 2015-10-29 11:43:55 +1100 | [diff] [blame] | 1157 | static inline void save_sprs(struct thread_struct *t) |
| 1158 | { |
| 1159 | #ifdef CONFIG_ALTIVEC |
Oliver O'Halloran | 01d7c2a2 | 2016-03-08 09:08:47 +1100 | [diff] [blame] | 1160 | if (cpu_has_feature(CPU_FTR_ALTIVEC)) |
Anton Blanchard | 152d523 | 2015-10-29 11:43:55 +1100 | [diff] [blame] | 1161 | t->vrsave = mfspr(SPRN_VRSAVE); |
| 1162 | #endif |
| 1163 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 1164 | if (cpu_has_feature(CPU_FTR_DSCR)) |
| 1165 | t->dscr = mfspr(SPRN_DSCR); |
| 1166 | |
| 1167 | if (cpu_has_feature(CPU_FTR_ARCH_207S)) { |
| 1168 | t->bescr = mfspr(SPRN_BESCR); |
| 1169 | t->ebbhr = mfspr(SPRN_EBBHR); |
| 1170 | t->ebbrr = mfspr(SPRN_EBBRR); |
| 1171 | |
| 1172 | t->fscr = mfspr(SPRN_FSCR); |
| 1173 | |
| 1174 | /* |
| 1175 | * Note that the TAR is not available for use in the kernel. |
| 1176 | * (To provide this, the TAR should be backed up/restored on |
| 1177 | * exception entry/exit instead, and be in pt_regs. FIXME, |
| 1178 | * this should be in pt_regs anyway (for debug).) |
| 1179 | */ |
| 1180 | t->tar = mfspr(SPRN_TAR); |
| 1181 | } |
| 1182 | #endif |
Ram Pai | 06bb53b | 2018-01-18 17:50:31 -0800 | [diff] [blame] | 1183 | |
| 1184 | thread_pkey_regs_save(t); |
Anton Blanchard | 152d523 | 2015-10-29 11:43:55 +1100 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | static inline void restore_sprs(struct thread_struct *old_thread, |
| 1188 | struct thread_struct *new_thread) |
| 1189 | { |
| 1190 | #ifdef CONFIG_ALTIVEC |
| 1191 | if (cpu_has_feature(CPU_FTR_ALTIVEC) && |
| 1192 | old_thread->vrsave != new_thread->vrsave) |
| 1193 | mtspr(SPRN_VRSAVE, new_thread->vrsave); |
| 1194 | #endif |
| 1195 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 1196 | if (cpu_has_feature(CPU_FTR_DSCR)) { |
| 1197 | u64 dscr = get_paca()->dscr_default; |
Michael Neuling | b57bd2d | 2016-06-09 12:31:08 +1000 | [diff] [blame] | 1198 | if (new_thread->dscr_inherit) |
Anton Blanchard | 152d523 | 2015-10-29 11:43:55 +1100 | [diff] [blame] | 1199 | dscr = new_thread->dscr; |
Anton Blanchard | 152d523 | 2015-10-29 11:43:55 +1100 | [diff] [blame] | 1200 | |
| 1201 | if (old_thread->dscr != dscr) |
| 1202 | mtspr(SPRN_DSCR, dscr); |
Anton Blanchard | 152d523 | 2015-10-29 11:43:55 +1100 | [diff] [blame] | 1203 | } |
| 1204 | |
| 1205 | if (cpu_has_feature(CPU_FTR_ARCH_207S)) { |
| 1206 | if (old_thread->bescr != new_thread->bescr) |
| 1207 | mtspr(SPRN_BESCR, new_thread->bescr); |
| 1208 | if (old_thread->ebbhr != new_thread->ebbhr) |
| 1209 | mtspr(SPRN_EBBHR, new_thread->ebbhr); |
| 1210 | if (old_thread->ebbrr != new_thread->ebbrr) |
| 1211 | mtspr(SPRN_EBBRR, new_thread->ebbrr); |
| 1212 | |
Michael Neuling | b57bd2d | 2016-06-09 12:31:08 +1000 | [diff] [blame] | 1213 | if (old_thread->fscr != new_thread->fscr) |
| 1214 | mtspr(SPRN_FSCR, new_thread->fscr); |
| 1215 | |
Anton Blanchard | 152d523 | 2015-10-29 11:43:55 +1100 | [diff] [blame] | 1216 | if (old_thread->tar != new_thread->tar) |
| 1217 | mtspr(SPRN_TAR, new_thread->tar); |
| 1218 | } |
Sukadev Bhattiprolu | ec233ed | 2017-11-07 18:23:53 -0800 | [diff] [blame] | 1219 | |
Alastair D'Silva | 3449f19 | 2018-05-11 16:12:58 +1000 | [diff] [blame] | 1220 | if (cpu_has_feature(CPU_FTR_P9_TIDR) && |
Sukadev Bhattiprolu | ec233ed | 2017-11-07 18:23:53 -0800 | [diff] [blame] | 1221 | old_thread->tidr != new_thread->tidr) |
| 1222 | mtspr(SPRN_TIDR, new_thread->tidr); |
Anton Blanchard | 152d523 | 2015-10-29 11:43:55 +1100 | [diff] [blame] | 1223 | #endif |
Ram Pai | 06bb53b | 2018-01-18 17:50:31 -0800 | [diff] [blame] | 1224 | |
| 1225 | thread_pkey_regs_restore(new_thread, old_thread); |
Anton Blanchard | 152d523 | 2015-10-29 11:43:55 +1100 | [diff] [blame] | 1226 | } |
| 1227 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1228 | struct task_struct *__switch_to(struct task_struct *prev, |
| 1229 | struct task_struct *new) |
| 1230 | { |
| 1231 | struct thread_struct *new_thread, *old_thread; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1232 | struct task_struct *last; |
Peter Zijlstra | d6bf29b | 2011-05-24 17:11:48 -0700 | [diff] [blame] | 1233 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 1234 | struct ppc64_tlb_batch *batch; |
| 1235 | #endif |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1236 | |
Anton Blanchard | 152d523 | 2015-10-29 11:43:55 +1100 | [diff] [blame] | 1237 | new_thread = &new->thread; |
| 1238 | old_thread = ¤t->thread; |
| 1239 | |
Michael Neuling | 7ba5fef | 2013-10-02 17:15:14 +1000 | [diff] [blame] | 1240 | WARN_ON(!irqs_disabled()); |
| 1241 | |
Michael Ellerman | 4e00374 | 2017-10-19 15:08:43 +1100 | [diff] [blame] | 1242 | #ifdef CONFIG_PPC_BOOK3S_64 |
Christoph Lameter | 69111ba | 2014-10-21 15:23:25 -0500 | [diff] [blame] | 1243 | batch = this_cpu_ptr(&ppc64_tlb_batch); |
Peter Zijlstra | d6bf29b | 2011-05-24 17:11:48 -0700 | [diff] [blame] | 1244 | if (batch->active) { |
| 1245 | current_thread_info()->local_flags |= _TLF_LAZY_MMU; |
| 1246 | if (batch->index) |
| 1247 | __flush_tlb_pending(batch); |
| 1248 | batch->active = 0; |
| 1249 | } |
Michael Ellerman | 4e00374 | 2017-10-19 15:08:43 +1100 | [diff] [blame] | 1250 | #endif /* CONFIG_PPC_BOOK3S_64 */ |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1251 | |
Anton Blanchard | f3d885c | 2015-10-29 11:44:10 +1100 | [diff] [blame] | 1252 | #ifdef CONFIG_PPC_ADV_DEBUG_REGS |
| 1253 | switch_booke_debug_regs(&new->thread.debug); |
| 1254 | #else |
| 1255 | /* |
| 1256 | * For PPC_BOOK3S_64, we use the hw-breakpoint interfaces that would |
| 1257 | * schedule DABR |
| 1258 | */ |
| 1259 | #ifndef CONFIG_HAVE_HW_BREAKPOINT |
Ravi Bangoria | 303e6a9 | 2020-05-14 16:47:34 +0530 | [diff] [blame] | 1260 | switch_hw_breakpoint(new); |
Anton Blanchard | f3d885c | 2015-10-29 11:44:10 +1100 | [diff] [blame] | 1261 | #endif /* CONFIG_HAVE_HW_BREAKPOINT */ |
| 1262 | #endif |
| 1263 | |
| 1264 | /* |
| 1265 | * We need to save SPRs before treclaim/trecheckpoint as these will |
| 1266 | * change a number of them. |
| 1267 | */ |
| 1268 | save_sprs(&prev->thread); |
| 1269 | |
Anton Blanchard | f3d885c | 2015-10-29 11:44:10 +1100 | [diff] [blame] | 1270 | /* Save FPU, Altivec, VSX and SPE state */ |
| 1271 | giveup_all(prev); |
| 1272 | |
Cyril Bur | dc31066 | 2016-09-23 16:18:24 +1000 | [diff] [blame] | 1273 | __switch_to_tm(prev, new); |
| 1274 | |
Nicholas Piggin | e4c0fc5 | 2017-06-09 01:36:06 +1000 | [diff] [blame] | 1275 | if (!radix_enabled()) { |
| 1276 | /* |
| 1277 | * We can't take a PMU exception inside _switch() since there |
| 1278 | * is a window where the kernel stack SLB and the kernel stack |
| 1279 | * are out of sync. Hard disable here. |
| 1280 | */ |
| 1281 | hard_irq_disable(); |
| 1282 | } |
Michael Neuling | bc2a940 | 2013-02-13 16:21:40 +0000 | [diff] [blame] | 1283 | |
Anton Blanchard | 20dbe67 | 2015-12-10 20:44:39 +1100 | [diff] [blame] | 1284 | /* |
| 1285 | * Call restore_sprs() before calling _switch(). If we move it after |
| 1286 | * _switch() then we miss out on calling it for new tasks. The reason |
| 1287 | * for this is we manually create a stack frame for new tasks that |
| 1288 | * directly returns through ret_from_fork() or |
| 1289 | * ret_from_kernel_thread(). See copy_thread() for details. |
| 1290 | */ |
Anton Blanchard | f3d885c | 2015-10-29 11:44:10 +1100 | [diff] [blame] | 1291 | restore_sprs(old_thread, new_thread); |
| 1292 | |
Anton Blanchard | 20dbe67 | 2015-12-10 20:44:39 +1100 | [diff] [blame] | 1293 | last = _switch(old_thread, new_thread); |
| 1294 | |
Michael Ellerman | 4e00374 | 2017-10-19 15:08:43 +1100 | [diff] [blame] | 1295 | #ifdef CONFIG_PPC_BOOK3S_64 |
Peter Zijlstra | d6bf29b | 2011-05-24 17:11:48 -0700 | [diff] [blame] | 1296 | if (current_thread_info()->local_flags & _TLF_LAZY_MMU) { |
| 1297 | current_thread_info()->local_flags &= ~_TLF_LAZY_MMU; |
Christoph Lameter | 69111ba | 2014-10-21 15:23:25 -0500 | [diff] [blame] | 1298 | batch = this_cpu_ptr(&ppc64_tlb_batch); |
Peter Zijlstra | d6bf29b | 2011-05-24 17:11:48 -0700 | [diff] [blame] | 1299 | batch->active = 1; |
| 1300 | } |
Cyril Bur | 70fe3d9 | 2016-02-29 17:53:47 +1100 | [diff] [blame] | 1301 | |
Christophe Leroy | 05b9879 | 2019-01-17 23:25:12 +1100 | [diff] [blame] | 1302 | if (current->thread.regs) { |
| 1303 | restore_math(current->thread.regs); |
Nicholas Piggin | 07d2a62 | 2017-06-09 01:36:09 +1000 | [diff] [blame] | 1304 | |
| 1305 | /* |
Nicholas Piggin | dc46226 | 2020-08-25 17:55:35 +1000 | [diff] [blame] | 1306 | * On POWER9 the copy-paste buffer can only paste into |
| 1307 | * foreign real addresses, so unprivileged processes can not |
| 1308 | * see the data or use it in any way unless they have |
| 1309 | * foreign real mappings. If the new process has the foreign |
| 1310 | * real address mappings, we must issue a cp_abort to clear |
| 1311 | * any state and prevent snooping, corruption or a covert |
| 1312 | * channel. ISA v3.1 supports paste into local memory. |
Nicholas Piggin | 07d2a62 | 2017-06-09 01:36:09 +1000 | [diff] [blame] | 1313 | */ |
Haren Myneni | c420644 | 2020-04-15 23:08:11 -0700 | [diff] [blame] | 1314 | if (current->mm && |
Nicholas Piggin | dc46226 | 2020-08-25 17:55:35 +1000 | [diff] [blame] | 1315 | (cpu_has_feature(CPU_FTR_ARCH_31) || |
| 1316 | atomic_read(¤t->mm->context.vas_windows))) |
Sukadev Bhattiprolu | 9d2a4d7 | 2017-11-07 18:23:54 -0800 | [diff] [blame] | 1317 | asm volatile(PPC_CP_ABORT); |
Nicholas Piggin | 07d2a62 | 2017-06-09 01:36:09 +1000 | [diff] [blame] | 1318 | } |
Michael Ellerman | 4e00374 | 2017-10-19 15:08:43 +1100 | [diff] [blame] | 1319 | #endif /* CONFIG_PPC_BOOK3S_64 */ |
Peter Zijlstra | d6bf29b | 2011-05-24 17:11:48 -0700 | [diff] [blame] | 1320 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1321 | return last; |
| 1322 | } |
| 1323 | |
Christophe Leroy | df13102 | 2018-10-06 16:51:16 +0000 | [diff] [blame] | 1324 | #define NR_INSN_TO_PRINT 16 |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1325 | |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1326 | static void show_instructions(struct pt_regs *regs) |
| 1327 | { |
| 1328 | int i; |
Aneesh Kumar K.V | a6e2c22 | 2020-05-24 15:08:19 +0530 | [diff] [blame] | 1329 | unsigned long nip = regs->nip; |
Christophe Leroy | df13102 | 2018-10-06 16:51:16 +0000 | [diff] [blame] | 1330 | unsigned long pc = regs->nip - (NR_INSN_TO_PRINT * 3 / 4 * sizeof(int)); |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1331 | |
| 1332 | printk("Instruction dump:"); |
| 1333 | |
Aneesh Kumar K.V | a6e2c22 | 2020-05-24 15:08:19 +0530 | [diff] [blame] | 1334 | /* |
| 1335 | * If we were executing with the MMU off for instructions, adjust pc |
| 1336 | * rather than printing XXXXXXXX. |
| 1337 | */ |
| 1338 | if (!IS_ENABLED(CONFIG_BOOKE) && !(regs->msr & MSR_IR)) { |
| 1339 | pc = (unsigned long)phys_to_virt(pc); |
| 1340 | nip = (unsigned long)phys_to_virt(regs->nip); |
| 1341 | } |
| 1342 | |
Christophe Leroy | df13102 | 2018-10-06 16:51:16 +0000 | [diff] [blame] | 1343 | for (i = 0; i < NR_INSN_TO_PRINT; i++) { |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1344 | int instr; |
| 1345 | |
| 1346 | if (!(i % 8)) |
Andrew Donnellan | 2ffd04d | 2016-11-04 17:20:40 +1100 | [diff] [blame] | 1347 | pr_cont("\n"); |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1348 | |
Anton Blanchard | 00ae36d | 2006-10-13 12:17:16 +1000 | [diff] [blame] | 1349 | if (!__kernel_text_address(pc) || |
Christoph Hellwig | 25f12ae | 2020-06-17 09:37:55 +0200 | [diff] [blame] | 1350 | get_kernel_nofault(instr, (const void *)pc)) { |
Andrew Donnellan | 2ffd04d | 2016-11-04 17:20:40 +1100 | [diff] [blame] | 1351 | pr_cont("XXXXXXXX "); |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1352 | } else { |
Aneesh Kumar K.V | a6e2c22 | 2020-05-24 15:08:19 +0530 | [diff] [blame] | 1353 | if (nip == pc) |
Andrew Donnellan | 2ffd04d | 2016-11-04 17:20:40 +1100 | [diff] [blame] | 1354 | pr_cont("<%08x> ", instr); |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1355 | else |
Andrew Donnellan | 2ffd04d | 2016-11-04 17:20:40 +1100 | [diff] [blame] | 1356 | pr_cont("%08x ", instr); |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1357 | } |
| 1358 | |
| 1359 | pc += sizeof(int); |
| 1360 | } |
| 1361 | |
Andrew Donnellan | 2ffd04d | 2016-11-04 17:20:40 +1100 | [diff] [blame] | 1362 | pr_cont("\n"); |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1363 | } |
| 1364 | |
Murilo Opsfelder Araujo | 88b0fe1 | 2018-08-01 18:33:19 -0300 | [diff] [blame] | 1365 | void show_user_instructions(struct pt_regs *regs) |
| 1366 | { |
| 1367 | unsigned long pc; |
Christophe Leroy | df13102 | 2018-10-06 16:51:16 +0000 | [diff] [blame] | 1368 | int n = NR_INSN_TO_PRINT; |
Christophe Leroy | fb2d950 | 2018-10-06 16:51:14 +0000 | [diff] [blame] | 1369 | struct seq_buf s; |
| 1370 | char buf[96]; /* enough for 8 times 9 + 2 chars */ |
Murilo Opsfelder Araujo | 88b0fe1 | 2018-08-01 18:33:19 -0300 | [diff] [blame] | 1371 | |
Christophe Leroy | df13102 | 2018-10-06 16:51:16 +0000 | [diff] [blame] | 1372 | pc = regs->nip - (NR_INSN_TO_PRINT * 3 / 4 * sizeof(int)); |
Murilo Opsfelder Araujo | 88b0fe1 | 2018-08-01 18:33:19 -0300 | [diff] [blame] | 1373 | |
Christophe Leroy | fb2d950 | 2018-10-06 16:51:14 +0000 | [diff] [blame] | 1374 | seq_buf_init(&s, buf, sizeof(buf)); |
Murilo Opsfelder Araujo | 88b0fe1 | 2018-08-01 18:33:19 -0300 | [diff] [blame] | 1375 | |
Christophe Leroy | fb2d950 | 2018-10-06 16:51:14 +0000 | [diff] [blame] | 1376 | while (n) { |
| 1377 | int i; |
Murilo Opsfelder Araujo | 88b0fe1 | 2018-08-01 18:33:19 -0300 | [diff] [blame] | 1378 | |
Christophe Leroy | fb2d950 | 2018-10-06 16:51:14 +0000 | [diff] [blame] | 1379 | seq_buf_clear(&s); |
| 1380 | |
| 1381 | for (i = 0; i < 8 && n; i++, n--, pc += sizeof(int)) { |
| 1382 | int instr; |
| 1383 | |
Christoph Hellwig | c0ee37e | 2020-06-17 09:37:54 +0200 | [diff] [blame] | 1384 | if (copy_from_user_nofault(&instr, (void __user *)pc, |
| 1385 | sizeof(instr))) { |
Christophe Leroy | fb2d950 | 2018-10-06 16:51:14 +0000 | [diff] [blame] | 1386 | seq_buf_printf(&s, "XXXXXXXX "); |
| 1387 | continue; |
| 1388 | } |
| 1389 | seq_buf_printf(&s, regs->nip == pc ? "<%08x> " : "%08x ", instr); |
Murilo Opsfelder Araujo | 88b0fe1 | 2018-08-01 18:33:19 -0300 | [diff] [blame] | 1390 | } |
| 1391 | |
Christophe Leroy | fb2d950 | 2018-10-06 16:51:14 +0000 | [diff] [blame] | 1392 | if (!seq_buf_has_overflowed(&s)) |
| 1393 | pr_info("%s[%d]: code: %s\n", current->comm, |
| 1394 | current->pid, s.buffer); |
Murilo Opsfelder Araujo | 88b0fe1 | 2018-08-01 18:33:19 -0300 | [diff] [blame] | 1395 | } |
Murilo Opsfelder Araujo | 88b0fe1 | 2018-08-01 18:33:19 -0300 | [diff] [blame] | 1396 | } |
| 1397 | |
Michael Neuling | 801c0b2 | 2015-11-20 15:15:32 +1100 | [diff] [blame] | 1398 | struct regbit { |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1399 | unsigned long bit; |
| 1400 | const char *name; |
Michael Neuling | 801c0b2 | 2015-11-20 15:15:32 +1100 | [diff] [blame] | 1401 | }; |
| 1402 | |
| 1403 | static struct regbit msr_bits[] = { |
Anton Blanchard | 3bfd0c9c | 2011-11-24 19:35:57 +0000 | [diff] [blame] | 1404 | #if defined(CONFIG_PPC64) && !defined(CONFIG_BOOKE) |
| 1405 | {MSR_SF, "SF"}, |
| 1406 | {MSR_HV, "HV"}, |
| 1407 | #endif |
| 1408 | {MSR_VEC, "VEC"}, |
| 1409 | {MSR_VSX, "VSX"}, |
| 1410 | #ifdef CONFIG_BOOKE |
| 1411 | {MSR_CE, "CE"}, |
| 1412 | #endif |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1413 | {MSR_EE, "EE"}, |
| 1414 | {MSR_PR, "PR"}, |
| 1415 | {MSR_FP, "FP"}, |
| 1416 | {MSR_ME, "ME"}, |
Anton Blanchard | 3bfd0c9c | 2011-11-24 19:35:57 +0000 | [diff] [blame] | 1417 | #ifdef CONFIG_BOOKE |
Kumar Gala | 1b98326 | 2008-11-19 04:39:53 +0000 | [diff] [blame] | 1418 | {MSR_DE, "DE"}, |
Anton Blanchard | 3bfd0c9c | 2011-11-24 19:35:57 +0000 | [diff] [blame] | 1419 | #else |
| 1420 | {MSR_SE, "SE"}, |
| 1421 | {MSR_BE, "BE"}, |
| 1422 | #endif |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1423 | {MSR_IR, "IR"}, |
| 1424 | {MSR_DR, "DR"}, |
Anton Blanchard | 3bfd0c9c | 2011-11-24 19:35:57 +0000 | [diff] [blame] | 1425 | {MSR_PMM, "PMM"}, |
| 1426 | #ifndef CONFIG_BOOKE |
| 1427 | {MSR_RI, "RI"}, |
| 1428 | {MSR_LE, "LE"}, |
| 1429 | #endif |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1430 | {0, NULL} |
| 1431 | }; |
| 1432 | |
Michael Neuling | 801c0b2 | 2015-11-20 15:15:32 +1100 | [diff] [blame] | 1433 | static void print_bits(unsigned long val, struct regbit *bits, const char *sep) |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1434 | { |
Michael Neuling | 801c0b2 | 2015-11-20 15:15:32 +1100 | [diff] [blame] | 1435 | const char *s = ""; |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1436 | |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1437 | for (; bits->bit; ++bits) |
| 1438 | if (val & bits->bit) { |
Michael Ellerman | db5ba5a | 2016-11-02 22:20:47 +1100 | [diff] [blame] | 1439 | pr_cont("%s%s", s, bits->name); |
Michael Neuling | 801c0b2 | 2015-11-20 15:15:32 +1100 | [diff] [blame] | 1440 | s = sep; |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1441 | } |
Michael Neuling | 801c0b2 | 2015-11-20 15:15:32 +1100 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | #ifdef CONFIG_PPC_TRANSACTIONAL_MEM |
| 1445 | static struct regbit msr_tm_bits[] = { |
| 1446 | {MSR_TS_T, "T"}, |
| 1447 | {MSR_TS_S, "S"}, |
| 1448 | {MSR_TM, "E"}, |
| 1449 | {0, NULL} |
| 1450 | }; |
| 1451 | |
| 1452 | static void print_tm_bits(unsigned long val) |
| 1453 | { |
| 1454 | /* |
| 1455 | * This only prints something if at least one of the TM bit is set. |
| 1456 | * Inside the TM[], the output means: |
| 1457 | * E: Enabled (bit 32) |
| 1458 | * S: Suspended (bit 33) |
| 1459 | * T: Transactional (bit 34) |
| 1460 | */ |
| 1461 | if (val & (MSR_TM | MSR_TS_S | MSR_TS_T)) { |
Michael Ellerman | db5ba5a | 2016-11-02 22:20:47 +1100 | [diff] [blame] | 1462 | pr_cont(",TM["); |
Michael Neuling | 801c0b2 | 2015-11-20 15:15:32 +1100 | [diff] [blame] | 1463 | print_bits(val, msr_tm_bits, ""); |
Michael Ellerman | db5ba5a | 2016-11-02 22:20:47 +1100 | [diff] [blame] | 1464 | pr_cont("]"); |
Michael Neuling | 801c0b2 | 2015-11-20 15:15:32 +1100 | [diff] [blame] | 1465 | } |
| 1466 | } |
| 1467 | #else |
| 1468 | static void print_tm_bits(unsigned long val) {} |
| 1469 | #endif |
| 1470 | |
| 1471 | static void print_msr_bits(unsigned long val) |
| 1472 | { |
Michael Ellerman | db5ba5a | 2016-11-02 22:20:47 +1100 | [diff] [blame] | 1473 | pr_cont("<"); |
Michael Neuling | 801c0b2 | 2015-11-20 15:15:32 +1100 | [diff] [blame] | 1474 | print_bits(val, msr_bits, ","); |
| 1475 | print_tm_bits(val); |
Michael Ellerman | db5ba5a | 2016-11-02 22:20:47 +1100 | [diff] [blame] | 1476 | pr_cont(">"); |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | #ifdef CONFIG_PPC64 |
anton@samba.org | f6f7dde | 2007-03-20 20:38:19 -0500 | [diff] [blame] | 1480 | #define REG "%016lx" |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1481 | #define REGS_PER_LINE 4 |
| 1482 | #define LAST_VOLATILE 13 |
| 1483 | #else |
anton@samba.org | f6f7dde | 2007-03-20 20:38:19 -0500 | [diff] [blame] | 1484 | #define REG "%08lx" |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1485 | #define REGS_PER_LINE 8 |
| 1486 | #define LAST_VOLATILE 12 |
| 1487 | #endif |
| 1488 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1489 | void show_regs(struct pt_regs * regs) |
| 1490 | { |
| 1491 | int i, trap; |
| 1492 | |
Tejun Heo | a43cb95 | 2013-04-30 15:27:17 -0700 | [diff] [blame] | 1493 | show_regs_print_info(KERN_DEFAULT); |
| 1494 | |
Michael Ellerman | a603610 | 2017-08-23 23:56:24 +1000 | [diff] [blame] | 1495 | printk("NIP: "REG" LR: "REG" CTR: "REG"\n", |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1496 | regs->nip, regs->link, regs->ctr); |
Michael Ellerman | 182dc9c | 2017-12-18 16:33:36 +1100 | [diff] [blame] | 1497 | printk("REGS: %px TRAP: %04lx %s (%s)\n", |
Serge E. Hallyn | 96b644b | 2006-10-02 02:18:13 -0700 | [diff] [blame] | 1498 | regs, regs->trap, print_tainted(), init_utsname()->release); |
Michael Ellerman | a603610 | 2017-08-23 23:56:24 +1000 | [diff] [blame] | 1499 | printk("MSR: "REG" ", regs->msr); |
Michael Neuling | 801c0b2 | 2015-11-20 15:15:32 +1100 | [diff] [blame] | 1500 | print_msr_bits(regs->msr); |
Michael Ellerman | f6fc73f | 2017-08-23 23:56:23 +1000 | [diff] [blame] | 1501 | pr_cont(" CR: %08lx XER: %08lx\n", regs->ccr, regs->xer); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1502 | trap = TRAP(regs); |
Nicholas Piggin | 912237e | 2020-05-07 22:13:31 +1000 | [diff] [blame] | 1503 | if (!trap_is_syscall(regs) && cpu_has_feature(CPU_FTR_CFAR)) |
Michael Ellerman | 7dae865 | 2016-11-03 20:45:26 +1100 | [diff] [blame] | 1504 | pr_cont("CFAR: "REG" ", regs->orig_gpr3); |
Christophe Leroy | 2ec4299 | 2020-08-17 05:46:43 +0000 | [diff] [blame] | 1505 | if (trap == 0x200 || trap == 0x300 || trap == 0x600) { |
| 1506 | if (IS_ENABLED(CONFIG_4xx) || IS_ENABLED(CONFIG_BOOKE)) |
| 1507 | pr_cont("DEAR: "REG" ESR: "REG" ", regs->dar, regs->dsisr); |
| 1508 | else |
| 1509 | pr_cont("DAR: "REG" DSISR: %08lx ", regs->dar, regs->dsisr); |
| 1510 | } |
| 1511 | |
Anton Blanchard | 9db8bcf | 2013-11-15 15:48:38 +1100 | [diff] [blame] | 1512 | #ifdef CONFIG_PPC64 |
Nicholas Piggin | 3130a7b | 2018-05-10 11:04:24 +1000 | [diff] [blame] | 1513 | pr_cont("IRQMASK: %lx ", regs->softe); |
Anton Blanchard | 9db8bcf | 2013-11-15 15:48:38 +1100 | [diff] [blame] | 1514 | #endif |
| 1515 | #ifdef CONFIG_PPC_TRANSACTIONAL_MEM |
Anton Blanchard | 6d888d1 | 2013-11-18 13:19:17 +1100 | [diff] [blame] | 1516 | if (MSR_TM_ACTIVE(regs->msr)) |
Michael Ellerman | 7dae865 | 2016-11-03 20:45:26 +1100 | [diff] [blame] | 1517 | pr_cont("\nPACATMSCRATCH: %016llx ", get_paca()->tm_scratch); |
Kumar Gala | 1417078 | 2007-07-26 00:46:15 -0500 | [diff] [blame] | 1518 | #endif |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1519 | |
| 1520 | for (i = 0; i < 32; i++) { |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1521 | if ((i % REGS_PER_LINE) == 0) |
Michael Ellerman | 7dae865 | 2016-11-03 20:45:26 +1100 | [diff] [blame] | 1522 | pr_cont("\nGPR%02d: ", i); |
| 1523 | pr_cont(REG " ", regs->gpr[i]); |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1524 | if (i == LAST_VOLATILE && !FULL_REGS(regs)) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1525 | break; |
| 1526 | } |
Michael Ellerman | 7dae865 | 2016-11-03 20:45:26 +1100 | [diff] [blame] | 1527 | pr_cont("\n"); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1528 | /* |
| 1529 | * Lookup NIP late so we have the best change of getting the |
| 1530 | * above info out without failing |
| 1531 | */ |
Christophe Leroy | 8f020c7 | 2020-08-17 05:46:44 +0000 | [diff] [blame] | 1532 | if (IS_ENABLED(CONFIG_KALLSYMS)) { |
| 1533 | printk("NIP ["REG"] %pS\n", regs->nip, (void *)regs->nip); |
| 1534 | printk("LR ["REG"] %pS\n", regs->link, (void *)regs->link); |
| 1535 | } |
Dmitry Safonov | 9cb8f06 | 2020-06-08 21:32:29 -0700 | [diff] [blame] | 1536 | show_stack(current, (unsigned long *) regs->gpr[1], KERN_DEFAULT); |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1537 | if (!user_mode(regs)) |
| 1538 | show_instructions(regs); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1539 | } |
| 1540 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1541 | void flush_thread(void) |
| 1542 | { |
K.Prasad | e0780b7 | 2011-02-10 04:44:35 +0000 | [diff] [blame] | 1543 | #ifdef CONFIG_HAVE_HW_BREAKPOINT |
K.Prasad | 5aae8a5 | 2010-06-15 11:35:19 +0530 | [diff] [blame] | 1544 | flush_ptrace_hw_breakpoint(current); |
K.Prasad | e0780b7 | 2011-02-10 04:44:35 +0000 | [diff] [blame] | 1545 | #else /* CONFIG_HAVE_HW_BREAKPOINT */ |
Dave Kleikamp | 3bffb65 | 2010-02-08 11:51:18 +0000 | [diff] [blame] | 1546 | set_debug_reg_defaults(¤t->thread); |
K.Prasad | e0780b7 | 2011-02-10 04:44:35 +0000 | [diff] [blame] | 1547 | #endif /* CONFIG_HAVE_HW_BREAKPOINT */ |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1548 | } |
| 1549 | |
Nicholas Piggin | 425d331 | 2018-09-15 01:30:55 +1000 | [diff] [blame] | 1550 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 1551 | void arch_setup_new_exec(void) |
| 1552 | { |
| 1553 | if (radix_enabled()) |
| 1554 | return; |
| 1555 | hash__setup_new_exec(); |
| 1556 | } |
| 1557 | #endif |
| 1558 | |
Sukadev Bhattiprolu | ec233ed | 2017-11-07 18:23:53 -0800 | [diff] [blame] | 1559 | #ifdef CONFIG_PPC64 |
Alastair D'Silva | 71cc64a | 2018-05-11 16:12:59 +1000 | [diff] [blame] | 1560 | /** |
| 1561 | * Assign a TIDR (thread ID) for task @t and set it in the thread |
Sukadev Bhattiprolu | ec233ed | 2017-11-07 18:23:53 -0800 | [diff] [blame] | 1562 | * structure. For now, we only support setting TIDR for 'current' task. |
Alastair D'Silva | 71cc64a | 2018-05-11 16:12:59 +1000 | [diff] [blame] | 1563 | * |
| 1564 | * Since the TID value is a truncated form of it PID, it is possible |
| 1565 | * (but unlikely) for 2 threads to have the same TID. In the unlikely event |
| 1566 | * that 2 threads share the same TID and are waiting, one of the following |
| 1567 | * cases will happen: |
| 1568 | * |
| 1569 | * 1. The correct thread is running, the wrong thread is not |
| 1570 | * In this situation, the correct thread is woken and proceeds to pass it's |
| 1571 | * condition check. |
| 1572 | * |
| 1573 | * 2. Neither threads are running |
| 1574 | * In this situation, neither thread will be woken. When scheduled, the waiting |
| 1575 | * threads will execute either a wait, which will return immediately, followed |
| 1576 | * by a condition check, which will pass for the correct thread and fail |
| 1577 | * for the wrong thread, or they will execute the condition check immediately. |
| 1578 | * |
| 1579 | * 3. The wrong thread is running, the correct thread is not |
| 1580 | * The wrong thread will be woken, but will fail it's condition check and |
| 1581 | * re-execute wait. The correct thread, when scheduled, will execute either |
| 1582 | * it's condition check (which will pass), or wait, which returns immediately |
| 1583 | * when called the first time after the thread is scheduled, followed by it's |
| 1584 | * condition check (which will pass). |
| 1585 | * |
| 1586 | * 4. Both threads are running |
| 1587 | * Both threads will be woken. The wrong thread will fail it's condition check |
| 1588 | * and execute another wait, while the correct thread will pass it's condition |
| 1589 | * check. |
| 1590 | * |
| 1591 | * @t: the task to set the thread ID for |
Sukadev Bhattiprolu | ec233ed | 2017-11-07 18:23:53 -0800 | [diff] [blame] | 1592 | */ |
| 1593 | int set_thread_tidr(struct task_struct *t) |
| 1594 | { |
Alastair D'Silva | 3449f19 | 2018-05-11 16:12:58 +1000 | [diff] [blame] | 1595 | if (!cpu_has_feature(CPU_FTR_P9_TIDR)) |
Sukadev Bhattiprolu | ec233ed | 2017-11-07 18:23:53 -0800 | [diff] [blame] | 1596 | return -EINVAL; |
| 1597 | |
| 1598 | if (t != current) |
| 1599 | return -EINVAL; |
| 1600 | |
Vaibhav Jain | 7e4d423 | 2017-11-24 14:03:38 +0530 | [diff] [blame] | 1601 | if (t->thread.tidr) |
| 1602 | return 0; |
| 1603 | |
Alastair D'Silva | 71cc64a | 2018-05-11 16:12:59 +1000 | [diff] [blame] | 1604 | t->thread.tidr = (u16)task_pid_nr(t); |
Sukadev Bhattiprolu | ec233ed | 2017-11-07 18:23:53 -0800 | [diff] [blame] | 1605 | mtspr(SPRN_TIDR, t->thread.tidr); |
| 1606 | |
| 1607 | return 0; |
| 1608 | } |
Christophe Lombard | b1db551 | 2018-01-11 09:55:25 +0100 | [diff] [blame] | 1609 | EXPORT_SYMBOL_GPL(set_thread_tidr); |
Sukadev Bhattiprolu | ec233ed | 2017-11-07 18:23:53 -0800 | [diff] [blame] | 1610 | |
| 1611 | #endif /* CONFIG_PPC64 */ |
| 1612 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1613 | void |
| 1614 | release_thread(struct task_struct *t) |
| 1615 | { |
| 1616 | } |
| 1617 | |
| 1618 | /* |
Suresh Siddha | 55ccf3f | 2012-05-16 15:03:51 -0700 | [diff] [blame] | 1619 | * this gets called so that we can store coprocessor state into memory and |
| 1620 | * copy the current task into the new thread. |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1621 | */ |
Suresh Siddha | 55ccf3f | 2012-05-16 15:03:51 -0700 | [diff] [blame] | 1622 | int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1623 | { |
Anton Blanchard | 579e633 | 2015-10-29 11:44:09 +1100 | [diff] [blame] | 1624 | flush_all_to_thread(src); |
Michael Neuling | 621b506 | 2014-03-03 14:21:40 +1100 | [diff] [blame] | 1625 | /* |
| 1626 | * Flush TM state out so we can copy it. __switch_to_tm() does this |
| 1627 | * flush but it removes the checkpointed state from the current CPU and |
| 1628 | * transitions the CPU out of TM mode. Hence we need to call |
| 1629 | * tm_recheckpoint_new_task() (on the same task) to restore the |
| 1630 | * checkpointed state back and the TM mode. |
Cyril Bur | 5d176f7 | 2016-09-14 18:02:16 +1000 | [diff] [blame] | 1631 | * |
| 1632 | * Can't pass dst because it isn't ready. Doesn't matter, passing |
| 1633 | * dst is only important for __switch_to() |
Michael Neuling | 621b506 | 2014-03-03 14:21:40 +1100 | [diff] [blame] | 1634 | */ |
Cyril Bur | dc31066 | 2016-09-23 16:18:24 +1000 | [diff] [blame] | 1635 | __switch_to_tm(src, src); |
Michael Ellerman | 330a1eb | 2013-06-28 18:15:16 +1000 | [diff] [blame] | 1636 | |
Suresh Siddha | 55ccf3f | 2012-05-16 15:03:51 -0700 | [diff] [blame] | 1637 | *dst = *src; |
Michael Ellerman | 330a1eb | 2013-06-28 18:15:16 +1000 | [diff] [blame] | 1638 | |
| 1639 | clear_task_ebb(dst); |
| 1640 | |
Suresh Siddha | 55ccf3f | 2012-05-16 15:03:51 -0700 | [diff] [blame] | 1641 | return 0; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1642 | } |
| 1643 | |
Michael Ellerman | cec1548 | 2014-07-10 12:29:21 +1000 | [diff] [blame] | 1644 | static void setup_ksp_vsid(struct task_struct *p, unsigned long sp) |
| 1645 | { |
Michael Ellerman | 4e00374 | 2017-10-19 15:08:43 +1100 | [diff] [blame] | 1646 | #ifdef CONFIG_PPC_BOOK3S_64 |
Michael Ellerman | cec1548 | 2014-07-10 12:29:21 +1000 | [diff] [blame] | 1647 | unsigned long sp_vsid; |
| 1648 | unsigned long llp = mmu_psize_defs[mmu_linear_psize].sllp; |
| 1649 | |
Aneesh Kumar K.V | caca285 | 2016-04-29 23:26:07 +1000 | [diff] [blame] | 1650 | if (radix_enabled()) |
| 1651 | return; |
| 1652 | |
Michael Ellerman | cec1548 | 2014-07-10 12:29:21 +1000 | [diff] [blame] | 1653 | if (mmu_has_feature(MMU_FTR_1T_SEGMENT)) |
| 1654 | sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_1T) |
| 1655 | << SLB_VSID_SHIFT_1T; |
| 1656 | else |
| 1657 | sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_256M) |
| 1658 | << SLB_VSID_SHIFT; |
| 1659 | sp_vsid |= SLB_VSID_KERNEL | llp; |
| 1660 | p->thread.ksp_vsid = sp_vsid; |
| 1661 | #endif |
| 1662 | } |
| 1663 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1664 | /* |
| 1665 | * Copy a thread.. |
| 1666 | */ |
Alexey Kardashevskiy | efcac65 | 2011-03-02 15:18:48 +0000 | [diff] [blame] | 1667 | |
Alex Dowad | 6eca893 | 2015-03-13 20:14:46 +0200 | [diff] [blame] | 1668 | /* |
| 1669 | * Copy architecture-specific thread state |
| 1670 | */ |
Christian Brauner | 714acdb | 2020-06-11 11:04:15 +0200 | [diff] [blame] | 1671 | int copy_thread(unsigned long clone_flags, unsigned long usp, |
Nicholas Piggin | facd04a | 2019-08-27 13:30:06 +1000 | [diff] [blame] | 1672 | unsigned long kthread_arg, struct task_struct *p, |
| 1673 | unsigned long tls) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1674 | { |
| 1675 | struct pt_regs *childregs, *kregs; |
| 1676 | extern void ret_from_fork(void); |
Nicholas Piggin | 7fa95f9 | 2020-06-11 18:12:03 +1000 | [diff] [blame] | 1677 | extern void ret_from_fork_scv(void); |
Al Viro | 58254e1 | 2012-09-12 18:32:42 -0400 | [diff] [blame] | 1678 | extern void ret_from_kernel_thread(void); |
| 1679 | void (*f)(void); |
Al Viro | 0cec6fd | 2006-01-12 01:06:02 -0800 | [diff] [blame] | 1680 | unsigned long sp = (unsigned long)task_stack_page(p) + THREAD_SIZE; |
Michael Ellerman | 5d31a96 | 2016-03-24 22:04:04 +1100 | [diff] [blame] | 1681 | struct thread_info *ti = task_thread_info(p); |
Ravi Bangoria | 6b424ef | 2020-05-14 16:47:35 +0530 | [diff] [blame] | 1682 | #ifdef CONFIG_HAVE_HW_BREAKPOINT |
| 1683 | int i; |
| 1684 | #endif |
Michael Ellerman | 5d31a96 | 2016-03-24 22:04:04 +1100 | [diff] [blame] | 1685 | |
Christophe Leroy | ed1cd6d | 2019-01-31 10:08:58 +0000 | [diff] [blame] | 1686 | klp_init_thread_info(p); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1687 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1688 | /* Copy registers */ |
| 1689 | sp -= sizeof(struct pt_regs); |
| 1690 | childregs = (struct pt_regs *) sp; |
Al Viro | ab75819 | 2012-10-21 22:33:39 -0400 | [diff] [blame] | 1691 | if (unlikely(p->flags & PF_KTHREAD)) { |
Alex Dowad | 6eca893 | 2015-03-13 20:14:46 +0200 | [diff] [blame] | 1692 | /* kernel thread */ |
Al Viro | 58254e1 | 2012-09-12 18:32:42 -0400 | [diff] [blame] | 1693 | memset(childregs, 0, sizeof(struct pt_regs)); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1694 | childregs->gpr[1] = sp + sizeof(struct pt_regs); |
Anton Blanchard | 7cedd60 | 2014-02-04 16:08:51 +1100 | [diff] [blame] | 1695 | /* function */ |
| 1696 | if (usp) |
| 1697 | childregs->gpr[14] = ppc_function_entry((void *)usp); |
Al Viro | 58254e1 | 2012-09-12 18:32:42 -0400 | [diff] [blame] | 1698 | #ifdef CONFIG_PPC64 |
Al Viro | b5e2fc1 | 2006-01-12 01:06:01 -0800 | [diff] [blame] | 1699 | clear_tsk_thread_flag(p, TIF_32BIT); |
Madhavan Srinivasan | c2e480b | 2017-12-20 09:25:42 +0530 | [diff] [blame] | 1700 | childregs->softe = IRQS_ENABLED; |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1701 | #endif |
Alex Dowad | 6eca893 | 2015-03-13 20:14:46 +0200 | [diff] [blame] | 1702 | childregs->gpr[15] = kthread_arg; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1703 | p->thread.regs = NULL; /* no user register state */ |
Al Viro | 138d1ce | 2012-10-11 08:41:43 -0400 | [diff] [blame] | 1704 | ti->flags |= _TIF_RESTOREALL; |
Al Viro | 58254e1 | 2012-09-12 18:32:42 -0400 | [diff] [blame] | 1705 | f = ret_from_kernel_thread; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1706 | } else { |
Alex Dowad | 6eca893 | 2015-03-13 20:14:46 +0200 | [diff] [blame] | 1707 | /* user thread */ |
Al Viro | afa86fc | 2012-10-22 22:51:14 -0400 | [diff] [blame] | 1708 | struct pt_regs *regs = current_pt_regs(); |
Al Viro | 58254e1 | 2012-09-12 18:32:42 -0400 | [diff] [blame] | 1709 | CHECK_FULL_REGS(regs); |
| 1710 | *childregs = *regs; |
Al Viro | ea516b1 | 2012-10-21 22:28:43 -0400 | [diff] [blame] | 1711 | if (usp) |
| 1712 | childregs->gpr[1] = usp; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1713 | p->thread.regs = childregs; |
Nicholas Piggin | 7fa95f9 | 2020-06-11 18:12:03 +1000 | [diff] [blame] | 1714 | /* 64s sets this in ret_from_fork */ |
| 1715 | if (!IS_ENABLED(CONFIG_PPC_BOOK3S_64)) |
| 1716 | childregs->gpr[3] = 0; /* Result from fork() */ |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1717 | if (clone_flags & CLONE_SETTLS) { |
Denis Kirjanov | 9904b00 | 2010-07-29 22:04:39 +0000 | [diff] [blame] | 1718 | if (!is_32bit_task()) |
Nicholas Piggin | facd04a | 2019-08-27 13:30:06 +1000 | [diff] [blame] | 1719 | childregs->gpr[13] = tls; |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1720 | else |
Nicholas Piggin | facd04a | 2019-08-27 13:30:06 +1000 | [diff] [blame] | 1721 | childregs->gpr[2] = tls; |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1722 | } |
Al Viro | 58254e1 | 2012-09-12 18:32:42 -0400 | [diff] [blame] | 1723 | |
Nicholas Piggin | 7fa95f9 | 2020-06-11 18:12:03 +1000 | [diff] [blame] | 1724 | if (trap_is_scv(regs)) |
| 1725 | f = ret_from_fork_scv; |
| 1726 | else |
| 1727 | f = ret_from_fork; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1728 | } |
Cyril Bur | d272f66 | 2016-02-29 17:53:46 +1100 | [diff] [blame] | 1729 | childregs->msr &= ~(MSR_FP|MSR_VEC|MSR_VSX); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1730 | sp -= STACK_FRAME_OVERHEAD; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1731 | |
| 1732 | /* |
| 1733 | * The way this works is that at some point in the future |
| 1734 | * some task will call _switch to switch to the new task. |
| 1735 | * That will pop off the stack frame created below and start |
| 1736 | * the new task running at ret_from_fork. The new task will |
| 1737 | * do some house keeping and then return from the fork or clone |
| 1738 | * system call, using the stack frame created above. |
| 1739 | */ |
Li Zhong | af945cf | 2013-05-06 22:44:41 +0000 | [diff] [blame] | 1740 | ((unsigned long *)sp)[0] = 0; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1741 | sp -= sizeof(struct pt_regs); |
| 1742 | kregs = (struct pt_regs *) sp; |
| 1743 | sp -= STACK_FRAME_OVERHEAD; |
| 1744 | p->thread.ksp = sp; |
Benjamin Herrenschmidt | cbc9565 | 2013-09-24 15:17:21 +1000 | [diff] [blame] | 1745 | #ifdef CONFIG_PPC32 |
Christophe Leroy | a7916a1 | 2019-01-31 10:09:00 +0000 | [diff] [blame] | 1746 | p->thread.ksp_limit = (unsigned long)end_of_stack(p); |
Benjamin Herrenschmidt | cbc9565 | 2013-09-24 15:17:21 +1000 | [diff] [blame] | 1747 | #endif |
Oleg Nesterov | 28d170ab | 2013-04-21 06:47:59 +0000 | [diff] [blame] | 1748 | #ifdef CONFIG_HAVE_HW_BREAKPOINT |
Ravi Bangoria | 6b424ef | 2020-05-14 16:47:35 +0530 | [diff] [blame] | 1749 | for (i = 0; i < nr_wp_slots(); i++) |
| 1750 | p->thread.ptrace_bps[i] = NULL; |
Oleg Nesterov | 28d170ab | 2013-04-21 06:47:59 +0000 | [diff] [blame] | 1751 | #endif |
| 1752 | |
Paul Mackerras | 1846196 | 2013-09-10 20:21:10 +1000 | [diff] [blame] | 1753 | p->thread.fp_save_area = NULL; |
| 1754 | #ifdef CONFIG_ALTIVEC |
| 1755 | p->thread.vr_save_area = NULL; |
| 1756 | #endif |
| 1757 | |
Michael Ellerman | cec1548 | 2014-07-10 12:29:21 +1000 | [diff] [blame] | 1758 | setup_ksp_vsid(p, sp); |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1759 | |
Alexey Kardashevskiy | efcac65 | 2011-03-02 15:18:48 +0000 | [diff] [blame] | 1760 | #ifdef CONFIG_PPC64 |
| 1761 | if (cpu_has_feature(CPU_FTR_DSCR)) { |
Anton Blanchard | 1021cb2 | 2012-09-03 16:49:47 +0000 | [diff] [blame] | 1762 | p->thread.dscr_inherit = current->thread.dscr_inherit; |
Anton Blanchard | db1231dc | 2015-12-09 20:11:47 +1100 | [diff] [blame] | 1763 | p->thread.dscr = mfspr(SPRN_DSCR); |
Alexey Kardashevskiy | efcac65 | 2011-03-02 15:18:48 +0000 | [diff] [blame] | 1764 | } |
Haren Myneni | 9277924 | 2012-12-06 21:49:56 +0000 | [diff] [blame] | 1765 | if (cpu_has_feature(CPU_FTR_HAS_PPR)) |
Nicholas Piggin | 4c2de74 | 2018-10-13 00:15:16 +1100 | [diff] [blame] | 1766 | childregs->ppr = DEFAULT_PPR; |
Sukadev Bhattiprolu | ec233ed | 2017-11-07 18:23:53 -0800 | [diff] [blame] | 1767 | |
| 1768 | p->thread.tidr = 0; |
Alexey Kardashevskiy | efcac65 | 2011-03-02 15:18:48 +0000 | [diff] [blame] | 1769 | #endif |
Anton Blanchard | 7cedd60 | 2014-02-04 16:08:51 +1100 | [diff] [blame] | 1770 | kregs->nip = ppc_function_entry(f); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1771 | return 0; |
| 1772 | } |
| 1773 | |
Nicholas Piggin | 5434ae7 | 2018-09-15 01:30:56 +1000 | [diff] [blame] | 1774 | void preload_new_slb_context(unsigned long start, unsigned long sp); |
| 1775 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1776 | /* |
| 1777 | * Set up a thread for executing a new program |
| 1778 | */ |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1779 | void start_thread(struct pt_regs *regs, unsigned long start, unsigned long sp) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1780 | { |
Michael Ellerman | 90eac72 | 2005-10-21 16:01:33 +1000 | [diff] [blame] | 1781 | #ifdef CONFIG_PPC64 |
| 1782 | unsigned long load_addr = regs->gpr[2]; /* saved by ELF_PLAT_INIT */ |
Nicholas Piggin | 5434ae7 | 2018-09-15 01:30:56 +1000 | [diff] [blame] | 1783 | |
Christophe Leroy | bfac279 | 2020-08-17 05:46:42 +0000 | [diff] [blame] | 1784 | if (IS_ENABLED(CONFIG_PPC_BOOK3S_64) && !radix_enabled()) |
Aneesh Kumar K.V | f89bd8b | 2019-04-09 09:33:28 +0530 | [diff] [blame] | 1785 | preload_new_slb_context(start, sp); |
Nicholas Piggin | 5434ae7 | 2018-09-15 01:30:56 +1000 | [diff] [blame] | 1786 | #endif |
Michael Ellerman | 90eac72 | 2005-10-21 16:01:33 +1000 | [diff] [blame] | 1787 | |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1788 | /* |
| 1789 | * If we exec out of a kernel thread then thread.regs will not be |
| 1790 | * set. Do it now. |
| 1791 | */ |
| 1792 | if (!current->thread.regs) { |
Al Viro | 0cec6fd | 2006-01-12 01:06:02 -0800 | [diff] [blame] | 1793 | struct pt_regs *regs = task_stack_page(current) + THREAD_SIZE; |
| 1794 | current->thread.regs = regs - 1; |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1795 | } |
| 1796 | |
Cyril Bur | 8e96a87 | 2016-06-17 14:58:34 +1000 | [diff] [blame] | 1797 | #ifdef CONFIG_PPC_TRANSACTIONAL_MEM |
| 1798 | /* |
| 1799 | * Clear any transactional state, we're exec()ing. The cause is |
| 1800 | * not important as there will never be a recheckpoint so it's not |
| 1801 | * user visible. |
| 1802 | */ |
| 1803 | if (MSR_TM_SUSPENDED(mfmsr())) |
| 1804 | tm_reclaim_current(0); |
| 1805 | #endif |
| 1806 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1807 | memset(regs->gpr, 0, sizeof(regs->gpr)); |
| 1808 | regs->ctr = 0; |
| 1809 | regs->link = 0; |
| 1810 | regs->xer = 0; |
| 1811 | regs->ccr = 0; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1812 | regs->gpr[1] = sp; |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1813 | |
Roland McGrath | 474f819 | 2007-09-24 16:52:44 -0700 | [diff] [blame] | 1814 | /* |
| 1815 | * We have just cleared all the nonvolatile GPRs, so make |
| 1816 | * FULL_REGS(regs) return true. This is necessary to allow |
| 1817 | * ptrace to examine the thread immediately after exec. |
| 1818 | */ |
Nicholas Piggin | feb9df3 | 2020-05-07 22:13:29 +1000 | [diff] [blame] | 1819 | SET_FULL_REGS(regs); |
Roland McGrath | 474f819 | 2007-09-24 16:52:44 -0700 | [diff] [blame] | 1820 | |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1821 | #ifdef CONFIG_PPC32 |
| 1822 | regs->mq = 0; |
| 1823 | regs->nip = start; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1824 | regs->msr = MSR_USER; |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1825 | #else |
Denis Kirjanov | 9904b00 | 2010-07-29 22:04:39 +0000 | [diff] [blame] | 1826 | if (!is_32bit_task()) { |
Rusty Russell | 94af3ab | 2013-11-20 22:15:02 +1100 | [diff] [blame] | 1827 | unsigned long entry; |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1828 | |
Rusty Russell | 94af3ab | 2013-11-20 22:15:02 +1100 | [diff] [blame] | 1829 | if (is_elf2_task()) { |
| 1830 | /* Look ma, no function descriptors! */ |
| 1831 | entry = start; |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1832 | |
Rusty Russell | 94af3ab | 2013-11-20 22:15:02 +1100 | [diff] [blame] | 1833 | /* |
| 1834 | * Ulrich says: |
| 1835 | * The latest iteration of the ABI requires that when |
| 1836 | * calling a function (at its global entry point), |
| 1837 | * the caller must ensure r12 holds the entry point |
| 1838 | * address (so that the function can quickly |
| 1839 | * establish addressability). |
| 1840 | */ |
| 1841 | regs->gpr[12] = start; |
| 1842 | /* Make sure that's restored on entry to userspace. */ |
| 1843 | set_thread_flag(TIF_RESTOREALL); |
| 1844 | } else { |
| 1845 | unsigned long toc; |
| 1846 | |
| 1847 | /* start is a relocated pointer to the function |
| 1848 | * descriptor for the elf _start routine. The first |
| 1849 | * entry in the function descriptor is the entry |
| 1850 | * address of _start and the second entry is the TOC |
| 1851 | * value we need to use. |
| 1852 | */ |
| 1853 | __get_user(entry, (unsigned long __user *)start); |
| 1854 | __get_user(toc, (unsigned long __user *)start+1); |
| 1855 | |
| 1856 | /* Check whether the e_entry function descriptor entries |
| 1857 | * need to be relocated before we can use them. |
| 1858 | */ |
| 1859 | if (load_addr != 0) { |
| 1860 | entry += load_addr; |
| 1861 | toc += load_addr; |
| 1862 | } |
| 1863 | regs->gpr[2] = toc; |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1864 | } |
| 1865 | regs->nip = entry; |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1866 | regs->msr = MSR_USER64; |
Stephen Rothwell | d4bf9a7 | 2005-10-13 13:40:54 +1000 | [diff] [blame] | 1867 | } else { |
| 1868 | regs->nip = start; |
| 1869 | regs->gpr[2] = 0; |
| 1870 | regs->msr = MSR_USER32; |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1871 | } |
| 1872 | #endif |
Michael Neuling | ce48b21 | 2008-06-25 14:07:18 +1000 | [diff] [blame] | 1873 | #ifdef CONFIG_VSX |
| 1874 | current->thread.used_vsr = 0; |
| 1875 | #endif |
Nicholas Piggin | 5434ae7 | 2018-09-15 01:30:56 +1000 | [diff] [blame] | 1876 | current->thread.load_slb = 0; |
Breno Leitao | 1195892 | 2017-06-02 18:43:30 -0300 | [diff] [blame] | 1877 | current->thread.load_fp = 0; |
Paul Mackerras | de79f7b | 2013-09-10 20:20:42 +1000 | [diff] [blame] | 1878 | memset(¤t->thread.fp_state, 0, sizeof(current->thread.fp_state)); |
Paul Mackerras | 1846196 | 2013-09-10 20:21:10 +1000 | [diff] [blame] | 1879 | current->thread.fp_save_area = NULL; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1880 | #ifdef CONFIG_ALTIVEC |
Paul Mackerras | de79f7b | 2013-09-10 20:20:42 +1000 | [diff] [blame] | 1881 | memset(¤t->thread.vr_state, 0, sizeof(current->thread.vr_state)); |
| 1882 | current->thread.vr_state.vscr.u[3] = 0x00010000; /* Java mode disabled */ |
Paul Mackerras | 1846196 | 2013-09-10 20:21:10 +1000 | [diff] [blame] | 1883 | current->thread.vr_save_area = NULL; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1884 | current->thread.vrsave = 0; |
| 1885 | current->thread.used_vr = 0; |
Breno Leitao | 1195892 | 2017-06-02 18:43:30 -0300 | [diff] [blame] | 1886 | current->thread.load_vec = 0; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1887 | #endif /* CONFIG_ALTIVEC */ |
| 1888 | #ifdef CONFIG_SPE |
| 1889 | memset(current->thread.evr, 0, sizeof(current->thread.evr)); |
| 1890 | current->thread.acc = 0; |
| 1891 | current->thread.spefscr = 0; |
| 1892 | current->thread.used_spe = 0; |
| 1893 | #endif /* CONFIG_SPE */ |
Michael Neuling | bc2a940 | 2013-02-13 16:21:40 +0000 | [diff] [blame] | 1894 | #ifdef CONFIG_PPC_TRANSACTIONAL_MEM |
Michael Neuling | bc2a940 | 2013-02-13 16:21:40 +0000 | [diff] [blame] | 1895 | current->thread.tm_tfhar = 0; |
| 1896 | current->thread.tm_texasr = 0; |
| 1897 | current->thread.tm_tfiar = 0; |
Breno Leitao | 7f22ced | 2017-06-05 11:40:59 -0300 | [diff] [blame] | 1898 | current->thread.load_tm = 0; |
Michael Neuling | bc2a940 | 2013-02-13 16:21:40 +0000 | [diff] [blame] | 1899 | #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */ |
Ram Pai | 06bb53b | 2018-01-18 17:50:31 -0800 | [diff] [blame] | 1900 | |
| 1901 | thread_pkey_regs_init(¤t->thread); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1902 | } |
Anton Blanchard | e1802b0 | 2014-08-20 08:00:02 +1000 | [diff] [blame] | 1903 | EXPORT_SYMBOL(start_thread); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1904 | |
| 1905 | #define PR_FP_ALL_EXCEPT (PR_FP_EXC_DIV | PR_FP_EXC_OVF | PR_FP_EXC_UND \ |
| 1906 | | PR_FP_EXC_RES | PR_FP_EXC_INV) |
| 1907 | |
| 1908 | int set_fpexc_mode(struct task_struct *tsk, unsigned int val) |
| 1909 | { |
| 1910 | struct pt_regs *regs = tsk->thread.regs; |
| 1911 | |
| 1912 | /* This is a bit hairy. If we are an SPE enabled processor |
| 1913 | * (have embedded fp) we store the IEEE exception enable flags in |
| 1914 | * fpexc_mode. fpexc_mode is also used for setting FP exception |
| 1915 | * mode (asyn, precise, disabled) for 'Classic' FP. */ |
| 1916 | if (val & PR_FP_EXC_SW_ENABLE) { |
| 1917 | #ifdef CONFIG_SPE |
Kumar Gala | 5e14d21 | 2007-09-13 01:44:20 -0500 | [diff] [blame] | 1918 | if (cpu_has_feature(CPU_FTR_SPE)) { |
Joseph Myers | 640e922 | 2013-12-10 23:07:45 +0000 | [diff] [blame] | 1919 | /* |
| 1920 | * When the sticky exception bits are set |
| 1921 | * directly by userspace, it must call prctl |
| 1922 | * with PR_GET_FPEXC (with PR_FP_EXC_SW_ENABLE |
| 1923 | * in the existing prctl settings) or |
| 1924 | * PR_SET_FPEXC (with PR_FP_EXC_SW_ENABLE in |
| 1925 | * the bits being set). <fenv.h> functions |
| 1926 | * saving and restoring the whole |
| 1927 | * floating-point environment need to do so |
| 1928 | * anyway to restore the prctl settings from |
| 1929 | * the saved environment. |
| 1930 | */ |
| 1931 | tsk->thread.spefscr_last = mfspr(SPRN_SPEFSCR); |
Kumar Gala | 5e14d21 | 2007-09-13 01:44:20 -0500 | [diff] [blame] | 1932 | tsk->thread.fpexc_mode = val & |
| 1933 | (PR_FP_EXC_SW_ENABLE | PR_FP_ALL_EXCEPT); |
| 1934 | return 0; |
| 1935 | } else { |
| 1936 | return -EINVAL; |
| 1937 | } |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1938 | #else |
| 1939 | return -EINVAL; |
| 1940 | #endif |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1941 | } |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 1942 | |
| 1943 | /* on a CONFIG_SPE this does not hurt us. The bits that |
| 1944 | * __pack_fe01 use do not overlap with bits used for |
| 1945 | * PR_FP_EXC_SW_ENABLE. Additionally, the MSR[FE0,FE1] bits |
| 1946 | * on CONFIG_SPE implementations are reserved so writing to |
| 1947 | * them does not change anything */ |
| 1948 | if (val > PR_FP_EXC_PRECISE) |
| 1949 | return -EINVAL; |
| 1950 | tsk->thread.fpexc_mode = __pack_fe01(val); |
| 1951 | if (regs != NULL && (regs->msr & MSR_FP) != 0) |
| 1952 | regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1)) |
| 1953 | | tsk->thread.fpexc_mode; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1954 | return 0; |
| 1955 | } |
| 1956 | |
| 1957 | int get_fpexc_mode(struct task_struct *tsk, unsigned long adr) |
| 1958 | { |
| 1959 | unsigned int val; |
| 1960 | |
| 1961 | if (tsk->thread.fpexc_mode & PR_FP_EXC_SW_ENABLE) |
| 1962 | #ifdef CONFIG_SPE |
Joseph Myers | 640e922 | 2013-12-10 23:07:45 +0000 | [diff] [blame] | 1963 | if (cpu_has_feature(CPU_FTR_SPE)) { |
| 1964 | /* |
| 1965 | * When the sticky exception bits are set |
| 1966 | * directly by userspace, it must call prctl |
| 1967 | * with PR_GET_FPEXC (with PR_FP_EXC_SW_ENABLE |
| 1968 | * in the existing prctl settings) or |
| 1969 | * PR_SET_FPEXC (with PR_FP_EXC_SW_ENABLE in |
| 1970 | * the bits being set). <fenv.h> functions |
| 1971 | * saving and restoring the whole |
| 1972 | * floating-point environment need to do so |
| 1973 | * anyway to restore the prctl settings from |
| 1974 | * the saved environment. |
| 1975 | */ |
| 1976 | tsk->thread.spefscr_last = mfspr(SPRN_SPEFSCR); |
Kumar Gala | 5e14d21 | 2007-09-13 01:44:20 -0500 | [diff] [blame] | 1977 | val = tsk->thread.fpexc_mode; |
Joseph Myers | 640e922 | 2013-12-10 23:07:45 +0000 | [diff] [blame] | 1978 | } else |
Kumar Gala | 5e14d21 | 2007-09-13 01:44:20 -0500 | [diff] [blame] | 1979 | return -EINVAL; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1980 | #else |
| 1981 | return -EINVAL; |
| 1982 | #endif |
| 1983 | else |
| 1984 | val = __unpack_fe01(tsk->thread.fpexc_mode); |
| 1985 | return put_user(val, (unsigned int __user *) adr); |
| 1986 | } |
| 1987 | |
Paul Mackerras | fab5db9 | 2006-06-07 16:14:40 +1000 | [diff] [blame] | 1988 | int set_endian(struct task_struct *tsk, unsigned int val) |
| 1989 | { |
| 1990 | struct pt_regs *regs = tsk->thread.regs; |
| 1991 | |
| 1992 | if ((val == PR_ENDIAN_LITTLE && !cpu_has_feature(CPU_FTR_REAL_LE)) || |
| 1993 | (val == PR_ENDIAN_PPC_LITTLE && !cpu_has_feature(CPU_FTR_PPC_LE))) |
| 1994 | return -EINVAL; |
| 1995 | |
| 1996 | if (regs == NULL) |
| 1997 | return -EINVAL; |
| 1998 | |
| 1999 | if (val == PR_ENDIAN_BIG) |
| 2000 | regs->msr &= ~MSR_LE; |
| 2001 | else if (val == PR_ENDIAN_LITTLE || val == PR_ENDIAN_PPC_LITTLE) |
| 2002 | regs->msr |= MSR_LE; |
| 2003 | else |
| 2004 | return -EINVAL; |
| 2005 | |
| 2006 | return 0; |
| 2007 | } |
| 2008 | |
| 2009 | int get_endian(struct task_struct *tsk, unsigned long adr) |
| 2010 | { |
| 2011 | struct pt_regs *regs = tsk->thread.regs; |
| 2012 | unsigned int val; |
| 2013 | |
| 2014 | if (!cpu_has_feature(CPU_FTR_PPC_LE) && |
| 2015 | !cpu_has_feature(CPU_FTR_REAL_LE)) |
| 2016 | return -EINVAL; |
| 2017 | |
| 2018 | if (regs == NULL) |
| 2019 | return -EINVAL; |
| 2020 | |
| 2021 | if (regs->msr & MSR_LE) { |
| 2022 | if (cpu_has_feature(CPU_FTR_REAL_LE)) |
| 2023 | val = PR_ENDIAN_LITTLE; |
| 2024 | else |
| 2025 | val = PR_ENDIAN_PPC_LITTLE; |
| 2026 | } else |
| 2027 | val = PR_ENDIAN_BIG; |
| 2028 | |
| 2029 | return put_user(val, (unsigned int __user *)adr); |
| 2030 | } |
| 2031 | |
Paul Mackerras | e9370ae | 2006-06-07 16:15:39 +1000 | [diff] [blame] | 2032 | int set_unalign_ctl(struct task_struct *tsk, unsigned int val) |
| 2033 | { |
| 2034 | tsk->thread.align_ctl = val; |
| 2035 | return 0; |
| 2036 | } |
| 2037 | |
| 2038 | int get_unalign_ctl(struct task_struct *tsk, unsigned long adr) |
| 2039 | { |
| 2040 | return put_user(tsk->thread.align_ctl, (unsigned int __user *)adr); |
| 2041 | } |
| 2042 | |
Paul Mackerras | bb72c48 | 2007-02-19 11:42:42 +1100 | [diff] [blame] | 2043 | static inline int valid_irq_stack(unsigned long sp, struct task_struct *p, |
| 2044 | unsigned long nbytes) |
| 2045 | { |
| 2046 | unsigned long stack_page; |
| 2047 | unsigned long cpu = task_cpu(p); |
| 2048 | |
Christophe Leroy | a7916a1 | 2019-01-31 10:09:00 +0000 | [diff] [blame] | 2049 | stack_page = (unsigned long)hardirq_ctx[cpu]; |
| 2050 | if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes) |
| 2051 | return 1; |
Paul Mackerras | bb72c48 | 2007-02-19 11:42:42 +1100 | [diff] [blame] | 2052 | |
Christophe Leroy | a7916a1 | 2019-01-31 10:09:00 +0000 | [diff] [blame] | 2053 | stack_page = (unsigned long)softirq_ctx[cpu]; |
| 2054 | if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes) |
| 2055 | return 1; |
| 2056 | |
Paul Mackerras | bb72c48 | 2007-02-19 11:42:42 +1100 | [diff] [blame] | 2057 | return 0; |
| 2058 | } |
| 2059 | |
Nicholas Piggin | a2e3668 | 2020-03-25 20:41:44 +1000 | [diff] [blame] | 2060 | static inline int valid_emergency_stack(unsigned long sp, struct task_struct *p, |
| 2061 | unsigned long nbytes) |
| 2062 | { |
| 2063 | #ifdef CONFIG_PPC64 |
| 2064 | unsigned long stack_page; |
| 2065 | unsigned long cpu = task_cpu(p); |
| 2066 | |
| 2067 | stack_page = (unsigned long)paca_ptrs[cpu]->emergency_sp - THREAD_SIZE; |
| 2068 | if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes) |
| 2069 | return 1; |
| 2070 | |
| 2071 | # ifdef CONFIG_PPC_BOOK3S_64 |
| 2072 | stack_page = (unsigned long)paca_ptrs[cpu]->nmi_emergency_sp - THREAD_SIZE; |
| 2073 | if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes) |
| 2074 | return 1; |
| 2075 | |
| 2076 | stack_page = (unsigned long)paca_ptrs[cpu]->mc_emergency_sp - THREAD_SIZE; |
| 2077 | if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes) |
| 2078 | return 1; |
| 2079 | # endif |
| 2080 | #endif |
| 2081 | |
| 2082 | return 0; |
| 2083 | } |
| 2084 | |
| 2085 | |
Anton Blanchard | 2f25194 | 2006-03-27 11:46:18 +1100 | [diff] [blame] | 2086 | int validate_sp(unsigned long sp, struct task_struct *p, |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 2087 | unsigned long nbytes) |
| 2088 | { |
Al Viro | 0cec6fd | 2006-01-12 01:06:02 -0800 | [diff] [blame] | 2089 | unsigned long stack_page = (unsigned long)task_stack_page(p); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 2090 | |
Christophe Leroy | a7916a1 | 2019-01-31 10:09:00 +0000 | [diff] [blame] | 2091 | if (sp < THREAD_SIZE) |
| 2092 | return 0; |
| 2093 | |
| 2094 | if (sp >= stack_page && sp <= stack_page + THREAD_SIZE - nbytes) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 2095 | return 1; |
| 2096 | |
Nicholas Piggin | a2e3668 | 2020-03-25 20:41:44 +1000 | [diff] [blame] | 2097 | if (valid_irq_stack(sp, p, nbytes)) |
| 2098 | return 1; |
| 2099 | |
| 2100 | return valid_emergency_stack(sp, p, nbytes); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 2101 | } |
| 2102 | |
Anton Blanchard | 2f25194 | 2006-03-27 11:46:18 +1100 | [diff] [blame] | 2103 | EXPORT_SYMBOL(validate_sp); |
| 2104 | |
Christophe Leroy | 018cce3 | 2019-01-31 10:08:52 +0000 | [diff] [blame] | 2105 | static unsigned long __get_wchan(struct task_struct *p) |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 2106 | { |
| 2107 | unsigned long ip, sp; |
| 2108 | int count = 0; |
| 2109 | |
| 2110 | if (!p || p == current || p->state == TASK_RUNNING) |
| 2111 | return 0; |
| 2112 | |
| 2113 | sp = p->thread.ksp; |
Benjamin Herrenschmidt | ec2b36b | 2008-04-17 14:34:59 +1000 | [diff] [blame] | 2114 | if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD)) |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 2115 | return 0; |
| 2116 | |
| 2117 | do { |
| 2118 | sp = *(unsigned long *)sp; |
Kautuk Consul | 4ca360f | 2016-04-19 15:48:21 +0530 | [diff] [blame] | 2119 | if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD) || |
| 2120 | p->state == TASK_RUNNING) |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 2121 | return 0; |
| 2122 | if (count > 0) { |
Benjamin Herrenschmidt | ec2b36b | 2008-04-17 14:34:59 +1000 | [diff] [blame] | 2123 | ip = ((unsigned long *)sp)[STACK_FRAME_LR_SAVE]; |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 2124 | if (!in_sched_functions(ip)) |
| 2125 | return ip; |
| 2126 | } |
| 2127 | } while (count++ < 16); |
| 2128 | return 0; |
| 2129 | } |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 2130 | |
Christophe Leroy | 018cce3 | 2019-01-31 10:08:52 +0000 | [diff] [blame] | 2131 | unsigned long get_wchan(struct task_struct *p) |
| 2132 | { |
| 2133 | unsigned long ret; |
| 2134 | |
| 2135 | if (!try_get_task_stack(p)) |
| 2136 | return 0; |
| 2137 | |
| 2138 | ret = __get_wchan(p); |
| 2139 | |
| 2140 | put_task_stack(p); |
| 2141 | |
| 2142 | return ret; |
| 2143 | } |
| 2144 | |
Johannes Berg | c4d04be | 2008-11-20 03:24:07 +0000 | [diff] [blame] | 2145 | static int kstack_depth_to_print = CONFIG_PRINT_STACK_DEPTH; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 2146 | |
Dmitry Safonov | 9cb8f06 | 2020-06-08 21:32:29 -0700 | [diff] [blame] | 2147 | void show_stack(struct task_struct *tsk, unsigned long *stack, |
| 2148 | const char *loglvl) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 2149 | { |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 2150 | unsigned long sp, ip, lr, newsp; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 2151 | int count = 0; |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 2152 | int firstframe = 1; |
Naveen N. Rao | 7c1bb6b | 2019-09-05 23:50:30 +0530 | [diff] [blame] | 2153 | unsigned long ret_addr; |
| 2154 | int ftrace_idx = 0; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 2155 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 2156 | if (tsk == NULL) |
| 2157 | tsk = current; |
Christophe Leroy | 018cce3 | 2019-01-31 10:08:52 +0000 | [diff] [blame] | 2158 | |
| 2159 | if (!try_get_task_stack(tsk)) |
| 2160 | return; |
| 2161 | |
| 2162 | sp = (unsigned long) stack; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 2163 | if (sp == 0) { |
| 2164 | if (tsk == current) |
Michael Ellerman | 3d13e83 | 2020-02-20 22:51:37 +1100 | [diff] [blame] | 2165 | sp = current_stack_frame(); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 2166 | else |
| 2167 | sp = tsk->thread.ksp; |
| 2168 | } |
| 2169 | |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 2170 | lr = 0; |
Dmitry Safonov | b9677a8 | 2020-06-08 21:31:14 -0700 | [diff] [blame] | 2171 | printk("%sCall Trace:\n", loglvl); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 2172 | do { |
Benjamin Herrenschmidt | ec2b36b | 2008-04-17 14:34:59 +1000 | [diff] [blame] | 2173 | if (!validate_sp(sp, tsk, STACK_FRAME_OVERHEAD)) |
Christophe Leroy | 018cce3 | 2019-01-31 10:08:52 +0000 | [diff] [blame] | 2174 | break; |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 2175 | |
| 2176 | stack = (unsigned long *) sp; |
| 2177 | newsp = stack[0]; |
Benjamin Herrenschmidt | ec2b36b | 2008-04-17 14:34:59 +1000 | [diff] [blame] | 2178 | ip = stack[STACK_FRAME_LR_SAVE]; |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 2179 | if (!firstframe || ip != lr) { |
Dmitry Safonov | b9677a8 | 2020-06-08 21:31:14 -0700 | [diff] [blame] | 2180 | printk("%s["REG"] ["REG"] %pS", |
| 2181 | loglvl, sp, ip, (void *)ip); |
Naveen N. Rao | 7c1bb6b | 2019-09-05 23:50:30 +0530 | [diff] [blame] | 2182 | ret_addr = ftrace_graph_ret_addr(current, |
| 2183 | &ftrace_idx, ip, stack); |
| 2184 | if (ret_addr != ip) |
| 2185 | pr_cont(" (%pS)", (void *)ret_addr); |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 2186 | if (firstframe) |
Michael Ellerman | 9a1f490 | 2016-11-02 22:20:46 +1100 | [diff] [blame] | 2187 | pr_cont(" (unreliable)"); |
| 2188 | pr_cont("\n"); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 2189 | } |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 2190 | firstframe = 0; |
| 2191 | |
| 2192 | /* |
| 2193 | * See if this is an exception frame. |
| 2194 | * We look for the "regshere" marker in the current frame. |
| 2195 | */ |
Benjamin Herrenschmidt | ec2b36b | 2008-04-17 14:34:59 +1000 | [diff] [blame] | 2196 | if (validate_sp(sp, tsk, STACK_INT_FRAME_SIZE) |
| 2197 | && stack[STACK_FRAME_MARKER] == STACK_FRAME_REGS_MARKER) { |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 2198 | struct pt_regs *regs = (struct pt_regs *) |
| 2199 | (sp + STACK_FRAME_OVERHEAD); |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 2200 | lr = regs->link; |
Dmitry Safonov | b9677a8 | 2020-06-08 21:31:14 -0700 | [diff] [blame] | 2201 | printk("%s--- interrupt: %lx at %pS\n LR = %pS\n", |
| 2202 | loglvl, regs->trap, |
| 2203 | (void *)regs->nip, (void *)lr); |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 2204 | firstframe = 1; |
| 2205 | } |
| 2206 | |
| 2207 | sp = newsp; |
| 2208 | } while (count++ < kstack_depth_to_print); |
Christophe Leroy | 018cce3 | 2019-01-31 10:08:52 +0000 | [diff] [blame] | 2209 | |
| 2210 | put_task_stack(tsk); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 2211 | } |
Paul Mackerras | 06d67d5 | 2005-10-10 22:29:05 +1000 | [diff] [blame] | 2212 | |
Anton Blanchard | cb2c9b2 | 2006-02-13 14:48:35 +1100 | [diff] [blame] | 2213 | #ifdef CONFIG_PPC64 |
Benjamin Herrenschmidt | fe1952f | 2012-03-01 12:45:27 +1100 | [diff] [blame] | 2214 | /* Called with hard IRQs off */ |
Michael Ellerman | 0e37739 | 2013-06-13 21:04:56 +1000 | [diff] [blame] | 2215 | void notrace __ppc64_runlatch_on(void) |
Anton Blanchard | cb2c9b2 | 2006-02-13 14:48:35 +1100 | [diff] [blame] | 2216 | { |
Benjamin Herrenschmidt | fe1952f | 2012-03-01 12:45:27 +1100 | [diff] [blame] | 2217 | struct thread_info *ti = current_thread_info(); |
Anton Blanchard | cb2c9b2 | 2006-02-13 14:48:35 +1100 | [diff] [blame] | 2218 | |
Nicholas Piggin | d1d0d5f | 2017-08-12 02:39:07 +1000 | [diff] [blame] | 2219 | if (cpu_has_feature(CPU_FTR_ARCH_206)) { |
| 2220 | /* |
| 2221 | * Least significant bit (RUN) is the only writable bit of |
| 2222 | * the CTRL register, so we can avoid mfspr. 2.06 is not the |
| 2223 | * earliest ISA where this is the case, but it's convenient. |
| 2224 | */ |
| 2225 | mtspr(SPRN_CTRLT, CTRL_RUNLATCH); |
| 2226 | } else { |
| 2227 | unsigned long ctrl; |
| 2228 | |
| 2229 | /* |
| 2230 | * Some architectures (e.g., Cell) have writable fields other |
| 2231 | * than RUN, so do the read-modify-write. |
| 2232 | */ |
| 2233 | ctrl = mfspr(SPRN_CTRLF); |
| 2234 | ctrl |= CTRL_RUNLATCH; |
| 2235 | mtspr(SPRN_CTRLT, ctrl); |
| 2236 | } |
Anton Blanchard | cb2c9b2 | 2006-02-13 14:48:35 +1100 | [diff] [blame] | 2237 | |
Benjamin Herrenschmidt | fae2e0f | 2012-04-11 10:42:15 +1000 | [diff] [blame] | 2238 | ti->local_flags |= _TLF_RUNLATCH; |
Anton Blanchard | cb2c9b2 | 2006-02-13 14:48:35 +1100 | [diff] [blame] | 2239 | } |
| 2240 | |
Benjamin Herrenschmidt | fe1952f | 2012-03-01 12:45:27 +1100 | [diff] [blame] | 2241 | /* Called with hard IRQs off */ |
Michael Ellerman | 0e37739 | 2013-06-13 21:04:56 +1000 | [diff] [blame] | 2242 | void notrace __ppc64_runlatch_off(void) |
Anton Blanchard | cb2c9b2 | 2006-02-13 14:48:35 +1100 | [diff] [blame] | 2243 | { |
Benjamin Herrenschmidt | fe1952f | 2012-03-01 12:45:27 +1100 | [diff] [blame] | 2244 | struct thread_info *ti = current_thread_info(); |
Anton Blanchard | cb2c9b2 | 2006-02-13 14:48:35 +1100 | [diff] [blame] | 2245 | |
Benjamin Herrenschmidt | fae2e0f | 2012-04-11 10:42:15 +1000 | [diff] [blame] | 2246 | ti->local_flags &= ~_TLF_RUNLATCH; |
Anton Blanchard | cb2c9b2 | 2006-02-13 14:48:35 +1100 | [diff] [blame] | 2247 | |
Nicholas Piggin | d1d0d5f | 2017-08-12 02:39:07 +1000 | [diff] [blame] | 2248 | if (cpu_has_feature(CPU_FTR_ARCH_206)) { |
| 2249 | mtspr(SPRN_CTRLT, 0); |
| 2250 | } else { |
| 2251 | unsigned long ctrl; |
| 2252 | |
| 2253 | ctrl = mfspr(SPRN_CTRLF); |
| 2254 | ctrl &= ~CTRL_RUNLATCH; |
| 2255 | mtspr(SPRN_CTRLT, ctrl); |
| 2256 | } |
Anton Blanchard | cb2c9b2 | 2006-02-13 14:48:35 +1100 | [diff] [blame] | 2257 | } |
Benjamin Herrenschmidt | fe1952f | 2012-03-01 12:45:27 +1100 | [diff] [blame] | 2258 | #endif /* CONFIG_PPC64 */ |
Benjamin Herrenschmidt | f6a6168 | 2008-04-18 16:56:17 +1000 | [diff] [blame] | 2259 | |
Anton Blanchard | d839088 | 2009-02-22 01:50:03 +0000 | [diff] [blame] | 2260 | unsigned long arch_align_stack(unsigned long sp) |
| 2261 | { |
| 2262 | if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space) |
| 2263 | sp -= get_random_int() & ~PAGE_MASK; |
| 2264 | return sp & ~0xf; |
| 2265 | } |
Anton Blanchard | 912f9ee | 2009-02-22 01:50:04 +0000 | [diff] [blame] | 2266 | |
| 2267 | static inline unsigned long brk_rnd(void) |
| 2268 | { |
| 2269 | unsigned long rnd = 0; |
| 2270 | |
| 2271 | /* 8MB for 32bit, 1GB for 64bit */ |
| 2272 | if (is_32bit_task()) |
Daniel Cashman | 5ef11c3 | 2016-02-26 15:19:37 -0800 | [diff] [blame] | 2273 | rnd = (get_random_long() % (1UL<<(23-PAGE_SHIFT))); |
Anton Blanchard | 912f9ee | 2009-02-22 01:50:04 +0000 | [diff] [blame] | 2274 | else |
Daniel Cashman | 5ef11c3 | 2016-02-26 15:19:37 -0800 | [diff] [blame] | 2275 | rnd = (get_random_long() % (1UL<<(30-PAGE_SHIFT))); |
Anton Blanchard | 912f9ee | 2009-02-22 01:50:04 +0000 | [diff] [blame] | 2276 | |
| 2277 | return rnd << PAGE_SHIFT; |
| 2278 | } |
| 2279 | |
| 2280 | unsigned long arch_randomize_brk(struct mm_struct *mm) |
| 2281 | { |
Anton Blanchard | 8bbde7a | 2009-09-21 16:52:35 +0000 | [diff] [blame] | 2282 | unsigned long base = mm->brk; |
| 2283 | unsigned long ret; |
| 2284 | |
Michael Ellerman | 4e00374 | 2017-10-19 15:08:43 +1100 | [diff] [blame] | 2285 | #ifdef CONFIG_PPC_BOOK3S_64 |
Anton Blanchard | 8bbde7a | 2009-09-21 16:52:35 +0000 | [diff] [blame] | 2286 | /* |
| 2287 | * If we are using 1TB segments and we are allowed to randomise |
| 2288 | * the heap, we can put it above 1TB so it is backed by a 1TB |
| 2289 | * segment. Otherwise the heap will be in the bottom 1TB |
| 2290 | * which always uses 256MB segments and this may result in a |
Aneesh Kumar K.V | caca285 | 2016-04-29 23:26:07 +1000 | [diff] [blame] | 2291 | * performance penalty. We don't need to worry about radix. For |
| 2292 | * radix, mmu_highuser_ssize remains unchanged from 256MB. |
Anton Blanchard | 8bbde7a | 2009-09-21 16:52:35 +0000 | [diff] [blame] | 2293 | */ |
| 2294 | if (!is_32bit_task() && (mmu_highuser_ssize == MMU_SEGSIZE_1T)) |
| 2295 | base = max_t(unsigned long, mm->brk, 1UL << SID_SHIFT_1T); |
| 2296 | #endif |
| 2297 | |
| 2298 | ret = PAGE_ALIGN(base + brk_rnd()); |
Anton Blanchard | 912f9ee | 2009-02-22 01:50:04 +0000 | [diff] [blame] | 2299 | |
| 2300 | if (ret < mm->brk) |
| 2301 | return mm->brk; |
| 2302 | |
| 2303 | return ret; |
| 2304 | } |
Anton Blanchard | 501cb16 | 2009-02-22 01:50:07 +0000 | [diff] [blame] | 2305 | |