xref: /linux/arch/loongarch/lib/clear_user.S (revision 9f2c9170934eace462499ba0bfe042cc72900173)
1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
4 */
5
6#include <asm/alternative-asm.h>
7#include <asm/asm.h>
8#include <asm/asmmacro.h>
9#include <asm/asm-extable.h>
10#include <asm/cpu.h>
11#include <asm/export.h>
12#include <asm/regdef.h>
13
14.irp to, 0, 1, 2, 3, 4, 5, 6, 7
15.L_fixup_handle_\to\():
16	addi.d	a0, a1, (\to) * (-8)
17	jr	ra
18.endr
19
20SYM_FUNC_START(__clear_user)
21	/*
22	 * Some CPUs support hardware unaligned access
23	 */
24	ALTERNATIVE	"b __clear_user_generic",	\
25			"b __clear_user_fast", CPU_FEATURE_UAL
26SYM_FUNC_END(__clear_user)
27
28EXPORT_SYMBOL(__clear_user)
29
30/*
31 * unsigned long __clear_user_generic(void *addr, size_t size)
32 *
33 * a0: addr
34 * a1: size
35 */
36SYM_FUNC_START(__clear_user_generic)
37	beqz	a1, 2f
38
391:	st.b	zero, a0, 0
40	addi.d	a0, a0, 1
41	addi.d	a1, a1, -1
42	bgtz	a1, 1b
43
442:	move	a0, a1
45	jr	ra
46
47	_asm_extable 1b, .L_fixup_handle_0
48SYM_FUNC_END(__clear_user_generic)
49
50/*
51 * unsigned long __clear_user_fast(void *addr, unsigned long size)
52 *
53 * a0: addr
54 * a1: size
55 */
56SYM_FUNC_START(__clear_user_fast)
57	beqz	a1, 10f
58
59	ori	a2, zero, 64
60	blt	a1, a2, 9f
61
62	/* set 64 bytes at a time */
631:	st.d	zero, a0, 0
642:	st.d	zero, a0, 8
653:	st.d	zero, a0, 16
664:	st.d	zero, a0, 24
675:	st.d	zero, a0, 32
686:	st.d	zero, a0, 40
697:	st.d	zero, a0, 48
708:	st.d	zero, a0, 56
71
72	addi.d	a0, a0, 64
73	addi.d	a1, a1, -64
74	bge	a1, a2, 1b
75
76	beqz	a1, 10f
77
78	/* set the remaining bytes */
799:	st.b	zero, a0, 0
80	addi.d	a0, a0, 1
81	addi.d	a1, a1, -1
82	bgt	a1, zero, 9b
83
84	/* return */
8510:	move	a0, a1
86	jr	ra
87
88	/* fixup and ex_table */
89	_asm_extable 1b, .L_fixup_handle_0
90	_asm_extable 2b, .L_fixup_handle_1
91	_asm_extable 3b, .L_fixup_handle_2
92	_asm_extable 4b, .L_fixup_handle_3
93	_asm_extable 5b, .L_fixup_handle_4
94	_asm_extable 6b, .L_fixup_handle_5
95	_asm_extable 7b, .L_fixup_handle_6
96	_asm_extable 8b, .L_fixup_handle_7
97	_asm_extable 9b, .L_fixup_handle_0
98SYM_FUNC_END(__clear_user_fast)
99