blob: 3b7c40a2e3e1a42013bb5436cff93c998c371bc4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * User address space access functions.
3 *
4 * Copyright 1997 Andi Kleen <ak@muc.de>
5 * Copyright 1997 Linus Torvalds
6 * Copyright 2002 Andi Kleen <ak@suse.de>
7 */
Paul Gortmakere6830142016-07-13 20:18:57 -04008#include <linux/export.h>
Andy Lutomirski13d4ea02016-07-14 13:22:57 -07009#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
11/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Zero Userspace
13 */
14
15unsigned long __clear_user(void __user *addr, unsigned long size)
16{
17 long __d0;
Nick Piggin3ee1afa2008-09-10 13:37:17 +020018 might_fault();
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 /* no memory constraint because it doesn't change any memory gcc knows
20 about */
H. Peter Anvin63bcff22012-09-21 12:43:12 -070021 stac();
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 asm volatile(
23 " testq %[size8],%[size8]\n"
24 " jz 4f\n"
25 "0: movq %[zero],(%[dst])\n"
26 " addq %[eight],%[dst]\n"
27 " decl %%ecx ; jnz 0b\n"
28 "4: movq %[size1],%%rcx\n"
29 " testl %%ecx,%%ecx\n"
30 " jz 2f\n"
31 "1: movb %b[zero],(%[dst])\n"
32 " incq %[dst]\n"
33 " decl %%ecx ; jnz 1b\n"
34 "2:\n"
35 ".section .fixup,\"ax\"\n"
36 "3: lea 0(%[size1],%[size8],8),%[size8]\n"
37 " jmp 2b\n"
38 ".previous\n"
H. Peter Anvin8da804f2008-02-04 16:47:57 +010039 _ASM_EXTABLE(0b,3b)
40 _ASM_EXTABLE(1b,2b)
Andi Kleene0a96122009-01-16 15:22:11 +010041 : [size8] "=&c"(size), [dst] "=&D" (__d0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 : [size1] "r"(size & 7), "[size8]" (size / 8), "[dst]"(addr),
43 [zero] "r" (0UL), [eight] "r" (8UL));
H. Peter Anvin63bcff22012-09-21 12:43:12 -070044 clac();
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 return size;
46}
Andi Kleen2ee60e172006-06-26 13:59:44 +020047EXPORT_SYMBOL(__clear_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49unsigned long clear_user(void __user *to, unsigned long n)
50{
51 if (access_ok(VERIFY_WRITE, to, n))
52 return __clear_user(to, n);
53 return n;
54}
Andi Kleen2ee60e172006-06-26 13:59:44 +020055EXPORT_SYMBOL(clear_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Vitaly Mayatskikh11295852008-07-02 15:48:21 +020057/*
58 * Try to copy last bytes and clear the rest if needed.
59 * Since protection fault in copy_from/to_user is not a normal situation,
60 * it is not necessary to optimize tail handling.
61 */
Andi Kleen277d5b42013-08-05 15:02:43 -070062__visible unsigned long
Linus Torvaldscae2a172015-04-06 10:26:17 -070063copy_user_handle_tail(char *to, char *from, unsigned len)
Vitaly Mayatskikh11295852008-07-02 15:48:21 +020064{
CQ Tang66db3fe2013-03-18 11:02:21 -040065 for (; len; --len, to++) {
Linus Torvaldscae2a172015-04-06 10:26:17 -070066 char c;
67
Vitaly Mayatskikh11295852008-07-02 15:48:21 +020068 if (__get_user_nocheck(c, from++, sizeof(char)))
69 break;
CQ Tang66db3fe2013-03-18 11:02:21 -040070 if (__put_user_nocheck(c, to, sizeof(char)))
Vitaly Mayatskikh11295852008-07-02 15:48:21 +020071 break;
72 }
H. Peter Anvin63bcff22012-09-21 12:43:12 -070073 clac();
Vitaly Mayatskikh11295852008-07-02 15:48:21 +020074 return len;
75}