blob: 19d1fdb17d0e8b4f5b3ac6ce0a72fe7c11c65a6b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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>
10#include <asm/uaccess.h>
11
12/* Calculating the exact fault address when using
13 * block loads and stores can be very complicated.
David S. Millerefdc1e22005-09-28 21:06:47 -070014 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Instead of trying to be clever and handling all
16 * of the cases, just fix things up simply here.
17 */
18
David S. Millerefdc1e22005-09-28 21:06:47 -070019static unsigned long compute_size(unsigned long start, unsigned long size, unsigned long *offset)
20{
21 unsigned long fault_addr = current_thread_info()->fault_address;
22 unsigned long end = start + size;
23
24 if (fault_addr < start || fault_addr >= end) {
25 *offset = 0;
26 } else {
27 *offset = start - fault_addr;
28 size = end - fault_addr;
29 }
30 return size;
31}
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033unsigned long copy_from_user_fixup(void *to, const void __user *from, unsigned long size)
34{
David S. Millerefdc1e22005-09-28 21:06:47 -070035 unsigned long offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
David S. Millerefdc1e22005-09-28 21:06:47 -070037 size = compute_size((unsigned long) from, size, &offset);
38 if (likely(size))
39 memset(to + offset, 0, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41 return size;
42}
43
44unsigned long copy_to_user_fixup(void __user *to, const void *from, unsigned long size)
45{
David S. Millerefdc1e22005-09-28 21:06:47 -070046 unsigned long offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
David S. Millerefdc1e22005-09-28 21:06:47 -070048 return compute_size((unsigned long) to, size, &offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
50
51unsigned long copy_in_user_fixup(void __user *to, void __user *from, unsigned long size)
52{
David S. Millerefdc1e22005-09-28 21:06:47 -070053 unsigned long fault_addr = current_thread_info()->fault_address;
54 unsigned long start = (unsigned long) to;
55 unsigned long end = start + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
David S. Millerefdc1e22005-09-28 21:06:47 -070057 if (fault_addr >= start && fault_addr < end)
58 return end - fault_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
David S. Millerefdc1e22005-09-28 21:06:47 -070060 start = (unsigned long) from;
61 end = start + size;
62 if (fault_addr >= start && fault_addr < end)
63 return end - fault_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 return size;
66}