/*1* Copyright 2011 Tilera Corporation. All Rights Reserved.2*3* This program is free software; you can redistribute it and/or4* modify it under the terms of the GNU General Public License5* as published by the Free Software Foundation, version 2.6*7* This program is distributed in the hope that it will be useful, but8* WITHOUT ANY WARRANTY; without even the implied warranty of9* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or10* NON INFRINGEMENT. See the GNU General Public License for11* more details.12*13* Linux interrupt vectors.14*/1516#include <linux/linkage.h>17#include <linux/errno.h>18#include <linux/unistd.h>19#include <asm/ptrace.h>20#include <asm/thread_info.h>21#include <asm/irqflags.h>22#include <asm/asm-offsets.h>23#include <asm/types.h>24#include <hv/hypervisor.h>25#include <arch/abi.h>26#include <arch/interrupts.h>27#include <arch/spr_def.h>2829#ifdef CONFIG_PREEMPT30# error "No support for kernel preemption currently"31#endif3233#define PTREGS_PTR(reg, ptreg) addli reg, sp, C_ABI_SAVE_AREA_SIZE + (ptreg)3435#define PTREGS_OFFSET_SYSCALL PTREGS_OFFSET_REG(TREG_SYSCALL_NR)363738.macro push_reg reg, ptr=sp, delta=-839{40st \ptr, \reg41addli \ptr, \ptr, \delta42}43.endm4445.macro pop_reg reg, ptr=sp, delta=846{47ld \reg, \ptr48addli \ptr, \ptr, \delta49}50.endm5152.macro pop_reg_zero reg, zreg, ptr=sp, delta=853{54move \zreg, zero55ld \reg, \ptr56addi \ptr, \ptr, \delta57}58.endm5960.macro push_extra_callee_saves reg61PTREGS_PTR(\reg, PTREGS_OFFSET_REG(51))62push_reg r51, \reg63push_reg r50, \reg64push_reg r49, \reg65push_reg r48, \reg66push_reg r47, \reg67push_reg r46, \reg68push_reg r45, \reg69push_reg r44, \reg70push_reg r43, \reg71push_reg r42, \reg72push_reg r41, \reg73push_reg r40, \reg74push_reg r39, \reg75push_reg r38, \reg76push_reg r37, \reg77push_reg r36, \reg78push_reg r35, \reg79push_reg r34, \reg, PTREGS_OFFSET_BASE - PTREGS_OFFSET_REG(34)80.endm8182.macro panic str83.pushsection .rodata, "a"841:85.asciz "\str"86.popsection87{88moveli r0, hw2_last(1b)89}90{91shl16insli r0, r0, hw1(1b)92}93{94shl16insli r0, r0, hw0(1b)95jal panic96}97.endm9899100#ifdef __COLLECT_LINKER_FEEDBACK__101.pushsection .text.intvec_feedback,"ax"102intvec_feedback:103.popsection104#endif105106/*107* Default interrupt handler.108*109* vecnum is where we'll put this code.110* c_routine is the C routine we'll call.111*112* The C routine is passed two arguments:113* - A pointer to the pt_regs state.114* - The interrupt vector number.115*116* The "processing" argument specifies the code for processing117* the interrupt. Defaults to "handle_interrupt".118*/119.macro int_hand vecnum, vecname, c_routine, processing=handle_interrupt120.org (\vecnum << 8)121intvec_\vecname:122/* Temporarily save a register so we have somewhere to work. */123124mtspr SPR_SYSTEM_SAVE_K_1, r0125mfspr r0, SPR_EX_CONTEXT_K_1126127andi r0, r0, SPR_EX_CONTEXT_1_1__PL_MASK /* mask off ICS */128129.ifc \vecnum, INT_DOUBLE_FAULT130/*131* For double-faults from user-space, fall through to the normal132* register save and stack setup path. Otherwise, it's the133* hypervisor giving us one last chance to dump diagnostics, and we134* branch to the kernel_double_fault routine to do so.135*/136beqz r0, 1f137j _kernel_double_fault1381:139.else140/*141* If we're coming from user-space, then set sp to the top of142* the kernel stack. Otherwise, assume sp is already valid.143*/144{145bnez r0, 0f146move r0, sp147}148.endif149150.ifc \c_routine, do_page_fault151/*152* The page_fault handler may be downcalled directly by the153* hypervisor even when Linux is running and has ICS set.154*155* In this case the contents of EX_CONTEXT_K_1 reflect the156* previous fault and can't be relied on to choose whether or157* not to reinitialize the stack pointer. So we add a test158* to see whether SYSTEM_SAVE_K_2 has the high bit set,159* and if so we don't reinitialize sp, since we must be coming160* from Linux. (In fact the precise case is !(val & ~1),161* but any Linux PC has to have the high bit set.)162*163* Note that the hypervisor *always* sets SYSTEM_SAVE_K_2 for164* any path that turns into a downcall to one of our TLB handlers.165*166* FIXME: if we end up never using this path, perhaps we should167* prevent the hypervisor from generating downcalls in this case.168* The advantage of getting a downcall is we can panic in Linux.169*/170mfspr r0, SPR_SYSTEM_SAVE_K_2171{172bltz r0, 0f /* high bit in S_S_1_2 is for a PC to use */173move r0, sp174}175.endif176177178/*179* SYSTEM_SAVE_K_0 holds the cpu number in the low bits, and180* the current stack top in the higher bits. So we recover181* our stack top by just masking off the low bits, then182* point sp at the top aligned address on the actual stack page.183*/184mfspr r0, SPR_SYSTEM_SAVE_K_0185mm r0, zero, LOG2_THREAD_SIZE, 631861870:188/*189* Align the stack mod 64 so we can properly predict what190* cache lines we need to write-hint to reduce memory fetch191* latency as we enter the kernel. The layout of memory is192* as follows, with cache line 0 at the lowest VA, and cache193* line 8 just below the r0 value this "andi" computes.194* Note that we never write to cache line 8, and we skip195* cache lines 1-3 for syscalls.196*197* cache line 8: ptregs padding (two words)198* cache line 7: sp, lr, pc, ex1, faultnum, orig_r0, flags, cmpexch199* cache line 6: r46...r53 (tp)200* cache line 5: r38...r45201* cache line 4: r30...r37202* cache line 3: r22...r29203* cache line 2: r14...r21204* cache line 1: r6...r13205* cache line 0: 2 x frame, r0..r5206*/207andi r0, r0, -64208209/*210* Push the first four registers on the stack, so that we can set211* them to vector-unique values before we jump to the common code.212*213* Registers are pushed on the stack as a struct pt_regs,214* with the sp initially just above the struct, and when we're215* done, sp points to the base of the struct, minus216* C_ABI_SAVE_AREA_SIZE, so we can directly jal to C code.217*218* This routine saves just the first four registers, plus the219* stack context so we can do proper backtracing right away,220* and defers to handle_interrupt to save the rest.221* The backtracer needs pc, ex1, lr, sp, r52, and faultnum.222*/223addli r0, r0, PTREGS_OFFSET_LR - (PTREGS_SIZE + KSTK_PTREGS_GAP)224wh64 r0 /* cache line 7 */225{226st r0, lr227addli r0, r0, PTREGS_OFFSET_SP - PTREGS_OFFSET_LR228}229{230st r0, sp231addli sp, r0, PTREGS_OFFSET_REG(52) - PTREGS_OFFSET_SP232}233wh64 sp /* cache line 6 */234{235st sp, r52236addli sp, sp, PTREGS_OFFSET_REG(1) - PTREGS_OFFSET_REG(52)237}238wh64 sp /* cache line 0 */239{240st sp, r1241addli sp, sp, PTREGS_OFFSET_REG(2) - PTREGS_OFFSET_REG(1)242}243{244st sp, r2245addli sp, sp, PTREGS_OFFSET_REG(3) - PTREGS_OFFSET_REG(2)246}247{248st sp, r3249addli sp, sp, PTREGS_OFFSET_PC - PTREGS_OFFSET_REG(3)250}251mfspr r0, SPR_EX_CONTEXT_K_0252.ifc \processing,handle_syscall253/*254* Bump the saved PC by one bundle so that when we return, we won't255* execute the same swint instruction again. We need to do this while256* we're in the critical section.257*/258addi r0, r0, 8259.endif260{261st sp, r0262addli sp, sp, PTREGS_OFFSET_EX1 - PTREGS_OFFSET_PC263}264mfspr r0, SPR_EX_CONTEXT_K_1265{266st sp, r0267addi sp, sp, PTREGS_OFFSET_FAULTNUM - PTREGS_OFFSET_EX1268/*269* Use r0 for syscalls so it's a temporary; use r1 for interrupts270* so that it gets passed through unchanged to the handler routine.271* Note that the .if conditional confusingly spans bundles.272*/273.ifc \processing,handle_syscall274movei r0, \vecnum275}276{277st sp, r0278.else279movei r1, \vecnum280}281{282st sp, r1283.endif284addli sp, sp, PTREGS_OFFSET_REG(0) - PTREGS_OFFSET_FAULTNUM285}286mfspr r0, SPR_SYSTEM_SAVE_K_1 /* Original r0 */287{288st sp, r0289addi sp, sp, -PTREGS_OFFSET_REG(0) - 8290}291{292st sp, zero /* write zero into "Next SP" frame pointer */293addi sp, sp, -8 /* leave SP pointing at bottom of frame */294}295.ifc \processing,handle_syscall296j handle_syscall297.else298/* Capture per-interrupt SPR context to registers. */299.ifc \c_routine, do_page_fault300mfspr r2, SPR_SYSTEM_SAVE_K_3 /* address of page fault */301mfspr r3, SPR_SYSTEM_SAVE_K_2 /* info about page fault */302.else303.ifc \vecnum, INT_ILL_TRANS304mfspr r2, ILL_TRANS_REASON305.else306.ifc \vecnum, INT_DOUBLE_FAULT307mfspr r2, SPR_SYSTEM_SAVE_K_2 /* double fault info from HV */308.else309.ifc \c_routine, do_trap310mfspr r2, GPV_REASON311.else312.ifc \c_routine, op_handle_perf_interrupt313mfspr r2, PERF_COUNT_STS314#if CHIP_HAS_AUX_PERF_COUNTERS()315.else316.ifc \c_routine, op_handle_aux_perf_interrupt317mfspr r2, AUX_PERF_COUNT_STS318.endif319#endif320.endif321.endif322.endif323.endif324.endif325/* Put function pointer in r0 */326moveli r0, hw2_last(\c_routine)327shl16insli r0, r0, hw1(\c_routine)328{329shl16insli r0, r0, hw0(\c_routine)330j \processing331}332.endif333ENDPROC(intvec_\vecname)334335#ifdef __COLLECT_LINKER_FEEDBACK__336.pushsection .text.intvec_feedback,"ax"337.org (\vecnum << 5)338FEEDBACK_ENTER_EXPLICIT(intvec_\vecname, .intrpt1, 1 << 8)339jrp lr340.popsection341#endif342343.endm344345346/*347* Save the rest of the registers that we didn't save in the actual348* vector itself. We can't use r0-r10 inclusive here.349*/350.macro finish_interrupt_save, function351352/* If it's a syscall, save a proper orig_r0, otherwise just zero. */353PTREGS_PTR(r52, PTREGS_OFFSET_ORIG_R0)354{355.ifc \function,handle_syscall356st r52, r0357.else358st r52, zero359.endif360PTREGS_PTR(r52, PTREGS_OFFSET_TP)361}362st r52, tp363{364mfspr tp, CMPEXCH_VALUE365PTREGS_PTR(r52, PTREGS_OFFSET_CMPEXCH)366}367368/*369* For ordinary syscalls, we save neither caller- nor callee-370* save registers, since the syscall invoker doesn't expect the371* caller-saves to be saved, and the called kernel functions will372* take care of saving the callee-saves for us.373*374* For interrupts we save just the caller-save registers. Saving375* them is required (since the "caller" can't save them). Again,376* the called kernel functions will restore the callee-save377* registers for us appropriately.378*379* On return, we normally restore nothing special for syscalls,380* and just the caller-save registers for interrupts.381*382* However, there are some important caveats to all this:383*384* - We always save a few callee-save registers to give us385* some scratchpad registers to carry across function calls.386*387* - fork/vfork/etc require us to save all the callee-save388* registers, which we do in PTREGS_SYSCALL_ALL_REGS, below.389*390* - We always save r0..r5 and r10 for syscalls, since we need391* to reload them a bit later for the actual kernel call, and392* since we might need them for -ERESTARTNOINTR, etc.393*394* - Before invoking a signal handler, we save the unsaved395* callee-save registers so they are visible to the396* signal handler or any ptracer.397*398* - If the unsaved callee-save registers are modified, we set399* a bit in pt_regs so we know to reload them from pt_regs400* and not just rely on the kernel function unwinding.401* (Done for ptrace register writes and SA_SIGINFO handler.)402*/403{404st r52, tp405PTREGS_PTR(r52, PTREGS_OFFSET_REG(33))406}407wh64 r52 /* cache line 4 */408push_reg r33, r52409push_reg r32, r52410push_reg r31, r52411.ifc \function,handle_syscall412push_reg r30, r52, PTREGS_OFFSET_SYSCALL - PTREGS_OFFSET_REG(30)413push_reg TREG_SYSCALL_NR_NAME, r52, \414PTREGS_OFFSET_REG(5) - PTREGS_OFFSET_SYSCALL415.else416417push_reg r30, r52, PTREGS_OFFSET_REG(29) - PTREGS_OFFSET_REG(30)418wh64 r52 /* cache line 3 */419push_reg r29, r52420push_reg r28, r52421push_reg r27, r52422push_reg r26, r52423push_reg r25, r52424push_reg r24, r52425push_reg r23, r52426push_reg r22, r52427wh64 r52 /* cache line 2 */428push_reg r21, r52429push_reg r20, r52430push_reg r19, r52431push_reg r18, r52432push_reg r17, r52433push_reg r16, r52434push_reg r15, r52435push_reg r14, r52436wh64 r52 /* cache line 1 */437push_reg r13, r52438push_reg r12, r52439push_reg r11, r52440push_reg r10, r52441push_reg r9, r52442push_reg r8, r52443push_reg r7, r52444push_reg r6, r52445446.endif447448push_reg r5, r52449st r52, r4450451/* Load tp with our per-cpu offset. */452#ifdef CONFIG_SMP453{454mfspr r20, SPR_SYSTEM_SAVE_K_0455moveli r21, hw2_last(__per_cpu_offset)456}457{458shl16insli r21, r21, hw1(__per_cpu_offset)459bfextu r20, r20, 0, LOG2_THREAD_SIZE-1460}461shl16insli r21, r21, hw0(__per_cpu_offset)462shl3add r20, r20, r21463ld tp, r20464#else465move tp, zero466#endif467468/*469* If we will be returning to the kernel, we will need to470* reset the interrupt masks to the state they had before.471* Set DISABLE_IRQ in flags iff we came from PL1 with irqs disabled.472*/473mfspr r32, SPR_EX_CONTEXT_K_1474{475andi r32, r32, SPR_EX_CONTEXT_1_1__PL_MASK /* mask off ICS */476PTREGS_PTR(r21, PTREGS_OFFSET_FLAGS)477}478beqzt r32, 1f /* zero if from user space */479IRQS_DISABLED(r32) /* zero if irqs enabled */480#if PT_FLAGS_DISABLE_IRQ != 1481# error Value of IRQS_DISABLED used to set PT_FLAGS_DISABLE_IRQ; fix482#endif4831:484.ifnc \function,handle_syscall485/* Record the fact that we saved the caller-save registers above. */486ori r32, r32, PT_FLAGS_CALLER_SAVES487.endif488st r21, r32489490#ifdef __COLLECT_LINKER_FEEDBACK__491/*492* Notify the feedback routines that we were in the493* appropriate fixed interrupt vector area. Note that we494* still have ICS set at this point, so we can't invoke any495* atomic operations or we will panic. The feedback496* routines internally preserve r0..r10 and r30 up.497*/498.ifnc \function,handle_syscall499shli r20, r1, 5500.else501moveli r20, INT_SWINT_1 << 5502.endif503moveli r21, hw2_last(intvec_feedback)504shl16insli r21, r21, hw1(intvec_feedback)505shl16insli r21, r21, hw0(intvec_feedback)506add r20, r20, r21507jalr r20508509/* And now notify the feedback routines that we are here. */510FEEDBACK_ENTER(\function)511#endif512513/*514* we've captured enough state to the stack (including in515* particular our EX_CONTEXT state) that we can now release516* the interrupt critical section and replace it with our517* standard "interrupts disabled" mask value. This allows518* synchronous interrupts (and profile interrupts) to punch519* through from this point onwards.520*/521.ifc \function,handle_nmi522IRQ_DISABLE_ALL(r20)523.else524IRQ_DISABLE(r20, r21)525.endif526mtspr INTERRUPT_CRITICAL_SECTION, zero527528/*529* Prepare the first 256 stack bytes to be rapidly accessible530* without having to fetch the background data.531*/532addi r52, sp, -64533{534wh64 r52535addi r52, r52, -64536}537{538wh64 r52539addi r52, r52, -64540}541{542wh64 r52543addi r52, r52, -64544}545wh64 r52546547#ifdef CONFIG_TRACE_IRQFLAGS548.ifnc \function,handle_nmi549/*550* We finally have enough state set up to notify the irq551* tracing code that irqs were disabled on entry to the handler.552* The TRACE_IRQS_OFF call clobbers registers r0-r29.553* For syscalls, we already have the register state saved away554* on the stack, so we don't bother to do any register saves here,555* and later we pop the registers back off the kernel stack.556* For interrupt handlers, save r0-r3 in callee-saved registers.557*/558.ifnc \function,handle_syscall559{ move r30, r0; move r31, r1 }560{ move r32, r2; move r33, r3 }561.endif562TRACE_IRQS_OFF563.ifnc \function,handle_syscall564{ move r0, r30; move r1, r31 }565{ move r2, r32; move r3, r33 }566.endif567.endif568#endif569570.endm571572/*573* Redispatch a downcall.574*/575.macro dc_dispatch vecnum, vecname576.org (\vecnum << 8)577intvec_\vecname:578j hv_downcall_dispatch579ENDPROC(intvec_\vecname)580.endm581582/*583* Common code for most interrupts. The C function we're eventually584* going to is in r0, and the faultnum is in r1; the original585* values for those registers are on the stack.586*/587.pushsection .text.handle_interrupt,"ax"588handle_interrupt:589finish_interrupt_save handle_interrupt590591/* Jump to the C routine; it should enable irqs as soon as possible. */592{593jalr r0594PTREGS_PTR(r0, PTREGS_OFFSET_BASE)595}596FEEDBACK_REENTER(handle_interrupt)597{598movei r30, 0 /* not an NMI */599j interrupt_return600}601STD_ENDPROC(handle_interrupt)602603/*604* This routine takes a boolean in r30 indicating if this is an NMI.605* If so, we also expect a boolean in r31 indicating whether to606* re-enable the oprofile interrupts.607*/608STD_ENTRY(interrupt_return)609/* If we're resuming to kernel space, don't check thread flags. */610{611bnez r30, .Lrestore_all /* NMIs don't special-case user-space */612PTREGS_PTR(r29, PTREGS_OFFSET_EX1)613}614ld r29, r29615andi r29, r29, SPR_EX_CONTEXT_1_1__PL_MASK /* mask off ICS */616{617beqzt r29, .Lresume_userspace618PTREGS_PTR(r29, PTREGS_OFFSET_PC)619}620621/* If we're resuming to _cpu_idle_nap, bump PC forward by 8. */622moveli r27, hw2_last(_cpu_idle_nap)623{624ld r28, r29625shl16insli r27, r27, hw1(_cpu_idle_nap)626}627{628shl16insli r27, r27, hw0(_cpu_idle_nap)629}630{631cmpeq r27, r27, r28632}633{634blbc r27, .Lrestore_all635addi r28, r28, 8636}637st r29, r28638j .Lrestore_all639640.Lresume_userspace:641FEEDBACK_REENTER(interrupt_return)642643/*644* Disable interrupts so as to make sure we don't645* miss an interrupt that sets any of the thread flags (like646* need_resched or sigpending) between sampling and the iret.647* Routines like schedule() or do_signal() may re-enable648* interrupts before returning.649*/650IRQ_DISABLE(r20, r21)651TRACE_IRQS_OFF /* Note: clobbers registers r0-r29 */652653/* Get base of stack in r32; note r30/31 are used as arguments here. */654GET_THREAD_INFO(r32)655656657/* Check to see if there is any work to do before returning to user. */658{659addi r29, r32, THREAD_INFO_FLAGS_OFFSET660moveli r1, hw1_last(_TIF_ALLWORK_MASK)661}662{663ld r29, r29664shl16insli r1, r1, hw0(_TIF_ALLWORK_MASK)665}666and r1, r29, r1667beqzt r1, .Lrestore_all668669/*670* Make sure we have all the registers saved for signal671* handling or single-step. Call out to C code to figure out672* exactly what we need to do for each flag bit, then if673* necessary, reload the flags and recheck.674*/675push_extra_callee_saves r0676{677PTREGS_PTR(r0, PTREGS_OFFSET_BASE)678jal do_work_pending679}680bnez r0, .Lresume_userspace681682/*683* In the NMI case we684* omit the call to single_process_check_nohz, which normally checks685* to see if we should start or stop the scheduler tick, because686* we can't call arbitrary Linux code from an NMI context.687* We always call the homecache TLB deferral code to re-trigger688* the deferral mechanism.689*690* The other chunk of responsibility this code has is to reset the691* interrupt masks appropriately to reset irqs and NMIs. We have692* to call TRACE_IRQS_OFF and TRACE_IRQS_ON to support all the693* lockdep-type stuff, but we can't set ICS until afterwards, since694* ICS can only be used in very tight chunks of code to avoid695* tripping over various assertions that it is off.696*/697.Lrestore_all:698PTREGS_PTR(r0, PTREGS_OFFSET_EX1)699{700ld r0, r0701PTREGS_PTR(r32, PTREGS_OFFSET_FLAGS)702}703{704andi r0, r0, SPR_EX_CONTEXT_1_1__PL_MASK705ld r32, r32706}707bnez r0, 1f708j 2f709#if PT_FLAGS_DISABLE_IRQ != 1710# error Assuming PT_FLAGS_DISABLE_IRQ == 1 so we can use blbct below711#endif7121: blbct r32, 2f713IRQ_DISABLE(r20,r21)714TRACE_IRQS_OFF715movei r0, 1716mtspr INTERRUPT_CRITICAL_SECTION, r0717beqzt r30, .Lrestore_regs718j 3f7192: TRACE_IRQS_ON720movei r0, 1721mtspr INTERRUPT_CRITICAL_SECTION, r0722IRQ_ENABLE(r20, r21)723beqzt r30, .Lrestore_regs7243:725726727/*728* We now commit to returning from this interrupt, since we will be729* doing things like setting EX_CONTEXT SPRs and unwinding the stack730* frame. No calls should be made to any other code after this point.731* This code should only be entered with ICS set.732* r32 must still be set to ptregs.flags.733* We launch loads to each cache line separately first, so we can734* get some parallelism out of the memory subsystem.735* We start zeroing caller-saved registers throughout, since736* that will save some cycles if this turns out to be a syscall.737*/738.Lrestore_regs:739FEEDBACK_REENTER(interrupt_return) /* called from elsewhere */740741/*742* Rotate so we have one high bit and one low bit to test.743* - low bit says whether to restore all the callee-saved registers,744* or just r30-r33, and r52 up.745* - high bit (i.e. sign bit) says whether to restore all the746* caller-saved registers, or just r0.747*/748#if PT_FLAGS_CALLER_SAVES != 2 || PT_FLAGS_RESTORE_REGS != 4749# error Rotate trick does not work :-)750#endif751{752rotli r20, r32, 62753PTREGS_PTR(sp, PTREGS_OFFSET_REG(0))754}755756/*757* Load cache lines 0, 4, 6 and 7, in that order, then use758* the last loaded value, which makes it likely that the other759* cache lines have also loaded, at which point we should be760* able to safely read all the remaining words on those cache761* lines without waiting for the memory subsystem.762*/763pop_reg r0, sp, PTREGS_OFFSET_REG(30) - PTREGS_OFFSET_REG(0)764pop_reg r30, sp, PTREGS_OFFSET_REG(52) - PTREGS_OFFSET_REG(30)765pop_reg_zero r52, r3, sp, PTREGS_OFFSET_CMPEXCH - PTREGS_OFFSET_REG(52)766pop_reg_zero r21, r27, sp, PTREGS_OFFSET_EX1 - PTREGS_OFFSET_CMPEXCH767pop_reg_zero lr, r2, sp, PTREGS_OFFSET_PC - PTREGS_OFFSET_EX1768{769mtspr CMPEXCH_VALUE, r21770move r4, zero771}772pop_reg r21, sp, PTREGS_OFFSET_REG(31) - PTREGS_OFFSET_PC773{774mtspr SPR_EX_CONTEXT_K_1, lr775andi lr, lr, SPR_EX_CONTEXT_1_1__PL_MASK /* mask off ICS */776}777{778mtspr SPR_EX_CONTEXT_K_0, r21779move r5, zero780}781782/* Restore callee-saveds that we actually use. */783pop_reg_zero r31, r6784pop_reg_zero r32, r7785pop_reg_zero r33, r8, sp, PTREGS_OFFSET_REG(29) - PTREGS_OFFSET_REG(33)786787/*788* If we modified other callee-saveds, restore them now.789* This is rare, but could be via ptrace or signal handler.790*/791{792move r9, zero793blbs r20, .Lrestore_callees794}795.Lcontinue_restore_regs:796797/* Check if we're returning from a syscall. */798{799move r10, zero800bltzt r20, 1f /* no, so go restore callee-save registers */801}802803/*804* Check if we're returning to userspace.805* Note that if we're not, we don't worry about zeroing everything.806*/807{808addli sp, sp, PTREGS_OFFSET_LR - PTREGS_OFFSET_REG(29)809bnez lr, .Lkernel_return810}811812/*813* On return from syscall, we've restored r0 from pt_regs, but we814* clear the remainder of the caller-saved registers. We could815* restore the syscall arguments, but there's not much point,816* and it ensures user programs aren't trying to use the817* caller-saves if we clear them, as well as avoiding leaking818* kernel pointers into userspace.819*/820pop_reg_zero lr, r11, sp, PTREGS_OFFSET_TP - PTREGS_OFFSET_LR821pop_reg_zero tp, r12, sp, PTREGS_OFFSET_SP - PTREGS_OFFSET_TP822{823ld sp, sp824move r13, zero825move r14, zero826}827{ move r15, zero; move r16, zero }828{ move r17, zero; move r18, zero }829{ move r19, zero; move r20, zero }830{ move r21, zero; move r22, zero }831{ move r23, zero; move r24, zero }832{ move r25, zero; move r26, zero }833834/* Set r1 to errno if we are returning an error, otherwise zero. */835{836moveli r29, 4096837sub r1, zero, r0838}839{840move r28, zero841cmpltu r29, r1, r29842}843{844mnz r1, r29, r1845move r29, zero846}847iret848849/*850* Not a syscall, so restore caller-saved registers.851* First kick off loads for cache lines 1-3, which we're touching852* for the first time here.853*/854.align 648551: pop_reg r29, sp, PTREGS_OFFSET_REG(21) - PTREGS_OFFSET_REG(29)856pop_reg r21, sp, PTREGS_OFFSET_REG(13) - PTREGS_OFFSET_REG(21)857pop_reg r13, sp, PTREGS_OFFSET_REG(1) - PTREGS_OFFSET_REG(13)858pop_reg r1859pop_reg r2860pop_reg r3861pop_reg r4862pop_reg r5863pop_reg r6864pop_reg r7865pop_reg r8866pop_reg r9867pop_reg r10868pop_reg r11869pop_reg r12, sp, 16870/* r13 already restored above */871pop_reg r14872pop_reg r15873pop_reg r16874pop_reg r17875pop_reg r18876pop_reg r19877pop_reg r20, sp, 16878/* r21 already restored above */879pop_reg r22880pop_reg r23881pop_reg r24882pop_reg r25883pop_reg r26884pop_reg r27885pop_reg r28, sp, PTREGS_OFFSET_LR - PTREGS_OFFSET_REG(28)886/* r29 already restored above */887bnez lr, .Lkernel_return888pop_reg lr, sp, PTREGS_OFFSET_TP - PTREGS_OFFSET_LR889pop_reg tp, sp, PTREGS_OFFSET_SP - PTREGS_OFFSET_TP890ld sp, sp891iret892893/*894* We can't restore tp when in kernel mode, since a thread might895* have migrated from another cpu and brought a stale tp value.896*/897.Lkernel_return:898pop_reg lr, sp, PTREGS_OFFSET_SP - PTREGS_OFFSET_LR899ld sp, sp900iret901902/* Restore callee-saved registers from r34 to r51. */903.Lrestore_callees:904addli sp, sp, PTREGS_OFFSET_REG(34) - PTREGS_OFFSET_REG(29)905pop_reg r34906pop_reg r35907pop_reg r36908pop_reg r37909pop_reg r38910pop_reg r39911pop_reg r40912pop_reg r41913pop_reg r42914pop_reg r43915pop_reg r44916pop_reg r45917pop_reg r46918pop_reg r47919pop_reg r48920pop_reg r49921pop_reg r50922pop_reg r51, sp, PTREGS_OFFSET_REG(29) - PTREGS_OFFSET_REG(51)923j .Lcontinue_restore_regs924STD_ENDPROC(interrupt_return)925926/*927* "NMI" interrupts mask ALL interrupts before calling the928* handler, and don't check thread flags, etc., on the way929* back out. In general, the only things we do here for NMIs930* are register save/restore and dataplane kernel-TLB management.931* We don't (for example) deal with start/stop of the sched tick.932*/933.pushsection .text.handle_nmi,"ax"934handle_nmi:935finish_interrupt_save handle_nmi936{937jalr r0938PTREGS_PTR(r0, PTREGS_OFFSET_BASE)939}940FEEDBACK_REENTER(handle_nmi)941{942movei r30, 1943move r31, r0944}945j interrupt_return946STD_ENDPROC(handle_nmi)947948/*949* Parallel code for syscalls to handle_interrupt.950*/951.pushsection .text.handle_syscall,"ax"952handle_syscall:953finish_interrupt_save handle_syscall954955/* Enable irqs. */956TRACE_IRQS_ON957IRQ_ENABLE(r20, r21)958959/* Bump the counter for syscalls made on this tile. */960moveli r20, hw2_last(irq_stat + IRQ_CPUSTAT_SYSCALL_COUNT_OFFSET)961shl16insli r20, r20, hw1(irq_stat + IRQ_CPUSTAT_SYSCALL_COUNT_OFFSET)962shl16insli r20, r20, hw0(irq_stat + IRQ_CPUSTAT_SYSCALL_COUNT_OFFSET)963add r20, r20, tp964ld4s r21, r20965addi r21, r21, 1966st4 r20, r21967968/* Trace syscalls, if requested. */969GET_THREAD_INFO(r31)970addi r31, r31, THREAD_INFO_FLAGS_OFFSET971ld r30, r31972andi r30, r30, _TIF_SYSCALL_TRACE973{974addi r30, r31, THREAD_INFO_STATUS_OFFSET - THREAD_INFO_FLAGS_OFFSET975beqzt r30, .Lrestore_syscall_regs976}977jal do_syscall_trace978FEEDBACK_REENTER(handle_syscall)979980/*981* We always reload our registers from the stack at this982* point. They might be valid, if we didn't build with983* TRACE_IRQFLAGS, and this isn't a dataplane tile, and we're not984* doing syscall tracing, but there are enough cases now that it985* seems simplest just to do the reload unconditionally.986*/987.Lrestore_syscall_regs:988{989ld r30, r30990PTREGS_PTR(r11, PTREGS_OFFSET_REG(0))991}992pop_reg r0, r11993pop_reg r1, r11994pop_reg r2, r11995pop_reg r3, r11996pop_reg r4, r11997pop_reg r5, r11, PTREGS_OFFSET_SYSCALL - PTREGS_OFFSET_REG(5)998{999ld TREG_SYSCALL_NR_NAME, r111000moveli r21, __NR_syscalls1001}10021003/* Ensure that the syscall number is within the legal range. */1004{1005moveli r20, hw2(sys_call_table)1006blbs r30, .Lcompat_syscall1007}1008{1009cmpltu r21, TREG_SYSCALL_NR_NAME, r211010shl16insli r20, r20, hw1(sys_call_table)1011}1012{1013blbc r21, .Linvalid_syscall1014shl16insli r20, r20, hw0(sys_call_table)1015}1016.Lload_syscall_pointer:1017shl3add r20, TREG_SYSCALL_NR_NAME, r201018ld r20, r2010191020/* Jump to syscall handler. */1021jalr r201022.Lhandle_syscall_link: /* value of "lr" after "jalr r20" above */10231024/*1025* Write our r0 onto the stack so it gets restored instead1026* of whatever the user had there before.1027* In compat mode, sign-extend r0 before storing it.1028*/1029{1030PTREGS_PTR(r29, PTREGS_OFFSET_REG(0))1031blbct r30, 1f1032}1033addxi r0, r0, 010341: st r29, r010351036.Lsyscall_sigreturn_skip:1037FEEDBACK_REENTER(handle_syscall)10381039/* Do syscall trace again, if requested. */1040ld r30, r311041andi r30, r30, _TIF_SYSCALL_TRACE1042beqzt r30, 1f1043jal do_syscall_trace1044FEEDBACK_REENTER(handle_syscall)10451: j .Lresume_userspace /* jump into middle of interrupt_return */10461047.Lcompat_syscall:1048/*1049* Load the base of the compat syscall table in r20, and1050* range-check the syscall number (duplicated from 64-bit path).1051* Sign-extend all the user's passed arguments to make them consistent.1052* Also save the original "r(n)" values away in "r(11+n)" in1053* case the syscall table entry wants to validate them.1054*/1055moveli r20, hw2(compat_sys_call_table)1056{1057cmpltu r21, TREG_SYSCALL_NR_NAME, r211058shl16insli r20, r20, hw1(compat_sys_call_table)1059}1060{1061blbc r21, .Linvalid_syscall1062shl16insli r20, r20, hw0(compat_sys_call_table)1063}1064{ move r11, r0; addxi r0, r0, 0 }1065{ move r12, r1; addxi r1, r1, 0 }1066{ move r13, r2; addxi r2, r2, 0 }1067{ move r14, r3; addxi r3, r3, 0 }1068{ move r15, r4; addxi r4, r4, 0 }1069{ move r16, r5; addxi r5, r5, 0 }1070j .Lload_syscall_pointer10711072.Linvalid_syscall:1073/* Report an invalid syscall back to the user program */1074{1075PTREGS_PTR(r29, PTREGS_OFFSET_REG(0))1076movei r28, -ENOSYS1077}1078st r29, r281079j .Lresume_userspace /* jump into middle of interrupt_return */1080STD_ENDPROC(handle_syscall)10811082/* Return the address for oprofile to suppress in backtraces. */1083STD_ENTRY_SECTION(handle_syscall_link_address, .text.handle_syscall)1084lnk r01085{1086addli r0, r0, .Lhandle_syscall_link - .1087jrp lr1088}1089STD_ENDPROC(handle_syscall_link_address)10901091STD_ENTRY(ret_from_fork)1092jal sim_notify_fork1093jal schedule_tail1094FEEDBACK_REENTER(ret_from_fork)1095j .Lresume_userspace1096STD_ENDPROC(ret_from_fork)10971098/* Various stub interrupt handlers and syscall handlers */10991100STD_ENTRY_LOCAL(_kernel_double_fault)1101mfspr r1, SPR_EX_CONTEXT_K_01102move r2, lr1103move r3, sp1104move r4, r521105addi sp, sp, -C_ABI_SAVE_AREA_SIZE1106j kernel_double_fault1107STD_ENDPROC(_kernel_double_fault)11081109STD_ENTRY_LOCAL(bad_intr)1110mfspr r2, SPR_EX_CONTEXT_K_01111panic "Unhandled interrupt %#x: PC %#lx"1112STD_ENDPROC(bad_intr)11131114/* Put address of pt_regs in reg and jump. */1115#define PTREGS_SYSCALL(x, reg) \1116STD_ENTRY(_##x); \1117{ \1118PTREGS_PTR(reg, PTREGS_OFFSET_BASE); \1119j x \1120}; \1121STD_ENDPROC(_##x)11221123/*1124* Special-case sigreturn to not write r0 to the stack on return.1125* This is technically more efficient, but it also avoids difficulties1126* in the 64-bit OS when handling 32-bit compat code, since we must not1127* sign-extend r0 for the sigreturn return-value case.1128*/1129#define PTREGS_SYSCALL_SIGRETURN(x, reg) \1130STD_ENTRY(_##x); \1131addli lr, lr, .Lsyscall_sigreturn_skip - .Lhandle_syscall_link; \1132{ \1133PTREGS_PTR(reg, PTREGS_OFFSET_BASE); \1134j x \1135}; \1136STD_ENDPROC(_##x)11371138PTREGS_SYSCALL(sys_execve, r3)1139PTREGS_SYSCALL(sys_sigaltstack, r2)1140PTREGS_SYSCALL_SIGRETURN(sys_rt_sigreturn, r0)1141#ifdef CONFIG_COMPAT1142PTREGS_SYSCALL(compat_sys_execve, r3)1143PTREGS_SYSCALL(compat_sys_sigaltstack, r2)1144PTREGS_SYSCALL_SIGRETURN(compat_sys_rt_sigreturn, r0)1145#endif11461147/* Save additional callee-saves to pt_regs, put address in r4 and jump. */1148STD_ENTRY(_sys_clone)1149push_extra_callee_saves r41150j sys_clone1151STD_ENDPROC(_sys_clone)11521153/* The single-step support may need to read all the registers. */1154int_unalign:1155push_extra_callee_saves r01156j do_trap11571158/* Include .intrpt1 array of interrupt vectors */1159.section ".intrpt1", "ax"11601161#define op_handle_perf_interrupt bad_intr1162#define op_handle_aux_perf_interrupt bad_intr11631164#ifndef CONFIG_HARDWALL1165#define do_hardwall_trap bad_intr1166#endif11671168int_hand INT_MEM_ERROR, MEM_ERROR, bad_intr1169int_hand INT_SINGLE_STEP_3, SINGLE_STEP_3, bad_intr1170#if CONFIG_KERNEL_PL == 21171int_hand INT_SINGLE_STEP_2, SINGLE_STEP_2, gx_singlestep_handle1172int_hand INT_SINGLE_STEP_1, SINGLE_STEP_1, bad_intr1173#else1174int_hand INT_SINGLE_STEP_2, SINGLE_STEP_2, bad_intr1175int_hand INT_SINGLE_STEP_1, SINGLE_STEP_1, gx_singlestep_handle1176#endif1177int_hand INT_SINGLE_STEP_0, SINGLE_STEP_0, bad_intr1178int_hand INT_IDN_COMPLETE, IDN_COMPLETE, bad_intr1179int_hand INT_UDN_COMPLETE, UDN_COMPLETE, bad_intr1180int_hand INT_ITLB_MISS, ITLB_MISS, do_page_fault1181int_hand INT_ILL, ILL, do_trap1182int_hand INT_GPV, GPV, do_trap1183int_hand INT_IDN_ACCESS, IDN_ACCESS, do_trap1184int_hand INT_UDN_ACCESS, UDN_ACCESS, do_trap1185int_hand INT_SWINT_3, SWINT_3, do_trap1186int_hand INT_SWINT_2, SWINT_2, do_trap1187int_hand INT_SWINT_1, SWINT_1, SYSCALL, handle_syscall1188int_hand INT_SWINT_0, SWINT_0, do_trap1189int_hand INT_ILL_TRANS, ILL_TRANS, do_trap1190int_hand INT_UNALIGN_DATA, UNALIGN_DATA, int_unalign1191int_hand INT_DTLB_MISS, DTLB_MISS, do_page_fault1192int_hand INT_DTLB_ACCESS, DTLB_ACCESS, do_page_fault1193int_hand INT_IDN_FIREWALL, IDN_FIREWALL, bad_intr1194int_hand INT_UDN_FIREWALL, UDN_FIREWALL, do_hardwall_trap1195int_hand INT_TILE_TIMER, TILE_TIMER, do_timer_interrupt1196int_hand INT_IDN_TIMER, IDN_TIMER, bad_intr1197int_hand INT_UDN_TIMER, UDN_TIMER, bad_intr1198int_hand INT_IDN_AVAIL, IDN_AVAIL, bad_intr1199int_hand INT_UDN_AVAIL, UDN_AVAIL, bad_intr1200int_hand INT_IPI_3, IPI_3, bad_intr1201#if CONFIG_KERNEL_PL == 21202int_hand INT_IPI_2, IPI_2, tile_dev_intr1203int_hand INT_IPI_1, IPI_1, bad_intr1204#else1205int_hand INT_IPI_2, IPI_2, bad_intr1206int_hand INT_IPI_1, IPI_1, tile_dev_intr1207#endif1208int_hand INT_IPI_0, IPI_0, bad_intr1209int_hand INT_PERF_COUNT, PERF_COUNT, \1210op_handle_perf_interrupt, handle_nmi1211int_hand INT_AUX_PERF_COUNT, AUX_PERF_COUNT, \1212op_handle_perf_interrupt, handle_nmi1213int_hand INT_INTCTRL_3, INTCTRL_3, bad_intr1214#if CONFIG_KERNEL_PL == 21215dc_dispatch INT_INTCTRL_2, INTCTRL_21216int_hand INT_INTCTRL_1, INTCTRL_1, bad_intr1217#else1218int_hand INT_INTCTRL_2, INTCTRL_2, bad_intr1219dc_dispatch INT_INTCTRL_1, INTCTRL_11220#endif1221int_hand INT_INTCTRL_0, INTCTRL_0, bad_intr1222int_hand INT_MESSAGE_RCV_DWNCL, MESSAGE_RCV_DWNCL, \1223hv_message_intr1224int_hand INT_DEV_INTR_DWNCL, DEV_INTR_DWNCL, bad_intr1225int_hand INT_I_ASID, I_ASID, bad_intr1226int_hand INT_D_ASID, D_ASID, bad_intr1227int_hand INT_DOUBLE_FAULT, DOUBLE_FAULT, do_trap12281229/* Synthetic interrupt delivered only by the simulator */1230int_hand INT_BREAKPOINT, BREAKPOINT, do_breakpoint123112321233