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>
26 #include <asm/processor.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
132 2: /* if from kernel, check interrupted DOZE/NAP mode and
133 * check for stack overflow
135 lwz r9,THREAD_INFO-THREAD(r12)
136 cmplw r1,r9 /* if r1 <= current->thread_info */
137 ble- stack_ovf /* then the kernel stack overflowed */
140 tophys(r9,r9) /* check local flags */
141 lwz r12,TI_LOCAL_FLAGS(r9)
143 bt- 31-TLF_NAPPING,4f
144 #endif /* CONFIG_6xx */
145 .globl transfer_to_handler_cont
146 transfer_to_handler_cont:
149 lwz r11,0(r9) /* virtual address of handler */
150 lwz r9,4(r9) /* where to go when done */
155 RFI /* jump to handler, enable MMU */
158 4: rlwinm r12,r12,0,~_TLF_NAPPING
159 stw r12,TI_LOCAL_FLAGS(r9)
160 b power_save_6xx_restore
164 * On kernel stack overflow, load up an initial stack pointer
165 * and call StackOverflow(regs), which should not return.
168 /* sometimes we use a statically-allocated stack, which is OK. */
172 ble 5b /* r1 <= &_end is OK */
174 addi r3,r1,STACK_FRAME_OVERHEAD
175 lis r1,init_thread_union@ha
176 addi r1,r1,init_thread_union@l
177 addi r1,r1,THREAD_SIZE-STACK_FRAME_OVERHEAD
178 lis r9,StackOverflow@ha
179 addi r9,r9,StackOverflow@l
180 LOAD_MSR_KERNEL(r10,MSR_KERNEL)
188 * Handle a system call.
190 .stabs "arch/ppc/kernel/",N_SO,0,0,0f
191 .stabs "entry.S",N_SO,0,0,0f
195 stw r0,THREAD+LAST_SYSCALL(r2)
199 lwz r11,_CCR(r1) /* Clear SO bit in CR */
204 #endif /* SHOW_SYSCALLS */
205 rlwinm r10,r1,0,0,18 /* current_thread_info() */
206 lwz r11,TI_FLAGS(r10)
207 andi. r11,r11,_TIF_SYSCALL_T_OR_A
209 syscall_dotrace_cont:
210 cmplwi 0,r0,NR_syscalls
211 lis r10,sys_call_table@h
212 ori r10,r10,sys_call_table@l
215 lwzx r10,r10,r0 /* Fetch system call handler [ptr] */
217 addi r9,r1,STACK_FRAME_OVERHEAD
219 blrl /* Call handler */
220 .globl ret_from_syscall
223 bl do_show_syscall_exit
226 rlwinm r12,r1,0,0,18 /* current_thread_info() */
227 /* disable interrupts so current_thread_info()->flags can't change */
228 LOAD_MSR_KERNEL(r10,MSR_KERNEL) /* doesn't include MSR_EE */
233 andi. r0,r9,(_TIF_SYSCALL_T_OR_A|_TIF_SINGLESTEP|_TIF_USER_WORK_MASK|_TIF_PERSYSCALL_MASK)
234 bne- syscall_exit_work
236 blt+ syscall_exit_cont
237 lwz r11,_CCR(r1) /* Load CR */
239 oris r11,r11,0x1000 /* Set SO bit in CR */
242 #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
243 /* If the process has its own DBCR0 value, load it up. The single
244 step bit tells us that dbcr0 should be loaded. */
245 lwz r0,THREAD+THREAD_DBCR0(r2)
246 andis. r10,r0,DBCR0_IC@h
249 stwcx. r0,0,r1 /* to clear the reservation */
274 /* Traced system call support */
279 addi r3,r1,STACK_FRAME_OVERHEAD
280 bl do_syscall_trace_enter
281 lwz r0,GPR0(r1) /* Restore original registers */
289 b syscall_dotrace_cont
292 andi. r0,r9,_TIF_RESTOREALL
298 andi. r0,r9,_TIF_NOERROR
300 lwz r11,_CCR(r1) /* Load CR */
302 oris r11,r11,0x1000 /* Set SO bit in CR */
305 1: stw r6,RESULT(r1) /* Save result */
306 stw r3,GPR3(r1) /* Update return value */
307 2: andi. r0,r9,(_TIF_PERSYSCALL_MASK)
310 /* Clear per-syscall TIF flags if any are set. */
312 li r11,_TIF_PERSYSCALL_MASK
313 addi r12,r12,TI_FLAGS
316 #ifdef CONFIG_IBM405_ERR77
321 subi r12,r12,TI_FLAGS
323 4: /* Anything which requires enabling interrupts? */
324 andi. r0,r9,(_TIF_SYSCALL_T_OR_A|_TIF_SINGLESTEP)
327 /* Re-enable interrupts */
332 /* Save NVGPRS if they're not saved already */
340 addi r3,r1,STACK_FRAME_OVERHEAD
341 bl do_syscall_trace_leave
342 b ret_from_except_full
346 #ifdef SHOW_SYSCALLS_TASK
347 lis r11,show_syscalls_task@ha
348 lwz r11,show_syscalls_task@l(r11)
379 do_show_syscall_exit:
380 #ifdef SHOW_SYSCALLS_TASK
381 lis r11,show_syscalls_task@ha
382 lwz r11,show_syscalls_task@l(r11)
388 stw r3,RESULT(r1) /* Save result */
398 7: .string "syscall %d(%x, %x, %x, %x, %x, "
399 77: .string "%x), current=%p\n"
400 79: .string " -> %x\n"
403 #ifdef SHOW_SYSCALLS_TASK
405 .globl show_syscalls_task
410 #endif /* SHOW_SYSCALLS */
413 * The fork/clone functions need to copy the full register set into
414 * the child process. Therefore we need to save all the nonvolatile
415 * registers (r13 - r31) before calling the C code.
421 rlwinm r0,r0,0,0,30 /* clear LSB to indicate full */
422 stw r0,TRAP(r1) /* register set saved */
429 rlwinm r0,r0,0,0,30 /* clear LSB to indicate full */
430 stw r0,TRAP(r1) /* register set saved */
437 rlwinm r0,r0,0,0,30 /* clear LSB to indicate full */
438 stw r0,TRAP(r1) /* register set saved */
441 .globl ppc_swapcontext
445 rlwinm r0,r0,0,0,30 /* clear LSB to indicate full */
446 stw r0,TRAP(r1) /* register set saved */
450 * Top-level page fault handling.
451 * This is in assembler because if do_page_fault tells us that
452 * it is a bad kernel page fault, we want to save the non-volatile
453 * registers before calling bad_page_fault.
455 .globl handle_page_fault
458 addi r3,r1,STACK_FRAME_OVERHEAD
467 addi r3,r1,STACK_FRAME_OVERHEAD
470 b ret_from_except_full
473 * This routine switches between two different tasks. The process
474 * state of one is saved on its kernel stack. Then the state
475 * of the other is restored from its kernel stack. The memory
476 * management hardware is updated to the second process's state.
477 * Finally, we can return to the second process.
478 * On entry, r3 points to the THREAD for the current task, r4
479 * points to the THREAD for the new task.
481 * This routine is always called with interrupts disabled.
483 * Note: there are two ways to get to the "going out" portion
484 * of this code; either by coming in via the entry (_switch)
485 * or via "fork" which must set up an environment equivalent
486 * to the "_switch" path. If you change this , you'll have to
487 * change the fork code also.
489 * The code which creates the new task context is in 'copy_thread'
490 * in arch/ppc/kernel/process.c
493 stwu r1,-INT_FRAME_SIZE(r1)
495 stw r0,INT_FRAME_SIZE+4(r1)
496 /* r3-r12 are caller saved -- Cort */
498 stw r0,_NIP(r1) /* Return to switch caller */
500 li r0,MSR_FP /* Disable floating-point */
501 #ifdef CONFIG_ALTIVEC
503 oris r0,r0,MSR_VEC@h /* Disable altivec */
504 mfspr r12,SPRN_VRSAVE /* save vrsave register value */
505 stw r12,THREAD+THREAD_VRSAVE(r2)
506 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
507 #endif /* CONFIG_ALTIVEC */
509 oris r0,r0,MSR_SPE@h /* Disable SPE */
510 mfspr r12,SPRN_SPEFSCR /* save spefscr register value */
511 stw r12,THREAD+THREAD_SPEFSCR(r2)
512 #endif /* CONFIG_SPE */
513 and. r0,r0,r11 /* FP or altivec or SPE enabled? */
521 stw r1,KSP(r3) /* Set old stack pointer */
524 /* We need a sync somewhere here to make sure that if the
525 * previous task gets rescheduled on another CPU, it sees all
526 * stores it has performed on this one.
529 #endif /* CONFIG_SMP */
533 mtspr SPRN_SPRG3,r0 /* Update current THREAD phys addr */
534 lwz r1,KSP(r4) /* Load new stack pointer */
536 /* save the old current 'last' for return value */
538 addi r2,r4,-THREAD /* Update current */
540 #ifdef CONFIG_ALTIVEC
542 lwz r0,THREAD+THREAD_VRSAVE(r2)
543 mtspr SPRN_VRSAVE,r0 /* if G4, restore VRSAVE reg */
544 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
545 #endif /* CONFIG_ALTIVEC */
547 lwz r0,THREAD+THREAD_SPEFSCR(r2)
548 mtspr SPRN_SPEFSCR,r0 /* restore SPEFSCR reg */
549 #endif /* CONFIG_SPE */
553 /* r3-r12 are destroyed -- Cort */
556 lwz r4,_NIP(r1) /* Return to _switch caller in new task */
558 addi r1,r1,INT_FRAME_SIZE
561 .globl fast_exception_return
562 fast_exception_return:
563 #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
564 andi. r10,r9,MSR_RI /* check for recoverable interrupt */
565 beq 1f /* if not, we've got problems */
568 2: REST_4GPRS(3, r11)
583 #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
584 /* check if the exception happened in a restartable section */
585 1: lis r3,exc_exit_restart_end@ha
586 addi r3,r3,exc_exit_restart_end@l
589 lis r4,exc_exit_restart@ha
590 addi r4,r4,exc_exit_restart@l
593 lis r3,fee_restarts@ha
595 lwz r5,fee_restarts@l(r3)
597 stw r5,fee_restarts@l(r3)
598 mr r12,r4 /* restart at exc_exit_restart */
603 /* aargh, a nonrecoverable interrupt, panic */
604 /* aargh, we don't know which trap this is */
605 /* but the 601 doesn't implement the RI bit, so assume it's OK */
609 END_FTR_SECTION_IFSET(CPU_FTR_601)
612 addi r3,r1,STACK_FRAME_OVERHEAD
614 ori r10,r10,MSR_KERNEL@l
615 bl transfer_to_handler_full
616 .long nonrecoverable_exception
617 .long ret_from_except
620 .globl ret_from_except_full
621 ret_from_except_full:
625 .globl ret_from_except
627 /* Hard-disable interrupts so that current_thread_info()->flags
628 * can't change between when we test it and when we return
629 * from the interrupt. */
630 LOAD_MSR_KERNEL(r10,MSR_KERNEL)
631 SYNC /* Some chip revs have problems here... */
632 MTMSRD(r10) /* disable interrupts */
634 lwz r3,_MSR(r1) /* Returning to user mode? */
638 user_exc_return: /* r10 contains MSR_KERNEL here */
639 /* Check current_thread_info()->flags */
642 andi. r0,r9,(_TIF_SIGPENDING|_TIF_RESTORE_SIGMASK|_TIF_NEED_RESCHED)
646 #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
647 /* Check whether this process has its own DBCR0 value. The single
648 step bit tells us that dbcr0 should be loaded. */
649 lwz r0,THREAD+THREAD_DBCR0(r2)
650 andis. r10,r0,DBCR0_IC@h
654 #ifdef CONFIG_PREEMPT
657 /* N.B. the only way to get here is from the beq following ret_from_except. */
659 /* check current_thread_info->preempt_count */
661 lwz r0,TI_PREEMPT(r9)
662 cmpwi 0,r0,0 /* if non-zero, just restore regs and return */
665 andi. r0,r0,_TIF_NEED_RESCHED
667 andi. r0,r3,MSR_EE /* interrupts off? */
668 beq restore /* don't schedule if so */
669 1: bl preempt_schedule_irq
672 andi. r0,r3,_TIF_NEED_RESCHED
676 #endif /* CONFIG_PREEMPT */
678 /* interrupts are hard-disabled at this point */
691 stwcx. r0,0,r1 /* to clear the reservation */
693 #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
695 andi. r10,r9,MSR_RI /* check if this exception occurred */
696 beql nonrecoverable /* at a bad place (MSR:RI = 0) */
704 * Once we put values in SRR0 and SRR1, we are in a state
705 * where exceptions are not recoverable, since taking an
706 * exception will trash SRR0 and SRR1. Therefore we clear the
707 * MSR:RI bit to indicate this. If we do take an exception,
708 * we can't return to the point of the exception but we
709 * can restart the exception exit path at the label
710 * exc_exit_restart below. -- paulus
712 LOAD_MSR_KERNEL(r10,MSR_KERNEL & ~MSR_RI)
714 MTMSRD(r10) /* clear the RI bit */
715 .globl exc_exit_restart
724 .globl exc_exit_restart_end
725 exc_exit_restart_end:
729 #else /* !(CONFIG_4xx || CONFIG_BOOKE) */
731 * This is a bit different on 4xx/Book-E because it doesn't have
732 * the RI bit in the MSR.
733 * The TLB miss handler checks if we have interrupted
734 * the exception exit path and restarts it if so
735 * (well maybe one day it will... :).
742 .globl exc_exit_restart
751 .globl exc_exit_restart_end
752 exc_exit_restart_end:
755 b . /* prevent prefetch past rfi */
758 * Returning from a critical interrupt in user mode doesn't need
759 * to be any different from a normal exception. For a critical
760 * interrupt in the kernel, we just return (without checking for
761 * preemption) since the interrupt may have happened at some crucial
762 * place (e.g. inside the TLB miss handler), and because we will be
763 * running with r1 pointing into critical_stack, not the current
764 * process's kernel stack (and therefore current_thread_info() will
765 * give the wrong answer).
766 * We have to restore various SPRs that may have been in use at the
767 * time of the critical interrupt.
771 #define PPC_40x_TURN_OFF_MSR_DR \
772 /* avoid any possible TLB misses here by turning off MSR.DR, we \
773 * assume the instructions here are mapped by a pinned TLB entry */ \
779 #define PPC_40x_TURN_OFF_MSR_DR
782 #define RET_FROM_EXC_LEVEL(exc_lvl_srr0, exc_lvl_srr1, exc_lvl_rfi) \
785 andi. r3,r3,MSR_PR; \
786 LOAD_MSR_KERNEL(r10,MSR_KERNEL); \
787 bne user_exc_return; \
794 mtspr SPRN_XER,r10; \
796 PPC405_ERR77(0,r1); \
797 stwcx. r0,0,r1; /* to clear the reservation */ \
802 PPC_40x_TURN_OFF_MSR_DR; \
805 mtspr SPRN_DEAR,r9; \
806 mtspr SPRN_ESR,r10; \
809 mtspr exc_lvl_srr0,r11; \
810 mtspr exc_lvl_srr1,r12; \
818 b .; /* prevent prefetch past exc_lvl_rfi */
820 .globl ret_from_crit_exc
822 RET_FROM_EXC_LEVEL(SPRN_CSRR0, SPRN_CSRR1, RFCI)
825 .globl ret_from_debug_exc
827 RET_FROM_EXC_LEVEL(SPRN_DSRR0, SPRN_DSRR1, RFDI)
829 .globl ret_from_mcheck_exc
831 RET_FROM_EXC_LEVEL(SPRN_MCSRR0, SPRN_MCSRR1, RFMCI)
832 #endif /* CONFIG_BOOKE */
835 * Load the DBCR0 value for a task that is being ptraced,
836 * having first saved away the global DBCR0. Note that r0
837 * has the dbcr0 value to set upon entry to this.
840 mfmsr r10 /* first disable debug exceptions */
841 rlwinm r10,r10,0,~MSR_DE
845 lis r11,global_dbcr0@ha
846 addi r11,r11,global_dbcr0@l
853 mtspr SPRN_DBSR,r11 /* clear all pending debug events */
857 #endif /* !(CONFIG_4xx || CONFIG_BOOKE) */
859 do_work: /* r10 contains MSR_KERNEL here */
860 andi. r0,r9,_TIF_NEED_RESCHED
863 do_resched: /* r10 contains MSR_KERNEL here */
866 MTMSRD(r10) /* hard-enable interrupts */
869 LOAD_MSR_KERNEL(r10,MSR_KERNEL)
871 MTMSRD(r10) /* disable interrupts */
874 andi. r0,r9,_TIF_NEED_RESCHED
876 andi. r0,r9,_TIF_SIGPENDING
878 do_user_signal: /* r10 contains MSR_KERNEL here */
881 MTMSRD(r10) /* hard-enable interrupts */
882 /* save r13-r31 in the exception frame, if not already done */
890 addi r4,r1,STACK_FRAME_OVERHEAD
896 * We come here when we are at the end of handling an exception
897 * that occurred at a place where taking an exception will lose
898 * state information, such as the contents of SRR0 and SRR1.
901 lis r10,exc_exit_restart_end@ha
902 addi r10,r10,exc_exit_restart_end@l
905 lis r11,exc_exit_restart@ha
906 addi r11,r11,exc_exit_restart@l
909 lis r10,ee_restarts@ha
910 lwz r12,ee_restarts@l(r10)
912 stw r12,ee_restarts@l(r10)
913 mr r12,r11 /* restart at exc_exit_restart */
915 3: /* OK, we can't recover, kill this process */
916 /* but the 601 doesn't implement the RI bit, so assume it's OK */
919 END_FTR_SECTION_IFSET(CPU_FTR_601)
926 4: addi r3,r1,STACK_FRAME_OVERHEAD
927 bl nonrecoverable_exception
928 /* shouldn't return */