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