Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
Andi Kleen | 2ee60e17 | 2006-06-26 13:59:44 +0200 | [diff] [blame] | 8 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <asm/uaccess.h> |
| 10 | |
| 11 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * Zero Userspace |
| 13 | */ |
| 14 | |
| 15 | unsigned long __clear_user(void __user *addr, unsigned long size) |
| 16 | { |
| 17 | long __d0; |
Nick Piggin | 3ee1afa | 2008-09-10 13:37:17 +0200 | [diff] [blame] | 18 | might_fault(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | /* no memory constraint because it doesn't change any memory gcc knows |
| 20 | about */ |
| 21 | asm volatile( |
| 22 | " testq %[size8],%[size8]\n" |
| 23 | " jz 4f\n" |
| 24 | "0: movq %[zero],(%[dst])\n" |
| 25 | " addq %[eight],%[dst]\n" |
| 26 | " decl %%ecx ; jnz 0b\n" |
| 27 | "4: movq %[size1],%%rcx\n" |
| 28 | " testl %%ecx,%%ecx\n" |
| 29 | " jz 2f\n" |
| 30 | "1: movb %b[zero],(%[dst])\n" |
| 31 | " incq %[dst]\n" |
| 32 | " decl %%ecx ; jnz 1b\n" |
| 33 | "2:\n" |
| 34 | ".section .fixup,\"ax\"\n" |
| 35 | "3: lea 0(%[size1],%[size8],8),%[size8]\n" |
| 36 | " jmp 2b\n" |
| 37 | ".previous\n" |
H. Peter Anvin | 8da804f | 2008-02-04 16:47:57 +0100 | [diff] [blame] | 38 | _ASM_EXTABLE(0b,3b) |
| 39 | _ASM_EXTABLE(1b,2b) |
Andi Kleen | e0a9612 | 2009-01-16 15:22:11 +0100 | [diff] [blame] | 40 | : [size8] "=&c"(size), [dst] "=&D" (__d0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | : [size1] "r"(size & 7), "[size8]" (size / 8), "[dst]"(addr), |
| 42 | [zero] "r" (0UL), [eight] "r" (8UL)); |
| 43 | return size; |
| 44 | } |
Andi Kleen | 2ee60e17 | 2006-06-26 13:59:44 +0200 | [diff] [blame] | 45 | EXPORT_SYMBOL(__clear_user); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
| 47 | unsigned long clear_user(void __user *to, unsigned long n) |
| 48 | { |
| 49 | if (access_ok(VERIFY_WRITE, to, n)) |
| 50 | return __clear_user(to, n); |
| 51 | return n; |
| 52 | } |
Andi Kleen | 2ee60e17 | 2006-06-26 13:59:44 +0200 | [diff] [blame] | 53 | EXPORT_SYMBOL(clear_user); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | /* |
| 56 | * Return the size of a string (including the ending 0) |
| 57 | * |
| 58 | * Return 0 on exception, a value greater than N if too long |
| 59 | */ |
| 60 | |
Jan Beulich | 5f1d189 | 2006-01-11 22:46:48 +0100 | [diff] [blame] | 61 | long __strnlen_user(const char __user *s, long n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | { |
| 63 | long res = 0; |
| 64 | char c; |
| 65 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | while (1) { |
| 67 | if (res>n) |
| 68 | return n+1; |
| 69 | if (__get_user(c, s)) |
| 70 | return 0; |
| 71 | if (!c) |
| 72 | return res+1; |
| 73 | res++; |
| 74 | s++; |
| 75 | } |
| 76 | } |
Andi Kleen | 2ee60e17 | 2006-06-26 13:59:44 +0200 | [diff] [blame] | 77 | EXPORT_SYMBOL(__strnlen_user); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
Jan Beulich | 5f1d189 | 2006-01-11 22:46:48 +0100 | [diff] [blame] | 79 | long strnlen_user(const char __user *s, long n) |
| 80 | { |
Linus Torvalds | 9063c61 | 2009-06-20 15:40:00 -0700 | [diff] [blame] | 81 | if (!access_ok(VERIFY_READ, s, 1)) |
Jan Beulich | 5f1d189 | 2006-01-11 22:46:48 +0100 | [diff] [blame] | 82 | return 0; |
| 83 | return __strnlen_user(s, n); |
| 84 | } |
Andi Kleen | 2ee60e17 | 2006-06-26 13:59:44 +0200 | [diff] [blame] | 85 | EXPORT_SYMBOL(strnlen_user); |
Jan Beulich | 5f1d189 | 2006-01-11 22:46:48 +0100 | [diff] [blame] | 86 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | long strlen_user(const char __user *s) |
| 88 | { |
| 89 | long res = 0; |
| 90 | char c; |
| 91 | |
| 92 | for (;;) { |
| 93 | if (get_user(c, s)) |
| 94 | return 0; |
| 95 | if (!c) |
| 96 | return res+1; |
| 97 | res++; |
| 98 | s++; |
| 99 | } |
| 100 | } |
Andi Kleen | 2ee60e17 | 2006-06-26 13:59:44 +0200 | [diff] [blame] | 101 | EXPORT_SYMBOL(strlen_user); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
| 103 | unsigned long copy_in_user(void __user *to, const void __user *from, unsigned len) |
| 104 | { |
| 105 | if (access_ok(VERIFY_WRITE, to, len) && access_ok(VERIFY_READ, from, len)) { |
| 106 | return copy_user_generic((__force void *)to, (__force void *)from, len); |
| 107 | } |
| 108 | return len; |
| 109 | } |
Andi Kleen | 2ee60e17 | 2006-06-26 13:59:44 +0200 | [diff] [blame] | 110 | EXPORT_SYMBOL(copy_in_user); |
| 111 | |
Vitaly Mayatskikh | 1129585 | 2008-07-02 15:48:21 +0200 | [diff] [blame] | 112 | /* |
| 113 | * Try to copy last bytes and clear the rest if needed. |
| 114 | * Since protection fault in copy_from/to_user is not a normal situation, |
| 115 | * it is not necessary to optimize tail handling. |
| 116 | */ |
| 117 | unsigned long |
| 118 | copy_user_handle_tail(char *to, char *from, unsigned len, unsigned zerorest) |
| 119 | { |
| 120 | char c; |
| 121 | unsigned zero_len; |
| 122 | |
| 123 | for (; len; --len) { |
| 124 | if (__get_user_nocheck(c, from++, sizeof(char))) |
| 125 | break; |
| 126 | if (__put_user_nocheck(c, to++, sizeof(char))) |
| 127 | break; |
| 128 | } |
| 129 | |
| 130 | for (c = 0, zero_len = len; zerorest && zero_len; --zero_len) |
| 131 | if (__put_user_nocheck(c, to++, sizeof(char))) |
| 132 | break; |
| 133 | return len; |
| 134 | } |