xref: /openbsd-src/sys/dev/pci/drm/include/linux/uaccess.h (revision 99fd087599a8791921855f21bd7e36130f39aadc)
1 /*	$OpenBSD: uaccess.h,v 1.2 2019/12/18 08:56:19 kettenis 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 #define user_access_begin()
26 #define user_access_end()
27 
28 static inline unsigned long
29 __copy_to_user(void *to, const void *from, unsigned len)
30 {
31 	if (copyout(from, to, len))
32 		return len;
33 	return 0;
34 }
35 
36 static inline unsigned long
37 copy_to_user(void *to, const void *from, unsigned len)
38 {
39 	return __copy_to_user(to, from, len);
40 }
41 
42 static inline unsigned long
43 __copy_from_user(void *to, const void *from, unsigned len)
44 {
45 	if (copyin(from, to, len))
46 		return len;
47 	return 0;
48 }
49 
50 static inline unsigned long
51 copy_from_user(void *to, const void *from, unsigned len)
52 {
53 	return __copy_from_user(to, from, len);
54 }
55 
56 #define get_user(x, ptr)	-copyin(ptr, &(x), sizeof(x))
57 #define put_user(x, ptr) ({				\
58 	__typeof((x)) __tmp = (x);			\
59 	-copyout(&(__tmp), ptr, sizeof(__tmp));		\
60 })
61 #define __get_user(x, ptr)	get_user((x), (ptr))
62 #define __put_user(x, ptr)	put_user((x), (ptr))
63 
64 #define unsafe_put_user(x, ptr, err) ({				\
65 	__typeof((x)) __tmp = (x);				\
66 	if (copyout(&(__tmp), ptr, sizeof(__tmp)) != 0)		\
67 		goto err;					\
68 })
69 
70 #define VERIFY_READ     0x1
71 #define VERIFY_WRITE    0x2
72 static inline int
73 access_ok(int type, const void *addr, unsigned long size)
74 {
75 	return true;
76 }
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