xref: /linux/arch/loongarch/kernel/entry.S (revision 100c85421b52e41269ada88f7d71a6b8a06c7a11)
1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
4 *
5 * Derived from MIPS:
6 * Copyright (C) 1994 - 2000, 2001, 2003 Ralf Baechle
7 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
8 * Copyright (C) 2001 MIPS Technologies, Inc.
9 */
10
11#include <asm/asm.h>
12#include <asm/asmmacro.h>
13#include <asm/loongarch.h>
14#include <asm/regdef.h>
15#include <asm/stackframe.h>
16#include <asm/thread_info.h>
17#include <asm/unwind_hints.h>
18
19	.text
20	.cfi_sections	.debug_frame
21	.align	5
22SYM_CODE_START(handle_syscall)
23	UNWIND_HINT_UNDEFINED
24	csrrd		t0, PERCPU_BASE_KS
25	la.pcrel	t1, kernelsp
26	add.d		t1, t1, t0
27	move		t2, sp
28	ld.d		sp, t1, 0
29
30	addi.d		sp, sp, -PT_SIZE
31	cfi_st		t2, PT_R3
32	cfi_rel_offset	sp, PT_R3
33	st.d		zero, sp, PT_R0
34	csrrd		t2, LOONGARCH_CSR_PRMD
35	st.d		t2, sp, PT_PRMD
36	csrrd		t2, LOONGARCH_CSR_CRMD
37	st.d		t2, sp, PT_CRMD
38	csrrd		t2, LOONGARCH_CSR_EUEN
39	st.d		t2, sp, PT_EUEN
40	csrrd		t2, LOONGARCH_CSR_ECFG
41	st.d		t2, sp, PT_ECFG
42	csrrd		t2, LOONGARCH_CSR_ESTAT
43	st.d		t2, sp, PT_ESTAT
44	cfi_st		ra, PT_R1
45	cfi_st		a0, PT_R4
46	cfi_st		a1, PT_R5
47	cfi_st		a2, PT_R6
48	cfi_st		a3, PT_R7
49	cfi_st		a4, PT_R8
50	cfi_st		a5, PT_R9
51	cfi_st		a6, PT_R10
52	cfi_st		a7, PT_R11
53	csrrd		ra, LOONGARCH_CSR_ERA
54	st.d		ra, sp, PT_ERA
55	cfi_rel_offset	ra, PT_ERA
56
57	cfi_st		tp, PT_R2
58	cfi_st		u0, PT_R21
59	cfi_st		fp, PT_R22
60
61	SAVE_STATIC
62	UNWIND_HINT_REGS
63
64#ifdef CONFIG_KGDB
65	li.w		t1, CSR_CRMD_WE
66	csrxchg		t1, t1, LOONGARCH_CSR_CRMD
67#endif
68
69	move		u0, t0
70	li.d		tp, ~_THREAD_MASK
71	and		tp, tp, sp
72
73	move		a0, sp
74	bl		do_syscall
75
76	RESTORE_ALL_AND_RET
77SYM_CODE_END(handle_syscall)
78_ASM_NOKPROBE(handle_syscall)
79
80SYM_CODE_START(ret_from_fork)
81	UNWIND_HINT_REGS
82	bl		schedule_tail		# a0 = struct task_struct *prev
83	move		a0, sp
84	bl 		syscall_exit_to_user_mode
85	RESTORE_STATIC
86	RESTORE_SOME
87	RESTORE_SP_AND_RET
88SYM_CODE_END(ret_from_fork)
89
90SYM_CODE_START(ret_from_kernel_thread)
91	UNWIND_HINT_REGS
92	bl		schedule_tail		# a0 = struct task_struct *prev
93	move		a0, s1
94	jirl		ra, s0, 0
95	move		a0, sp
96	bl		syscall_exit_to_user_mode
97	RESTORE_STATIC
98	RESTORE_SOME
99	RESTORE_SP_AND_RET
100SYM_CODE_END(ret_from_kernel_thread)
101