xref: /openbsd/sys/dev/pci/drm/include/linux/uaccess.h (revision 3cab2bb3)
1 /*	$OpenBSD: uaccess.h,v 1.3 2020/06/08 04:48:15 jsg Exp $	*/
2 /*
3  * Copyright (c) 2015 Mark Kettenis
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #ifndef _LINUX_UACCESS_H
19 #define _LINUX_UACCESS_H
20 
21 #include <sys/types.h>
22 #include <sys/systm.h>
23 #include <linux/sched.h>
24 
25 static inline unsigned long
26 __copy_to_user(void *to, const void *from, unsigned len)
27 {
28 	if (copyout(from, to, len))
29 		return len;
30 	return 0;
31 }
32 
33 static inline unsigned long
34 copy_to_user(void *to, const void *from, unsigned len)
35 {
36 	return __copy_to_user(to, from, len);
37 }
38 
39 static inline unsigned long
40 __copy_from_user(void *to, const void *from, unsigned len)
41 {
42 	if (copyin(from, to, len))
43 		return len;
44 	return 0;
45 }
46 
47 static inline unsigned long
48 copy_from_user(void *to, const void *from, unsigned len)
49 {
50 	return __copy_from_user(to, from, len);
51 }
52 
53 #define get_user(x, ptr)	-copyin(ptr, &(x), sizeof(x))
54 #define put_user(x, ptr) ({				\
55 	__typeof((x)) __tmp = (x);			\
56 	-copyout(&(__tmp), ptr, sizeof(__tmp));		\
57 })
58 #define __get_user(x, ptr)	get_user((x), (ptr))
59 #define __put_user(x, ptr)	put_user((x), (ptr))
60 
61 #define unsafe_put_user(x, ptr, err) ({				\
62 	__typeof((x)) __tmp = (x);				\
63 	if (copyout(&(__tmp), ptr, sizeof(__tmp)) != 0)		\
64 		goto err;					\
65 })
66 
67 #define VERIFY_READ     0x1
68 #define VERIFY_WRITE    0x2
69 static inline int
70 access_ok(const void *addr, unsigned long size)
71 {
72 	return 1;
73 }
74 
75 #define user_access_begin(addr, size)	access_ok(addr, size)
76 #define user_access_end()
77 
78 #if defined(__i386__) || defined(__amd64__)
79 
80 static inline void
81 pagefault_disable(void)
82 {
83 	curcpu()->ci_inatomic++;
84 	KASSERT(curcpu()->ci_inatomic > 0);
85 }
86 
87 static inline void
88 pagefault_enable(void)
89 {
90 	KASSERT(curcpu()->ci_inatomic > 0);
91 	curcpu()->ci_inatomic--;
92 }
93 
94 static inline int
95 pagefault_disabled(void)
96 {
97 	return curcpu()->ci_inatomic;
98 }
99 
100 static inline unsigned long
101 __copy_to_user_inatomic(void *to, const void *from, unsigned len)
102 {
103 	struct cpu_info *ci = curcpu();
104 	int inatomic = ci->ci_inatomic;
105 	int error;
106 
107 	ci->ci_inatomic = 1;
108 	error = copyout(from, to, len);
109 	ci->ci_inatomic = inatomic;
110 
111 	return (error ? len : 0);
112 }
113 
114 static inline unsigned long
115 __copy_from_user_inatomic(void *to, const void *from, unsigned len)
116 {
117 	struct cpu_info *ci = curcpu();
118 	int inatomic = ci->ci_inatomic;
119 	int error;
120 
121 	ci->ci_inatomic = 1;
122 	error = copyin(from, to, len);
123 	ci->ci_inatomic = inatomic;
124 
125 	return (error ? len : 0);
126 }
127 
128 static inline unsigned long
129 __copy_from_user_inatomic_nocache(void *to, const void *from, unsigned len)
130 {
131 	return __copy_from_user_inatomic(to, from, len);
132 }
133 
134 #endif /* defined(__i386__) || defined(__amd64__) */
135 
136 #endif
137