Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Jeff Dike | ae2587e | 2007-10-16 01:26:57 -0700 | [diff] [blame] | 2 | * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
| 6 | #ifndef __ARCH_UM_UACCESS_H |
| 7 | #define __ARCH_UM_UACCESS_H |
| 8 | |
Jeff Dike | 8192ab4 | 2008-02-04 22:30:53 -0800 | [diff] [blame] | 9 | #include <asm/elf.h> |
| 10 | #include <asm/fixmap.h> |
| 11 | #include "sysdep/archsetjmp.h" |
Pekka J Enberg | bf001b2 | 2005-12-12 00:37:16 -0800 | [diff] [blame] | 12 | |
Paolo 'Blaisorblade' Giarrusso | 7a59061 | 2005-11-13 16:07:13 -0800 | [diff] [blame] | 13 | #define __under_task_size(addr, size) \ |
| 14 | (((unsigned long) (addr) < TASK_SIZE) && \ |
Jeff Dike | ae2587e | 2007-10-16 01:26:57 -0700 | [diff] [blame] | 15 | (((unsigned long) (addr) + (size)) < TASK_SIZE)) |
Paolo 'Blaisorblade' Giarrusso | 7a59061 | 2005-11-13 16:07:13 -0800 | [diff] [blame] | 16 | |
| 17 | #define __access_ok_vsyscall(type, addr, size) \ |
| 18 | ((type == VERIFY_READ) && \ |
| 19 | ((unsigned long) (addr) >= FIXADDR_USER_START) && \ |
| 20 | ((unsigned long) (addr) + (size) <= FIXADDR_USER_END) && \ |
| 21 | ((unsigned long) (addr) + (size) >= (unsigned long)(addr))) |
| 22 | |
| 23 | #define __addr_range_nowrap(addr, size) \ |
| 24 | ((unsigned long) (addr) <= ((unsigned long) (addr) + (size))) |
| 25 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #define access_ok(type, addr, size) \ |
Paolo 'Blaisorblade' Giarrusso | 7a59061 | 2005-11-13 16:07:13 -0800 | [diff] [blame] | 27 | (__addr_range_nowrap(addr, size) && \ |
| 28 | (__under_task_size(addr, size) || \ |
| 29 | __access_ok_vsyscall(type, addr, size) || \ |
Jeff Dike | 6aa802c | 2007-10-16 01:26:56 -0700 | [diff] [blame] | 30 | segment_eq(get_fs(), KERNEL_DS))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
Jeff Dike | 6aa802c | 2007-10-16 01:26:56 -0700 | [diff] [blame] | 32 | extern int copy_from_user(void *to, const void __user *from, int n); |
| 33 | extern int copy_to_user(void __user *to, const void *from, int n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
Jeff Dike | fab95c5 | 2007-10-16 01:27:05 -0700 | [diff] [blame] | 35 | extern int __do_copy_to_user(void *to, const void *from, int n, |
| 36 | void **fault_addr, jmp_buf **fault_catcher); |
Jeff Dike | fab95c5 | 2007-10-16 01:27:05 -0700 | [diff] [blame] | 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | /* |
| 39 | * strncpy_from_user: - Copy a NUL terminated string from userspace. |
| 40 | * @dst: Destination address, in kernel space. This buffer must be at |
| 41 | * least @count bytes long. |
| 42 | * @src: Source address, in user space. |
| 43 | * @count: Maximum number of bytes to copy, including the trailing NUL. |
| 44 | * |
| 45 | * Copies a NUL-terminated string from userspace to kernel space. |
| 46 | * |
| 47 | * On success, returns the length of the string (not including the trailing |
| 48 | * NUL). |
| 49 | * |
| 50 | * If access to userspace fails, returns -EFAULT (some data may have been |
| 51 | * copied). |
| 52 | * |
| 53 | * If @count is smaller than the length of the string, copies @count bytes |
| 54 | * and returns @count. |
| 55 | */ |
| 56 | |
Jeff Dike | 6aa802c | 2007-10-16 01:26:56 -0700 | [diff] [blame] | 57 | extern int strncpy_from_user(char *dst, const char __user *src, int count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
| 59 | /* |
| 60 | * __clear_user: - Zero a block of memory in user space, with less checking. |
| 61 | * @to: Destination address, in user space. |
| 62 | * @n: Number of bytes to zero. |
| 63 | * |
| 64 | * Zero a block of memory in user space. Caller must check |
| 65 | * the specified block with access_ok() before calling this function. |
| 66 | * |
| 67 | * Returns number of bytes that could not be cleared. |
| 68 | * On success, this will be zero. |
| 69 | */ |
Jeff Dike | 6aa802c | 2007-10-16 01:26:56 -0700 | [diff] [blame] | 70 | extern int __clear_user(void __user *mem, int len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
| 72 | /* |
| 73 | * clear_user: - Zero a block of memory in user space. |
| 74 | * @to: Destination address, in user space. |
| 75 | * @n: Number of bytes to zero. |
| 76 | * |
| 77 | * Zero a block of memory in user space. |
| 78 | * |
| 79 | * Returns number of bytes that could not be cleared. |
| 80 | * On success, this will be zero. |
| 81 | */ |
Jeff Dike | 6aa802c | 2007-10-16 01:26:56 -0700 | [diff] [blame] | 82 | extern int clear_user(void __user *mem, int len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
| 84 | /* |
| 85 | * strlen_user: - Get the size of a string in user space. |
| 86 | * @str: The string to measure. |
| 87 | * @n: The maximum valid length |
| 88 | * |
| 89 | * Get the size of a NUL-terminated string in user space. |
| 90 | * |
| 91 | * Returns the size of the string INCLUDING the terminating NUL. |
| 92 | * On exception, returns 0. |
| 93 | * If the string is too long, returns a value greater than @n. |
| 94 | */ |
Jeff Dike | 6aa802c | 2007-10-16 01:26:56 -0700 | [diff] [blame] | 95 | extern int strnlen_user(const void __user *str, int len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
| 97 | #endif |