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