Added generic backtrace functions to allow backtrace output on buffers
[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
80 static inline int __put_user(void *dst, const void *src, unsigned int count)
81 {
82         int err = 0;
83
84         switch (count) {
85         case 1:
86                 __put_user_asm(*(const uint8_t *) src, (uint8_t *) dst, err, "b",
87                                            "b", "iq", -EFAULT);
88                 break;
89         case 2:
90                 __put_user_asm(*(const uint16_t *) src, (uint16_t *) dst, err, "w",
91                                            "w", "ir", -EFAULT);
92                 break;
93         case 4:
94                 __put_user_asm(*(const uint32_t *) src, (uint32_t *) dst, err, "l",
95                                            "k", "ir", -EFAULT);
96                 break;
97         case 8:
98                 __put_user_asm(*(const uint64_t *) src, (uint64_t *) dst, err, "q",
99                                            "", "er", -EFAULT);
100                 break;
101         default:
102                 __user_memcpy(dst, src, count, err, -EFAULT);
103         }
104
105         return err;
106 }
107
108 static inline int copy_to_user(void *dst, const void *src, unsigned int count)
109 {
110         int err = 0;
111
112         if (unlikely(!is_user_rwaddr(dst, count))) {
113                 err = -EFAULT;
114         } else if (!__builtin_constant_p(count)) {
115                 __user_memcpy(dst, src, count, err, -EFAULT);
116         } else {
117                 err = __put_user(dst, src, count);
118         }
119
120         return err;
121 }
122
123 static inline int __get_user(void *dst, const void *src, unsigned int count)
124 {
125         int err = 0;
126
127         switch (count) {
128         case 1:
129                 __get_user_asm(*(uint8_t *) dst, (const uint8_t *) src, err, "b",
130                                            "b", "=q", -EFAULT);
131                 break;
132         case 2:
133                 __get_user_asm(*(uint16_t *) dst, (const uint16_t *) src, err, "w",
134                                            "w", "=r", -EFAULT);
135                 break;
136         case 4:
137                 __get_user_asm(*(uint32_t *) dst, (const uint32_t *) src, err, "l",
138                                            "k", "=r", -EFAULT);
139                 break;
140         case 8:
141                 __get_user_asm(*(uint64_t *) dst, (const uint64_t *) src, err, "q",
142                                            "", "=r", -EFAULT);
143                 break;
144         default:
145                 __user_memcpy(dst, src, count, err, -EFAULT);
146         }
147
148         return err;
149 }
150
151 static inline int copy_from_user(void *dst, const void *src,
152                                                                  unsigned int count)
153 {
154         int err = 0;
155
156         if (unlikely(!is_user_raddr((void *) src, count))) {
157                 err = -EFAULT;
158         } else if (!__builtin_constant_p(count)) {
159                 __user_memcpy(dst, src, count, err, -EFAULT);
160         } else {
161                 err = __get_user(dst, src, count);
162         }
163
164         return err;
165 }
166
167 static inline uintptr_t ex_insn_addr(const struct extable_ip_fixup *x)
168 {
169         return (uintptr_t) &x->insn + x->insn;
170 }
171
172 static inline uintptr_t ex_fixup_addr(const struct extable_ip_fixup *x)
173 {
174         return (uintptr_t) &x->fixup + x->fixup;
175 }