/*1* arch/xtensa/kernel/align.S2*3* Handle unalignment exceptions in kernel space.4*5* This file is subject to the terms and conditions of the GNU General6* Public License. See the file "COPYING" in the main directory of7* this archive for more details.8*9* Copyright (C) 2001 - 2005 Tensilica, Inc.10*11* Rewritten by Chris Zankel <[email protected]>12*13* Based on work from Joe Taylor <[email protected], [email protected]>14* and Marc Gauthier <[email protected], [email protected]>15*/1617#include <linux/linkage.h>18#include <asm/current.h>19#include <asm/asm-offsets.h>20#include <asm/processor.h>2122#if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION2324/* First-level exception handler for unaligned exceptions.25*26* Note: This handler works only for kernel exceptions. Unaligned user27* access should get a seg fault.28*/2930/* Big and little endian 16-bit values are located in31* different halves of a register. HWORD_START helps to32* abstract the notion of extracting a 16-bit value from a33* register.34* We also have to define new shifting instructions because35* lsb and msb are on 'opposite' ends in a register for36* different endian machines.37*38* Assume a memory region in ascending address:39* 0 1 2 3|4 5 6 740*41* When loading one word into a register, the content of that register is:42* LE 3 2 1 0, 7 6 5 443* BE 0 1 2 3, 4 5 6 744*45* Masking the bits of the higher/lower address means:46* LE X X 0 0, 0 0 X X47* BE 0 0 X X, X X 0 048*49* Shifting to higher/lower addresses, means:50* LE shift left / shift right51* BE shift right / shift left52*53* Extracting 16 bits from a 32 bit reg. value to higher/lower address means:54* LE mask 0 0 X X / shift left55* BE shift left / mask 0 0 X X56*/5758#define UNALIGNED_USER_EXCEPTION5960#if XCHAL_HAVE_BE6162#define HWORD_START 1663#define INSN_OP0 2864#define INSN_T 2465#define INSN_OP1 166667.macro __src_b r, w0, w1; src \r, \w0, \w1; .endm68.macro __ssa8 r; ssa8b \r; .endm69.macro __ssa8r r; ssa8l \r; .endm70.macro __sh r, s; srl \r, \s; .endm71.macro __sl r, s; sll \r, \s; .endm72.macro __exth r, s; extui \r, \s, 0, 16; .endm73.macro __extl r, s; slli \r, \s, 16; .endm7475#else7677#define HWORD_START 078#define INSN_OP0 079#define INSN_T 480#define INSN_OP1 128182.macro __src_b r, w0, w1; src \r, \w1, \w0; .endm83.macro __ssa8 r; ssa8l \r; .endm84.macro __ssa8r r; ssa8b \r; .endm85.macro __sh r, s; sll \r, \s; .endm86.macro __sl r, s; srl \r, \s; .endm87.macro __exth r, s; slli \r, \s, 16; .endm88.macro __extl r, s; extui \r, \s, 0, 16; .endm8990#endif9192/*93* xxxx xxxx = imm8 field94* yyyy = imm4 field95* ssss = s field96* tttt = t field97*98* 16 099* -------------------100* L32I.N yyyy ssss tttt 1000101* S32I.N yyyy ssss tttt 1001102*103* 23 0104* -----------------------------105* res 0000 0010106* L16UI xxxx xxxx 0001 ssss tttt 0010107* L32I xxxx xxxx 0010 ssss tttt 0010108* XXX 0011 ssss tttt 0010109* XXX 0100 ssss tttt 0010110* S16I xxxx xxxx 0101 ssss tttt 0010111* S32I xxxx xxxx 0110 ssss tttt 0010112* XXX 0111 ssss tttt 0010113* XXX 1000 ssss tttt 0010114* L16SI xxxx xxxx 1001 ssss tttt 0010115* XXX 1010 0010116* **L32AI xxxx xxxx 1011 ssss tttt 0010 unsupported117* XXX 1100 0010118* XXX 1101 0010119* XXX 1110 0010120* **S32RI xxxx xxxx 1111 ssss tttt 0010 unsupported121* -----------------------------122* ^ ^ ^123* sub-opcode (NIBBLE_R) -+ | |124* t field (NIBBLE_T) -----------+ |125* major opcode (NIBBLE_OP0) --------------+126*/127128#define OP0_L32I_N 0x8 /* load immediate narrow */129#define OP0_S32I_N 0x9 /* store immediate narrow */130#define OP1_SI_MASK 0x4 /* OP1 bit set for stores */131#define OP1_SI_BIT 2 /* OP1 bit number for stores */132133#define OP1_L32I 0x2134#define OP1_L16UI 0x1135#define OP1_L16SI 0x9136#define OP1_L32AI 0xb137138#define OP1_S32I 0x6139#define OP1_S16I 0x5140#define OP1_S32RI 0xf141142/*143* Entry condition:144*145* a0: trashed, original value saved on stack (PT_AREG0)146* a1: a1147* a2: new stack pointer, original in DEPC148* a3: dispatch table149* depc: a2, original value saved on stack (PT_DEPC)150* excsave_1: a3151*152* PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC153* < VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception154*/155156157ENTRY(fast_unaligned)158159/* Note: We don't expect the address to be aligned on a word160* boundary. After all, the processor generated that exception161* and it would be a hardware fault.162*/163164/* Save some working register */165166s32i a4, a2, PT_AREG4167s32i a5, a2, PT_AREG5168s32i a6, a2, PT_AREG6169s32i a7, a2, PT_AREG7170s32i a8, a2, PT_AREG8171172rsr a0, DEPC173xsr a3, EXCSAVE_1174s32i a0, a2, PT_AREG2175s32i a3, a2, PT_AREG3176177/* Keep value of SAR in a0 */178179rsr a0, SAR180rsr a8, EXCVADDR # load unaligned memory address181182/* Now, identify one of the following load/store instructions.183*184* The only possible danger of a double exception on the185* following l32i instructions is kernel code in vmalloc186* memory. The processor was just executing at the EPC_1187* address, and indeed, already fetched the instruction. That188* guarantees a TLB mapping, which hasn't been replaced by189* this unaligned exception handler that uses only static TLB190* mappings. However, high-level interrupt handlers might191* modify TLB entries, so for the generic case, we register a192* TABLE_FIXUP handler here, too.193*/194195/* a3...a6 saved on stack, a2 = SP */196197/* Extract the instruction that caused the unaligned access. */198199rsr a7, EPC_1 # load exception address200movi a3, ~3201and a3, a3, a7 # mask lower bits202203l32i a4, a3, 0 # load 2 words204l32i a5, a3, 4205206__ssa8 a7207__src_b a4, a4, a5 # a4 has the instruction208209/* Analyze the instruction (load or store?). */210211extui a5, a4, INSN_OP0, 4 # get insn.op0 nibble212213#if XCHAL_HAVE_DENSITY214_beqi a5, OP0_L32I_N, .Lload # L32I.N, jump215addi a6, a5, -OP0_S32I_N216_beqz a6, .Lstore # S32I.N, do a store217#endif218/* 'store indicator bit' not set, jump */219_bbci.l a4, OP1_SI_BIT + INSN_OP1, .Lload220221/* Store: Jump to table entry to get the value in the source register.*/222223.Lstore:movi a5, .Lstore_table # table224extui a6, a4, INSN_T, 4 # get source register225addx8 a5, a6, a5226jx a5 # jump into table227228/* Invalid instruction, CRITICAL! */229.Linvalid_instruction_load:230j .Linvalid_instruction231232/* Load: Load memory address. */233234.Lload: movi a3, ~3235and a3, a3, a8 # align memory address236237__ssa8 a8238#ifdef UNALIGNED_USER_EXCEPTION239addi a3, a3, 8240l32e a5, a3, -8241l32e a6, a3, -4242#else243l32i a5, a3, 0244l32i a6, a3, 4245#endif246__src_b a3, a5, a6 # a3 has the data word247248#if XCHAL_HAVE_DENSITY249addi a7, a7, 2 # increment PC (assume 16-bit insn)250251extui a5, a4, INSN_OP0, 4252_beqi a5, OP0_L32I_N, 1f # l32i.n: jump253254addi a7, a7, 1255#else256addi a7, a7, 3257#endif258259extui a5, a4, INSN_OP1, 4260_beqi a5, OP1_L32I, 1f # l32i: jump261262extui a3, a3, 0, 16 # extract lower 16 bits263_beqi a5, OP1_L16UI, 1f264addi a5, a5, -OP1_L16SI265_bnez a5, .Linvalid_instruction_load266267/* sign extend value */268269slli a3, a3, 16270srai a3, a3, 16271272/* Set target register. */2732741:275276#if XCHAL_HAVE_LOOPS277rsr a5, LEND # check if we reached LEND278bne a7, a5, 1f279rsr a5, LCOUNT # and LCOUNT != 0280beqz a5, 1f281addi a5, a5, -1 # decrement LCOUNT and set282rsr a7, LBEG # set PC to LBEGIN283wsr a5, LCOUNT284#endif2852861: wsr a7, EPC_1 # skip load instruction287extui a4, a4, INSN_T, 4 # extract target register288movi a5, .Lload_table289addx8 a4, a4, a5290jx a4 # jump to entry for target register291292.align 8293.Lload_table:294s32i a3, a2, PT_AREG0; _j .Lexit; .align 8295mov a1, a3; _j .Lexit; .align 8 # fishy??296s32i a3, a2, PT_AREG2; _j .Lexit; .align 8297s32i a3, a2, PT_AREG3; _j .Lexit; .align 8298s32i a3, a2, PT_AREG4; _j .Lexit; .align 8299s32i a3, a2, PT_AREG5; _j .Lexit; .align 8300s32i a3, a2, PT_AREG6; _j .Lexit; .align 8301s32i a3, a2, PT_AREG7; _j .Lexit; .align 8302s32i a3, a2, PT_AREG8; _j .Lexit; .align 8303mov a9, a3 ; _j .Lexit; .align 8304mov a10, a3 ; _j .Lexit; .align 8305mov a11, a3 ; _j .Lexit; .align 8306mov a12, a3 ; _j .Lexit; .align 8307mov a13, a3 ; _j .Lexit; .align 8308mov a14, a3 ; _j .Lexit; .align 8309mov a15, a3 ; _j .Lexit; .align 8310311.Lstore_table:312l32i a3, a2, PT_AREG0; _j 1f; .align 8313mov a3, a1; _j 1f; .align 8 # fishy??314l32i a3, a2, PT_AREG2; _j 1f; .align 8315l32i a3, a2, PT_AREG3; _j 1f; .align 8316l32i a3, a2, PT_AREG4; _j 1f; .align 8317l32i a3, a2, PT_AREG5; _j 1f; .align 8318l32i a3, a2, PT_AREG6; _j 1f; .align 8319l32i a3, a2, PT_AREG7; _j 1f; .align 8320l32i a3, a2, PT_AREG8; _j 1f; .align 8321mov a3, a9 ; _j 1f; .align 8322mov a3, a10 ; _j 1f; .align 8323mov a3, a11 ; _j 1f; .align 8324mov a3, a12 ; _j 1f; .align 8325mov a3, a13 ; _j 1f; .align 8326mov a3, a14 ; _j 1f; .align 8327mov a3, a15 ; _j 1f; .align 83283291: # a7: instruction pointer, a4: instruction, a3: value330331movi a6, 0 # mask: ffffffff:00000000332333#if XCHAL_HAVE_DENSITY334addi a7, a7, 2 # incr. PC,assume 16-bit instruction335336extui a5, a4, INSN_OP0, 4 # extract OP0337addi a5, a5, -OP0_S32I_N338_beqz a5, 1f # s32i.n: jump339340addi a7, a7, 1 # increment PC, 32-bit instruction341#else342addi a7, a7, 3 # increment PC, 32-bit instruction343#endif344345extui a5, a4, INSN_OP1, 4 # extract OP1346_beqi a5, OP1_S32I, 1f # jump if 32 bit store347_bnei a5, OP1_S16I, .Linvalid_instruction_store348349movi a5, -1350__extl a3, a3 # get 16-bit value351__exth a6, a5 # get 16-bit mask ffffffff:ffff0000352353/* Get memory address */3543551:356#if XCHAL_HAVE_LOOPS357rsr a4, LEND # check if we reached LEND358bne a7, a4, 1f359rsr a4, LCOUNT # and LCOUNT != 0360beqz a4, 1f361addi a4, a4, -1 # decrement LCOUNT and set362rsr a7, LBEG # set PC to LBEGIN363wsr a4, LCOUNT364#endif3653661: wsr a7, EPC_1 # skip store instruction367movi a4, ~3368and a4, a4, a8 # align memory address369370/* Insert value into memory */371372movi a5, -1 # mask: ffffffff:XXXX0000373#ifdef UNALIGNED_USER_EXCEPTION374addi a4, a4, 8375#endif376377__ssa8r a8378__src_b a7, a5, a6 # lo-mask F..F0..0 (BE) 0..0F..F (LE)379__src_b a6, a6, a5 # hi-mask 0..0F..F (BE) F..F0..0 (LE)380#ifdef UNALIGNED_USER_EXCEPTION381l32e a5, a4, -8382#else383l32i a5, a4, 0 # load lower address word384#endif385and a5, a5, a7 # mask386__sh a7, a3 # shift value387or a5, a5, a7 # or with original value388#ifdef UNALIGNED_USER_EXCEPTION389s32e a5, a4, -8390l32e a7, a4, -4391#else392s32i a5, a4, 0 # store393l32i a7, a4, 4 # same for upper address word394#endif395__sl a5, a3396and a6, a7, a6397or a6, a6, a5398#ifdef UNALIGNED_USER_EXCEPTION399s32e a6, a4, -4400#else401s32i a6, a4, 4402#endif403404/* Done. restore stack and return */405406.Lexit:407movi a4, 0408rsr a3, EXCSAVE_1409s32i a4, a3, EXC_TABLE_FIXUP410411/* Restore working register */412413l32i a8, a2, PT_AREG8414l32i a7, a2, PT_AREG7415l32i a6, a2, PT_AREG6416l32i a5, a2, PT_AREG5417l32i a4, a2, PT_AREG4418l32i a3, a2, PT_AREG3419420/* restore SAR and return */421422wsr a0, SAR423l32i a0, a2, PT_AREG0424l32i a2, a2, PT_AREG2425rfe426427/* We cannot handle this exception. */428429.extern _kernel_exception430.Linvalid_instruction_store:431.Linvalid_instruction:432433/* Restore a4...a8 and SAR, set SP, and jump to default exception. */434435l32i a8, a2, PT_AREG8436l32i a7, a2, PT_AREG7437l32i a6, a2, PT_AREG6438l32i a5, a2, PT_AREG5439l32i a4, a2, PT_AREG4440wsr a0, SAR441mov a1, a2442443rsr a0, PS444bbsi.l a2, PS_UM_BIT, 1f # jump if user mode445446movi a0, _kernel_exception447jx a04484491: movi a0, _user_exception450jx a0451452453#endif /* XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION */454455456457