Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* user_fixup.c: Fix up user copy faults. |
| 2 | * |
| 3 | * Copyright (C) 2004 David S. Miller <davem@redhat.com> |
| 4 | */ |
| 5 | |
| 6 | #include <linux/compiler.h> |
| 7 | #include <linux/kernel.h> |
| 8 | #include <linux/string.h> |
| 9 | #include <linux/errno.h> |
Sam Ravnborg | 917c366 | 2009-01-08 16:58:20 -0800 | [diff] [blame] | 10 | #include <linux/module.h> |
| 11 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <asm/uaccess.h> |
| 13 | |
| 14 | /* Calculating the exact fault address when using |
| 15 | * block loads and stores can be very complicated. |
David S. Miller | efdc1e2 | 2005-09-28 21:06:47 -0700 | [diff] [blame] | 16 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | * Instead of trying to be clever and handling all |
| 18 | * of the cases, just fix things up simply here. |
| 19 | */ |
| 20 | |
David S. Miller | efdc1e2 | 2005-09-28 21:06:47 -0700 | [diff] [blame] | 21 | static unsigned long compute_size(unsigned long start, unsigned long size, unsigned long *offset) |
| 22 | { |
| 23 | unsigned long fault_addr = current_thread_info()->fault_address; |
| 24 | unsigned long end = start + size; |
| 25 | |
| 26 | if (fault_addr < start || fault_addr >= end) { |
| 27 | *offset = 0; |
| 28 | } else { |
David S. Miller | b270ee8 | 2008-12-01 02:48:26 -0800 | [diff] [blame] | 29 | *offset = fault_addr - start; |
David S. Miller | efdc1e2 | 2005-09-28 21:06:47 -0700 | [diff] [blame] | 30 | size = end - fault_addr; |
| 31 | } |
| 32 | return size; |
| 33 | } |
| 34 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | unsigned long copy_from_user_fixup(void *to, const void __user *from, unsigned long size) |
| 36 | { |
David S. Miller | efdc1e2 | 2005-09-28 21:06:47 -0700 | [diff] [blame] | 37 | unsigned long offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
David S. Miller | efdc1e2 | 2005-09-28 21:06:47 -0700 | [diff] [blame] | 39 | size = compute_size((unsigned long) from, size, &offset); |
| 40 | if (likely(size)) |
| 41 | memset(to + offset, 0, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | return size; |
| 44 | } |
Sam Ravnborg | 917c366 | 2009-01-08 16:58:20 -0800 | [diff] [blame] | 45 | EXPORT_SYMBOL(copy_from_user_fixup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
| 47 | unsigned long copy_to_user_fixup(void __user *to, const void *from, unsigned long size) |
| 48 | { |
David S. Miller | efdc1e2 | 2005-09-28 21:06:47 -0700 | [diff] [blame] | 49 | unsigned long offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
David S. Miller | efdc1e2 | 2005-09-28 21:06:47 -0700 | [diff] [blame] | 51 | return compute_size((unsigned long) to, size, &offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | } |
Sam Ravnborg | 917c366 | 2009-01-08 16:58:20 -0800 | [diff] [blame] | 53 | EXPORT_SYMBOL(copy_to_user_fixup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | unsigned long copy_in_user_fixup(void __user *to, void __user *from, unsigned long size) |
| 56 | { |
David S. Miller | efdc1e2 | 2005-09-28 21:06:47 -0700 | [diff] [blame] | 57 | unsigned long fault_addr = current_thread_info()->fault_address; |
| 58 | unsigned long start = (unsigned long) to; |
| 59 | unsigned long end = start + size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
David S. Miller | efdc1e2 | 2005-09-28 21:06:47 -0700 | [diff] [blame] | 61 | if (fault_addr >= start && fault_addr < end) |
| 62 | return end - fault_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
David S. Miller | efdc1e2 | 2005-09-28 21:06:47 -0700 | [diff] [blame] | 64 | start = (unsigned long) from; |
| 65 | end = start + size; |
| 66 | if (fault_addr >= start && fault_addr < end) |
| 67 | return end - fault_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
| 69 | return size; |
| 70 | } |
Sam Ravnborg | 917c366 | 2009-01-08 16:58:20 -0800 | [diff] [blame] | 71 | EXPORT_SYMBOL(copy_in_user_fixup); |