1b2441318SGreg Kroah-Hartman/* SPDX-License-Identifier: GPL-2.0 */ 2a88b5ba8SSam Ravnborg/* 3a88b5ba8SSam Ravnborg * etrap.S: Preparing for entry into the kernel on Sparc V9. 4a88b5ba8SSam Ravnborg * 5a88b5ba8SSam Ravnborg * Copyright (C) 1996, 1997 David S. Miller (davem@caip.rutgers.edu) 6a88b5ba8SSam Ravnborg * Copyright (C) 1997, 1998, 1999 Jakub Jelinek (jj@ultra.linux.cz) 7a88b5ba8SSam Ravnborg */ 8a88b5ba8SSam Ravnborg 9a88b5ba8SSam Ravnborg 10a88b5ba8SSam Ravnborg#include <asm/asi.h> 11a88b5ba8SSam Ravnborg#include <asm/pstate.h> 12a88b5ba8SSam Ravnborg#include <asm/ptrace.h> 13a88b5ba8SSam Ravnborg#include <asm/page.h> 14a88b5ba8SSam Ravnborg#include <asm/spitfire.h> 15a88b5ba8SSam Ravnborg#include <asm/head.h> 16a88b5ba8SSam Ravnborg#include <asm/processor.h> 17a88b5ba8SSam Ravnborg#include <asm/mmu.h> 18a88b5ba8SSam Ravnborg 19a88b5ba8SSam Ravnborg#define TASK_REGOFF (THREAD_SIZE-TRACEREG_SZ-STACKFRAME_SZ) 20a88b5ba8SSam Ravnborg#define ETRAP_PSTATE1 (PSTATE_TSO | PSTATE_PRIV) 21a88b5ba8SSam Ravnborg#define ETRAP_PSTATE2 \ 22a88b5ba8SSam Ravnborg (PSTATE_TSO | PSTATE_PEF | PSTATE_PRIV | PSTATE_IE) 23a88b5ba8SSam Ravnborg 24a88b5ba8SSam Ravnborg/* 25a88b5ba8SSam Ravnborg * On entry, %g7 is return address - 0x4. 26a88b5ba8SSam Ravnborg * %g4 and %g5 will be preserved %l4 and %l5 respectively. 27a88b5ba8SSam Ravnborg */ 28a88b5ba8SSam Ravnborg 29a88b5ba8SSam Ravnborg .text 30a88b5ba8SSam Ravnborg .align 64 31a88b5ba8SSam Ravnborg .globl etrap_syscall, etrap, etrap_irq, etraptl1 32a88b5ba8SSam Ravnborgetrap: rdpr %pil, %g2 33a88b5ba8SSam Ravnborgetrap_irq: clr %g3 34a88b5ba8SSam Ravnborgetrap_syscall: TRAP_LOAD_THREAD_REG(%g6, %g1) 35a88b5ba8SSam Ravnborg rdpr %tstate, %g1 36a88b5ba8SSam Ravnborg or %g1, %g3, %g1 37a88b5ba8SSam Ravnborg sllx %g2, 20, %g3 38a88b5ba8SSam Ravnborg andcc %g1, TSTATE_PRIV, %g0 39a88b5ba8SSam Ravnborg or %g1, %g3, %g1 40a88b5ba8SSam Ravnborg bne,pn %xcc, 1f 41a88b5ba8SSam Ravnborg sub %sp, STACKFRAME_SZ+TRACEREG_SZ-STACK_BIAS, %g2 42a7159a87SAnthony Yznaga661: wrpr %g0, 7, %cleanwin 43a7159a87SAnthony Yznaga .section .fast_win_ctrl_1insn_patch, "ax" 44a7159a87SAnthony Yznaga .word 661b 45a7159a87SAnthony Yznaga .word 0x85880000 ! allclean 46a7159a87SAnthony Yznaga .previous 47a88b5ba8SSam Ravnborg 48a88b5ba8SSam Ravnborg sethi %hi(TASK_REGOFF), %g2 49a88b5ba8SSam Ravnborg sethi %hi(TSTATE_PEF), %g3 50a88b5ba8SSam Ravnborg or %g2, %lo(TASK_REGOFF), %g2 51a88b5ba8SSam Ravnborg and %g1, %g3, %g3 52a88b5ba8SSam Ravnborg brnz,pn %g3, 1f 53a88b5ba8SSam Ravnborg add %g6, %g2, %g2 54a88b5ba8SSam Ravnborg wr %g0, 0, %fprs 55a88b5ba8SSam Ravnborg1: rdpr %tpc, %g3 56a88b5ba8SSam Ravnborg 57a88b5ba8SSam Ravnborg stx %g1, [%g2 + STACKFRAME_SZ + PT_V9_TSTATE] 58a88b5ba8SSam Ravnborg rdpr %tnpc, %g1 59a88b5ba8SSam Ravnborg stx %g3, [%g2 + STACKFRAME_SZ + PT_V9_TPC] 60a88b5ba8SSam Ravnborg rd %y, %g3 61a88b5ba8SSam Ravnborg stx %g1, [%g2 + STACKFRAME_SZ + PT_V9_TNPC] 62a88b5ba8SSam Ravnborg rdpr %tt, %g1 63a88b5ba8SSam Ravnborg st %g3, [%g2 + STACKFRAME_SZ + PT_V9_Y] 64a88b5ba8SSam Ravnborg sethi %hi(PT_REGS_MAGIC), %g3 65a88b5ba8SSam Ravnborg or %g3, %g1, %g1 66a88b5ba8SSam Ravnborg st %g1, [%g2 + STACKFRAME_SZ + PT_V9_MAGIC] 67a88b5ba8SSam Ravnborg 68a88b5ba8SSam Ravnborg rdpr %cansave, %g1 69a88b5ba8SSam Ravnborg brnz,pt %g1, etrap_save 70a88b5ba8SSam Ravnborg nop 71a88b5ba8SSam Ravnborg 72a88b5ba8SSam Ravnborg rdpr %cwp, %g1 73a88b5ba8SSam Ravnborg add %g1, 2, %g1 74a88b5ba8SSam Ravnborg wrpr %g1, %cwp 75a88b5ba8SSam Ravnborg be,pt %xcc, etrap_user_spill 76a88b5ba8SSam Ravnborg mov ASI_AIUP, %g3 77a88b5ba8SSam Ravnborg 78a88b5ba8SSam Ravnborg rdpr %otherwin, %g3 79a88b5ba8SSam Ravnborg brz %g3, etrap_kernel_spill 80a88b5ba8SSam Ravnborg mov ASI_AIUS, %g3 81a88b5ba8SSam Ravnborg 82a88b5ba8SSam Ravnborgetrap_user_spill: 83a88b5ba8SSam Ravnborg 84a88b5ba8SSam Ravnborg wr %g3, 0x0, %asi 85a88b5ba8SSam Ravnborg ldx [%g6 + TI_FLAGS], %g3 86a88b5ba8SSam Ravnborg and %g3, _TIF_32BIT, %g3 87a88b5ba8SSam Ravnborg brnz,pt %g3, etrap_user_spill_32bit 88a88b5ba8SSam Ravnborg nop 89a88b5ba8SSam Ravnborg ba,a,pt %xcc, etrap_user_spill_64bit 90a88b5ba8SSam Ravnborg 91a88b5ba8SSam Ravnborgetrap_save: save %g2, -STACK_BIAS, %sp 92a88b5ba8SSam Ravnborg mov %g6, %l6 93a88b5ba8SSam Ravnborg 94a88b5ba8SSam Ravnborg bne,pn %xcc, 3f 95a88b5ba8SSam Ravnborg mov PRIMARY_CONTEXT, %l4 96a7159a87SAnthony Yznaga661: rdpr %canrestore, %g3 97a7159a87SAnthony Yznaga .section .fast_win_ctrl_1insn_patch, "ax" 98a7159a87SAnthony Yznaga .word 661b 99a7159a87SAnthony Yznaga nop 100a7159a87SAnthony Yznaga .previous 101a7159a87SAnthony Yznaga 102a88b5ba8SSam Ravnborg rdpr %wstate, %g2 103a7159a87SAnthony Yznaga661: wrpr %g0, 0, %canrestore 104a7159a87SAnthony Yznaga .section .fast_win_ctrl_1insn_patch, "ax" 105a7159a87SAnthony Yznaga .word 661b 106a7159a87SAnthony Yznaga nop 107a7159a87SAnthony Yznaga .previous 108a88b5ba8SSam Ravnborg sll %g2, 3, %g2 109dff933daSAl Viro 110dff933daSAl Viro /* Set TI_SYS_FPDEPTH to 1 and clear TI_SYS_NOERROR. */ 111a88b5ba8SSam Ravnborg mov 1, %l5 112dff933daSAl Viro sth %l5, [%l6 + TI_SYS_NOERROR] 113a88b5ba8SSam Ravnborg 114a7159a87SAnthony Yznaga661: wrpr %g3, 0, %otherwin 115a7159a87SAnthony Yznaga .section .fast_win_ctrl_1insn_patch, "ax" 116a7159a87SAnthony Yznaga .word 661b 117a7159a87SAnthony Yznaga .word 0x87880000 ! otherw 118a7159a87SAnthony Yznaga .previous 119a7159a87SAnthony Yznaga 120a88b5ba8SSam Ravnborg wrpr %g2, 0, %wstate 121a88b5ba8SSam Ravnborg sethi %hi(sparc64_kern_pri_context), %g2 122a88b5ba8SSam Ravnborg ldx [%g2 + %lo(sparc64_kern_pri_context)], %g3 123a88b5ba8SSam Ravnborg 124a88b5ba8SSam Ravnborg661: stxa %g3, [%l4] ASI_DMMU 125a88b5ba8SSam Ravnborg .section .sun4v_1insn_patch, "ax" 126a88b5ba8SSam Ravnborg .word 661b 127a88b5ba8SSam Ravnborg stxa %g3, [%l4] ASI_MMU 128a88b5ba8SSam Ravnborg .previous 129a88b5ba8SSam Ravnborg 130a88b5ba8SSam Ravnborg sethi %hi(KERNBASE), %l4 131a88b5ba8SSam Ravnborg flush %l4 132a88b5ba8SSam Ravnborg mov ASI_AIUS, %l7 133a88b5ba8SSam Ravnborg2: mov %g4, %l4 134a88b5ba8SSam Ravnborg mov %g5, %l5 135a88b5ba8SSam Ravnborg add %g7, 4, %l2 136a88b5ba8SSam Ravnborg 137a88b5ba8SSam Ravnborg /* Go to trap time globals so we can save them. */ 138a88b5ba8SSam Ravnborg661: wrpr %g0, ETRAP_PSTATE1, %pstate 139a88b5ba8SSam Ravnborg .section .sun4v_1insn_patch, "ax" 140a88b5ba8SSam Ravnborg .word 661b 141a88b5ba8SSam Ravnborg SET_GL(0) 142a88b5ba8SSam Ravnborg .previous 143a88b5ba8SSam Ravnborg 144a88b5ba8SSam Ravnborg stx %g1, [%sp + PTREGS_OFF + PT_V9_G1] 145a88b5ba8SSam Ravnborg stx %g2, [%sp + PTREGS_OFF + PT_V9_G2] 146a88b5ba8SSam Ravnborg sllx %l7, 24, %l7 147a88b5ba8SSam Ravnborg stx %g3, [%sp + PTREGS_OFF + PT_V9_G3] 148a88b5ba8SSam Ravnborg rdpr %cwp, %l0 149a88b5ba8SSam Ravnborg stx %g4, [%sp + PTREGS_OFF + PT_V9_G4] 150a88b5ba8SSam Ravnborg stx %g5, [%sp + PTREGS_OFF + PT_V9_G5] 151a88b5ba8SSam Ravnborg stx %g6, [%sp + PTREGS_OFF + PT_V9_G6] 152a88b5ba8SSam Ravnborg stx %g7, [%sp + PTREGS_OFF + PT_V9_G7] 153a88b5ba8SSam Ravnborg or %l7, %l0, %l7 154*74a04967SKhalid Aziz661: sethi %hi(TSTATE_TSO | TSTATE_PEF), %l0 155*74a04967SKhalid Aziz /* If userspace is using ADI, it could potentially pass 156*74a04967SKhalid Aziz * a pointer with version tag embedded in it. To maintain 157*74a04967SKhalid Aziz * the ADI security, we must enable PSTATE.mcde. Userspace 158*74a04967SKhalid Aziz * would have already set TTE.mcd in an earlier call to 159*74a04967SKhalid Aziz * kernel and set the version tag for the address being 160*74a04967SKhalid Aziz * dereferenced. Setting PSTATE.mcde would ensure any 161*74a04967SKhalid Aziz * access to userspace data through a system call honors 162*74a04967SKhalid Aziz * ADI and does not allow a rogue app to bypass ADI by 163*74a04967SKhalid Aziz * using system calls. Setting PSTATE.mcde only affects 164*74a04967SKhalid Aziz * accesses to virtual addresses that have TTE.mcd set. 165*74a04967SKhalid Aziz * Set PMCDPER to ensure any exceptions caused by ADI 166*74a04967SKhalid Aziz * version tag mismatch are exposed before system call 167*74a04967SKhalid Aziz * returns to userspace. Setting PMCDPER affects only 168*74a04967SKhalid Aziz * writes to virtual addresses that have TTE.mcd set and 169*74a04967SKhalid Aziz * have a version tag set as well. 170*74a04967SKhalid Aziz */ 171*74a04967SKhalid Aziz .section .sun_m7_1insn_patch, "ax" 172*74a04967SKhalid Aziz .word 661b 173*74a04967SKhalid Aziz sethi %hi(TSTATE_TSO | TSTATE_PEF | TSTATE_MCDE), %l0 174*74a04967SKhalid Aziz .previous 175*74a04967SKhalid Aziz661: nop 176*74a04967SKhalid Aziz .section .sun_m7_1insn_patch, "ax" 177*74a04967SKhalid Aziz .word 661b 178*74a04967SKhalid Aziz .word 0xaf902001 /* wrpr %g0, 1, %pmcdper */ 179*74a04967SKhalid Aziz .previous 180a88b5ba8SSam Ravnborg or %l7, %l0, %l7 181a88b5ba8SSam Ravnborg wrpr %l2, %tnpc 182a88b5ba8SSam Ravnborg wrpr %l7, (TSTATE_PRIV | TSTATE_IE), %tstate 183a88b5ba8SSam Ravnborg stx %i0, [%sp + PTREGS_OFF + PT_V9_I0] 184a88b5ba8SSam Ravnborg stx %i1, [%sp + PTREGS_OFF + PT_V9_I1] 185a88b5ba8SSam Ravnborg stx %i2, [%sp + PTREGS_OFF + PT_V9_I2] 186a88b5ba8SSam Ravnborg stx %i3, [%sp + PTREGS_OFF + PT_V9_I3] 187a88b5ba8SSam Ravnborg stx %i4, [%sp + PTREGS_OFF + PT_V9_I4] 188a88b5ba8SSam Ravnborg stx %i5, [%sp + PTREGS_OFF + PT_V9_I5] 189a88b5ba8SSam Ravnborg stx %i6, [%sp + PTREGS_OFF + PT_V9_I6] 190a88b5ba8SSam Ravnborg mov %l6, %g6 191a88b5ba8SSam Ravnborg stx %i7, [%sp + PTREGS_OFF + PT_V9_I7] 192a88b5ba8SSam Ravnborg LOAD_PER_CPU_BASE(%g5, %g6, %g4, %g3, %l1) 193a88b5ba8SSam Ravnborg ldx [%g6 + TI_TASK], %g4 194a88b5ba8SSam Ravnborg done 195a88b5ba8SSam Ravnborg 196a88b5ba8SSam Ravnborg3: mov ASI_P, %l7 197a88b5ba8SSam Ravnborg ldub [%l6 + TI_FPDEPTH], %l5 198a88b5ba8SSam Ravnborg add %l6, TI_FPSAVED + 1, %l4 199a88b5ba8SSam Ravnborg srl %l5, 1, %l3 200a88b5ba8SSam Ravnborg add %l5, 2, %l5 201dff933daSAl Viro 202dff933daSAl Viro /* Set TI_SYS_FPDEPTH to %l5 and clear TI_SYS_NOERROR. */ 203dff933daSAl Viro sth %l5, [%l6 + TI_SYS_NOERROR] 204a88b5ba8SSam Ravnborg ba,pt %xcc, 2b 205a88b5ba8SSam Ravnborg stb %g0, [%l4 + %l3] 206a88b5ba8SSam Ravnborg nop 207a88b5ba8SSam Ravnborg 208a88b5ba8SSam Ravnborgetraptl1: /* Save tstate/tpc/tnpc of TL 1-->4 and the tl register itself. 209a88b5ba8SSam Ravnborg * We place this right after pt_regs on the trap stack. 210a88b5ba8SSam Ravnborg * The layout is: 211a88b5ba8SSam Ravnborg * 0x00 TL1's TSTATE 212a88b5ba8SSam Ravnborg * 0x08 TL1's TPC 213a88b5ba8SSam Ravnborg * 0x10 TL1's TNPC 214a88b5ba8SSam Ravnborg * 0x18 TL1's TT 215a88b5ba8SSam Ravnborg * ... 216a88b5ba8SSam Ravnborg * 0x58 TL4's TT 217a88b5ba8SSam Ravnborg * 0x60 TL 218a88b5ba8SSam Ravnborg */ 219a88b5ba8SSam Ravnborg TRAP_LOAD_THREAD_REG(%g6, %g1) 220a88b5ba8SSam Ravnborg sub %sp, ((4 * 8) * 4) + 8, %g2 221a88b5ba8SSam Ravnborg rdpr %tl, %g1 222a88b5ba8SSam Ravnborg 223a88b5ba8SSam Ravnborg wrpr %g0, 1, %tl 224a88b5ba8SSam Ravnborg rdpr %tstate, %g3 225a88b5ba8SSam Ravnborg stx %g3, [%g2 + STACK_BIAS + 0x00] 226a88b5ba8SSam Ravnborg rdpr %tpc, %g3 227a88b5ba8SSam Ravnborg stx %g3, [%g2 + STACK_BIAS + 0x08] 228a88b5ba8SSam Ravnborg rdpr %tnpc, %g3 229a88b5ba8SSam Ravnborg stx %g3, [%g2 + STACK_BIAS + 0x10] 230a88b5ba8SSam Ravnborg rdpr %tt, %g3 231a88b5ba8SSam Ravnborg stx %g3, [%g2 + STACK_BIAS + 0x18] 232a88b5ba8SSam Ravnborg 233a88b5ba8SSam Ravnborg wrpr %g0, 2, %tl 234a88b5ba8SSam Ravnborg rdpr %tstate, %g3 235a88b5ba8SSam Ravnborg stx %g3, [%g2 + STACK_BIAS + 0x20] 236a88b5ba8SSam Ravnborg rdpr %tpc, %g3 237a88b5ba8SSam Ravnborg stx %g3, [%g2 + STACK_BIAS + 0x28] 238a88b5ba8SSam Ravnborg rdpr %tnpc, %g3 239a88b5ba8SSam Ravnborg stx %g3, [%g2 + STACK_BIAS + 0x30] 240a88b5ba8SSam Ravnborg rdpr %tt, %g3 241a88b5ba8SSam Ravnborg stx %g3, [%g2 + STACK_BIAS + 0x38] 242a88b5ba8SSam Ravnborg 243a88b5ba8SSam Ravnborg sethi %hi(is_sun4v), %g3 244a88b5ba8SSam Ravnborg lduw [%g3 + %lo(is_sun4v)], %g3 245a88b5ba8SSam Ravnborg brnz,pn %g3, finish_tl1_capture 246a88b5ba8SSam Ravnborg nop 247a88b5ba8SSam Ravnborg 248a88b5ba8SSam Ravnborg wrpr %g0, 3, %tl 249a88b5ba8SSam Ravnborg rdpr %tstate, %g3 250a88b5ba8SSam Ravnborg stx %g3, [%g2 + STACK_BIAS + 0x40] 251a88b5ba8SSam Ravnborg rdpr %tpc, %g3 252a88b5ba8SSam Ravnborg stx %g3, [%g2 + STACK_BIAS + 0x48] 253a88b5ba8SSam Ravnborg rdpr %tnpc, %g3 254a88b5ba8SSam Ravnborg stx %g3, [%g2 + STACK_BIAS + 0x50] 255a88b5ba8SSam Ravnborg rdpr %tt, %g3 256a88b5ba8SSam Ravnborg stx %g3, [%g2 + STACK_BIAS + 0x58] 257a88b5ba8SSam Ravnborg 258a88b5ba8SSam Ravnborg wrpr %g0, 4, %tl 259a88b5ba8SSam Ravnborg rdpr %tstate, %g3 260a88b5ba8SSam Ravnborg stx %g3, [%g2 + STACK_BIAS + 0x60] 261a88b5ba8SSam Ravnborg rdpr %tpc, %g3 262a88b5ba8SSam Ravnborg stx %g3, [%g2 + STACK_BIAS + 0x68] 263a88b5ba8SSam Ravnborg rdpr %tnpc, %g3 264a88b5ba8SSam Ravnborg stx %g3, [%g2 + STACK_BIAS + 0x70] 265a88b5ba8SSam Ravnborg rdpr %tt, %g3 266a88b5ba8SSam Ravnborg stx %g3, [%g2 + STACK_BIAS + 0x78] 267a88b5ba8SSam Ravnborg 268a88b5ba8SSam Ravnborg stx %g1, [%g2 + STACK_BIAS + 0x80] 269a88b5ba8SSam Ravnborg 270a88b5ba8SSam Ravnborgfinish_tl1_capture: 271a88b5ba8SSam Ravnborg wrpr %g0, 1, %tl 272a88b5ba8SSam Ravnborg661: nop 273a88b5ba8SSam Ravnborg .section .sun4v_1insn_patch, "ax" 274a88b5ba8SSam Ravnborg .word 661b 275a88b5ba8SSam Ravnborg SET_GL(1) 276a88b5ba8SSam Ravnborg .previous 277a88b5ba8SSam Ravnborg 278a88b5ba8SSam Ravnborg rdpr %tstate, %g1 279a88b5ba8SSam Ravnborg sub %g2, STACKFRAME_SZ + TRACEREG_SZ - STACK_BIAS, %g2 280a88b5ba8SSam Ravnborg ba,pt %xcc, 1b 281a88b5ba8SSam Ravnborg andcc %g1, TSTATE_PRIV, %g0 282a88b5ba8SSam Ravnborg 283a88b5ba8SSam Ravnborg#undef TASK_REGOFF 284a88b5ba8SSam Ravnborg#undef ETRAP_PSTATE1 285