Merge branch 'etnaviv/next' of https://git.pengutronix.de/git/lst/linux into drm...
[sfrench/cifs-2.6.git] / arch / powerpc / kernel / entry_64.S
1 /*
2  *  PowerPC version 
3  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
4  *  Rewritten by Cort Dougan (cort@cs.nmt.edu) for PReP
5  *    Copyright (C) 1996 Cort Dougan <cort@cs.nmt.edu>
6  *  Adapted for Power Macintosh by Paul Mackerras.
7  *  Low-level exception handlers and MMU support
8  *  rewritten by Paul Mackerras.
9  *    Copyright (C) 1996 Paul Mackerras.
10  *  MPC8xx modifications Copyright (C) 1997 Dan Malek (dmalek@jlc.net).
11  *
12  *  This file contains the system call entry code, context switch
13  *  code, and exception/interrupt return code for PowerPC.
14  *
15  *  This program is free software; you can redistribute it and/or
16  *  modify it under the terms of the GNU General Public License
17  *  as published by the Free Software Foundation; either version
18  *  2 of the License, or (at your option) any later version.
19  */
20
21 #include <linux/errno.h>
22 #include <linux/err.h>
23 #include <asm/unistd.h>
24 #include <asm/processor.h>
25 #include <asm/page.h>
26 #include <asm/mmu.h>
27 #include <asm/thread_info.h>
28 #include <asm/ppc_asm.h>
29 #include <asm/asm-offsets.h>
30 #include <asm/cputable.h>
31 #include <asm/firmware.h>
32 #include <asm/bug.h>
33 #include <asm/ptrace.h>
34 #include <asm/irqflags.h>
35 #include <asm/hw_irq.h>
36 #include <asm/context_tracking.h>
37 #include <asm/tm.h>
38 #include <asm/ppc-opcode.h>
39 #include <asm/export.h>
40
41 /*
42  * System calls.
43  */
44         .section        ".toc","aw"
45 SYS_CALL_TABLE:
46         .tc sys_call_table[TC],sys_call_table
47
48 /* This value is used to mark exception frames on the stack. */
49 exception_marker:
50         .tc     ID_EXC_MARKER[TC],STACK_FRAME_REGS_MARKER
51
52         .section        ".text"
53         .align 7
54
55         .globl system_call_common
56 system_call_common:
57 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
58 BEGIN_FTR_SECTION
59         extrdi. r10, r12, 1, (63-MSR_TS_T_LG) /* transaction active? */
60         bne     tabort_syscall
61 END_FTR_SECTION_IFSET(CPU_FTR_TM)
62 #endif
63         andi.   r10,r12,MSR_PR
64         mr      r10,r1
65         addi    r1,r1,-INT_FRAME_SIZE
66         beq-    1f
67         ld      r1,PACAKSAVE(r13)
68 1:      std     r10,0(r1)
69         std     r11,_NIP(r1)
70         std     r12,_MSR(r1)
71         std     r0,GPR0(r1)
72         std     r10,GPR1(r1)
73         beq     2f                      /* if from kernel mode */
74         ACCOUNT_CPU_USER_ENTRY(r13, r10, r11)
75 2:      std     r2,GPR2(r1)
76         std     r3,GPR3(r1)
77         mfcr    r2
78         std     r4,GPR4(r1)
79         std     r5,GPR5(r1)
80         std     r6,GPR6(r1)
81         std     r7,GPR7(r1)
82         std     r8,GPR8(r1)
83         li      r11,0
84         std     r11,GPR9(r1)
85         std     r11,GPR10(r1)
86         std     r11,GPR11(r1)
87         std     r11,GPR12(r1)
88         std     r11,_XER(r1)
89         std     r11,_CTR(r1)
90         std     r9,GPR13(r1)
91         mflr    r10
92         /*
93          * This clears CR0.SO (bit 28), which is the error indication on
94          * return from this system call.
95          */
96         rldimi  r2,r11,28,(63-28)
97         li      r11,0xc01
98         std     r10,_LINK(r1)
99         std     r11,_TRAP(r1)
100         std     r3,ORIG_GPR3(r1)
101         std     r2,_CCR(r1)
102         ld      r2,PACATOC(r13)
103         addi    r9,r1,STACK_FRAME_OVERHEAD
104         ld      r11,exception_marker@toc(r2)
105         std     r11,-16(r9)             /* "regshere" marker */
106 #if defined(CONFIG_VIRT_CPU_ACCOUNTING_NATIVE) && defined(CONFIG_PPC_SPLPAR)
107 BEGIN_FW_FTR_SECTION
108         beq     33f
109         /* if from user, see if there are any DTL entries to process */
110         ld      r10,PACALPPACAPTR(r13)  /* get ptr to VPA */
111         ld      r11,PACA_DTL_RIDX(r13)  /* get log read index */
112         addi    r10,r10,LPPACA_DTLIDX
113         LDX_BE  r10,0,r10               /* get log write index */
114         cmpd    cr1,r11,r10
115         beq+    cr1,33f
116         bl      accumulate_stolen_time
117         REST_GPR(0,r1)
118         REST_4GPRS(3,r1)
119         REST_2GPRS(7,r1)
120         addi    r9,r1,STACK_FRAME_OVERHEAD
121 33:
122 END_FW_FTR_SECTION_IFSET(FW_FEATURE_SPLPAR)
123 #endif /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE && CONFIG_PPC_SPLPAR */
124
125         /*
126          * A syscall should always be called with interrupts enabled
127          * so we just unconditionally hard-enable here. When some kind
128          * of irq tracing is used, we additionally check that condition
129          * is correct
130          */
131 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_BUG)
132         lbz     r10,PACASOFTIRQEN(r13)
133         xori    r10,r10,1
134 1:      tdnei   r10,0
135         EMIT_BUG_ENTRY 1b,__FILE__,__LINE__,BUGFLAG_WARNING
136 #endif
137
138 #ifdef CONFIG_PPC_BOOK3E
139         wrteei  1
140 #else
141         li      r11,MSR_RI
142         ori     r11,r11,MSR_EE
143         mtmsrd  r11,1
144 #endif /* CONFIG_PPC_BOOK3E */
145
146         /* We do need to set SOFTE in the stack frame or the return
147          * from interrupt will be painful
148          */
149         li      r10,1
150         std     r10,SOFTE(r1)
151
152         CURRENT_THREAD_INFO(r11, r1)
153         ld      r10,TI_FLAGS(r11)
154         andi.   r11,r10,_TIF_SYSCALL_DOTRACE
155         bne     syscall_dotrace         /* does not return */
156         cmpldi  0,r0,NR_syscalls
157         bge-    syscall_enosys
158
159 system_call:                    /* label this so stack traces look sane */
160 /*
161  * Need to vector to 32 Bit or default sys_call_table here,
162  * based on caller's run-mode / personality.
163  */
164         ld      r11,SYS_CALL_TABLE@toc(2)
165         andi.   r10,r10,_TIF_32BIT
166         beq     15f
167         addi    r11,r11,8       /* use 32-bit syscall entries */
168         clrldi  r3,r3,32
169         clrldi  r4,r4,32
170         clrldi  r5,r5,32
171         clrldi  r6,r6,32
172         clrldi  r7,r7,32
173         clrldi  r8,r8,32
174 15:
175         slwi    r0,r0,4
176         ldx     r12,r11,r0      /* Fetch system call handler [ptr] */
177         mtctr   r12
178         bctrl                   /* Call handler */
179
180 .Lsyscall_exit:
181         std     r3,RESULT(r1)
182         CURRENT_THREAD_INFO(r12, r1)
183
184         ld      r8,_MSR(r1)
185 #ifdef CONFIG_PPC_BOOK3S
186         /* No MSR:RI on BookE */
187         andi.   r10,r8,MSR_RI
188         beq-    unrecov_restore
189 #endif
190         /*
191          * Disable interrupts so current_thread_info()->flags can't change,
192          * and so that we don't get interrupted after loading SRR0/1.
193          */
194 #ifdef CONFIG_PPC_BOOK3E
195         wrteei  0
196 #else
197         /*
198          * For performance reasons we clear RI the same time that we
199          * clear EE. We only need to clear RI just before we restore r13
200          * below, but batching it with EE saves us one expensive mtmsrd call.
201          * We have to be careful to restore RI if we branch anywhere from
202          * here (eg syscall_exit_work).
203          */
204         li      r11,0
205         mtmsrd  r11,1
206 #endif /* CONFIG_PPC_BOOK3E */
207
208         ld      r9,TI_FLAGS(r12)
209         li      r11,-MAX_ERRNO
210         andi.   r0,r9,(_TIF_SYSCALL_DOTRACE|_TIF_SINGLESTEP|_TIF_USER_WORK_MASK|_TIF_PERSYSCALL_MASK)
211         bne-    syscall_exit_work
212
213         andi.   r0,r8,MSR_FP
214         beq 2f
215 #ifdef CONFIG_ALTIVEC
216         andis.  r0,r8,MSR_VEC@h
217         bne     3f
218 #endif
219 2:      addi    r3,r1,STACK_FRAME_OVERHEAD
220 #ifdef CONFIG_PPC_BOOK3S
221         li      r10,MSR_RI
222         mtmsrd  r10,1           /* Restore RI */
223 #endif
224         bl      restore_math
225 #ifdef CONFIG_PPC_BOOK3S
226         li      r11,0
227         mtmsrd  r11,1
228 #endif
229         ld      r8,_MSR(r1)
230         ld      r3,RESULT(r1)
231         li      r11,-MAX_ERRNO
232
233 3:      cmpld   r3,r11
234         ld      r5,_CCR(r1)
235         bge-    syscall_error
236 .Lsyscall_error_cont:
237         ld      r7,_NIP(r1)
238 BEGIN_FTR_SECTION
239         stdcx.  r0,0,r1                 /* to clear the reservation */
240 END_FTR_SECTION_IFCLR(CPU_FTR_STCX_CHECKS_ADDRESS)
241         andi.   r6,r8,MSR_PR
242         ld      r4,_LINK(r1)
243
244         beq-    1f
245         ACCOUNT_CPU_USER_EXIT(r13, r11, r12)
246
247 BEGIN_FTR_SECTION
248         HMT_MEDIUM_LOW
249 END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
250
251         ld      r13,GPR13(r1)   /* only restore r13 if returning to usermode */
252 1:      ld      r2,GPR2(r1)
253         ld      r1,GPR1(r1)
254         mtlr    r4
255         mtcr    r5
256         mtspr   SPRN_SRR0,r7
257         mtspr   SPRN_SRR1,r8
258         RFI
259         b       .       /* prevent speculative execution */
260
261 syscall_error:  
262         oris    r5,r5,0x1000    /* Set SO bit in CR */
263         neg     r3,r3
264         std     r5,_CCR(r1)
265         b       .Lsyscall_error_cont
266         
267 /* Traced system call support */
268 syscall_dotrace:
269         bl      save_nvgprs
270         addi    r3,r1,STACK_FRAME_OVERHEAD
271         bl      do_syscall_trace_enter
272
273         /*
274          * We use the return value of do_syscall_trace_enter() as the syscall
275          * number. If the syscall was rejected for any reason do_syscall_trace_enter()
276          * returns an invalid syscall number and the test below against
277          * NR_syscalls will fail.
278          */
279         mr      r0,r3
280
281         /* Restore argument registers just clobbered and/or possibly changed. */
282         ld      r3,GPR3(r1)
283         ld      r4,GPR4(r1)
284         ld      r5,GPR5(r1)
285         ld      r6,GPR6(r1)
286         ld      r7,GPR7(r1)
287         ld      r8,GPR8(r1)
288
289         /* Repopulate r9 and r10 for the system_call path */
290         addi    r9,r1,STACK_FRAME_OVERHEAD
291         CURRENT_THREAD_INFO(r10, r1)
292         ld      r10,TI_FLAGS(r10)
293
294         cmpldi  r0,NR_syscalls
295         blt+    system_call
296
297         /* Return code is already in r3 thanks to do_syscall_trace_enter() */
298         b       .Lsyscall_exit
299
300
301 syscall_enosys:
302         li      r3,-ENOSYS
303         b       .Lsyscall_exit
304         
305 syscall_exit_work:
306 #ifdef CONFIG_PPC_BOOK3S
307         li      r10,MSR_RI
308         mtmsrd  r10,1           /* Restore RI */
309 #endif
310         /* If TIF_RESTOREALL is set, don't scribble on either r3 or ccr.
311          If TIF_NOERROR is set, just save r3 as it is. */
312
313         andi.   r0,r9,_TIF_RESTOREALL
314         beq+    0f
315         REST_NVGPRS(r1)
316         b       2f
317 0:      cmpld   r3,r11          /* r11 is -MAX_ERRNO */
318         blt+    1f
319         andi.   r0,r9,_TIF_NOERROR
320         bne-    1f
321         ld      r5,_CCR(r1)
322         neg     r3,r3
323         oris    r5,r5,0x1000    /* Set SO bit in CR */
324         std     r5,_CCR(r1)
325 1:      std     r3,GPR3(r1)
326 2:      andi.   r0,r9,(_TIF_PERSYSCALL_MASK)
327         beq     4f
328
329         /* Clear per-syscall TIF flags if any are set.  */
330
331         li      r11,_TIF_PERSYSCALL_MASK
332         addi    r12,r12,TI_FLAGS
333 3:      ldarx   r10,0,r12
334         andc    r10,r10,r11
335         stdcx.  r10,0,r12
336         bne-    3b
337         subi    r12,r12,TI_FLAGS
338
339 4:      /* Anything else left to do? */
340 BEGIN_FTR_SECTION
341         lis     r3,INIT_PPR@highest     /* Set thread.ppr = 3 */
342         ld      r10,PACACURRENT(r13)
343         sldi    r3,r3,32        /* bits 11-13 are used for ppr */
344         std     r3,TASKTHREADPPR(r10)
345 END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
346
347         andi.   r0,r9,(_TIF_SYSCALL_DOTRACE|_TIF_SINGLESTEP)
348         beq     ret_from_except_lite
349
350         /* Re-enable interrupts */
351 #ifdef CONFIG_PPC_BOOK3E
352         wrteei  1
353 #else
354         li      r10,MSR_RI
355         ori     r10,r10,MSR_EE
356         mtmsrd  r10,1
357 #endif /* CONFIG_PPC_BOOK3E */
358
359         bl      save_nvgprs
360         addi    r3,r1,STACK_FRAME_OVERHEAD
361         bl      do_syscall_trace_leave
362         b       ret_from_except
363
364 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
365 tabort_syscall:
366         /* Firstly we need to enable TM in the kernel */
367         mfmsr   r10
368         li      r9, 1
369         rldimi  r10, r9, MSR_TM_LG, 63-MSR_TM_LG
370         mtmsrd  r10, 0
371
372         /* tabort, this dooms the transaction, nothing else */
373         li      r9, (TM_CAUSE_SYSCALL|TM_CAUSE_PERSISTENT)
374         TABORT(R9)
375
376         /*
377          * Return directly to userspace. We have corrupted user register state,
378          * but userspace will never see that register state. Execution will
379          * resume after the tbegin of the aborted transaction with the
380          * checkpointed register state.
381          */
382         li      r9, MSR_RI
383         andc    r10, r10, r9
384         mtmsrd  r10, 1
385         mtspr   SPRN_SRR0, r11
386         mtspr   SPRN_SRR1, r12
387
388         rfid
389         b       .       /* prevent speculative execution */
390 #endif
391
392 /* Save non-volatile GPRs, if not already saved. */
393 _GLOBAL(save_nvgprs)
394         ld      r11,_TRAP(r1)
395         andi.   r0,r11,1
396         beqlr-
397         SAVE_NVGPRS(r1)
398         clrrdi  r0,r11,1
399         std     r0,_TRAP(r1)
400         blr
401
402         
403 /*
404  * The sigsuspend and rt_sigsuspend system calls can call do_signal
405  * and thus put the process into the stopped state where we might
406  * want to examine its user state with ptrace.  Therefore we need
407  * to save all the nonvolatile registers (r14 - r31) before calling
408  * the C code.  Similarly, fork, vfork and clone need the full
409  * register state on the stack so that it can be copied to the child.
410  */
411
412 _GLOBAL(ppc_fork)
413         bl      save_nvgprs
414         bl      sys_fork
415         b       .Lsyscall_exit
416
417 _GLOBAL(ppc_vfork)
418         bl      save_nvgprs
419         bl      sys_vfork
420         b       .Lsyscall_exit
421
422 _GLOBAL(ppc_clone)
423         bl      save_nvgprs
424         bl      sys_clone
425         b       .Lsyscall_exit
426
427 _GLOBAL(ppc32_swapcontext)
428         bl      save_nvgprs
429         bl      compat_sys_swapcontext
430         b       .Lsyscall_exit
431
432 _GLOBAL(ppc64_swapcontext)
433         bl      save_nvgprs
434         bl      sys_swapcontext
435         b       .Lsyscall_exit
436
437 _GLOBAL(ppc_switch_endian)
438         bl      save_nvgprs
439         bl      sys_switch_endian
440         b       .Lsyscall_exit
441
442 _GLOBAL(ret_from_fork)
443         bl      schedule_tail
444         REST_NVGPRS(r1)
445         li      r3,0
446         b       .Lsyscall_exit
447
448 _GLOBAL(ret_from_kernel_thread)
449         bl      schedule_tail
450         REST_NVGPRS(r1)
451         mtlr    r14
452         mr      r3,r15
453 #ifdef PPC64_ELF_ABI_v2
454         mr      r12,r14
455 #endif
456         blrl
457         li      r3,0
458         b       .Lsyscall_exit
459
460 /*
461  * This routine switches between two different tasks.  The process
462  * state of one is saved on its kernel stack.  Then the state
463  * of the other is restored from its kernel stack.  The memory
464  * management hardware is updated to the second process's state.
465  * Finally, we can return to the second process, via ret_from_except.
466  * On entry, r3 points to the THREAD for the current task, r4
467  * points to the THREAD for the new task.
468  *
469  * Note: there are two ways to get to the "going out" portion
470  * of this code; either by coming in via the entry (_switch)
471  * or via "fork" which must set up an environment equivalent
472  * to the "_switch" path.  If you change this you'll have to change
473  * the fork code also.
474  *
475  * The code which creates the new task context is in 'copy_thread'
476  * in arch/powerpc/kernel/process.c 
477  */
478         .align  7
479 _GLOBAL(_switch)
480         mflr    r0
481         std     r0,16(r1)
482         stdu    r1,-SWITCH_FRAME_SIZE(r1)
483         /* r3-r13 are caller saved -- Cort */
484         SAVE_8GPRS(14, r1)
485         SAVE_10GPRS(22, r1)
486         std     r0,_NIP(r1)     /* Return to switch caller */
487         mfcr    r23
488         std     r23,_CCR(r1)
489         std     r1,KSP(r3)      /* Set old stack pointer */
490
491 #ifdef CONFIG_SMP
492         /* We need a sync somewhere here to make sure that if the
493          * previous task gets rescheduled on another CPU, it sees all
494          * stores it has performed on this one.
495          */
496         sync
497 #endif /* CONFIG_SMP */
498
499         /*
500          * If we optimise away the clear of the reservation in system
501          * calls because we know the CPU tracks the address of the
502          * reservation, then we need to clear it here to cover the
503          * case that the kernel context switch path has no larx
504          * instructions.
505          */
506 BEGIN_FTR_SECTION
507         ldarx   r6,0,r1
508 END_FTR_SECTION_IFSET(CPU_FTR_STCX_CHECKS_ADDRESS)
509
510 BEGIN_FTR_SECTION
511 /*
512  * A cp_abort (copy paste abort) here ensures that when context switching, a
513  * copy from one process can't leak into the paste of another.
514  */
515         PPC_CP_ABORT
516 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
517
518 #ifdef CONFIG_PPC_BOOK3S
519 /* Cancel all explict user streams as they will have no use after context
520  * switch and will stop the HW from creating streams itself
521  */
522         DCBT_STOP_ALL_STREAM_IDS(r6)
523 #endif
524
525         addi    r6,r4,-THREAD   /* Convert THREAD to 'current' */
526         std     r6,PACACURRENT(r13)     /* Set new 'current' */
527
528         ld      r8,KSP(r4)      /* new stack pointer */
529 #ifdef CONFIG_PPC_STD_MMU_64
530 BEGIN_MMU_FTR_SECTION
531         b       2f
532 END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_RADIX)
533 BEGIN_FTR_SECTION
534         clrrdi  r6,r8,28        /* get its ESID */
535         clrrdi  r9,r1,28        /* get current sp ESID */
536 FTR_SECTION_ELSE
537         clrrdi  r6,r8,40        /* get its 1T ESID */
538         clrrdi  r9,r1,40        /* get current sp 1T ESID */
539 ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_1T_SEGMENT)
540         clrldi. r0,r6,2         /* is new ESID c00000000? */
541         cmpd    cr1,r6,r9       /* or is new ESID the same as current ESID? */
542         cror    eq,4*cr1+eq,eq
543         beq     2f              /* if yes, don't slbie it */
544
545         /* Bolt in the new stack SLB entry */
546         ld      r7,KSP_VSID(r4) /* Get new stack's VSID */
547         oris    r0,r6,(SLB_ESID_V)@h
548         ori     r0,r0,(SLB_NUM_BOLTED-1)@l
549 BEGIN_FTR_SECTION
550         li      r9,MMU_SEGSIZE_1T       /* insert B field */
551         oris    r6,r6,(MMU_SEGSIZE_1T << SLBIE_SSIZE_SHIFT)@h
552         rldimi  r7,r9,SLB_VSID_SSIZE_SHIFT,0
553 END_MMU_FTR_SECTION_IFSET(MMU_FTR_1T_SEGMENT)
554
555         /* Update the last bolted SLB.  No write barriers are needed
556          * here, provided we only update the current CPU's SLB shadow
557          * buffer.
558          */
559         ld      r9,PACA_SLBSHADOWPTR(r13)
560         li      r12,0
561         std     r12,SLBSHADOW_STACKESID(r9)     /* Clear ESID */
562         li      r12,SLBSHADOW_STACKVSID
563         STDX_BE r7,r12,r9                       /* Save VSID */
564         li      r12,SLBSHADOW_STACKESID
565         STDX_BE r0,r12,r9                       /* Save ESID */
566
567         /* No need to check for MMU_FTR_NO_SLBIE_B here, since when
568          * we have 1TB segments, the only CPUs known to have the errata
569          * only support less than 1TB of system memory and we'll never
570          * actually hit this code path.
571          */
572
573         slbie   r6
574         slbie   r6              /* Workaround POWER5 < DD2.1 issue */
575         slbmte  r7,r0
576         isync
577 2:
578 #endif /* CONFIG_PPC_STD_MMU_64 */
579
580         CURRENT_THREAD_INFO(r7, r8)  /* base of new stack */
581         /* Note: this uses SWITCH_FRAME_SIZE rather than INT_FRAME_SIZE
582            because we don't need to leave the 288-byte ABI gap at the
583            top of the kernel stack. */
584         addi    r7,r7,THREAD_SIZE-SWITCH_FRAME_SIZE
585
586         mr      r1,r8           /* start using new stack pointer */
587         std     r7,PACAKSAVE(r13)
588
589         ld      r6,_CCR(r1)
590         mtcrf   0xFF,r6
591
592         /* r3-r13 are destroyed -- Cort */
593         REST_8GPRS(14, r1)
594         REST_10GPRS(22, r1)
595
596         /* convert old thread to its task_struct for return value */
597         addi    r3,r3,-THREAD
598         ld      r7,_NIP(r1)     /* Return to _switch caller in new task */
599         mtlr    r7
600         addi    r1,r1,SWITCH_FRAME_SIZE
601         blr
602
603         .align  7
604 _GLOBAL(ret_from_except)
605         ld      r11,_TRAP(r1)
606         andi.   r0,r11,1
607         bne     ret_from_except_lite
608         REST_NVGPRS(r1)
609
610 _GLOBAL(ret_from_except_lite)
611         /*
612          * Disable interrupts so that current_thread_info()->flags
613          * can't change between when we test it and when we return
614          * from the interrupt.
615          */
616 #ifdef CONFIG_PPC_BOOK3E
617         wrteei  0
618 #else
619         li      r10,MSR_RI
620         mtmsrd  r10,1             /* Update machine state */
621 #endif /* CONFIG_PPC_BOOK3E */
622
623         CURRENT_THREAD_INFO(r9, r1)
624         ld      r3,_MSR(r1)
625 #ifdef CONFIG_PPC_BOOK3E
626         ld      r10,PACACURRENT(r13)
627 #endif /* CONFIG_PPC_BOOK3E */
628         ld      r4,TI_FLAGS(r9)
629         andi.   r3,r3,MSR_PR
630         beq     resume_kernel
631 #ifdef CONFIG_PPC_BOOK3E
632         lwz     r3,(THREAD+THREAD_DBCR0)(r10)
633 #endif /* CONFIG_PPC_BOOK3E */
634
635         /* Check current_thread_info()->flags */
636         andi.   r0,r4,_TIF_USER_WORK_MASK
637         bne     1f
638 #ifdef CONFIG_PPC_BOOK3E
639         /*
640          * Check to see if the dbcr0 register is set up to debug.
641          * Use the internal debug mode bit to do this.
642          */
643         andis.  r0,r3,DBCR0_IDM@h
644         beq     restore
645         mfmsr   r0
646         rlwinm  r0,r0,0,~MSR_DE /* Clear MSR.DE */
647         mtmsr   r0
648         mtspr   SPRN_DBCR0,r3
649         li      r10, -1
650         mtspr   SPRN_DBSR,r10
651         b       restore
652 #else
653         addi    r3,r1,STACK_FRAME_OVERHEAD
654         bl      restore_math
655         b       restore
656 #endif
657 1:      andi.   r0,r4,_TIF_NEED_RESCHED
658         beq     2f
659         bl      restore_interrupts
660         SCHEDULE_USER
661         b       ret_from_except_lite
662 2:
663 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
664         andi.   r0,r4,_TIF_USER_WORK_MASK & ~_TIF_RESTORE_TM
665         bne     3f              /* only restore TM if nothing else to do */
666         addi    r3,r1,STACK_FRAME_OVERHEAD
667         bl      restore_tm_state
668         b       restore
669 3:
670 #endif
671         bl      save_nvgprs
672         /*
673          * Use a non volatile GPR to save and restore our thread_info flags
674          * across the call to restore_interrupts.
675          */
676         mr      r30,r4
677         bl      restore_interrupts
678         mr      r4,r30
679         addi    r3,r1,STACK_FRAME_OVERHEAD
680         bl      do_notify_resume
681         b       ret_from_except
682
683 resume_kernel:
684         /* check current_thread_info, _TIF_EMULATE_STACK_STORE */
685         andis.  r8,r4,_TIF_EMULATE_STACK_STORE@h
686         beq+    1f
687
688         addi    r8,r1,INT_FRAME_SIZE    /* Get the kprobed function entry */
689
690         ld      r3,GPR1(r1)
691         subi    r3,r3,INT_FRAME_SIZE    /* dst: Allocate a trampoline exception frame */
692         mr      r4,r1                   /* src:  current exception frame */
693         mr      r1,r3                   /* Reroute the trampoline frame to r1 */
694
695         /* Copy from the original to the trampoline. */
696         li      r5,INT_FRAME_SIZE/8     /* size: INT_FRAME_SIZE */
697         li      r6,0                    /* start offset: 0 */
698         mtctr   r5
699 2:      ldx     r0,r6,r4
700         stdx    r0,r6,r3
701         addi    r6,r6,8
702         bdnz    2b
703
704         /* Do real store operation to complete stdu */
705         ld      r5,GPR1(r1)
706         std     r8,0(r5)
707
708         /* Clear _TIF_EMULATE_STACK_STORE flag */
709         lis     r11,_TIF_EMULATE_STACK_STORE@h
710         addi    r5,r9,TI_FLAGS
711 0:      ldarx   r4,0,r5
712         andc    r4,r4,r11
713         stdcx.  r4,0,r5
714         bne-    0b
715 1:
716
717 #ifdef CONFIG_PREEMPT
718         /* Check if we need to preempt */
719         andi.   r0,r4,_TIF_NEED_RESCHED
720         beq+    restore
721         /* Check that preempt_count() == 0 and interrupts are enabled */
722         lwz     r8,TI_PREEMPT(r9)
723         cmpwi   cr1,r8,0
724         ld      r0,SOFTE(r1)
725         cmpdi   r0,0
726         crandc  eq,cr1*4+eq,eq
727         bne     restore
728
729         /*
730          * Here we are preempting the current task. We want to make
731          * sure we are soft-disabled first and reconcile irq state.
732          */
733         RECONCILE_IRQ_STATE(r3,r4)
734 1:      bl      preempt_schedule_irq
735
736         /* Re-test flags and eventually loop */
737         CURRENT_THREAD_INFO(r9, r1)
738         ld      r4,TI_FLAGS(r9)
739         andi.   r0,r4,_TIF_NEED_RESCHED
740         bne     1b
741
742         /*
743          * arch_local_irq_restore() from preempt_schedule_irq above may
744          * enable hard interrupt but we really should disable interrupts
745          * when we return from the interrupt, and so that we don't get
746          * interrupted after loading SRR0/1.
747          */
748 #ifdef CONFIG_PPC_BOOK3E
749         wrteei  0
750 #else
751         li      r10,MSR_RI
752         mtmsrd  r10,1             /* Update machine state */
753 #endif /* CONFIG_PPC_BOOK3E */
754 #endif /* CONFIG_PREEMPT */
755
756         .globl  fast_exc_return_irq
757 fast_exc_return_irq:
758 restore:
759         /*
760          * This is the main kernel exit path. First we check if we
761          * are about to re-enable interrupts
762          */
763         ld      r5,SOFTE(r1)
764         lbz     r6,PACASOFTIRQEN(r13)
765         cmpwi   cr0,r5,0
766         beq     restore_irq_off
767
768         /* We are enabling, were we already enabled ? Yes, just return */
769         cmpwi   cr0,r6,1
770         beq     cr0,do_restore
771
772         /*
773          * We are about to soft-enable interrupts (we are hard disabled
774          * at this point). We check if there's anything that needs to
775          * be replayed first.
776          */
777         lbz     r0,PACAIRQHAPPENED(r13)
778         cmpwi   cr0,r0,0
779         bne-    restore_check_irq_replay
780
781         /*
782          * Get here when nothing happened while soft-disabled, just
783          * soft-enable and move-on. We will hard-enable as a side
784          * effect of rfi
785          */
786 restore_no_replay:
787         TRACE_ENABLE_INTS
788         li      r0,1
789         stb     r0,PACASOFTIRQEN(r13);
790
791         /*
792          * Final return path. BookE is handled in a different file
793          */
794 do_restore:
795 #ifdef CONFIG_PPC_BOOK3E
796         b       exception_return_book3e
797 #else
798         /*
799          * Clear the reservation. If we know the CPU tracks the address of
800          * the reservation then we can potentially save some cycles and use
801          * a larx. On POWER6 and POWER7 this is significantly faster.
802          */
803 BEGIN_FTR_SECTION
804         stdcx.  r0,0,r1         /* to clear the reservation */
805 FTR_SECTION_ELSE
806         ldarx   r4,0,r1
807 ALT_FTR_SECTION_END_IFCLR(CPU_FTR_STCX_CHECKS_ADDRESS)
808
809         /*
810          * Some code path such as load_up_fpu or altivec return directly
811          * here. They run entirely hard disabled and do not alter the
812          * interrupt state. They also don't use lwarx/stwcx. and thus
813          * are known not to leave dangling reservations.
814          */
815         .globl  fast_exception_return
816 fast_exception_return:
817         ld      r3,_MSR(r1)
818         ld      r4,_CTR(r1)
819         ld      r0,_LINK(r1)
820         mtctr   r4
821         mtlr    r0
822         ld      r4,_XER(r1)
823         mtspr   SPRN_XER,r4
824
825         REST_8GPRS(5, r1)
826
827         andi.   r0,r3,MSR_RI
828         beq-    unrecov_restore
829
830         /* Load PPR from thread struct before we clear MSR:RI */
831 BEGIN_FTR_SECTION
832         ld      r2,PACACURRENT(r13)
833         ld      r2,TASKTHREADPPR(r2)
834 END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
835
836         /*
837          * Clear RI before restoring r13.  If we are returning to
838          * userspace and we take an exception after restoring r13,
839          * we end up corrupting the userspace r13 value.
840          */
841         li      r4,0
842         mtmsrd  r4,1
843
844 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
845         /* TM debug */
846         std     r3, PACATMSCRATCH(r13) /* Stash returned-to MSR */
847 #endif
848         /*
849          * r13 is our per cpu area, only restore it if we are returning to
850          * userspace the value stored in the stack frame may belong to
851          * another CPU.
852          */
853         andi.   r0,r3,MSR_PR
854         beq     1f
855 BEGIN_FTR_SECTION
856         mtspr   SPRN_PPR,r2     /* Restore PPR */
857 END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR)
858         ACCOUNT_CPU_USER_EXIT(r13, r2, r4)
859         REST_GPR(13, r1)
860 1:
861         mtspr   SPRN_SRR1,r3
862
863         ld      r2,_CCR(r1)
864         mtcrf   0xFF,r2
865         ld      r2,_NIP(r1)
866         mtspr   SPRN_SRR0,r2
867
868         ld      r0,GPR0(r1)
869         ld      r2,GPR2(r1)
870         ld      r3,GPR3(r1)
871         ld      r4,GPR4(r1)
872         ld      r1,GPR1(r1)
873
874         rfid
875         b       .       /* prevent speculative execution */
876
877 #endif /* CONFIG_PPC_BOOK3E */
878
879         /*
880          * We are returning to a context with interrupts soft disabled.
881          *
882          * However, we may also about to hard enable, so we need to
883          * make sure that in this case, we also clear PACA_IRQ_HARD_DIS
884          * or that bit can get out of sync and bad things will happen
885          */
886 restore_irq_off:
887         ld      r3,_MSR(r1)
888         lbz     r7,PACAIRQHAPPENED(r13)
889         andi.   r0,r3,MSR_EE
890         beq     1f
891         rlwinm  r7,r7,0,~PACA_IRQ_HARD_DIS
892         stb     r7,PACAIRQHAPPENED(r13)
893 1:      li      r0,0
894         stb     r0,PACASOFTIRQEN(r13);
895         TRACE_DISABLE_INTS
896         b       do_restore
897
898         /*
899          * Something did happen, check if a re-emit is needed
900          * (this also clears paca->irq_happened)
901          */
902 restore_check_irq_replay:
903         /* XXX: We could implement a fast path here where we check
904          * for irq_happened being just 0x01, in which case we can
905          * clear it and return. That means that we would potentially
906          * miss a decrementer having wrapped all the way around.
907          *
908          * Still, this might be useful for things like hash_page
909          */
910         bl      __check_irq_replay
911         cmpwi   cr0,r3,0
912         beq     restore_no_replay
913  
914         /*
915          * We need to re-emit an interrupt. We do so by re-using our
916          * existing exception frame. We first change the trap value,
917          * but we need to ensure we preserve the low nibble of it
918          */
919         ld      r4,_TRAP(r1)
920         clrldi  r4,r4,60
921         or      r4,r4,r3
922         std     r4,_TRAP(r1)
923
924         /*
925          * Then find the right handler and call it. Interrupts are
926          * still soft-disabled and we keep them that way.
927         */
928         cmpwi   cr0,r3,0x500
929         bne     1f
930         addi    r3,r1,STACK_FRAME_OVERHEAD;
931         bl      do_IRQ
932         b       ret_from_except
933 1:      cmpwi   cr0,r3,0xe60
934         bne     1f
935         addi    r3,r1,STACK_FRAME_OVERHEAD;
936         bl      handle_hmi_exception
937         b       ret_from_except
938 1:      cmpwi   cr0,r3,0x900
939         bne     1f
940         addi    r3,r1,STACK_FRAME_OVERHEAD;
941         bl      timer_interrupt
942         b       ret_from_except
943 #ifdef CONFIG_PPC_DOORBELL
944 1:
945 #ifdef CONFIG_PPC_BOOK3E
946         cmpwi   cr0,r3,0x280
947 #else
948         BEGIN_FTR_SECTION
949                 cmpwi   cr0,r3,0xe80
950         FTR_SECTION_ELSE
951                 cmpwi   cr0,r3,0xa00
952         ALT_FTR_SECTION_END_IFSET(CPU_FTR_HVMODE)
953 #endif /* CONFIG_PPC_BOOK3E */
954         bne     1f
955         addi    r3,r1,STACK_FRAME_OVERHEAD;
956         bl      doorbell_exception
957         b       ret_from_except
958 #endif /* CONFIG_PPC_DOORBELL */
959 1:      b       ret_from_except /* What else to do here ? */
960  
961 unrecov_restore:
962         addi    r3,r1,STACK_FRAME_OVERHEAD
963         bl      unrecoverable_exception
964         b       unrecov_restore
965
966 #ifdef CONFIG_PPC_RTAS
967 /*
968  * On CHRP, the Run-Time Abstraction Services (RTAS) have to be
969  * called with the MMU off.
970  *
971  * In addition, we need to be in 32b mode, at least for now.
972  * 
973  * Note: r3 is an input parameter to rtas, so don't trash it...
974  */
975 _GLOBAL(enter_rtas)
976         mflr    r0
977         std     r0,16(r1)
978         stdu    r1,-RTAS_FRAME_SIZE(r1) /* Save SP and create stack space. */
979
980         /* Because RTAS is running in 32b mode, it clobbers the high order half
981          * of all registers that it saves.  We therefore save those registers
982          * RTAS might touch to the stack.  (r0, r3-r13 are caller saved)
983          */
984         SAVE_GPR(2, r1)                 /* Save the TOC */
985         SAVE_GPR(13, r1)                /* Save paca */
986         SAVE_8GPRS(14, r1)              /* Save the non-volatiles */
987         SAVE_10GPRS(22, r1)             /* ditto */
988
989         mfcr    r4
990         std     r4,_CCR(r1)
991         mfctr   r5
992         std     r5,_CTR(r1)
993         mfspr   r6,SPRN_XER
994         std     r6,_XER(r1)
995         mfdar   r7
996         std     r7,_DAR(r1)
997         mfdsisr r8
998         std     r8,_DSISR(r1)
999
1000         /* Temporary workaround to clear CR until RTAS can be modified to
1001          * ignore all bits.
1002          */
1003         li      r0,0
1004         mtcr    r0
1005
1006 #ifdef CONFIG_BUG       
1007         /* There is no way it is acceptable to get here with interrupts enabled,
1008          * check it with the asm equivalent of WARN_ON
1009          */
1010         lbz     r0,PACASOFTIRQEN(r13)
1011 1:      tdnei   r0,0
1012         EMIT_BUG_ENTRY 1b,__FILE__,__LINE__,BUGFLAG_WARNING
1013 #endif
1014         
1015         /* Hard-disable interrupts */
1016         mfmsr   r6
1017         rldicl  r7,r6,48,1
1018         rotldi  r7,r7,16
1019         mtmsrd  r7,1
1020
1021         /* Unfortunately, the stack pointer and the MSR are also clobbered,
1022          * so they are saved in the PACA which allows us to restore
1023          * our original state after RTAS returns.
1024          */
1025         std     r1,PACAR1(r13)
1026         std     r6,PACASAVEDMSR(r13)
1027
1028         /* Setup our real return addr */        
1029         LOAD_REG_ADDR(r4,rtas_return_loc)
1030         clrldi  r4,r4,2                 /* convert to realmode address */
1031         mtlr    r4
1032
1033         li      r0,0
1034         ori     r0,r0,MSR_EE|MSR_SE|MSR_BE|MSR_RI
1035         andc    r0,r6,r0
1036         
1037         li      r9,1
1038         rldicr  r9,r9,MSR_SF_LG,(63-MSR_SF_LG)
1039         ori     r9,r9,MSR_IR|MSR_DR|MSR_FE0|MSR_FE1|MSR_FP|MSR_RI|MSR_LE
1040         andc    r6,r0,r9
1041         sync                            /* disable interrupts so SRR0/1 */
1042         mtmsrd  r0                      /* don't get trashed */
1043
1044         LOAD_REG_ADDR(r4, rtas)
1045         ld      r5,RTASENTRY(r4)        /* get the rtas->entry value */
1046         ld      r4,RTASBASE(r4)         /* get the rtas->base value */
1047         
1048         mtspr   SPRN_SRR0,r5
1049         mtspr   SPRN_SRR1,r6
1050         rfid
1051         b       .       /* prevent speculative execution */
1052
1053 rtas_return_loc:
1054         FIXUP_ENDIAN
1055
1056         /* relocation is off at this point */
1057         GET_PACA(r4)
1058         clrldi  r4,r4,2                 /* convert to realmode address */
1059
1060         bcl     20,31,$+4
1061 0:      mflr    r3
1062         ld      r3,(1f-0b)(r3)          /* get &rtas_restore_regs */
1063
1064         mfmsr   r6
1065         li      r0,MSR_RI
1066         andc    r6,r6,r0
1067         sync    
1068         mtmsrd  r6
1069         
1070         ld      r1,PACAR1(r4)           /* Restore our SP */
1071         ld      r4,PACASAVEDMSR(r4)     /* Restore our MSR */
1072
1073         mtspr   SPRN_SRR0,r3
1074         mtspr   SPRN_SRR1,r4
1075         rfid
1076         b       .       /* prevent speculative execution */
1077
1078         .align  3
1079 1:      .llong  rtas_restore_regs
1080
1081 rtas_restore_regs:
1082         /* relocation is on at this point */
1083         REST_GPR(2, r1)                 /* Restore the TOC */
1084         REST_GPR(13, r1)                /* Restore paca */
1085         REST_8GPRS(14, r1)              /* Restore the non-volatiles */
1086         REST_10GPRS(22, r1)             /* ditto */
1087
1088         GET_PACA(r13)
1089
1090         ld      r4,_CCR(r1)
1091         mtcr    r4
1092         ld      r5,_CTR(r1)
1093         mtctr   r5
1094         ld      r6,_XER(r1)
1095         mtspr   SPRN_XER,r6
1096         ld      r7,_DAR(r1)
1097         mtdar   r7
1098         ld      r8,_DSISR(r1)
1099         mtdsisr r8
1100
1101         addi    r1,r1,RTAS_FRAME_SIZE   /* Unstack our frame */
1102         ld      r0,16(r1)               /* get return address */
1103
1104         mtlr    r0
1105         blr                             /* return to caller */
1106
1107 #endif /* CONFIG_PPC_RTAS */
1108
1109 _GLOBAL(enter_prom)
1110         mflr    r0
1111         std     r0,16(r1)
1112         stdu    r1,-PROM_FRAME_SIZE(r1) /* Save SP and create stack space */
1113
1114         /* Because PROM is running in 32b mode, it clobbers the high order half
1115          * of all registers that it saves.  We therefore save those registers
1116          * PROM might touch to the stack.  (r0, r3-r13 are caller saved)
1117          */
1118         SAVE_GPR(2, r1)
1119         SAVE_GPR(13, r1)
1120         SAVE_8GPRS(14, r1)
1121         SAVE_10GPRS(22, r1)
1122         mfcr    r10
1123         mfmsr   r11
1124         std     r10,_CCR(r1)
1125         std     r11,_MSR(r1)
1126
1127         /* Put PROM address in SRR0 */
1128         mtsrr0  r4
1129
1130         /* Setup our trampoline return addr in LR */
1131         bcl     20,31,$+4
1132 0:      mflr    r4
1133         addi    r4,r4,(1f - 0b)
1134         mtlr    r4
1135
1136         /* Prepare a 32-bit mode big endian MSR
1137          */
1138 #ifdef CONFIG_PPC_BOOK3E
1139         rlwinm  r11,r11,0,1,31
1140         mtsrr1  r11
1141         rfi
1142 #else /* CONFIG_PPC_BOOK3E */
1143         LOAD_REG_IMMEDIATE(r12, MSR_SF | MSR_ISF | MSR_LE)
1144         andc    r11,r11,r12
1145         mtsrr1  r11
1146         rfid
1147 #endif /* CONFIG_PPC_BOOK3E */
1148
1149 1:      /* Return from OF */
1150         FIXUP_ENDIAN
1151
1152         /* Just make sure that r1 top 32 bits didn't get
1153          * corrupt by OF
1154          */
1155         rldicl  r1,r1,0,32
1156
1157         /* Restore the MSR (back to 64 bits) */
1158         ld      r0,_MSR(r1)
1159         MTMSRD(r0)
1160         isync
1161
1162         /* Restore other registers */
1163         REST_GPR(2, r1)
1164         REST_GPR(13, r1)
1165         REST_8GPRS(14, r1)
1166         REST_10GPRS(22, r1)
1167         ld      r4,_CCR(r1)
1168         mtcr    r4
1169         
1170         addi    r1,r1,PROM_FRAME_SIZE
1171         ld      r0,16(r1)
1172         mtlr    r0
1173         blr