[GFS2] Fix up merge of Linus' kernel into GFS2
[pandora-kernel.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 <asm/unistd.h>
23 #include <asm/processor.h>
24 #include <asm/page.h>
25 #include <asm/mmu.h>
26 #include <asm/thread_info.h>
27 #include <asm/ppc_asm.h>
28 #include <asm/asm-offsets.h>
29 #include <asm/cputable.h>
30
31 #ifdef CONFIG_PPC_ISERIES
32 #define DO_SOFT_DISABLE
33 #endif
34
35 /*
36  * System calls.
37  */
38         .section        ".toc","aw"
39 .SYS_CALL_TABLE:
40         .tc .sys_call_table[TC],.sys_call_table
41
42 /* This value is used to mark exception frames on the stack. */
43 exception_marker:
44         .tc     ID_72656773_68657265[TC],0x7265677368657265
45
46         .section        ".text"
47         .align 7
48
49 #undef SHOW_SYSCALLS
50
51         .globl system_call_common
52 system_call_common:
53         andi.   r10,r12,MSR_PR
54         mr      r10,r1
55         addi    r1,r1,-INT_FRAME_SIZE
56         beq-    1f
57         ld      r1,PACAKSAVE(r13)
58 1:      std     r10,0(r1)
59         crclr   so
60         std     r11,_NIP(r1)
61         std     r12,_MSR(r1)
62         std     r0,GPR0(r1)
63         std     r10,GPR1(r1)
64         ACCOUNT_CPU_USER_ENTRY(r10, r11)
65         std     r2,GPR2(r1)
66         std     r3,GPR3(r1)
67         std     r4,GPR4(r1)
68         std     r5,GPR5(r1)
69         std     r6,GPR6(r1)
70         std     r7,GPR7(r1)
71         std     r8,GPR8(r1)
72         li      r11,0
73         std     r11,GPR9(r1)
74         std     r11,GPR10(r1)
75         std     r11,GPR11(r1)
76         std     r11,GPR12(r1)
77         std     r9,GPR13(r1)
78         mfcr    r9
79         mflr    r10
80         li      r11,0xc01
81         std     r9,_CCR(r1)
82         std     r10,_LINK(r1)
83         std     r11,_TRAP(r1)
84         mfxer   r9
85         mfctr   r10
86         std     r9,_XER(r1)
87         std     r10,_CTR(r1)
88         std     r3,ORIG_GPR3(r1)
89         ld      r2,PACATOC(r13)
90         addi    r9,r1,STACK_FRAME_OVERHEAD
91         ld      r11,exception_marker@toc(r2)
92         std     r11,-16(r9)             /* "regshere" marker */
93 #ifdef CONFIG_PPC_ISERIES
94         /* Hack for handling interrupts when soft-enabling on iSeries */
95         cmpdi   cr1,r0,0x5555           /* syscall 0x5555 */
96         andi.   r10,r12,MSR_PR          /* from kernel */
97         crand   4*cr0+eq,4*cr1+eq,4*cr0+eq
98         beq     hardware_interrupt_entry
99         lbz     r10,PACAPROCENABLED(r13)
100         std     r10,SOFTE(r1)
101 #endif
102         mfmsr   r11
103         ori     r11,r11,MSR_EE
104         mtmsrd  r11,1
105
106 #ifdef SHOW_SYSCALLS
107         bl      .do_show_syscall
108         REST_GPR(0,r1)
109         REST_4GPRS(3,r1)
110         REST_2GPRS(7,r1)
111         addi    r9,r1,STACK_FRAME_OVERHEAD
112 #endif
113         clrrdi  r11,r1,THREAD_SHIFT
114         ld      r10,TI_FLAGS(r11)
115         andi.   r11,r10,_TIF_SYSCALL_T_OR_A
116         bne-    syscall_dotrace
117 syscall_dotrace_cont:
118         cmpldi  0,r0,NR_syscalls
119         bge-    syscall_enosys
120
121 system_call:                    /* label this so stack traces look sane */
122 /*
123  * Need to vector to 32 Bit or default sys_call_table here,
124  * based on caller's run-mode / personality.
125  */
126         ld      r11,.SYS_CALL_TABLE@toc(2)
127         andi.   r10,r10,_TIF_32BIT
128         beq     15f
129         addi    r11,r11,8       /* use 32-bit syscall entries */
130         clrldi  r3,r3,32
131         clrldi  r4,r4,32
132         clrldi  r5,r5,32
133         clrldi  r6,r6,32
134         clrldi  r7,r7,32
135         clrldi  r8,r8,32
136 15:
137         slwi    r0,r0,4
138         ldx     r10,r11,r0      /* Fetch system call handler [ptr] */
139         mtctr   r10
140         bctrl                   /* Call handler */
141
142 syscall_exit:
143         std     r3,RESULT(r1)
144 #ifdef SHOW_SYSCALLS
145         bl      .do_show_syscall_exit
146         ld      r3,RESULT(r1)
147 #endif
148         clrrdi  r12,r1,THREAD_SHIFT
149
150         /* disable interrupts so current_thread_info()->flags can't change,
151            and so that we don't get interrupted after loading SRR0/1. */
152         ld      r8,_MSR(r1)
153         andi.   r10,r8,MSR_RI
154         beq-    unrecov_restore
155         mfmsr   r10
156         rldicl  r10,r10,48,1
157         rotldi  r10,r10,16
158         mtmsrd  r10,1
159         ld      r9,TI_FLAGS(r12)
160         li      r11,-_LAST_ERRNO
161         andi.   r0,r9,(_TIF_SYSCALL_T_OR_A|_TIF_SINGLESTEP|_TIF_USER_WORK_MASK|_TIF_PERSYSCALL_MASK)
162         bne-    syscall_exit_work
163         cmpld   r3,r11
164         ld      r5,_CCR(r1)
165         bge-    syscall_error
166 syscall_error_cont:
167         ld      r7,_NIP(r1)
168         stdcx.  r0,0,r1                 /* to clear the reservation */
169         andi.   r6,r8,MSR_PR
170         ld      r4,_LINK(r1)
171         beq-    1f
172         ACCOUNT_CPU_USER_EXIT(r11, r12)
173         ld      r13,GPR13(r1)   /* only restore r13 if returning to usermode */
174 1:      ld      r2,GPR2(r1)
175         li      r12,MSR_RI
176         andc    r11,r10,r12
177         mtmsrd  r11,1                   /* clear MSR.RI */
178         ld      r1,GPR1(r1)
179         mtlr    r4
180         mtcr    r5
181         mtspr   SPRN_SRR0,r7
182         mtspr   SPRN_SRR1,r8
183         rfid
184         b       .       /* prevent speculative execution */
185
186 syscall_error:  
187         oris    r5,r5,0x1000    /* Set SO bit in CR */
188         neg     r3,r3
189         std     r5,_CCR(r1)
190         b       syscall_error_cont
191         
192 /* Traced system call support */
193 syscall_dotrace:
194         bl      .save_nvgprs
195         addi    r3,r1,STACK_FRAME_OVERHEAD
196         bl      .do_syscall_trace_enter
197         ld      r0,GPR0(r1)     /* Restore original registers */
198         ld      r3,GPR3(r1)
199         ld      r4,GPR4(r1)
200         ld      r5,GPR5(r1)
201         ld      r6,GPR6(r1)
202         ld      r7,GPR7(r1)
203         ld      r8,GPR8(r1)
204         addi    r9,r1,STACK_FRAME_OVERHEAD
205         clrrdi  r10,r1,THREAD_SHIFT
206         ld      r10,TI_FLAGS(r10)
207         b       syscall_dotrace_cont
208
209 syscall_enosys:
210         li      r3,-ENOSYS
211         b       syscall_exit
212         
213 syscall_exit_work:
214         /* If TIF_RESTOREALL is set, don't scribble on either r3 or ccr.
215          If TIF_NOERROR is set, just save r3 as it is. */
216
217         andi.   r0,r9,_TIF_RESTOREALL
218         beq+    0f
219         REST_NVGPRS(r1)
220         b       2f
221 0:      cmpld   r3,r11          /* r10 is -LAST_ERRNO */
222         blt+    1f
223         andi.   r0,r9,_TIF_NOERROR
224         bne-    1f
225         ld      r5,_CCR(r1)
226         neg     r3,r3
227         oris    r5,r5,0x1000    /* Set SO bit in CR */
228         std     r5,_CCR(r1)
229 1:      std     r3,GPR3(r1)
230 2:      andi.   r0,r9,(_TIF_PERSYSCALL_MASK)
231         beq     4f
232
233         /* Clear per-syscall TIF flags if any are set.  */
234
235         li      r11,_TIF_PERSYSCALL_MASK
236         addi    r12,r12,TI_FLAGS
237 3:      ldarx   r10,0,r12
238         andc    r10,r10,r11
239         stdcx.  r10,0,r12
240         bne-    3b
241         subi    r12,r12,TI_FLAGS
242
243 4:      /* Anything else left to do? */
244         andi.   r0,r9,(_TIF_SYSCALL_T_OR_A|_TIF_SINGLESTEP)
245         beq     .ret_from_except_lite
246
247         /* Re-enable interrupts */
248         mfmsr   r10
249         ori     r10,r10,MSR_EE
250         mtmsrd  r10,1
251
252         bl      .save_nvgprs
253         addi    r3,r1,STACK_FRAME_OVERHEAD
254         bl      .do_syscall_trace_leave
255         b       .ret_from_except
256
257 /* Save non-volatile GPRs, if not already saved. */
258 _GLOBAL(save_nvgprs)
259         ld      r11,_TRAP(r1)
260         andi.   r0,r11,1
261         beqlr-
262         SAVE_NVGPRS(r1)
263         clrrdi  r0,r11,1
264         std     r0,_TRAP(r1)
265         blr
266
267         
268 /*
269  * The sigsuspend and rt_sigsuspend system calls can call do_signal
270  * and thus put the process into the stopped state where we might
271  * want to examine its user state with ptrace.  Therefore we need
272  * to save all the nonvolatile registers (r14 - r31) before calling
273  * the C code.  Similarly, fork, vfork and clone need the full
274  * register state on the stack so that it can be copied to the child.
275  */
276
277 _GLOBAL(ppc_fork)
278         bl      .save_nvgprs
279         bl      .sys_fork
280         b       syscall_exit
281
282 _GLOBAL(ppc_vfork)
283         bl      .save_nvgprs
284         bl      .sys_vfork
285         b       syscall_exit
286
287 _GLOBAL(ppc_clone)
288         bl      .save_nvgprs
289         bl      .sys_clone
290         b       syscall_exit
291
292 _GLOBAL(ppc32_swapcontext)
293         bl      .save_nvgprs
294         bl      .compat_sys_swapcontext
295         b       syscall_exit
296
297 _GLOBAL(ppc64_swapcontext)
298         bl      .save_nvgprs
299         bl      .sys_swapcontext
300         b       syscall_exit
301
302 _GLOBAL(ret_from_fork)
303         bl      .schedule_tail
304         REST_NVGPRS(r1)
305         li      r3,0
306         b       syscall_exit
307
308 /*
309  * This routine switches between two different tasks.  The process
310  * state of one is saved on its kernel stack.  Then the state
311  * of the other is restored from its kernel stack.  The memory
312  * management hardware is updated to the second process's state.
313  * Finally, we can return to the second process, via ret_from_except.
314  * On entry, r3 points to the THREAD for the current task, r4
315  * points to the THREAD for the new task.
316  *
317  * Note: there are two ways to get to the "going out" portion
318  * of this code; either by coming in via the entry (_switch)
319  * or via "fork" which must set up an environment equivalent
320  * to the "_switch" path.  If you change this you'll have to change
321  * the fork code also.
322  *
323  * The code which creates the new task context is in 'copy_thread'
324  * in arch/powerpc/kernel/process.c 
325  */
326         .align  7
327 _GLOBAL(_switch)
328         mflr    r0
329         std     r0,16(r1)
330         stdu    r1,-SWITCH_FRAME_SIZE(r1)
331         /* r3-r13 are caller saved -- Cort */
332         SAVE_8GPRS(14, r1)
333         SAVE_10GPRS(22, r1)
334         mflr    r20             /* Return to switch caller */
335         mfmsr   r22
336         li      r0, MSR_FP
337 #ifdef CONFIG_ALTIVEC
338 BEGIN_FTR_SECTION
339         oris    r0,r0,MSR_VEC@h /* Disable altivec */
340         mfspr   r24,SPRN_VRSAVE /* save vrsave register value */
341         std     r24,THREAD_VRSAVE(r3)
342 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
343 #endif /* CONFIG_ALTIVEC */
344         and.    r0,r0,r22
345         beq+    1f
346         andc    r22,r22,r0
347         mtmsrd  r22
348         isync
349 1:      std     r20,_NIP(r1)
350         mfcr    r23
351         std     r23,_CCR(r1)
352         std     r1,KSP(r3)      /* Set old stack pointer */
353
354 #ifdef CONFIG_SMP
355         /* We need a sync somewhere here to make sure that if the
356          * previous task gets rescheduled on another CPU, it sees all
357          * stores it has performed on this one.
358          */
359         sync
360 #endif /* CONFIG_SMP */
361
362         addi    r6,r4,-THREAD   /* Convert THREAD to 'current' */
363         std     r6,PACACURRENT(r13)     /* Set new 'current' */
364
365         ld      r8,KSP(r4)      /* new stack pointer */
366 BEGIN_FTR_SECTION
367         clrrdi  r6,r8,28        /* get its ESID */
368         clrrdi  r9,r1,28        /* get current sp ESID */
369         clrldi. r0,r6,2         /* is new ESID c00000000? */
370         cmpd    cr1,r6,r9       /* or is new ESID the same as current ESID? */
371         cror    eq,4*cr1+eq,eq
372         beq     2f              /* if yes, don't slbie it */
373
374         /* Bolt in the new stack SLB entry */
375         ld      r7,KSP_VSID(r4) /* Get new stack's VSID */
376         oris    r0,r6,(SLB_ESID_V)@h
377         ori     r0,r0,(SLB_NUM_BOLTED-1)@l
378
379         /* Update the last bolted SLB */
380         ld      r9,PACA_SLBSHADOWPTR(r13)
381         li      r12,0
382         std     r12,SLBSHADOW_STACKESID(r9) /* Clear ESID */
383         std     r7,SLBSHADOW_STACKVSID(r9)  /* Save VSID */
384         std     r0,SLBSHADOW_STACKESID(r9)  /* Save ESID */
385
386         slbie   r6
387         slbie   r6              /* Workaround POWER5 < DD2.1 issue */
388         slbmte  r7,r0
389         isync
390
391 2:
392 END_FTR_SECTION_IFSET(CPU_FTR_SLB)
393         clrrdi  r7,r8,THREAD_SHIFT      /* base of new stack */
394         /* Note: this uses SWITCH_FRAME_SIZE rather than INT_FRAME_SIZE
395            because we don't need to leave the 288-byte ABI gap at the
396            top of the kernel stack. */
397         addi    r7,r7,THREAD_SIZE-SWITCH_FRAME_SIZE
398
399         mr      r1,r8           /* start using new stack pointer */
400         std     r7,PACAKSAVE(r13)
401
402         ld      r6,_CCR(r1)
403         mtcrf   0xFF,r6
404
405 #ifdef CONFIG_ALTIVEC
406 BEGIN_FTR_SECTION
407         ld      r0,THREAD_VRSAVE(r4)
408         mtspr   SPRN_VRSAVE,r0          /* if G4, restore VRSAVE reg */
409 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
410 #endif /* CONFIG_ALTIVEC */
411
412         /* r3-r13 are destroyed -- Cort */
413         REST_8GPRS(14, r1)
414         REST_10GPRS(22, r1)
415
416         /* convert old thread to its task_struct for return value */
417         addi    r3,r3,-THREAD
418         ld      r7,_NIP(r1)     /* Return to _switch caller in new task */
419         mtlr    r7
420         addi    r1,r1,SWITCH_FRAME_SIZE
421         blr
422
423         .align  7
424 _GLOBAL(ret_from_except)
425         ld      r11,_TRAP(r1)
426         andi.   r0,r11,1
427         bne     .ret_from_except_lite
428         REST_NVGPRS(r1)
429
430 _GLOBAL(ret_from_except_lite)
431         /*
432          * Disable interrupts so that current_thread_info()->flags
433          * can't change between when we test it and when we return
434          * from the interrupt.
435          */
436         mfmsr   r10             /* Get current interrupt state */
437         rldicl  r9,r10,48,1     /* clear MSR_EE */
438         rotldi  r9,r9,16
439         mtmsrd  r9,1            /* Update machine state */
440
441 #ifdef CONFIG_PREEMPT
442         clrrdi  r9,r1,THREAD_SHIFT      /* current_thread_info() */
443         li      r0,_TIF_NEED_RESCHED    /* bits to check */
444         ld      r3,_MSR(r1)
445         ld      r4,TI_FLAGS(r9)
446         /* Move MSR_PR bit in r3 to _TIF_SIGPENDING position in r0 */
447         rlwimi  r0,r3,32+TIF_SIGPENDING-MSR_PR_LG,_TIF_SIGPENDING
448         and.    r0,r4,r0        /* check NEED_RESCHED and maybe SIGPENDING */
449         bne     do_work
450
451 #else /* !CONFIG_PREEMPT */
452         ld      r3,_MSR(r1)     /* Returning to user mode? */
453         andi.   r3,r3,MSR_PR
454         beq     restore         /* if not, just restore regs and return */
455
456         /* Check current_thread_info()->flags */
457         clrrdi  r9,r1,THREAD_SHIFT
458         ld      r4,TI_FLAGS(r9)
459         andi.   r0,r4,_TIF_USER_WORK_MASK
460         bne     do_work
461 #endif
462
463 restore:
464 #ifdef CONFIG_PPC_ISERIES
465         ld      r5,SOFTE(r1)
466         cmpdi   0,r5,0
467         beq     4f
468         /* Check for pending interrupts (iSeries) */
469         ld      r3,PACALPPACAPTR(r13)
470         ld      r3,LPPACAANYINT(r3)
471         cmpdi   r3,0
472         beq+    4f                      /* skip do_IRQ if no interrupts */
473
474         li      r3,0
475         stb     r3,PACAPROCENABLED(r13) /* ensure we are soft-disabled */
476         ori     r10,r10,MSR_EE
477         mtmsrd  r10                     /* hard-enable again */
478         addi    r3,r1,STACK_FRAME_OVERHEAD
479         bl      .do_IRQ
480         b       .ret_from_except_lite           /* loop back and handle more */
481
482 4:      stb     r5,PACAPROCENABLED(r13)
483 #endif
484
485         ld      r3,_MSR(r1)
486         andi.   r0,r3,MSR_RI
487         beq-    unrecov_restore
488
489         andi.   r0,r3,MSR_PR
490
491         /*
492          * r13 is our per cpu area, only restore it if we are returning to
493          * userspace
494          */
495         beq     1f
496         ACCOUNT_CPU_USER_EXIT(r3, r4)
497         REST_GPR(13, r1)
498 1:
499         ld      r3,_CTR(r1)
500         ld      r0,_LINK(r1)
501         mtctr   r3
502         mtlr    r0
503         ld      r3,_XER(r1)
504         mtspr   SPRN_XER,r3
505
506         REST_8GPRS(5, r1)
507
508         stdcx.  r0,0,r1         /* to clear the reservation */
509
510         mfmsr   r0
511         li      r2, MSR_RI
512         andc    r0,r0,r2
513         mtmsrd  r0,1
514
515         ld      r0,_MSR(r1)
516         mtspr   SPRN_SRR1,r0
517
518         ld      r2,_CCR(r1)
519         mtcrf   0xFF,r2
520         ld      r2,_NIP(r1)
521         mtspr   SPRN_SRR0,r2
522
523         ld      r0,GPR0(r1)
524         ld      r2,GPR2(r1)
525         ld      r3,GPR3(r1)
526         ld      r4,GPR4(r1)
527         ld      r1,GPR1(r1)
528
529         rfid
530         b       .       /* prevent speculative execution */
531
532 /* Note: this must change if we start using the TIF_NOTIFY_RESUME bit */
533 do_work:
534 #ifdef CONFIG_PREEMPT
535         andi.   r0,r3,MSR_PR    /* Returning to user mode? */
536         bne     user_work
537         /* Check that preempt_count() == 0 and interrupts are enabled */
538         lwz     r8,TI_PREEMPT(r9)
539         cmpwi   cr1,r8,0
540 #ifdef CONFIG_PPC_ISERIES
541         ld      r0,SOFTE(r1)
542         cmpdi   r0,0
543 #else
544         andi.   r0,r3,MSR_EE
545 #endif
546         crandc  eq,cr1*4+eq,eq
547         bne     restore
548         /* here we are preempting the current task */
549 1:
550 #ifdef CONFIG_PPC_ISERIES
551         li      r0,1
552         stb     r0,PACAPROCENABLED(r13)
553 #endif
554         ori     r10,r10,MSR_EE
555         mtmsrd  r10,1           /* reenable interrupts */
556         bl      .preempt_schedule
557         mfmsr   r10
558         clrrdi  r9,r1,THREAD_SHIFT
559         rldicl  r10,r10,48,1    /* disable interrupts again */
560         rotldi  r10,r10,16
561         mtmsrd  r10,1
562         ld      r4,TI_FLAGS(r9)
563         andi.   r0,r4,_TIF_NEED_RESCHED
564         bne     1b
565         b       restore
566
567 user_work:
568 #endif
569         /* Enable interrupts */
570         ori     r10,r10,MSR_EE
571         mtmsrd  r10,1
572
573         andi.   r0,r4,_TIF_NEED_RESCHED
574         beq     1f
575         bl      .schedule
576         b       .ret_from_except_lite
577
578 1:      bl      .save_nvgprs
579         li      r3,0
580         addi    r4,r1,STACK_FRAME_OVERHEAD
581         bl      .do_signal
582         b       .ret_from_except
583
584 unrecov_restore:
585         addi    r3,r1,STACK_FRAME_OVERHEAD
586         bl      .unrecoverable_exception
587         b       unrecov_restore
588
589 #ifdef CONFIG_PPC_RTAS
590 /*
591  * On CHRP, the Run-Time Abstraction Services (RTAS) have to be
592  * called with the MMU off.
593  *
594  * In addition, we need to be in 32b mode, at least for now.
595  * 
596  * Note: r3 is an input parameter to rtas, so don't trash it...
597  */
598 _GLOBAL(enter_rtas)
599         mflr    r0
600         std     r0,16(r1)
601         stdu    r1,-RTAS_FRAME_SIZE(r1) /* Save SP and create stack space. */
602
603         /* Because RTAS is running in 32b mode, it clobbers the high order half
604          * of all registers that it saves.  We therefore save those registers
605          * RTAS might touch to the stack.  (r0, r3-r13 are caller saved)
606          */
607         SAVE_GPR(2, r1)                 /* Save the TOC */
608         SAVE_GPR(13, r1)                /* Save paca */
609         SAVE_8GPRS(14, r1)              /* Save the non-volatiles */
610         SAVE_10GPRS(22, r1)             /* ditto */
611
612         mfcr    r4
613         std     r4,_CCR(r1)
614         mfctr   r5
615         std     r5,_CTR(r1)
616         mfspr   r6,SPRN_XER
617         std     r6,_XER(r1)
618         mfdar   r7
619         std     r7,_DAR(r1)
620         mfdsisr r8
621         std     r8,_DSISR(r1)
622         mfsrr0  r9
623         std     r9,_SRR0(r1)
624         mfsrr1  r10
625         std     r10,_SRR1(r1)
626
627         /* Temporary workaround to clear CR until RTAS can be modified to
628          * ignore all bits.
629          */
630         li      r0,0
631         mtcr    r0
632
633         /* There is no way it is acceptable to get here with interrupts enabled,
634          * check it with the asm equivalent of WARN_ON
635          */
636         mfmsr   r6
637         andi.   r0,r6,MSR_EE
638 1:      tdnei   r0,0
639 .section __bug_table,"a"
640         .llong  1b,__LINE__ + 0x1000000, 1f, 2f
641 .previous
642 .section .rodata,"a"
643 1:      .asciz  __FILE__
644 2:      .asciz "enter_rtas"
645 .previous
646         
647         /* Unfortunately, the stack pointer and the MSR are also clobbered,
648          * so they are saved in the PACA which allows us to restore
649          * our original state after RTAS returns.
650          */
651         std     r1,PACAR1(r13)
652         std     r6,PACASAVEDMSR(r13)
653
654         /* Setup our real return addr */        
655         LOAD_REG_ADDR(r4,.rtas_return_loc)
656         clrldi  r4,r4,2                 /* convert to realmode address */
657         mtlr    r4
658
659         li      r0,0
660         ori     r0,r0,MSR_EE|MSR_SE|MSR_BE|MSR_RI
661         andc    r0,r6,r0
662         
663         li      r9,1
664         rldicr  r9,r9,MSR_SF_LG,(63-MSR_SF_LG)
665         ori     r9,r9,MSR_IR|MSR_DR|MSR_FE0|MSR_FE1|MSR_FP
666         andc    r6,r0,r9
667         ori     r6,r6,MSR_RI
668         sync                            /* disable interrupts so SRR0/1 */
669         mtmsrd  r0                      /* don't get trashed */
670
671         LOAD_REG_ADDR(r4, rtas)
672         ld      r5,RTASENTRY(r4)        /* get the rtas->entry value */
673         ld      r4,RTASBASE(r4)         /* get the rtas->base value */
674         
675         mtspr   SPRN_SRR0,r5
676         mtspr   SPRN_SRR1,r6
677         rfid
678         b       .       /* prevent speculative execution */
679
680 _STATIC(rtas_return_loc)
681         /* relocation is off at this point */
682         mfspr   r4,SPRN_SPRG3           /* Get PACA */
683         clrldi  r4,r4,2                 /* convert to realmode address */
684
685         mfmsr   r6
686         li      r0,MSR_RI
687         andc    r6,r6,r0
688         sync    
689         mtmsrd  r6
690         
691         ld      r1,PACAR1(r4)           /* Restore our SP */
692         LOAD_REG_IMMEDIATE(r3,.rtas_restore_regs)
693         ld      r4,PACASAVEDMSR(r4)     /* Restore our MSR */
694
695         mtspr   SPRN_SRR0,r3
696         mtspr   SPRN_SRR1,r4
697         rfid
698         b       .       /* prevent speculative execution */
699
700 _STATIC(rtas_restore_regs)
701         /* relocation is on at this point */
702         REST_GPR(2, r1)                 /* Restore the TOC */
703         REST_GPR(13, r1)                /* Restore paca */
704         REST_8GPRS(14, r1)              /* Restore the non-volatiles */
705         REST_10GPRS(22, r1)             /* ditto */
706
707         mfspr   r13,SPRN_SPRG3
708
709         ld      r4,_CCR(r1)
710         mtcr    r4
711         ld      r5,_CTR(r1)
712         mtctr   r5
713         ld      r6,_XER(r1)
714         mtspr   SPRN_XER,r6
715         ld      r7,_DAR(r1)
716         mtdar   r7
717         ld      r8,_DSISR(r1)
718         mtdsisr r8
719         ld      r9,_SRR0(r1)
720         mtsrr0  r9
721         ld      r10,_SRR1(r1)
722         mtsrr1  r10
723
724         addi    r1,r1,RTAS_FRAME_SIZE   /* Unstack our frame */
725         ld      r0,16(r1)               /* get return address */
726
727         mtlr    r0
728         blr                             /* return to caller */
729
730 #endif /* CONFIG_PPC_RTAS */
731
732 #ifdef CONFIG_PPC_MULTIPLATFORM
733
734 _GLOBAL(enter_prom)
735         mflr    r0
736         std     r0,16(r1)
737         stdu    r1,-PROM_FRAME_SIZE(r1) /* Save SP and create stack space */
738
739         /* Because PROM is running in 32b mode, it clobbers the high order half
740          * of all registers that it saves.  We therefore save those registers
741          * PROM might touch to the stack.  (r0, r3-r13 are caller saved)
742          */
743         SAVE_8GPRS(2, r1)
744         SAVE_GPR(13, r1)
745         SAVE_8GPRS(14, r1)
746         SAVE_10GPRS(22, r1)
747         mfcr    r4
748         std     r4,_CCR(r1)
749         mfctr   r5
750         std     r5,_CTR(r1)
751         mfspr   r6,SPRN_XER
752         std     r6,_XER(r1)
753         mfdar   r7
754         std     r7,_DAR(r1)
755         mfdsisr r8
756         std     r8,_DSISR(r1)
757         mfsrr0  r9
758         std     r9,_SRR0(r1)
759         mfsrr1  r10
760         std     r10,_SRR1(r1)
761         mfmsr   r11
762         std     r11,_MSR(r1)
763
764         /* Get the PROM entrypoint */
765         ld      r0,GPR4(r1)
766         mtlr    r0
767
768         /* Switch MSR to 32 bits mode
769          */
770         mfmsr   r11
771         li      r12,1
772         rldicr  r12,r12,MSR_SF_LG,(63-MSR_SF_LG)
773         andc    r11,r11,r12
774         li      r12,1
775         rldicr  r12,r12,MSR_ISF_LG,(63-MSR_ISF_LG)
776         andc    r11,r11,r12
777         mtmsrd  r11
778         isync
779
780         /* Restore arguments & enter PROM here... */
781         ld      r3,GPR3(r1)
782         blrl
783
784         /* Just make sure that r1 top 32 bits didn't get
785          * corrupt by OF
786          */
787         rldicl  r1,r1,0,32
788
789         /* Restore the MSR (back to 64 bits) */
790         ld      r0,_MSR(r1)
791         mtmsrd  r0
792         isync
793
794         /* Restore other registers */
795         REST_GPR(2, r1)
796         REST_GPR(13, r1)
797         REST_8GPRS(14, r1)
798         REST_10GPRS(22, r1)
799         ld      r4,_CCR(r1)
800         mtcr    r4
801         ld      r5,_CTR(r1)
802         mtctr   r5
803         ld      r6,_XER(r1)
804         mtspr   SPRN_XER,r6
805         ld      r7,_DAR(r1)
806         mtdar   r7
807         ld      r8,_DSISR(r1)
808         mtdsisr r8
809         ld      r9,_SRR0(r1)
810         mtsrr0  r9
811         ld      r10,_SRR1(r1)
812         mtsrr1  r10
813         
814         addi    r1,r1,PROM_FRAME_SIZE
815         ld      r0,16(r1)
816         mtlr    r0
817         blr
818         
819 #endif  /* CONFIG_PPC_MULTIPLATFORM */