59a1562c9784ab149b3e7baab4d8cda8e71519d3
[akaros.git] / user / parlib / include / vcore.h
1 #ifndef PARLIB_VCORE_H
2 #define PARLIB_VCORE_H
3
4 #include <parlib/arch/vcore.h>
5 #include <parlib/arch/atomic.h>
6 #include <sys/param.h>
7 #include <string.h>
8 #include <parlib/timing.h>
9
10 __BEGIN_DECLS
11
12 /*****************************************************************************/
13 /* TODO: This is a complete hack, but necessary for vcore stuff to work for now
14  * The issue is that exit sometimes calls sys_yield(), and we can't recover from
15  * that properly under our vcore model (we shouldn't though).  We really need to
16  * rethink what sys_yield 'should' do when in multicore mode, or else come up 
17  * with a different syscall entirely. */
18 #include <stdlib.h>
19 #include <unistd.h>
20 #undef exit
21 #define exit(status) _exit(status)
22 /*****************************************************************************/
23
24 #define LOG2_MAX_VCORES 6
25 #define MAX_VCORES (1 << LOG2_MAX_VCORES)
26
27 #define TRANSITION_STACK_PAGES 2
28 #define TRANSITION_STACK_SIZE (TRANSITION_STACK_PAGES*PGSIZE)
29
30 /* Defined by glibc; Must be implemented by a user level threading library */
31 extern void vcore_entry();
32 /* Defined in glibc's start.c */
33 extern __thread bool __vcore_context;
34 extern __thread int __vcoreid;
35 /* Defined in vcore.c */
36 extern __thread struct syscall __vcore_one_sysc;        /* see sys_change_vcore */
37
38 /* Vcore API functions */
39 static inline uint32_t max_vcores(void);
40 static inline uint32_t num_vcores(void);
41 static inline int vcore_id(void);
42 static inline bool in_vcore_context(void);
43 static inline bool in_multi_mode(void);
44 static inline void __enable_notifs(uint32_t vcoreid);
45 static inline void __disable_notifs(uint32_t vcoreid);
46 static inline bool notif_is_enabled(uint32_t vcoreid);
47 static inline bool vcore_is_mapped(uint32_t vcoreid);
48 static inline bool vcore_is_preempted(uint32_t vcoreid);
49 static inline struct preempt_data *vcpd_of(uint32_t vcoreid);
50 static inline bool preempt_is_pending(uint32_t vcoreid);
51 static inline bool __preempt_is_pending(uint32_t vcoreid);
52 static inline void *get_vcpd_tls_desc(uint32_t vcoreid);
53 static inline void set_vcpd_tls_desc(uint32_t vcoreid, void *tls_desc);
54 static inline uint64_t vcore_account_resume_nsec(uint32_t vcoreid);
55 static inline uint64_t vcore_account_total_nsec(uint32_t vcoreid);
56 void vcore_lib_init(void);
57 void vcore_change_to_m(void);
58 int vcore_request(long nr_new_vcores);
59 void vcore_yield(bool preempt_pending);
60 void vcore_reenter(void (*entry_func)(void));
61 void enable_notifs(uint32_t vcoreid);
62 void disable_notifs(uint32_t vcoreid);
63 void vcore_idle(void);
64 void ensure_vcore_runs(uint32_t vcoreid);
65 void cpu_relax_vc(uint32_t vcoreid);
66 uint32_t get_vcoreid(void);
67 bool check_vcoreid(const char *str, uint32_t vcoreid);
68
69 /* This works so long as we don't dlopen parlib (which we never do) */
70 #define get_tlsvar_linaddr(_vcoreid, _var)                                     \
71 ({                                                                             \
72         uintptr_t vc_tls_desc = (uintptr_t)get_vcpd_tls_desc(_vcoreid);            \
73         uintptr_t var_off = (uintptr_t)&_var - (uintptr_t)get_tls_desc();          \
74         (typeof(_var) *)(vc_tls_desc + var_off);                                   \
75 })
76
77 /* Static inlines */
78 static inline uint32_t max_vcores(void)
79 {
80         return MIN(__procinfo.max_vcores, MAX_VCORES);
81 }
82
83 static inline uint32_t num_vcores(void)
84 {
85         return __procinfo.num_vcores;
86 }
87
88 static inline int vcore_id(void)
89 {
90         return __vcoreid;
91 }
92
93 static inline bool in_vcore_context(void)
94 {
95         return __vcore_context;
96 }
97
98 static inline bool in_multi_mode(void)
99 {
100         return __procinfo.is_mcp;
101 }
102
103 /* Only call this if you know what you are doing. */
104 static inline void __enable_notifs(uint32_t vcoreid)
105 {
106         vcpd_of(vcoreid)->notif_disabled = FALSE;
107 }
108
109 static inline void __disable_notifs(uint32_t vcoreid)
110 {
111         vcpd_of(vcoreid)->notif_disabled = TRUE;
112 }
113
114 static inline bool notif_is_enabled(uint32_t vcoreid)
115 {
116         return !vcpd_of(vcoreid)->notif_disabled;
117 }
118
119 static inline bool vcore_is_mapped(uint32_t vcoreid)
120 {
121         return __procinfo.vcoremap[vcoreid].valid;
122 }
123
124 /* We could also check for VC_K_LOCK, but that's a bit much. */
125 static inline bool vcore_is_preempted(uint32_t vcoreid)
126 {
127         struct preempt_data *vcpd = vcpd_of(vcoreid);
128         return atomic_read(&vcpd->flags) & VC_PREEMPTED;
129 }
130
131 static inline struct preempt_data *vcpd_of(uint32_t vcoreid)
132 {
133         return &__procdata.vcore_preempt_data[vcoreid];
134 }
135
136 /* Uthread's can call this in case they care if a preemption is coming.  If a
137  * preempt is incoming, this will return TRUE, if you are in uthread context.  A
138  * reasonable response for a uthread is to yield, and vcore_entry will deal with
139  * the preempt pending.
140  *
141  * If you call this from vcore context, it will do nothing.  In general, it's
142  * not safe to just yield (or do whatever you plan on doing) from arbitrary
143  * places in vcore context.  So we just lie about PP. */
144 static inline bool preempt_is_pending(uint32_t vcoreid)
145 {
146         if (in_vcore_context())
147                 return FALSE;
148         return __preempt_is_pending(vcoreid);
149 }
150
151 static inline bool __preempt_is_pending(uint32_t vcoreid)
152 {
153         return __procinfo.vcoremap[vcoreid].preempt_pending;
154 }
155
156 /* The kernel interface uses uintptr_t, but we have a lot of older code that
157  * uses void *, hence the casting. */
158 static inline void *get_vcpd_tls_desc(uint32_t vcoreid)
159 {
160         return (void*)__procdata.vcore_preempt_data[vcoreid].vcore_tls_desc;
161 }
162
163 static inline void set_vcpd_tls_desc(uint32_t vcoreid, void *tls_desc)
164 {
165         __procdata.vcore_preempt_data[vcoreid].vcore_tls_desc = (uintptr_t)tls_desc;
166 }
167
168 static inline uint64_t vcore_account_resume_ticks(uint32_t vcoreid)
169 {
170         return __procinfo.vcoremap[vcoreid].resume_ticks;
171 }
172
173 static inline uint64_t vcore_account_resume_nsec(uint32_t vcoreid)
174 {
175         return tsc2nsec(vcore_account_resume_ticks(vcoreid));
176 }
177
178 static inline uint64_t vcore_account_total_ticks(uint32_t vcoreid)
179 {
180         return __procinfo.vcoremap[vcoreid].total_ticks;
181 }
182
183 static inline uint64_t vcore_account_total_nsec(uint32_t vcoreid)
184 {
185         return tsc2nsec(vcore_account_total_ticks(vcoreid));
186 }
187
188 static inline uint64_t vcore_account_uptime_ticks(uint32_t vcoreid)
189 {
190         uint64_t resume = __procinfo.vcoremap[vcoreid].resume_ticks; 
191         uint64_t total = __procinfo.vcoremap[vcoreid].total_ticks; 
192         uint64_t now = read_tsc();
193         return now - resume + total;
194 }
195
196 static inline uint64_t vcore_account_uptime_nsec(uint32_t vcoreid)
197 {
198         return tsc2nsec(vcore_account_uptime_ticks(vcoreid));
199 }
200
201 #ifndef __PIC__
202
203 #define begin_safe_access_tls_vars()
204
205 #define end_safe_access_tls_vars()
206
207 #else
208
209 #include <features.h>
210 #if __GNUC_PREREQ(4,4)
211
212 /* These macro acrobatics trick the compiler into not caching the (linear)
213  * address of TLS variables across loads/stores of the TLS descriptor, in lieu
214  * of a "TLS cmb()". */
215 #define begin_safe_access_tls_vars()                                           \
216 {                                                                              \
217         void __attribute__((noinline, optimize("O0")))                             \
218         safe_access_tls_var_internal() {                                           \
219                 asm("");                                                               \
220
221 #define end_safe_access_tls_vars()                                             \
222         } safe_access_tls_var_internal();                                          \
223 }
224
225 #else
226
227 #define begin_safe_access_tls_vars()                                           \
228         printf("ERROR: For PIC use gcc 4.4 or above for tls support!\n");          \
229         printf("ERROR: As a quick fix, recompile your app with -static...\n");     \
230         exit(2);
231
232 #define end_safe_access_tls_vars()                                             \
233         printf("Will never be called because we abort above!");                    \
234         exit(2);
235
236 #endif //__GNUC_PREREQ
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
319
320 #endif /* PARLIB_VCORE_H */