tile: fast-path unaligned memory access for tilegx
[cascardo/linux.git] / arch / tile / kernel / intvec_64.S
1 /*
2  * Copyright 2011 Tilera Corporation. All Rights Reserved.
3  *
4  *   This program is free software; you can redistribute it and/or
5  *   modify it under the terms of the GNU General Public License
6  *   as published by the Free Software Foundation, version 2.
7  *
8  *   This program is distributed in the hope that it will be useful, but
9  *   WITHOUT ANY WARRANTY; without even the implied warranty of
10  *   MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11  *   NON INFRINGEMENT.  See the GNU General Public License for
12  *   more details.
13  *
14  * Linux interrupt vectors.
15  */
16
17 #include <linux/linkage.h>
18 #include <linux/errno.h>
19 #include <linux/unistd.h>
20 #include <linux/init.h>
21 #include <asm/ptrace.h>
22 #include <asm/thread_info.h>
23 #include <asm/irqflags.h>
24 #include <asm/asm-offsets.h>
25 #include <asm/types.h>
26 #include <asm/traps.h>
27 #include <asm/signal.h>
28 #include <hv/hypervisor.h>
29 #include <arch/abi.h>
30 #include <arch/interrupts.h>
31 #include <arch/spr_def.h>
32
33 #ifdef CONFIG_PREEMPT
34 # error "No support for kernel preemption currently"
35 #endif
36
37 #define PTREGS_PTR(reg, ptreg) addli reg, sp, C_ABI_SAVE_AREA_SIZE + (ptreg)
38
39 #define PTREGS_OFFSET_SYSCALL PTREGS_OFFSET_REG(TREG_SYSCALL_NR)
40
41
42         .macro  push_reg reg, ptr=sp, delta=-8
43         {
44          st     \ptr, \reg
45          addli  \ptr, \ptr, \delta
46         }
47         .endm
48
49         .macro  pop_reg reg, ptr=sp, delta=8
50         {
51          ld     \reg, \ptr
52          addli  \ptr, \ptr, \delta
53         }
54         .endm
55
56         .macro  pop_reg_zero reg, zreg, ptr=sp, delta=8
57         {
58          move   \zreg, zero
59          ld     \reg, \ptr
60          addi   \ptr, \ptr, \delta
61         }
62         .endm
63
64         .macro  push_extra_callee_saves reg
65         PTREGS_PTR(\reg, PTREGS_OFFSET_REG(51))
66         push_reg r51, \reg
67         push_reg r50, \reg
68         push_reg r49, \reg
69         push_reg r48, \reg
70         push_reg r47, \reg
71         push_reg r46, \reg
72         push_reg r45, \reg
73         push_reg r44, \reg
74         push_reg r43, \reg
75         push_reg r42, \reg
76         push_reg r41, \reg
77         push_reg r40, \reg
78         push_reg r39, \reg
79         push_reg r38, \reg
80         push_reg r37, \reg
81         push_reg r36, \reg
82         push_reg r35, \reg
83         push_reg r34, \reg, PTREGS_OFFSET_BASE - PTREGS_OFFSET_REG(34)
84         .endm
85
86         .macro  panic str
87         .pushsection .rodata, "a"
88 1:
89         .asciz  "\str"
90         .popsection
91         {
92          moveli r0, hw2_last(1b)
93         }
94         {
95          shl16insli r0, r0, hw1(1b)
96         }
97         {
98          shl16insli r0, r0, hw0(1b)
99          jal    panic
100         }
101         .endm
102
103         /*
104          * Unalign data exception fast handling: In order to handle
105          * unaligned data access, a fast JIT version is generated and stored
106          * in a specific area in user space. We first need to do a quick poke
107          * to see if the JIT is available. We use certain bits in the fault
108          * PC (3 to 9 is used for 16KB page size) as index to address the JIT
109          * code area. The first 64bit word is the fault PC, and the 2nd one is
110          * the fault bundle itself. If these 2 words both match, then we
111          * directly "iret" to JIT code. If not, a slow path is invoked to
112          * generate new JIT code. Note: the current JIT code WILL be
113          * overwritten if it existed. So, ideally we can handle 128 unalign
114          * fixups via JIT. For lookup efficiency and to effectively support
115          * tight loops with multiple unaligned reference, a simple
116          * direct-mapped cache is used.
117          *
118          * SPR_EX_CONTEXT_K_0 is modified to return to JIT code.
119          * SPR_EX_CONTEXT_K_1 has ICS set.
120          * SPR_EX_CONTEXT_0_0 is setup to user program's next PC.
121          * SPR_EX_CONTEXT_0_1 = 0.
122          */
123         .macro int_hand_unalign_fast  vecnum, vecname
124         .org  (\vecnum << 8)
125 intvec_\vecname:
126         /* Put r3 in SPR_SYSTEM_SAVE_K_1.  */
127         mtspr   SPR_SYSTEM_SAVE_K_1, r3
128
129         mfspr   r3, SPR_EX_CONTEXT_K_1
130         /*
131          * Examine if exception comes from user without ICS set.
132          * If not, just go directly to the slow path.
133          */
134         bnez    r3, hand_unalign_slow_nonuser
135
136         mfspr   r3, SPR_SYSTEM_SAVE_K_0
137
138         /* Get &thread_info->unalign_jit_tmp[0] in r3. */
139         mm      r3, zero, LOG2_THREAD_SIZE, 63
140 #if THREAD_SIZE < 65536
141         addli   r3, r3, -(PAGE_SIZE - THREAD_INFO_UNALIGN_JIT_TMP_OFFSET)
142 #else
143         addli   r3, r3, -(PAGE_SIZE/2)
144         addli   r3, r3, -(PAGE_SIZE/2 - THREAD_INFO_UNALIGN_JIT_TMP_OFFSET)
145 #endif
146
147         /*
148          * Save r0, r1, r2 into thread_info array r3 points to
149          * from low to high memory in order.
150          */
151         st_add  r3, r0, 8
152         st_add  r3, r1, 8
153         {
154          st_add r3, r2, 8
155          andi   r2, sp, 7
156         }
157
158         /* Save stored r3 value so we can revert it on a page fault. */
159         mfspr   r1, SPR_SYSTEM_SAVE_K_1
160         st      r3, r1
161
162         {
163          /* Generate a SIGBUS if sp is not 8-byte aligned. */
164          bnez   r2, hand_unalign_slow_badsp
165         }
166
167         /*
168          * Get the thread_info in r0; load r1 with pc. Set the low bit of sp
169          * as an indicator to the page fault code in case we fault.
170          */
171         {
172          ori    sp, sp, 1
173          mfspr  r1, SPR_EX_CONTEXT_K_0
174         }
175
176         /* Add the jit_info offset in thread_info; extract r1 [3:9] into r2. */
177         {
178          addli  r0, r3, THREAD_INFO_UNALIGN_JIT_BASE_OFFSET - \
179           (THREAD_INFO_UNALIGN_JIT_TMP_OFFSET + (3 * 8))
180          bfextu r2, r1, 3, (2 + PAGE_SHIFT - UNALIGN_JIT_SHIFT)
181         }
182
183         /* Load the jit_info; multiply r2 by 128. */
184         {
185          ld     r0, r0
186          shli   r2, r2, UNALIGN_JIT_SHIFT
187         }
188
189         /*
190          * If r0 is NULL, the JIT page is not mapped, so go to slow path;
191          * add offset r2 to r0 at the same time.
192          */
193         {
194          beqz   r0, hand_unalign_slow
195          add    r2, r0, r2
196         }
197
198         /*
199          * We are loading from userspace (both the JIT info PC and
200          * instruction word, and the instruction word we executed)
201          * and since either could fault while holding the interrupt
202          * critical section, we must tag this region and check it in
203          * do_page_fault() to handle it properly.
204          */
205 ENTRY(__start_unalign_asm_code)
206
207         /* Load first word of JIT in r0 and increment r2 by 8. */
208         ld_add  r0, r2, 8
209
210         /*
211          * Compare the PC with the 1st word in JIT; load the fault bundle
212          * into r1.
213          */
214         {
215          cmpeq  r0, r0, r1
216          ld     r1, r1
217         }
218
219         /* Go to slow path if PC doesn't match. */
220         beqz    r0, hand_unalign_slow
221
222         /*
223          * Load the 2nd word of JIT, which is supposed to be the fault
224          * bundle for a cache hit. Increment r2; after this bundle r2 will
225          * point to the potential start of the JIT code we want to run.
226          */
227         ld_add  r0, r2, 8
228
229         /* No further accesses to userspace are done after this point. */
230 ENTRY(__end_unalign_asm_code)
231
232         /* Compare the real bundle with what is saved in the JIT area. */
233         {
234          cmpeq  r0, r1, r0
235          mtspr  SPR_EX_CONTEXT_0_1, zero
236         }
237
238         /* Go to slow path if the fault bundle does not match. */
239         beqz    r0, hand_unalign_slow
240
241         /*
242          * A cache hit is found.
243          * r2 points to start of JIT code (3rd word).
244          * r0 is the fault pc.
245          * r1 is the fault bundle.
246          * Reset the low bit of sp.
247          */
248         {
249          mfspr  r0, SPR_EX_CONTEXT_K_0
250          andi   sp, sp, ~1
251         }
252
253         /* Write r2 into EX_CONTEXT_K_0 and increment PC. */
254         {
255          mtspr  SPR_EX_CONTEXT_K_0, r2
256          addi   r0, r0, 8
257         }
258
259         /*
260          * Set ICS on kernel EX_CONTEXT_K_1 in order to "iret" to
261          * user with ICS set. This way, if the JIT fixup causes another
262          * unalign exception (which shouldn't be possible) the user
263          * process will be terminated with SIGBUS. Also, our fixup will
264          * run without interleaving with external interrupts.
265          * Each fixup is at most 14 bundles, so it won't hold ICS for long.
266          */
267         {
268          movei  r1, PL_ICS_EX1(USER_PL, 1)
269          mtspr  SPR_EX_CONTEXT_0_0, r0
270         }
271
272         {
273          mtspr  SPR_EX_CONTEXT_K_1, r1
274          addi   r3, r3, -(3 * 8)
275         }
276
277         /* Restore r0..r3. */
278         ld_add  r0, r3, 8
279         ld_add  r1, r3, 8
280         ld_add  r2, r3, 8
281         ld      r3, r3
282
283         iret
284         ENDPROC(intvec_\vecname)
285         .endm
286
287 #ifdef __COLLECT_LINKER_FEEDBACK__
288         .pushsection .text.intvec_feedback,"ax"
289 intvec_feedback:
290         .popsection
291 #endif
292
293         /*
294          * Default interrupt handler.
295          *
296          * vecnum is where we'll put this code.
297          * c_routine is the C routine we'll call.
298          *
299          * The C routine is passed two arguments:
300          * - A pointer to the pt_regs state.
301          * - The interrupt vector number.
302          *
303          * The "processing" argument specifies the code for processing
304          * the interrupt. Defaults to "handle_interrupt".
305          */
306         .macro __int_hand vecnum, vecname, c_routine,processing=handle_interrupt
307 intvec_\vecname:
308         /* Temporarily save a register so we have somewhere to work. */
309
310         mtspr   SPR_SYSTEM_SAVE_K_1, r0
311         mfspr   r0, SPR_EX_CONTEXT_K_1
312
313         /*
314          * The unalign data fastpath code sets the low bit in sp to
315          * force us to reset it here on fault.
316          */
317         {
318          blbs   sp, 2f
319          andi   r0, r0, SPR_EX_CONTEXT_1_1__PL_MASK  /* mask off ICS */
320         }
321
322         .ifc    \vecnum, INT_DOUBLE_FAULT
323         /*
324          * For double-faults from user-space, fall through to the normal
325          * register save and stack setup path.  Otherwise, it's the
326          * hypervisor giving us one last chance to dump diagnostics, and we
327          * branch to the kernel_double_fault routine to do so.
328          */
329         beqz    r0, 1f
330         j       _kernel_double_fault
331 1:
332         .else
333         /*
334          * If we're coming from user-space, then set sp to the top of
335          * the kernel stack.  Otherwise, assume sp is already valid.
336          */
337         {
338          bnez   r0, 0f
339          move   r0, sp
340         }
341         .endif
342
343         .ifc    \c_routine, do_page_fault
344         /*
345          * The page_fault handler may be downcalled directly by the
346          * hypervisor even when Linux is running and has ICS set.
347          *
348          * In this case the contents of EX_CONTEXT_K_1 reflect the
349          * previous fault and can't be relied on to choose whether or
350          * not to reinitialize the stack pointer.  So we add a test
351          * to see whether SYSTEM_SAVE_K_2 has the high bit set,
352          * and if so we don't reinitialize sp, since we must be coming
353          * from Linux.  (In fact the precise case is !(val & ~1),
354          * but any Linux PC has to have the high bit set.)
355          *
356          * Note that the hypervisor *always* sets SYSTEM_SAVE_K_2 for
357          * any path that turns into a downcall to one of our TLB handlers.
358          *
359          * FIXME: if we end up never using this path, perhaps we should
360          * prevent the hypervisor from generating downcalls in this case.
361          * The advantage of getting a downcall is we can panic in Linux.
362          */
363         mfspr   r0, SPR_SYSTEM_SAVE_K_2
364         {
365          bltz   r0, 0f    /* high bit in S_S_1_2 is for a PC to use */
366          move   r0, sp
367         }
368         .endif
369
370 2:
371         /*
372          * SYSTEM_SAVE_K_0 holds the cpu number in the low bits, and
373          * the current stack top in the higher bits.  So we recover
374          * our stack top by just masking off the low bits, then
375          * point sp at the top aligned address on the actual stack page.
376          */
377         mfspr   r0, SPR_SYSTEM_SAVE_K_0
378         mm      r0, zero, LOG2_THREAD_SIZE, 63
379
380 0:
381         /*
382          * Align the stack mod 64 so we can properly predict what
383          * cache lines we need to write-hint to reduce memory fetch
384          * latency as we enter the kernel.  The layout of memory is
385          * as follows, with cache line 0 at the lowest VA, and cache
386          * line 8 just below the r0 value this "andi" computes.
387          * Note that we never write to cache line 8, and we skip
388          * cache lines 1-3 for syscalls.
389          *
390          *    cache line 8: ptregs padding (two words)
391          *    cache line 7: sp, lr, pc, ex1, faultnum, orig_r0, flags, cmpexch
392          *    cache line 6: r46...r53 (tp)
393          *    cache line 5: r38...r45
394          *    cache line 4: r30...r37
395          *    cache line 3: r22...r29
396          *    cache line 2: r14...r21
397          *    cache line 1: r6...r13
398          *    cache line 0: 2 x frame, r0..r5
399          */
400         andi    r0, r0, -64
401
402         /*
403          * Push the first four registers on the stack, so that we can set
404          * them to vector-unique values before we jump to the common code.
405          *
406          * Registers are pushed on the stack as a struct pt_regs,
407          * with the sp initially just above the struct, and when we're
408          * done, sp points to the base of the struct, minus
409          * C_ABI_SAVE_AREA_SIZE, so we can directly jal to C code.
410          *
411          * This routine saves just the first four registers, plus the
412          * stack context so we can do proper backtracing right away,
413          * and defers to handle_interrupt to save the rest.
414          * The backtracer needs pc, ex1, lr, sp, r52, and faultnum,
415          * and needs sp set to its final location at the bottom of
416          * the stack frame.
417          */
418         addli   r0, r0, PTREGS_OFFSET_LR - (PTREGS_SIZE + KSTK_PTREGS_GAP)
419         wh64    r0   /* cache line 7 */
420         {
421          st     r0, lr
422          addli  r0, r0, PTREGS_OFFSET_SP - PTREGS_OFFSET_LR
423         }
424         {
425          st     r0, sp
426          addli  sp, r0, PTREGS_OFFSET_REG(52) - PTREGS_OFFSET_SP
427         }
428         wh64    sp   /* cache line 6 */
429         {
430          st     sp, r52
431          addli  sp, sp, PTREGS_OFFSET_REG(1) - PTREGS_OFFSET_REG(52)
432         }
433         wh64    sp   /* cache line 0 */
434         {
435          st     sp, r1
436          addli  sp, sp, PTREGS_OFFSET_REG(2) - PTREGS_OFFSET_REG(1)
437         }
438         {
439          st     sp, r2
440          addli  sp, sp, PTREGS_OFFSET_REG(3) - PTREGS_OFFSET_REG(2)
441         }
442         {
443          st     sp, r3
444          addli  sp, sp, PTREGS_OFFSET_PC - PTREGS_OFFSET_REG(3)
445         }
446         mfspr   r0, SPR_EX_CONTEXT_K_0
447         .ifc \processing,handle_syscall
448         /*
449          * Bump the saved PC by one bundle so that when we return, we won't
450          * execute the same swint instruction again.  We need to do this while
451          * we're in the critical section.
452          */
453         addi    r0, r0, 8
454         .endif
455         {
456          st     sp, r0
457          addli  sp, sp, PTREGS_OFFSET_EX1 - PTREGS_OFFSET_PC
458         }
459         mfspr   r0, SPR_EX_CONTEXT_K_1
460         {
461          st     sp, r0
462          addi   sp, sp, PTREGS_OFFSET_FAULTNUM - PTREGS_OFFSET_EX1
463         /*
464          * Use r0 for syscalls so it's a temporary; use r1 for interrupts
465          * so that it gets passed through unchanged to the handler routine.
466          * Note that the .if conditional confusingly spans bundles.
467          */
468          .ifc \processing,handle_syscall
469          movei  r0, \vecnum
470         }
471         {
472          st     sp, r0
473          .else
474          movei  r1, \vecnum
475         }
476         {
477          st     sp, r1
478          .endif
479          addli  sp, sp, PTREGS_OFFSET_REG(0) - PTREGS_OFFSET_FAULTNUM
480         }
481         mfspr   r0, SPR_SYSTEM_SAVE_K_1    /* Original r0 */
482         {
483          st     sp, r0
484          addi   sp, sp, -PTREGS_OFFSET_REG(0) - 8
485         }
486         {
487          st     sp, zero        /* write zero into "Next SP" frame pointer */
488          addi   sp, sp, -8      /* leave SP pointing at bottom of frame */
489         }
490         .ifc \processing,handle_syscall
491         j       handle_syscall
492         .else
493         /* Capture per-interrupt SPR context to registers. */
494         .ifc \c_routine, do_page_fault
495         mfspr   r2, SPR_SYSTEM_SAVE_K_3   /* address of page fault */
496         mfspr   r3, SPR_SYSTEM_SAVE_K_2   /* info about page fault */
497         .else
498         .ifc \vecnum, INT_ILL_TRANS
499         mfspr   r2, ILL_TRANS_REASON
500         .else
501         .ifc \vecnum, INT_DOUBLE_FAULT
502         mfspr   r2, SPR_SYSTEM_SAVE_K_2   /* double fault info from HV */
503         .else
504         .ifc \c_routine, do_trap
505         mfspr   r2, GPV_REASON
506         .else
507         .ifc \c_routine, op_handle_perf_interrupt
508         mfspr   r2, PERF_COUNT_STS
509 #if CHIP_HAS_AUX_PERF_COUNTERS()
510         .else
511         .ifc \c_routine, op_handle_aux_perf_interrupt
512         mfspr   r2, AUX_PERF_COUNT_STS
513         .endif
514 #endif
515         .endif
516         .endif
517         .endif
518         .endif
519         .endif
520         /* Put function pointer in r0 */
521         moveli  r0, hw2_last(\c_routine)
522         shl16insli r0, r0, hw1(\c_routine)
523         {
524          shl16insli r0, r0, hw0(\c_routine)
525          j       \processing
526         }
527         .endif
528         ENDPROC(intvec_\vecname)
529
530 #ifdef __COLLECT_LINKER_FEEDBACK__
531         .pushsection .text.intvec_feedback,"ax"
532         .org    (\vecnum << 5)
533         FEEDBACK_ENTER_EXPLICIT(intvec_\vecname, .intrpt1, 1 << 8)
534         jrp     lr
535         .popsection
536 #endif
537
538         .endm
539
540
541         /*
542          * Save the rest of the registers that we didn't save in the actual
543          * vector itself.  We can't use r0-r10 inclusive here.
544          */
545         .macro  finish_interrupt_save, function
546
547         /* If it's a syscall, save a proper orig_r0, otherwise just zero. */
548         PTREGS_PTR(r52, PTREGS_OFFSET_ORIG_R0)
549         {
550          .ifc \function,handle_syscall
551          st     r52, r0
552          .else
553          st     r52, zero
554          .endif
555          PTREGS_PTR(r52, PTREGS_OFFSET_TP)
556         }
557         st      r52, tp
558         {
559          mfspr  tp, CMPEXCH_VALUE
560          PTREGS_PTR(r52, PTREGS_OFFSET_CMPEXCH)
561         }
562
563         /*
564          * For ordinary syscalls, we save neither caller- nor callee-
565          * save registers, since the syscall invoker doesn't expect the
566          * caller-saves to be saved, and the called kernel functions will
567          * take care of saving the callee-saves for us.
568          *
569          * For interrupts we save just the caller-save registers.  Saving
570          * them is required (since the "caller" can't save them).  Again,
571          * the called kernel functions will restore the callee-save
572          * registers for us appropriately.
573          *
574          * On return, we normally restore nothing special for syscalls,
575          * and just the caller-save registers for interrupts.
576          *
577          * However, there are some important caveats to all this:
578          *
579          * - We always save a few callee-save registers to give us
580          *   some scratchpad registers to carry across function calls.
581          *
582          * - fork/vfork/etc require us to save all the callee-save
583          *   registers, which we do in PTREGS_SYSCALL_ALL_REGS, below.
584          *
585          * - We always save r0..r5 and r10 for syscalls, since we need
586          *   to reload them a bit later for the actual kernel call, and
587          *   since we might need them for -ERESTARTNOINTR, etc.
588          *
589          * - Before invoking a signal handler, we save the unsaved
590          *   callee-save registers so they are visible to the
591          *   signal handler or any ptracer.
592          *
593          * - If the unsaved callee-save registers are modified, we set
594          *   a bit in pt_regs so we know to reload them from pt_regs
595          *   and not just rely on the kernel function unwinding.
596          *   (Done for ptrace register writes and SA_SIGINFO handler.)
597          */
598         {
599          st     r52, tp
600          PTREGS_PTR(r52, PTREGS_OFFSET_REG(33))
601         }
602         wh64    r52    /* cache line 4 */
603         push_reg r33, r52
604         push_reg r32, r52
605         push_reg r31, r52
606         .ifc \function,handle_syscall
607         push_reg r30, r52, PTREGS_OFFSET_SYSCALL - PTREGS_OFFSET_REG(30)
608         push_reg TREG_SYSCALL_NR_NAME, r52, \
609           PTREGS_OFFSET_REG(5) - PTREGS_OFFSET_SYSCALL
610         .else
611
612         push_reg r30, r52, PTREGS_OFFSET_REG(29) - PTREGS_OFFSET_REG(30)
613         wh64    r52   /* cache line 3 */
614         push_reg r29, r52
615         push_reg r28, r52
616         push_reg r27, r52
617         push_reg r26, r52
618         push_reg r25, r52
619         push_reg r24, r52
620         push_reg r23, r52
621         push_reg r22, r52
622         wh64    r52   /* cache line 2 */
623         push_reg r21, r52
624         push_reg r20, r52
625         push_reg r19, r52
626         push_reg r18, r52
627         push_reg r17, r52
628         push_reg r16, r52
629         push_reg r15, r52
630         push_reg r14, r52
631         wh64    r52   /* cache line 1 */
632         push_reg r13, r52
633         push_reg r12, r52
634         push_reg r11, r52
635         push_reg r10, r52
636         push_reg r9, r52
637         push_reg r8, r52
638         push_reg r7, r52
639         push_reg r6, r52
640
641         .endif
642
643         push_reg r5, r52
644         st      r52, r4
645
646         /*
647          * If we will be returning to the kernel, we will need to
648          * reset the interrupt masks to the state they had before.
649          * Set DISABLE_IRQ in flags iff we came from PL1 with irqs disabled.
650          */
651         mfspr   r32, SPR_EX_CONTEXT_K_1
652         {
653          andi   r32, r32, SPR_EX_CONTEXT_1_1__PL_MASK  /* mask off ICS */
654          PTREGS_PTR(r21, PTREGS_OFFSET_FLAGS)
655         }
656         beqzt   r32, 1f       /* zero if from user space */
657         IRQS_DISABLED(r32)    /* zero if irqs enabled */
658 #if PT_FLAGS_DISABLE_IRQ != 1
659 # error Value of IRQS_DISABLED used to set PT_FLAGS_DISABLE_IRQ; fix
660 #endif
661 1:
662         .ifnc \function,handle_syscall
663         /* Record the fact that we saved the caller-save registers above. */
664         ori     r32, r32, PT_FLAGS_CALLER_SAVES
665         .endif
666         st      r21, r32
667
668         /*
669          * we've captured enough state to the stack (including in
670          * particular our EX_CONTEXT state) that we can now release
671          * the interrupt critical section and replace it with our
672          * standard "interrupts disabled" mask value.  This allows
673          * synchronous interrupts (and profile interrupts) to punch
674          * through from this point onwards.
675          *
676          * It's important that no code before this point touch memory
677          * other than our own stack (to keep the invariant that this
678          * is all that gets touched under ICS), and that no code after
679          * this point reference any interrupt-specific SPR, in particular
680          * the EX_CONTEXT_K_ values.
681          */
682         .ifc \function,handle_nmi
683         IRQ_DISABLE_ALL(r20)
684         .else
685         IRQ_DISABLE(r20, r21)
686         .endif
687         mtspr   INTERRUPT_CRITICAL_SECTION, zero
688
689         /* Load tp with our per-cpu offset. */
690 #ifdef CONFIG_SMP
691         {
692          mfspr  r20, SPR_SYSTEM_SAVE_K_0
693          moveli r21, hw2_last(__per_cpu_offset)
694         }
695         {
696          shl16insli r21, r21, hw1(__per_cpu_offset)
697          bfextu r20, r20, 0, LOG2_THREAD_SIZE-1
698         }
699         shl16insli r21, r21, hw0(__per_cpu_offset)
700         shl3add r20, r20, r21
701         ld      tp, r20
702 #else
703         move    tp, zero
704 #endif
705
706 #ifdef __COLLECT_LINKER_FEEDBACK__
707         /*
708          * Notify the feedback routines that we were in the
709          * appropriate fixed interrupt vector area.  Note that we
710          * still have ICS set at this point, so we can't invoke any
711          * atomic operations or we will panic.  The feedback
712          * routines internally preserve r0..r10 and r30 up.
713          */
714         .ifnc \function,handle_syscall
715         shli    r20, r1, 5
716         .else
717         moveli  r20, INT_SWINT_1 << 5
718         .endif
719         moveli  r21, hw2_last(intvec_feedback)
720         shl16insli r21, r21, hw1(intvec_feedback)
721         shl16insli r21, r21, hw0(intvec_feedback)
722         add     r20, r20, r21
723         jalr    r20
724
725         /* And now notify the feedback routines that we are here. */
726         FEEDBACK_ENTER(\function)
727 #endif
728
729         /*
730          * Prepare the first 256 stack bytes to be rapidly accessible
731          * without having to fetch the background data.
732          */
733         addi    r52, sp, -64
734         {
735          wh64   r52
736          addi   r52, r52, -64
737         }
738         {
739          wh64   r52
740          addi   r52, r52, -64
741         }
742         {
743          wh64   r52
744          addi   r52, r52, -64
745         }
746         wh64    r52
747
748 #ifdef CONFIG_TRACE_IRQFLAGS
749         .ifnc \function,handle_nmi
750         /*
751          * We finally have enough state set up to notify the irq
752          * tracing code that irqs were disabled on entry to the handler.
753          * The TRACE_IRQS_OFF call clobbers registers r0-r29.
754          * For syscalls, we already have the register state saved away
755          * on the stack, so we don't bother to do any register saves here,
756          * and later we pop the registers back off the kernel stack.
757          * For interrupt handlers, save r0-r3 in callee-saved registers.
758          */
759         .ifnc \function,handle_syscall
760         { move r30, r0; move r31, r1 }
761         { move r32, r2; move r33, r3 }
762         .endif
763         TRACE_IRQS_OFF
764         .ifnc \function,handle_syscall
765         { move r0, r30; move r1, r31 }
766         { move r2, r32; move r3, r33 }
767         .endif
768         .endif
769 #endif
770
771         .endm
772
773         /*
774          * Redispatch a downcall.
775          */
776         .macro  dc_dispatch vecnum, vecname
777         .org    (\vecnum << 8)
778 intvec_\vecname:
779         j       hv_downcall_dispatch
780         ENDPROC(intvec_\vecname)
781         .endm
782
783         /*
784          * Common code for most interrupts.  The C function we're eventually
785          * going to is in r0, and the faultnum is in r1; the original
786          * values for those registers are on the stack.
787          */
788         .pushsection .text.handle_interrupt,"ax"
789 handle_interrupt:
790         finish_interrupt_save handle_interrupt
791
792         /* Jump to the C routine; it should enable irqs as soon as possible. */
793         {
794          jalr   r0
795          PTREGS_PTR(r0, PTREGS_OFFSET_BASE)
796         }
797         FEEDBACK_REENTER(handle_interrupt)
798         {
799          movei  r30, 0   /* not an NMI */
800          j      interrupt_return
801         }
802         STD_ENDPROC(handle_interrupt)
803
804 /*
805  * This routine takes a boolean in r30 indicating if this is an NMI.
806  * If so, we also expect a boolean in r31 indicating whether to
807  * re-enable the oprofile interrupts.
808  *
809  * Note that .Lresume_userspace is jumped to directly in several
810  * places, and we need to make sure r30 is set correctly in those
811  * callers as well.
812  */
813 STD_ENTRY(interrupt_return)
814         /* If we're resuming to kernel space, don't check thread flags. */
815         {
816          bnez   r30, .Lrestore_all  /* NMIs don't special-case user-space */
817          PTREGS_PTR(r29, PTREGS_OFFSET_EX1)
818         }
819         ld      r29, r29
820         andi    r29, r29, SPR_EX_CONTEXT_1_1__PL_MASK  /* mask off ICS */
821         {
822          beqzt  r29, .Lresume_userspace
823          PTREGS_PTR(r29, PTREGS_OFFSET_PC)
824         }
825
826         /* If we're resuming to _cpu_idle_nap, bump PC forward by 8. */
827         moveli  r27, hw2_last(_cpu_idle_nap)
828         {
829          ld     r28, r29
830          shl16insli r27, r27, hw1(_cpu_idle_nap)
831         }
832         {
833          shl16insli r27, r27, hw0(_cpu_idle_nap)
834         }
835         {
836          cmpeq  r27, r27, r28
837         }
838         {
839          blbc   r27, .Lrestore_all
840          addi   r28, r28, 8
841         }
842         st      r29, r28
843         j       .Lrestore_all
844
845 .Lresume_userspace:
846         FEEDBACK_REENTER(interrupt_return)
847
848         /*
849          * Use r33 to hold whether we have already loaded the callee-saves
850          * into ptregs.  We don't want to do it twice in this loop, since
851          * then we'd clobber whatever changes are made by ptrace, etc.
852          */
853         {
854          movei  r33, 0
855          move   r32, sp
856         }
857
858         /* Get base of stack in r32. */
859         EXTRACT_THREAD_INFO(r32)
860
861 .Lretry_work_pending:
862         /*
863          * Disable interrupts so as to make sure we don't
864          * miss an interrupt that sets any of the thread flags (like
865          * need_resched or sigpending) between sampling and the iret.
866          * Routines like schedule() or do_signal() may re-enable
867          * interrupts before returning.
868          */
869         IRQ_DISABLE(r20, r21)
870         TRACE_IRQS_OFF  /* Note: clobbers registers r0-r29 */
871
872
873         /* Check to see if there is any work to do before returning to user. */
874         {
875          addi   r29, r32, THREAD_INFO_FLAGS_OFFSET
876          moveli r1, hw1_last(_TIF_ALLWORK_MASK)
877         }
878         {
879          ld     r29, r29
880          shl16insli r1, r1, hw0(_TIF_ALLWORK_MASK)
881         }
882         and     r1, r29, r1
883         beqzt   r1, .Lrestore_all
884
885         /*
886          * Make sure we have all the registers saved for signal
887          * handling or notify-resume.  Call out to C code to figure out
888          * exactly what we need to do for each flag bit, then if
889          * necessary, reload the flags and recheck.
890          */
891         {
892          PTREGS_PTR(r0, PTREGS_OFFSET_BASE)
893          bnez   r33, 1f
894         }
895         push_extra_callee_saves r0
896         movei   r33, 1
897 1:      jal     do_work_pending
898         bnez    r0, .Lretry_work_pending
899
900         /*
901          * In the NMI case we
902          * omit the call to single_process_check_nohz, which normally checks
903          * to see if we should start or stop the scheduler tick, because
904          * we can't call arbitrary Linux code from an NMI context.
905          * We always call the homecache TLB deferral code to re-trigger
906          * the deferral mechanism.
907          *
908          * The other chunk of responsibility this code has is to reset the
909          * interrupt masks appropriately to reset irqs and NMIs.  We have
910          * to call TRACE_IRQS_OFF and TRACE_IRQS_ON to support all the
911          * lockdep-type stuff, but we can't set ICS until afterwards, since
912          * ICS can only be used in very tight chunks of code to avoid
913          * tripping over various assertions that it is off.
914          */
915 .Lrestore_all:
916         PTREGS_PTR(r0, PTREGS_OFFSET_EX1)
917         {
918          ld      r0, r0
919          PTREGS_PTR(r32, PTREGS_OFFSET_FLAGS)
920         }
921         {
922          andi   r0, r0, SPR_EX_CONTEXT_1_1__PL_MASK
923          ld     r32, r32
924         }
925         bnez    r0, 1f
926         j       2f
927 #if PT_FLAGS_DISABLE_IRQ != 1
928 # error Assuming PT_FLAGS_DISABLE_IRQ == 1 so we can use blbct below
929 #endif
930 1:      blbct   r32, 2f
931         IRQ_DISABLE(r20,r21)
932         TRACE_IRQS_OFF
933         movei   r0, 1
934         mtspr   INTERRUPT_CRITICAL_SECTION, r0
935         beqzt   r30, .Lrestore_regs
936         j       3f
937 2:      TRACE_IRQS_ON
938         IRQ_ENABLE_LOAD(r20, r21)
939         movei   r0, 1
940         mtspr   INTERRUPT_CRITICAL_SECTION, r0
941         IRQ_ENABLE_APPLY(r20, r21)
942         beqzt   r30, .Lrestore_regs
943 3:
944
945
946         /*
947          * We now commit to returning from this interrupt, since we will be
948          * doing things like setting EX_CONTEXT SPRs and unwinding the stack
949          * frame.  No calls should be made to any other code after this point.
950          * This code should only be entered with ICS set.
951          * r32 must still be set to ptregs.flags.
952          * We launch loads to each cache line separately first, so we can
953          * get some parallelism out of the memory subsystem.
954          * We start zeroing caller-saved registers throughout, since
955          * that will save some cycles if this turns out to be a syscall.
956          */
957 .Lrestore_regs:
958
959         /*
960          * Rotate so we have one high bit and one low bit to test.
961          * - low bit says whether to restore all the callee-saved registers,
962          *   or just r30-r33, and r52 up.
963          * - high bit (i.e. sign bit) says whether to restore all the
964          *   caller-saved registers, or just r0.
965          */
966 #if PT_FLAGS_CALLER_SAVES != 2 || PT_FLAGS_RESTORE_REGS != 4
967 # error Rotate trick does not work :-)
968 #endif
969         {
970          rotli  r20, r32, 62
971          PTREGS_PTR(sp, PTREGS_OFFSET_REG(0))
972         }
973
974         /*
975          * Load cache lines 0, 4, 6 and 7, in that order, then use
976          * the last loaded value, which makes it likely that the other
977          * cache lines have also loaded, at which point we should be
978          * able to safely read all the remaining words on those cache
979          * lines without waiting for the memory subsystem.
980          */
981         pop_reg r0, sp, PTREGS_OFFSET_REG(30) - PTREGS_OFFSET_REG(0)
982         pop_reg r30, sp, PTREGS_OFFSET_REG(52) - PTREGS_OFFSET_REG(30)
983         pop_reg_zero r52, r3, sp, PTREGS_OFFSET_CMPEXCH - PTREGS_OFFSET_REG(52)
984         pop_reg_zero r21, r27, sp, PTREGS_OFFSET_EX1 - PTREGS_OFFSET_CMPEXCH
985         pop_reg_zero lr, r2, sp, PTREGS_OFFSET_PC - PTREGS_OFFSET_EX1
986         {
987          mtspr  CMPEXCH_VALUE, r21
988          move   r4, zero
989         }
990         pop_reg r21, sp, PTREGS_OFFSET_REG(31) - PTREGS_OFFSET_PC
991         {
992          mtspr  SPR_EX_CONTEXT_K_1, lr
993          andi   lr, lr, SPR_EX_CONTEXT_1_1__PL_MASK  /* mask off ICS */
994         }
995         {
996          mtspr  SPR_EX_CONTEXT_K_0, r21
997          move   r5, zero
998         }
999
1000         /* Restore callee-saveds that we actually use. */
1001         pop_reg_zero r31, r6
1002         pop_reg_zero r32, r7
1003         pop_reg_zero r33, r8, sp, PTREGS_OFFSET_REG(29) - PTREGS_OFFSET_REG(33)
1004
1005         /*
1006          * If we modified other callee-saveds, restore them now.
1007          * This is rare, but could be via ptrace or signal handler.
1008          */
1009         {
1010          move   r9, zero
1011          blbs   r20, .Lrestore_callees
1012         }
1013 .Lcontinue_restore_regs:
1014
1015         /* Check if we're returning from a syscall. */
1016         {
1017          move   r10, zero
1018          bltzt  r20, 1f  /* no, so go restore callee-save registers */
1019         }
1020
1021         /*
1022          * Check if we're returning to userspace.
1023          * Note that if we're not, we don't worry about zeroing everything.
1024          */
1025         {
1026          addli  sp, sp, PTREGS_OFFSET_LR - PTREGS_OFFSET_REG(29)
1027          bnez   lr, .Lkernel_return
1028         }
1029
1030         /*
1031          * On return from syscall, we've restored r0 from pt_regs, but we
1032          * clear the remainder of the caller-saved registers.  We could
1033          * restore the syscall arguments, but there's not much point,
1034          * and it ensures user programs aren't trying to use the
1035          * caller-saves if we clear them, as well as avoiding leaking
1036          * kernel pointers into userspace.
1037          */
1038         pop_reg_zero lr, r11, sp, PTREGS_OFFSET_TP - PTREGS_OFFSET_LR
1039         pop_reg_zero tp, r12, sp, PTREGS_OFFSET_SP - PTREGS_OFFSET_TP
1040         {
1041          ld     sp, sp
1042          move   r13, zero
1043          move   r14, zero
1044         }
1045         { move r15, zero; move r16, zero }
1046         { move r17, zero; move r18, zero }
1047         { move r19, zero; move r20, zero }
1048         { move r21, zero; move r22, zero }
1049         { move r23, zero; move r24, zero }
1050         { move r25, zero; move r26, zero }
1051
1052         /* Set r1 to errno if we are returning an error, otherwise zero. */
1053         {
1054          moveli r29, 4096
1055          sub    r1, zero, r0
1056         }
1057         {
1058          move   r28, zero
1059          cmpltu r29, r1, r29
1060         }
1061         {
1062          mnz    r1, r29, r1
1063          move   r29, zero
1064         }
1065         iret
1066
1067         /*
1068          * Not a syscall, so restore caller-saved registers.
1069          * First kick off loads for cache lines 1-3, which we're touching
1070          * for the first time here.
1071          */
1072         .align 64
1073 1:      pop_reg r29, sp, PTREGS_OFFSET_REG(21) - PTREGS_OFFSET_REG(29)
1074         pop_reg r21, sp, PTREGS_OFFSET_REG(13) - PTREGS_OFFSET_REG(21)
1075         pop_reg r13, sp, PTREGS_OFFSET_REG(1) - PTREGS_OFFSET_REG(13)
1076         pop_reg r1
1077         pop_reg r2
1078         pop_reg r3
1079         pop_reg r4
1080         pop_reg r5
1081         pop_reg r6
1082         pop_reg r7
1083         pop_reg r8
1084         pop_reg r9
1085         pop_reg r10
1086         pop_reg r11
1087         pop_reg r12, sp, 16
1088         /* r13 already restored above */
1089         pop_reg r14
1090         pop_reg r15
1091         pop_reg r16
1092         pop_reg r17
1093         pop_reg r18
1094         pop_reg r19
1095         pop_reg r20, sp, 16
1096         /* r21 already restored above */
1097         pop_reg r22
1098         pop_reg r23
1099         pop_reg r24
1100         pop_reg r25
1101         pop_reg r26
1102         pop_reg r27
1103         pop_reg r28, sp, PTREGS_OFFSET_LR - PTREGS_OFFSET_REG(28)
1104         /* r29 already restored above */
1105         bnez    lr, .Lkernel_return
1106         pop_reg lr, sp, PTREGS_OFFSET_TP - PTREGS_OFFSET_LR
1107         pop_reg tp, sp, PTREGS_OFFSET_SP - PTREGS_OFFSET_TP
1108         ld      sp, sp
1109         iret
1110
1111         /*
1112          * We can't restore tp when in kernel mode, since a thread might
1113          * have migrated from another cpu and brought a stale tp value.
1114          */
1115 .Lkernel_return:
1116         pop_reg lr, sp, PTREGS_OFFSET_SP - PTREGS_OFFSET_LR
1117         ld      sp, sp
1118         iret
1119
1120         /* Restore callee-saved registers from r34 to r51. */
1121 .Lrestore_callees:
1122         addli  sp, sp, PTREGS_OFFSET_REG(34) - PTREGS_OFFSET_REG(29)
1123         pop_reg r34
1124         pop_reg r35
1125         pop_reg r36
1126         pop_reg r37
1127         pop_reg r38
1128         pop_reg r39
1129         pop_reg r40
1130         pop_reg r41
1131         pop_reg r42
1132         pop_reg r43
1133         pop_reg r44
1134         pop_reg r45
1135         pop_reg r46
1136         pop_reg r47
1137         pop_reg r48
1138         pop_reg r49
1139         pop_reg r50
1140         pop_reg r51, sp, PTREGS_OFFSET_REG(29) - PTREGS_OFFSET_REG(51)
1141         j .Lcontinue_restore_regs
1142         STD_ENDPROC(interrupt_return)
1143
1144         /*
1145          * "NMI" interrupts mask ALL interrupts before calling the
1146          * handler, and don't check thread flags, etc., on the way
1147          * back out.  In general, the only things we do here for NMIs
1148          * are register save/restore and dataplane kernel-TLB management.
1149          * We don't (for example) deal with start/stop of the sched tick.
1150          */
1151         .pushsection .text.handle_nmi,"ax"
1152 handle_nmi:
1153         finish_interrupt_save handle_nmi
1154         {
1155          jalr   r0
1156          PTREGS_PTR(r0, PTREGS_OFFSET_BASE)
1157         }
1158         FEEDBACK_REENTER(handle_nmi)
1159         {
1160          movei  r30, 1
1161          move   r31, r0
1162         }
1163         j       interrupt_return
1164         STD_ENDPROC(handle_nmi)
1165
1166         /*
1167          * Parallel code for syscalls to handle_interrupt.
1168          */
1169         .pushsection .text.handle_syscall,"ax"
1170 handle_syscall:
1171         finish_interrupt_save handle_syscall
1172
1173         /* Enable irqs. */
1174         TRACE_IRQS_ON
1175         IRQ_ENABLE(r20, r21)
1176
1177         /* Bump the counter for syscalls made on this tile. */
1178         moveli r20, hw2_last(irq_stat + IRQ_CPUSTAT_SYSCALL_COUNT_OFFSET)
1179         shl16insli r20, r20, hw1(irq_stat + IRQ_CPUSTAT_SYSCALL_COUNT_OFFSET)
1180         shl16insli r20, r20, hw0(irq_stat + IRQ_CPUSTAT_SYSCALL_COUNT_OFFSET)
1181         add     r20, r20, tp
1182         ld4s    r21, r20
1183         {
1184          addi   r21, r21, 1
1185          move   r31, sp
1186         }
1187         {
1188          st4    r20, r21
1189          EXTRACT_THREAD_INFO(r31)
1190         }
1191
1192         /* Trace syscalls, if requested. */
1193         addi    r31, r31, THREAD_INFO_FLAGS_OFFSET
1194         {
1195          ld     r30, r31
1196          moveli r32, _TIF_SYSCALL_ENTRY_WORK
1197         }
1198         and     r30, r30, r32
1199         {
1200          addi   r30, r31, THREAD_INFO_STATUS_OFFSET - THREAD_INFO_FLAGS_OFFSET
1201          beqzt  r30, .Lrestore_syscall_regs
1202         }
1203         {
1204          PTREGS_PTR(r0, PTREGS_OFFSET_BASE)
1205          jal    do_syscall_trace_enter
1206         }
1207         FEEDBACK_REENTER(handle_syscall)
1208
1209         /*
1210          * We always reload our registers from the stack at this
1211          * point.  They might be valid, if we didn't build with
1212          * TRACE_IRQFLAGS, and this isn't a dataplane tile, and we're not
1213          * doing syscall tracing, but there are enough cases now that it
1214          * seems simplest just to do the reload unconditionally.
1215          */
1216 .Lrestore_syscall_regs:
1217         {
1218          ld     r30, r30
1219          PTREGS_PTR(r11, PTREGS_OFFSET_REG(0))
1220         }
1221         pop_reg r0,  r11
1222         pop_reg r1,  r11
1223         pop_reg r2,  r11
1224         pop_reg r3,  r11
1225         pop_reg r4,  r11
1226         pop_reg r5,  r11, PTREGS_OFFSET_SYSCALL - PTREGS_OFFSET_REG(5)
1227         {
1228          ld     TREG_SYSCALL_NR_NAME, r11
1229          moveli r21, __NR_syscalls
1230         }
1231
1232         /* Ensure that the syscall number is within the legal range. */
1233         {
1234          moveli r20, hw2(sys_call_table)
1235 #ifdef CONFIG_COMPAT
1236          blbs   r30, .Lcompat_syscall
1237 #endif
1238         }
1239         {
1240          cmpltu r21, TREG_SYSCALL_NR_NAME, r21
1241          shl16insli r20, r20, hw1(sys_call_table)
1242         }
1243         {
1244          blbc   r21, .Linvalid_syscall
1245          shl16insli r20, r20, hw0(sys_call_table)
1246         }
1247 .Lload_syscall_pointer:
1248         shl3add r20, TREG_SYSCALL_NR_NAME, r20
1249         ld      r20, r20
1250
1251         /* Jump to syscall handler. */
1252         jalr    r20
1253 .Lhandle_syscall_link: /* value of "lr" after "jalr r20" above */
1254
1255         /*
1256          * Write our r0 onto the stack so it gets restored instead
1257          * of whatever the user had there before.
1258          * In compat mode, sign-extend r0 before storing it.
1259          */
1260         {
1261          PTREGS_PTR(r29, PTREGS_OFFSET_REG(0))
1262          blbct  r30, 1f
1263         }
1264         addxi   r0, r0, 0
1265 1:      st      r29, r0
1266
1267 .Lsyscall_sigreturn_skip:
1268         FEEDBACK_REENTER(handle_syscall)
1269
1270         /* Do syscall trace again, if requested. */
1271         {
1272          ld      r30, r31
1273          moveli  r32, _TIF_SYSCALL_EXIT_WORK
1274         }
1275         and      r0, r30, r32
1276         {
1277          andi    r0, r30, _TIF_SINGLESTEP
1278          beqzt   r0, 1f
1279         }
1280         {
1281          PTREGS_PTR(r0, PTREGS_OFFSET_BASE)
1282          jal    do_syscall_trace_exit
1283         }
1284         FEEDBACK_REENTER(handle_syscall)
1285         andi    r0, r30, _TIF_SINGLESTEP
1286
1287 1:      beqzt   r0, 2f
1288
1289         /* Single stepping -- notify ptrace. */
1290         {
1291          movei   r0, SIGTRAP
1292          jal     ptrace_notify
1293         }
1294         FEEDBACK_REENTER(handle_syscall)
1295
1296 2:      {
1297          movei  r30, 0               /* not an NMI */
1298          j      .Lresume_userspace   /* jump into middle of interrupt_return */
1299         }
1300
1301 #ifdef CONFIG_COMPAT
1302 .Lcompat_syscall:
1303         /*
1304          * Load the base of the compat syscall table in r20, and
1305          * range-check the syscall number (duplicated from 64-bit path).
1306          * Sign-extend all the user's passed arguments to make them consistent.
1307          * Also save the original "r(n)" values away in "r(11+n)" in
1308          * case the syscall table entry wants to validate them.
1309          */
1310         moveli  r20, hw2(compat_sys_call_table)
1311         {
1312          cmpltu r21, TREG_SYSCALL_NR_NAME, r21
1313          shl16insli r20, r20, hw1(compat_sys_call_table)
1314         }
1315         {
1316          blbc   r21, .Linvalid_syscall
1317          shl16insli r20, r20, hw0(compat_sys_call_table)
1318         }
1319         { move r11, r0; addxi r0, r0, 0 }
1320         { move r12, r1; addxi r1, r1, 0 }
1321         { move r13, r2; addxi r2, r2, 0 }
1322         { move r14, r3; addxi r3, r3, 0 }
1323         { move r15, r4; addxi r4, r4, 0 }
1324         { move r16, r5; addxi r5, r5, 0 }
1325         j .Lload_syscall_pointer
1326 #endif
1327
1328 .Linvalid_syscall:
1329         /* Report an invalid syscall back to the user program */
1330         {
1331          PTREGS_PTR(r29, PTREGS_OFFSET_REG(0))
1332          movei  r28, -ENOSYS
1333         }
1334         st      r29, r28
1335         {
1336          movei  r30, 0               /* not an NMI */
1337          j      .Lresume_userspace   /* jump into middle of interrupt_return */
1338         }
1339         STD_ENDPROC(handle_syscall)
1340
1341         /* Return the address for oprofile to suppress in backtraces. */
1342 STD_ENTRY_SECTION(handle_syscall_link_address, .text.handle_syscall)
1343         lnk     r0
1344         {
1345          addli  r0, r0, .Lhandle_syscall_link - .
1346          jrp    lr
1347         }
1348         STD_ENDPROC(handle_syscall_link_address)
1349
1350 STD_ENTRY(ret_from_fork)
1351         jal     sim_notify_fork
1352         jal     schedule_tail
1353         FEEDBACK_REENTER(ret_from_fork)
1354         {
1355          movei  r30, 0               /* not an NMI */
1356          j      .Lresume_userspace   /* jump into middle of interrupt_return */
1357         }
1358         STD_ENDPROC(ret_from_fork)
1359
1360 STD_ENTRY(ret_from_kernel_thread)
1361         jal     sim_notify_fork
1362         jal     schedule_tail
1363         FEEDBACK_REENTER(ret_from_fork)
1364         {
1365          move   r0, r31
1366          jalr   r30
1367         }
1368         FEEDBACK_REENTER(ret_from_kernel_thread)
1369         {
1370          movei  r30, 0               /* not an NMI */
1371          j      .Lresume_userspace   /* jump into middle of interrupt_return */
1372         }
1373         STD_ENDPROC(ret_from_kernel_thread)
1374
1375 /* Various stub interrupt handlers and syscall handlers */
1376
1377 STD_ENTRY_LOCAL(_kernel_double_fault)
1378         mfspr   r1, SPR_EX_CONTEXT_K_0
1379         move    r2, lr
1380         move    r3, sp
1381         move    r4, r52
1382         addi    sp, sp, -C_ABI_SAVE_AREA_SIZE
1383         j       kernel_double_fault
1384         STD_ENDPROC(_kernel_double_fault)
1385
1386 STD_ENTRY_LOCAL(bad_intr)
1387         mfspr   r2, SPR_EX_CONTEXT_K_0
1388         panic   "Unhandled interrupt %#x: PC %#lx"
1389         STD_ENDPROC(bad_intr)
1390
1391 /*
1392  * Special-case sigreturn to not write r0 to the stack on return.
1393  * This is technically more efficient, but it also avoids difficulties
1394  * in the 64-bit OS when handling 32-bit compat code, since we must not
1395  * sign-extend r0 for the sigreturn return-value case.
1396  */
1397 #define PTREGS_SYSCALL_SIGRETURN(x, reg)                \
1398         STD_ENTRY(_##x);                                \
1399         addli   lr, lr, .Lsyscall_sigreturn_skip - .Lhandle_syscall_link; \
1400         {                                               \
1401          PTREGS_PTR(reg, PTREGS_OFFSET_BASE);           \
1402          j      x                                       \
1403         };                                              \
1404         STD_ENDPROC(_##x)
1405
1406 PTREGS_SYSCALL_SIGRETURN(sys_rt_sigreturn, r0)
1407 #ifdef CONFIG_COMPAT
1408 PTREGS_SYSCALL_SIGRETURN(compat_sys_rt_sigreturn, r0)
1409 #endif
1410
1411 /* Save additional callee-saves to pt_regs and jump to standard function. */
1412 STD_ENTRY(_sys_clone)
1413         push_extra_callee_saves r4
1414         j       sys_clone
1415         STD_ENDPROC(_sys_clone)
1416
1417         /*
1418          * Recover r3, r2, r1 and r0 here saved by unalign fast vector.
1419          * The vector area limit is 32 bundles, so we handle the reload here.
1420          * r0, r1, r2 are in thread_info from low to high memory in order.
1421          * r3 points to location the original r3 was saved.
1422          * We put this code in the __HEAD section so it can be reached
1423          * via a conditional branch from the fast path.
1424          */
1425         __HEAD
1426 hand_unalign_slow:
1427         andi    sp, sp, ~1
1428 hand_unalign_slow_badsp:
1429         addi    r3, r3, -(3 * 8)
1430         ld_add  r0, r3, 8
1431         ld_add  r1, r3, 8
1432         ld      r2, r3
1433 hand_unalign_slow_nonuser:
1434         mfspr   r3, SPR_SYSTEM_SAVE_K_1
1435         __int_hand     INT_UNALIGN_DATA, UNALIGN_DATA_SLOW, int_unalign
1436
1437 /* The unaligned data support needs to read all the registers. */
1438 int_unalign:
1439         push_extra_callee_saves r0
1440         j       do_unaligned
1441 ENDPROC(hand_unalign_slow)
1442
1443 /* Fill the return address stack with nonzero entries. */
1444 STD_ENTRY(fill_ra_stack)
1445         {
1446          move   r0, lr
1447          jal    1f
1448         }
1449 1:      jal     2f
1450 2:      jal     3f
1451 3:      jal     4f
1452 4:      jrp     r0
1453         STD_ENDPROC(fill_ra_stack)
1454
1455         .macro int_hand  vecnum, vecname, c_routine, processing=handle_interrupt
1456         .org   (\vecnum << 8)
1457                 __int_hand   \vecnum, \vecname, \c_routine, \processing
1458         .endm
1459
1460 /* Include .intrpt1 array of interrupt vectors */
1461         .section ".intrpt1", "ax"
1462
1463 #define op_handle_perf_interrupt bad_intr
1464 #define op_handle_aux_perf_interrupt bad_intr
1465
1466 #ifndef CONFIG_HARDWALL
1467 #define do_hardwall_trap bad_intr
1468 #endif
1469
1470         int_hand     INT_MEM_ERROR, MEM_ERROR, do_trap
1471         int_hand     INT_SINGLE_STEP_3, SINGLE_STEP_3, bad_intr
1472 #if CONFIG_KERNEL_PL == 2
1473         int_hand     INT_SINGLE_STEP_2, SINGLE_STEP_2, gx_singlestep_handle
1474         int_hand     INT_SINGLE_STEP_1, SINGLE_STEP_1, bad_intr
1475 #else
1476         int_hand     INT_SINGLE_STEP_2, SINGLE_STEP_2, bad_intr
1477         int_hand     INT_SINGLE_STEP_1, SINGLE_STEP_1, gx_singlestep_handle
1478 #endif
1479         int_hand     INT_SINGLE_STEP_0, SINGLE_STEP_0, bad_intr
1480         int_hand     INT_IDN_COMPLETE, IDN_COMPLETE, bad_intr
1481         int_hand     INT_UDN_COMPLETE, UDN_COMPLETE, bad_intr
1482         int_hand     INT_ITLB_MISS, ITLB_MISS, do_page_fault
1483         int_hand     INT_ILL, ILL, do_trap
1484         int_hand     INT_GPV, GPV, do_trap
1485         int_hand     INT_IDN_ACCESS, IDN_ACCESS, do_trap
1486         int_hand     INT_UDN_ACCESS, UDN_ACCESS, do_trap
1487         int_hand     INT_SWINT_3, SWINT_3, do_trap
1488         int_hand     INT_SWINT_2, SWINT_2, do_trap
1489         int_hand     INT_SWINT_1, SWINT_1, SYSCALL, handle_syscall
1490         int_hand     INT_SWINT_0, SWINT_0, do_trap
1491         int_hand     INT_ILL_TRANS, ILL_TRANS, do_trap
1492         int_hand_unalign_fast INT_UNALIGN_DATA, UNALIGN_DATA
1493         int_hand     INT_DTLB_MISS, DTLB_MISS, do_page_fault
1494         int_hand     INT_DTLB_ACCESS, DTLB_ACCESS, do_page_fault
1495         int_hand     INT_IDN_FIREWALL, IDN_FIREWALL, do_hardwall_trap
1496         int_hand     INT_UDN_FIREWALL, UDN_FIREWALL, do_hardwall_trap
1497         int_hand     INT_TILE_TIMER, TILE_TIMER, do_timer_interrupt
1498         int_hand     INT_IDN_TIMER, IDN_TIMER, bad_intr
1499         int_hand     INT_UDN_TIMER, UDN_TIMER, bad_intr
1500         int_hand     INT_IDN_AVAIL, IDN_AVAIL, bad_intr
1501         int_hand     INT_UDN_AVAIL, UDN_AVAIL, bad_intr
1502         int_hand     INT_IPI_3, IPI_3, bad_intr
1503 #if CONFIG_KERNEL_PL == 2
1504         int_hand     INT_IPI_2, IPI_2, tile_dev_intr
1505         int_hand     INT_IPI_1, IPI_1, bad_intr
1506 #else
1507         int_hand     INT_IPI_2, IPI_2, bad_intr
1508         int_hand     INT_IPI_1, IPI_1, tile_dev_intr
1509 #endif
1510         int_hand     INT_IPI_0, IPI_0, bad_intr
1511         int_hand     INT_PERF_COUNT, PERF_COUNT, \
1512                      op_handle_perf_interrupt, handle_nmi
1513         int_hand     INT_AUX_PERF_COUNT, AUX_PERF_COUNT, \
1514                      op_handle_perf_interrupt, handle_nmi
1515         int_hand     INT_INTCTRL_3, INTCTRL_3, bad_intr
1516 #if CONFIG_KERNEL_PL == 2
1517         dc_dispatch  INT_INTCTRL_2, INTCTRL_2
1518         int_hand     INT_INTCTRL_1, INTCTRL_1, bad_intr
1519 #else
1520         int_hand     INT_INTCTRL_2, INTCTRL_2, bad_intr
1521         dc_dispatch  INT_INTCTRL_1, INTCTRL_1
1522 #endif
1523         int_hand     INT_INTCTRL_0, INTCTRL_0, bad_intr
1524         int_hand     INT_MESSAGE_RCV_DWNCL, MESSAGE_RCV_DWNCL, \
1525                      hv_message_intr
1526         int_hand     INT_DEV_INTR_DWNCL, DEV_INTR_DWNCL, bad_intr
1527         int_hand     INT_I_ASID, I_ASID, bad_intr
1528         int_hand     INT_D_ASID, D_ASID, bad_intr
1529         int_hand     INT_DOUBLE_FAULT, DOUBLE_FAULT, do_trap
1530
1531         /* Synthetic interrupt delivered only by the simulator */
1532         int_hand     INT_BREAKPOINT, BREAKPOINT, do_breakpoint