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