parlib: Fix dlopen() with -rdynamic
[akaros.git] / user / parlib / include / parlib / vcore.h
1 #pragma once
2
3 #include <parlib/arch/vcore.h>
4 #include <parlib/arch/atomic.h>
5 #include <sys/param.h>
6 #include <string.h>
7 #include <parlib/timing.h>
8 #include <parlib/common.h>
9
10 __BEGIN_DECLS
11
12 #define TRANSITION_STACK_PAGES 2
13 #define TRANSITION_STACK_SIZE (TRANSITION_STACK_PAGES*PGSIZE)
14
15 /* Defined in vcore.c */
16 void __attribute__((noreturn)) vcore_entry(void);
17 extern __thread bool __vcore_context;
18 extern __thread int __vcoreid;
19 extern __thread struct syscall __vcore_one_sysc;        /* see sys_change_vcore */
20
21 /* Arch specific entry from the kernel */
22 void __attribute__((noreturn)) __kernel_vcore_entry(void);
23
24 /* Vcore API functions */
25 static inline uint32_t max_vcores(void);
26 static inline uint32_t num_vcores(void);
27 static inline int vcore_id(void);
28 static inline bool in_vcore_context(void);
29 static inline bool in_multi_mode(void);
30 static inline void __enable_notifs(uint32_t vcoreid);
31 static inline void __disable_notifs(uint32_t vcoreid);
32 static inline bool notif_is_enabled(uint32_t vcoreid);
33 static inline bool vcore_is_mapped(uint32_t vcoreid);
34 static inline bool vcore_is_preempted(uint32_t vcoreid);
35 static inline struct preempt_data *vcpd_of(uint32_t vcoreid);
36 static inline bool preempt_is_pending(uint32_t vcoreid);
37 static inline bool __preempt_is_pending(uint32_t vcoreid);
38 static inline void *get_vcpd_tls_desc(uint32_t vcoreid);
39 static inline void set_vcpd_tls_desc(uint32_t vcoreid, void *tls_desc);
40 static inline uint64_t vcore_account_resume_nsec(uint32_t vcoreid);
41 static inline uint64_t vcore_account_total_nsec(uint32_t vcoreid);
42 static inline void cpu_relax_any(void);
43 static inline bool __in_fake_parlib(void);
44
45 void vcore_lib_init(void);
46 void vcore_change_to_m(void);
47 void vcore_request_more(long nr_new_vcores);
48 void vcore_request_total(long nr_vcores_wanted);
49 void vcore_yield(bool preempt_pending);
50 void vcore_reenter(void (*entry_func)(void));
51 void enable_notifs(uint32_t vcoreid);
52 void disable_notifs(uint32_t vcoreid);
53 void vcore_idle(void);
54 void ensure_vcore_runs(uint32_t vcoreid);
55 void cpu_relax_vc(uint32_t vcoreid);
56 uint32_t get_vcoreid(void);
57 bool check_vcoreid(const char *str, uint32_t vcoreid);
58 void __attribute__((noreturn)) vcore_yield_or_restart(void);
59
60 /* This works so long as we don't dlopen parlib (which we never do) */
61 #define get_tlsvar_linaddr(_vcoreid, _var)                                     \
62 ({                                                                             \
63         uintptr_t vc_tls_desc = (uintptr_t)get_vcpd_tls_desc(_vcoreid);            \
64         uintptr_t var_off = (uintptr_t)&_var - (uintptr_t)get_tls_desc();          \
65         (typeof(_var) *)(vc_tls_desc + var_off);                                   \
66 })
67
68 /* Static inlines */
69 static inline uint32_t max_vcores(void)
70 {
71         return MAX(1, __procinfo.max_vcores);
72 }
73
74 static inline uint32_t num_vcores(void)
75 {
76         return __procinfo.num_vcores;
77 }
78
79 static inline int vcore_id(void)
80 {
81         return __vcoreid;
82 }
83
84 static inline bool in_vcore_context(void)
85 {
86         return __vcore_context;
87 }
88
89 static inline bool in_multi_mode(void)
90 {
91         return __procinfo.is_mcp;
92 }
93
94 /* Only call this if you know what you are doing. */
95 static inline void __enable_notifs(uint32_t vcoreid)
96 {
97         vcpd_of(vcoreid)->notif_disabled = FALSE;
98 }
99
100 static inline void __disable_notifs(uint32_t vcoreid)
101 {
102         vcpd_of(vcoreid)->notif_disabled = TRUE;
103 }
104
105 static inline bool notif_is_enabled(uint32_t vcoreid)
106 {
107         return !vcpd_of(vcoreid)->notif_disabled;
108 }
109
110 static inline bool vcore_is_mapped(uint32_t vcoreid)
111 {
112         return __procinfo.vcoremap[vcoreid].valid;
113 }
114
115 /* We could also check for VC_K_LOCK, but that's a bit much. */
116 static inline bool vcore_is_preempted(uint32_t vcoreid)
117 {
118         struct preempt_data *vcpd = vcpd_of(vcoreid);
119         return atomic_read(&vcpd->flags) & VC_PREEMPTED;
120 }
121
122 static inline struct preempt_data *vcpd_of(uint32_t vcoreid)
123 {
124         return &__procdata.vcore_preempt_data[vcoreid];
125 }
126
127 /* Uthread's can call this in case they care if a preemption is coming.  If a
128  * preempt is incoming, this will return TRUE, if you are in uthread context.  A
129  * reasonable response for a uthread is to yield, and vcore_entry will deal with
130  * the preempt pending.
131  *
132  * If you call this from vcore context, it will do nothing.  In general, it's
133  * not safe to just yield (or do whatever you plan on doing) from arbitrary
134  * places in vcore context.  So we just lie about PP. */
135 static inline bool preempt_is_pending(uint32_t vcoreid)
136 {
137         if (in_vcore_context())
138                 return FALSE;
139         return __preempt_is_pending(vcoreid);
140 }
141
142 static inline bool __preempt_is_pending(uint32_t vcoreid)
143 {
144         return __procinfo.vcoremap[vcoreid].preempt_pending;
145 }
146
147 /* The kernel interface uses uintptr_t, but we have a lot of older code that
148  * uses void *, hence the casting. */
149 static inline void *get_vcpd_tls_desc(uint32_t vcoreid)
150 {
151         return (void*)__procdata.vcore_preempt_data[vcoreid].vcore_tls_desc;
152 }
153
154 static inline void set_vcpd_tls_desc(uint32_t vcoreid, void *tls_desc)
155 {
156         __procdata.vcore_preempt_data[vcoreid].vcore_tls_desc = (uintptr_t)tls_desc;
157 }
158
159 static inline uint64_t vcore_account_resume_ticks(uint32_t vcoreid)
160 {
161         return __procinfo.vcoremap[vcoreid].resume_ticks;
162 }
163
164 static inline uint64_t vcore_account_resume_nsec(uint32_t vcoreid)
165 {
166         return tsc2nsec(vcore_account_resume_ticks(vcoreid));
167 }
168
169 static inline uint64_t vcore_account_total_ticks(uint32_t vcoreid)
170 {
171         return __procinfo.vcoremap[vcoreid].total_ticks;
172 }
173
174 static inline uint64_t vcore_account_total_nsec(uint32_t vcoreid)
175 {
176         return tsc2nsec(vcore_account_total_ticks(vcoreid));
177 }
178
179 static inline uint64_t vcore_account_uptime_ticks(uint32_t vcoreid)
180 {
181         uint64_t resume = __procinfo.vcoremap[vcoreid].resume_ticks; 
182         uint64_t total = __procinfo.vcoremap[vcoreid].total_ticks; 
183         uint64_t now = read_tsc();
184         return now - resume + total;
185 }
186
187 static inline uint64_t vcore_account_uptime_nsec(uint32_t vcoreid)
188 {
189         return tsc2nsec(vcore_account_uptime_ticks(vcoreid));
190 }
191
192 static inline void cpu_relax_any(void)
193 {
194         return cpu_relax_vc(vcore_id());
195 }
196
197 /* Shared libraries also contain parlib.  That'll be true until we start making
198  * parlib a .so, which has some TLS implications (and maybe others).  The real
199  * parlib is the one in the program binary, not the shared libraries.  This
200  * detection works because all shared libs, both the -l and the dlopens, are
201  * mapped above the BRK.
202  *
203  * Previously, we tried using weak symbols, specifically _start or _end, but be
204  * careful.  If you pass e.g. _start or _end to a function or inline asm, the
205  * program binary will do something slightly different, which may make the
206  * shared library load different values. */
207 static inline bool __in_fake_parlib(void)
208 {
209         static char dummy;
210
211         return (uintptr_t)&dummy > BRK_START;
212 }
213
214 #ifndef __PIC__
215
216 #define begin_safe_access_tls_vars()
217
218 #define end_safe_access_tls_vars()
219
220 #else
221
222 #include <features.h>
223
224 /* These macro acrobatics trick the compiler into not caching the (linear)
225  * address of TLS variables across loads/stores of the TLS descriptor, in lieu
226  * of a "TLS cmb()". */
227 #define begin_safe_access_tls_vars()                                           \
228 {                                                                              \
229         void __attribute__((noinline, optimize("O0")))                             \
230         safe_access_tls_var_internal() {                                           \
231                 asm("");                                                               \
232
233 #define end_safe_access_tls_vars()                                             \
234         } safe_access_tls_var_internal();                                          \
235 }
236
237 #endif // __PIC__
238
239 /* Switches into the TLS 'tls_desc'.  Capable of being called from either
240  * uthread or vcore context.  Pairs with end_access_tls_vars(). */
241 #define begin_access_tls_vars(tls_desc)                                        \
242 {                                                                              \
243         struct uthread *caller;                                                    \
244         uint32_t vcoreid;                                                          \
245         void *temp_tls_desc;                                                       \
246         bool invcore = in_vcore_context();                                         \
247         if (!invcore) {                                                            \
248                 caller = current_uthread;                                              \
249                 /* If you have no current_uthread, you might be called too early in the
250                  * process's lifetime.  Make sure something like uthread_slim_init() has
251                  * been run. */                                                        \
252                 assert(caller);                                                        \
253                 /* We need to disable notifs here (in addition to not migrating), since
254                  * we could get interrupted when we're in the other TLS, and when the
255                  * vcore restarts us, it will put us in our old TLS, not the one we were
256                  * in when we were interrupted.  We need to not migrate, since once we
257                  * know the vcoreid, we depend on being on the same vcore throughout.*/\
258                 caller->flags |= UTHREAD_DONT_MIGRATE;                                 \
259                 /* Not concerned about cross-core memory ordering, so no CPU mbs needed.
260                  * The cmb is to prevent the compiler from issuing the vcore read before
261                  * the DONT_MIGRATE write. */                                          \
262                 cmb();                                                                 \
263                 vcoreid = vcore_id();                                                  \
264                 disable_notifs(vcoreid);                                               \
265         } else { /* vcore context */                                               \
266                 vcoreid = vcore_id();                                                  \
267         }                                                                          \
268         temp_tls_desc = get_tls_desc();                                            \
269         set_tls_desc(tls_desc);                                                    \
270         begin_safe_access_tls_vars();
271
272 #define end_access_tls_vars()                                                  \
273         end_safe_access_tls_vars();                                                \
274         set_tls_desc(temp_tls_desc);                                               \
275         if (!invcore) {                                                            \
276                 /* Note we reenable migration before enabling notifs, which is reverse
277                  * from how we disabled notifs.  We must enabling migration before
278                  * enabling notifs.  See 6c7fb12 and 5e4825eb4 for details. */         \
279                 caller->flags &= ~UTHREAD_DONT_MIGRATE;                                \
280                 cmb();  /* turn off DONT_MIGRATE before enabling notifs */             \
281                 enable_notifs(vcoreid);                                                \
282         }                                                                          \
283 }
284
285 #define safe_set_tls_var(name, val)                                            \
286 ({                                                                             \
287         begin_safe_access_tls_vars();                                              \
288         name = val;                                                                \
289         end_safe_access_tls_vars();                                                \
290 })
291
292 #define safe_get_tls_var(name)                                                 \
293 ({                                                                             \
294         typeof(name) __val;                                                        \
295         begin_safe_access_tls_vars();                                              \
296         __val = name;                                                              \
297         end_safe_access_tls_vars();                                                \
298         __val;                                                                     \
299 })
300
301 #define vcore_set_tls_var(name, val)                                           \
302 ({                                                                             \
303         typeof(val) __val = val;                                                   \
304         begin_access_tls_vars(get_vcpd_tls_desc(vcoreid));                         \
305         name = __val;                                                              \
306         end_access_tls_vars();                                                     \
307 })
308
309 #define vcore_get_tls_var(name)                                                \
310 ({                                                                             \
311         typeof(name) val;                                                          \
312         begin_access_tls_vars(get_vcpd_tls_desc(vcoreid));                         \
313         val = name;                                                                \
314         end_access_tls_vars();                                                     \
315         val;                                                                       \
316 })
317
318 __END_DECLS