1 /* $OpenBSD: uaccess.h,v 1.4 2020/11/17 11:20:59 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 static inline int 68 access_ok(const void *addr, unsigned long size) 69 { 70 return 1; 71 } 72 73 #define user_access_begin(addr, size) access_ok(addr, size) 74 #define user_access_end() 75 76 #if defined(__i386__) || defined(__amd64__) 77 78 static inline void 79 pagefault_disable(void) 80 { 81 curcpu()->ci_inatomic++; 82 KASSERT(curcpu()->ci_inatomic > 0); 83 } 84 85 static inline void 86 pagefault_enable(void) 87 { 88 KASSERT(curcpu()->ci_inatomic > 0); 89 curcpu()->ci_inatomic--; 90 } 91 92 static inline int 93 pagefault_disabled(void) 94 { 95 return curcpu()->ci_inatomic; 96 } 97 98 static inline unsigned long 99 __copy_to_user_inatomic(void *to, const void *from, unsigned len) 100 { 101 struct cpu_info *ci = curcpu(); 102 int inatomic = ci->ci_inatomic; 103 int error; 104 105 ci->ci_inatomic = 1; 106 error = copyout(from, to, len); 107 ci->ci_inatomic = inatomic; 108 109 return (error ? len : 0); 110 } 111 112 static inline unsigned long 113 __copy_from_user_inatomic(void *to, const void *from, unsigned len) 114 { 115 struct cpu_info *ci = curcpu(); 116 int inatomic = ci->ci_inatomic; 117 int error; 118 119 ci->ci_inatomic = 1; 120 error = copyin(from, to, len); 121 ci->ci_inatomic = inatomic; 122 123 return (error ? len : 0); 124 } 125 126 static inline unsigned long 127 __copy_from_user_inatomic_nocache(void *to, const void *from, unsigned len) 128 { 129 return __copy_from_user_inatomic(to, from, len); 130 } 131 132 #endif /* defined(__i386__) || defined(__amd64__) */ 133 134 #endif 135