xref: /linux/arch/x86/lib/putuser.S (revision bb29b957eaaa15be33243550d9ee576c2d601669)
1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * The out-of-line helpers for put_user().
4 *
5 * (C) Copyright 2005 Linus Torvalds
6 * (C) Copyright 2005 Andi Kleen
7 * (C) Copyright 2008 Glauber Costa
8 *
9 * These functions have a non-standard call interface
10 * to make them more efficient, especially as they
11 * return an error value in addition to the "real"
12 * return value.
13 */
14#include <linux/export.h>
15#include <linux/linkage.h>
16#include <linux/objtool.h>
17#include <asm/thread_info.h>
18#include <asm/errno.h>
19#include <asm/asm.h>
20#include <asm/smap.h>
21
22/*
23 * __put_user_X
24 *
25 * Inputs:	%eax[:%edx] contains the data
26 *		%ecx contains the address
27 *
28 * Outputs:	%ecx is error code (0 or -EFAULT)
29 *
30 * Clobbers:	%ebx needed for task pointer
31 *
32 * These functions should not modify any other registers,
33 * as they get called from within inline assembly.
34 */
35
36.macro check_range size:req
37.if IS_ENABLED(CONFIG_X86_64)
38	mov %rcx, %rbx
39	sar $63, %rbx
40	or %rbx, %rcx
41.else
42	cmp $TASK_SIZE_MAX-\size+1, %ecx
43	jae .Lbad_put_user
44.endif
45.endm
46
47.text
48SYM_FUNC_START(__put_user_1)
49	ANNOTATE_NOENDBR
50	check_range size=1
51	ASM_STAC
521:	movb %al,(%_ASM_CX)
53	xor %ecx,%ecx
54	ASM_CLAC
55	RET
56SYM_FUNC_END(__put_user_1)
57EXPORT_SYMBOL(__put_user_1)
58
59SYM_FUNC_START(__put_user_2)
60	ANNOTATE_NOENDBR
61	check_range size=2
62	ASM_STAC
632:	movw %ax,(%_ASM_CX)
64	xor %ecx,%ecx
65	ASM_CLAC
66	RET
67SYM_FUNC_END(__put_user_2)
68EXPORT_SYMBOL(__put_user_2)
69
70SYM_FUNC_START(__put_user_4)
71	ANNOTATE_NOENDBR
72	check_range size=4
73	ASM_STAC
743:	movl %eax,(%_ASM_CX)
75	xor %ecx,%ecx
76	ASM_CLAC
77	RET
78SYM_FUNC_END(__put_user_4)
79EXPORT_SYMBOL(__put_user_4)
80
81SYM_FUNC_START(__put_user_8)
82	ANNOTATE_NOENDBR
83	check_range size=8
84	ASM_STAC
854:	mov %_ASM_AX,(%_ASM_CX)
86#ifdef CONFIG_X86_32
875:	movl %edx,4(%_ASM_CX)
88#endif
89	xor %ecx,%ecx
90	ASM_CLAC
91	RET
92SYM_FUNC_END(__put_user_8)
93EXPORT_SYMBOL(__put_user_8)
94
95SYM_CODE_START_LOCAL(__put_user_handle_exception)
96	ASM_CLAC
97.Lbad_put_user:
98	movl $-EFAULT,%ecx
99	RET
100SYM_CODE_END(__put_user_handle_exception)
101
102	_ASM_EXTABLE_UA(1b, __put_user_handle_exception)
103	_ASM_EXTABLE_UA(2b, __put_user_handle_exception)
104	_ASM_EXTABLE_UA(3b, __put_user_handle_exception)
105	_ASM_EXTABLE_UA(4b, __put_user_handle_exception)
106#ifdef CONFIG_X86_32
107	_ASM_EXTABLE_UA(5b, __put_user_handle_exception)
108#endif
109