2 * arch/xtensa/kernel/align.S
4 * Handle unalignment exceptions in kernel space.
6 * This file is subject to the terms and conditions of the GNU General
7 * Public License. See the file "COPYING" in the main directory of
8 * this archive for more details.
10 * Copyright (C) 2001 - 2005 Tensilica, Inc.
11 * Copyright (C) 2014 Cadence Design Systems Inc.
19 #include <linux/linkage.h>
20 #include <asm/current.h>
21 #include <asm/asm-offsets.h>
22 #include <asm/asmmacro.h>
23 #include <asm/processor.h>
25 #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
27 /* First-level exception handler for unaligned exceptions.
29 * Note: This handler works only for kernel exceptions. Unaligned user
30 * access should get a seg fault.
33 /* Big and little endian 16-bit values are located in
34 * different halves of a register. HWORD_START helps to
35 * abstract the notion of extracting a 16-bit value from a
37 * We also have to define new shifting instructions because
38 * lsb and msb are on 'opposite' ends in a register for
39 * different endian machines.
41 * Assume a memory region in ascending address:
44 * When loading one word into a register, the content of that register is:
48 * Masking the bits of the higher/lower address means:
52 * Shifting to higher/lower addresses, means:
53 * LE shift left / shift right
54 * BE shift right / shift left
56 * Extracting 16 bits from a 32 bit reg. value to higher/lower address means:
57 * LE mask 0 0 X X / shift left
58 * BE shift left / mask 0 0 X X
61 #if XCHAL_HAVE_WINDOWED
62 #define UNALIGNED_USER_EXCEPTION
67 #define HWORD_START 16
72 .macro __ssa8r r; ssa8l \r; .endm
73 .macro __sh r, s; srl \r, \s; .endm
74 .macro __sl r, s; sll \r, \s; .endm
75 .macro __exth r, s; extui \r, \s, 0, 16; .endm
76 .macro __extl r, s; slli \r, \s, 16; .endm
85 .macro __ssa8r r; ssa8b \r; .endm
86 .macro __sh r, s; sll \r, \s; .endm
87 .macro __sl r, s; srl \r, \s; .endm
88 .macro __exth r, s; slli \r, \s, 16; .endm
89 .macro __extl r, s; extui \r, \s, 0, 16; .endm
94 * xxxx xxxx = imm8 field
100 * -------------------
101 * L32I.N yyyy ssss tttt 1000
102 * S32I.N yyyy ssss tttt 1001
105 * -----------------------------
107 * L16UI xxxx xxxx 0001 ssss tttt 0010
108 * L32I xxxx xxxx 0010 ssss tttt 0010
109 * XXX 0011 ssss tttt 0010
110 * XXX 0100 ssss tttt 0010
111 * S16I xxxx xxxx 0101 ssss tttt 0010
112 * S32I xxxx xxxx 0110 ssss tttt 0010
113 * XXX 0111 ssss tttt 0010
114 * XXX 1000 ssss tttt 0010
115 * L16SI xxxx xxxx 1001 ssss tttt 0010
117 * **L32AI xxxx xxxx 1011 ssss tttt 0010 unsupported
121 * **S32RI xxxx xxxx 1111 ssss tttt 0010 unsupported
122 * -----------------------------
124 * sub-opcode (NIBBLE_R) -+ | |
125 * t field (NIBBLE_T) -----------+ |
126 * major opcode (NIBBLE_OP0) --------------+
129 #define OP0_L32I_N 0x8 /* load immediate narrow */
130 #define OP0_S32I_N 0x9 /* store immediate narrow */
131 #define OP1_SI_MASK 0x4 /* OP1 bit set for stores */
132 #define OP1_SI_BIT 2 /* OP1 bit number for stores */
135 #define OP1_L16UI 0x1
136 #define OP1_L16SI 0x9
137 #define OP1_L32AI 0xb
141 #define OP1_S32RI 0xf
146 * a0: trashed, original value saved on stack (PT_AREG0)
148 * a2: new stack pointer, original in DEPC
150 * depc: a2, original value saved on stack (PT_DEPC)
151 * excsave_1: dispatch table
153 * PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
154 * < VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
158 ENTRY(fast_unaligned)
160 /* Note: We don't expect the address to be aligned on a word
161 * boundary. After all, the processor generated that exception
162 * and it would be a hardware fault.
165 /* Save some working register */
167 s32i a4, a2, PT_AREG4
168 s32i a5, a2, PT_AREG5
169 s32i a6, a2, PT_AREG6
170 s32i a7, a2, PT_AREG7
171 s32i a8, a2, PT_AREG8
174 s32i a0, a2, PT_AREG2
175 s32i a3, a2, PT_AREG3
178 movi a4, fast_unaligned_fixup
179 s32i a4, a3, EXC_TABLE_FIXUP
181 /* Keep value of SAR in a0 */
184 rsr a8, excvaddr # load unaligned memory address
186 /* Now, identify one of the following load/store instructions.
188 * The only possible danger of a double exception on the
189 * following l32i instructions is kernel code in vmalloc
190 * memory. The processor was just executing at the EPC_1
191 * address, and indeed, already fetched the instruction. That
192 * guarantees a TLB mapping, which hasn't been replaced by
193 * this unaligned exception handler that uses only static TLB
194 * mappings. However, high-level interrupt handlers might
195 * modify TLB entries, so for the generic case, we register a
196 * TABLE_FIXUP handler here, too.
199 /* a3...a6 saved on stack, a2 = SP */
201 /* Extract the instruction that caused the unaligned access. */
203 rsr a7, epc1 # load exception address
205 and a3, a3, a7 # mask lower bits
207 l32i a4, a3, 0 # load 2 words
211 __src_b a4, a4, a5 # a4 has the instruction
213 /* Analyze the instruction (load or store?). */
215 extui a5, a4, INSN_OP0, 4 # get insn.op0 nibble
217 #if XCHAL_HAVE_DENSITY
218 _beqi a5, OP0_L32I_N, .Lload # L32I.N, jump
219 addi a6, a5, -OP0_S32I_N
220 _beqz a6, .Lstore # S32I.N, do a store
222 /* 'store indicator bit' not set, jump */
223 _bbci.l a4, OP1_SI_BIT + INSN_OP1, .Lload
225 /* Store: Jump to table entry to get the value in the source register.*/
227 .Lstore:movi a5, .Lstore_table # table
228 extui a6, a4, INSN_T, 4 # get source register
230 jx a5 # jump into table
232 /* Load: Load memory address. */
235 and a3, a3, a8 # align memory address
238 #ifdef UNALIGNED_USER_EXCEPTION
246 __src_b a3, a5, a6 # a3 has the data word
248 #if XCHAL_HAVE_DENSITY
249 addi a7, a7, 2 # increment PC (assume 16-bit insn)
251 extui a5, a4, INSN_OP0, 4
252 _beqi a5, OP0_L32I_N, 1f # l32i.n: jump
259 extui a5, a4, INSN_OP1, 4
260 _beqi a5, OP1_L32I, 1f # l32i: jump
262 extui a3, a3, 0, 16 # extract lower 16 bits
263 _beqi a5, OP1_L16UI, 1f
264 addi a5, a5, -OP1_L16SI
265 _bnez a5, .Linvalid_instruction_load
267 /* sign extend value */
272 /* Set target register. */
275 extui a4, a4, INSN_T, 4 # extract target register
276 movi a5, .Lload_table
278 jx a4 # jump to entry for target register
282 s32i a3, a2, PT_AREG0; _j .Lexit; .align 8
283 mov a1, a3; _j .Lexit; .align 8 # fishy??
284 s32i a3, a2, PT_AREG2; _j .Lexit; .align 8
285 s32i a3, a2, PT_AREG3; _j .Lexit; .align 8
286 s32i a3, a2, PT_AREG4; _j .Lexit; .align 8
287 s32i a3, a2, PT_AREG5; _j .Lexit; .align 8
288 s32i a3, a2, PT_AREG6; _j .Lexit; .align 8
289 s32i a3, a2, PT_AREG7; _j .Lexit; .align 8
290 s32i a3, a2, PT_AREG8; _j .Lexit; .align 8
291 mov a9, a3 ; _j .Lexit; .align 8
292 mov a10, a3 ; _j .Lexit; .align 8
293 mov a11, a3 ; _j .Lexit; .align 8
294 mov a12, a3 ; _j .Lexit; .align 8
295 mov a13, a3 ; _j .Lexit; .align 8
296 mov a14, a3 ; _j .Lexit; .align 8
297 mov a15, a3 ; _j .Lexit; .align 8
300 l32i a3, a2, PT_AREG0; _j 1f; .align 8
301 mov a3, a1; _j 1f; .align 8 # fishy??
302 l32i a3, a2, PT_AREG2; _j 1f; .align 8
303 l32i a3, a2, PT_AREG3; _j 1f; .align 8
304 l32i a3, a2, PT_AREG4; _j 1f; .align 8
305 l32i a3, a2, PT_AREG5; _j 1f; .align 8
306 l32i a3, a2, PT_AREG6; _j 1f; .align 8
307 l32i a3, a2, PT_AREG7; _j 1f; .align 8
308 l32i a3, a2, PT_AREG8; _j 1f; .align 8
309 mov a3, a9 ; _j 1f; .align 8
310 mov a3, a10 ; _j 1f; .align 8
311 mov a3, a11 ; _j 1f; .align 8
312 mov a3, a12 ; _j 1f; .align 8
313 mov a3, a13 ; _j 1f; .align 8
314 mov a3, a14 ; _j 1f; .align 8
315 mov a3, a15 ; _j 1f; .align 8
317 /* We cannot handle this exception. */
319 .extern _kernel_exception
320 .Linvalid_instruction_load:
321 .Linvalid_instruction_store:
325 s32i a4, a3, EXC_TABLE_FIXUP
327 /* Restore a4...a8 and SAR, set SP, and jump to default exception. */
329 l32i a8, a2, PT_AREG8
330 l32i a7, a2, PT_AREG7
331 l32i a6, a2, PT_AREG6
332 l32i a5, a2, PT_AREG5
333 l32i a4, a2, PT_AREG4
338 bbsi.l a0, PS_UM_BIT, 2f # jump if user mode
340 movi a0, _kernel_exception
343 2: movi a0, _user_exception
346 1: # a7: instruction pointer, a4: instruction, a3: value
348 movi a6, 0 # mask: ffffffff:00000000
350 #if XCHAL_HAVE_DENSITY
351 addi a7, a7, 2 # incr. PC,assume 16-bit instruction
353 extui a5, a4, INSN_OP0, 4 # extract OP0
354 addi a5, a5, -OP0_S32I_N
355 _beqz a5, 1f # s32i.n: jump
357 addi a7, a7, 1 # increment PC, 32-bit instruction
359 addi a7, a7, 3 # increment PC, 32-bit instruction
362 extui a5, a4, INSN_OP1, 4 # extract OP1
363 _beqi a5, OP1_S32I, 1f # jump if 32 bit store
364 _bnei a5, OP1_S16I, .Linvalid_instruction_store
367 __extl a3, a3 # get 16-bit value
368 __exth a6, a5 # get 16-bit mask ffffffff:ffff0000
370 /* Get memory address */
374 and a4, a4, a8 # align memory address
376 /* Insert value into memory */
378 movi a5, -1 # mask: ffffffff:XXXX0000
379 #ifdef UNALIGNED_USER_EXCEPTION
384 __src_b a8, a5, a6 # lo-mask F..F0..0 (BE) 0..0F..F (LE)
385 __src_b a6, a6, a5 # hi-mask 0..0F..F (BE) F..F0..0 (LE)
386 #ifdef UNALIGNED_USER_EXCEPTION
389 l32i a5, a4, 0 # load lower address word
391 and a5, a5, a8 # mask
392 __sh a8, a3 # shift value
393 or a5, a5, a8 # or with original value
394 #ifdef UNALIGNED_USER_EXCEPTION
398 s32i a5, a4, 0 # store
399 l32i a8, a4, 4 # same for upper address word
404 #ifdef UNALIGNED_USER_EXCEPTION
412 rsr a4, lend # check if we reached LEND
414 rsr a4, lcount # and LCOUNT != 0
416 addi a4, a4, -1 # decrement LCOUNT and set
417 rsr a7, lbeg # set PC to LBEGIN
421 1: wsr a7, epc1 # skip emulated instruction
423 /* Update icount if we're single-stepping in userspace. */
426 bgeui a4, LOCKLEVEL + 1, 1f
433 s32i a4, a3, EXC_TABLE_FIXUP
435 /* Restore working register */
437 l32i a8, a2, PT_AREG8
438 l32i a7, a2, PT_AREG7
439 l32i a6, a2, PT_AREG6
440 l32i a5, a2, PT_AREG5
441 l32i a4, a2, PT_AREG4
442 l32i a3, a2, PT_AREG3
444 /* restore SAR and return */
447 l32i a0, a2, PT_AREG0
448 l32i a2, a2, PT_AREG2
451 ENDPROC(fast_unaligned)
453 ENTRY(fast_unaligned_fixup)
455 l32i a2, a3, EXC_TABLE_DOUBLE_SAVE
458 l32i a8, a2, PT_AREG8
459 l32i a7, a2, PT_AREG7
460 l32i a6, a2, PT_AREG6
461 l32i a5, a2, PT_AREG5
462 l32i a4, a2, PT_AREG4
463 l32i a0, a2, PT_AREG2
464 xsr a0, depc # restore depc and a0
468 s32i a0, a2, PT_DEPC # mark as a regular exception
471 bbsi.l a0, PS_UM_BIT, 1f # jump if user mode
474 addx4 a0, a0, a3 # find entry in table
475 l32i a0, a0, EXC_TABLE_FAST_KERNEL # load handler
476 l32i a3, a2, PT_AREG3
480 addx4 a0, a0, a3 # find entry in table
481 l32i a0, a0, EXC_TABLE_FAST_USER # load handler
482 l32i a3, a2, PT_AREG3
485 ENDPROC(fast_unaligned_fixup)
487 #endif /* XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION */