Cleans up vcore/uth/pth init code
[akaros.git] / user / parlib / include / vcore.h
1 #ifndef _VCORE_H
2 #define _VCORE_H
3
4 #ifdef __cplusplus
5 extern "C" {
6 #endif
7
8 #include <arch/vcore.h>
9 #include <arch/atomic.h>
10 #include <sys/param.h>
11 #include <string.h>
12
13 /*****************************************************************************/
14 /* TODO: This is a complete hack, but necessary for vcore stuff to work for now
15  * The issue is that exit sometimes calls sys_yield(), and we can't recover from
16  * that properly under our vcore model (we shouldn't though).  We really need to
17  * rethink what sys_yield 'should' do when in multicore mode, or else come up 
18  * with a different syscall entirely. */
19 #include <stdlib.h>
20 #include <unistd.h>
21 #undef exit
22 #define exit(status) _exit(status)
23 /*****************************************************************************/
24
25 #define LOG2_MAX_VCORES 6
26 #define MAX_VCORES (1 << LOG2_MAX_VCORES)
27
28 #define TRANSITION_STACK_PAGES 2
29 #define TRANSITION_STACK_SIZE (TRANSITION_STACK_PAGES*PGSIZE)
30
31 /* Defined by glibc; Must be implemented by a user level threading library */
32 extern void vcore_entry();
33 /* Defined in glibc's start.c */
34 extern __thread bool __vcore_context;
35 extern __thread int __vcoreid;
36 /* Defined in vcore.c */
37 extern __thread struct syscall __vcore_one_sysc;        /* see sys_change_vcore */
38
39 /* Vcore API functions */
40 static inline size_t max_vcores(void);
41 static inline size_t num_vcores(void);
42 static inline int vcore_id(void);
43 static inline bool in_vcore_context(void);
44 static inline bool in_multi_mode(void);
45 static inline void __enable_notifs(uint32_t vcoreid);
46 static inline void __disable_notifs(uint32_t vcoreid);
47 static inline bool notif_is_enabled(uint32_t vcoreid);
48 static inline bool vcore_is_mapped(uint32_t vcoreid);
49 static inline bool vcore_is_preempted(uint32_t vcoreid);
50 static inline struct preempt_data *vcpd_of(uint32_t vcoreid);
51 static inline bool preempt_is_pending(uint32_t vcoreid);
52 static inline bool __preempt_is_pending(uint32_t vcoreid);
53 void vcore_init(void);
54 void vcore_event_init(void);
55 void vcore_change_to_m(void);
56 int vcore_request(long nr_new_vcores);
57 void vcore_yield(bool preempt_pending);
58 bool clear_notif_pending(uint32_t vcoreid);
59 void enable_notifs(uint32_t vcoreid);
60 void disable_notifs(uint32_t vcoreid);
61 void vcore_idle(void);
62 void ensure_vcore_runs(uint32_t vcoreid);
63 void cpu_relax_vc(uint32_t vcoreid);
64 uint32_t get_vcoreid(void);
65 bool check_vcoreid(const char *str, uint32_t vcoreid);
66
67 /* Static inlines */
68 static inline size_t max_vcores(void)
69 {
70         return MIN(__procinfo.max_vcores, MAX_VCORES);
71 }
72
73 static inline size_t num_vcores(void)
74 {
75         return __procinfo.num_vcores;
76 }
77
78 static inline int vcore_id(void)
79 {
80         return __vcoreid;
81 }
82
83 static inline bool in_vcore_context(void)
84 {
85         return __vcore_context;
86 }
87
88 static inline bool in_multi_mode(void)
89 {
90         return __procinfo.is_mcp;
91 }
92
93 /* Only call this if you know what you are doing. */
94 static inline void __enable_notifs(uint32_t vcoreid)
95 {
96         vcpd_of(vcoreid)->notif_disabled = FALSE;
97 }
98
99 static inline void __disable_notifs(uint32_t vcoreid)
100 {
101         vcpd_of(vcoreid)->notif_disabled = TRUE;
102 }
103
104 static inline bool notif_is_enabled(uint32_t vcoreid)
105 {
106         return !vcpd_of(vcoreid)->notif_disabled;
107 }
108
109 static inline bool vcore_is_mapped(uint32_t vcoreid)
110 {
111         return __procinfo.vcoremap[vcoreid].valid;
112 }
113
114 /* We could also check for VC_K_LOCK, but that's a bit much. */
115 static inline bool vcore_is_preempted(uint32_t vcoreid)
116 {
117         struct preempt_data *vcpd = vcpd_of(vcoreid);
118         return atomic_read(&vcpd->flags) & VC_PREEMPTED;
119 }
120
121 static inline struct preempt_data *vcpd_of(uint32_t vcoreid)
122 {
123         return &__procdata.vcore_preempt_data[vcoreid];
124 }
125
126 /* Uthread's can call this in case they care if a preemption is coming.  If a
127  * preempt is incoming, this will return TRUE, if you are in uthread context.  A
128  * reasonable response for a uthread is to yield, and vcore_entry will deal with
129  * the preempt pending.
130  *
131  * If you call this from vcore context, it will do nothing.  In general, it's
132  * not safe to just yield (or do whatever you plan on doing) from arbitrary
133  * places in vcore context.  So we just lie about PP. */
134 static inline bool preempt_is_pending(uint32_t vcoreid)
135 {
136         if (in_vcore_context())
137                 return FALSE;
138         return __preempt_is_pending(vcoreid);
139 }
140
141 static inline bool __preempt_is_pending(uint32_t vcoreid)
142 {
143         return __procinfo.vcoremap[vcoreid].preempt_pending;
144 }
145
146
147 #ifndef __PIC__
148
149 #define begin_safe_access_tls_vars()
150
151 #define end_safe_access_tls_vars()
152
153 #else
154
155 #include <features.h>
156 #if __GNUC_PREREQ(4,4)
157
158 /* These macro acrobatics trick the compiler into not caching the (linear)
159  * address of TLS variables across loads/stores of the TLS descriptor, in lieu
160  * of a "TLS cmb()". */
161 #define begin_safe_access_tls_vars()                                           \
162         void __attribute__((noinline, optimize("O0")))                             \
163         safe_access_tls_var_internal() {                                           \
164                 asm("");                                                               \
165
166 #define end_safe_access_tls_vars()                                             \
167         } safe_access_tls_var_internal();                                          \
168
169 #else
170
171 #define begin_safe_access_tls_vars()                                           \
172         printf("ERROR: For PIC use gcc 4.4 or above for tls support!\n");          \
173         printf("ERROR: As a quick fix, recompile your app with -static...\n");     \
174         exit(2);
175
176 #define end_safe_access_tls_vars()                                             \
177         printf("Will never be called because we abort above!");                    \
178         exit(2);
179
180 #endif //__GNUC_PREREQ
181 #endif // __PIC__
182
183 /* Switches into the TLS 'tls_desc'.  Capable of being called from either
184  * uthread or vcore context.  Pairs with end_access_tls_vars(). */
185 #define begin_access_tls_vars(tls_desc)                                        \
186 {                                                                              \
187         struct uthread *caller;                                                    \
188         uint32_t vcoreid;                                                          \
189         void *temp_tls_desc;                                                       \
190         bool invcore = in_vcore_context();                                         \
191         if (!invcore) {                                                            \
192                 caller = current_uthread;                                              \
193                 /* If you have no current_uthread, you might be called too early in the
194                  * process's lifetime.  Make sure something like uthread_slim_init() has
195                  * been run. */                                                        \
196                 assert(caller);                                                        \
197                 /* We need to disable notifs here (in addition to not migrating), since
198                  * we could get interrupted when we're in the other TLS, and when the
199                  * vcore restarts us, it will put us in our old TLS, not the one we were
200                  * in when we were interrupted.  We need to not migrate, since once we
201                  * know the vcoreid, we depend on being on the same vcore throughout.*/\
202                 caller->flags |= UTHREAD_DONT_MIGRATE;                                 \
203                 /* Not concerned about cross-core memory ordering, so no CPU mbs needed.
204                  * The cmb is to prevent the compiler from issuing the vcore read before
205                  * the DONT_MIGRATE write. */                                          \
206                 cmb();                                                                 \
207                 vcoreid = vcore_id();                                                  \
208                 disable_notifs(vcoreid);                                               \
209         } else { /* vcore context */                                               \
210                 vcoreid = vcore_id();                                                  \
211         }                                                                          \
212         temp_tls_desc = get_tls_desc(vcoreid);                                     \
213         set_tls_desc(tls_desc, vcoreid);                                           \
214         begin_safe_access_tls_vars();
215
216 #define end_access_tls_vars()                                                  \
217         end_safe_access_tls_vars();                                                \
218         set_tls_desc(temp_tls_desc, vcoreid);                                      \
219         if (!invcore) {                                                            \
220                 /* Note we reenable migration before enabling notifs, which is reverse
221                  * from how we disabled notifs.  We must enabling migration before
222                  * enabling notifs.  See 6c7fb12 and 5e4825eb4 for details. */         \
223                 caller->flags &= ~UTHREAD_DONT_MIGRATE;                                \
224                 cmb();  /* turn off DONT_MIGRATE before enabling notifs */             \
225                 if (in_multi_mode())                                                   \
226                         enable_notifs(vcoreid);                                            \
227         }                                                                          \
228 }
229
230 #define safe_set_tls_var(name, val)                                            \
231 ({                                                                             \
232         begin_safe_access_tls_vars();                                              \
233         name = val;                                                                \
234         end_safe_access_tls_vars();                                                \
235 })
236
237 #define safe_get_tls_var(name)                                                 \
238 ({                                                                             \
239         typeof(name) __val;                                                        \
240         begin_safe_access_tls_vars();                                              \
241         __val = name;                                                              \
242         end_safe_access_tls_vars();                                                \
243         __val;                                                                     \
244 })
245
246 #define vcore_set_tls_var(name, val)                                           \
247 ({                                                                             \
248         extern void** vcore_thread_control_blocks;                                 \
249         typeof(val) __val = val;                                                   \
250         begin_access_tls_vars(vcore_thread_control_blocks[vcoreid]);               \
251         name = __val;                                                              \
252         end_access_tls_vars();                                                     \
253 })
254
255 #define vcore_get_tls_var(name)                                                \
256 ({                                                                             \
257         typeof(name) val;                                                          \
258         begin_access_tls_vars(vcore_tls_descs[vcoreid]);                           \
259         val = name;                                                                \
260         end_access_tls_vars();                                                     \
261         val;                                                                       \
262 })
263
264 #ifdef __cplusplus
265 }
266 #endif
267
268 #endif