10603839bSHuacai Chen /* SPDX-License-Identifier: GPL-2.0 */ 20603839bSHuacai Chen /* 30603839bSHuacai Chen * Copyright (C) 2020-2022 Loongson Technology Corporation Limited 40603839bSHuacai Chen */ 50603839bSHuacai Chen #ifndef _ASM_STACKFRAME_H 60603839bSHuacai Chen #define _ASM_STACKFRAME_H 70603839bSHuacai Chen 80603839bSHuacai Chen #include <linux/threads.h> 90603839bSHuacai Chen 108cbd5ebfSYouling Tang #include <asm/addrspace.h> 110603839bSHuacai Chen #include <asm/asm.h> 120603839bSHuacai Chen #include <asm/asmmacro.h> 130603839bSHuacai Chen #include <asm/asm-offsets.h> 140603839bSHuacai Chen #include <asm/loongarch.h> 150603839bSHuacai Chen #include <asm/thread_info.h> 16*cb8a2ef0STiezhu Yang #include <asm/unwind_hints.h> 170603839bSHuacai Chen 180603839bSHuacai Chen /* Make the addition of cfi info a little easier. */ 190603839bSHuacai Chen .macro cfi_rel_offset reg offset=0 docfi=0 200603839bSHuacai Chen .if \docfi 210603839bSHuacai Chen .cfi_rel_offset \reg, \offset 220603839bSHuacai Chen .endif 230603839bSHuacai Chen .endm 240603839bSHuacai Chen 250603839bSHuacai Chen .macro cfi_st reg offset=0 docfi=0 260603839bSHuacai Chen cfi_rel_offset \reg, \offset, \docfi 270603839bSHuacai Chen LONG_S \reg, sp, \offset 280603839bSHuacai Chen .endm 290603839bSHuacai Chen 300603839bSHuacai Chen .macro cfi_restore reg offset=0 docfi=0 310603839bSHuacai Chen .if \docfi 320603839bSHuacai Chen .cfi_restore \reg 330603839bSHuacai Chen .endif 340603839bSHuacai Chen .endm 350603839bSHuacai Chen 360603839bSHuacai Chen .macro cfi_ld reg offset=0 docfi=0 370603839bSHuacai Chen LONG_L \reg, sp, \offset 380603839bSHuacai Chen cfi_restore \reg \offset \docfi 390603839bSHuacai Chen .endm 400603839bSHuacai Chen 418cbd5ebfSYouling Tang /* Jump to the runtime virtual address. */ 428cbd5ebfSYouling Tang .macro JUMP_VIRT_ADDR temp1 temp2 438cbd5ebfSYouling Tang li.d \temp1, CACHE_BASE 448cbd5ebfSYouling Tang pcaddi \temp2, 0 458cbd5ebfSYouling Tang or \temp1, \temp1, \temp2 468cbd5ebfSYouling Tang jirl zero, \temp1, 0xc 478cbd5ebfSYouling Tang .endm 488cbd5ebfSYouling Tang 490603839bSHuacai Chen .macro BACKUP_T0T1 500603839bSHuacai Chen csrwr t0, EXCEPTION_KS0 510603839bSHuacai Chen csrwr t1, EXCEPTION_KS1 520603839bSHuacai Chen .endm 530603839bSHuacai Chen 540603839bSHuacai Chen .macro RELOAD_T0T1 550603839bSHuacai Chen csrrd t0, EXCEPTION_KS0 560603839bSHuacai Chen csrrd t1, EXCEPTION_KS1 570603839bSHuacai Chen .endm 580603839bSHuacai Chen 590603839bSHuacai Chen .macro SAVE_TEMP docfi=0 600603839bSHuacai Chen RELOAD_T0T1 610603839bSHuacai Chen cfi_st t0, PT_R12, \docfi 620603839bSHuacai Chen cfi_st t1, PT_R13, \docfi 630603839bSHuacai Chen cfi_st t2, PT_R14, \docfi 640603839bSHuacai Chen cfi_st t3, PT_R15, \docfi 650603839bSHuacai Chen cfi_st t4, PT_R16, \docfi 660603839bSHuacai Chen cfi_st t5, PT_R17, \docfi 670603839bSHuacai Chen cfi_st t6, PT_R18, \docfi 680603839bSHuacai Chen cfi_st t7, PT_R19, \docfi 690603839bSHuacai Chen cfi_st t8, PT_R20, \docfi 700603839bSHuacai Chen .endm 710603839bSHuacai Chen 720603839bSHuacai Chen .macro SAVE_STATIC docfi=0 730603839bSHuacai Chen cfi_st s0, PT_R23, \docfi 740603839bSHuacai Chen cfi_st s1, PT_R24, \docfi 750603839bSHuacai Chen cfi_st s2, PT_R25, \docfi 760603839bSHuacai Chen cfi_st s3, PT_R26, \docfi 770603839bSHuacai Chen cfi_st s4, PT_R27, \docfi 780603839bSHuacai Chen cfi_st s5, PT_R28, \docfi 790603839bSHuacai Chen cfi_st s6, PT_R29, \docfi 800603839bSHuacai Chen cfi_st s7, PT_R30, \docfi 810603839bSHuacai Chen cfi_st s8, PT_R31, \docfi 820603839bSHuacai Chen .endm 830603839bSHuacai Chen 840603839bSHuacai Chen /* 850603839bSHuacai Chen * get_saved_sp returns the SP for the current CPU by looking in the 860603839bSHuacai Chen * kernelsp array for it. It stores the current sp in t0 and loads the 870603839bSHuacai Chen * new value in sp. 880603839bSHuacai Chen */ 890603839bSHuacai Chen .macro get_saved_sp docfi=0 90396233c6SYouling Tang la_abs t1, kernelsp 9146859ac8SHuacai Chen #ifdef CONFIG_SMP 9246859ac8SHuacai Chen csrrd t0, PERCPU_BASE_KS 9346859ac8SHuacai Chen LONG_ADD t1, t1, t0 9446859ac8SHuacai Chen #endif 950603839bSHuacai Chen move t0, sp 960603839bSHuacai Chen .if \docfi 970603839bSHuacai Chen .cfi_register sp, t0 980603839bSHuacai Chen .endif 990603839bSHuacai Chen LONG_L sp, t1, 0 1000603839bSHuacai Chen .endm 1010603839bSHuacai Chen 1020603839bSHuacai Chen .macro set_saved_sp stackp temp temp2 103f733f119SXi Ruoyao la.pcrel \temp, kernelsp 10446859ac8SHuacai Chen #ifdef CONFIG_SMP 10546859ac8SHuacai Chen LONG_ADD \temp, \temp, u0 10646859ac8SHuacai Chen #endif 1070603839bSHuacai Chen LONG_S \stackp, \temp, 0 1080603839bSHuacai Chen .endm 1090603839bSHuacai Chen 1100603839bSHuacai Chen .macro SAVE_SOME docfi=0 1110603839bSHuacai Chen csrrd t1, LOONGARCH_CSR_PRMD 1120603839bSHuacai Chen andi t1, t1, 0x3 /* extract pplv bit */ 1130603839bSHuacai Chen move t0, sp 1140603839bSHuacai Chen beqz t1, 8f 1150603839bSHuacai Chen /* Called from user mode, new stack. */ 1160603839bSHuacai Chen get_saved_sp docfi=\docfi 1170603839bSHuacai Chen 8: 1180603839bSHuacai Chen PTR_ADDI sp, sp, -PT_SIZE 1190603839bSHuacai Chen .if \docfi 1200603839bSHuacai Chen .cfi_def_cfa sp, 0 1210603839bSHuacai Chen .endif 1220603839bSHuacai Chen cfi_st t0, PT_R3, \docfi 1230603839bSHuacai Chen cfi_rel_offset sp, PT_R3, \docfi 1240603839bSHuacai Chen LONG_S zero, sp, PT_R0 1250603839bSHuacai Chen csrrd t0, LOONGARCH_CSR_PRMD 1260603839bSHuacai Chen LONG_S t0, sp, PT_PRMD 1270603839bSHuacai Chen csrrd t0, LOONGARCH_CSR_CRMD 1280603839bSHuacai Chen LONG_S t0, sp, PT_CRMD 1290603839bSHuacai Chen csrrd t0, LOONGARCH_CSR_EUEN 1300603839bSHuacai Chen LONG_S t0, sp, PT_EUEN 1310603839bSHuacai Chen csrrd t0, LOONGARCH_CSR_ECFG 1320603839bSHuacai Chen LONG_S t0, sp, PT_ECFG 1330603839bSHuacai Chen csrrd t0, LOONGARCH_CSR_ESTAT 1340603839bSHuacai Chen PTR_S t0, sp, PT_ESTAT 1350603839bSHuacai Chen cfi_st ra, PT_R1, \docfi 1360603839bSHuacai Chen cfi_st a0, PT_R4, \docfi 1370603839bSHuacai Chen cfi_st a1, PT_R5, \docfi 1380603839bSHuacai Chen cfi_st a2, PT_R6, \docfi 1390603839bSHuacai Chen cfi_st a3, PT_R7, \docfi 1400603839bSHuacai Chen cfi_st a4, PT_R8, \docfi 1410603839bSHuacai Chen cfi_st a5, PT_R9, \docfi 1420603839bSHuacai Chen cfi_st a6, PT_R10, \docfi 1430603839bSHuacai Chen cfi_st a7, PT_R11, \docfi 1440603839bSHuacai Chen csrrd ra, LOONGARCH_CSR_ERA 1450603839bSHuacai Chen LONG_S ra, sp, PT_ERA 1460603839bSHuacai Chen .if \docfi 1470603839bSHuacai Chen .cfi_rel_offset ra, PT_ERA 1480603839bSHuacai Chen .endif 1490603839bSHuacai Chen cfi_st tp, PT_R2, \docfi 1500603839bSHuacai Chen cfi_st fp, PT_R22, \docfi 1510603839bSHuacai Chen 1520603839bSHuacai Chen /* Set thread_info if we're coming from user mode */ 1530603839bSHuacai Chen csrrd t0, LOONGARCH_CSR_PRMD 1540603839bSHuacai Chen andi t0, t0, 0x3 /* extract pplv bit */ 1550603839bSHuacai Chen beqz t0, 9f 1560603839bSHuacai Chen 1570603839bSHuacai Chen li.d tp, ~_THREAD_MASK 1580603839bSHuacai Chen and tp, tp, sp 1590603839bSHuacai Chen cfi_st u0, PT_R21, \docfi 1600603839bSHuacai Chen csrrd u0, PERCPU_BASE_KS 1610603839bSHuacai Chen 9: 162e14dd076SQing Zhang #ifdef CONFIG_KGDB 163e14dd076SQing Zhang li.w t0, CSR_CRMD_WE 164e14dd076SQing Zhang csrxchg t0, t0, LOONGARCH_CSR_CRMD 165e14dd076SQing Zhang #endif 166*cb8a2ef0STiezhu Yang UNWIND_HINT_REGS 1670603839bSHuacai Chen .endm 1680603839bSHuacai Chen 1690603839bSHuacai Chen .macro SAVE_ALL docfi=0 1700603839bSHuacai Chen SAVE_SOME \docfi 1710603839bSHuacai Chen SAVE_TEMP \docfi 1720603839bSHuacai Chen SAVE_STATIC \docfi 1730603839bSHuacai Chen .endm 1740603839bSHuacai Chen 1750603839bSHuacai Chen .macro RESTORE_TEMP docfi=0 1760603839bSHuacai Chen cfi_ld t0, PT_R12, \docfi 1770603839bSHuacai Chen cfi_ld t1, PT_R13, \docfi 1780603839bSHuacai Chen cfi_ld t2, PT_R14, \docfi 1790603839bSHuacai Chen cfi_ld t3, PT_R15, \docfi 1800603839bSHuacai Chen cfi_ld t4, PT_R16, \docfi 1810603839bSHuacai Chen cfi_ld t5, PT_R17, \docfi 1820603839bSHuacai Chen cfi_ld t6, PT_R18, \docfi 1830603839bSHuacai Chen cfi_ld t7, PT_R19, \docfi 1840603839bSHuacai Chen cfi_ld t8, PT_R20, \docfi 1850603839bSHuacai Chen .endm 1860603839bSHuacai Chen 1870603839bSHuacai Chen .macro RESTORE_STATIC docfi=0 1880603839bSHuacai Chen cfi_ld s0, PT_R23, \docfi 1890603839bSHuacai Chen cfi_ld s1, PT_R24, \docfi 1900603839bSHuacai Chen cfi_ld s2, PT_R25, \docfi 1910603839bSHuacai Chen cfi_ld s3, PT_R26, \docfi 1920603839bSHuacai Chen cfi_ld s4, PT_R27, \docfi 1930603839bSHuacai Chen cfi_ld s5, PT_R28, \docfi 1940603839bSHuacai Chen cfi_ld s6, PT_R29, \docfi 1950603839bSHuacai Chen cfi_ld s7, PT_R30, \docfi 1960603839bSHuacai Chen cfi_ld s8, PT_R31, \docfi 1970603839bSHuacai Chen .endm 1980603839bSHuacai Chen 1990603839bSHuacai Chen .macro RESTORE_SOME docfi=0 2000603839bSHuacai Chen LONG_L a0, sp, PT_PRMD 2010603839bSHuacai Chen andi a0, a0, 0x3 /* extract pplv bit */ 2020603839bSHuacai Chen beqz a0, 8f 2030603839bSHuacai Chen cfi_ld u0, PT_R21, \docfi 2040603839bSHuacai Chen 8: 2050603839bSHuacai Chen LONG_L a0, sp, PT_ERA 2060603839bSHuacai Chen csrwr a0, LOONGARCH_CSR_ERA 2070603839bSHuacai Chen LONG_L a0, sp, PT_PRMD 2080603839bSHuacai Chen csrwr a0, LOONGARCH_CSR_PRMD 2090603839bSHuacai Chen cfi_ld ra, PT_R1, \docfi 2100603839bSHuacai Chen cfi_ld a0, PT_R4, \docfi 2110603839bSHuacai Chen cfi_ld a1, PT_R5, \docfi 2120603839bSHuacai Chen cfi_ld a2, PT_R6, \docfi 2130603839bSHuacai Chen cfi_ld a3, PT_R7, \docfi 2140603839bSHuacai Chen cfi_ld a4, PT_R8, \docfi 2150603839bSHuacai Chen cfi_ld a5, PT_R9, \docfi 2160603839bSHuacai Chen cfi_ld a6, PT_R10, \docfi 2170603839bSHuacai Chen cfi_ld a7, PT_R11, \docfi 2180603839bSHuacai Chen cfi_ld tp, PT_R2, \docfi 2190603839bSHuacai Chen cfi_ld fp, PT_R22, \docfi 2200603839bSHuacai Chen .endm 2210603839bSHuacai Chen 2220603839bSHuacai Chen .macro RESTORE_SP_AND_RET docfi=0 2230603839bSHuacai Chen cfi_ld sp, PT_R3, \docfi 224*cb8a2ef0STiezhu Yang UNWIND_HINT_FUNC 2250603839bSHuacai Chen ertn 2260603839bSHuacai Chen .endm 2270603839bSHuacai Chen 2280603839bSHuacai Chen .macro RESTORE_ALL_AND_RET docfi=0 2290603839bSHuacai Chen RESTORE_STATIC \docfi 2300603839bSHuacai Chen RESTORE_TEMP \docfi 2310603839bSHuacai Chen RESTORE_SOME \docfi 2320603839bSHuacai Chen RESTORE_SP_AND_RET \docfi 2330603839bSHuacai Chen .endm 2340603839bSHuacai Chen 2350603839bSHuacai Chen #endif /* _ASM_STACKFRAME_H */ 236