/*-1* Copyright (C) 2006-2009 Semihalf, Rafal Jaworowski <[email protected]>2* Copyright (C) 2006 Semihalf, Marian Balakowicz <[email protected]>3* Copyright (C) 2006 Juniper Networks, Inc.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14* 3. The name of the author may not be used to endorse or promote products15* derived from this software without specific prior written permission.16*17* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR18* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES19* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN20* NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED22* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR23* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF24* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING25* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS26* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.27*/28/*-29* Copyright (C) 1995, 1996 Wolfgang Solfrank.30* Copyright (C) 1995, 1996 TooLs GmbH.31* All rights reserved.32*33* Redistribution and use in source and binary forms, with or without34* modification, are permitted provided that the following conditions35* are met:36* 1. Redistributions of source code must retain the above copyright37* notice, this list of conditions and the following disclaimer.38* 2. Redistributions in binary form must reproduce the above copyright39* notice, this list of conditions and the following disclaimer in the40* documentation and/or other materials provided with the distribution.41* 3. All advertising materials mentioning features or use of this software42* must display the following acknowledgement:43* This product includes software developed by TooLs GmbH.44* 4. The name of TooLs GmbH may not be used to endorse or promote products45* derived from this software without specific prior written permission.46*47* THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR48* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES49* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.50* IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,51* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,52* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;53* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,54* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR55* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF56* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.57*58* from: $NetBSD: trap_subr.S,v 1.20 2002/04/22 23:20:08 kleink Exp $59*/6061/*62* NOTICE: This is not a standalone file. to use it, #include it in63* your port's locore.S, like so:64*65* #include <powerpc/booke/trap_subr.S>66*/6768/*69* SPRG usage notes70*71* SPRG0 - pcpu pointer72* SPRG1 - all interrupts except TLB miss, critical, machine check73* SPRG2 - critical74* SPRG3 - machine check75* SPRG4-6 - scratch76*77*/7879/* Get the per-CPU data structure */80#define GET_CPUINFO(r) mfsprg0 r8182#define RES_GRANULE 6483#define RES_LOCK 0 /* offset to the 'lock' word */84#ifdef __powerpc64__85#define RES_RECURSE 8 /* offset to the 'recurse' word */86#else87#define RES_RECURSE 4 /* offset to the 'recurse' word */88#endif8990/*91* Standard interrupt prolog92*93* sprg_sp - SPRG{1-3} reg used to temporarily store the SP94* savearea - temp save area (pc_{tempsave, disisave, critsave, mchksave})95* isrr0-1 - save restore registers with CPU state at interrupt time (may be96* SRR0-1, CSRR0-1, MCSRR0-197*98* 1. saves in the given savearea:99* - R30-31100* - DEAR, ESR101* - xSRR0-1102*103* 2. saves CR -> R30104*105* 3. switches to kstack if needed106*107* 4. notes:108* - R31 can be used as scratch register until a new frame is laid on109* the stack with FRAME_SETUP110*111* - potential TLB miss: NO. Saveareas are always acessible via TLB1112* permanent entries, and within this prolog we do not dereference any113* locations potentially not in the TLB114*/115#define STANDARD_PROLOG(sprg_sp, savearea, isrr0, isrr1) \116mtspr sprg_sp, %r1; /* Save SP */ \117GET_CPUINFO(%r1); /* Per-cpu structure */ \118STORE %r30, (savearea+CPUSAVE_R30)(%r1); \119STORE %r31, (savearea+CPUSAVE_R31)(%r1); \120mfspr %r30, SPR_DEAR; \121mfspr %r31, SPR_ESR; \122STORE %r30, (savearea+CPUSAVE_BOOKE_DEAR)(%r1); \123STORE %r31, (savearea+CPUSAVE_BOOKE_ESR)(%r1); \124mfspr %r30, isrr0; \125mfspr %r31, isrr1; /* MSR at interrupt time */ \126STORE %r30, (savearea+CPUSAVE_SRR0)(%r1); \127STORE %r31, (savearea+CPUSAVE_SRR1)(%r1); \128isync; \129mfspr %r1, sprg_sp; /* Restore SP */ \130mfcr %r30; /* Save CR */ \131/* switch to per-thread kstack if intr taken in user mode */ \132mtcr %r31; /* MSR at interrupt time */ \133bf 17, 1f; \134GET_CPUINFO(%r1); /* Per-cpu structure */ \135LOAD %r1, PC_CURPCB(%r1); /* Per-thread kernel stack */ \1361:137138#define STANDARD_CRIT_PROLOG(sprg_sp, savearea, isrr0, isrr1) \139mtspr sprg_sp, %r1; /* Save SP */ \140GET_CPUINFO(%r1); /* Per-cpu structure */ \141STORE %r30, (savearea+CPUSAVE_R30)(%r1); \142STORE %r31, (savearea+CPUSAVE_R31)(%r1); \143mfspr %r30, SPR_DEAR; \144mfspr %r31, SPR_ESR; \145STORE %r30, (savearea+CPUSAVE_BOOKE_DEAR)(%r1); \146STORE %r31, (savearea+CPUSAVE_BOOKE_ESR)(%r1); \147mfspr %r30, isrr0; \148mfspr %r31, isrr1; /* MSR at interrupt time */ \149STORE %r30, (savearea+CPUSAVE_SRR0)(%r1); \150STORE %r31, (savearea+CPUSAVE_SRR1)(%r1); \151mfspr %r30, SPR_SRR0; \152mfspr %r31, SPR_SRR1; /* MSR at interrupt time */ \153STORE %r30, (savearea+BOOKE_CRITSAVE_SRR0)(%r1); \154STORE %r31, (savearea+BOOKE_CRITSAVE_SRR1)(%r1); \155isync; \156mfspr %r1, sprg_sp; /* Restore SP */ \157mfcr %r30; /* Save CR */ \158/* switch to per-thread kstack if intr taken in user mode */ \159mtcr %r31; /* MSR at interrupt time */ \160bf 17, 1f; \161GET_CPUINFO(%r1); /* Per-cpu structure */ \162LOAD %r1, PC_CURPCB(%r1); /* Per-thread kernel stack */ \1631:164165/*166* FRAME_SETUP assumes:167* SPRG{1-3} SP at the time interrupt occurred168* savearea r30-r31, DEAR, ESR, xSRR0-1169* r30 CR170* r31 scratch171* r1 kernel stack172*173* sprg_sp - SPRG reg containing SP at the time interrupt occurred174* savearea - temp save175* exc - exception number (EXC_xxx)176*177* 1. sets a new frame178* 2. saves in the frame:179* - R0, R1 (SP at the time of interrupt), R2, LR, CR180* - R3-31 (R30-31 first restored from savearea)181* - XER, CTR, DEAR, ESR (from savearea), xSRR0-1182*183* Notes:184* - potential TLB miss: YES, since we make dereferences to kstack, which185* can happen not covered (we can have up to two DTLB misses if fortunate186* enough i.e. when kstack crosses page boundary and both pages are187* untranslated)188*/189#ifdef __powerpc64__190#define SAVE_REGS(r) \191std %r3, FRAME_3+CALLSIZE(r); \192std %r4, FRAME_4+CALLSIZE(r); \193std %r5, FRAME_5+CALLSIZE(r); \194std %r6, FRAME_6+CALLSIZE(r); \195std %r7, FRAME_7+CALLSIZE(r); \196std %r8, FRAME_8+CALLSIZE(r); \197std %r9, FRAME_9+CALLSIZE(r); \198std %r10, FRAME_10+CALLSIZE(r); \199std %r11, FRAME_11+CALLSIZE(r); \200std %r12, FRAME_12+CALLSIZE(r); \201std %r13, FRAME_13+CALLSIZE(r); \202std %r14, FRAME_14+CALLSIZE(r); \203std %r15, FRAME_15+CALLSIZE(r); \204std %r16, FRAME_16+CALLSIZE(r); \205std %r17, FRAME_17+CALLSIZE(r); \206std %r18, FRAME_18+CALLSIZE(r); \207std %r19, FRAME_19+CALLSIZE(r); \208std %r20, FRAME_20+CALLSIZE(r); \209std %r21, FRAME_21+CALLSIZE(r); \210std %r22, FRAME_22+CALLSIZE(r); \211std %r23, FRAME_23+CALLSIZE(r); \212std %r24, FRAME_24+CALLSIZE(r); \213std %r25, FRAME_25+CALLSIZE(r); \214std %r26, FRAME_26+CALLSIZE(r); \215std %r27, FRAME_27+CALLSIZE(r); \216std %r28, FRAME_28+CALLSIZE(r); \217std %r29, FRAME_29+CALLSIZE(r); \218std %r30, FRAME_30+CALLSIZE(r); \219std %r31, FRAME_31+CALLSIZE(r)220#define LD_REGS(r) \221ld %r3, FRAME_3+CALLSIZE(r); \222ld %r4, FRAME_4+CALLSIZE(r); \223ld %r5, FRAME_5+CALLSIZE(r); \224ld %r6, FRAME_6+CALLSIZE(r); \225ld %r7, FRAME_7+CALLSIZE(r); \226ld %r8, FRAME_8+CALLSIZE(r); \227ld %r9, FRAME_9+CALLSIZE(r); \228ld %r10, FRAME_10+CALLSIZE(r); \229ld %r11, FRAME_11+CALLSIZE(r); \230ld %r12, FRAME_12+CALLSIZE(r); \231ld %r13, FRAME_13+CALLSIZE(r); \232ld %r14, FRAME_14+CALLSIZE(r); \233ld %r15, FRAME_15+CALLSIZE(r); \234ld %r16, FRAME_16+CALLSIZE(r); \235ld %r17, FRAME_17+CALLSIZE(r); \236ld %r18, FRAME_18+CALLSIZE(r); \237ld %r19, FRAME_19+CALLSIZE(r); \238ld %r20, FRAME_20+CALLSIZE(r); \239ld %r21, FRAME_21+CALLSIZE(r); \240ld %r22, FRAME_22+CALLSIZE(r); \241ld %r23, FRAME_23+CALLSIZE(r); \242ld %r24, FRAME_24+CALLSIZE(r); \243ld %r25, FRAME_25+CALLSIZE(r); \244ld %r26, FRAME_26+CALLSIZE(r); \245ld %r27, FRAME_27+CALLSIZE(r); \246ld %r28, FRAME_28+CALLSIZE(r); \247ld %r29, FRAME_29+CALLSIZE(r); \248ld %r30, FRAME_30+CALLSIZE(r); \249ld %r31, FRAME_31+CALLSIZE(r)250#else251#define SAVE_REGS(r) \252stmw %r3, FRAME_3+CALLSIZE(r)253#define LD_REGS(r) \254lmw %r3, FRAME_3+CALLSIZE(r)255#endif256#define FRAME_SETUP(sprg_sp, savearea, exc) \257mfspr %r31, sprg_sp; /* get saved SP */ \258/* establish a new stack frame and put everything on it */ \259STU %r31, -(FRAMELEN+REDZONE)(%r1); \260STORE %r0, FRAME_0+CALLSIZE(%r1); /* save r0 in the trapframe */ \261STORE %r31, FRAME_1+CALLSIZE(%r1); /* save SP " " */ \262STORE %r2, FRAME_2+CALLSIZE(%r1); /* save r2 " " */ \263mflr %r31; \264STORE %r31, FRAME_LR+CALLSIZE(%r1); /* save LR " " */ \265STORE %r30, FRAME_CR+CALLSIZE(%r1); /* save CR " " */ \266GET_CPUINFO(%r2); \267LOAD %r30, (savearea+CPUSAVE_R30)(%r2); /* get saved r30 */ \268LOAD %r31, (savearea+CPUSAVE_R31)(%r2); /* get saved r31 */ \269/* save R3-31 */ \270SAVE_REGS(%r1); \271/* save DEAR, ESR */ \272LOAD %r28, (savearea+CPUSAVE_BOOKE_DEAR)(%r2); \273LOAD %r29, (savearea+CPUSAVE_BOOKE_ESR)(%r2); \274STORE %r28, FRAME_BOOKE_DEAR+CALLSIZE(%r1); \275STORE %r29, FRAME_BOOKE_ESR+CALLSIZE(%r1); \276/* save XER, CTR, exc number */ \277mfxer %r3; \278mfctr %r4; \279STORE %r3, FRAME_XER+CALLSIZE(%r1); \280STORE %r4, FRAME_CTR+CALLSIZE(%r1); \281li %r5, exc; \282STORE %r5, FRAME_EXC+CALLSIZE(%r1); \283/* save DBCR0 */ \284mfspr %r3, SPR_DBCR0; \285STORE %r3, FRAME_BOOKE_DBCR0+CALLSIZE(%r1); \286/* save xSSR0-1 */ \287LOAD %r30, (savearea+CPUSAVE_SRR0)(%r2); \288LOAD %r31, (savearea+CPUSAVE_SRR1)(%r2); \289STORE %r30, FRAME_SRR0+CALLSIZE(%r1); \290STORE %r31, FRAME_SRR1+CALLSIZE(%r1); \291LOAD THREAD_REG, PC_CURTHREAD(%r2); \292293/*294*295* isrr0-1 - save restore registers to restore CPU state to (may be296* SRR0-1, CSRR0-1, MCSRR0-1297*298* Notes:299* - potential TLB miss: YES. The deref'd kstack may be not covered300*/301#define FRAME_LEAVE(isrr0, isrr1) \302wrteei 0; \303/* restore CTR, XER, LR, CR */ \304LOAD %r4, FRAME_CTR+CALLSIZE(%r1); \305LOAD %r5, FRAME_XER+CALLSIZE(%r1); \306LOAD %r6, FRAME_LR+CALLSIZE(%r1); \307LOAD %r7, FRAME_CR+CALLSIZE(%r1); \308mtctr %r4; \309mtxer %r5; \310mtlr %r6; \311mtcr %r7; \312/* restore DBCR0 */ \313LOAD %r4, FRAME_BOOKE_DBCR0+CALLSIZE(%r1); \314mtspr SPR_DBCR0, %r4; \315/* restore xSRR0-1 */ \316LOAD %r30, FRAME_SRR0+CALLSIZE(%r1); \317LOAD %r31, FRAME_SRR1+CALLSIZE(%r1); \318mtspr isrr0, %r30; \319mtspr isrr1, %r31; \320/* restore R2-31, SP */ \321LD_REGS(%r1); \322LOAD %r2, FRAME_2+CALLSIZE(%r1); \323LOAD %r0, FRAME_0+CALLSIZE(%r1); \324LOAD %r1, FRAME_1+CALLSIZE(%r1); \325isync326327/*328* TLB miss prolog329*330* saves LR, CR, SRR0-1, R20-31 in the TLBSAVE area331*332* Notes:333* - potential TLB miss: NO. It is crucial that we do not generate a TLB334* miss within the TLB prolog itself!335* - TLBSAVE is always translated336*/337#ifdef __powerpc64__338#define TLB_SAVE_REGS(br) \339std %r20, (TLBSAVE_BOOKE_R20)(br); \340std %r21, (TLBSAVE_BOOKE_R21)(br); \341std %r22, (TLBSAVE_BOOKE_R22)(br); \342std %r23, (TLBSAVE_BOOKE_R23)(br); \343std %r24, (TLBSAVE_BOOKE_R24)(br); \344std %r25, (TLBSAVE_BOOKE_R25)(br); \345std %r26, (TLBSAVE_BOOKE_R26)(br); \346std %r27, (TLBSAVE_BOOKE_R27)(br); \347std %r28, (TLBSAVE_BOOKE_R28)(br); \348std %r29, (TLBSAVE_BOOKE_R29)(br); \349std %r30, (TLBSAVE_BOOKE_R30)(br); \350std %r31, (TLBSAVE_BOOKE_R31)(br);351#define TLB_RESTORE_REGS(br) \352ld %r20, (TLBSAVE_BOOKE_R20)(br); \353ld %r21, (TLBSAVE_BOOKE_R21)(br); \354ld %r22, (TLBSAVE_BOOKE_R22)(br); \355ld %r23, (TLBSAVE_BOOKE_R23)(br); \356ld %r24, (TLBSAVE_BOOKE_R24)(br); \357ld %r25, (TLBSAVE_BOOKE_R25)(br); \358ld %r26, (TLBSAVE_BOOKE_R26)(br); \359ld %r27, (TLBSAVE_BOOKE_R27)(br); \360ld %r28, (TLBSAVE_BOOKE_R28)(br); \361ld %r29, (TLBSAVE_BOOKE_R29)(br); \362ld %r30, (TLBSAVE_BOOKE_R30)(br); \363ld %r31, (TLBSAVE_BOOKE_R31)(br);364#define TLB_NEST(outr,inr) \365rlwinm outr, inr, 7, 23, 24; /* 8 x TLBSAVE_LEN */366#else367#define TLB_SAVE_REGS(br) \368stmw %r20, TLBSAVE_BOOKE_R20(br)369#define TLB_RESTORE_REGS(br) \370lmw %r20, TLBSAVE_BOOKE_R20(br)371#define TLB_NEST(outr,inr) \372rlwinm outr, inr, 6, 24, 25; /* 4 x TLBSAVE_LEN */373#endif374#define TLB_PROLOG \375mtspr SPR_SPRG4, %r1; /* Save SP */ \376mtspr SPR_SPRG5, %r28; \377mtspr SPR_SPRG6, %r29; \378/* calculate TLB nesting level and TLBSAVE instance address */ \379GET_CPUINFO(%r1); /* Per-cpu structure */ \380LOAD %r28, PC_BOOKE_TLB_LEVEL(%r1); \381TLB_NEST(%r29,%r28); \382addi %r28, %r28, 1; \383STORE %r28, PC_BOOKE_TLB_LEVEL(%r1); \384addi %r29, %r29, PC_BOOKE_TLBSAVE@l; \385add %r1, %r1, %r29; /* current TLBSAVE ptr */ \386\387/* save R20-31 */ \388mfspr %r28, SPR_SPRG5; \389mfspr %r29, SPR_SPRG6; \390TLB_SAVE_REGS(%r1); \391/* save LR, CR */ \392mflr %r30; \393mfcr %r31; \394STORE %r30, (TLBSAVE_BOOKE_LR)(%r1); \395STORE %r31, (TLBSAVE_BOOKE_CR)(%r1); \396/* save SRR0-1 */ \397mfsrr0 %r30; /* execution addr at interrupt time */ \398mfsrr1 %r31; /* MSR at interrupt time*/ \399STORE %r30, (TLBSAVE_BOOKE_SRR0)(%r1); /* save SRR0 */ \400STORE %r31, (TLBSAVE_BOOKE_SRR1)(%r1); /* save SRR1 */ \401isync; \402mfspr %r1, SPR_SPRG4403404/*405* restores LR, CR, SRR0-1, R20-31 from the TLBSAVE area406*407* same notes as for the TLB_PROLOG408*/409#define TLB_RESTORE \410mtspr SPR_SPRG4, %r1; /* Save SP */ \411GET_CPUINFO(%r1); /* Per-cpu structure */ \412/* calculate TLB nesting level and TLBSAVE instance addr */ \413LOAD %r28, PC_BOOKE_TLB_LEVEL(%r1); \414subi %r28, %r28, 1; \415STORE %r28, PC_BOOKE_TLB_LEVEL(%r1); \416TLB_NEST(%r29,%r28); \417addi %r29, %r29, PC_BOOKE_TLBSAVE@l; \418add %r1, %r1, %r29; \419\420/* restore LR, CR */ \421LOAD %r30, (TLBSAVE_BOOKE_LR)(%r1); \422LOAD %r31, (TLBSAVE_BOOKE_CR)(%r1); \423mtlr %r30; \424mtcr %r31; \425/* restore SRR0-1 */ \426LOAD %r30, (TLBSAVE_BOOKE_SRR0)(%r1); \427LOAD %r31, (TLBSAVE_BOOKE_SRR1)(%r1); \428mtsrr0 %r30; \429mtsrr1 %r31; \430/* restore R20-31 */ \431TLB_RESTORE_REGS(%r1); \432mfspr %r1, SPR_SPRG4433434#ifdef SMP435#define TLB_LOCK \436GET_CPUINFO(%r20); \437LOAD %r21, PC_CURTHREAD(%r20); \438LOAD %r22, PC_BOOKE_TLB_LOCK(%r20); \439\4401: LOADX %r23, 0, %r22; \441CMPI %r23, TLB_UNLOCKED; \442beq 2f; \443\444/* check if this is recursion */ \445CMPL cr0, %r21, %r23; \446bne- 1b; \447\4482: /* try to acquire lock */ \449STOREX %r21, 0, %r22; \450bne- 1b; \451\452/* got it, update recursion counter */ \453lwz %r21, RES_RECURSE(%r22); \454addi %r21, %r21, 1; \455stw %r21, RES_RECURSE(%r22); \456isync; \457msync458459#define TLB_UNLOCK \460GET_CPUINFO(%r20); \461LOAD %r21, PC_CURTHREAD(%r20); \462LOAD %r22, PC_BOOKE_TLB_LOCK(%r20); \463\464/* update recursion counter */ \465lwz %r23, RES_RECURSE(%r22); \466subi %r23, %r23, 1; \467stw %r23, RES_RECURSE(%r22); \468\469cmplwi %r23, 0; \470bne 1f; \471isync; \472msync; \473\474/* release the lock */ \475li %r23, TLB_UNLOCKED; \476STORE %r23, 0(%r22); \4771: isync; \478msync479#else480#define TLB_LOCK481#define TLB_UNLOCK482#endif /* SMP */483484#define INTERRUPT(label) \485.globl label; \486.align 5; \487CNAME(label):488489/*490* Interrupt handling routines in BookE can be flexibly placed and do not have491* to live in pre-defined vectors location. Note they need to be TLB-mapped at492* all times in order to be able to handle exceptions. We thus arrange for493* them to be part of kernel text which is always TLB-accessible.494*495* The interrupt handling routines have to be 16 bytes aligned: we align them496* to 32 bytes (cache line length) which supposedly performs better.497*498*/499.text500.globl CNAME(interrupt_vector_base)501.align 5502interrupt_vector_base:503/*****************************************************************************504* Catch-all handler to handle uninstalled IVORs505****************************************************************************/506INTERRUPT(int_unknown)507STANDARD_PROLOG(SPR_SPRG1, PC_TEMPSAVE, SPR_SRR0, SPR_SRR1)508FRAME_SETUP(SPR_SPRG1, PC_TEMPSAVE, EXC_RSVD)509b trap_common510511/*****************************************************************************512* Critical input interrupt513****************************************************************************/514INTERRUPT(int_critical_input)515STANDARD_CRIT_PROLOG(SPR_SPRG2, PC_BOOKE_CRITSAVE, SPR_CSRR0, SPR_CSRR1)516FRAME_SETUP(SPR_SPRG2, PC_BOOKE_CRITSAVE, EXC_CRIT)517GET_TOCBASE(%r2)518addi %r3, %r1, CALLSIZE519bl CNAME(powerpc_interrupt)520TOC_RESTORE521FRAME_LEAVE(SPR_CSRR0, SPR_CSRR1)522rfci523524525/*****************************************************************************526* Machine check interrupt527****************************************************************************/528INTERRUPT(int_machine_check)529STANDARD_PROLOG(SPR_SPRG3, PC_BOOKE_MCHKSAVE, SPR_MCSRR0, SPR_MCSRR1)530FRAME_SETUP(SPR_SPRG3, PC_BOOKE_MCHKSAVE, EXC_MCHK)531GET_TOCBASE(%r2)532addi %r3, %r1, CALLSIZE533bl CNAME(powerpc_interrupt)534TOC_RESTORE535FRAME_LEAVE(SPR_MCSRR0, SPR_MCSRR1)536rfmci537538539/*****************************************************************************540* Data storage interrupt541****************************************************************************/542INTERRUPT(int_data_storage)543STANDARD_PROLOG(SPR_SPRG1, PC_DISISAVE, SPR_SRR0, SPR_SRR1)544FRAME_SETUP(SPR_SPRG1, PC_DISISAVE, EXC_DSI)545b trap_common546547548/*****************************************************************************549* Instruction storage interrupt550****************************************************************************/551INTERRUPT(int_instr_storage)552STANDARD_PROLOG(SPR_SPRG1, PC_TEMPSAVE, SPR_SRR0, SPR_SRR1)553FRAME_SETUP(SPR_SPRG1, PC_TEMPSAVE, EXC_ISI)554b trap_common555556557/*****************************************************************************558* External input interrupt559****************************************************************************/560INTERRUPT(int_external_input)561STANDARD_PROLOG(SPR_SPRG1, PC_TEMPSAVE, SPR_SRR0, SPR_SRR1)562FRAME_SETUP(SPR_SPRG1, PC_TEMPSAVE, EXC_EXI)563b trap_common564565566INTERRUPT(int_alignment)567STANDARD_PROLOG(SPR_SPRG1, PC_TEMPSAVE, SPR_SRR0, SPR_SRR1)568FRAME_SETUP(SPR_SPRG1, PC_TEMPSAVE, EXC_ALI)569b trap_common570571572INTERRUPT(int_program)573STANDARD_PROLOG(SPR_SPRG1, PC_TEMPSAVE, SPR_SRR0, SPR_SRR1)574FRAME_SETUP(SPR_SPRG1, PC_TEMPSAVE, EXC_PGM)575b trap_common576577578INTERRUPT(int_fpu)579STANDARD_PROLOG(SPR_SPRG1, PC_TEMPSAVE, SPR_SRR0, SPR_SRR1)580FRAME_SETUP(SPR_SPRG1, PC_TEMPSAVE, EXC_FPU)581b trap_common582583584/*****************************************************************************585* System call586****************************************************************************/587INTERRUPT(int_syscall)588STANDARD_PROLOG(SPR_SPRG1, PC_TEMPSAVE, SPR_SRR0, SPR_SRR1)589FRAME_SETUP(SPR_SPRG1, PC_TEMPSAVE, EXC_SC)590b trap_common591592593/*****************************************************************************594* Decrementer interrupt595****************************************************************************/596INTERRUPT(int_decrementer)597STANDARD_PROLOG(SPR_SPRG1, PC_TEMPSAVE, SPR_SRR0, SPR_SRR1)598FRAME_SETUP(SPR_SPRG1, PC_TEMPSAVE, EXC_DECR)599b trap_common600601602/*****************************************************************************603* Fixed interval timer604****************************************************************************/605INTERRUPT(int_fixed_interval_timer)606STANDARD_PROLOG(SPR_SPRG1, PC_TEMPSAVE, SPR_SRR0, SPR_SRR1)607FRAME_SETUP(SPR_SPRG1, PC_TEMPSAVE, EXC_FIT)608b trap_common609610611/*****************************************************************************612* Watchdog interrupt613****************************************************************************/614INTERRUPT(int_watchdog)615STANDARD_PROLOG(SPR_SPRG1, PC_TEMPSAVE, SPR_SRR0, SPR_SRR1)616FRAME_SETUP(SPR_SPRG1, PC_TEMPSAVE, EXC_WDOG)617b trap_common618619620/*****************************************************************************621* Altivec Unavailable interrupt622****************************************************************************/623INTERRUPT(int_vec)624STANDARD_PROLOG(SPR_SPRG1, PC_TEMPSAVE, SPR_SRR0, SPR_SRR1)625FRAME_SETUP(SPR_SPRG1, PC_TEMPSAVE, EXC_VEC)626b trap_common627628629/*****************************************************************************630* Altivec Assist interrupt631****************************************************************************/632INTERRUPT(int_vecast)633STANDARD_PROLOG(SPR_SPRG1, PC_TEMPSAVE, SPR_SRR0, SPR_SRR1)634FRAME_SETUP(SPR_SPRG1, PC_TEMPSAVE, EXC_VECAST_E)635b trap_common636637638#ifdef HWPMC_HOOKS639/*****************************************************************************640* PMC Interrupt641****************************************************************************/642INTERRUPT(int_performance_counter)643STANDARD_PROLOG(SPR_SPRG3, PC_TEMPSAVE, SPR_SRR0, SPR_SRR1)644FRAME_SETUP(SPR_SPRG3, PC_TEMPSAVE, EXC_PERF)645b trap_common646#endif647648649/*****************************************************************************650* Data TLB miss interrupt651*652* There can be nested TLB misses - while handling a TLB miss we reference653* data structures that may be not covered by translations. We support up to654* TLB_NESTED_MAX-1 nested misses.655*656* Registers use:657* r31 - dear658* r30 - unused659* r29 - saved mas0660* r28 - saved mas1661* r27 - saved mas2662* r26 - pmap address663* r25 - pte address664*665* r20:r23 - scratch registers666****************************************************************************/667INTERRUPT(int_data_tlb_error)668TLB_PROLOG669TLB_LOCK670671mfspr %r31, SPR_DEAR672673/*674* Save MAS0-MAS2 registers. There might be another tlb miss during675* pte lookup overwriting current contents (which was hw filled).676*/677mfspr %r29, SPR_MAS0678mfspr %r28, SPR_MAS1679mfspr %r27, SPR_MAS2680681/* Check faulting address. */682LOAD_ADDR(%r21, VM_MAXUSER_ADDRESS)683CMPL cr0, %r31, %r21684blt search_user_pmap685686/* If it's kernel address, allow only supervisor mode misses. */687mfsrr1 %r21688mtcr %r21689bt 17, search_failed /* check MSR[PR] */690691#ifdef __powerpc64__692srdi %r21, %r31, 48693cmpldi cr0, %r21, VM_MIN_KERNEL_ADDRESS@highest694#else695lis %r21, VM_MIN_KERNEL_ADDRESS@h696cmplw cr0, %r31, %r21697#endif698blt search_failed699700search_kernel_pmap:701/* Load r26 with kernel_pmap address */702bl 1f703#ifdef __powerpc64__704.llong kernel_pmap_store-.705#else706.long kernel_pmap_store-.707#endif7081: mflr %r21709LOAD %r26, 0(%r21)710add %r26, %r21, %r26 /* kernel_pmap_store in r26 */711712/* Force kernel tid, set TID to 0 in MAS1. */713li %r21, 0714rlwimi %r28, %r21, 0, 8, 15 /* clear TID bits */715716tlb_miss_handle:717/* This may result in nested tlb miss. */718bl pte_lookup /* returns PTE address in R25 */719720CMPI %r25, 0 /* pte found? */721beq search_failed722723/* Finish up, write TLB entry. */724bl tlb_fill_entry725726tlb_miss_return:727TLB_UNLOCK728TLB_RESTORE729rfi730731search_user_pmap:732/* Load r26 with current user space process pmap */733GET_CPUINFO(%r26)734LOAD %r26, PC_CURPMAP(%r26)735736b tlb_miss_handle737738search_failed:739/*740* Whenever we don't find a TLB mapping in PT, set a TLB0 entry with741* the faulting virtual address anyway, but put a fake RPN and no742* access rights. This should cause a following {D,I}SI exception.743*/744lis %r23, 0xffff0000@h /* revoke all permissions */745746/* Load MAS registers. */747mtspr SPR_MAS0, %r29748mtspr SPR_MAS1, %r28749mtspr SPR_MAS2, %r27750mtspr SPR_MAS3, %r23751752li %r23, 0753mtspr SPR_MAS7, %r23754755isync756tlbwe757msync758isync759b tlb_miss_return760761/*****************************************************************************762*763* Return pte address that corresponds to given pmap/va. If there is no valid764* entry return 0.765*766* input: r26 - pmap767* input: r31 - dear768* output: r25 - pte address769*770* scratch regs used: r21771*772****************************************************************************/773pte_lookup:774CMPI %r26, 0775beq 1f /* fail quickly if pmap is invalid */776777#ifdef __powerpc64__778rldicl %r21, %r31, (64 - PG_ROOT_L), (64 - PG_ROOT_NUM) /* pp2d offset */779slwi %r21, %r21, PG_ROOT_ENTRY_SHIFT /* multiply by pp2d entry size */780ld %r25, PM_ROOT(%r26) /* pmap pm_pp2d[] address */781ldx %r25, %r25, %r21 /* get pdir address, i.e. pmap->pm_pp2d[pp2d_idx] * */782783cmpdi %r25, 0784beq 2f785786rldicl %r21, %r31, (64 - PDIR_L1_L), (64 - PDIR_L1_NUM) /* pp2d offset */787slwi %r21, %r21, PDIR_L1_ENTRY_SHIFT /* multiply by pp2d entry size */788ldx %r25, %r25, %r21 /* get pdir address, i.e. pmap->pm_pp2d[pp2d_idx] * */789790cmpdi %r25, 0791beq 2f792793rldicl %r21, %r31, (64 - PDIR_L), (64 - PDIR_NUM) /* pdir offset */794slwi %r21, %r21, PDIR_ENTRY_SHIFT /* multiply by pdir entry size */795ldx %r25, %r25, %r21 /* get ptbl address, i.e. pmap->pm_pp2d[pp2d_idx][pdir_idx] */796797cmpdi %r25, 0798beq 2f799800rldicl %r21, %r31, (64 - PTBL_L), (64 - PTBL_NUM) /* ptbl offset */801slwi %r21, %r21, PTBL_ENTRY_SHIFT /* multiply by pte entry size */802803#else804srwi %r21, %r31, PDIR_SHIFT /* pdir offset */805slwi %r21, %r21, PDIR_ENTRY_SHIFT /* multiply by pdir entry size */806807lwz %r25, PM_PDIR(%r26) /* pmap pm_dir[] address */808/*809* Get ptbl address, i.e. pmap->pm_pdir[pdir_idx]810* This load may cause a Data TLB miss for non-kernel pmap!811*/812lwzx %r25, %r25, %r21 /* offset within pm_pdir[] table */813cmpwi %r25, 0814beq 2f815816lis %r21, PTBL_MASK@h817ori %r21, %r21, PTBL_MASK@l818and %r21, %r21, %r31819820/* ptbl offset, multiply by ptbl entry size */821srwi %r21, %r21, (PTBL_SHIFT - PTBL_ENTRY_SHIFT)822#endif823824add %r25, %r25, %r21 /* address of pte entry */825/*826* Get pte->flags827* This load may cause a Data TLB miss for non-kernel pmap!828*/829lwz %r21, PTE_FLAGS(%r25)830andi. %r21, %r21, PTE_VALID@l831bne 2f8321:833li %r25, 08342:835blr836837/*****************************************************************************838*839* Load MAS1-MAS3 registers with data, write TLB entry840*841* input:842* r29 - mas0843* r28 - mas1844* r27 - mas2845* r25 - pte846*847* output: none848*849* scratch regs: r21-r23850*851****************************************************************************/852tlb_fill_entry:853/*854* Update PTE flags: we have to do it atomically, as pmap_protect()855* running on other CPUs could attempt to update the flags at the same856* time.857*/858li %r23, PTE_FLAGS8591:860lwarx %r21, %r23, %r25 /* get pte->flags */861oris %r21, %r21, PTE_REFERENCED@h /* set referenced bit */862863andi. %r22, %r21, (PTE_SW | PTE_UW)@l /* check if writable */864beq 2f865ori %r21, %r21, PTE_MODIFIED@l /* set modified bit */8662:867stwcx. %r21, %r23, %r25 /* write it back */868bne- 1b869870/* Update MAS2. */871rlwimi %r27, %r21, 13, 27, 30 /* insert WIMG bits from pte */872873/* Setup MAS3 value in r23. */874LOAD %r23, PTE_RPN(%r25) /* get pte->rpn */875#ifdef __powerpc64__876rldicr %r22, %r23, 52, 51 /* extract MAS3 portion of RPN */877rldicl %r23, %r23, 20, 54 /* extract MAS7 portion of RPN */878879rlwimi %r22, %r21, 30, 26, 31 /* insert protection bits from pte */880#else881rlwinm %r22, %r23, 20, 0, 11 /* extract MAS3 portion of RPN */882883rlwimi %r22, %r21, 30, 26, 31 /* insert protection bits from pte */884rlwimi %r22, %r21, 20, 12, 19 /* insert lower 8 RPN bits to MAS3 */885rlwinm %r23, %r23, 20, 24, 31 /* MAS7 portion of RPN */886#endif887888/* Load MAS registers. */889mtspr SPR_MAS0, %r29890mtspr SPR_MAS1, %r28891mtspr SPR_MAS2, %r27892mtspr SPR_MAS3, %r22893mtspr SPR_MAS7, %r23894895isync896tlbwe897isync898msync899blr900901/*****************************************************************************902* Instruction TLB miss interrupt903*904* Same notes as for the Data TLB miss905****************************************************************************/906INTERRUPT(int_inst_tlb_error)907TLB_PROLOG908TLB_LOCK909910mfsrr0 %r31 /* faulting address */911912/*913* Save MAS0-MAS2 registers. There might be another tlb miss during pte914* lookup overwriting current contents (which was hw filled).915*/916mfspr %r29, SPR_MAS0917mfspr %r28, SPR_MAS1918mfspr %r27, SPR_MAS2919920mfsrr1 %r21921mtcr %r21922923/* check MSR[PR] */924bt 17, search_user_pmap925b search_kernel_pmap926927928.globl interrupt_vector_top929interrupt_vector_top:930931/*****************************************************************************932* Debug interrupt933****************************************************************************/934INTERRUPT(int_debug)935STANDARD_CRIT_PROLOG(SPR_SPRG2, PC_BOOKE_CRITSAVE, SPR_CSRR0, SPR_CSRR1)936FRAME_SETUP(SPR_SPRG2, PC_BOOKE_CRITSAVE, EXC_DEBUG)937bl int_debug_int938FRAME_LEAVE(SPR_CSRR0, SPR_CSRR1)939rfci940941INTERRUPT(int_debug_ed)942STANDARD_CRIT_PROLOG(SPR_SPRG2, PC_BOOKE_CRITSAVE, SPR_DSRR0, SPR_DSRR1)943FRAME_SETUP(SPR_SPRG2, PC_BOOKE_CRITSAVE, EXC_DEBUG)944bl int_debug_int945FRAME_LEAVE(SPR_DSRR0, SPR_DSRR1)946rfdi947/* .long 0x4c00004e */948949/* Internal helper for debug interrupt handling. */950/* Common code between e500v1/v2 and e500mc-based cores. */951int_debug_int:952mflr %r14953GET_CPUINFO(%r3)954LOAD %r3, (PC_BOOKE_CRITSAVE+CPUSAVE_SRR0)(%r3)955bl 0f956ADDR(interrupt_vector_base-.)957ADDR(interrupt_vector_top-.)9580: mflr %r5959LOAD %r4,0(%r5) /* interrupt_vector_base in r4 */960add %r4,%r4,%r5961CMPL cr0, %r3, %r4962blt trap_common963LOAD %r4,WORD_SIZE(%r5) /* interrupt_vector_top in r4 */964add %r4,%r4,%r5965addi %r4,%r4,4966CMPL cr0, %r3, %r4967bge trap_common968/* Disable single-stepping for the interrupt handlers. */969LOAD %r3, FRAME_SRR1+CALLSIZE(%r1);970rlwinm %r3, %r3, 0, 23, 21971STORE %r3, FRAME_SRR1+CALLSIZE(%r1);972/* Restore srr0 and srr1 as they could have been clobbered. */973GET_CPUINFO(%r4)974LOAD %r3, (PC_BOOKE_CRITSAVE+BOOKE_CRITSAVE_SRR0)(%r4);975mtspr SPR_SRR0, %r3976LOAD %r4, (PC_BOOKE_CRITSAVE+BOOKE_CRITSAVE_SRR1)(%r4);977mtspr SPR_SRR1, %r4978mtlr %r14979blr980981/*****************************************************************************982* Common trap code983****************************************************************************/984trap_common:985/* Call C trap dispatcher */986GET_TOCBASE(%r2)987addi %r3, %r1, CALLSIZE988bl CNAME(powerpc_interrupt)989TOC_RESTORE990991.globl CNAME(trapexit) /* exported for db_backtrace use */992CNAME(trapexit):993/* disable interrupts */994wrteei 0995996/* Test AST pending - makes sense for user process only */997LOAD %r5, FRAME_SRR1+CALLSIZE(%r1)998mtcr %r5999bf 17, 1f10001001GET_CPUINFO(%r3)1002LOAD %r4, PC_CURTHREAD(%r3)1003lwz %r4, TD_AST(%r4)1004cmpwi %r4, 01005beq 1f10061007/* re-enable interrupts before calling ast() */1008wrteei 110091010addi %r3, %r1, CALLSIZE1011bl CNAME(ast)1012TOC_RESTORE1013.globl CNAME(asttrapexit) /* db_backtrace code sentinel #2 */1014CNAME(asttrapexit):1015b trapexit /* test ast ret value ? */10161:1017FRAME_LEAVE(SPR_SRR0, SPR_SRR1)1018rfi101910201021#if defined(KDB)1022/*1023* Deliberate entry to dbtrap1024*/1025/* .globl CNAME(breakpoint)*/1026ASENTRY_NOPROF(breakpoint)1027mtsprg1 %r11028mfmsr %r31029mtsrr1 %r31030li %r4, ~(PSL_EE | PSL_ME)@l1031oris %r4, %r4, ~(PSL_EE | PSL_ME)@h1032and %r3, %r3, %r41033mtmsr %r3 /* disable interrupts */1034isync1035GET_CPUINFO(%r3)1036STORE %r30, (PC_DBSAVE+CPUSAVE_R30)(%r3)1037STORE %r31, (PC_DBSAVE+CPUSAVE_R31)(%r3)10381039mflr %r311040mtsrr0 %r3110411042mfspr %r30, SPR_DEAR1043mfspr %r31, SPR_ESR1044STORE %r30, (PC_DBSAVE+CPUSAVE_BOOKE_DEAR)(%r3)1045STORE %r31, (PC_DBSAVE+CPUSAVE_BOOKE_ESR)(%r3)10461047mfsrr0 %r301048mfsrr1 %r311049STORE %r30, (PC_DBSAVE+CPUSAVE_SRR0)(%r3)1050STORE %r31, (PC_DBSAVE+CPUSAVE_SRR1)(%r3)1051isync10521053mfcr %r3010541055/*1056* Now the kdb trap catching code.1057*/1058dbtrap:1059FRAME_SETUP(SPR_SPRG1, PC_DBSAVE, EXC_DEBUG)1060/* Call C trap code: */1061GET_TOCBASE(%r2)1062addi %r3, %r1, CALLSIZE1063bl CNAME(db_trap_glue)1064TOC_RESTORE1065or. %r3, %r3, %r31066bne dbleave1067/* This wasn't for KDB, so switch to real trap: */1068b trap_common10691070dbleave:1071FRAME_LEAVE(SPR_SRR0, SPR_SRR1)1072rfi1073ASEND(breakpoint)1074#endif /* KDB */10751076#ifdef SMP1077ENTRY(tlb_lock)1078GET_CPUINFO(%r5)1079LOAD %r5, PC_CURTHREAD(%r5)10801: LOADX %r4, 0, %r31081CMPI %r4, TLB_UNLOCKED1082bne 1b1083STOREX %r5, 0, %r31084bne- 1b1085isync1086msync1087blr1088END(tlb_lock)10891090ENTRY(tlb_unlock)1091isync1092msync1093li %r4, TLB_UNLOCKED1094STORE %r4, 0(%r3)1095isync1096msync1097blr1098END(tlb_unlock)10991100/*1101* TLB miss spin locks. For each CPU we have a reservation granule (32 bytes);1102* only a single word from this granule will actually be used as a spin lock1103* for mutual exclusion between TLB miss handler and pmap layer that1104* manipulates page table contents.1105*/1106.data1107.align 51108GLOBAL(tlb0_miss_locks)1109.space RES_GRANULE * MAXCPU1110#endif111111121113