Flags RSI, RDI, ECX as RW operands so that GCC does not assume const
[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 __read_msr_asm(eax, edx, addr, err, errret)                                             \
44         asm volatile(ASM_STAC "\n"                                                                                      \
45                                  "1:            rdmsr\n"                                                                        \
46                                  "                      mfence\n"                                                                       \
47                                  "2: " ASM_CLAC "\n"                                                                    \
48                                  ".section .fixup,\"ax\"\n"                                                             \
49                                  "3:            mov %4,%0\n"                                                            \
50                                  "      jmp 2b\n"                                                                                       \
51                                  ".previous\n"                                                                                  \
52                                  _ASM_EXTABLE(1b, 3b)                                                                   \
53                                  : "=r" (err), "=d" (edx), "=a" (eax)                                   \
54                                  : "c" (addr), "i" (errret), "0" (err))
55
56 #define __write_msr_asm(val, addr, err, errret)                                                 \
57         asm volatile(ASM_STAC "\n"                                                                                      \
58                                  "1:            wrmsr\n"                                                                        \
59                                  "2: " ASM_CLAC "\n"                                                                    \
60                                  ".section .fixup,\"ax\"\n"                                                             \
61                                  "3:            mov %4,%0\n"                                                            \
62                                  "      jmp 2b\n"                                                                                       \
63                                  ".previous\n"                                                                                  \
64                                  _ASM_EXTABLE(1b, 3b)                                                                   \
65                                  : "=r" (err)                                                                                   \
66                                  : "d" ((uint32_t) (val >> 32)),                                                \
67                                    "a" ((uint32_t) (val & 0xffffffff)), "c" (addr),             \
68                                    "i" (errret), "0" (err))
69
70 #define __put_user_asm(x, addr, err, itype, rtype, ltype, errret)               \
71         asm volatile(ASM_STAC "\n"                                                                                      \
72                                  "1:            mov"itype" %"rtype"1,%2\n"                                      \
73                                  "2: " ASM_CLAC "\n"                                                                    \
74                                  ".section .fixup,\"ax\"\n"                                                             \
75                                  "3:            mov %3,%0\n"                                                            \
76                                  "      jmp 2b\n"                                                                                       \
77                                  ".previous\n"                                                                                  \
78                                  _ASM_EXTABLE(1b, 3b)                                                                   \
79                                  : "=r"(err)                                                                                    \
80                                  : ltype(x), "m" (__m(addr)), "i" (errret), "0" (err))
81
82 #define __get_user_asm(x, addr, err, itype, rtype, ltype, errret)       \
83         asm volatile(ASM_STAC "\n"                                                                              \
84                                  "1:            mov"itype" %2,%"rtype"1\n"                              \
85                                  "2: " ASM_CLAC "\n"                                                            \
86                                  ".section .fixup,\"ax\"\n"                                                     \
87                                  "3:            mov %3,%0\n"                                                    \
88                                  "      xor"itype" %"rtype"1,%"rtype"1\n"                               \
89                                  "      jmp 2b\n"                                                                               \
90                                  ".previous\n"                                                                          \
91                                  _ASM_EXTABLE(1b, 3b)                                                           \
92                                  : "=r" (err), ltype(x)                                                         \
93                                  : "m" (__m(addr)), "i" (errret), "0" (err))
94
95 #define __user_memcpy(dst, src, count, err, errret)                                             \
96         asm volatile(ASM_STAC "\n"                                                                                      \
97                                  "              cld\n"                                                                          \
98                                  "1:            rep movsb\n"                                                            \
99                                  "2: " ASM_CLAC "\n"                                                                    \
100                                  ".section .fixup,\"ax\"\n"                                                             \
101                                  "3:            mov %4,%0\n"                                                            \
102                                  "      jmp 2b\n"                                                                                       \
103                                  ".previous\n"                                                                                  \
104                                  _ASM_EXTABLE(1b, 3b)                                                                   \
105                                  : "=r"(err), "+D" (dst), "+S" (src), "+c" (count)              \
106                                  : "i" (errret), "0" (err)                                                              \
107                                  : "memory")
108
109 static inline int __put_user(void *dst, const void *src, unsigned int count)
110 {
111         int err = 0;
112
113         switch (count) {
114         case 1:
115                 __put_user_asm(*(const uint8_t *) src, (uint8_t *) dst, err, "b",
116                                            "b", "iq", -EFAULT);
117                 break;
118         case 2:
119                 __put_user_asm(*(const uint16_t *) src, (uint16_t *) dst, err, "w",
120                                            "w", "ir", -EFAULT);
121                 break;
122         case 4:
123                 __put_user_asm(*(const uint32_t *) src, (uint32_t *) dst, err, "l",
124                                            "k", "ir", -EFAULT);
125                 break;
126         case 8:
127                 __put_user_asm(*(const uint64_t *) src, (uint64_t *) dst, err, "q",
128                                            "", "er", -EFAULT);
129                 break;
130         default:
131                 __user_memcpy(dst, src, count, err, -EFAULT);
132         }
133
134         return err;
135 }
136
137 static inline int copy_to_user(void *dst, const void *src, unsigned int count)
138 {
139         int err = 0;
140
141         if (unlikely(!is_user_rwaddr(dst, count))) {
142                 err = -EFAULT;
143         } else if (!__builtin_constant_p(count)) {
144                 __user_memcpy(dst, src, count, err, -EFAULT);
145         } else {
146                 err = __put_user(dst, src, count);
147         }
148
149         return err;
150 }
151
152 static inline int __get_user(void *dst, const void *src, unsigned int count)
153 {
154         int err = 0;
155
156         switch (count) {
157         case 1:
158                 __get_user_asm(*(uint8_t *) dst, (const uint8_t *) src, err, "b",
159                                            "b", "=q", -EFAULT);
160                 break;
161         case 2:
162                 __get_user_asm(*(uint16_t *) dst, (const uint16_t *) src, err, "w",
163                                            "w", "=r", -EFAULT);
164                 break;
165         case 4:
166                 __get_user_asm(*(uint32_t *) dst, (const uint32_t *) src, err, "l",
167                                            "k", "=r", -EFAULT);
168                 break;
169         case 8:
170                 __get_user_asm(*(uint64_t *) dst, (const uint64_t *) src, err, "q",
171                                            "", "=r", -EFAULT);
172                 break;
173         default:
174                 __user_memcpy(dst, src, count, err, -EFAULT);
175         }
176
177         return err;
178 }
179
180 static inline int copy_from_user(void *dst, const void *src,
181                                                                  unsigned int count)
182 {
183         int err = 0;
184
185         if (unlikely(!is_user_raddr((void *) src, count))) {
186                 err = -EFAULT;
187         } else if (!__builtin_constant_p(count)) {
188                 __user_memcpy(dst, src, count, err, -EFAULT);
189         } else {
190                 err = __get_user(dst, src, count);
191         }
192
193         return err;
194 }
195
196 static inline int safe_read_msr(uint32_t addr, uint64_t *value)
197 {
198         int err = 0;
199         uint32_t edx, eax;
200
201         __read_msr_asm(eax, edx, addr, err, -EFAULT);
202         if (likely(err == 0))
203                 *value = ((uint64_t) edx << 32) | eax;
204
205         return err;
206 }
207
208 static inline int safe_write_msr(uint32_t addr, uint64_t value)
209 {
210         int err = 0;
211
212         __write_msr_asm(value, addr, err, -EFAULT);
213
214         return err;
215 }
216
217 static inline uintptr_t ex_insn_addr(const struct extable_ip_fixup *x)
218 {
219         return (uintptr_t) &x->insn + x->insn;
220 }
221
222 static inline uintptr_t ex_fixup_addr(const struct extable_ip_fixup *x)
223 {
224         return (uintptr_t) &x->fixup + x->fixup;
225 }