18a23fdecSChristophe Leroy /* SPDX-License-Identifier: GPL-2.0 */ 28a23fdecSChristophe Leroy #ifndef __HEAD_32_H__ 38a23fdecSChristophe Leroy #define __HEAD_32_H__ 48a23fdecSChristophe Leroy 58a23fdecSChristophe Leroy #include <asm/ptrace.h> /* for STACK_FRAME_REGS_MARKER */ 68a23fdecSChristophe Leroy 78a23fdecSChristophe Leroy /* 88a23fdecSChristophe Leroy * Exception entry code. This code runs with address translation 98a23fdecSChristophe Leroy * turned off, i.e. using physical addresses. 108a23fdecSChristophe Leroy * We assume sprg3 has the physical address of the current 118a23fdecSChristophe Leroy * task's thread_struct. 128a23fdecSChristophe Leroy */ 138a23fdecSChristophe Leroy .macro EXCEPTION_PROLOG 141f1c4d01SChristophe Leroy EXCEPTION_PROLOG_0 151f1c4d01SChristophe Leroy EXCEPTION_PROLOG_1 161f1c4d01SChristophe Leroy EXCEPTION_PROLOG_2 171f1c4d01SChristophe Leroy .endm 181f1c4d01SChristophe Leroy 191f1c4d01SChristophe Leroy .macro EXCEPTION_PROLOG_0 208a23fdecSChristophe Leroy mtspr SPRN_SPRG_SCRATCH0,r10 218a23fdecSChristophe Leroy mtspr SPRN_SPRG_SCRATCH1,r11 225ae8fabcSChristophe Leroy mfspr r11, SPRN_SRR1 /* check whether user or kernel */ 238a23fdecSChristophe Leroy mfcr r10 245ae8fabcSChristophe Leroy andi. r11, r11, MSR_PR 258a23fdecSChristophe Leroy .endm 268a23fdecSChristophe Leroy 278a23fdecSChristophe Leroy .macro EXCEPTION_PROLOG_1 288a23fdecSChristophe Leroy tophys(r11,r1) /* use tophys(r1) if kernel */ 298a23fdecSChristophe Leroy beq 1f 308a23fdecSChristophe Leroy mfspr r11,SPRN_SPRG_THREAD 318a23fdecSChristophe Leroy lwz r11,TASK_STACK-THREAD(r11) 328a23fdecSChristophe Leroy addi r11,r11,THREAD_SIZE 338a23fdecSChristophe Leroy tophys(r11,r11) 348a23fdecSChristophe Leroy 1: subi r11,r11,INT_FRAME_SIZE /* alloc exc. frame */ 358a23fdecSChristophe Leroy .endm 368a23fdecSChristophe Leroy 378a23fdecSChristophe Leroy .macro EXCEPTION_PROLOG_2 388a23fdecSChristophe Leroy stw r10,_CCR(r11) /* save registers */ 398a23fdecSChristophe Leroy stw r12,GPR12(r11) 408a23fdecSChristophe Leroy stw r9,GPR9(r11) 418a23fdecSChristophe Leroy mfspr r10,SPRN_SPRG_SCRATCH0 428a23fdecSChristophe Leroy stw r10,GPR10(r11) 438a23fdecSChristophe Leroy mfspr r12,SPRN_SPRG_SCRATCH1 448a23fdecSChristophe Leroy stw r12,GPR11(r11) 458a23fdecSChristophe Leroy mflr r10 468a23fdecSChristophe Leroy stw r10,_LINK(r11) 478a23fdecSChristophe Leroy mfspr r12,SPRN_SRR0 488a23fdecSChristophe Leroy mfspr r9,SPRN_SRR1 498a23fdecSChristophe Leroy stw r1,GPR1(r11) 508a23fdecSChristophe Leroy stw r1,0(r11) 518a23fdecSChristophe Leroy tovirt(r1,r11) /* set new kernel sp */ 5290f204b9SChristophe Leroy #ifdef CONFIG_40x 5390f204b9SChristophe Leroy rlwinm r9,r9,0,14,12 /* clear MSR_WE (necessary?) */ 5490f204b9SChristophe Leroy #else 558a23fdecSChristophe Leroy li r10,MSR_KERNEL & ~(MSR_IR|MSR_DR) /* can take exceptions */ 5639bccfd1SChristophe Leroy mtmsr r10 /* (except for mach check in rtas) */ 5790f204b9SChristophe Leroy #endif 588a23fdecSChristophe Leroy stw r0,GPR0(r11) 598a23fdecSChristophe Leroy lis r10,STACK_FRAME_REGS_MARKER@ha /* exception frame marker */ 608a23fdecSChristophe Leroy addi r10,r10,STACK_FRAME_REGS_MARKER@l 618a23fdecSChristophe Leroy stw r10,8(r11) 628a23fdecSChristophe Leroy SAVE_4GPRS(3, r11) 638a23fdecSChristophe Leroy SAVE_2GPRS(7, r11) 648a23fdecSChristophe Leroy .endm 658a23fdecSChristophe Leroy 66b86fb888SChristophe Leroy .macro SYSCALL_ENTRY trapno 67b86fb888SChristophe Leroy mfspr r12,SPRN_SPRG_THREAD 68b86fb888SChristophe Leroy mfcr r10 69b86fb888SChristophe Leroy lwz r11,TASK_STACK-THREAD(r12) 70b86fb888SChristophe Leroy mflr r9 71b86fb888SChristophe Leroy addi r11,r11,THREAD_SIZE - INT_FRAME_SIZE 72b86fb888SChristophe Leroy rlwinm r10,r10,0,4,2 /* Clear SO bit in CR */ 73b86fb888SChristophe Leroy tophys(r11,r11) 74b86fb888SChristophe Leroy stw r10,_CCR(r11) /* save registers */ 75b86fb888SChristophe Leroy mfspr r10,SPRN_SRR0 76b86fb888SChristophe Leroy stw r9,_LINK(r11) 77b86fb888SChristophe Leroy mfspr r9,SPRN_SRR1 78b86fb888SChristophe Leroy stw r1,GPR1(r11) 79b86fb888SChristophe Leroy stw r1,0(r11) 80b86fb888SChristophe Leroy tovirt(r1,r11) /* set new kernel sp */ 81b86fb888SChristophe Leroy stw r10,_NIP(r11) 82b86fb888SChristophe Leroy #ifdef CONFIG_40x 83b86fb888SChristophe Leroy rlwinm r9,r9,0,14,12 /* clear MSR_WE (necessary?) */ 84b86fb888SChristophe Leroy #else 85ba18025fSChristophe Leroy LOAD_REG_IMMEDIATE(r10, MSR_KERNEL & ~(MSR_IR|MSR_DR)) /* can take exceptions */ 8639bccfd1SChristophe Leroy mtmsr r10 /* (except for mach check in rtas) */ 87b86fb888SChristophe Leroy #endif 88b86fb888SChristophe Leroy lis r10,STACK_FRAME_REGS_MARKER@ha /* exception frame marker */ 89b86fb888SChristophe Leroy stw r2,GPR2(r11) 90b86fb888SChristophe Leroy addi r10,r10,STACK_FRAME_REGS_MARKER@l 91b86fb888SChristophe Leroy stw r9,_MSR(r11) 92b86fb888SChristophe Leroy li r2, \trapno + 1 93b86fb888SChristophe Leroy stw r10,8(r11) 94b86fb888SChristophe Leroy stw r2,_TRAP(r11) 95b86fb888SChristophe Leroy SAVE_GPR(0, r11) 96b86fb888SChristophe Leroy SAVE_4GPRS(3, r11) 97b86fb888SChristophe Leroy SAVE_2GPRS(7, r11) 98b86fb888SChristophe Leroy addi r11,r1,STACK_FRAME_OVERHEAD 99b86fb888SChristophe Leroy addi r2,r12,-THREAD 100b86fb888SChristophe Leroy stw r11,PT_REGS(r12) 101b86fb888SChristophe Leroy #if defined(CONFIG_40x) 102b86fb888SChristophe Leroy /* Check to see if the dbcr0 register is set up to debug. Use the 103b86fb888SChristophe Leroy internal debug mode bit to do this. */ 104b86fb888SChristophe Leroy lwz r12,THREAD_DBCR0(r12) 105b86fb888SChristophe Leroy andis. r12,r12,DBCR0_IDM@h 106b86fb888SChristophe Leroy #endif 107b86fb888SChristophe Leroy ACCOUNT_CPU_USER_ENTRY(r2, r11, r12) 108b86fb888SChristophe Leroy #if defined(CONFIG_40x) 109b86fb888SChristophe Leroy beq+ 3f 110b86fb888SChristophe Leroy /* From user and task is ptraced - load up global dbcr0 */ 111b86fb888SChristophe Leroy li r12,-1 /* clear all pending debug events */ 112b86fb888SChristophe Leroy mtspr SPRN_DBSR,r12 113b86fb888SChristophe Leroy lis r11,global_dbcr0@ha 114b86fb888SChristophe Leroy tophys(r11,r11) 115b86fb888SChristophe Leroy addi r11,r11,global_dbcr0@l 116b86fb888SChristophe Leroy lwz r12,0(r11) 117b86fb888SChristophe Leroy mtspr SPRN_DBCR0,r12 118b86fb888SChristophe Leroy lwz r12,4(r11) 119b86fb888SChristophe Leroy addi r12,r12,-1 120b86fb888SChristophe Leroy stw r12,4(r11) 121b86fb888SChristophe Leroy #endif 122b86fb888SChristophe Leroy 123b86fb888SChristophe Leroy 3: 124b86fb888SChristophe Leroy tovirt(r2, r2) /* set r2 to current */ 125b86fb888SChristophe Leroy lis r11, transfer_to_syscall@h 126b86fb888SChristophe Leroy ori r11, r11, transfer_to_syscall@l 127b86fb888SChristophe Leroy #ifdef CONFIG_TRACE_IRQFLAGS 128b86fb888SChristophe Leroy /* 129b86fb888SChristophe Leroy * If MSR is changing we need to keep interrupts disabled at this point 130b86fb888SChristophe Leroy * otherwise we might risk taking an interrupt before we tell lockdep 131b86fb888SChristophe Leroy * they are enabled. 132b86fb888SChristophe Leroy */ 133ba18025fSChristophe Leroy LOAD_REG_IMMEDIATE(r10, MSR_KERNEL) 134b86fb888SChristophe Leroy rlwimi r10, r9, 0, MSR_EE 135b86fb888SChristophe Leroy #else 136ba18025fSChristophe Leroy LOAD_REG_IMMEDIATE(r10, MSR_KERNEL | MSR_EE) 137b86fb888SChristophe Leroy #endif 138b86fb888SChristophe Leroy #if defined(CONFIG_PPC_8xx) && defined(CONFIG_PERF_EVENTS) 139b86fb888SChristophe Leroy mtspr SPRN_NRI, r0 140b86fb888SChristophe Leroy #endif 141b86fb888SChristophe Leroy mtspr SPRN_SRR1,r10 142b86fb888SChristophe Leroy mtspr SPRN_SRR0,r11 143b86fb888SChristophe Leroy SYNC 144b86fb888SChristophe Leroy RFI /* jump to handler, enable MMU */ 145b86fb888SChristophe Leroy .endm 146b86fb888SChristophe Leroy 147*c9c84fd9SChristophe Leroy .macro save_dar_dsisr_on_stack reg1, reg2, sp 148*c9c84fd9SChristophe Leroy mfspr \reg1, SPRN_DAR 149*c9c84fd9SChristophe Leroy mfspr \reg2, SPRN_DSISR 150*c9c84fd9SChristophe Leroy stw \reg1, _DAR(\sp) 151*c9c84fd9SChristophe Leroy stw \reg2, _DSISR(\sp) 152*c9c84fd9SChristophe Leroy .endm 153*c9c84fd9SChristophe Leroy 154*c9c84fd9SChristophe Leroy .macro get_and_save_dar_dsisr_on_stack reg1, reg2, sp 155*c9c84fd9SChristophe Leroy save_dar_dsisr_on_stack \reg1, \reg2, \sp 156*c9c84fd9SChristophe Leroy .endm 157*c9c84fd9SChristophe Leroy 1588a23fdecSChristophe Leroy /* 1598a23fdecSChristophe Leroy * Note: code which follows this uses cr0.eq (set if from kernel), 1608a23fdecSChristophe Leroy * r11, r12 (SRR0), and r9 (SRR1). 1618a23fdecSChristophe Leroy * 1628a23fdecSChristophe Leroy * Note2: once we have set r1 we are in a position to take exceptions 1638a23fdecSChristophe Leroy * again, and we could thus set MSR:RI at that point. 1648a23fdecSChristophe Leroy */ 1658a23fdecSChristophe Leroy 1668a23fdecSChristophe Leroy /* 1678a23fdecSChristophe Leroy * Exception vectors. 1688a23fdecSChristophe Leroy */ 1698a23fdecSChristophe Leroy #ifdef CONFIG_PPC_BOOK3S 1708a23fdecSChristophe Leroy #define START_EXCEPTION(n, label) \ 1718a23fdecSChristophe Leroy . = n; \ 1728a23fdecSChristophe Leroy DO_KVM n; \ 1738a23fdecSChristophe Leroy label: 1748a23fdecSChristophe Leroy 1758a23fdecSChristophe Leroy #else 1768a23fdecSChristophe Leroy #define START_EXCEPTION(n, label) \ 1778a23fdecSChristophe Leroy . = n; \ 1788a23fdecSChristophe Leroy label: 1798a23fdecSChristophe Leroy 1808a23fdecSChristophe Leroy #endif 1818a23fdecSChristophe Leroy 1828a23fdecSChristophe Leroy #define EXCEPTION(n, label, hdlr, xfer) \ 1838a23fdecSChristophe Leroy START_EXCEPTION(n, label) \ 1848a23fdecSChristophe Leroy EXCEPTION_PROLOG; \ 1858a23fdecSChristophe Leroy addi r3,r1,STACK_FRAME_OVERHEAD; \ 1868a23fdecSChristophe Leroy xfer(n, hdlr) 1878a23fdecSChristophe Leroy 1881ae99b4bSChristophe Leroy #define EXC_XFER_TEMPLATE(hdlr, trap, msr, tfer, ret) \ 1898a23fdecSChristophe Leroy li r10,trap; \ 1908a23fdecSChristophe Leroy stw r10,_TRAP(r11); \ 191ba18025fSChristophe Leroy LOAD_REG_IMMEDIATE(r10, msr); \ 1928a23fdecSChristophe Leroy bl tfer; \ 1938a23fdecSChristophe Leroy .long hdlr; \ 1948a23fdecSChristophe Leroy .long ret 1958a23fdecSChristophe Leroy 1968a23fdecSChristophe Leroy #define EXC_XFER_STD(n, hdlr) \ 1971ae99b4bSChristophe Leroy EXC_XFER_TEMPLATE(hdlr, n, MSR_KERNEL, transfer_to_handler_full, \ 1988a23fdecSChristophe Leroy ret_from_except_full) 1998a23fdecSChristophe Leroy 2008a23fdecSChristophe Leroy #define EXC_XFER_LITE(n, hdlr) \ 2011ae99b4bSChristophe Leroy EXC_XFER_TEMPLATE(hdlr, n+1, MSR_KERNEL, transfer_to_handler, \ 2028a23fdecSChristophe Leroy ret_from_except) 2038a23fdecSChristophe Leroy 2048a23fdecSChristophe Leroy #endif /* __HEAD_32_H__ */ 205