Path: blob/master/arch/microblaze/kernel/hw_exception_handler.S
10817 views
/*1* Exception handling for Microblaze2*3* Rewriten interrupt handling4*5* Copyright (C) 2008-2009 Michal Simek <[email protected]>6* Copyright (C) 2008-2009 PetaLogix7*8* uClinux customisation (C) 2005 John Williams9*10* MMU code derived from arch/ppc/kernel/head_4xx.S:11* Copyright (C) 1995-1996 Gary Thomas <[email protected]>12* Initial PowerPC version.13* Copyright (C) 1996 Cort Dougan <[email protected]>14* Rewritten for PReP15* Copyright (C) 1996 Paul Mackerras <[email protected]>16* Low-level exception handers, MMU support, and rewrite.17* Copyright (C) 1997 Dan Malek <[email protected]>18* PowerPC 8xx modifications.19* Copyright (C) 1998-1999 TiVo, Inc.20* PowerPC 403GCX modifications.21* Copyright (C) 1999 Grant Erickson <[email protected]>22* PowerPC 403GCX/405GP modifications.23* Copyright 2000 MontaVista Software Inc.24* PPC405 modifications25* PowerPC 403GCX/405GP modifications.26* Author: MontaVista Software, Inc.27* [email protected] or [email protected]28* [email protected]29*30* Original code31* Copyright (C) 2004 Xilinx, Inc.32*33* This program is free software; you can redistribute it and/or modify it34* under the terms of the GNU General Public License version 2 as published35* by the Free Software Foundation.36*/3738/*39* Here are the handlers which don't require enabling translation40* and calling other kernel code thus we can keep their design very simple41* and do all processing in real mode. All what they need is a valid current42* (that is an issue for the CONFIG_REGISTER_TASK_PTR case)43* This handlers use r3,r4,r5,r6 and optionally r[current] to work therefore44* these registers are saved/restored45* The handlers which require translation are in entry.S --KAA46*47* Microblaze HW Exception Handler48* - Non self-modifying exception handler for the following exception conditions49* - Unalignment50* - Instruction bus error51* - Data bus error52* - Illegal instruction opcode53* - Divide-by-zero54*55* - Privileged instruction exception (MMU)56* - Data storage exception (MMU)57* - Instruction storage exception (MMU)58* - Data TLB miss exception (MMU)59* - Instruction TLB miss exception (MMU)60*61* Note we disable interrupts during exception handling, otherwise we will62* possibly get multiple re-entrancy if interrupt handles themselves cause63* exceptions. JW64*/6566#include <asm/exceptions.h>67#include <asm/unistd.h>68#include <asm/page.h>6970#include <asm/entry.h>71#include <asm/current.h>72#include <linux/linkage.h>7374#include <asm/mmu.h>75#include <asm/pgtable.h>76#include <asm/signal.h>77#include <asm/asm-offsets.h>7879#undef DEBUG8081/* Helpful Macros */82#define NUM_TO_REG(num) r ## num8384#ifdef CONFIG_MMU85#define RESTORE_STATE \86lwi r5, r1, 0; \87mts rmsr, r5; \88nop; \89lwi r3, r1, PT_R3; \90lwi r4, r1, PT_R4; \91lwi r5, r1, PT_R5; \92lwi r6, r1, PT_R6; \93lwi r11, r1, PT_R11; \94lwi r31, r1, PT_R31; \95lwi r1, r1, PT_R1;96#endif /* CONFIG_MMU */9798#define LWREG_NOP \99bri ex_handler_unhandled; \100nop;101102#define SWREG_NOP \103bri ex_handler_unhandled; \104nop;105106/* FIXME this is weird - for noMMU kernel is not possible to use brid107* instruction which can shorten executed time108*/109110/* r3 is the source */111#define R3_TO_LWREG_V(regnum) \112swi r3, r1, 4 * regnum; \113bri ex_handler_done;114115/* r3 is the source */116#define R3_TO_LWREG(regnum) \117or NUM_TO_REG (regnum), r0, r3; \118bri ex_handler_done;119120/* r3 is the target */121#define SWREG_TO_R3_V(regnum) \122lwi r3, r1, 4 * regnum; \123bri ex_sw_tail;124125/* r3 is the target */126#define SWREG_TO_R3(regnum) \127or r3, r0, NUM_TO_REG (regnum); \128bri ex_sw_tail;129130#ifdef CONFIG_MMU131#define R3_TO_LWREG_VM_V(regnum) \132brid ex_lw_end_vm; \133swi r3, r7, 4 * regnum;134135#define R3_TO_LWREG_VM(regnum) \136brid ex_lw_end_vm; \137or NUM_TO_REG (regnum), r0, r3;138139#define SWREG_TO_R3_VM_V(regnum) \140brid ex_sw_tail_vm; \141lwi r3, r7, 4 * regnum;142143#define SWREG_TO_R3_VM(regnum) \144brid ex_sw_tail_vm; \145or r3, r0, NUM_TO_REG (regnum);146147/* Shift right instruction depending on available configuration */148#if CONFIG_XILINX_MICROBLAZE0_USE_BARREL > 0149#define BSRLI(rD, rA, imm) \150bsrli rD, rA, imm151#else152#define BSRLI(rD, rA, imm) BSRLI ## imm (rD, rA)153/* Only the used shift constants defined here - add more if needed */154#define BSRLI2(rD, rA) \155srl rD, rA; /* << 1 */ \156srl rD, rD; /* << 2 */157#define BSRLI10(rD, rA) \158srl rD, rA; /* << 1 */ \159srl rD, rD; /* << 2 */ \160srl rD, rD; /* << 3 */ \161srl rD, rD; /* << 4 */ \162srl rD, rD; /* << 5 */ \163srl rD, rD; /* << 6 */ \164srl rD, rD; /* << 7 */ \165srl rD, rD; /* << 8 */ \166srl rD, rD; /* << 9 */ \167srl rD, rD /* << 10 */168#define BSRLI20(rD, rA) \169BSRLI10(rD, rA); \170BSRLI10(rD, rD)171#endif172#endif /* CONFIG_MMU */173174.extern other_exception_handler /* Defined in exception.c */175176/*177* hw_exception_handler - Handler for exceptions178*179* Exception handler notes:180* - Handles all exceptions181* - Does not handle unaligned exceptions during load into r17, r1, r0.182* - Does not handle unaligned exceptions during store from r17 (cannot be183* done) and r1 (slows down common case)184*185* Relevant register structures186*187* EAR - |----|----|----|----|----|----|----|----|188* - < ## 32 bit faulting address ## >189*190* ESR - |----|----|----|----|----| - | - |-----|-----|191* - W S REG EXC192*193*194* STACK FRAME STRUCTURE (for NO_MMU)195* ---------------------------------196*197* +-------------+ + 0198* | MSR |199* +-------------+ + 4200* | r1 |201* | . |202* | . |203* | . |204* | . |205* | r18 |206* +-------------+ + 76207* | . |208* | . |209*210* MMU kernel uses the same 'pt_pool_space' pointed space211* which is used for storing register values - noMMu style was, that values were212* stored in stack but in case of failure you lost information about register.213* Currently you can see register value in memory in specific place.214* In compare to with previous solution the speed should be the same.215*216* MMU exception handler has different handling compare to no MMU kernel.217* Exception handler use jump table for directing of what happen. For MMU kernel218* is this approach better because MMU relate exception are handled by asm code219* in this file. In compare to with MMU expect of unaligned exception220* is everything handled by C code.221*/222223/*224* every of these handlers is entered having R3/4/5/6/11/current saved on stack225* and clobbered so care should be taken to restore them if someone is going to226* return from exception227*/228229/* wrappers to restore state before coming to entry.S */230#ifdef CONFIG_MMU231.section .data232.align 4233pt_pool_space:234.space PT_SIZE235236#ifdef DEBUG237/* Create space for exception counting. */238.section .data239.global exception_debug_table240.align 4241exception_debug_table:242/* Look at exception vector table. There is 32 exceptions * word size */243.space (32 * 4)244#endif /* DEBUG */245246.section .rodata247.align 4248_MB_HW_ExceptionVectorTable:249/* 0 - Undefined */250.long TOPHYS(ex_handler_unhandled)251/* 1 - Unaligned data access exception */252.long TOPHYS(handle_unaligned_ex)253/* 2 - Illegal op-code exception */254.long TOPHYS(full_exception_trapw)255/* 3 - Instruction bus error exception */256.long TOPHYS(full_exception_trapw)257/* 4 - Data bus error exception */258.long TOPHYS(full_exception_trapw)259/* 5 - Divide by zero exception */260.long TOPHYS(full_exception_trapw)261/* 6 - Floating point unit exception */262.long TOPHYS(full_exception_trapw)263/* 7 - Privileged instruction exception */264.long TOPHYS(full_exception_trapw)265/* 8 - 15 - Undefined */266.long TOPHYS(ex_handler_unhandled)267.long TOPHYS(ex_handler_unhandled)268.long TOPHYS(ex_handler_unhandled)269.long TOPHYS(ex_handler_unhandled)270.long TOPHYS(ex_handler_unhandled)271.long TOPHYS(ex_handler_unhandled)272.long TOPHYS(ex_handler_unhandled)273.long TOPHYS(ex_handler_unhandled)274/* 16 - Data storage exception */275.long TOPHYS(handle_data_storage_exception)276/* 17 - Instruction storage exception */277.long TOPHYS(handle_instruction_storage_exception)278/* 18 - Data TLB miss exception */279.long TOPHYS(handle_data_tlb_miss_exception)280/* 19 - Instruction TLB miss exception */281.long TOPHYS(handle_instruction_tlb_miss_exception)282/* 20 - 31 - Undefined */283.long TOPHYS(ex_handler_unhandled)284.long TOPHYS(ex_handler_unhandled)285.long TOPHYS(ex_handler_unhandled)286.long TOPHYS(ex_handler_unhandled)287.long TOPHYS(ex_handler_unhandled)288.long TOPHYS(ex_handler_unhandled)289.long TOPHYS(ex_handler_unhandled)290.long TOPHYS(ex_handler_unhandled)291.long TOPHYS(ex_handler_unhandled)292.long TOPHYS(ex_handler_unhandled)293.long TOPHYS(ex_handler_unhandled)294.long TOPHYS(ex_handler_unhandled)295#endif296297.global _hw_exception_handler298.section .text299.align 4300.ent _hw_exception_handler301_hw_exception_handler:302#ifndef CONFIG_MMU303addik r1, r1, -(EX_HANDLER_STACK_SIZ); /* Create stack frame */304#else305swi r1, r0, TOPHYS(pt_pool_space + PT_R1); /* GET_SP */306/* Save date to kernel memory. Here is the problem307* when you came from user space */308ori r1, r0, TOPHYS(pt_pool_space);309#endif310swi r3, r1, PT_R3311swi r4, r1, PT_R4312swi r5, r1, PT_R5313swi r6, r1, PT_R6314315#ifdef CONFIG_MMU316swi r11, r1, PT_R11317swi r31, r1, PT_R31318lwi r31, r0, TOPHYS(PER_CPU(CURRENT_SAVE)) /* get saved current */319#endif320321mfs r5, rmsr;322nop323swi r5, r1, 0;324mfs r4, resr325nop326mfs r3, rear;327nop328329#ifndef CONFIG_MMU330andi r5, r4, 0x1000; /* Check ESR[DS] */331beqi r5, not_in_delay_slot; /* Branch if ESR[DS] not set */332mfs r17, rbtr; /* ESR[DS] set - return address in BTR */333nop334not_in_delay_slot:335swi r17, r1, PT_R17336#endif337338andi r5, r4, 0x1F; /* Extract ESR[EXC] */339340#ifdef CONFIG_MMU341/* Calculate exception vector offset = r5 << 2 */342addk r6, r5, r5; /* << 1 */343addk r6, r6, r6; /* << 2 */344345#ifdef DEBUG346/* counting which exception happen */347lwi r5, r0, TOPHYS(exception_debug_table)348addi r5, r5, 1349swi r5, r0, TOPHYS(exception_debug_table)350lwi r5, r6, TOPHYS(exception_debug_table)351addi r5, r5, 1352swi r5, r6, TOPHYS(exception_debug_table)353#endif354/* end */355/* Load the HW Exception vector */356lwi r6, r6, TOPHYS(_MB_HW_ExceptionVectorTable)357bra r6358359full_exception_trapw:360RESTORE_STATE361bri full_exception_trap362#else363/* Exceptions enabled here. This will allow nested exceptions */364mfs r6, rmsr;365nop366swi r6, r1, 0; /* RMSR_OFFSET */367ori r6, r6, 0x100; /* Turn ON the EE bit */368andi r6, r6, ~2; /* Disable interrupts */369mts rmsr, r6;370nop371372xori r6, r5, 1; /* 00001 = Unaligned Exception */373/* Jump to unalignment exception handler */374beqi r6, handle_unaligned_ex;375376handle_other_ex: /* Handle Other exceptions here */377/* Save other volatiles before we make procedure calls below */378swi r7, r1, PT_R7379swi r8, r1, PT_R8380swi r9, r1, PT_R9381swi r10, r1, PT_R10382swi r11, r1, PT_R11383swi r12, r1, PT_R12384swi r14, r1, PT_R14385swi r15, r1, PT_R15386swi r18, r1, PT_R18387388or r5, r1, r0389andi r6, r4, 0x1F; /* Load ESR[EC] */390lwi r7, r0, PER_CPU(KM) /* MS: saving current kernel mode to regs */391swi r7, r1, PT_MODE392mfs r7, rfsr393nop394addk r8, r17, r0; /* Load exception address */395bralid r15, full_exception; /* Branch to the handler */396nop;397mts rfsr, r0; /* Clear sticky fsr */398nop399400/*401* Trigger execution of the signal handler by enabling402* interrupts and calling an invalid syscall.403*/404mfs r5, rmsr;405nop406ori r5, r5, 2;407mts rmsr, r5; /* enable interrupt */408nop409addi r12, r0, __NR_syscalls;410brki r14, 0x08;411mfs r5, rmsr; /* disable interrupt */412nop413andi r5, r5, ~2;414mts rmsr, r5;415nop416417lwi r7, r1, PT_R7418lwi r8, r1, PT_R8419lwi r9, r1, PT_R9420lwi r10, r1, PT_R10421lwi r11, r1, PT_R11422lwi r12, r1, PT_R12423lwi r14, r1, PT_R14424lwi r15, r1, PT_R15425lwi r18, r1, PT_R18426427bri ex_handler_done; /* Complete exception handling */428#endif429430/* 0x01 - Unaligned data access exception431* This occurs when a word access is not aligned on a word boundary,432* or when a 16-bit access is not aligned on a 16-bit boundary.433* This handler perform the access, and returns, except for MMU when434* the unaligned address is last on a 4k page or the physical address is435* not found in the page table, in which case unaligned_data_trap is called.436*/437handle_unaligned_ex:438/* Working registers already saved: R3, R4, R5, R6439* R4 = ESR440* R3 = EAR441*/442#ifdef CONFIG_MMU443andi r6, r4, 0x1000 /* Check ESR[DS] */444beqi r6, _no_delayslot /* Branch if ESR[DS] not set */445mfs r17, rbtr; /* ESR[DS] set - return address in BTR */446nop447_no_delayslot:448/* jump to high level unaligned handler */449RESTORE_STATE;450bri unaligned_data_trap451#endif452andi r6, r4, 0x3E0; /* Mask and extract the register operand */453srl r6, r6; /* r6 >> 5 */454srl r6, r6;455srl r6, r6;456srl r6, r6;457srl r6, r6;458/* Store the register operand in a temporary location */459sbi r6, r0, TOPHYS(ex_reg_op);460461andi r6, r4, 0x400; /* Extract ESR[S] */462bnei r6, ex_sw;463ex_lw:464andi r6, r4, 0x800; /* Extract ESR[W] */465beqi r6, ex_lhw;466lbui r5, r3, 0; /* Exception address in r3 */467/* Load a word, byte-by-byte from destination address468and save it in tmp space */469sbi r5, r0, TOPHYS(ex_tmp_data_loc_0);470lbui r5, r3, 1;471sbi r5, r0, TOPHYS(ex_tmp_data_loc_1);472lbui r5, r3, 2;473sbi r5, r0, TOPHYS(ex_tmp_data_loc_2);474lbui r5, r3, 3;475sbi r5, r0, TOPHYS(ex_tmp_data_loc_3);476/* Get the destination register value into r4 */477lwi r4, r0, TOPHYS(ex_tmp_data_loc_0);478bri ex_lw_tail;479ex_lhw:480lbui r5, r3, 0; /* Exception address in r3 */481/* Load a half-word, byte-by-byte from destination482address and save it in tmp space */483sbi r5, r0, TOPHYS(ex_tmp_data_loc_0);484lbui r5, r3, 1;485sbi r5, r0, TOPHYS(ex_tmp_data_loc_1);486/* Get the destination register value into r4 */487lhui r4, r0, TOPHYS(ex_tmp_data_loc_0);488ex_lw_tail:489/* Get the destination register number into r5 */490lbui r5, r0, TOPHYS(ex_reg_op);491/* Form load_word jump table offset (lw_table + (8 * regnum)) */492addik r6, r0, TOPHYS(lw_table);493addk r5, r5, r5;494addk r5, r5, r5;495addk r5, r5, r5;496addk r5, r5, r6;497bra r5;498ex_lw_end: /* Exception handling of load word, ends */499ex_sw:500/* Get the destination register number into r5 */501lbui r5, r0, TOPHYS(ex_reg_op);502/* Form store_word jump table offset (sw_table + (8 * regnum)) */503addik r6, r0, TOPHYS(sw_table);504add r5, r5, r5;505add r5, r5, r5;506add r5, r5, r5;507add r5, r5, r6;508bra r5;509ex_sw_tail:510mfs r6, resr;511nop512andi r6, r6, 0x800; /* Extract ESR[W] */513beqi r6, ex_shw;514/* Get the word - delay slot */515swi r4, r0, TOPHYS(ex_tmp_data_loc_0);516/* Store the word, byte-by-byte into destination address */517lbui r4, r0, TOPHYS(ex_tmp_data_loc_0);518sbi r4, r3, 0;519lbui r4, r0, TOPHYS(ex_tmp_data_loc_1);520sbi r4, r3, 1;521lbui r4, r0, TOPHYS(ex_tmp_data_loc_2);522sbi r4, r3, 2;523lbui r4, r0, TOPHYS(ex_tmp_data_loc_3);524sbi r4, r3, 3;525bri ex_handler_done;526527ex_shw:528/* Store the lower half-word, byte-by-byte into destination address */529swi r4, r0, TOPHYS(ex_tmp_data_loc_0);530lbui r4, r0, TOPHYS(ex_tmp_data_loc_2);531sbi r4, r3, 0;532lbui r4, r0, TOPHYS(ex_tmp_data_loc_3);533sbi r4, r3, 1;534ex_sw_end: /* Exception handling of store word, ends. */535536ex_handler_done:537#ifndef CONFIG_MMU538lwi r5, r1, 0 /* RMSR */539mts rmsr, r5540nop541lwi r3, r1, PT_R3542lwi r4, r1, PT_R4543lwi r5, r1, PT_R5544lwi r6, r1, PT_R6545lwi r17, r1, PT_R17546547rted r17, 0548addik r1, r1, (EX_HANDLER_STACK_SIZ); /* Restore stack frame */549#else550RESTORE_STATE;551rted r17, 0552nop553#endif554555#ifdef CONFIG_MMU556/* Exception vector entry code. This code runs with address translation557* turned off (i.e. using physical addresses). */558559/* Exception vectors. */560561/* 0x10 - Data Storage Exception562* This happens for just a few reasons. U0 set (but we don't do that),563* or zone protection fault (user violation, write to protected page).564* If this is just an update of modified status, we do that quickly565* and exit. Otherwise, we call heavyweight functions to do the work.566*/567handle_data_storage_exception:568/* Working registers already saved: R3, R4, R5, R6569* R3 = ESR570*/571mfs r11, rpid572nop573/* If we are faulting a kernel address, we have to use the574* kernel page tables.575*/576ori r5, r0, CONFIG_KERNEL_START577cmpu r5, r3, r5578bgti r5, ex3579/* First, check if it was a zone fault (which means a user580* tried to access a kernel or read-protected page - always581* a SEGV). All other faults here must be stores, so no582* need to check ESR_S as well. */583andi r4, r4, 0x800 /* ESR_Z - zone protection */584bnei r4, ex2585586ori r4, r0, swapper_pg_dir587mts rpid, r0 /* TLB will have 0 TID */588nop589bri ex4590591/* Get the PGD for the current thread. */592ex3:593/* First, check if it was a zone fault (which means a user594* tried to access a kernel or read-protected page - always595* a SEGV). All other faults here must be stores, so no596* need to check ESR_S as well. */597andi r4, r4, 0x800 /* ESR_Z */598bnei r4, ex2599/* get current task address */600addi r4 ,CURRENT_TASK, TOPHYS(0);601lwi r4, r4, TASK_THREAD+PGDIR602ex4:603tophys(r4,r4)604BSRLI(r5,r3,20) /* Create L1 (pgdir/pmd) address */605andi r5, r5, 0xffc606/* Assume pgdir aligned on 4K boundary, no need for "andi r4,r4,0xfffff003" */607or r4, r4, r5608lwi r4, r4, 0 /* Get L1 entry */609andi r5, r4, 0xfffff000 /* Extract L2 (pte) base address */610beqi r5, ex2 /* Bail if no table */611612tophys(r5,r5)613BSRLI(r6,r3,10) /* Compute PTE address */614andi r6, r6, 0xffc615andi r5, r5, 0xfffff003616or r5, r5, r6617lwi r4, r5, 0 /* Get Linux PTE */618619andi r6, r4, _PAGE_RW /* Is it writeable? */620beqi r6, ex2 /* Bail if not */621622/* Update 'changed' */623ori r4, r4, _PAGE_DIRTY|_PAGE_ACCESSED|_PAGE_HWWRITE624swi r4, r5, 0 /* Update Linux page table */625626/* Most of the Linux PTE is ready to load into the TLB LO.627* We set ZSEL, where only the LS-bit determines user access.628* We set execute, because we don't have the granularity to629* properly set this at the page level (Linux problem).630* If shared is set, we cause a zero PID->TID load.631* Many of these bits are software only. Bits we don't set632* here we (properly should) assume have the appropriate value.633*/634andni r4, r4, 0x0ce2 /* Make sure 20, 21 are zero */635ori r4, r4, _PAGE_HWEXEC /* make it executable */636637/* find the TLB index that caused the fault. It has to be here*/638mts rtlbsx, r3639nop640mfs r5, rtlbx /* DEBUG: TBD */641nop642mts rtlblo, r4 /* Load TLB LO */643nop644/* Will sync shadow TLBs */645646/* Done...restore registers and get out of here. */647mts rpid, r11648nop649bri 4650651RESTORE_STATE;652rted r17, 0653nop654ex2:655/* The bailout. Restore registers to pre-exception conditions656* and call the heavyweights to help us out. */657mts rpid, r11658nop659bri 4660RESTORE_STATE;661bri page_fault_data_trap662663664/* 0x11 - Instruction Storage Exception665* This is caused by a fetch from non-execute or guarded pages. */666handle_instruction_storage_exception:667/* Working registers already saved: R3, R4, R5, R6668* R3 = ESR669*/670671RESTORE_STATE;672bri page_fault_instr_trap673674/* 0x12 - Data TLB Miss Exception675* As the name implies, translation is not in the MMU, so search the676* page tables and fix it. The only purpose of this function is to677* load TLB entries from the page table if they exist.678*/679handle_data_tlb_miss_exception:680/* Working registers already saved: R3, R4, R5, R6681* R3 = EAR, R4 = ESR682*/683mfs r11, rpid684nop685686/* If we are faulting a kernel address, we have to use the687* kernel page tables. */688ori r6, r0, CONFIG_KERNEL_START689cmpu r4, r3, r6690bgti r4, ex5691ori r4, r0, swapper_pg_dir692mts rpid, r0 /* TLB will have 0 TID */693nop694bri ex6695696/* Get the PGD for the current thread. */697ex5:698/* get current task address */699addi r4 ,CURRENT_TASK, TOPHYS(0);700lwi r4, r4, TASK_THREAD+PGDIR701ex6:702tophys(r4,r4)703BSRLI(r5,r3,20) /* Create L1 (pgdir/pmd) address */704andi r5, r5, 0xffc705/* Assume pgdir aligned on 4K boundary, no need for "andi r4,r4,0xfffff003" */706or r4, r4, r5707lwi r4, r4, 0 /* Get L1 entry */708andi r5, r4, 0xfffff000 /* Extract L2 (pte) base address */709beqi r5, ex7 /* Bail if no table */710711tophys(r5,r5)712BSRLI(r6,r3,10) /* Compute PTE address */713andi r6, r6, 0xffc714andi r5, r5, 0xfffff003715or r5, r5, r6716lwi r4, r5, 0 /* Get Linux PTE */717718andi r6, r4, _PAGE_PRESENT719beqi r6, ex7720721ori r4, r4, _PAGE_ACCESSED722swi r4, r5, 0723724/* Most of the Linux PTE is ready to load into the TLB LO.725* We set ZSEL, where only the LS-bit determines user access.726* We set execute, because we don't have the granularity to727* properly set this at the page level (Linux problem).728* If shared is set, we cause a zero PID->TID load.729* Many of these bits are software only. Bits we don't set730* here we (properly should) assume have the appropriate value.731*/732brid finish_tlb_load733andni r4, r4, 0x0ce2 /* Make sure 20, 21 are zero */734ex7:735/* The bailout. Restore registers to pre-exception conditions736* and call the heavyweights to help us out.737*/738mts rpid, r11739nop740bri 4741RESTORE_STATE;742bri page_fault_data_trap743744/* 0x13 - Instruction TLB Miss Exception745* Nearly the same as above, except we get our information from746* different registers and bailout to a different point.747*/748handle_instruction_tlb_miss_exception:749/* Working registers already saved: R3, R4, R5, R6750* R3 = ESR751*/752mfs r11, rpid753nop754755/* If we are faulting a kernel address, we have to use the756* kernel page tables.757*/758ori r4, r0, CONFIG_KERNEL_START759cmpu r4, r3, r4760bgti r4, ex8761ori r4, r0, swapper_pg_dir762mts rpid, r0 /* TLB will have 0 TID */763nop764bri ex9765766/* Get the PGD for the current thread. */767ex8:768/* get current task address */769addi r4 ,CURRENT_TASK, TOPHYS(0);770lwi r4, r4, TASK_THREAD+PGDIR771ex9:772tophys(r4,r4)773BSRLI(r5,r3,20) /* Create L1 (pgdir/pmd) address */774andi r5, r5, 0xffc775/* Assume pgdir aligned on 4K boundary, no need for "andi r4,r4,0xfffff003" */776or r4, r4, r5777lwi r4, r4, 0 /* Get L1 entry */778andi r5, r4, 0xfffff000 /* Extract L2 (pte) base address */779beqi r5, ex10 /* Bail if no table */780781tophys(r5,r5)782BSRLI(r6,r3,10) /* Compute PTE address */783andi r6, r6, 0xffc784andi r5, r5, 0xfffff003785or r5, r5, r6786lwi r4, r5, 0 /* Get Linux PTE */787788andi r6, r4, _PAGE_PRESENT789beqi r6, ex10790791ori r4, r4, _PAGE_ACCESSED792swi r4, r5, 0793794/* Most of the Linux PTE is ready to load into the TLB LO.795* We set ZSEL, where only the LS-bit determines user access.796* We set execute, because we don't have the granularity to797* properly set this at the page level (Linux problem).798* If shared is set, we cause a zero PID->TID load.799* Many of these bits are software only. Bits we don't set800* here we (properly should) assume have the appropriate value.801*/802brid finish_tlb_load803andni r4, r4, 0x0ce2 /* Make sure 20, 21 are zero */804ex10:805/* The bailout. Restore registers to pre-exception conditions806* and call the heavyweights to help us out.807*/808mts rpid, r11809nop810bri 4811RESTORE_STATE;812bri page_fault_instr_trap813814/* Both the instruction and data TLB miss get to this point to load the TLB.815* r3 - EA of fault816* r4 - TLB LO (info from Linux PTE)817* r5, r6 - available to use818* PID - loaded with proper value when we get here819* Upon exit, we reload everything and RFI.820* A common place to load the TLB.821*/822tlb_index:823.long 1 /* MS: storing last used tlb index */824finish_tlb_load:825/* MS: load the last used TLB index. */826lwi r5, r0, TOPHYS(tlb_index)827addik r5, r5, 1 /* MS: inc tlb_index -> use next one */828829/* MS: FIXME this is potential fault, because this is mask not count */830andi r5, r5, (MICROBLAZE_TLB_SIZE-1)831ori r6, r0, 1832cmp r31, r5, r6833blti r31, ex12834addik r5, r6, 1835ex12:836/* MS: save back current TLB index */837swi r5, r0, TOPHYS(tlb_index)838839ori r4, r4, _PAGE_HWEXEC /* make it executable */840mts rtlbx, r5 /* MS: save current TLB */841nop842mts rtlblo, r4 /* MS: save to TLB LO */843nop844845/* Create EPN. This is the faulting address plus a static846* set of bits. These are size, valid, E, U0, and ensure847* bits 20 and 21 are zero.848*/849andi r3, r3, 0xfffff000850ori r3, r3, 0x0c0851mts rtlbhi, r3 /* Load TLB HI */852nop853854/* Done...restore registers and get out of here. */855mts rpid, r11856nop857bri 4858RESTORE_STATE;859rted r17, 0860nop861862/* extern void giveup_fpu(struct task_struct *prev)863*864* The MicroBlaze processor may have an FPU, so this should not just865* return: TBD.866*/867.globl giveup_fpu;868.align 4;869giveup_fpu:870bralid r15,0 /* TBD */871nop872873/* At present, this routine just hangs. - extern void abort(void) */874.globl abort;875.align 4;876abort:877br r0878879.globl set_context;880.align 4;881set_context:882mts rpid, r5 /* Shadow TLBs are automatically */883nop884bri 4 /* flushed by changing PID */885rtsd r15,8886nop887888#endif889.end _hw_exception_handler890891#ifdef CONFIG_MMU892/* Unaligned data access exception last on a 4k page for MMU.893* When this is called, we are in virtual mode with exceptions enabled894* and registers 1-13,15,17,18 saved.895*896* R3 = ESR897* R4 = EAR898* R7 = pointer to saved registers (struct pt_regs *regs)899*900* This handler perform the access, and returns via ret_from_exc.901*/902.global _unaligned_data_exception903.ent _unaligned_data_exception904_unaligned_data_exception:905andi r8, r3, 0x3E0; /* Mask and extract the register operand */906BSRLI(r8,r8,2); /* r8 >> 2 = register operand * 8 */907andi r6, r3, 0x400; /* Extract ESR[S] */908bneid r6, ex_sw_vm;909andi r6, r3, 0x800; /* Extract ESR[W] - delay slot */910ex_lw_vm:911beqid r6, ex_lhw_vm;912load1: lbui r5, r4, 0; /* Exception address in r4 - delay slot */913/* Load a word, byte-by-byte from destination address and save it in tmp space*/914addik r6, r0, ex_tmp_data_loc_0;915sbi r5, r6, 0;916load2: lbui r5, r4, 1;917sbi r5, r6, 1;918load3: lbui r5, r4, 2;919sbi r5, r6, 2;920load4: lbui r5, r4, 3;921sbi r5, r6, 3;922brid ex_lw_tail_vm;923/* Get the destination register value into r3 - delay slot */924lwi r3, r6, 0;925ex_lhw_vm:926/* Load a half-word, byte-by-byte from destination address and927* save it in tmp space */928addik r6, r0, ex_tmp_data_loc_0;929sbi r5, r6, 0;930load5: lbui r5, r4, 1;931sbi r5, r6, 1;932lhui r3, r6, 0; /* Get the destination register value into r3 */933ex_lw_tail_vm:934/* Form load_word jump table offset (lw_table_vm + (8 * regnum)) */935addik r5, r8, lw_table_vm;936bra r5;937ex_lw_end_vm: /* Exception handling of load word, ends */938brai ret_from_exc;939ex_sw_vm:940/* Form store_word jump table offset (sw_table_vm + (8 * regnum)) */941addik r5, r8, sw_table_vm;942bra r5;943ex_sw_tail_vm:944addik r5, r0, ex_tmp_data_loc_0;945beqid r6, ex_shw_vm;946swi r3, r5, 0; /* Get the word - delay slot */947/* Store the word, byte-by-byte into destination address */948lbui r3, r5, 0;949store1: sbi r3, r4, 0;950lbui r3, r5, 1;951store2: sbi r3, r4, 1;952lbui r3, r5, 2;953store3: sbi r3, r4, 2;954lbui r3, r5, 3;955brid ret_from_exc;956store4: sbi r3, r4, 3; /* Delay slot */957ex_shw_vm:958/* Store the lower half-word, byte-by-byte into destination address */959#ifdef __MICROBLAZEEL__960lbui r3, r5, 0;961store5: sbi r3, r4, 0;962lbui r3, r5, 1;963brid ret_from_exc;964store6: sbi r3, r4, 1; /* Delay slot */965#else966lbui r3, r5, 2;967store5: sbi r3, r4, 0;968lbui r3, r5, 3;969brid ret_from_exc;970store6: sbi r3, r4, 1; /* Delay slot */971#endif972973ex_sw_end_vm: /* Exception handling of store word, ends. */974975/* We have to prevent cases that get/put_user macros get unaligned pointer976* to bad page area. We have to find out which origin instruction caused it977* and called fixup for that origin instruction not instruction in unaligned978* handler */979ex_unaligned_fixup:980ori r5, r7, 0 /* setup pointer to pt_regs */981lwi r6, r7, PT_PC; /* faulting address is one instruction above */982addik r6, r6, -4 /* for finding proper fixup */983swi r6, r7, PT_PC; /* a save back it to PT_PC */984addik r7, r0, SIGSEGV985/* call bad_page_fault for finding aligned fixup, fixup address is saved986* in PT_PC which is used as return address from exception */987addik r15, r0, ret_from_exc-8 /* setup return address */988brid bad_page_fault989nop990991/* We prevent all load/store because it could failed any attempt to access */992.section __ex_table,"a";993.word load1,ex_unaligned_fixup;994.word load2,ex_unaligned_fixup;995.word load3,ex_unaligned_fixup;996.word load4,ex_unaligned_fixup;997.word load5,ex_unaligned_fixup;998.word store1,ex_unaligned_fixup;999.word store2,ex_unaligned_fixup;1000.word store3,ex_unaligned_fixup;1001.word store4,ex_unaligned_fixup;1002.word store5,ex_unaligned_fixup;1003.word store6,ex_unaligned_fixup;1004.previous;1005.end _unaligned_data_exception1006#endif /* CONFIG_MMU */10071008.global ex_handler_unhandled1009ex_handler_unhandled:1010/* FIXME add handle function for unhandled exception - dump register */1011bri 010121013/*1014* hw_exception_handler Jump Table1015* - Contains code snippets for each register that caused the unalign exception1016* - Hence exception handler is NOT self-modifying1017* - Separate table for load exceptions and store exceptions.1018* - Each table is of size: (8 * 32) = 256 bytes1019*/10201021.section .text1022.align 41023lw_table:1024lw_r0: R3_TO_LWREG (0);1025lw_r1: LWREG_NOP;1026lw_r2: R3_TO_LWREG (2);1027lw_r3: R3_TO_LWREG_V (3);1028lw_r4: R3_TO_LWREG_V (4);1029lw_r5: R3_TO_LWREG_V (5);1030lw_r6: R3_TO_LWREG_V (6);1031lw_r7: R3_TO_LWREG (7);1032lw_r8: R3_TO_LWREG (8);1033lw_r9: R3_TO_LWREG (9);1034lw_r10: R3_TO_LWREG (10);1035lw_r11: R3_TO_LWREG (11);1036lw_r12: R3_TO_LWREG (12);1037lw_r13: R3_TO_LWREG (13);1038lw_r14: R3_TO_LWREG (14);1039lw_r15: R3_TO_LWREG (15);1040lw_r16: R3_TO_LWREG (16);1041lw_r17: LWREG_NOP;1042lw_r18: R3_TO_LWREG (18);1043lw_r19: R3_TO_LWREG (19);1044lw_r20: R3_TO_LWREG (20);1045lw_r21: R3_TO_LWREG (21);1046lw_r22: R3_TO_LWREG (22);1047lw_r23: R3_TO_LWREG (23);1048lw_r24: R3_TO_LWREG (24);1049lw_r25: R3_TO_LWREG (25);1050lw_r26: R3_TO_LWREG (26);1051lw_r27: R3_TO_LWREG (27);1052lw_r28: R3_TO_LWREG (28);1053lw_r29: R3_TO_LWREG (29);1054lw_r30: R3_TO_LWREG (30);1055#ifdef CONFIG_MMU1056lw_r31: R3_TO_LWREG_V (31);1057#else1058lw_r31: R3_TO_LWREG (31);1059#endif10601061sw_table:1062sw_r0: SWREG_TO_R3 (0);1063sw_r1: SWREG_NOP;1064sw_r2: SWREG_TO_R3 (2);1065sw_r3: SWREG_TO_R3_V (3);1066sw_r4: SWREG_TO_R3_V (4);1067sw_r5: SWREG_TO_R3_V (5);1068sw_r6: SWREG_TO_R3_V (6);1069sw_r7: SWREG_TO_R3 (7);1070sw_r8: SWREG_TO_R3 (8);1071sw_r9: SWREG_TO_R3 (9);1072sw_r10: SWREG_TO_R3 (10);1073sw_r11: SWREG_TO_R3 (11);1074sw_r12: SWREG_TO_R3 (12);1075sw_r13: SWREG_TO_R3 (13);1076sw_r14: SWREG_TO_R3 (14);1077sw_r15: SWREG_TO_R3 (15);1078sw_r16: SWREG_TO_R3 (16);1079sw_r17: SWREG_NOP;1080sw_r18: SWREG_TO_R3 (18);1081sw_r19: SWREG_TO_R3 (19);1082sw_r20: SWREG_TO_R3 (20);1083sw_r21: SWREG_TO_R3 (21);1084sw_r22: SWREG_TO_R3 (22);1085sw_r23: SWREG_TO_R3 (23);1086sw_r24: SWREG_TO_R3 (24);1087sw_r25: SWREG_TO_R3 (25);1088sw_r26: SWREG_TO_R3 (26);1089sw_r27: SWREG_TO_R3 (27);1090sw_r28: SWREG_TO_R3 (28);1091sw_r29: SWREG_TO_R3 (29);1092sw_r30: SWREG_TO_R3 (30);1093#ifdef CONFIG_MMU1094sw_r31: SWREG_TO_R3_V (31);1095#else1096sw_r31: SWREG_TO_R3 (31);1097#endif10981099#ifdef CONFIG_MMU1100lw_table_vm:1101lw_r0_vm: R3_TO_LWREG_VM (0);1102lw_r1_vm: R3_TO_LWREG_VM_V (1);1103lw_r2_vm: R3_TO_LWREG_VM_V (2);1104lw_r3_vm: R3_TO_LWREG_VM_V (3);1105lw_r4_vm: R3_TO_LWREG_VM_V (4);1106lw_r5_vm: R3_TO_LWREG_VM_V (5);1107lw_r6_vm: R3_TO_LWREG_VM_V (6);1108lw_r7_vm: R3_TO_LWREG_VM_V (7);1109lw_r8_vm: R3_TO_LWREG_VM_V (8);1110lw_r9_vm: R3_TO_LWREG_VM_V (9);1111lw_r10_vm: R3_TO_LWREG_VM_V (10);1112lw_r11_vm: R3_TO_LWREG_VM_V (11);1113lw_r12_vm: R3_TO_LWREG_VM_V (12);1114lw_r13_vm: R3_TO_LWREG_VM_V (13);1115lw_r14_vm: R3_TO_LWREG_VM (14);1116lw_r15_vm: R3_TO_LWREG_VM_V (15);1117lw_r16_vm: R3_TO_LWREG_VM (16);1118lw_r17_vm: R3_TO_LWREG_VM_V (17);1119lw_r18_vm: R3_TO_LWREG_VM_V (18);1120lw_r19_vm: R3_TO_LWREG_VM (19);1121lw_r20_vm: R3_TO_LWREG_VM (20);1122lw_r21_vm: R3_TO_LWREG_VM (21);1123lw_r22_vm: R3_TO_LWREG_VM (22);1124lw_r23_vm: R3_TO_LWREG_VM (23);1125lw_r24_vm: R3_TO_LWREG_VM (24);1126lw_r25_vm: R3_TO_LWREG_VM (25);1127lw_r26_vm: R3_TO_LWREG_VM (26);1128lw_r27_vm: R3_TO_LWREG_VM (27);1129lw_r28_vm: R3_TO_LWREG_VM (28);1130lw_r29_vm: R3_TO_LWREG_VM (29);1131lw_r30_vm: R3_TO_LWREG_VM (30);1132lw_r31_vm: R3_TO_LWREG_VM_V (31);11331134sw_table_vm:1135sw_r0_vm: SWREG_TO_R3_VM (0);1136sw_r1_vm: SWREG_TO_R3_VM_V (1);1137sw_r2_vm: SWREG_TO_R3_VM_V (2);1138sw_r3_vm: SWREG_TO_R3_VM_V (3);1139sw_r4_vm: SWREG_TO_R3_VM_V (4);1140sw_r5_vm: SWREG_TO_R3_VM_V (5);1141sw_r6_vm: SWREG_TO_R3_VM_V (6);1142sw_r7_vm: SWREG_TO_R3_VM_V (7);1143sw_r8_vm: SWREG_TO_R3_VM_V (8);1144sw_r9_vm: SWREG_TO_R3_VM_V (9);1145sw_r10_vm: SWREG_TO_R3_VM_V (10);1146sw_r11_vm: SWREG_TO_R3_VM_V (11);1147sw_r12_vm: SWREG_TO_R3_VM_V (12);1148sw_r13_vm: SWREG_TO_R3_VM_V (13);1149sw_r14_vm: SWREG_TO_R3_VM (14);1150sw_r15_vm: SWREG_TO_R3_VM_V (15);1151sw_r16_vm: SWREG_TO_R3_VM (16);1152sw_r17_vm: SWREG_TO_R3_VM_V (17);1153sw_r18_vm: SWREG_TO_R3_VM_V (18);1154sw_r19_vm: SWREG_TO_R3_VM (19);1155sw_r20_vm: SWREG_TO_R3_VM (20);1156sw_r21_vm: SWREG_TO_R3_VM (21);1157sw_r22_vm: SWREG_TO_R3_VM (22);1158sw_r23_vm: SWREG_TO_R3_VM (23);1159sw_r24_vm: SWREG_TO_R3_VM (24);1160sw_r25_vm: SWREG_TO_R3_VM (25);1161sw_r26_vm: SWREG_TO_R3_VM (26);1162sw_r27_vm: SWREG_TO_R3_VM (27);1163sw_r28_vm: SWREG_TO_R3_VM (28);1164sw_r29_vm: SWREG_TO_R3_VM (29);1165sw_r30_vm: SWREG_TO_R3_VM (30);1166sw_r31_vm: SWREG_TO_R3_VM_V (31);1167#endif /* CONFIG_MMU */11681169/* Temporary data structures used in the handler */1170.section .data1171.align 41172ex_tmp_data_loc_0:1173.byte 01174ex_tmp_data_loc_1:1175.byte 01176ex_tmp_data_loc_2:1177.byte 01178ex_tmp_data_loc_3:1179.byte 01180ex_reg_op:1181.byte 0118211831184