Implemented the new profiler
[akaros.git] / kern / arch / x86 / uaccess.h
1 /* Copyright (c) 2015 Google Inc
2  * Davide Libenzi <dlibenzi@google.com>
3  * See LICENSE for details.
4  *
5  * Part of this code coming from a Linux kernel file:
6  *
7  * linux/arch/x86/include/asm/uaccess.h
8  *
9  * Which, even though missing specific copyright, it is supposed to be
10  * ruled by the overall Linux copyright.
11  */
12
13 #pragma once
14
15 #include <ros/errno.h>
16 #include <compiler.h>
17 #include <stdint.h>
18 #include <umem.h>
19
20 #define ASM_STAC
21 #define ASM_CLAC
22 #define __m(x) *(x)
23
24 struct extable_ip_fixup {
25         uint64_t insn;
26         uint64_t fixup;
27 };
28
29 #define _ASM_EXTABLE_INIT()                                                                             \
30         asm volatile(                                                                                           \
31         " .pushsection \"__ex_table\",\"a\"\n"                                          \
32         " .balign 16\n"                                                                                         \
33         " .popsection\n"                                                                                        \
34         : :)
35
36 #define _ASM_EXTABLE(from, to)                                                                  \
37         " .pushsection \"__ex_table\",\"a\"\n"                                          \
38         " .balign 16\n"                                                                                         \
39         " .quad (" #from ") - .\n"                                                                      \
40         " .quad (" #to ") - .\n"                                                                        \
41         " .popsection\n"
42
43 #define __put_user_asm(x, addr, err, itype, rtype, ltype, errret)               \
44         asm volatile(ASM_STAC "\n"                                                                                      \
45                                  "1:            mov"itype" %"rtype"1,%2\n"                                      \
46                                  "2: " ASM_CLAC "\n"                                                                    \
47                                  ".section .fixup,\"ax\"\n"                                                             \
48                                  "3:            mov %3,%0\n"                                                            \
49                                  "      jmp 2b\n"                                                                                       \
50                                  ".previous\n"                                                                                  \
51                                  _ASM_EXTABLE(1b, 3b)                                                                   \
52                                  : "=r"(err)                                                                                    \
53                                  : ltype(x), "m" (__m(addr)), "i" (errret), "0" (err))
54
55 #define __get_user_asm(x, addr, err, itype, rtype, ltype, errret)       \
56         asm volatile(ASM_STAC "\n"                                                                              \
57                                  "1:            mov"itype" %2,%"rtype"1\n"                              \
58                                  "2: " ASM_CLAC "\n"                                                            \
59                                  ".section .fixup,\"ax\"\n"                                                     \
60                                  "3:            mov %3,%0\n"                                                    \
61                                  "      xor"itype" %"rtype"1,%"rtype"1\n"                               \
62                                  "      jmp 2b\n"                                                                               \
63                                  ".previous\n"                                                                          \
64                                  _ASM_EXTABLE(1b, 3b)                                                           \
65                                  : "=r" (err), ltype(x)                                                         \
66                                  : "m" (__m(addr)), "i" (errret), "0" (err))
67
68 #define __user_memcpy(dst, src, count, err, errret)                                             \
69         asm volatile(ASM_STAC "\n"                                                                                      \
70                                  "1:            rep movsb\n"                                                            \
71                                  "2: " ASM_CLAC "\n"                                                                    \
72                                  ".section .fixup,\"ax\"\n"                                                             \
73                                  "3:            mov %4,%0\n"                                                            \
74                                  "      jmp 2b\n"                                                                                       \
75                                  ".previous\n"                                                                                  \
76                                  _ASM_EXTABLE(1b, 3b)                                                                   \
77                                  : "=r"(err)                                                                                    \
78                                  : "D" (dst), "S" (src), "c" (count), "i" (errret), "0" (err) \
79                                  : "memory")
80
81 static inline int __put_user(void *dst, const void *src, unsigned int count)
82 {
83         int err = 0;
84
85         switch (count) {
86         case 1:
87                 __put_user_asm(*(const uint8_t *) src, (uint8_t *) dst, err, "b",
88                                            "b", "iq", -EFAULT);
89                 break;
90         case 2:
91                 __put_user_asm(*(const uint16_t *) src, (uint16_t *) dst, err, "w",
92                                            "w", "ir", -EFAULT);
93                 break;
94         case 4:
95                 __put_user_asm(*(const uint32_t *) src, (uint32_t *) dst, err, "l",
96                                            "k", "ir", -EFAULT);
97                 break;
98         case 8:
99                 __put_user_asm(*(const uint64_t *) src, (uint64_t *) dst, err, "q",
100                                            "", "er", -EFAULT);
101                 break;
102         default:
103                 __user_memcpy(dst, src, count, err, -EFAULT);
104         }
105
106         return err;
107 }
108
109 static inline int copy_to_user(void *dst, const void *src, unsigned int count)
110 {
111         int err = 0;
112
113         if (unlikely(!is_user_rwaddr(dst, count))) {
114                 err = -EFAULT;
115         } else if (!__builtin_constant_p(count)) {
116                 __user_memcpy(dst, src, count, err, -EFAULT);
117         } else {
118                 err = __put_user(dst, src, count);
119         }
120
121         return err;
122 }
123
124 static inline int __get_user(void *dst, const void *src, unsigned int count)
125 {
126         int err = 0;
127
128         switch (count) {
129         case 1:
130                 __get_user_asm(*(uint8_t *) dst, (const uint8_t *) src, err, "b",
131                                            "b", "=q", -EFAULT);
132                 break;
133         case 2:
134                 __get_user_asm(*(uint16_t *) dst, (const uint16_t *) src, err, "w",
135                                            "w", "=r", -EFAULT);
136                 break;
137         case 4:
138                 __get_user_asm(*(uint32_t *) dst, (const uint32_t *) src, err, "l",
139                                            "k", "=r", -EFAULT);
140                 break;
141         case 8:
142                 __get_user_asm(*(uint64_t *) dst, (const uint64_t *) src, err, "q",
143                                            "", "=r", -EFAULT);
144                 break;
145         default:
146                 __user_memcpy(dst, src, count, err, -EFAULT);
147         }
148
149         return err;
150 }
151
152 static inline int copy_from_user(void *dst, const void *src,
153                                                                  unsigned int count)
154 {
155         int err = 0;
156
157         if (unlikely(!is_user_raddr((void *) src, count))) {
158                 err = -EFAULT;
159         } else if (!__builtin_constant_p(count)) {
160                 __user_memcpy(dst, src, count, err, -EFAULT);
161         } else {
162                 err = __get_user(dst, src, count);
163         }
164
165         return err;
166 }
167
168 static inline uintptr_t ex_insn_addr(const struct extable_ip_fixup *x)
169 {
170         return (uintptr_t) &x->insn + x->insn;
171 }
172
173 static inline uintptr_t ex_fixup_addr(const struct extable_ip_fixup *x)
174 {
175         return (uintptr_t) &x->fixup + x->fixup;
176 }