Ingo Molnar | c33fa9f | 2008-04-17 20:05:36 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * Access kernel memory without faulting. |
| 3 | */ |
| 4 | #include <linux/uaccess.h> |
| 5 | #include <linux/module.h> |
| 6 | #include <linux/mm.h> |
| 7 | |
| 8 | /** |
| 9 | * probe_kernel_read(): safely attempt to read from a location |
| 10 | * @dst: pointer to the buffer that shall take the data |
| 11 | * @src: address to read from |
| 12 | * @size: size of the data chunk |
| 13 | * |
| 14 | * Safely read from address @src to the buffer at @dst. If a kernel fault |
| 15 | * happens, handle that and return -EFAULT. |
| 16 | */ |
| 17 | long probe_kernel_read(void *dst, void *src, size_t size) |
| 18 | { |
| 19 | long ret; |
| 20 | |
| 21 | pagefault_disable(); |
| 22 | ret = __copy_from_user_inatomic(dst, |
| 23 | (__force const void __user *)src, size); |
| 24 | pagefault_enable(); |
| 25 | |
| 26 | return ret ? -EFAULT : 0; |
| 27 | } |
| 28 | EXPORT_SYMBOL_GPL(probe_kernel_read); |
| 29 | |
| 30 | /** |
| 31 | * probe_kernel_write(): safely attempt to write to a location |
| 32 | * @dst: address to write to |
| 33 | * @src: pointer to the data that shall be written |
| 34 | * @size: size of the data chunk |
| 35 | * |
| 36 | * Safely write to address @dst from the buffer at @src. If a kernel fault |
| 37 | * happens, handle that and return -EFAULT. |
| 38 | */ |
| 39 | long probe_kernel_write(void *dst, void *src, size_t size) |
| 40 | { |
| 41 | long ret; |
| 42 | |
| 43 | pagefault_disable(); |
| 44 | ret = __copy_to_user_inatomic((__force void __user *)dst, src, size); |
| 45 | pagefault_enable(); |
| 46 | |
| 47 | return ret ? -EFAULT : 0; |
| 48 | } |
| 49 | EXPORT_SYMBOL_GPL(probe_kernel_write); |