blob: c074799bddae178257b0bfe78766faccac75f737 [file] [log] [blame]
Robert Richter1ac2e6c2011-06-07 11:49:55 +02001/*
2 * User address space access functions.
3 *
4 * For licencing details see kernel-base/COPYING
5 */
6
7#include <linux/highmem.h>
Paul Gortmakere6830142016-07-13 20:18:57 -04008#include <linux/export.h>
Robert Richter1ac2e6c2011-06-07 11:49:55 +02009
Linus Torvalds92ae03f2012-04-06 14:32:32 -070010#include <asm/word-at-a-time.h>
Arun Sharmadb0dc752012-04-20 15:41:36 -070011#include <linux/sched.h>
Linus Torvalds92ae03f2012-04-06 14:32:32 -070012
Robert Richter1ac2e6c2011-06-07 11:49:55 +020013/*
Peter Zijlstrae00b12e2013-10-24 12:52:06 +020014 * We rely on the nested NMI work to allow atomic faults from the NMI path; the
15 * nested NMI paths are careful to preserve CR2.
Robert Richter1ac2e6c2011-06-07 11:49:55 +020016 */
17unsigned long
18copy_from_user_nmi(void *to, const void __user *from, unsigned long n)
19{
Peter Zijlstrae00b12e2013-10-24 12:52:06 +020020 unsigned long ret;
Robert Richter1ac2e6c2011-06-07 11:49:55 +020021
Stephane Eranian25f42982012-06-11 15:44:26 +020022 if (__range_not_ok(from, n, TASK_SIZE))
Yann Droneaudebf2d262015-06-22 21:38:43 +020023 return n;
Arun Sharmadb0dc752012-04-20 15:41:36 -070024
Peter Zijlstrae00b12e2013-10-24 12:52:06 +020025 /*
26 * Even though this function is typically called from NMI/IRQ context
27 * disable pagefaults so that its behaviour is consistent even when
28 * called form other contexts.
29 */
30 pagefault_disable();
31 ret = __copy_from_user_inatomic(to, from, n);
32 pagefault_enable();
Robert Richter1ac2e6c2011-06-07 11:49:55 +020033
Peter Zijlstra0a196842013-10-30 21:16:22 +010034 return ret;
Robert Richter1ac2e6c2011-06-07 11:49:55 +020035}
36EXPORT_SYMBOL_GPL(copy_from_user_nmi);
Borislav Petkovadb402c2016-10-31 16:10:15 +010037
38/**
39 * copy_to_user: - Copy a block of data into user space.
40 * @to: Destination address, in user space.
41 * @from: Source address, in kernel space.
42 * @n: Number of bytes to copy.
43 *
44 * Context: User context only. This function may sleep if pagefaults are
45 * enabled.
46 *
47 * Copy data from kernel space to user space.
48 *
49 * Returns number of bytes that could not be copied.
50 * On success, this will be zero.
51 */
52unsigned long _copy_to_user(void __user *to, const void *from, unsigned n)
53{
54 if (access_ok(VERIFY_WRITE, to, n))
55 n = __copy_to_user(to, from, n);
56 return n;
57}
58EXPORT_SYMBOL(_copy_to_user);
59
60/**
61 * copy_from_user: - Copy a block of data from user space.
62 * @to: Destination address, in kernel space.
63 * @from: Source address, in user space.
64 * @n: Number of bytes to copy.
65 *
66 * Context: User context only. This function may sleep if pagefaults are
67 * enabled.
68 *
69 * Copy data from user space to kernel space.
70 *
71 * Returns number of bytes that could not be copied.
72 * On success, this will be zero.
73 *
74 * If some data could not be copied, this function will pad the copied
75 * data to the requested size using zero bytes.
76 */
77unsigned long _copy_from_user(void *to, const void __user *from, unsigned n)
78{
79 if (access_ok(VERIFY_READ, from, n))
80 n = __copy_from_user(to, from, n);
81 else
82 memset(to, 0, n);
83 return n;
84}
85EXPORT_SYMBOL(_copy_from_user);