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_FUNC_START(handle_syscall)
22	csrrd	t0, PERCPU_BASE_KS
23	la.abs	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	move	u0, t0
62	li.d	tp, ~_THREAD_MASK
63	and	tp, tp, sp
64
65	move	a0, sp
66	bl	do_syscall
67
68	RESTORE_ALL_AND_RET
69SYM_FUNC_END(handle_syscall)
70
71SYM_CODE_START(ret_from_fork)
72	bl	schedule_tail		# a0 = struct task_struct *prev
73	move	a0, sp
74	bl 	syscall_exit_to_user_mode
75	RESTORE_STATIC
76	RESTORE_SOME
77	RESTORE_SP_AND_RET
78SYM_CODE_END(ret_from_fork)
79
80SYM_CODE_START(ret_from_kernel_thread)
81	bl	schedule_tail		# a0 = struct task_struct *prev
82	move	a0, s1
83	jirl	ra, s0, 0
84	move	a0, sp
85	bl	syscall_exit_to_user_mode
86	RESTORE_STATIC
87	RESTORE_SOME
88	RESTORE_SP_AND_RET
89SYM_CODE_END(ret_from_kernel_thread)
90