blob: 2f182bdf024f03044ca3ea541adba311f2cafe8a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * Copyright (C) 1994, 95, 96, 99, 2001 Ralf Baechle
7 * Copyright (C) 1994, 1995, 1996 Paul M. Antoine.
8 * Copyright (C) 1999 Silicon Graphics, Inc.
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +01009 * Copyright (C) 2007 Maciej W. Rozycki
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11#ifndef _ASM_STACKFRAME_H
12#define _ASM_STACKFRAME_H
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/threads.h>
15
16#include <asm/asm.h>
Ralf Baechle41c594a2006-04-05 09:45:45 +010017#include <asm/asmmacro.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/mipsregs.h>
Sam Ravnborg048eb582005-09-09 22:32:31 +020019#include <asm/asm-offsets.h>
Jayachandran Cc2377a42013-08-11 17:10:16 +053020#include <asm/thread_info.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Ralf Baechleb6336482014-05-23 16:29:44 +020022#if defined(CONFIG_CPU_R3000) || defined(CONFIG_CPU_TX39XX)
Maciej W. Rozyckifbf6ede2007-05-21 13:47:22 +010023#define STATMASK 0x3f
24#else
25#define STATMASK 0x1f
26#endif
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 .macro SAVE_AT
29 .set push
30 .set noat
31 LONG_S $1, PT_R1(sp)
32 .set pop
33 .endm
34
35 .macro SAVE_TEMP
Franck Bui-Huu9693a852007-02-02 17:41:47 +010036#ifdef CONFIG_CPU_HAS_SMARTMIPS
37 mflhxu v1
38 LONG_S v1, PT_LO(sp)
39 mflhxu v1
40 LONG_S v1, PT_HI(sp)
41 mflhxu v1
42 LONG_S v1, PT_ACX(sp)
Leonid Yegoshin6a0e9862014-10-27 11:37:47 +000043#elif !defined(CONFIG_CPU_MIPSR6)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 mfhi v1
Franck Bui-Huu9693a852007-02-02 17:41:47 +010045#endif
Ralf Baechle875d43e2005-09-03 15:56:16 -070046#ifdef CONFIG_32BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 LONG_S $8, PT_R8(sp)
48 LONG_S $9, PT_R9(sp)
49#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 LONG_S $10, PT_R10(sp)
51 LONG_S $11, PT_R11(sp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 LONG_S $12, PT_R12(sp)
Leonid Yegoshin6a0e9862014-10-27 11:37:47 +000053#if !defined(CONFIG_CPU_HAS_SMARTMIPS) && !defined(CONFIG_CPU_MIPSR6)
David Daney362e6962009-06-26 09:01:43 -070054 LONG_S v1, PT_HI(sp)
55 mflo v1
56#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 LONG_S $13, PT_R13(sp)
58 LONG_S $14, PT_R14(sp)
59 LONG_S $15, PT_R15(sp)
60 LONG_S $24, PT_R24(sp)
Leonid Yegoshin6a0e9862014-10-27 11:37:47 +000061#if !defined(CONFIG_CPU_HAS_SMARTMIPS) && !defined(CONFIG_CPU_MIPSR6)
David Daney362e6962009-06-26 09:01:43 -070062 LONG_S v1, PT_LO(sp)
63#endif
David Daney8dfdd022013-06-21 21:14:53 +000064#ifdef CONFIG_CPU_CAVIUM_OCTEON
65 /*
66 * The Octeon multiplier state is affected by general
67 * multiply instructions. It must be saved before and
68 * kernel code might corrupt it
69 */
70 jal octeon_mult_save
71#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 .endm
73
74 .macro SAVE_STATIC
75 LONG_S $16, PT_R16(sp)
76 LONG_S $17, PT_R17(sp)
77 LONG_S $18, PT_R18(sp)
78 LONG_S $19, PT_R19(sp)
79 LONG_S $20, PT_R20(sp)
80 LONG_S $21, PT_R21(sp)
81 LONG_S $22, PT_R22(sp)
82 LONG_S $23, PT_R23(sp)
83 LONG_S $30, PT_R30(sp)
84 .endm
85
86#ifdef CONFIG_SMP
87 .macro get_saved_sp /* SMP variation */
Jayachandran Cc2377a42013-08-11 17:10:16 +053088 ASM_CPUID_MFC0 k0, ASM_SMP_CPUID_REG
Franck Bui-Huu054c51b2007-02-15 14:21:36 +010089#if defined(CONFIG_32BIT) || defined(KBUILD_64BIT_SYM32)
90 lui k1, %hi(kernelsp)
91#else
Atsushi Nemoto9b95e622006-10-10 22:46:52 +090092 lui k1, %highest(kernelsp)
93 daddiu k1, %higher(kernelsp)
94 dsll k1, 16
95 daddiu k1, %hi(kernelsp)
96 dsll k1, 16
Atsushi Nemoto9b95e622006-10-10 22:46:52 +090097#endif
Jayachandran Cc2377a42013-08-11 17:10:16 +053098 LONG_SRL k0, SMP_CPUID_PTRSHIFT
Atsushi Nemoto9b95e622006-10-10 22:46:52 +090099 LONG_ADDU k1, k0
Ralf Baechle85b6e812005-02-13 00:32:43 +0000100 LONG_L k1, %lo(kernelsp)(k1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 .endm
102
103 .macro set_saved_sp stackp temp temp2
Jayachandran Cc2377a42013-08-11 17:10:16 +0530104 ASM_CPUID_MFC0 \temp, ASM_SMP_CPUID_REG
105 LONG_SRL \temp, SMP_CPUID_PTRSHIFT
Thiemo Seufer9556ac22005-07-08 08:03:48 +0000106 LONG_S \stackp, kernelsp(\temp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 .endm
Jayachandran Cc2377a42013-08-11 17:10:16 +0530108#else /* !CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 .macro get_saved_sp /* Uniprocessor variation */
Wu Zhangjinb197b622010-04-10 20:07:01 +0800110#ifdef CONFIG_CPU_JUMP_WORKAROUNDS
Wu Zhangjinf1df3232010-03-13 12:34:15 +0800111 /*
112 * Clear BTB (branch target buffer), forbid RAS (return address
113 * stack) to workaround the Out-of-order Issue in Loongson2F
114 * via its diagnostic register.
115 */
116 move k0, ra
117 jal 1f
118 nop
1191: jal 1f
120 nop
1211: jal 1f
122 nop
1231: jal 1f
124 nop
1251: move ra, k0
126 li k0, 3
127 mtc0 k0, $22
Steven J. Hill2a0b24f2013-03-25 12:15:55 -0500128#endif /* CONFIG_CPU_JUMP_WORKAROUNDS */
Franck Bui-Huu054c51b2007-02-15 14:21:36 +0100129#if defined(CONFIG_32BIT) || defined(KBUILD_64BIT_SYM32)
130 lui k1, %hi(kernelsp)
131#else
Thiemo Seufer9556ac22005-07-08 08:03:48 +0000132 lui k1, %highest(kernelsp)
133 daddiu k1, %higher(kernelsp)
134 dsll k1, k1, 16
135 daddiu k1, %hi(kernelsp)
136 dsll k1, k1, 16
Thiemo Seufer9556ac22005-07-08 08:03:48 +0000137#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 LONG_L k1, %lo(kernelsp)(k1)
139 .endm
140
141 .macro set_saved_sp stackp temp temp2
142 LONG_S \stackp, kernelsp
143 .endm
144#endif
145
146 .macro SAVE_SOME
147 .set push
148 .set noat
149 .set reorder
150 mfc0 k0, CP0_STATUS
151 sll k0, 3 /* extract cu0 bit */
152 .set noreorder
153 bltz k0, 8f
154 move k1, sp
James Hogan3aff47c2015-07-31 16:29:38 +0100155#ifdef CONFIG_EVA
156 /*
157 * Flush interAptiv's Return Prediction Stack (RPS) by writing
158 * EntryHi. Toggling Config7.RPS is slower and less portable.
159 *
160 * The RPS isn't automatically flushed when exceptions are
161 * taken, which can result in kernel mode speculative accesses
162 * to user addresses if the RPS mispredicts. That's harmless
163 * when user and kernel share the same address space, but with
164 * EVA the same user segments may be unmapped to kernel mode,
165 * even containing sensitive MMIO regions or invalid memory.
166 *
167 * This can happen when the kernel sets the return address to
168 * ret_from_* and jr's to the exception handler, which looks
169 * more like a tail call than a function call. If nested calls
170 * don't evict the last user address in the RPS, it will
171 * mispredict the return and fetch from a user controlled
172 * address into the icache.
173 *
174 * More recent EVA-capable cores with MAAR to restrict
175 * speculative accesses aren't affected.
176 */
177 MFC0 k0, CP0_ENTRYHI
178 MTC0 k0, CP0_ENTRYHI
179#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 .set reorder
181 /* Called from user mode, new stack. */
182 get_saved_sp
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100183#ifndef CONFIG_CPU_DADDI_WORKAROUNDS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848: move k0, sp
185 PTR_SUBU sp, k1, PT_SIZE
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100186#else
187 .set at=k0
1888: PTR_SUBU k1, PT_SIZE
189 .set noat
190 move k0, sp
191 move sp, k1
192#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 LONG_S k0, PT_R29(sp)
194 LONG_S $3, PT_R3(sp)
Ralf Baechle41c594a2006-04-05 09:45:45 +0100195 /*
196 * You might think that you don't need to save $0,
197 * but the FPU emulator and gdb remote debug stub
198 * need it to operate correctly
199 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 LONG_S $0, PT_R0(sp)
201 mfc0 v1, CP0_STATUS
202 LONG_S $2, PT_R2(sp)
Steven J. Hill2a0b24f2013-03-25 12:15:55 -0500203 LONG_S v1, PT_STATUS(sp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 LONG_S $4, PT_R4(sp)
David Daney362e6962009-06-26 09:01:43 -0700205 mfc0 v1, CP0_CAUSE
Steven J. Hill2a0b24f2013-03-25 12:15:55 -0500206 LONG_S $5, PT_R5(sp)
David Daney362e6962009-06-26 09:01:43 -0700207 LONG_S v1, PT_CAUSE(sp)
Steven J. Hill2a0b24f2013-03-25 12:15:55 -0500208 LONG_S $6, PT_R6(sp)
David Daney362e6962009-06-26 09:01:43 -0700209 MFC0 v1, CP0_EPC
Steven J. Hill2a0b24f2013-03-25 12:15:55 -0500210 LONG_S $7, PT_R7(sp)
Ralf Baechle875d43e2005-09-03 15:56:16 -0700211#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 LONG_S $8, PT_R8(sp)
213 LONG_S $9, PT_R9(sp)
214#endif
Steven J. Hill2a0b24f2013-03-25 12:15:55 -0500215 LONG_S v1, PT_EPC(sp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 LONG_S $25, PT_R25(sp)
217 LONG_S $28, PT_R28(sp)
218 LONG_S $31, PT_R31(sp)
Matt Redfearn510d8632016-12-19 14:20:58 +0000219
220 /* Set thread_info if we're coming from user mode */
221 mfc0 k0, CP0_STATUS
222 sll k0, 3 /* extract cu0 bit */
223 bltz k0, 9f
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 ori $28, sp, _THREAD_MASK
226 xori $28, _THREAD_MASK
David Daney2a219b02008-12-11 15:33:33 -0800227#ifdef CONFIG_CPU_CAVIUM_OCTEON
David Daney8dfdd022013-06-21 21:14:53 +0000228 .set mips64
229 pref 0, 0($28) /* Prefetch the current pointer */
David Daney2a219b02008-12-11 15:33:33 -0800230#endif
Matt Redfearn510d8632016-12-19 14:20:58 +00002319:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 .set pop
233 .endm
234
235 .macro SAVE_ALL
236 SAVE_SOME
237 SAVE_AT
238 SAVE_TEMP
239 SAVE_STATIC
240 .endm
241
242 .macro RESTORE_AT
243 .set push
244 .set noat
245 LONG_L $1, PT_R1(sp)
246 .set pop
247 .endm
248
249 .macro RESTORE_TEMP
David Daney8dfdd022013-06-21 21:14:53 +0000250#ifdef CONFIG_CPU_CAVIUM_OCTEON
251 /* Restore the Octeon multiplier state */
252 jal octeon_mult_restore
253#endif
Franck Bui-Huu9693a852007-02-02 17:41:47 +0100254#ifdef CONFIG_CPU_HAS_SMARTMIPS
255 LONG_L $24, PT_ACX(sp)
256 mtlhx $24
257 LONG_L $24, PT_HI(sp)
258 mtlhx $24
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 LONG_L $24, PT_LO(sp)
Franck Bui-Huu9693a852007-02-02 17:41:47 +0100260 mtlhx $24
Leonid Yegoshin6a0e9862014-10-27 11:37:47 +0000261#elif !defined(CONFIG_CPU_MIPSR6)
Franck Bui-Huu9693a852007-02-02 17:41:47 +0100262 LONG_L $24, PT_LO(sp)
263 mtlo $24
264 LONG_L $24, PT_HI(sp)
265 mthi $24
266#endif
Ralf Baechle875d43e2005-09-03 15:56:16 -0700267#ifdef CONFIG_32BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 LONG_L $8, PT_R8(sp)
269 LONG_L $9, PT_R9(sp)
270#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 LONG_L $10, PT_R10(sp)
272 LONG_L $11, PT_R11(sp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 LONG_L $12, PT_R12(sp)
274 LONG_L $13, PT_R13(sp)
275 LONG_L $14, PT_R14(sp)
276 LONG_L $15, PT_R15(sp)
277 LONG_L $24, PT_R24(sp)
278 .endm
279
280 .macro RESTORE_STATIC
281 LONG_L $16, PT_R16(sp)
282 LONG_L $17, PT_R17(sp)
283 LONG_L $18, PT_R18(sp)
284 LONG_L $19, PT_R19(sp)
285 LONG_L $20, PT_R20(sp)
286 LONG_L $21, PT_R21(sp)
287 LONG_L $22, PT_R22(sp)
288 LONG_L $23, PT_R23(sp)
289 LONG_L $30, PT_R30(sp)
290 .endm
291
292#if defined(CONFIG_CPU_R3000) || defined(CONFIG_CPU_TX39XX)
293
294 .macro RESTORE_SOME
295 .set push
296 .set reorder
297 .set noat
298 mfc0 a0, CP0_STATUS
James Hogan00fe56d2016-02-01 13:50:37 +0000299 li v1, ST0_CU1 | ST0_IM
Maciej W. Rozyckifbf6ede2007-05-21 13:47:22 +0100300 ori a0, STATMASK
301 xori a0, STATMASK
302 mtc0 a0, CP0_STATUS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 and a0, v1
304 LONG_L v0, PT_STATUS(sp)
305 nor v1, $0, v1
306 and v0, v1
307 or v0, a0
308 mtc0 v0, CP0_STATUS
309 LONG_L $31, PT_R31(sp)
310 LONG_L $28, PT_R28(sp)
311 LONG_L $25, PT_R25(sp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 LONG_L $7, PT_R7(sp)
313 LONG_L $6, PT_R6(sp)
314 LONG_L $5, PT_R5(sp)
315 LONG_L $4, PT_R4(sp)
316 LONG_L $3, PT_R3(sp)
317 LONG_L $2, PT_R2(sp)
318 .set pop
319 .endm
320
321 .macro RESTORE_SP_AND_RET
322 .set push
323 .set noreorder
324 LONG_L k0, PT_EPC(sp)
325 LONG_L sp, PT_R29(sp)
326 jr k0
327 rfe
328 .set pop
329 .endm
330
331#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 .macro RESTORE_SOME
333 .set push
334 .set reorder
335 .set noat
336 mfc0 a0, CP0_STATUS
Ralf Baechle41c594a2006-04-05 09:45:45 +0100337 ori a0, STATMASK
338 xori a0, STATMASK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 mtc0 a0, CP0_STATUS
James Hogan00fe56d2016-02-01 13:50:37 +0000340 li v1, ST0_CU1 | ST0_FR | ST0_IM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 and a0, v1
342 LONG_L v0, PT_STATUS(sp)
343 nor v1, $0, v1
344 and v0, v1
345 or v0, a0
346 mtc0 v0, CP0_STATUS
347 LONG_L v1, PT_EPC(sp)
348 MTC0 v1, CP0_EPC
349 LONG_L $31, PT_R31(sp)
350 LONG_L $28, PT_R28(sp)
351 LONG_L $25, PT_R25(sp)
Ralf Baechle875d43e2005-09-03 15:56:16 -0700352#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 LONG_L $8, PT_R8(sp)
354 LONG_L $9, PT_R9(sp)
355#endif
356 LONG_L $7, PT_R7(sp)
357 LONG_L $6, PT_R6(sp)
358 LONG_L $5, PT_R5(sp)
359 LONG_L $4, PT_R4(sp)
360 LONG_L $3, PT_R3(sp)
361 LONG_L $2, PT_R2(sp)
362 .set pop
363 .endm
364
365 .macro RESTORE_SP_AND_RET
366 LONG_L sp, PT_R29(sp)
Ralf Baechlea809d462014-03-30 13:20:10 +0200367 .set arch=r4000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 eret
369 .set mips0
370 .endm
371
372#endif
373
374 .macro RESTORE_SP
375 LONG_L sp, PT_R29(sp)
376 .endm
377
378 .macro RESTORE_ALL
379 RESTORE_TEMP
380 RESTORE_STATIC
381 RESTORE_AT
382 RESTORE_SOME
383 RESTORE_SP
384 .endm
385
386 .macro RESTORE_ALL_AND_RET
387 RESTORE_TEMP
388 RESTORE_STATIC
389 RESTORE_AT
390 RESTORE_SOME
391 RESTORE_SP_AND_RET
392 .endm
393
394/*
395 * Move to kernel mode and disable interrupts.
396 * Set cp0 enable bit as sign that we're running on the kernel stack
397 */
398 .macro CLI
399 mfc0 t0, CP0_STATUS
Maciej W. Rozyckifbf6ede2007-05-21 13:47:22 +0100400 li t1, ST0_CU0 | STATMASK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 or t0, t1
Maciej W. Rozyckifbf6ede2007-05-21 13:47:22 +0100402 xori t0, STATMASK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 mtc0 t0, CP0_STATUS
404 irq_disable_hazard
405 .endm
406
407/*
408 * Move to kernel mode and enable interrupts.
409 * Set cp0 enable bit as sign that we're running on the kernel stack
410 */
411 .macro STI
412 mfc0 t0, CP0_STATUS
Maciej W. Rozyckifbf6ede2007-05-21 13:47:22 +0100413 li t1, ST0_CU0 | STATMASK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 or t0, t1
Maciej W. Rozyckifbf6ede2007-05-21 13:47:22 +0100415 xori t0, STATMASK & ~1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 mtc0 t0, CP0_STATUS
417 irq_enable_hazard
418 .endm
419
420/*
Maciej W. Rozyckifbf6ede2007-05-21 13:47:22 +0100421 * Just move to kernel mode and leave interrupts as they are. Note
422 * for the R3000 this means copying the previous enable from IEp.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 * Set cp0 enable bit as sign that we're running on the kernel stack
424 */
425 .macro KMODE
426 mfc0 t0, CP0_STATUS
Maciej W. Rozyckifbf6ede2007-05-21 13:47:22 +0100427 li t1, ST0_CU0 | (STATMASK & ~1)
428#if defined(CONFIG_CPU_R3000) || defined(CONFIG_CPU_TX39XX)
429 andi t2, t0, ST0_IEP
430 srl t2, 2
431 or t0, t2
432#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 or t0, t1
Maciej W. Rozyckifbf6ede2007-05-21 13:47:22 +0100434 xori t0, STATMASK & ~1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 mtc0 t0, CP0_STATUS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 irq_disable_hazard
437 .endm
438
439#endif /* _ASM_STACKFRAME_H */