3 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
4 * Rewritten by Cort Dougan (cort@fsmlabs.com) for PReP
5 * Copyright (C) 1996 Cort Dougan <cort@fsmlabs.com>
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).
12 * This file contains the system call entry code, context switch
13 * code, and exception/interrupt return code for PowerPC.
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.
22 #include <linux/config.h>
23 #include <linux/errno.h>
24 #include <linux/sys.h>
25 #include <linux/threads.h>
29 #include <asm/cputable.h>
30 #include <asm/thread_info.h>
31 #include <asm/ppc_asm.h>
32 #include <asm/asm-offsets.h>
33 #include <asm/unistd.h>
36 #undef SHOW_SYSCALLS_TASK
39 * MSR_KERNEL is > 0x10000 on 4xx/Book-E since it include MSR_CE.
41 #if MSR_KERNEL >= 0x10000
42 #define LOAD_MSR_KERNEL(r, x) lis r,(x)@h; ori r,r,(x)@l
44 #define LOAD_MSR_KERNEL(r, x) li r,(x)
48 #include "head_booke.h"
49 #define TRANSFER_TO_HANDLER_EXC_LEVEL(exc_level) \
50 mtspr exc_level##_SPRG,r8; \
51 BOOKE_LOAD_EXC_LEVEL_STACK(exc_level); \
52 lwz r0,GPR10-INT_FRAME_SIZE(r8); \
54 lwz r0,GPR11-INT_FRAME_SIZE(r8); \
56 mfspr r8,exc_level##_SPRG
58 .globl mcheck_transfer_to_handler
59 mcheck_transfer_to_handler:
60 TRANSFER_TO_HANDLER_EXC_LEVEL(MCHECK)
61 b transfer_to_handler_full
63 .globl debug_transfer_to_handler
64 debug_transfer_to_handler:
65 TRANSFER_TO_HANDLER_EXC_LEVEL(DEBUG)
66 b transfer_to_handler_full
68 .globl crit_transfer_to_handler
69 crit_transfer_to_handler:
70 TRANSFER_TO_HANDLER_EXC_LEVEL(CRIT)
75 .globl crit_transfer_to_handler
76 crit_transfer_to_handler:
85 * This code finishes saving the registers to the exception frame
86 * and jumps to the appropriate handler for the exception, turning
87 * on address translation.
88 * Note that we rely on the caller having set cr0.eq iff the exception
89 * occurred in kernel mode (i.e. MSR:PR = 0).
91 .globl transfer_to_handler_full
92 transfer_to_handler_full:
96 .globl transfer_to_handler
108 tovirt(r2,r2) /* set r2 to current */
109 beq 2f /* if from user, fix up THREAD.regs */
110 addi r11,r1,STACK_FRAME_OVERHEAD
112 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
113 /* Check to see if the dbcr0 register is set up to debug. Use the
114 single-step bit to do this. */
115 lwz r12,THREAD_DBCR0(r12)
116 andis. r12,r12,DBCR0_IC@h
118 /* From user and task is ptraced - load up global dbcr0 */
119 li r12,-1 /* clear all pending debug events */
121 lis r11,global_dbcr0@ha
123 addi r11,r11,global_dbcr0@l
131 2: /* if from kernel, check interrupted DOZE/NAP mode and
132 * check for stack overflow
138 bt- 8,power_save_6xx_restore /* Check DOZE */
139 END_FTR_SECTION_IFSET(CPU_FTR_CAN_DOZE)
141 bt- 9,power_save_6xx_restore /* Check NAP */
142 END_FTR_SECTION_IFSET(CPU_FTR_CAN_NAP)
143 #endif /* CONFIG_6xx */
144 .globl transfer_to_handler_cont
145 transfer_to_handler_cont:
146 lwz r11,THREAD_INFO-THREAD(r12)
147 cmplw r1,r11 /* if r1 <= current->thread_info */
148 ble- stack_ovf /* then the kernel stack overflowed */
151 lwz r11,0(r9) /* virtual address of handler */
152 lwz r9,4(r9) /* where to go when done */
158 RFI /* jump to handler, enable MMU */
161 * On kernel stack overflow, load up an initial stack pointer
162 * and call StackOverflow(regs), which should not return.
165 /* sometimes we use a statically-allocated stack, which is OK. */
169 ble 3b /* r1 <= &_end is OK */
171 addi r3,r1,STACK_FRAME_OVERHEAD
172 lis r1,init_thread_union@ha
173 addi r1,r1,init_thread_union@l
174 addi r1,r1,THREAD_SIZE-STACK_FRAME_OVERHEAD
175 lis r9,StackOverflow@ha
176 addi r9,r9,StackOverflow@l
177 LOAD_MSR_KERNEL(r10,MSR_KERNEL)
185 * Handle a system call.
187 .stabs "arch/powerpc/kernel/",N_SO,0,0,0f
188 .stabs "entry_32.S",N_SO,0,0,0f
192 stw r0,THREAD+LAST_SYSCALL(r2)
196 lwz r11,_CCR(r1) /* Clear SO bit in CR */
201 #endif /* SHOW_SYSCALLS */
202 rlwinm r10,r1,0,0,(31-THREAD_SHIFT) /* current_thread_info() */
203 lwz r11,TI_FLAGS(r10)
204 andi. r11,r11,_TIF_SYSCALL_T_OR_A
206 syscall_dotrace_cont:
207 cmplwi 0,r0,NR_syscalls
208 lis r10,sys_call_table@h
209 ori r10,r10,sys_call_table@l
212 lwzx r10,r10,r0 /* Fetch system call handler [ptr] */
214 addi r9,r1,STACK_FRAME_OVERHEAD
216 blrl /* Call handler */
217 .globl ret_from_syscall
220 bl do_show_syscall_exit
223 rlwinm r12,r1,0,0,(31-THREAD_SHIFT) /* current_thread_info() */
224 /* disable interrupts so current_thread_info()->flags can't change */
225 LOAD_MSR_KERNEL(r10,MSR_KERNEL) /* doesn't include MSR_EE */
230 andi. r0,r9,(_TIF_SYSCALL_T_OR_A|_TIF_SIGPENDING|_TIF_NEED_RESCHED|_TIF_RESTOREALL)
231 bne- syscall_exit_work
233 blt+ syscall_exit_cont
234 lwz r11,_CCR(r1) /* Load CR */
236 oris r11,r11,0x1000 /* Set SO bit in CR */
239 #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
240 /* If the process has its own DBCR0 value, load it up. The single
241 step bit tells us that dbcr0 should be loaded. */
242 lwz r0,THREAD+THREAD_DBCR0(r2)
243 andis. r10,r0,DBCR0_IC@h
246 stwcx. r0,0,r1 /* to clear the reservation */
271 /* Traced system call support */
276 addi r3,r1,STACK_FRAME_OVERHEAD
277 bl do_syscall_trace_enter
278 lwz r0,GPR0(r1) /* Restore original registers */
286 b syscall_dotrace_cont
289 andi. r0,r9,_TIF_RESTOREALL
293 andi. r0,r9,_TIF_NOERROR
295 lwz r11,_CCR(r1) /* Load CR */
297 oris r11,r11,0x1000 /* Set SO bit in CR */
300 1: stw r6,RESULT(r1) /* Save result */
301 stw r3,GPR3(r1) /* Update return value */
302 2: andi. r0,r9,(_TIF_PERSYSCALL_MASK)
305 /* Clear per-syscall TIF flags if any are set, but _leave_
306 _TIF_SAVE_NVGPRS set in r9 since we haven't dealt with that
309 li r11,_TIF_PERSYSCALL_MASK
310 addi r12,r12,TI_FLAGS
313 #ifdef CONFIG_IBM405_ERR77
318 subi r12,r12,TI_FLAGS
320 4: /* Anything which requires enabling interrupts? */
321 andi. r0,r9,(_TIF_SYSCALL_T_OR_A|_TIF_SINGLESTEP|_TIF_SAVE_NVGPRS)
324 /* Save NVGPRS if they're not saved already */
332 /* Re-enable interrupts */
333 5: ori r10,r10,MSR_EE
337 andi. r0,r9,_TIF_SAVE_NVGPRS
340 save_user_nvgprs_cont:
341 andi. r0,r9,(_TIF_SYSCALL_T_OR_A|_TIF_SINGLESTEP)
344 addi r3,r1,STACK_FRAME_OVERHEAD
345 bl do_syscall_trace_leave
349 LOAD_MSR_KERNEL(r10,MSR_KERNEL) /* doesn't include MSR_EE */
351 MTMSRD(r10) /* disable interrupts again */
352 rlwinm r12,r1,0,0,(31-THREAD_SHIFT) /* current_thread_info() */
355 andi. r0,r9,_TIF_NEED_RESCHED
360 andi. r0,r9,_TIF_SIGPENDING
366 MTMSRD(r10) /* re-enable interrupts */
371 lwz r8,TI_SIGFRAME(r12)
373 .macro savewords start, end
374 1: stw \start,4*(\start)(r8)
375 .section __ex_table,"a"
377 .long 1b,save_user_nvgprs_fault
380 savewords "(\start+1)",\end
384 b save_user_nvgprs_cont
387 save_user_nvgprs_fault:
388 li r3,11 /* SIGSEGV */
392 rlwinm r12,r1,0,0,(31-THREAD_SHIFT) /* current_thread_info() */
394 b save_user_nvgprs_cont
398 #ifdef SHOW_SYSCALLS_TASK
399 lis r11,show_syscalls_task@ha
400 lwz r11,show_syscalls_task@l(r11)
431 do_show_syscall_exit:
432 #ifdef SHOW_SYSCALLS_TASK
433 lis r11,show_syscalls_task@ha
434 lwz r11,show_syscalls_task@l(r11)
440 stw r3,RESULT(r1) /* Save result */
450 7: .string "syscall %d(%x, %x, %x, %x, %x, "
451 77: .string "%x), current=%p\n"
452 79: .string " -> %x\n"
455 #ifdef SHOW_SYSCALLS_TASK
457 .globl show_syscalls_task
462 #endif /* SHOW_SYSCALLS */
465 * The fork/clone functions need to copy the full register set into
466 * the child process. Therefore we need to save all the nonvolatile
467 * registers (r13 - r31) before calling the C code.
473 rlwinm r0,r0,0,0,30 /* clear LSB to indicate full */
474 stw r0,_TRAP(r1) /* register set saved */
481 rlwinm r0,r0,0,0,30 /* clear LSB to indicate full */
482 stw r0,_TRAP(r1) /* register set saved */
489 rlwinm r0,r0,0,0,30 /* clear LSB to indicate full */
490 stw r0,_TRAP(r1) /* register set saved */
494 * Top-level page fault handling.
495 * This is in assembler because if do_page_fault tells us that
496 * it is a bad kernel page fault, we want to save the non-volatile
497 * registers before calling bad_page_fault.
499 .globl handle_page_fault
502 addi r3,r1,STACK_FRAME_OVERHEAD
511 addi r3,r1,STACK_FRAME_OVERHEAD
514 b ret_from_except_full
517 * This routine switches between two different tasks. The process
518 * state of one is saved on its kernel stack. Then the state
519 * of the other is restored from its kernel stack. The memory
520 * management hardware is updated to the second process's state.
521 * Finally, we can return to the second process.
522 * On entry, r3 points to the THREAD for the current task, r4
523 * points to the THREAD for the new task.
525 * This routine is always called with interrupts disabled.
527 * Note: there are two ways to get to the "going out" portion
528 * of this code; either by coming in via the entry (_switch)
529 * or via "fork" which must set up an environment equivalent
530 * to the "_switch" path. If you change this , you'll have to
531 * change the fork code also.
533 * The code which creates the new task context is in 'copy_thread'
534 * in arch/ppc/kernel/process.c
537 stwu r1,-INT_FRAME_SIZE(r1)
539 stw r0,INT_FRAME_SIZE+4(r1)
540 /* r3-r12 are caller saved -- Cort */
542 stw r0,_NIP(r1) /* Return to switch caller */
544 li r0,MSR_FP /* Disable floating-point */
545 #ifdef CONFIG_ALTIVEC
547 oris r0,r0,MSR_VEC@h /* Disable altivec */
548 mfspr r12,SPRN_VRSAVE /* save vrsave register value */
549 stw r12,THREAD+THREAD_VRSAVE(r2)
550 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
551 #endif /* CONFIG_ALTIVEC */
553 oris r0,r0,MSR_SPE@h /* Disable SPE */
554 mfspr r12,SPRN_SPEFSCR /* save spefscr register value */
555 stw r12,THREAD+THREAD_SPEFSCR(r2)
556 #endif /* CONFIG_SPE */
557 and. r0,r0,r11 /* FP or altivec or SPE enabled? */
565 stw r1,KSP(r3) /* Set old stack pointer */
568 /* We need a sync somewhere here to make sure that if the
569 * previous task gets rescheduled on another CPU, it sees all
570 * stores it has performed on this one.
573 #endif /* CONFIG_SMP */
577 mtspr SPRN_SPRG3,r0 /* Update current THREAD phys addr */
578 lwz r1,KSP(r4) /* Load new stack pointer */
580 /* save the old current 'last' for return value */
582 addi r2,r4,-THREAD /* Update current */
584 #ifdef CONFIG_ALTIVEC
586 lwz r0,THREAD+THREAD_VRSAVE(r2)
587 mtspr SPRN_VRSAVE,r0 /* if G4, restore VRSAVE reg */
588 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
589 #endif /* CONFIG_ALTIVEC */
591 lwz r0,THREAD+THREAD_SPEFSCR(r2)
592 mtspr SPRN_SPEFSCR,r0 /* restore SPEFSCR reg */
593 #endif /* CONFIG_SPE */
597 /* r3-r12 are destroyed -- Cort */
600 lwz r4,_NIP(r1) /* Return to _switch caller in new task */
602 addi r1,r1,INT_FRAME_SIZE
605 .globl fast_exception_return
606 fast_exception_return:
607 #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
608 andi. r10,r9,MSR_RI /* check for recoverable interrupt */
609 beq 1f /* if not, we've got problems */
612 2: REST_4GPRS(3, r11)
627 #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
628 /* check if the exception happened in a restartable section */
629 1: lis r3,exc_exit_restart_end@ha
630 addi r3,r3,exc_exit_restart_end@l
633 lis r4,exc_exit_restart@ha
634 addi r4,r4,exc_exit_restart@l
637 lis r3,fee_restarts@ha
639 lwz r5,fee_restarts@l(r3)
641 stw r5,fee_restarts@l(r3)
642 mr r12,r4 /* restart at exc_exit_restart */
647 /* aargh, a nonrecoverable interrupt, panic */
648 /* aargh, we don't know which trap this is */
649 /* but the 601 doesn't implement the RI bit, so assume it's OK */
653 END_FTR_SECTION_IFSET(CPU_FTR_601)
656 addi r3,r1,STACK_FRAME_OVERHEAD
658 ori r10,r10,MSR_KERNEL@l
659 bl transfer_to_handler_full
660 .long nonrecoverable_exception
661 .long ret_from_except
664 .globl ret_from_except_full
665 ret_from_except_full:
669 .globl ret_from_except
671 /* Hard-disable interrupts so that current_thread_info()->flags
672 * can't change between when we test it and when we return
673 * from the interrupt. */
674 LOAD_MSR_KERNEL(r10,MSR_KERNEL)
675 SYNC /* Some chip revs have problems here... */
676 MTMSRD(r10) /* disable interrupts */
678 lwz r3,_MSR(r1) /* Returning to user mode? */
682 user_exc_return: /* r10 contains MSR_KERNEL here */
683 /* Check current_thread_info()->flags */
684 rlwinm r9,r1,0,0,(31-THREAD_SHIFT)
686 andi. r0,r9,(_TIF_SIGPENDING|_TIF_NEED_RESCHED|_TIF_RESTOREALL)
690 #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
691 /* Check whether this process has its own DBCR0 value. The single
692 step bit tells us that dbcr0 should be loaded. */
693 lwz r0,THREAD+THREAD_DBCR0(r2)
694 andis. r10,r0,DBCR0_IC@h
698 #ifdef CONFIG_PREEMPT
701 /* N.B. the only way to get here is from the beq following ret_from_except. */
703 /* check current_thread_info->preempt_count */
704 rlwinm r9,r1,0,0,(31-THREAD_SHIFT)
705 lwz r0,TI_PREEMPT(r9)
706 cmpwi 0,r0,0 /* if non-zero, just restore regs and return */
709 andi. r0,r0,_TIF_NEED_RESCHED
711 andi. r0,r3,MSR_EE /* interrupts off? */
712 beq restore /* don't schedule if so */
713 1: bl preempt_schedule_irq
714 rlwinm r9,r1,0,0,(31-THREAD_SHIFT)
716 andi. r0,r3,_TIF_NEED_RESCHED
720 #endif /* CONFIG_PREEMPT */
722 /* interrupts are hard-disabled at this point */
735 stwcx. r0,0,r1 /* to clear the reservation */
737 #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
739 andi. r10,r9,MSR_RI /* check if this exception occurred */
740 beql nonrecoverable /* at a bad place (MSR:RI = 0) */
748 * Once we put values in SRR0 and SRR1, we are in a state
749 * where exceptions are not recoverable, since taking an
750 * exception will trash SRR0 and SRR1. Therefore we clear the
751 * MSR:RI bit to indicate this. If we do take an exception,
752 * we can't return to the point of the exception but we
753 * can restart the exception exit path at the label
754 * exc_exit_restart below. -- paulus
756 LOAD_MSR_KERNEL(r10,MSR_KERNEL & ~MSR_RI)
758 MTMSRD(r10) /* clear the RI bit */
759 .globl exc_exit_restart
768 .globl exc_exit_restart_end
769 exc_exit_restart_end:
773 #else /* !(CONFIG_4xx || CONFIG_BOOKE) */
775 * This is a bit different on 4xx/Book-E because it doesn't have
776 * the RI bit in the MSR.
777 * The TLB miss handler checks if we have interrupted
778 * the exception exit path and restarts it if so
779 * (well maybe one day it will... :).
786 .globl exc_exit_restart
795 .globl exc_exit_restart_end
796 exc_exit_restart_end:
799 b . /* prevent prefetch past rfi */
802 * Returning from a critical interrupt in user mode doesn't need
803 * to be any different from a normal exception. For a critical
804 * interrupt in the kernel, we just return (without checking for
805 * preemption) since the interrupt may have happened at some crucial
806 * place (e.g. inside the TLB miss handler), and because we will be
807 * running with r1 pointing into critical_stack, not the current
808 * process's kernel stack (and therefore current_thread_info() will
809 * give the wrong answer).
810 * We have to restore various SPRs that may have been in use at the
811 * time of the critical interrupt.
815 #define PPC_40x_TURN_OFF_MSR_DR \
816 /* avoid any possible TLB misses here by turning off MSR.DR, we \
817 * assume the instructions here are mapped by a pinned TLB entry */ \
823 #define PPC_40x_TURN_OFF_MSR_DR
826 #define RET_FROM_EXC_LEVEL(exc_lvl_srr0, exc_lvl_srr1, exc_lvl_rfi) \
829 andi. r3,r3,MSR_PR; \
830 LOAD_MSR_KERNEL(r10,MSR_KERNEL); \
831 bne user_exc_return; \
838 mtspr SPRN_XER,r10; \
840 PPC405_ERR77(0,r1); \
841 stwcx. r0,0,r1; /* to clear the reservation */ \
846 PPC_40x_TURN_OFF_MSR_DR; \
849 mtspr SPRN_DEAR,r9; \
850 mtspr SPRN_ESR,r10; \
853 mtspr exc_lvl_srr0,r11; \
854 mtspr exc_lvl_srr1,r12; \
862 b .; /* prevent prefetch past exc_lvl_rfi */
864 .globl ret_from_crit_exc
866 RET_FROM_EXC_LEVEL(SPRN_CSRR0, SPRN_CSRR1, RFCI)
869 .globl ret_from_debug_exc
871 RET_FROM_EXC_LEVEL(SPRN_DSRR0, SPRN_DSRR1, RFDI)
873 .globl ret_from_mcheck_exc
875 RET_FROM_EXC_LEVEL(SPRN_MCSRR0, SPRN_MCSRR1, RFMCI)
876 #endif /* CONFIG_BOOKE */
879 * Load the DBCR0 value for a task that is being ptraced,
880 * having first saved away the global DBCR0. Note that r0
881 * has the dbcr0 value to set upon entry to this.
884 mfmsr r10 /* first disable debug exceptions */
885 rlwinm r10,r10,0,~MSR_DE
889 lis r11,global_dbcr0@ha
890 addi r11,r11,global_dbcr0@l
897 mtspr SPRN_DBSR,r11 /* clear all pending debug events */
901 #endif /* !(CONFIG_4xx || CONFIG_BOOKE) */
903 do_work: /* r10 contains MSR_KERNEL here */
904 andi. r0,r9,_TIF_NEED_RESCHED
907 do_resched: /* r10 contains MSR_KERNEL here */
910 MTMSRD(r10) /* hard-enable interrupts */
913 LOAD_MSR_KERNEL(r10,MSR_KERNEL)
915 MTMSRD(r10) /* disable interrupts */
916 rlwinm r9,r1,0,0,(31-THREAD_SHIFT)
918 andi. r0,r9,_TIF_NEED_RESCHED
920 andi. r0,r9,_TIF_SIGPENDING
922 do_user_signal: /* r10 contains MSR_KERNEL here */
925 MTMSRD(r10) /* hard-enable interrupts */
926 /* save r13-r31 in the exception frame, if not already done */
934 addi r4,r1,STACK_FRAME_OVERHEAD
940 * We come here when we are at the end of handling an exception
941 * that occurred at a place where taking an exception will lose
942 * state information, such as the contents of SRR0 and SRR1.
945 lis r10,exc_exit_restart_end@ha
946 addi r10,r10,exc_exit_restart_end@l
949 lis r11,exc_exit_restart@ha
950 addi r11,r11,exc_exit_restart@l
953 lis r10,ee_restarts@ha
954 lwz r12,ee_restarts@l(r10)
956 stw r12,ee_restarts@l(r10)
957 mr r12,r11 /* restart at exc_exit_restart */
959 3: /* OK, we can't recover, kill this process */
960 /* but the 601 doesn't implement the RI bit, so assume it's OK */
963 END_FTR_SECTION_IFSET(CPU_FTR_601)
970 4: addi r3,r1,STACK_FRAME_OVERHEAD
971 bl nonrecoverable_exception
972 /* shouldn't return */
978 * PROM code for specific machines follows. Put it
979 * here so it's easy to add arch-specific sections later.
982 #ifdef CONFIG_PPC_RTAS
984 * On CHRP, the Run-Time Abstraction Services (RTAS) have to be
985 * called with the MMU off.
988 stwu r1,-INT_FRAME_SIZE(r1)
990 stw r0,INT_FRAME_SIZE+4(r1)
991 LOAD_REG_ADDR(r4, rtas)
992 lis r6,1f@ha /* physical return address for rtas */
1000 LOAD_MSR_KERNEL(r0,MSR_KERNEL)
1001 SYNC /* disable interrupts so SRR0/1 */
1002 MTMSRD(r0) /* don't get trashed */
1003 li r9,MSR_KERNEL & ~(MSR_IR|MSR_DR)
1010 lwz r8,INT_FRAME_SIZE+4(r9) /* get return address */
1011 lwz r9,8(r9) /* original msr value */
1013 addi r1,r1,INT_FRAME_SIZE
1018 RFI /* return to caller */
1020 .globl machine_check_in_rtas
1021 machine_check_in_rtas:
1023 /* XXX load up BATs and panic */
1025 #endif /* CONFIG_PPC_RTAS */