2 * Low Level Interrupts/Traps/Exceptions(non-TLB) Handling for ARC
4 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * vineetg: Feb 2011 (ptrace low level code fixes)
11 * -traced syscall return code (r0) was not saved into pt_regs for restoring
12 * into user reg-file when traded task rets to user space.
13 * -syscalls needing arch-wrappers (mainly for passing sp as pt_regs)
14 * were not invoking post-syscall trace hook (jumping directly into
15 * ret_from_system_call)
18 * -Vector table jumps (@8 bytes) converted into branches (@4 bytes)
19 * -To maintain the slot size of 8 bytes/vector, added nop, which is
20 * not executed at runtime.
22 * vineetg: Nov 2009 (Everything needed for TIF_RESTORE_SIGMASK)
23 * -do_signal()invoked upon TIF_RESTORE_SIGMASK as well
24 * -Wrappers for sys_{,rt_}sigsuspend() nolonger needed as they don't
28 * -In a rare scenario, Process gets a Priv-V exception and gets scheduled
29 * out. Since we don't do FAKE RTIE for Priv-V, CPU excpetion state remains
30 * active (AE bit enabled). This causes a double fault for a subseq valid
31 * exception. Thus FAKE RTIE needed in low level Priv-Violation handler.
32 * Instr Error could also cause similar scenario, so same there as well.
34 * Vineetg: Aug 28th 2008: Bug #94984
35 * -Zero Overhead Loop Context shd be cleared when entering IRQ/EXcp/Trap
36 * Normally CPU does this automatically, however when doing FAKE rtie,
37 * we need to explicitly do this. The problem in macros
38 * FAKE_RET_FROM_EXCPN and FAKE_RET_FROM_EXCPN_LOCK_IRQ was that this bit
39 * was being "CLEARED" rather then "SET". Since it is Loop INHIBIT Bit,
40 * setting it and not clearing it clears ZOL context
42 * Vineetg: May 16th, 2008
43 * - r25 now contains the Current Task when in kernel
45 * Vineetg: Dec 22, 2007
46 * Minor Surgery of Low Level ISR to make it SMP safe
47 * - MMU_SCRATCH0 Reg used for freeing up r9 in Level 1 ISR
48 * - _current_task is made an array of NR_CPUS
49 * - Access of _current_task wrapped inside a macro so that if hardware
50 * team agrees for a dedicated reg, no other code is touched
52 * Amit Bhor, Rahul Trivedi, Kanika Nema, Sameer Dhavale : Codito Tech 2004
55 /*------------------------------------------------------------------
57 *------------------------------------------------------------------
60 * Caller Saved Registers r0 - r12
61 * Callee Saved Registers r13- r25
62 * Global Pointer (gp) r26
63 * Frame Pointer (fp) r27
64 * Stack Pointer (sp) r28
65 * Interrupt link register (ilink1) r29
66 * Interrupt link register (ilink2) r30
67 * Branch link register (blink) r31
68 *------------------------------------------------------------------
73 ;############################ Vector Table #################################
76 #if 1 /* Just in case, build breaks */
84 .section .vector, "ax",@progbits
87 /* Each entry in the vector table must occupy 2 words. Since it is a jump
88 * across sections (.vector to .text) we are gauranteed that 'j somewhere'
89 * will use the 'j limm' form of the intrsuction as long as somewhere is in
90 * a section other than .vector.
93 ; ********* Critical System Events **********************
94 VECTOR res_service ; 0x0, Restart Vector (0x0)
95 VECTOR mem_service ; 0x8, Mem exception (0x1)
96 VECTOR instr_service ; 0x10, Instrn Error (0x2)
98 ; ******************** Device ISRs **********************
99 VECTOR handle_interrupt_level1
101 VECTOR handle_interrupt_level1
103 VECTOR handle_interrupt_level1
105 VECTOR handle_interrupt_level1
108 VECTOR handle_interrupt_level1 ; Other devices
111 /* FOR ARC600: timer = 0x3, uart = 0x8, emac = 0x10 */
113 ; ******************** Exceptions **********************
114 VECTOR EV_MachineCheck ; 0x100, Fatal Machine check (0x20)
115 VECTOR EV_TLBMissI ; 0x108, Intruction TLB miss (0x21)
116 VECTOR EV_TLBMissD ; 0x110, Data TLB miss (0x22)
117 VECTOR EV_TLBProtV ; 0x118, Protection Violation (0x23)
118 ; or Misaligned Access
119 VECTOR EV_PrivilegeV ; 0x120, Privilege Violation (0x24)
120 VECTOR EV_Trap ; 0x128, Trap exception (0x25)
121 VECTOR EV_Extension ; 0x130, Extn Intruction Excp (0x26)
124 VECTOR reserved ; Reserved Exceptions
127 #include <linux/linkage.h> /* ARC_{EXTRY,EXIT} */
128 #include <asm/entry.h> /* SAVE_ALL_{INT1,INT2,TRAP...} */
129 #include <asm/errno.h>
130 #include <asm/arcregs.h>
131 #include <asm/irqflags.h>
133 ;##################### Scratch Mem for IRQ stack switching #############
135 .section .data ; NOT .global
137 .type int1_saved_reg, @object
138 .size int1_saved_reg, 4
142 ; ---------------------------------------------
143 .section .text, "ax",@progbits
145 res_service: ; processor restart
146 flag 0x1 ; not implemented
150 reserved: ; processor restart
151 rtie ; jump to processor initializations
153 ;##################### Interrupt Handling ##############################
155 ; ---------------------------------------------
157 ; ---------------------------------------------
158 ARC_ENTRY handle_interrupt_level1
160 /* free up r9 as scratchpad */
161 st r9, [@int1_saved_reg]
163 ;Which mode (user/kernel) was the system in when intr occured
176 sr r8, [AUX_IRQ_LV12] ; clear bit in Sticky Status Reg
179 ARC_EXIT handle_interrupt_level1
181 ;################### Non TLB Exception Handling #############################
183 ; ---------------------------------------------
184 ; Instruction Error Exception Handler
185 ; ---------------------------------------------
187 ARC_ENTRY instr_service
189 EXCPN_PROLOG_FREEUP_REG r9
201 FAKE_RET_FROM_EXCPN r9
203 bl do_insterror_or_kprobe
205 ARC_EXIT instr_service
207 ; ---------------------------------------------
208 ; Memory Error Exception Handler
209 ; ---------------------------------------------
211 ARC_ENTRY mem_service
213 EXCPN_PROLOG_FREEUP_REG r9
227 ; ---------------------------------------------
228 ; Machine Check Exception Handler
229 ; ---------------------------------------------
231 ARC_ENTRY EV_MachineCheck
233 EXCPN_PROLOG_FREEUP_REG r9
243 brne r0, 0x200100, 1f
244 bl do_tlb_overlap_fault
248 ; DEAD END: can't do much, display Regs and HALT
249 SAVE_CALLEE_SAVED_USER
251 GET_CURR_TASK_FIELD_PTR TASK_THREAD, r10
252 st sp, [r10, THREAD_CALLEE_REG]
254 j do_machine_check_fault
256 ARC_EXIT EV_MachineCheck
258 ; ---------------------------------------------
259 ; Protection Violation Exception Handler
260 ; ---------------------------------------------
262 ARC_ENTRY EV_TLBProtV
264 EXCPN_PROLOG_FREEUP_REG r9
266 ;Which mode (user/kernel) was the system in when Exception occured
272 ;---------(3) Save some more regs-----------------
273 ; vineetg: Mar 6th: Random Seg Fault issue #1
274 ; ecr and efa were not saved in case an Intr sneaks in
280 ; --------(4) Return from CPU Exception Mode ---------
281 ; Fake a rtie, but rtie to next label
282 ; That way, subsequently, do_page_fault ( ) executes in pure kernel
283 ; mode with further Exceptions enabled
285 FAKE_RET_FROM_EXCPN r9
287 ;------ (5) Type of Protection Violation? ----------
289 ; ProtV Hardware Exception is triggered for Access Faults of 2 types
290 ; -Access Violaton (WRITE to READ ONLY Page) - for linux COW
291 ; -Unaligned Access (READ/WRITE on odd boundary)
293 cmp r3, 0x230400 ; Misaligned data access ?
296 ;========= (6a) Access Violation Processing ========
298 mov r1, 0x0 ; if LD exception ? write = 0
299 mov.ne r1, 0x1 ; else write = 1
301 mov r2, r4 ; faulting address
306 ;========== (6b) Non aligned access ============
308 mov r0, r3 ; cause code
309 mov r1, r4 ; faulting address
312 bl do_misaligned_access
317 ; ---------------------------------------------
318 ; Privilege Violation Exception Handler
319 ; ---------------------------------------------
320 ARC_ENTRY EV_PrivilegeV
322 EXCPN_PROLOG_FREEUP_REG r9
333 FAKE_RET_FROM_EXCPN r9
335 bl do_privilege_fault
337 ARC_EXIT EV_PrivilegeV
339 ; ---------------------------------------------
340 ; Extension Instruction Exception Handler
341 ; ---------------------------------------------
342 ARC_ENTRY EV_Extension
344 EXCPN_PROLOG_FREEUP_REG r9
353 bl do_extension_fault
355 ARC_EXIT EV_Extension
357 ;######################### System Call Tracing #########################
360 ; save EFA in case tracer wants the PC of traced task
361 ; using ERET won't work since next-PC has already committed
363 GET_CURR_TASK_FIELD_PTR TASK_THREAD, r11
364 st r12, [r11, THREAD_FAULT_ADDR]
366 ; PRE Sys Call Ptrace hook
367 mov r0, sp ; pt_regs needed
368 bl @syscall_trace_entry
370 ; Tracing code now returns the syscall num (orig or modif)
373 ; Do the Sys Call as we normally would.
374 ; Validate the Sys Call number
379 ; Restore the sys-call args. Mere invocation of the hook abv could have
380 ; clobbered them (since they are in scratch regs). The tracer could also
381 ; have deliberately changed the syscall args: r0-r7
390 ld.as r9, [sys_call_table, r8]
391 jl [r9] ; Entry into Sys Call Handler
394 st r0, [sp, PT_r0] ; sys call return value in pt_regs
396 ;POST Sys Call Ptrace Hook
397 bl @syscall_trace_exit
398 b ret_from_exception ; NOT ret_from_system_call at is saves r0 which
399 ; we'd done before calling post hook above
401 ;################### Break Point TRAP ##########################
403 ; ======= (5b) Trap is due to Break-Point =========
407 ; stop_pc info by gdb needs this info
408 st orig_r8_IS_BRKPT, [sp, PT_orig_r8]
414 ; Now that we have read EFA, its safe to do "fake" rtie
415 ; and get out of CPU exception mode
416 FAKE_RET_FROM_EXCPN r11
418 ; Save callee regs in case gdb wants to have a look
419 ; SP will grow up by size of CALLEE Reg-File
421 SAVE_CALLEE_SAVED_USER
423 ; save location of saved Callee Regs @ thread_struct->pc
424 GET_CURR_TASK_FIELD_PTR TASK_THREAD, r10
425 st sp, [r10, THREAD_CALLEE_REG]
427 ; Call the trap handler
430 ; unwind stack to discard Callee saved Regs
431 DISCARD_CALLEE_SAVED_USER
435 ;##################### Trap Handling ##############################
437 ; EV_Trap caused by TRAP_S and TRAP0 instructions.
438 ;------------------------------------------------------------------
440 ; :parameters in r0-r7.
441 ; :r8 has the system call number
443 ;------------------------------------------------------------------
447 ; Need at least 1 reg to code the early exception prolog
448 EXCPN_PROLOG_FREEUP_REG r9
450 ;Which mode (user/kernel) was the system in when intr occured
456 ;------- (4) What caused the Trap --------------
458 and.f 0, r12, ECR_PARAM_MASK
461 ; ======= (5a) Trap is due to System Call ========
463 ; Before doing anything, return from CPU Exception Mode
464 FAKE_RET_FROM_EXCPN r11
466 ; If syscall tracing ongoing, invoke pre-pos-hooks
467 GET_CURR_THR_INFO_FLAGS r10
468 btst r10, TIF_SYSCALL_TRACE
469 bnz tracesys ; this never comes back
471 ;============ This is normal System Call case ==========
472 ; Sys-call num shd not exceed the total system calls avail
475 bhi ret_from_system_call
477 ; Offset into the syscall_table and call handler
478 ld.as r9,[sys_call_table, r8]
479 jl [r9] ; Entry into Sys Call Handler
481 ; fall through to ret_from_system_call
484 ARC_ENTRY ret_from_system_call
486 st r0, [sp, PT_r0] ; sys call return value in pt_regs
488 ; fall through yet again to ret_from_exception
490 ;############# Return from Intr/Excp/Trap (Linux Specifics) ##############
492 ; If ret to user mode do we need to handle signals, schedule() et al.
494 ARC_ENTRY ret_from_exception
496 ; Pre-{IRQ,Trap,Exception} K/U mode from pt_regs->status32
497 ld r8, [sp, PT_status32] ; returning to User/Kernel Mode
499 #ifdef CONFIG_PREEMPT
500 bbit0 r8, STATUS_U_BIT, resume_kernel_mode
502 bbit0 r8, STATUS_U_BIT, restore_regs
505 ; Before returning to User mode check-for-and-complete any pending work
506 ; such as rescheduling/signal-delivery etc.
507 resume_user_mode_begin:
509 ; Disable IRQs to ensures that chk for pending work itself is atomic
510 ; (and we don't end up missing a NEED_RESCHED/SIGPENDING due to an
514 ; Fast Path return to user mode if no pending work
515 GET_CURR_THR_INFO_FLAGS r9
516 and.f 0, r9, _TIF_WORK_MASK
519 ; --- (Slow Path #1) task preemption ---
520 bbit0 r9, TIF_NEED_RESCHED, .Lchk_pend_signals
521 mov blink, resume_user_mode_begin ; tail-call to U mode ret chks
522 b @schedule ; BTST+Bnz causes relo error in link
527 ; --- (Slow Path #2) pending signal ---
528 mov r0, sp ; pt_regs for arg to do_signal()/do_notify_resume()
530 bbit0 r9, TIF_SIGPENDING, .Lchk_notify_resume
532 ; Normal Trap/IRQ entry only saves Scratch (caller-saved) regs
533 ; in pt_reg since the "C" ABI (kernel code) will automatically
534 ; save/restore callee-saved regs.
536 ; However, here we need to explicitly save callee regs because
537 ; (i) If this signal causes coredump - full regfile needed
538 ; (ii) If signal is SIGTRAP/SIGSTOP, task is being traced thus
539 ; tracer might call PEEKUSR(CALLEE reg)
541 ; NOTE: SP will grow up by size of CALLEE Reg-File
542 SAVE_CALLEE_SAVED_USER ; clobbers r12
544 ; save location of saved Callee Regs @ thread_struct->callee
545 GET_CURR_TASK_FIELD_PTR TASK_THREAD, r10
546 st sp, [r10, THREAD_CALLEE_REG]
550 ; Ideally we want to discard the Callee reg above, however if this was
551 ; a tracing signal, tracer could have done a POKEUSR(CALLEE reg)
552 RESTORE_CALLEE_SAVED_USER
554 b resume_user_mode_begin ; loop back to start of U mode ret
556 ; --- (Slow Path #3) notify_resume ---
558 btst r9, TIF_NOTIFY_RESUME
559 blnz @do_notify_resume
560 b resume_user_mode_begin ; unconditionally back to U mode ret chks
561 ; for single exit point from this block
563 #ifdef CONFIG_PREEMPT
567 ; Can't preempt if preemption disabled
568 GET_CURR_THR_INFO_FROM_SP r10
569 ld r8, [r10, THREAD_INFO_PREEMPT_COUNT]
570 brne r8, 0, restore_regs
572 ; check if this task's NEED_RESCHED flag set
573 ld r9, [r10, THREAD_INFO_FLAGS]
574 bbit0 r9, TIF_NEED_RESCHED, restore_regs
579 bl preempt_schedule_irq
581 ; preempt_schedule_irq() always returns with IRQ disabled
586 ;############# Return from Intr/Excp/Trap (ARC Specifics) ##############
588 ; Restore the saved sys context (common exit-path for EXCPN/IRQ/Trap)
589 ; IRQ shd definitely not happen between now and rtie
593 ; Disable Interrupts while restoring reg-file back
594 ; XXX can this be optimised out
595 IRQ_DISABLE_SAVE r9, r10 ;@r10 has prisitine (pre-disable) copy
597 #ifdef CONFIG_ARC_CURR_IN_REG
599 ; Earlier this used to be only for returning to user mode
600 ; However with 2 levels of IRQ this can also happen even if
603 brhs r9, VMALLOC_START, 8f
608 ; Restore REG File. In case multiple Events outstanding,
609 ; use the same priorty as rtie: EXCPN, L2 IRQ, L1 IRQ, None
610 ; Note that we use realtime STATUS32 (not pt_regs->status32) to
613 ; if Returning from Exception
614 bbit0 r10, STATUS_AE_BIT, not_exception
618 ; Not Exception so maybe Interrupts (Level 1 or 2)
622 bbit0 r10, STATUS_A1_BIT, not_level1_interrupt
630 not_level1_interrupt:
632 ;this case is for syscalls or Exceptions (with fake rtie)
635 debug_marker_syscall:
638 ARC_EXIT ret_from_exception
640 ARC_ENTRY ret_from_fork
641 ; when the forked child comes here from the __switch_to function
642 ; r0 has the last task pointer.
643 ; put last task in scheduler queue
646 ; If kernel thread, jump to it's entry-point
647 ld r9, [sp, PT_status32]
651 mov r0, r13 ; arg to payload
654 ; special case of kernel_thread entry point returning back due to
655 ; kernel_execve() - pretend return from syscall to ret to userland
657 ARC_EXIT ret_from_fork
659 ;################### Special Sys Call Wrappers ##########################
661 ; TBD: call do_fork directly from here
662 ARC_ENTRY sys_fork_wrapper
663 SAVE_CALLEE_SAVED_USER
665 DISCARD_CALLEE_SAVED_USER
667 GET_CURR_THR_INFO_FLAGS r10
668 btst r10, TIF_SYSCALL_TRACE
671 b ret_from_system_call
672 ARC_EXIT sys_fork_wrapper
674 ARC_ENTRY sys_vfork_wrapper
675 SAVE_CALLEE_SAVED_USER
677 DISCARD_CALLEE_SAVED_USER
679 GET_CURR_THR_INFO_FLAGS r10
680 btst r10, TIF_SYSCALL_TRACE
683 b ret_from_system_call
684 ARC_EXIT sys_vfork_wrapper
686 ARC_ENTRY sys_clone_wrapper
687 SAVE_CALLEE_SAVED_USER
689 DISCARD_CALLEE_SAVED_USER
691 GET_CURR_THR_INFO_FLAGS r10
692 btst r10, TIF_SYSCALL_TRACE
695 b ret_from_system_call
696 ARC_EXIT sys_clone_wrapper