xref: /linux/arch/loongarch/kernel/entry.S (revision e7d759f31ca295d589f7420719c311870bb3166f)
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
18	.text
19	.cfi_sections	.debug_frame
20	.align	5
21SYM_CODE_START(handle_syscall)
22	csrrd		t0, PERCPU_BASE_KS
23	la.pcrel	t1, kernelsp
24	add.d		t1, t1, t0
25	move		t2, sp
26	ld.d		sp, t1, 0
27
28	addi.d		sp, sp, -PT_SIZE
29	cfi_st		t2, PT_R3
30	cfi_rel_offset	sp, PT_R3
31	st.d		zero, sp, PT_R0
32	csrrd		t2, LOONGARCH_CSR_PRMD
33	st.d		t2, sp, PT_PRMD
34	csrrd		t2, LOONGARCH_CSR_CRMD
35	st.d		t2, sp, PT_CRMD
36	csrrd		t2, LOONGARCH_CSR_EUEN
37	st.d		t2, sp, PT_EUEN
38	csrrd		t2, LOONGARCH_CSR_ECFG
39	st.d		t2, sp, PT_ECFG
40	csrrd		t2, LOONGARCH_CSR_ESTAT
41	st.d		t2, sp, PT_ESTAT
42	cfi_st		ra, PT_R1
43	cfi_st		a0, PT_R4
44	cfi_st		a1, PT_R5
45	cfi_st		a2, PT_R6
46	cfi_st		a3, PT_R7
47	cfi_st		a4, PT_R8
48	cfi_st		a5, PT_R9
49	cfi_st		a6, PT_R10
50	cfi_st		a7, PT_R11
51	csrrd		ra, LOONGARCH_CSR_ERA
52	st.d		ra, sp, PT_ERA
53	cfi_rel_offset	ra, PT_ERA
54
55	cfi_st		tp, PT_R2
56	cfi_st		u0, PT_R21
57	cfi_st		fp, PT_R22
58
59	SAVE_STATIC
60
61#ifdef CONFIG_KGDB
62	li.w		t1, CSR_CRMD_WE
63	csrxchg		t1, t1, LOONGARCH_CSR_CRMD
64#endif
65
66	move		u0, t0
67	li.d		tp, ~_THREAD_MASK
68	and		tp, tp, sp
69
70	move		a0, sp
71	bl		do_syscall
72
73	RESTORE_ALL_AND_RET
74SYM_CODE_END(handle_syscall)
75_ASM_NOKPROBE(handle_syscall)
76
77SYM_CODE_START(ret_from_fork)
78	bl		schedule_tail		# a0 = struct task_struct *prev
79	move		a0, sp
80	bl 		syscall_exit_to_user_mode
81	RESTORE_STATIC
82	RESTORE_SOME
83	RESTORE_SP_AND_RET
84SYM_CODE_END(ret_from_fork)
85
86SYM_CODE_START(ret_from_kernel_thread)
87	bl		schedule_tail		# a0 = struct task_struct *prev
88	move		a0, s1
89	jirl		ra, s0, 0
90	move		a0, sp
91	bl		syscall_exit_to_user_mode
92	RESTORE_STATIC
93	RESTORE_SOME
94	RESTORE_SP_AND_RET
95SYM_CODE_END(ret_from_kernel_thread)
96