GNU Linux-libre 4.4.284-gnu1
[releases.git] / arch / arc / kernel / entry.S
1 /*
2  * Common Low Level Interrupts/Traps/Exceptions(non-TLB) Handling for ARC
3  * (included from entry-<isa>.S
4  *
5  * Copyright (C) 2014-15 Synopsys, Inc. (www.synopsys.com)
6  * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 /*------------------------------------------------------------------
14  *    Function                            ABI
15  *------------------------------------------------------------------
16  *
17  *  Arguments                           r0 - r7
18  *  Caller Saved Registers              r0 - r12
19  *  Callee Saved Registers              r13- r25
20  *  Global Pointer (gp)                 r26
21  *  Frame Pointer (fp)                  r27
22  *  Stack Pointer (sp)                  r28
23  *  Branch link register (blink)        r31
24  *------------------------------------------------------------------
25  */
26
27 ;################### Special Sys Call Wrappers ##########################
28
29 ENTRY(sys_clone_wrapper)
30         SAVE_CALLEE_SAVED_USER
31         bl  @sys_clone
32         DISCARD_CALLEE_SAVED_USER
33
34         GET_CURR_THR_INFO_FLAGS   r10
35         btst r10, TIF_SYSCALL_TRACE
36         bnz  tracesys_exit
37
38         b ret_from_system_call
39 END(sys_clone_wrapper)
40
41 ENTRY(ret_from_fork)
42         ; when the forked child comes here from the __switch_to function
43         ; r0 has the last task pointer.
44         ; put last task in scheduler queue
45         jl   @schedule_tail
46
47         ld   r9, [sp, PT_status32]
48         brne r9, 0, 1f
49
50         jl.d [r14]              ; kernel thread entry point
51         mov  r0, r13            ; (see PF_KTHREAD block in copy_thread)
52
53 1:
54         ; Return to user space
55         ; 1. Any forked task (Reach here via BRne above)
56         ; 2. First ever init task (Reach here via return from JL above)
57         ;    This is the historic "kernel_execve" use-case, to return to init
58         ;    user mode, in a round about way since that is always done from
59         ;    a kernel thread which is executed via JL above but always returns
60         ;    out whenever kernel_execve (now inline do_fork()) is involved
61         b    ret_from_exception
62 END(ret_from_fork)
63
64 #ifdef CONFIG_ARC_DW2_UNWIND
65 ; Workaround for bug 94179 (STAR ):
66 ; Despite -fasynchronous-unwind-tables, linker is not making dwarf2 unwinder
67 ; section (.debug_frame) as loadable. So we force it here.
68 ; This also fixes STAR 9000487933 where the prev-workaround (objcopy --setflag)
69 ; would not work after a clean build due to kernel build system dependencies.
70 .section .debug_frame, "wa",@progbits
71
72 ; Reset to .text as this file is included in entry-<isa>.S
73 .section .text, "ax",@progbits
74 #endif
75
76 ;################### Non TLB Exception Handling #############################
77
78 ; ---------------------------------------------
79 ; Instruction Error Exception Handler
80 ; ---------------------------------------------
81
82 ENTRY(instr_service)
83
84         EXCEPTION_PROLOGUE
85
86         lr  r0, [efa]
87         mov r1, sp
88
89         FAKE_RET_FROM_EXCPN
90
91         bl  do_insterror_or_kprobe
92         b   ret_from_exception
93 END(instr_service)
94
95 ; ---------------------------------------------
96 ; Machine Check Exception Handler
97 ; ---------------------------------------------
98
99 ENTRY(EV_MachineCheck)
100
101         EXCEPTION_PROLOGUE
102
103         lr  r2, [ecr]
104         lr  r0, [efa]
105         mov r1, sp
106
107         ; hardware auto-disables MMU, re-enable it to allow kernel vaddr
108         ; access for say stack unwinding of modules for crash dumps
109         lr      r3, [ARC_REG_PID]
110         or      r3, r3, MMU_ENABLE
111         sr      r3, [ARC_REG_PID]
112
113         lsr     r3, r2, 8
114         bmsk    r3, r3, 7
115         brne    r3, ECR_C_MCHK_DUP_TLB, 1f
116
117         bl      do_tlb_overlap_fault
118         b       ret_from_exception
119
120 1:
121         ; DEAD END: can't do much, display Regs and HALT
122         SAVE_CALLEE_SAVED_USER
123
124         GET_CURR_TASK_FIELD_PTR   TASK_THREAD, r10
125         st  sp, [r10, THREAD_CALLEE_REG]
126
127         j  do_machine_check_fault
128
129 END(EV_MachineCheck)
130
131 ; ---------------------------------------------
132 ; Privilege Violation Exception Handler
133 ; ---------------------------------------------
134 ENTRY(EV_PrivilegeV)
135
136         EXCEPTION_PROLOGUE
137
138         lr  r0, [efa]
139         mov r1, sp
140
141         FAKE_RET_FROM_EXCPN
142
143         bl  do_privilege_fault
144         b   ret_from_exception
145 END(EV_PrivilegeV)
146
147 ; ---------------------------------------------
148 ; Extension Instruction Exception Handler
149 ; ---------------------------------------------
150 ENTRY(EV_Extension)
151
152         EXCEPTION_PROLOGUE
153
154         lr  r0, [efa]
155         mov r1, sp
156
157         FAKE_RET_FROM_EXCPN
158
159         bl  do_extension_fault
160         b   ret_from_exception
161 END(EV_Extension)
162
163 ;################ Trap Handling (Syscall, Breakpoint) ##################
164
165 ; ---------------------------------------------
166 ; syscall Tracing
167 ; ---------------------------------------------
168 tracesys:
169         ; save EFA in case tracer wants the PC of traced task
170         ; using ERET won't work since next-PC has already committed
171         lr  r12, [efa]
172         GET_CURR_TASK_FIELD_PTR   TASK_THREAD, r11
173         st  r12, [r11, THREAD_FAULT_ADDR]       ; thread.fault_address
174
175         ; PRE Sys Call Ptrace hook
176         mov r0, sp                      ; pt_regs needed
177         bl  @syscall_trace_entry
178
179         ; Tracing code now returns the syscall num (orig or modif)
180         mov r8, r0
181
182         ; Do the Sys Call as we normally would.
183         ; Validate the Sys Call number
184         cmp     r8,  NR_syscalls - 1
185         mov.hi  r0, -ENOSYS
186         bhi     tracesys_exit
187
188         ; Restore the sys-call args. Mere invocation of the hook abv could have
189         ; clobbered them (since they are in scratch regs). The tracer could also
190         ; have deliberately changed the syscall args: r0-r7
191         ld  r0, [sp, PT_r0]
192         ld  r1, [sp, PT_r1]
193         ld  r2, [sp, PT_r2]
194         ld  r3, [sp, PT_r3]
195         ld  r4, [sp, PT_r4]
196         ld  r5, [sp, PT_r5]
197         ld  r6, [sp, PT_r6]
198         ld  r7, [sp, PT_r7]
199         ld.as   r9, [sys_call_table, r8]
200         jl      [r9]        ; Entry into Sys Call Handler
201
202 tracesys_exit:
203         st  r0, [sp, PT_r0]     ; sys call return value in pt_regs
204
205         ;POST Sys Call Ptrace Hook
206         bl  @syscall_trace_exit
207         b   ret_from_exception ; NOT ret_from_system_call at is saves r0 which
208         ; we'd done before calling post hook above
209
210 ; ---------------------------------------------
211 ; Breakpoint TRAP
212 ; ---------------------------------------------
213 trap_with_param:
214
215         ; stop_pc info by gdb needs this info
216         lr  r0, [efa]
217         mov r1, sp
218
219         ; Now that we have read EFA, it is safe to do "fake" rtie
220         ;   and get out of CPU exception mode
221         FAKE_RET_FROM_EXCPN
222
223         ; Save callee regs in case gdb wants to have a look
224         ; SP will grow up by size of CALLEE Reg-File
225         ; NOTE: clobbers r12
226         SAVE_CALLEE_SAVED_USER
227
228         ; save location of saved Callee Regs @ thread_struct->pc
229         GET_CURR_TASK_FIELD_PTR   TASK_THREAD, r10
230         st  sp, [r10, THREAD_CALLEE_REG]
231
232         ; Call the trap handler
233         bl  do_non_swi_trap
234
235         ; unwind stack to discard Callee saved Regs
236         DISCARD_CALLEE_SAVED_USER
237
238         b   ret_from_exception
239
240 ; ---------------------------------------------
241 ; syscall TRAP
242 ; ABI: (r0-r7) upto 8 args, (r8) syscall number
243 ; ---------------------------------------------
244
245 ENTRY(EV_Trap)
246
247         EXCEPTION_PROLOGUE
248
249         ;============ TRAP 1   :breakpoints
250         ; Check ECR for trap with arg (PROLOGUE ensures r9 has ECR)
251         bmsk.f 0, r9, 7
252         bnz    trap_with_param
253
254         ;============ TRAP  (no param): syscall top level
255
256         ; First return from Exception to pure K mode (Exception/IRQs renabled)
257         FAKE_RET_FROM_EXCPN
258
259         ; If syscall tracing ongoing, invoke pre-post-hooks
260         GET_CURR_THR_INFO_FLAGS   r10
261         btst r10, TIF_SYSCALL_TRACE
262         bnz tracesys  ; this never comes back
263
264         ;============ Normal syscall case
265
266         ; syscall num shd not exceed the total system calls avail
267         cmp     r8,  NR_syscalls - 1
268         mov.hi  r0, -ENOSYS
269         bhi     ret_from_system_call
270
271         ; Offset into the syscall_table and call handler
272         ld.as   r9,[sys_call_table, r8]
273         jl      [r9]        ; Entry into Sys Call Handler
274
275         ; fall through to ret_from_system_call
276 END(EV_Trap)
277
278 ENTRY(ret_from_system_call)
279
280         st  r0, [sp, PT_r0]     ; sys call return value in pt_regs
281
282         ; fall through yet again to ret_from_exception
283
284 ;############# Return from Intr/Excp/Trap (Linux Specifics) ##############
285 ;
286 ; If ret to user mode do we need to handle signals, schedule() et al.
287
288 ENTRY(ret_from_exception)
289
290         ; Pre-{IRQ,Trap,Exception} K/U mode from pt_regs->status32
291         ld  r8, [sp, PT_status32]   ; returning to User/Kernel Mode
292
293         bbit0  r8, STATUS_U_BIT, resume_kernel_mode
294
295         ; Before returning to User mode check-for-and-complete any pending work
296         ; such as rescheduling/signal-delivery etc.
297 resume_user_mode_begin:
298
299         ; Disable IRQs to ensures that chk for pending work itself is atomic
300         ; (and we don't end up missing a NEED_RESCHED/SIGPENDING due to an
301         ; interim IRQ).
302         IRQ_DISABLE     r10
303
304         ; Fast Path return to user mode if no pending work
305         GET_CURR_THR_INFO_FLAGS   r9
306         and.f  0,  r9, _TIF_WORK_MASK
307         bz     .Lrestore_regs
308
309         ; --- (Slow Path #1) task preemption ---
310         bbit0  r9, TIF_NEED_RESCHED, .Lchk_pend_signals
311         mov    blink, resume_user_mode_begin  ; tail-call to U mode ret chks
312         j      @schedule        ; BTST+Bnz causes relo error in link
313
314 .Lchk_pend_signals:
315         IRQ_ENABLE      r10
316
317         ; --- (Slow Path #2) pending signal  ---
318         mov r0, sp      ; pt_regs for arg to do_signal()/do_notify_resume()
319
320         GET_CURR_THR_INFO_FLAGS   r9
321         bbit0  r9, TIF_SIGPENDING, .Lchk_notify_resume
322
323         ; Normal Trap/IRQ entry only saves Scratch (caller-saved) regs
324         ; in pt_reg since the "C" ABI (kernel code) will automatically
325         ; save/restore callee-saved regs.
326         ;
327         ; However, here we need to explicitly save callee regs because
328         ; (i)  If this signal causes coredump - full regfile needed
329         ; (ii) If signal is SIGTRAP/SIGSTOP, task is being traced thus
330         ;      tracer might call PEEKUSR(CALLEE reg)
331         ;
332         ; NOTE: SP will grow up by size of CALLEE Reg-File
333         SAVE_CALLEE_SAVED_USER          ; clobbers r12
334
335         ; save location of saved Callee Regs @ thread_struct->callee
336         GET_CURR_TASK_FIELD_PTR   TASK_THREAD, r10
337         st  sp, [r10, THREAD_CALLEE_REG]
338
339         bl  @do_signal
340
341         ; Ideally we want to discard the Callee reg above, however if this was
342         ; a tracing signal, tracer could have done a POKEUSR(CALLEE reg)
343         RESTORE_CALLEE_SAVED_USER
344
345         b      resume_user_mode_begin   ; loop back to start of U mode ret
346
347         ; --- (Slow Path #3) notify_resume ---
348 .Lchk_notify_resume:
349         btst   r9, TIF_NOTIFY_RESUME
350         blnz   @do_notify_resume
351         b      resume_user_mode_begin   ; unconditionally back to U mode ret chks
352                                         ; for single exit point from this block
353
354 resume_kernel_mode:
355
356         ; Disable Interrupts from this point on
357         ; CONFIG_PREEMPT: This is a must for preempt_schedule_irq()
358         ; !CONFIG_PREEMPT: To ensure restore_regs is intr safe
359         IRQ_DISABLE     r9
360
361 #ifdef CONFIG_PREEMPT
362
363         ; Can't preempt if preemption disabled
364         GET_CURR_THR_INFO_FROM_SP   r10
365         ld  r8, [r10, THREAD_INFO_PREEMPT_COUNT]
366         brne  r8, 0, .Lrestore_regs
367
368         ; check if this task's NEED_RESCHED flag set
369         ld  r9, [r10, THREAD_INFO_FLAGS]
370         bbit0  r9, TIF_NEED_RESCHED, .Lrestore_regs
371
372         ; Invoke PREEMPTION
373         jl      preempt_schedule_irq
374
375         ; preempt_schedule_irq() always returns with IRQ disabled
376 #endif
377
378         b       .Lrestore_regs
379
380 ##### DONT ADD CODE HERE - .Lrestore_regs actually follows in entry-<isa>.S
381