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/errno.h>
23 #include <linux/sys.h>
24 #include <linux/threads.h>
28 #include <asm/cputable.h>
29 #include <asm/thread_info.h>
30 #include <asm/ppc_asm.h>
31 #include <asm/asm-offsets.h>
32 #include <asm/unistd.h>
35 #undef SHOW_SYSCALLS_TASK
38 * MSR_KERNEL is > 0x10000 on 4xx/Book-E since it include MSR_CE.
40 #if MSR_KERNEL >= 0x10000
41 #define LOAD_MSR_KERNEL(r, x) lis r,(x)@h; ori r,r,(x)@l
43 #define LOAD_MSR_KERNEL(r, x) li r,(x)
47 #include "head_booke.h"
48 #define TRANSFER_TO_HANDLER_EXC_LEVEL(exc_level) \
49 mtspr exc_level##_SPRG,r8; \
50 BOOKE_LOAD_EXC_LEVEL_STACK(exc_level); \
51 lwz r0,GPR10-INT_FRAME_SIZE(r8); \
53 lwz r0,GPR11-INT_FRAME_SIZE(r8); \
55 mfspr r8,exc_level##_SPRG
57 .globl mcheck_transfer_to_handler
58 mcheck_transfer_to_handler:
59 TRANSFER_TO_HANDLER_EXC_LEVEL(MCHECK)
60 b transfer_to_handler_full
62 .globl debug_transfer_to_handler
63 debug_transfer_to_handler:
64 TRANSFER_TO_HANDLER_EXC_LEVEL(DEBUG)
65 b transfer_to_handler_full
67 .globl crit_transfer_to_handler
68 crit_transfer_to_handler:
69 TRANSFER_TO_HANDLER_EXC_LEVEL(CRIT)
74 .globl crit_transfer_to_handler
75 crit_transfer_to_handler:
84 * This code finishes saving the registers to the exception frame
85 * and jumps to the appropriate handler for the exception, turning
86 * on address translation.
87 * Note that we rely on the caller having set cr0.eq iff the exception
88 * occurred in kernel mode (i.e. MSR:PR = 0).
90 .globl transfer_to_handler_full
91 transfer_to_handler_full:
95 .globl transfer_to_handler
107 tovirt(r2,r2) /* set r2 to current */
108 beq 2f /* if from user, fix up THREAD.regs */
109 addi r11,r1,STACK_FRAME_OVERHEAD
111 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
112 /* Check to see if the dbcr0 register is set up to debug. Use the
113 single-step bit to do this. */
114 lwz r12,THREAD_DBCR0(r12)
115 andis. r12,r12,DBCR0_IC@h
117 /* From user and task is ptraced - load up global dbcr0 */
118 li r12,-1 /* clear all pending debug events */
120 lis r11,global_dbcr0@ha
122 addi r11,r11,global_dbcr0@l
131 2: /* if from kernel, check interrupted DOZE/NAP mode and
132 * check for stack overflow
134 lwz r9,THREAD_INFO-THREAD(r12)
135 cmplw r1,r9 /* if r1 <= current->thread_info */
136 ble- stack_ovf /* then the kernel stack overflowed */
139 tophys(r9,r9) /* check local flags */
140 lwz r12,TI_LOCAL_FLAGS(r9)
142 bt- 31-TLF_NAPPING,4f
143 #endif /* CONFIG_6xx */
144 .globl transfer_to_handler_cont
145 transfer_to_handler_cont:
148 lwz r11,0(r9) /* virtual address of handler */
149 lwz r9,4(r9) /* where to go when done */
154 RFI /* jump to handler, enable MMU */
157 4: rlwinm r12,r12,0,~_TLF_NAPPING
158 stw r12,TI_LOCAL_FLAGS(r9)
159 b power_save_6xx_restore
163 * On kernel stack overflow, load up an initial stack pointer
164 * and call StackOverflow(regs), which should not return.
167 /* sometimes we use a statically-allocated stack, which is OK. */
171 ble 5b /* r1 <= &_end is OK */
173 addi r3,r1,STACK_FRAME_OVERHEAD
174 lis r1,init_thread_union@ha
175 addi r1,r1,init_thread_union@l
176 addi r1,r1,THREAD_SIZE-STACK_FRAME_OVERHEAD
177 lis r9,StackOverflow@ha
178 addi r9,r9,StackOverflow@l
179 LOAD_MSR_KERNEL(r10,MSR_KERNEL)
187 * Handle a system call.
189 .stabs "arch/powerpc/kernel/",N_SO,0,0,0f
190 .stabs "entry_32.S",N_SO,0,0,0f
197 lwz r11,_CCR(r1) /* Clear SO bit in CR */
202 #endif /* SHOW_SYSCALLS */
203 rlwinm r10,r1,0,0,(31-THREAD_SHIFT) /* current_thread_info() */
204 lwz r11,TI_FLAGS(r10)
205 andi. r11,r11,_TIF_SYSCALL_T_OR_A
207 syscall_dotrace_cont:
208 cmplwi 0,r0,NR_syscalls
209 lis r10,sys_call_table@h
210 ori r10,r10,sys_call_table@l
213 lwzx r10,r10,r0 /* Fetch system call handler [ptr] */
215 addi r9,r1,STACK_FRAME_OVERHEAD
217 blrl /* Call handler */
218 .globl ret_from_syscall
221 bl do_show_syscall_exit
224 rlwinm r12,r1,0,0,(31-THREAD_SHIFT) /* current_thread_info() */
225 /* disable interrupts so current_thread_info()->flags can't change */
226 LOAD_MSR_KERNEL(r10,MSR_KERNEL) /* doesn't include MSR_EE */
231 andi. r0,r9,(_TIF_SYSCALL_T_OR_A|_TIF_SINGLESTEP|_TIF_USER_WORK_MASK|_TIF_PERSYSCALL_MASK)
232 bne- syscall_exit_work
234 blt+ syscall_exit_cont
235 lwz r11,_CCR(r1) /* Load CR */
237 oris r11,r11,0x1000 /* Set SO bit in CR */
240 #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
241 /* If the process has its own DBCR0 value, load it up. The single
242 step bit tells us that dbcr0 should be loaded. */
243 lwz r0,THREAD+THREAD_DBCR0(r2)
244 andis. r10,r0,DBCR0_IC@h
248 lis r4,icache_44x_need_flush@ha
249 lwz r5,icache_44x_need_flush@l(r4)
253 #endif /* CONFIG_44x */
256 END_FTR_SECTION_IFSET(CPU_FTR_NEED_PAIRED_STWCX)
257 stwcx. r0,0,r1 /* to clear the reservation */
274 stw r7,icache_44x_need_flush@l(r4)
276 #endif /* CONFIG_44x */
288 /* Traced system call support */
293 addi r3,r1,STACK_FRAME_OVERHEAD
294 bl do_syscall_trace_enter
295 lwz r0,GPR0(r1) /* Restore original registers */
303 b syscall_dotrace_cont
306 andi. r0,r9,_TIF_RESTOREALL
312 andi. r0,r9,_TIF_NOERROR
314 lwz r11,_CCR(r1) /* Load CR */
316 oris r11,r11,0x1000 /* Set SO bit in CR */
319 1: stw r6,RESULT(r1) /* Save result */
320 stw r3,GPR3(r1) /* Update return value */
321 2: andi. r0,r9,(_TIF_PERSYSCALL_MASK)
324 /* Clear per-syscall TIF flags if any are set. */
326 li r11,_TIF_PERSYSCALL_MASK
327 addi r12,r12,TI_FLAGS
330 #ifdef CONFIG_IBM405_ERR77
335 subi r12,r12,TI_FLAGS
337 4: /* Anything which requires enabling interrupts? */
338 andi. r0,r9,(_TIF_SYSCALL_T_OR_A|_TIF_SINGLESTEP)
341 /* Re-enable interrupts */
346 /* Save NVGPRS if they're not saved already */
354 addi r3,r1,STACK_FRAME_OVERHEAD
355 bl do_syscall_trace_leave
356 b ret_from_except_full
360 #ifdef SHOW_SYSCALLS_TASK
361 lis r11,show_syscalls_task@ha
362 lwz r11,show_syscalls_task@l(r11)
393 do_show_syscall_exit:
394 #ifdef SHOW_SYSCALLS_TASK
395 lis r11,show_syscalls_task@ha
396 lwz r11,show_syscalls_task@l(r11)
402 stw r3,RESULT(r1) /* Save result */
412 7: .string "syscall %d(%x, %x, %x, %x, %x, "
413 77: .string "%x), current=%p\n"
414 79: .string " -> %x\n"
417 #ifdef SHOW_SYSCALLS_TASK
419 .globl show_syscalls_task
424 #endif /* SHOW_SYSCALLS */
427 * The fork/clone functions need to copy the full register set into
428 * the child process. Therefore we need to save all the nonvolatile
429 * registers (r13 - r31) before calling the C code.
435 rlwinm r0,r0,0,0,30 /* clear LSB to indicate full */
436 stw r0,_TRAP(r1) /* register set saved */
443 rlwinm r0,r0,0,0,30 /* clear LSB to indicate full */
444 stw r0,_TRAP(r1) /* register set saved */
451 rlwinm r0,r0,0,0,30 /* clear LSB to indicate full */
452 stw r0,_TRAP(r1) /* register set saved */
455 .globl ppc_swapcontext
459 rlwinm r0,r0,0,0,30 /* clear LSB to indicate full */
460 stw r0,_TRAP(r1) /* register set saved */
464 * Top-level page fault handling.
465 * This is in assembler because if do_page_fault tells us that
466 * it is a bad kernel page fault, we want to save the non-volatile
467 * registers before calling bad_page_fault.
469 .globl handle_page_fault
472 addi r3,r1,STACK_FRAME_OVERHEAD
481 addi r3,r1,STACK_FRAME_OVERHEAD
484 b ret_from_except_full
487 * This routine switches between two different tasks. The process
488 * state of one is saved on its kernel stack. Then the state
489 * of the other is restored from its kernel stack. The memory
490 * management hardware is updated to the second process's state.
491 * Finally, we can return to the second process.
492 * On entry, r3 points to the THREAD for the current task, r4
493 * points to the THREAD for the new task.
495 * This routine is always called with interrupts disabled.
497 * Note: there are two ways to get to the "going out" portion
498 * of this code; either by coming in via the entry (_switch)
499 * or via "fork" which must set up an environment equivalent
500 * to the "_switch" path. If you change this , you'll have to
501 * change the fork code also.
503 * The code which creates the new task context is in 'copy_thread'
504 * in arch/ppc/kernel/process.c
507 stwu r1,-INT_FRAME_SIZE(r1)
509 stw r0,INT_FRAME_SIZE+4(r1)
510 /* r3-r12 are caller saved -- Cort */
512 stw r0,_NIP(r1) /* Return to switch caller */
514 li r0,MSR_FP /* Disable floating-point */
515 #ifdef CONFIG_ALTIVEC
517 oris r0,r0,MSR_VEC@h /* Disable altivec */
518 mfspr r12,SPRN_VRSAVE /* save vrsave register value */
519 stw r12,THREAD+THREAD_VRSAVE(r2)
520 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
521 #endif /* CONFIG_ALTIVEC */
524 oris r0,r0,MSR_SPE@h /* Disable SPE */
525 mfspr r12,SPRN_SPEFSCR /* save spefscr register value */
526 stw r12,THREAD+THREAD_SPEFSCR(r2)
527 END_FTR_SECTION_IFSET(CPU_FTR_SPE)
528 #endif /* CONFIG_SPE */
529 and. r0,r0,r11 /* FP or altivec or SPE enabled? */
537 stw r1,KSP(r3) /* Set old stack pointer */
540 /* We need a sync somewhere here to make sure that if the
541 * previous task gets rescheduled on another CPU, it sees all
542 * stores it has performed on this one.
545 #endif /* CONFIG_SMP */
549 mtspr SPRN_SPRG3,r0 /* Update current THREAD phys addr */
550 lwz r1,KSP(r4) /* Load new stack pointer */
552 /* save the old current 'last' for return value */
554 addi r2,r4,-THREAD /* Update current */
556 #ifdef CONFIG_ALTIVEC
558 lwz r0,THREAD+THREAD_VRSAVE(r2)
559 mtspr SPRN_VRSAVE,r0 /* if G4, restore VRSAVE reg */
560 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
561 #endif /* CONFIG_ALTIVEC */
564 lwz r0,THREAD+THREAD_SPEFSCR(r2)
565 mtspr SPRN_SPEFSCR,r0 /* restore SPEFSCR reg */
566 END_FTR_SECTION_IFSET(CPU_FTR_SPE)
567 #endif /* CONFIG_SPE */
571 /* r3-r12 are destroyed -- Cort */
574 lwz r4,_NIP(r1) /* Return to _switch caller in new task */
576 addi r1,r1,INT_FRAME_SIZE
579 .globl fast_exception_return
580 fast_exception_return:
581 #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
582 andi. r10,r9,MSR_RI /* check for recoverable interrupt */
583 beq 1f /* if not, we've got problems */
586 2: REST_4GPRS(3, r11)
601 #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
602 /* check if the exception happened in a restartable section */
603 1: lis r3,exc_exit_restart_end@ha
604 addi r3,r3,exc_exit_restart_end@l
607 lis r4,exc_exit_restart@ha
608 addi r4,r4,exc_exit_restart@l
611 lis r3,fee_restarts@ha
613 lwz r5,fee_restarts@l(r3)
615 stw r5,fee_restarts@l(r3)
616 mr r12,r4 /* restart at exc_exit_restart */
625 /* aargh, a nonrecoverable interrupt, panic */
626 /* aargh, we don't know which trap this is */
627 /* but the 601 doesn't implement the RI bit, so assume it's OK */
631 END_FTR_SECTION_IFSET(CPU_FTR_601)
634 addi r3,r1,STACK_FRAME_OVERHEAD
636 ori r10,r10,MSR_KERNEL@l
637 bl transfer_to_handler_full
638 .long nonrecoverable_exception
639 .long ret_from_except
642 .globl ret_from_except_full
643 ret_from_except_full:
647 .globl ret_from_except
649 /* Hard-disable interrupts so that current_thread_info()->flags
650 * can't change between when we test it and when we return
651 * from the interrupt. */
652 LOAD_MSR_KERNEL(r10,MSR_KERNEL)
653 SYNC /* Some chip revs have problems here... */
654 MTMSRD(r10) /* disable interrupts */
656 lwz r3,_MSR(r1) /* Returning to user mode? */
660 user_exc_return: /* r10 contains MSR_KERNEL here */
661 /* Check current_thread_info()->flags */
662 rlwinm r9,r1,0,0,(31-THREAD_SHIFT)
664 andi. r0,r9,(_TIF_SIGPENDING|_TIF_RESTORE_SIGMASK|_TIF_NEED_RESCHED)
668 #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
669 /* Check whether this process has its own DBCR0 value. The single
670 step bit tells us that dbcr0 should be loaded. */
671 lwz r0,THREAD+THREAD_DBCR0(r2)
672 andis. r10,r0,DBCR0_IC@h
676 #ifdef CONFIG_PREEMPT
679 /* N.B. the only way to get here is from the beq following ret_from_except. */
681 /* check current_thread_info->preempt_count */
682 rlwinm r9,r1,0,0,(31-THREAD_SHIFT)
683 lwz r0,TI_PREEMPT(r9)
684 cmpwi 0,r0,0 /* if non-zero, just restore regs and return */
687 andi. r0,r0,_TIF_NEED_RESCHED
689 andi. r0,r3,MSR_EE /* interrupts off? */
690 beq restore /* don't schedule if so */
691 1: bl preempt_schedule_irq
692 rlwinm r9,r1,0,0,(31-THREAD_SHIFT)
694 andi. r0,r3,_TIF_NEED_RESCHED
698 #endif /* CONFIG_PREEMPT */
700 /* interrupts are hard-disabled at this point */
703 lis r4,icache_44x_need_flush@ha
704 lwz r5,icache_44x_need_flush@l(r4)
709 stw r6,icache_44x_need_flush@l(r4)
711 #endif /* CONFIG_44x */
725 END_FTR_SECTION_IFSET(CPU_FTR_NEED_PAIRED_STWCX)
726 stwcx. r0,0,r1 /* to clear the reservation */
728 #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
730 andi. r10,r9,MSR_RI /* check if this exception occurred */
731 beql nonrecoverable /* at a bad place (MSR:RI = 0) */
739 * Once we put values in SRR0 and SRR1, we are in a state
740 * where exceptions are not recoverable, since taking an
741 * exception will trash SRR0 and SRR1. Therefore we clear the
742 * MSR:RI bit to indicate this. If we do take an exception,
743 * we can't return to the point of the exception but we
744 * can restart the exception exit path at the label
745 * exc_exit_restart below. -- paulus
747 LOAD_MSR_KERNEL(r10,MSR_KERNEL & ~MSR_RI)
749 MTMSRD(r10) /* clear the RI bit */
750 .globl exc_exit_restart
759 .globl exc_exit_restart_end
760 exc_exit_restart_end:
764 #else /* !(CONFIG_4xx || CONFIG_BOOKE) */
766 * This is a bit different on 4xx/Book-E because it doesn't have
767 * the RI bit in the MSR.
768 * The TLB miss handler checks if we have interrupted
769 * the exception exit path and restarts it if so
770 * (well maybe one day it will... :).
777 .globl exc_exit_restart
786 .globl exc_exit_restart_end
787 exc_exit_restart_end:
790 b . /* prevent prefetch past rfi */
793 * Returning from a critical interrupt in user mode doesn't need
794 * to be any different from a normal exception. For a critical
795 * interrupt in the kernel, we just return (without checking for
796 * preemption) since the interrupt may have happened at some crucial
797 * place (e.g. inside the TLB miss handler), and because we will be
798 * running with r1 pointing into critical_stack, not the current
799 * process's kernel stack (and therefore current_thread_info() will
800 * give the wrong answer).
801 * We have to restore various SPRs that may have been in use at the
802 * time of the critical interrupt.
806 #define PPC_40x_TURN_OFF_MSR_DR \
807 /* avoid any possible TLB misses here by turning off MSR.DR, we \
808 * assume the instructions here are mapped by a pinned TLB entry */ \
814 #define PPC_40x_TURN_OFF_MSR_DR
817 #define RET_FROM_EXC_LEVEL(exc_lvl_srr0, exc_lvl_srr1, exc_lvl_rfi) \
820 andi. r3,r3,MSR_PR; \
821 LOAD_MSR_KERNEL(r10,MSR_KERNEL); \
822 bne user_exc_return; \
829 mtspr SPRN_XER,r10; \
831 PPC405_ERR77(0,r1); \
832 stwcx. r0,0,r1; /* to clear the reservation */ \
837 PPC_40x_TURN_OFF_MSR_DR; \
840 mtspr SPRN_DEAR,r9; \
841 mtspr SPRN_ESR,r10; \
844 mtspr exc_lvl_srr0,r11; \
845 mtspr exc_lvl_srr1,r12; \
853 b .; /* prevent prefetch past exc_lvl_rfi */
855 .globl ret_from_crit_exc
857 RET_FROM_EXC_LEVEL(SPRN_CSRR0, SPRN_CSRR1, RFCI)
860 .globl ret_from_debug_exc
862 RET_FROM_EXC_LEVEL(SPRN_DSRR0, SPRN_DSRR1, RFDI)
864 .globl ret_from_mcheck_exc
866 RET_FROM_EXC_LEVEL(SPRN_MCSRR0, SPRN_MCSRR1, RFMCI)
867 #endif /* CONFIG_BOOKE */
870 * Load the DBCR0 value for a task that is being ptraced,
871 * having first saved away the global DBCR0. Note that r0
872 * has the dbcr0 value to set upon entry to this.
875 mfmsr r10 /* first disable debug exceptions */
876 rlwinm r10,r10,0,~MSR_DE
880 lis r11,global_dbcr0@ha
881 addi r11,r11,global_dbcr0@l
888 mtspr SPRN_DBSR,r11 /* clear all pending debug events */
896 #endif /* !(CONFIG_4xx || CONFIG_BOOKE) */
898 do_work: /* r10 contains MSR_KERNEL here */
899 andi. r0,r9,_TIF_NEED_RESCHED
902 do_resched: /* r10 contains MSR_KERNEL here */
905 MTMSRD(r10) /* hard-enable interrupts */
908 LOAD_MSR_KERNEL(r10,MSR_KERNEL)
910 MTMSRD(r10) /* disable interrupts */
911 rlwinm r9,r1,0,0,(31-THREAD_SHIFT)
913 andi. r0,r9,_TIF_NEED_RESCHED
915 andi. r0,r9,_TIF_SIGPENDING|_TIF_RESTORE_SIGMASK
917 do_user_signal: /* r10 contains MSR_KERNEL here */
920 MTMSRD(r10) /* hard-enable interrupts */
921 /* save r13-r31 in the exception frame, if not already done */
929 addi r4,r1,STACK_FRAME_OVERHEAD
935 * We come here when we are at the end of handling an exception
936 * that occurred at a place where taking an exception will lose
937 * state information, such as the contents of SRR0 and SRR1.
940 lis r10,exc_exit_restart_end@ha
941 addi r10,r10,exc_exit_restart_end@l
944 lis r11,exc_exit_restart@ha
945 addi r11,r11,exc_exit_restart@l
948 lis r10,ee_restarts@ha
949 lwz r12,ee_restarts@l(r10)
951 stw r12,ee_restarts@l(r10)
952 mr r12,r11 /* restart at exc_exit_restart */
954 3: /* OK, we can't recover, kill this process */
955 /* but the 601 doesn't implement the RI bit, so assume it's OK */
958 END_FTR_SECTION_IFSET(CPU_FTR_601)
965 4: addi r3,r1,STACK_FRAME_OVERHEAD
966 bl nonrecoverable_exception
967 /* shouldn't return */
977 * PROM code for specific machines follows. Put it
978 * here so it's easy to add arch-specific sections later.
981 #ifdef CONFIG_PPC_RTAS
983 * On CHRP, the Run-Time Abstraction Services (RTAS) have to be
984 * called with the MMU off.
987 stwu r1,-INT_FRAME_SIZE(r1)
989 stw r0,INT_FRAME_SIZE+4(r1)
990 LOAD_REG_ADDR(r4, rtas)
991 lis r6,1f@ha /* physical return address for rtas */
999 LOAD_MSR_KERNEL(r0,MSR_KERNEL)
1000 SYNC /* disable interrupts so SRR0/1 */
1001 MTMSRD(r0) /* don't get trashed */
1002 li r9,MSR_KERNEL & ~(MSR_IR|MSR_DR)
1009 lwz r8,INT_FRAME_SIZE+4(r9) /* get return address */
1010 lwz r9,8(r9) /* original msr value */
1012 addi r1,r1,INT_FRAME_SIZE
1017 RFI /* return to caller */
1019 .globl machine_check_in_rtas
1020 machine_check_in_rtas:
1022 /* XXX load up BATs and panic */
1024 #endif /* CONFIG_PPC_RTAS */