/* SPDX-License-Identifier: GPL-2.0-only */1/*2* Event entry/exit for Hexagon3*4* Copyright (c) 2010-2013, The Linux Foundation. All rights reserved.5*/67#include <asm/asm-offsets.h> /* assembly-safer versions of C defines */8#include <asm/mem-layout.h> /* sigh, except for page_offset */9#include <asm/hexagon_vm.h>10#include <asm/thread_info.h>1112/*13* Entry into guest-mode Linux under Hexagon Virtual Machine.14* Stack pointer points to event record - build pt_regs on top of it,15* set up a plausible C stack frame, and dispatch to the C handler.16* On return, do vmrte virtual instruction with SP where we started.17*18* VM Spec 0.5 uses a trap to fetch HVM record now.19*/2021/*22* Save full register state, while setting up thread_info struct23* pointer derived from kernel stack pointer in THREADINFO_REG24* register, putting prior thread_info.regs pointer in a callee-save25* register (R24, which had better not ever be assigned to THREADINFO_REG),26* and updating thread_info.regs to point to current stack frame,27* so as to support nested events in kernel mode.28*29* As this is common code, we set the pt_regs system call number30* to -1 for all events. It will be replaced with the system call31* number in the case where we decode a system call (trap0(#1)).32*/3334#if CONFIG_HEXAGON_ARCH_VERSION < 435#define save_pt_regs()\36memd(R0 + #_PT_R3130) = R31:30; \37{ memw(R0 + #_PT_R2928) = R28; \38R31 = memw(R0 + #_PT_ER_VMPSP); }\39{ memw(R0 + #(_PT_R2928 + 4)) = R31; \40R31 = ugp; } \41{ memd(R0 + #_PT_R2726) = R27:26; \42R30 = gp ; } \43memd(R0 + #_PT_R2524) = R25:24; \44memd(R0 + #_PT_R2322) = R23:22; \45memd(R0 + #_PT_R2120) = R21:20; \46memd(R0 + #_PT_R1918) = R19:18; \47memd(R0 + #_PT_R1716) = R17:16; \48memd(R0 + #_PT_R1514) = R15:14; \49memd(R0 + #_PT_R1312) = R13:12; \50{ memd(R0 + #_PT_R1110) = R11:10; \51R15 = lc0; } \52{ memd(R0 + #_PT_R0908) = R9:8; \53R14 = sa0; } \54{ memd(R0 + #_PT_R0706) = R7:6; \55R13 = lc1; } \56{ memd(R0 + #_PT_R0504) = R5:4; \57R12 = sa1; } \58{ memd(R0 + #_PT_GPUGP) = R31:30; \59R11 = m1; \60R2.H = #HI(_THREAD_SIZE); } \61{ memd(R0 + #_PT_LC0SA0) = R15:14; \62R10 = m0; \63R2.L = #LO(_THREAD_SIZE); } \64{ memd(R0 + #_PT_LC1SA1) = R13:12; \65R15 = p3:0; \66R2 = neg(R2); } \67{ memd(R0 + #_PT_M1M0) = R11:10; \68R14 = usr; \69R2 = and(R0,R2); } \70{ memd(R0 + #_PT_PREDSUSR) = R15:14; \71THREADINFO_REG = R2; } \72{ r24 = memw(THREADINFO_REG + #_THREAD_INFO_PT_REGS); \73memw(THREADINFO_REG + #_THREAD_INFO_PT_REGS) = R0; \74R2 = #-1; } \75{ memw(R0 + #_PT_SYSCALL_NR) = R2; \76R30 = #0; }77#else78/* V4+ */79/* the # ## # syntax inserts a literal ## */80#define save_pt_regs()\81{ memd(R0 + #_PT_R3130) = R31:30; \82R30 = memw(R0 + #_PT_ER_VMPSP); }\83{ memw(R0 + #_PT_R2928) = R28; \84memw(R0 + #(_PT_R2928 + 4)) = R30; }\85{ R31:30 = C11:10; \86memd(R0 + #_PT_R2726) = R27:26; \87memd(R0 + #_PT_R2524) = R25:24; }\88{ memd(R0 + #_PT_R2322) = R23:22; \89memd(R0 + #_PT_R2120) = R21:20; }\90{ memd(R0 + #_PT_R1918) = R19:18; \91memd(R0 + #_PT_R1716) = R17:16; }\92{ memd(R0 + #_PT_R1514) = R15:14; \93memd(R0 + #_PT_R1312) = R13:12; \94R17:16 = C13:12; }\95{ memd(R0 + #_PT_R1110) = R11:10; \96memd(R0 + #_PT_R0908) = R9:8; \97R15:14 = C1:0; } \98{ memd(R0 + #_PT_R0706) = R7:6; \99memd(R0 + #_PT_R0504) = R5:4; \100R13:12 = C3:2; } \101{ memd(R0 + #_PT_GPUGP) = R31:30; \102memd(R0 + #_PT_LC0SA0) = R15:14; \103R11:10 = C7:6; }\104{ THREADINFO_REG = and(R0, # ## #-_THREAD_SIZE); \105memd(R0 + #_PT_LC1SA1) = R13:12; \106R15 = p3:0; }\107{ memd(R0 + #_PT_M1M0) = R11:10; \108memw(R0 + #_PT_PREDSUSR + 4) = R15; }\109{ r24 = memw(THREADINFO_REG + #_THREAD_INFO_PT_REGS); \110memw(THREADINFO_REG + #_THREAD_INFO_PT_REGS) = R0; \111R2 = #-1; } \112{ memw(R0 + #_PT_SYSCALL_NR) = R2; \113memd(R0 + #_PT_CS1CS0) = R17:16; \114R30 = #0; }115#endif116117/*118* Restore registers and thread_info.regs state. THREADINFO_REG119* is assumed to still be sane, and R24 to have been correctly120* preserved. Don't restore R29 (SP) until later.121*/122123#if CONFIG_HEXAGON_ARCH_VERSION < 4124#define restore_pt_regs() \125{ memw(THREADINFO_REG + #_THREAD_INFO_PT_REGS) = R24; \126R15:14 = memd(R0 + #_PT_PREDSUSR); } \127{ R11:10 = memd(R0 + #_PT_M1M0); \128p3:0 = R15; } \129{ R13:12 = memd(R0 + #_PT_LC1SA1); \130usr = R14; } \131{ R15:14 = memd(R0 + #_PT_LC0SA0); \132m1 = R11; } \133{ R3:2 = memd(R0 + #_PT_R0302); \134m0 = R10; } \135{ R5:4 = memd(R0 + #_PT_R0504); \136lc1 = R13; } \137{ R7:6 = memd(R0 + #_PT_R0706); \138sa1 = R12; } \139{ R9:8 = memd(R0 + #_PT_R0908); \140lc0 = R15; } \141{ R11:10 = memd(R0 + #_PT_R1110); \142sa0 = R14; } \143{ R13:12 = memd(R0 + #_PT_R1312); \144R15:14 = memd(R0 + #_PT_R1514); } \145{ R17:16 = memd(R0 + #_PT_R1716); \146R19:18 = memd(R0 + #_PT_R1918); } \147{ R21:20 = memd(R0 + #_PT_R2120); \148R23:22 = memd(R0 + #_PT_R2322); } \149{ R25:24 = memd(R0 + #_PT_R2524); \150R27:26 = memd(R0 + #_PT_R2726); } \151R31:30 = memd(R0 + #_PT_GPUGP); \152{ R28 = memw(R0 + #_PT_R2928); \153ugp = R31; } \154{ R31:30 = memd(R0 + #_PT_R3130); \155gp = R30; }156#else157/* V4+ */158#define restore_pt_regs() \159{ memw(THREADINFO_REG + #_THREAD_INFO_PT_REGS) = R24; \160R15:14 = memd(R0 + #_PT_PREDSUSR); } \161{ R11:10 = memd(R0 + #_PT_M1M0); \162R13:12 = memd(R0 + #_PT_LC1SA1); \163p3:0 = R15; } \164{ R15:14 = memd(R0 + #_PT_LC0SA0); \165R3:2 = memd(R0 + #_PT_R0302); \166usr = R14; } \167{ R5:4 = memd(R0 + #_PT_R0504); \168R7:6 = memd(R0 + #_PT_R0706); \169C7:6 = R11:10; }\170{ R9:8 = memd(R0 + #_PT_R0908); \171R11:10 = memd(R0 + #_PT_R1110); \172C3:2 = R13:12; }\173{ R13:12 = memd(R0 + #_PT_R1312); \174R15:14 = memd(R0 + #_PT_R1514); \175C1:0 = R15:14; }\176{ R17:16 = memd(R0 + #_PT_R1716); \177R19:18 = memd(R0 + #_PT_R1918); } \178{ R21:20 = memd(R0 + #_PT_R2120); \179R23:22 = memd(R0 + #_PT_R2322); } \180{ R25:24 = memd(R0 + #_PT_R2524); \181R27:26 = memd(R0 + #_PT_R2726); } \182R31:30 = memd(R0 + #_PT_CS1CS0); \183{ C13:12 = R31:30; \184R31:30 = memd(R0 + #_PT_GPUGP) ; \185R28 = memw(R0 + #_PT_R2928); }\186{ C11:10 = R31:30; \187R31:30 = memd(R0 + #_PT_R3130); }188#endif189190/*191* Clears off enough space for the rest of pt_regs; evrec is a part192* of pt_regs in HVM mode. Save R0/R1, set handler's address in R1.193* R0 is the address of pt_regs and is the parameter to save_pt_regs.194*/195196/*197* Since the HVM isn't automagically pushing the EVREC onto the stack anymore,198* we'll subract the entire size out and then fill it in ourselves.199* Need to save off R0, R1, R2, R3 immediately.200*/201202#if CONFIG_HEXAGON_ARCH_VERSION < 4203#define vm_event_entry(CHandler) \204{ \205R29 = add(R29, #-(_PT_REGS_SIZE)); \206memd(R29 + #(_PT_R0100 + -_PT_REGS_SIZE)) = R1:0; \207} \208{ \209memd(R29 +#_PT_R0302) = R3:2; \210} \211trap1(#HVM_TRAP1_VMGETREGS); \212{ \213memd(R29 + #_PT_ER_VMEL) = R1:0; \214R0 = R29; \215R1.L = #LO(CHandler); \216} \217{ \218memd(R29 + #_PT_ER_VMPSP) = R3:2; \219R1.H = #HI(CHandler); \220jump event_dispatch; \221}222#else223/* V4+ */224/* turn on I$ prefetch early */225/* the # ## # syntax inserts a literal ## */226#define vm_event_entry(CHandler) \227{ \228R29 = add(R29, #-(_PT_REGS_SIZE)); \229memd(R29 + #(_PT_R0100 + -_PT_REGS_SIZE)) = R1:0; \230memd(R29 + #(_PT_R0302 + -_PT_REGS_SIZE)) = R3:2; \231R0 = usr; \232} \233{ \234memw(R29 + #_PT_PREDSUSR) = R0; \235R0 = setbit(R0, #16); \236} \237usr = R0; \238R1:0 = G1:0; \239{ \240memd(R29 + #_PT_ER_VMEL) = R1:0; \241R1 = # ## #(CHandler); \242R3:2 = G3:2; \243} \244{ \245R0 = R29; \246memd(R29 + #_PT_ER_VMPSP) = R3:2; \247jump event_dispatch; \248}249#endif250251.text252/*253* Do bulk save/restore in one place.254* Adds a jump to dispatch latency, but255* saves hundreds of bytes.256*/257258event_dispatch:259save_pt_regs()260callr r1261262/*263* Coming back from the C-world, our thread info pointer264* should be in the designated register (usually R19)265*266* If we were in kernel mode, we don't need to check scheduler267* or signals if CONFIG_PREEMPTION is not set. If set, then it has268* to jump to a need_resched kind of block.269* BTW, CONFIG_PREEMPTION is not supported yet.270*/271272#ifdef CONFIG_PREEMPTION273R0 = #VM_INT_DISABLE274trap1(#HVM_TRAP1_VMSETIE)275#endif276277/* "Nested control path" -- if the previous mode was kernel */278{279R0 = memw(R29 + #_PT_ER_VMEST);280R26.L = #LO(do_work_pending);281}282{283P0 = tstbit(R0, #HVM_VMEST_UM_SFT);284if (!P0.new) jump:nt restore_all;285R26.H = #HI(do_work_pending);286R0 = #VM_INT_DISABLE;287}288289/*290* Check also the return from fork/system call, normally coming back from291* user mode292*293* R26 needs to have do_work_pending, and R0 should have VM_INT_DISABLE294*/295296check_work_pending:297/* Disable interrupts while checking TIF */298trap1(#HVM_TRAP1_VMSETIE)299{300R0 = R29; /* regs should still be at top of stack */301R1 = memw(THREADINFO_REG + #_THREAD_INFO_FLAGS);302callr R26;303}304305{306P0 = cmp.eq(R0, #0); if (!P0.new) jump:nt check_work_pending;307R0 = #VM_INT_DISABLE;308}309310restore_all:311/*312* Disable interrupts, if they weren't already, before reg restore.313* R0 gets preloaded with #VM_INT_DISABLE before we get here.314*/315trap1(#HVM_TRAP1_VMSETIE)316317/* do the setregs here for VM 0.5 */318/* R29 here should already be pointing at pt_regs */319{320R1:0 = memd(R29 + #_PT_ER_VMEL);321R3:2 = memd(R29 + #_PT_ER_VMPSP);322}323#if CONFIG_HEXAGON_ARCH_VERSION < 4324trap1(#HVM_TRAP1_VMSETREGS);325#else326G1:0 = R1:0;327G3:2 = R3:2;328#endif329330R0 = R29331restore_pt_regs()332{333R1:0 = memd(R29 + #_PT_R0100);334R29 = add(R29, #_PT_REGS_SIZE);335}336trap1(#HVM_TRAP1_VMRTE)337/* Notreached */338339340.globl _K_enter_genex341_K_enter_genex:342vm_event_entry(do_genex)343344.globl _K_enter_interrupt345_K_enter_interrupt:346vm_event_entry(arch_do_IRQ)347348.globl _K_enter_trap0349_K_enter_trap0:350vm_event_entry(do_trap0)351352.globl _K_enter_machcheck353_K_enter_machcheck:354vm_event_entry(do_machcheck)355356.globl _K_enter_debug357_K_enter_debug:358vm_event_entry(do_debug_exception)359360.globl ret_from_fork361ret_from_fork:362{363call schedule_tail364R26.H = #HI(do_work_pending);365}366{367P0 = cmp.eq(R24, #0);368R26.L = #LO(do_work_pending);369R0 = #VM_INT_DISABLE;370}371if (P0) jump check_work_pending372{373R0 = R25;374callr R24375}376{377jump check_work_pending378R0 = #VM_INT_DISABLE;379}380381382