Merge with Linux 2.5.74.
[linux-2.6/linux-mips.git] / arch / ppc64 / kernel / entry.S
blobb538e146bc0912614ddad6ecbbbe719538bfcd13
1 /*
2  *  arch/ppc64/kernel/entry.S
3  *
4  *  PowerPC version 
5  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
6  *  Rewritten by Cort Dougan (cort@cs.nmt.edu) for PReP
7  *    Copyright (C) 1996 Cort Dougan <cort@cs.nmt.edu>
8  *  Adapted for Power Macintosh by Paul Mackerras.
9  *  Low-level exception handlers and MMU support
10  *  rewritten by Paul Mackerras.
11  *    Copyright (C) 1996 Paul Mackerras.
12  *  MPC8xx modifications Copyright (C) 1997 Dan Malek (dmalek@jlc.net).
13  *
14  *  This file contains the system call entry code, context switch
15  *  code, and exception/interrupt return code for PowerPC.
16  *
17  *  This program is free software; you can redistribute it and/or
18  *  modify it under the terms of the GNU General Public License
19  *  as published by the Free Software Foundation; either version
20  *  2 of the License, or (at your option) any later version.
21  */
23 #include <linux/config.h>
24 #include <linux/errno.h>
25 #include <asm/unistd.h>
26 #include <asm/processor.h>
27 #include <asm/page.h>
28 #include <asm/mmu.h>
29 #include <asm/thread_info.h>
30 #include <asm/ppc_asm.h>
31 #include <asm/offsets.h>
33 #ifdef CONFIG_PPC_ISERIES
34 #define DO_SOFT_DISABLE
35 #endif
37 #undef SHOW_SYSCALLS
38 #undef SHOW_SYSCALLS_TASK
40 #ifdef SHOW_SYSCALLS_TASK
41         .data
42 show_syscalls_task:
43         .long   -1
44 #endif
46         .section        ".toc","aw"
47 .SYS_CALL_TABLE:
48         .tc .sys_call_table[TC],.sys_call_table
50 .SYS_CALL_TABLE32:
51         .tc .sys_call_table32[TC],.sys_call_table32
53         .section        ".text"
54         .align 3
57  * Handle a system call.
58  */
59 _GLOBAL(DoSyscall)
60         std     r3,ORIG_GPR3(r1)
61         ld      r11,_CCR(r1)    /* Clear SO bit in CR */
62         lis     r10,0x1000
63         andc    r11,r11,r10
64         std     r11,_CCR(r1)
65 #ifdef SHOW_SYSCALLS
66 #ifdef SHOW_SYSCALLS_TASK
67         LOADBASE(r31,show_syscalls_task)
68         ld      r31,show_syscalls_task@l(r31)
69         ld      r10,PACACURRENT(r13)
70         cmp     0,r10,r31
71         bne     1f
72 #endif
73         LOADADDR(r3,7f)
74         ld      r4,GPR0(r1)
75         ld      r5,GPR3(r1)
76         ld      r6,GPR4(r1)
77         ld      r7,GPR5(r1)
78         ld      r8,GPR6(r1)
79         ld      r9,GPR7(r1)
80         bl      .printk
81         LOADADDR(r3,77f)
82         ld      r4,GPR8(r1)
83         ld      r5,GPR9(r1)
84         ld      r6, PACACURRENT(r13)
85         bl      .printk
86         ld      r0,GPR0(r1)
87         ld      r3,GPR3(r1)
88         ld      r4,GPR4(r1)
89         ld      r5,GPR5(r1)
90         ld      r6,GPR6(r1)
91         ld      r7,GPR7(r1)
92         ld      r8,GPR8(r1)
94 #endif /* SHOW_SYSCALLS */
95         clrrdi  r10,r1,THREAD_SHIFT
96         ld      r10,TI_FLAGS(r10)
97         andi.   r11,r10,_TIF_SYSCALL_TRACE
98         bne-    50f
99         cmpli   0,r0,NR_syscalls
100         bge-    66f
102  * Need to vector to 32 Bit or default sys_call_table here,
103  * based on caller's run-mode / personality.
104  */
105         andi.   r11,r10,_TIF_32BIT
106         beq-    15f
107         ld      r10,.SYS_CALL_TABLE32@toc(2)
109  * We now zero extend all six arguments (r3 - r8), the compatibility
110  * layer assumes this.
111  */
112         clrldi  r3,r3,32
113         clrldi  r4,r4,32
114         clrldi  r5,r5,32
115         clrldi  r6,r6,32
116         clrldi  r7,r7,32
117         clrldi  r8,r8,32
118         b       17f
120         ld      r10,.SYS_CALL_TABLE@toc(2)
121 17:     slwi    r0,r0,3
122         ldx     r10,r10,r0      /* Fetch system call handler [ptr] */
123         mtlr    r10
124         addi    r9,r1,STACK_FRAME_OVERHEAD
125         blrl                    /* Call handler */
126 _GLOBAL(ret_from_syscall_1)
127         std     r3,RESULT(r1)   /* Save result */
128 #ifdef SHOW_SYSCALLS
129 #ifdef SHOW_SYSCALLS_TASK
130         ld      r10, PACACURRENT(13)
131         cmp     0,r10,r31
132         bne     91f
133 #endif
134         mr      r4,r3
135         LOADADDR(r3,79f)
136         bl      .printk
137         ld      r3,RESULT(r1)
139 #endif
140         li      r10,-_LAST_ERRNO
141         cmpl    0,r3,r10
142         blt     30f
143         neg     r3,r3
144 22:     ld      r10,_CCR(r1)    /* Set SO bit in CR */
145         oris    r10,r10,0x1000
146         std     r10,_CCR(r1)
147 30:     std     r3,GPR3(r1)     /* Update return value */
148         b       .ret_from_except
149 66:     li      r3,ENOSYS
150         b       22b
151         
152 /* Traced system call support */
153 50:     bl      .do_syscall_trace
154         ld      r0,GPR0(r1)     /* Restore original registers */
155         ld      r3,GPR3(r1)
156         ld      r4,GPR4(r1)
157         ld      r5,GPR5(r1)
158         ld      r6,GPR6(r1)
159         ld      r7,GPR7(r1)
160         ld      r8,GPR8(r1)
161         /* XXX check this - Anton */
162         ld      r9,GPR9(r1)
163         cmpli   0,r0,NR_syscalls
164         bge-    66f
166  * Need to vector to 32 Bit or default sys_call_table here,
167  * based on caller's run-mode / personality.
168  */
169         clrrdi  r10,r1,THREAD_SHIFT
170         ld      r10,TI_FLAGS(r10)
171         andi.   r11,r10,_TIF_32BIT
172         beq-    55f
173         ld      r10,.SYS_CALL_TABLE32@toc(2)
175  * We now zero extend all six arguments (r3 - r8), the compatibility
176  * layer assumes this.
177  */
178         clrldi  r3,r3,32
179         clrldi  r4,r4,32
180         clrldi  r5,r5,32
181         clrldi  r6,r6,32
182         clrldi  r7,r7,32
183         clrldi  r8,r8,32
184         b       57f
186         ld      r10,.SYS_CALL_TABLE@toc(2)
188         slwi    r0,r0,3
189         ldx     r10,r10,r0      /* Fetch system call handler [ptr] */
190         mtlr    r10
191         addi    r9,r1,STACK_FRAME_OVERHEAD
192         blrl                    /* Call handler */
193 _GLOBAL(ret_from_syscall_2)
194         std     r3,RESULT(r1)   /* Save result */       
195         li      r10,-_LAST_ERRNO
196         cmpl    0,r3,r10
197         blt     60f
198         neg     r3,r3
199 57:     ld      r10,_CCR(r1)    /* Set SO bit in CR */
200         oris    r10,r10,0x1000
201         std     r10,_CCR(r1)
202 60:     std     r3,GPR3(r1)     /* Update return value */
203         bl      .do_syscall_trace
204         b       .ret_from_except
205 66:     li      r3,ENOSYS
206         b       57b
207 #ifdef SHOW_SYSCALLS
208 7:      .string "syscall %d(%x, %x, %x, %x, %x, "
209 77:     .string "%x, %x), current=%p\n"
210 79:     .string " -> %x\n"
211         .align  2,0
212 #endif
214 _GLOBAL(ppc32_sigreturn)
215         bl      .sys32_sigreturn
216         b       80f
218 _GLOBAL(ppc32_rt_sigreturn)
219         bl      .sys32_rt_sigreturn
220         b       80f
222 _GLOBAL(ppc64_rt_sigreturn)
223         bl      .sys_rt_sigreturn
225 80:     clrrdi  r4,r1,THREAD_SHIFT
226         ld      r4,TI_FLAGS(r4)
227         andi.   r4,r4,_TIF_SYSCALL_TRACE
228         bne-    81f
229         cmpi    0,r3,0
230         bge     .ret_from_except
231         b       .ret_from_syscall_1
232 81:     cmpi    0,r3,0
233         blt     .ret_from_syscall_2
234         bl      .do_syscall_trace
235         b       .ret_from_except
238  * This routine switches between two different tasks.  The process
239  * state of one is saved on its kernel stack.  Then the state
240  * of the other is restored from its kernel stack.  The memory
241  * management hardware is updated to the second process's state.
242  * Finally, we can return to the second process, via ret_from_except.
243  * On entry, r3 points to the THREAD for the current task, r4
244  * points to the THREAD for the new task.
246  * Note: there are two ways to get to the "going out" portion
247  * of this code; either by coming in via the entry (_switch)
248  * or via "fork" which must set up an environment equivalent
249  * to the "_switch" path.  If you change this you'll have to change
250  * the fork code also.
252  * The code which creates the new task context is in 'copy_thread'
253  * in arch/ppc64/kernel/process.c
254  */
255 _GLOBAL(_switch)
256         mflr    r0
257         std     r0,16(r1)
258         stdu    r1,-SWITCH_FRAME_SIZE(r1)
259         /* r3-r13 are caller saved -- Cort */
260         SAVE_8GPRS(14, r1)
261         SAVE_10GPRS(22, r1)
262         mflr    r20             /* Return to switch caller */
263         mfmsr   r22
264         andi.   r21, r22, MSR_FP
265         beq+    1f
266         li      r6,MSR_FP       /* Disable floating-point */
267         andc    r22,r22,r6
268         mtmsrd  r22
269         isync
270 1:      std     r20,_NIP(r1)
271         mfcr    r23
272         std     r23,_CCR(r1)
273         std     r1,KSP(r3)      /* Set old stack pointer */
275         addi    r6,r4,-THREAD   /* Convert THREAD to 'current' */
276         std     r6,PACACURRENT(r13)     /* Set new 'current' */
278 #ifdef CONFIG_PPC_ISERIES
279 #error fixme
280         ld      r7,TI_FLAGS(r4) /* Get run light flag */
281         mfspr   r9,CTRLF
282         srdi    r7,r7,1         /* Align to run light bit in CTRL reg */
283         insrdi  r9,r7,1,63      /* Insert run light into CTRL */
284         mtspr   CTRLT,r9
285 #endif
287         ld      r1,KSP(r4)      /* Load new stack pointer */
288         ld      r6,_CCR(r1)
289         mtcrf   0xFF,r6
290         /* r3-r13 are destroyed -- Cort */
291         REST_8GPRS(14, r1)
292         REST_10GPRS(22, r1)
294         /* convert old thread to its task_struct for return value */
295         addi    r3,r3,-THREAD
296         ld      r7,_NIP(r1)     /* Return to _switch caller in new task */
297         mtlr    r7
298         addi    r1,r1,SWITCH_FRAME_SIZE
299         blr
301 _GLOBAL(ret_from_fork)
302         bl      .schedule_tail
303         clrrdi  r4,r1,THREAD_SHIFT
304         ld      r4,TI_FLAGS(r4)
305         andi.   r4,r4,_TIF_SYSCALL_TRACE
306         beq+    .ret_from_except
307         bl      .do_syscall_trace
308         b       .ret_from_except
310 _GLOBAL(ret_from_except)
311 #ifdef CONFIG_PPC_ISERIES
312         ld      r5,SOFTE(r1)
313         cmpdi   0,r5,0
314         beq     4f
315 irq_recheck:
316         /* Check for pending interrupts (iSeries) */
317         CHECKANYINT(r3,r4)
318         beq+    4f      /* skip do_IRQ if no interrupts */
320 #warning FIX ISERIES
321         mfspr   r5,SPRG3
322         li      r3,0
323         stb     r3,PACAPROCENABLED(r5)  /* ensure we are disabled */
324         addi    r3,r1,STACK_FRAME_OVERHEAD
325         bl      .do_IRQ
326         b       irq_recheck     /* loop back and handle more */
328 #endif
329         /*
330          * Disable interrupts so that current_thread_info()->flags
331          * can't change between when we test it and when we return
332          * from the interrupt.
333          */
334 recheck:
335         mfmsr   r10             /* Get current interrupt state */
336         li      r4,0
337         ori     r4,r4,MSR_EE
338         andc    r10,r10,r4      /* clear MSR_EE */
339         mtmsrd  r10,1           /* Update machine state */
341 #ifdef CONFIG_PPC_ISERIES
342 #error fix iSeries soft disable
343 #endif
345         ld      r3,_MSR(r1)     /* Returning to user mode? */
346         andi.   r3,r3,MSR_PR
347         beq     restore         /* if not, just restore regs and return */
349         /* Check current_thread_info()->flags */
350         clrrdi  r3,r1,THREAD_SHIFT
351         ld      r3,TI_FLAGS(r3)
352         andi.   r0,r3,_TIF_USER_WORK_MASK
353         bne     do_work
355         addi    r0,r1,INT_FRAME_SIZE    /* size of frame */
356         ld      r4,PACACURRENT(r13)
357         std     r0,THREAD+KSP(r4)       /* save kernel stack pointer */
358         std     r1,PACAKSAVE(r13)       /* save exception stack pointer */
360         /*
361          * r13 is our per cpu area, only restore it if we are returning to
362          * userspace
363          */
364         REST_GPR(13,r1)
366 restore:
367         ld      r3,_CTR(r1)
368         ld      r0,_LINK(r1)
369         mtctr   r3
370         mtlr    r0
371         ld      r3,_XER(r1)
372         mtspr   XER,r3
374         REST_8GPRS(5, r1)
375         REST_10GPRS(14, r1)
376         REST_8GPRS(24, r1)
378         stdcx.  r0,0,r1         /* to clear the reservation */
380 #ifdef DO_SOFT_DISABLE
381         /* XXX do this in do_work, r13 isnt valid here */
382         ld      r0,SOFTE(r1)
383         stb     r0,PACAPROCENABLED(r13)
384 #endif
386         mfmsr   r0
387         li      r2, MSR_RI
388         andc    r0,r0,r2
389         mtmsrd  r0,1
391         ld      r0,_MSR(r1)
392         mtspr   SRR1,r0
394         ld      r2,_CCR(r1)
395         mtcrf   0xFF,r2
396         ld      r2,_NIP(r1)
397         mtspr   SRR0,r2
399         ld      r0,GPR0(r1)
400         ld      r2,GPR2(r1)
401         ld      r3,GPR3(r1)
402         ld      r4,GPR4(r1)
403         ld      r1,GPR1(r1)
405         rfid
407 /* Note: this must change if we start using the  TIF_NOTIFY_RESUME bit */
408 do_work:
409         /* Enable interrupts */
410         ori     r10,r10,MSR_EE
411         mtmsrd  r10,1
413         andi.   r0,r3,_TIF_NEED_RESCHED
414         beq     1f
415         bl      .schedule
416         b       recheck
418 1:      andi.   r0,r3,_TIF_SIGPENDING
419         beq     recheck
420         li      r3,0
421         addi    r4,r1,STACK_FRAME_OVERHEAD
422         bl      .do_signal
423         b       recheck
426  * On CHRP, the Run-Time Abstraction Services (RTAS) have to be
427  * called with the MMU off.
429  * In addition, we need to be in 32b mode, at least for now.
430  * 
431  * Note: r3 is an input parameter to rtas, so don't trash it...
432  */
433 _GLOBAL(enter_rtas)
434         mflr    r0
435         std     r0,16(r1)
436         stdu    r1,-RTAS_FRAME_SIZE(r1) /* Save SP and create stack space. */
438         /* Because RTAS is running in 32b mode, it clobbers the high order half
439          * of all registers that it saves.  We therefore save those registers
440          * RTAS might touch to the stack.  (r0, r3-r13 are caller saved)
441          */
442         SAVE_GPR(2, r1)                 /* Save the TOC */
443         SAVE_GPR(13, r1)                /* Save paca */
444         SAVE_8GPRS(14, r1)              /* Save the non-volatiles */
445         SAVE_10GPRS(22, r1)             /* ditto */
447         mfcr    r4
448         std     r4,_CCR(r1)
449         mfctr   r5
450         std     r5,_CTR(r1)
451         mfspr   r6,XER
452         std     r6,_XER(r1)
453         mfdar   r7
454         std     r7,_DAR(r1)
455         mfdsisr r8
456         std     r8,_DSISR(r1)
457         mfsrr0  r9
458         std     r9,_SRR0(r1)
459         mfsrr1  r10
460         std     r10,_SRR1(r1)
462         /* Unfortunately, the stack pointer and the MSR are also clobbered,
463          * so they are saved in the PACA which allows us to restore
464          * our original state after RTAS returns.
465          */
466         std     r1,PACAR1(r13)
467         mfmsr   r6
468         std     r6,PACASAVEDMSR(r13)
470         /* Setup our real return addr */        
471         SET_REG_TO_LABEL(r4,.rtas_return_loc)
472         SET_REG_TO_CONST(r9,KERNELBASE)
473         sub     r4,r4,r9
474         mtlr    r4
476         li      r0,0
477         ori     r0,r0,MSR_EE|MSR_SE|MSR_BE|MSR_RI
478         andc    r0,r6,r0
479         
480         li      r9,1
481         rldicr  r9,r9,MSR_SF_LG,(63-MSR_SF_LG)
482         ori     r9,r9,MSR_IR|MSR_DR|MSR_FE0|MSR_FE1|MSR_FP
483         andc    r6,r0,r9
484         ori     r6,r6,MSR_RI
485         sync                            /* disable interrupts so SRR0/1 */
486         mtmsrd  r0                      /* don't get trashed */
488         SET_REG_TO_LABEL(r4,rtas)
489         ld      r5,RTASENTRY(r4)        /* get the rtas->entry value */
490         ld      r4,RTASBASE(r4)         /* get the rtas->base value */
491         
492         mtspr   SRR0,r5
493         mtspr   SRR1,r6
494         rfid
496 _STATIC(rtas_return_loc)
497         /* relocation is off at this point */
498         mfspr   r4,SPRG3                /* Get PACA */
499         SET_REG_TO_CONST(r5, KERNELBASE)
500         sub     r4,r4,r5                /* RELOC the PACA base pointer */
501         
502         ld      r1,PACAR1(r4)           /* Restore our SP */
503         LOADADDR(r3,.rtas_restore_regs)
504         ld      r4,PACASAVEDMSR(r4)     /* Restore our MSR */
506         mtspr   SRR0,r3
507         mtspr   SRR1,r4
508         rfid
510 _STATIC(rtas_restore_regs)
511         /* relocation is on at this point */
512         REST_GPR(2, r1)                 /* Restore the TOC */
513         REST_GPR(13, r1)                /* Restore paca */
514         REST_8GPRS(14, r1)              /* Restore the non-volatiles */
515         REST_10GPRS(22, r1)             /* ditto */
517         mfspr   r13,SPRG3
519         ld      r4,_CCR(r1)
520         mtcr    r4
521         ld      r5,_CTR(r1)
522         mtctr   r5
523         ld      r6,_XER(r1)
524         mtspr   XER,r6
525         ld      r7,_DAR(r1)
526         mtdar   r7
527         ld      r8,_DSISR(r1)
528         mtdsisr r8
529         ld      r9,_SRR0(r1)
530         mtsrr0  r9
531         ld      r10,_SRR1(r1)
532         mtsrr1  r10
534         addi    r1,r1,RTAS_FRAME_SIZE   /* Unstack our frame */
535         ld      r0,16(r1)               /* get return address */
537         mtlr    r0
538         blr                             /* return to caller */
540 _GLOBAL(enter_prom)
541         mflr    r0
542         std     r0,16(r1)
543         stdu    r1,-PROM_FRAME_SIZE(r1) /* Save SP and create stack space */
545         /* Because PROM is running in 32b mode, it clobbers the high order half
546          * of all registers that it saves.  We therefore save those registers
547          * PROM might touch to the stack.  (r0, r3-r13 are caller saved)
548          */
549         SAVE_8GPRS(2, r1)               /* Save the TOC & incoming param(s) */
550         SAVE_GPR(13, r1)                /* Save paca */
551         SAVE_8GPRS(14, r1)              /* Save the non-volatiles */
552         SAVE_10GPRS(22, r1)             /* ditto */
554         mfcr    r4
555         std     r4,_CCR(r1)
556         mfctr   r5
557         std     r5,_CTR(r1)
558         mfspr   r6,XER
559         std     r6,_XER(r1)
560         mfdar   r7
561         std     r7,_DAR(r1)
562         mfdsisr r8
563         std     r8,_DSISR(r1)
564         mfsrr0  r9
565         std     r9,_SRR0(r1)
566         mfsrr1  r10
567         std     r10,_SRR1(r1)
568         mfmsr   r11
569         std     r11,_MSR(r1)
571         /* Unfortunatly, the stack pointer is also clobbered, so it is saved
572          * in the SPRG2 which allows us to restore our original state after
573          * PROM returns.
574          */
575         mtspr   SPRG2,r1
577         /* put a relocation offset into r3 */
578         bl      .reloc_offset
579         LOADADDR(r12,prom)
580         sub     r12,r12,r3
581         ld      r12,PROMENTRY(r12)      /* get the prom->entry value */
582         mtlr    r12
584         mfmsr   r11                     /* grab the current MSR */
585         li      r12,1
586         rldicr  r12,r12,MSR_SF_LG,(63-MSR_SF_LG)
587         andc    r11,r11,r12
588         li      r12,1
589         rldicr  r12,r12,MSR_ISF_LG,(63-MSR_ISF_LG)
590         andc    r11,r11,r12
591         mtmsrd  r11
592         isync
594         REST_8GPRS(2, r1)               /* Restore the TOC & param(s) */
595         REST_GPR(13, r1)                /* Restore paca */
596         REST_8GPRS(14, r1)              /* Restore the non-volatiles */
597         REST_10GPRS(22, r1)             /* ditto */
598         blrl                            /* Entering PROM here... */
600         mfspr   r1,SPRG2                /* Restore the stack pointer */
601         ld      r6,_MSR(r1)             /* Restore the MSR */
602         mtmsrd  r6
603         isync
605         REST_GPR(2, r1)                 /* Restore the TOC */
606         REST_GPR(13, r1)                /* Restore paca */
607         REST_8GPRS(14, r1)              /* Restore the non-volatiles */
608         REST_10GPRS(22, r1)             /* ditto */
610         ld      r4,_CCR(r1)
611         mtcr    r4
612         ld      r5,_CTR(r1)
613         mtctr   r5
614         ld      r6,_XER(r1)
615         mtspr   XER,r6
616         ld      r7,_DAR(r1)
617         mtdar   r7
618         ld      r8,_DSISR(r1)
619         mtdsisr r8
620         ld      r9,_SRR0(r1)
621         mtsrr0  r9
622         ld      r10,_SRR1(r1)
623         mtsrr1  r10
624         addi    r1,r1,PROM_FRAME_SIZE
625         ld      r0,16(r1)               /* get return address */
627         mtlr    r0
628         blr                             /* return to caller */