1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // kernel.c -- |
---|
8 | // |
---|
9 | // Author : Thierry Delisle |
---|
10 | // Created On : Tue Jan 17 12:27:26 2017 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Mon Aug 31 07:08:20 2020 |
---|
13 | // Update Count : 71 |
---|
14 | // |
---|
15 | |
---|
16 | #define __cforall_thread__ |
---|
17 | // #define __CFA_DEBUG_PRINT_RUNTIME_CORE__ |
---|
18 | |
---|
19 | //C Includes |
---|
20 | #include <errno.h> |
---|
21 | #include <stdio.h> |
---|
22 | #include <signal.h> |
---|
23 | #include <unistd.h> |
---|
24 | extern "C" { |
---|
25 | #include <sys/eventfd.h> |
---|
26 | } |
---|
27 | |
---|
28 | //CFA Includes |
---|
29 | #include "kernel_private.hfa" |
---|
30 | #include "preemption.hfa" |
---|
31 | |
---|
32 | //Private includes |
---|
33 | #define __CFA_INVOKE_PRIVATE__ |
---|
34 | #include "invoke.h" |
---|
35 | |
---|
36 | #if !defined(__CFA_NO_STATISTICS__) |
---|
37 | #define __STATS( ...) __VA_ARGS__ |
---|
38 | #else |
---|
39 | #define __STATS( ...) |
---|
40 | #endif |
---|
41 | |
---|
42 | //----------------------------------------------------------------------------- |
---|
43 | // Some assembly required |
---|
44 | #if defined( __i386 ) |
---|
45 | // mxcr : SSE Status and Control bits (control bits are preserved across function calls) |
---|
46 | // fcw : X87 FPU control word (preserved across function calls) |
---|
47 | #define __x87_store \ |
---|
48 | uint32_t __mxcr; \ |
---|
49 | uint16_t __fcw; \ |
---|
50 | __asm__ volatile ( \ |
---|
51 | "stmxcsr %0\n" \ |
---|
52 | "fnstcw %1\n" \ |
---|
53 | : "=m" (__mxcr),\ |
---|
54 | "=m" (__fcw) \ |
---|
55 | ) |
---|
56 | |
---|
57 | #define __x87_load \ |
---|
58 | __asm__ volatile ( \ |
---|
59 | "fldcw %1\n" \ |
---|
60 | "ldmxcsr %0\n" \ |
---|
61 | ::"m" (__mxcr),\ |
---|
62 | "m" (__fcw) \ |
---|
63 | ) |
---|
64 | |
---|
65 | #elif defined( __x86_64 ) |
---|
66 | #define __x87_store \ |
---|
67 | uint32_t __mxcr; \ |
---|
68 | uint16_t __fcw; \ |
---|
69 | __asm__ volatile ( \ |
---|
70 | "stmxcsr %0\n" \ |
---|
71 | "fnstcw %1\n" \ |
---|
72 | : "=m" (__mxcr),\ |
---|
73 | "=m" (__fcw) \ |
---|
74 | ) |
---|
75 | |
---|
76 | #define __x87_load \ |
---|
77 | __asm__ volatile ( \ |
---|
78 | "fldcw %1\n" \ |
---|
79 | "ldmxcsr %0\n" \ |
---|
80 | :: "m" (__mxcr),\ |
---|
81 | "m" (__fcw) \ |
---|
82 | ) |
---|
83 | |
---|
84 | #elif defined( __arm__ ) |
---|
85 | #define __x87_store |
---|
86 | #define __x87_load |
---|
87 | |
---|
88 | #elif defined( __aarch64__ ) |
---|
89 | #define __x87_store \ |
---|
90 | uint32_t __fpcntl[2]; \ |
---|
91 | __asm__ volatile ( \ |
---|
92 | "mrs x9, FPCR\n" \ |
---|
93 | "mrs x10, FPSR\n" \ |
---|
94 | "stp x9, x10, %0\n" \ |
---|
95 | : "=m" (__fpcntl) : : "x9", "x10" \ |
---|
96 | ) |
---|
97 | |
---|
98 | #define __x87_load \ |
---|
99 | __asm__ volatile ( \ |
---|
100 | "ldp x9, x10, %0\n" \ |
---|
101 | "msr FPSR, x10\n" \ |
---|
102 | "msr FPCR, x9\n" \ |
---|
103 | : "=m" (__fpcntl) : : "x9", "x10" \ |
---|
104 | ) |
---|
105 | |
---|
106 | #else |
---|
107 | #error unsupported hardware architecture |
---|
108 | #endif |
---|
109 | |
---|
110 | extern $thread * mainThread; |
---|
111 | extern processor * mainProcessor; |
---|
112 | |
---|
113 | //----------------------------------------------------------------------------- |
---|
114 | // Kernel Scheduling logic |
---|
115 | static $thread * __next_thread(cluster * this); |
---|
116 | static $thread * __next_thread_slow(cluster * this); |
---|
117 | static inline bool __must_unpark( $thread * thrd ) __attribute((nonnull(1))); |
---|
118 | static void __run_thread(processor * this, $thread * dst); |
---|
119 | static void __wake_one(cluster * cltr); |
---|
120 | |
---|
121 | static void mark_idle (__cluster_proc_list & idles, processor & proc); |
---|
122 | static void mark_awake(__cluster_proc_list & idles, processor & proc); |
---|
123 | static [unsigned idle, unsigned total, * processor] query_idles( & __cluster_proc_list idles ); |
---|
124 | |
---|
125 | extern void __cfa_io_start( processor * ); |
---|
126 | extern bool __cfa_io_drain( processor * ); |
---|
127 | extern void __cfa_io_flush( processor * ); |
---|
128 | extern void __cfa_io_stop ( processor * ); |
---|
129 | static inline bool __maybe_io_drain( processor * ); |
---|
130 | |
---|
131 | extern void __disable_interrupts_hard(); |
---|
132 | extern void __enable_interrupts_hard(); |
---|
133 | |
---|
134 | static inline void __disable_interrupts_checked() { |
---|
135 | /* paranoid */ verify( __preemption_enabled() ); |
---|
136 | disable_interrupts(); |
---|
137 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
138 | } |
---|
139 | |
---|
140 | static inline void __enable_interrupts_checked( bool poll = true ) { |
---|
141 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
142 | enable_interrupts( poll ); |
---|
143 | /* paranoid */ verify( __preemption_enabled() ); |
---|
144 | } |
---|
145 | |
---|
146 | //============================================================================================= |
---|
147 | // Kernel Scheduling logic |
---|
148 | //============================================================================================= |
---|
149 | //Main of the processor contexts |
---|
150 | void main(processorCtx_t & runner) { |
---|
151 | // Because of a bug, we couldn't initialized the seed on construction |
---|
152 | // Do it here |
---|
153 | __cfaabi_tls.rand_seed ^= rdtscl(); |
---|
154 | __cfaabi_tls.ready_rng.fwd_seed = 25214903917_l64u * (rdtscl() ^ (uintptr_t)&runner); |
---|
155 | __tls_rand_advance_bck(); |
---|
156 | |
---|
157 | processor * this = runner.proc; |
---|
158 | verify(this); |
---|
159 | |
---|
160 | __cfa_io_start( this ); |
---|
161 | |
---|
162 | __cfadbg_print_safe(runtime_core, "Kernel : core %p starting\n", this); |
---|
163 | #if !defined(__CFA_NO_STATISTICS__) |
---|
164 | if( this->print_halts ) { |
---|
165 | __cfaabi_bits_print_safe( STDOUT_FILENO, "Processor : %d - %s (%p)\n", this->id, this->name, (void*)this); |
---|
166 | } |
---|
167 | #endif |
---|
168 | |
---|
169 | { |
---|
170 | // Setup preemption data |
---|
171 | preemption_scope scope = { this }; |
---|
172 | |
---|
173 | __STATS( unsigned long long last_tally = rdtscl(); ) |
---|
174 | |
---|
175 | // if we need to run some special setup, now is the time to do it. |
---|
176 | if(this->init.thrd) { |
---|
177 | this->init.thrd->curr_cluster = this->cltr; |
---|
178 | __run_thread(this, this->init.thrd); |
---|
179 | } |
---|
180 | |
---|
181 | __cfadbg_print_safe(runtime_core, "Kernel : core %p started\n", this); |
---|
182 | |
---|
183 | $thread * readyThread = 0p; |
---|
184 | MAIN_LOOP: |
---|
185 | for() { |
---|
186 | // Check if there is pending io |
---|
187 | __maybe_io_drain( this ); |
---|
188 | |
---|
189 | // Try to get the next thread |
---|
190 | readyThread = __next_thread( this->cltr ); |
---|
191 | |
---|
192 | if( !readyThread ) { |
---|
193 | __cfa_io_flush( this ); |
---|
194 | readyThread = __next_thread_slow( this->cltr ); |
---|
195 | } |
---|
196 | |
---|
197 | HALT: |
---|
198 | if( !readyThread ) { |
---|
199 | // Don't block if we are done |
---|
200 | if( __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST) ) break MAIN_LOOP; |
---|
201 | |
---|
202 | #if !defined(__CFA_NO_STATISTICS__) |
---|
203 | __tls_stats()->ready.sleep.halts++; |
---|
204 | #endif |
---|
205 | |
---|
206 | // Push self to idle stack |
---|
207 | mark_idle(this->cltr->procs, * this); |
---|
208 | |
---|
209 | // Confirm the ready-queue is empty |
---|
210 | readyThread = __next_thread_slow( this->cltr ); |
---|
211 | if( readyThread ) { |
---|
212 | // A thread was found, cancel the halt |
---|
213 | mark_awake(this->cltr->procs, * this); |
---|
214 | |
---|
215 | #if !defined(__CFA_NO_STATISTICS__) |
---|
216 | __tls_stats()->ready.sleep.cancels++; |
---|
217 | #endif |
---|
218 | |
---|
219 | // continue the mai loop |
---|
220 | break HALT; |
---|
221 | } |
---|
222 | |
---|
223 | #if !defined(__CFA_NO_STATISTICS__) |
---|
224 | if(this->print_halts) { |
---|
225 | __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 0\n", this->id, rdtscl()); |
---|
226 | } |
---|
227 | #endif |
---|
228 | |
---|
229 | __cfadbg_print_safe(runtime_core, "Kernel : core %p waiting on eventfd %d\n", this, this->idle); |
---|
230 | |
---|
231 | __disable_interrupts_hard(); |
---|
232 | eventfd_t val; |
---|
233 | eventfd_read( this->idle, &val ); |
---|
234 | __enable_interrupts_hard(); |
---|
235 | |
---|
236 | #if !defined(__CFA_NO_STATISTICS__) |
---|
237 | if(this->print_halts) { |
---|
238 | __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 1\n", this->id, rdtscl()); |
---|
239 | } |
---|
240 | #endif |
---|
241 | |
---|
242 | // We were woken up, remove self from idle |
---|
243 | mark_awake(this->cltr->procs, * this); |
---|
244 | |
---|
245 | // DON'T just proceed, start looking again |
---|
246 | continue MAIN_LOOP; |
---|
247 | } |
---|
248 | |
---|
249 | /* paranoid */ verify( readyThread ); |
---|
250 | |
---|
251 | // Reset io dirty bit |
---|
252 | this->io.dirty = false; |
---|
253 | |
---|
254 | // We found a thread run it |
---|
255 | __run_thread(this, readyThread); |
---|
256 | |
---|
257 | // Are we done? |
---|
258 | if( __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST) ) break MAIN_LOOP; |
---|
259 | |
---|
260 | #if !defined(__CFA_NO_STATISTICS__) |
---|
261 | unsigned long long curr = rdtscl(); |
---|
262 | if(curr > (last_tally + 500000000)) { |
---|
263 | __tally_stats(this->cltr->stats, __cfaabi_tls.this_stats); |
---|
264 | last_tally = curr; |
---|
265 | } |
---|
266 | #endif |
---|
267 | |
---|
268 | if(this->io.pending && !this->io.dirty) { |
---|
269 | __cfa_io_flush( this ); |
---|
270 | } |
---|
271 | |
---|
272 | // SEARCH: { |
---|
273 | // /* paranoid */ verify( ! __preemption_enabled() ); |
---|
274 | // /* paranoid */ verify( kernelTLS().this_proc_id ); |
---|
275 | |
---|
276 | // // First, lock the scheduler since we are searching for a thread |
---|
277 | |
---|
278 | // // Try to get the next thread |
---|
279 | // ready_schedule_lock(); |
---|
280 | // readyThread = pop_fast( this->cltr ); |
---|
281 | // ready_schedule_unlock(); |
---|
282 | // if(readyThread) { break SEARCH; } |
---|
283 | |
---|
284 | // // If we can't find a thread, might as well flush any outstanding I/O |
---|
285 | // if(this->io.pending) { __cfa_io_flush( this ); } |
---|
286 | |
---|
287 | // // Spin a little on I/O, just in case |
---|
288 | // for(25) { |
---|
289 | // __maybe_io_drain( this ); |
---|
290 | // ready_schedule_lock(); |
---|
291 | // readyThread = pop_fast( this->cltr ); |
---|
292 | // ready_schedule_unlock(); |
---|
293 | // if(readyThread) { break SEARCH; } |
---|
294 | // } |
---|
295 | |
---|
296 | // // no luck, try stealing a few times |
---|
297 | // for(25) { |
---|
298 | // if( __maybe_io_drain( this ) ) { |
---|
299 | // ready_schedule_lock(); |
---|
300 | // readyThread = pop_fast( this->cltr ); |
---|
301 | // } else { |
---|
302 | // ready_schedule_lock(); |
---|
303 | // readyThread = pop_slow( this->cltr ); |
---|
304 | // } |
---|
305 | // ready_schedule_unlock(); |
---|
306 | // if(readyThread) { break SEARCH; } |
---|
307 | // } |
---|
308 | |
---|
309 | // // still no luck, search for a thread |
---|
310 | // ready_schedule_lock(); |
---|
311 | // readyThread = pop_search( this->cltr ); |
---|
312 | // ready_schedule_unlock(); |
---|
313 | // if(readyThread) { break SEARCH; } |
---|
314 | |
---|
315 | // // Don't block if we are done |
---|
316 | // if( __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST) ) break MAIN_LOOP; |
---|
317 | |
---|
318 | // __STATS( __tls_stats()->ready.sleep.halts++; ) |
---|
319 | |
---|
320 | // // Push self to idle stack |
---|
321 | // mark_idle(this->cltr->procs, * this); |
---|
322 | |
---|
323 | // // Confirm the ready-queue is empty |
---|
324 | // __maybe_io_drain( this ); |
---|
325 | // ready_schedule_lock(); |
---|
326 | // readyThread = pop_search( this->cltr ); |
---|
327 | // ready_schedule_unlock(); |
---|
328 | |
---|
329 | // if( readyThread ) { |
---|
330 | // // A thread was found, cancel the halt |
---|
331 | // mark_awake(this->cltr->procs, * this); |
---|
332 | |
---|
333 | // __STATS( __tls_stats()->ready.sleep.cancels++; ) |
---|
334 | |
---|
335 | // // continue the main loop |
---|
336 | // break SEARCH; |
---|
337 | // } |
---|
338 | |
---|
339 | // __STATS( if(this->print_halts) __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 0\n", this->id, rdtscl()); ) |
---|
340 | // __cfadbg_print_safe(runtime_core, "Kernel : core %p waiting on eventfd %d\n", this, this->idle); |
---|
341 | |
---|
342 | // // __disable_interrupts_hard(); |
---|
343 | // eventfd_t val; |
---|
344 | // eventfd_read( this->idle, &val ); |
---|
345 | // // __enable_interrupts_hard(); |
---|
346 | |
---|
347 | // __STATS( if(this->print_halts) __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 1\n", this->id, rdtscl()); ) |
---|
348 | |
---|
349 | // // We were woken up, remove self from idle |
---|
350 | // mark_awake(this->cltr->procs, * this); |
---|
351 | |
---|
352 | // // DON'T just proceed, start looking again |
---|
353 | // continue MAIN_LOOP; |
---|
354 | // } |
---|
355 | |
---|
356 | // RUN_THREAD: |
---|
357 | // /* paranoid */ verify( kernelTLS().this_proc_id ); |
---|
358 | // /* paranoid */ verify( ! __preemption_enabled() ); |
---|
359 | // /* paranoid */ verify( readyThread ); |
---|
360 | |
---|
361 | // // Reset io dirty bit |
---|
362 | // this->io.dirty = false; |
---|
363 | |
---|
364 | // // We found a thread run it |
---|
365 | // __run_thread(this, readyThread); |
---|
366 | |
---|
367 | // // Are we done? |
---|
368 | // if( __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST) ) break MAIN_LOOP; |
---|
369 | |
---|
370 | // #if !defined(__CFA_NO_STATISTICS__) |
---|
371 | // unsigned long long curr = rdtscl(); |
---|
372 | // if(curr > (last_tally + 500000000)) { |
---|
373 | // __tally_stats(this->cltr->stats, __cfaabi_tls.this_stats); |
---|
374 | // last_tally = curr; |
---|
375 | // } |
---|
376 | // #endif |
---|
377 | |
---|
378 | // if(this->io.pending && !this->io.dirty) { |
---|
379 | // __cfa_io_flush( this ); |
---|
380 | // } |
---|
381 | |
---|
382 | // // Check if there is pending io |
---|
383 | // __maybe_io_drain( this ); |
---|
384 | } |
---|
385 | |
---|
386 | __cfadbg_print_safe(runtime_core, "Kernel : core %p stopping\n", this); |
---|
387 | } |
---|
388 | |
---|
389 | __cfa_io_stop( this ); |
---|
390 | |
---|
391 | post( this->terminated ); |
---|
392 | |
---|
393 | |
---|
394 | if(this == mainProcessor) { |
---|
395 | // HACK : the coroutine context switch expects this_thread to be set |
---|
396 | // and it make sense for it to be set in all other cases except here |
---|
397 | // fake it |
---|
398 | __cfaabi_tls.this_thread = mainThread; |
---|
399 | } |
---|
400 | |
---|
401 | __cfadbg_print_safe(runtime_core, "Kernel : core %p terminated\n", this); |
---|
402 | } |
---|
403 | |
---|
404 | static int * __volatile_errno() __attribute__((noinline)); |
---|
405 | static int * __volatile_errno() { asm(""); return &errno; } |
---|
406 | |
---|
407 | // KERNEL ONLY |
---|
408 | // runThread runs a thread by context switching |
---|
409 | // from the processor coroutine to the target thread |
---|
410 | static void __run_thread(processor * this, $thread * thrd_dst) { |
---|
411 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
412 | /* paranoid */ verifyf( thrd_dst->state == Ready || thrd_dst->preempted != __NO_PREEMPTION, "state : %d, preempted %d\n", thrd_dst->state, thrd_dst->preempted); |
---|
413 | /* paranoid */ verifyf( thrd_dst->link.next == 0p, "Expected null got %p", thrd_dst->link.next ); |
---|
414 | __builtin_prefetch( thrd_dst->context.SP ); |
---|
415 | |
---|
416 | __cfadbg_print_safe(runtime_core, "Kernel : core %p running thread %p (%s)\n", this, thrd_dst, thrd_dst->self_cor.name); |
---|
417 | |
---|
418 | $coroutine * proc_cor = get_coroutine(this->runner); |
---|
419 | |
---|
420 | // set state of processor coroutine to inactive |
---|
421 | verify(proc_cor->state == Active); |
---|
422 | proc_cor->state = Blocked; |
---|
423 | |
---|
424 | // Actually run the thread |
---|
425 | RUNNING: while(true) { |
---|
426 | thrd_dst->preempted = __NO_PREEMPTION; |
---|
427 | thrd_dst->state = Active; |
---|
428 | |
---|
429 | // Update global state |
---|
430 | kernelTLS().this_thread = thrd_dst; |
---|
431 | |
---|
432 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
433 | /* paranoid */ verify( kernelTLS().this_thread == thrd_dst ); |
---|
434 | /* paranoid */ verify( thrd_dst->curr_cluster == this->cltr ); |
---|
435 | /* paranoid */ verify( thrd_dst->context.SP ); |
---|
436 | /* paranoid */ verify( thrd_dst->state != Halted ); |
---|
437 | /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) < ((uintptr_t)__get_stack(thrd_dst->curr_cor)->base ) || thrd_dst->curr_cor == proc_cor, "ERROR : Destination $thread %p has been corrupted.\n StackPointer too small.\n", thrd_dst ); // add escape condition if we are setting up the processor |
---|
438 | /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) > ((uintptr_t)__get_stack(thrd_dst->curr_cor)->limit) || thrd_dst->curr_cor == proc_cor, "ERROR : Destination $thread %p has been corrupted.\n StackPointer too large.\n", thrd_dst ); // add escape condition if we are setting up the processor |
---|
439 | /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd_dst->canary ); |
---|
440 | |
---|
441 | |
---|
442 | |
---|
443 | // set context switch to the thread that the processor is executing |
---|
444 | __cfactx_switch( &proc_cor->context, &thrd_dst->context ); |
---|
445 | // when __cfactx_switch returns we are back in the processor coroutine |
---|
446 | |
---|
447 | /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd_dst->canary ); |
---|
448 | /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) > ((uintptr_t)__get_stack(thrd_dst->curr_cor)->limit), "ERROR : Destination $thread %p has been corrupted.\n StackPointer too large.\n", thrd_dst ); |
---|
449 | /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) < ((uintptr_t)__get_stack(thrd_dst->curr_cor)->base ), "ERROR : Destination $thread %p has been corrupted.\n StackPointer too small.\n", thrd_dst ); |
---|
450 | /* paranoid */ verify( thrd_dst->context.SP ); |
---|
451 | /* paranoid */ verify( thrd_dst->curr_cluster == this->cltr ); |
---|
452 | /* paranoid */ verify( kernelTLS().this_thread == thrd_dst ); |
---|
453 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
454 | |
---|
455 | // Reset global state |
---|
456 | kernelTLS().this_thread = 0p; |
---|
457 | |
---|
458 | // We just finished running a thread, there are a few things that could have happened. |
---|
459 | // 1 - Regular case : the thread has blocked and now one has scheduled it yet. |
---|
460 | // 2 - Racy case : the thread has blocked but someone has already tried to schedule it. |
---|
461 | // 4 - Preempted |
---|
462 | // In case 1, we may have won a race so we can't write to the state again. |
---|
463 | // In case 2, we lost the race so we now own the thread. |
---|
464 | |
---|
465 | if(unlikely(thrd_dst->preempted != __NO_PREEMPTION)) { |
---|
466 | // The thread was preempted, reschedule it and reset the flag |
---|
467 | schedule_thread$( thrd_dst ); |
---|
468 | break RUNNING; |
---|
469 | } |
---|
470 | |
---|
471 | if(unlikely(thrd_dst->state == Halting)) { |
---|
472 | // The thread has halted, it should never be scheduled/run again |
---|
473 | // finish the thread |
---|
474 | __thread_finish( thrd_dst ); |
---|
475 | break RUNNING; |
---|
476 | } |
---|
477 | |
---|
478 | /* paranoid */ verify( thrd_dst->state == Active ); |
---|
479 | thrd_dst->state = Blocked; |
---|
480 | |
---|
481 | // set state of processor coroutine to active and the thread to inactive |
---|
482 | int old_ticket = __atomic_fetch_sub(&thrd_dst->ticket, 1, __ATOMIC_SEQ_CST); |
---|
483 | switch(old_ticket) { |
---|
484 | case TICKET_RUNNING: |
---|
485 | // This is case 1, the regular case, nothing more is needed |
---|
486 | break RUNNING; |
---|
487 | case TICKET_UNBLOCK: |
---|
488 | #if !defined(__CFA_NO_STATISTICS__) |
---|
489 | __tls_stats()->ready.threads.threads++; |
---|
490 | __push_stat( __tls_stats(), __tls_stats()->ready.threads.threads, false, "Processor", this ); |
---|
491 | #endif |
---|
492 | // This is case 2, the racy case, someone tried to run this thread before it finished blocking |
---|
493 | // In this case, just run it again. |
---|
494 | continue RUNNING; |
---|
495 | default: |
---|
496 | // This makes no sense, something is wrong abort |
---|
497 | abort(); |
---|
498 | } |
---|
499 | } |
---|
500 | |
---|
501 | // Just before returning to the processor, set the processor coroutine to active |
---|
502 | proc_cor->state = Active; |
---|
503 | |
---|
504 | __cfadbg_print_safe(runtime_core, "Kernel : core %p finished running thread %p\n", this, thrd_dst); |
---|
505 | |
---|
506 | #if !defined(__CFA_NO_STATISTICS__) |
---|
507 | __tls_stats()->ready.threads.threads--; |
---|
508 | __push_stat( __tls_stats(), __tls_stats()->ready.threads.threads, false, "Processor", this ); |
---|
509 | #endif |
---|
510 | |
---|
511 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
512 | } |
---|
513 | |
---|
514 | // KERNEL_ONLY |
---|
515 | void returnToKernel() { |
---|
516 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
517 | $coroutine * proc_cor = get_coroutine(kernelTLS().this_processor->runner); |
---|
518 | $thread * thrd_src = kernelTLS().this_thread; |
---|
519 | |
---|
520 | __STATS( thrd_src->last_proc = kernelTLS().this_processor; ) |
---|
521 | |
---|
522 | // Run the thread on this processor |
---|
523 | { |
---|
524 | int local_errno = *__volatile_errno(); |
---|
525 | #if defined( __i386 ) || defined( __x86_64 ) |
---|
526 | __x87_store; |
---|
527 | #endif |
---|
528 | /* paranoid */ verify( proc_cor->context.SP ); |
---|
529 | /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd_src->canary ); |
---|
530 | __cfactx_switch( &thrd_src->context, &proc_cor->context ); |
---|
531 | /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd_src->canary ); |
---|
532 | #if defined( __i386 ) || defined( __x86_64 ) |
---|
533 | __x87_load; |
---|
534 | #endif |
---|
535 | *__volatile_errno() = local_errno; |
---|
536 | } |
---|
537 | |
---|
538 | #if !defined(__CFA_NO_STATISTICS__) |
---|
539 | /* paranoid */ verify( thrd_src->last_proc != 0p ); |
---|
540 | if(thrd_src->last_proc != kernelTLS().this_processor) { |
---|
541 | __tls_stats()->ready.threads.migration++; |
---|
542 | } |
---|
543 | #endif |
---|
544 | |
---|
545 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
546 | /* paranoid */ verifyf( ((uintptr_t)thrd_src->context.SP) < ((uintptr_t)__get_stack(thrd_src->curr_cor)->base ), "ERROR : Returning $thread %p has been corrupted.\n StackPointer too small.\n", thrd_src ); |
---|
547 | /* paranoid */ verifyf( ((uintptr_t)thrd_src->context.SP) > ((uintptr_t)__get_stack(thrd_src->curr_cor)->limit), "ERROR : Returning $thread %p has been corrupted.\n StackPointer too large.\n", thrd_src ); |
---|
548 | } |
---|
549 | |
---|
550 | //----------------------------------------------------------------------------- |
---|
551 | // Scheduler routines |
---|
552 | // KERNEL ONLY |
---|
553 | static void __schedule_thread( $thread * thrd ) { |
---|
554 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
555 | /* paranoid */ verify( kernelTLS().this_proc_id ); |
---|
556 | /* paranoid */ verify( ready_schedule_islocked()); |
---|
557 | /* paranoid */ verify( thrd ); |
---|
558 | /* paranoid */ verify( thrd->state != Halted ); |
---|
559 | /* paranoid */ verify( thrd->curr_cluster ); |
---|
560 | /* paranoid */ #if defined( __CFA_WITH_VERIFY__ ) |
---|
561 | /* paranoid */ if( thrd->state == Blocked || thrd->state == Start ) assertf( thrd->preempted == __NO_PREEMPTION, |
---|
562 | "Error inactive thread marked as preempted, state %d, preemption %d\n", thrd->state, thrd->preempted ); |
---|
563 | /* paranoid */ if( thrd->preempted != __NO_PREEMPTION ) assertf(thrd->state == Active, |
---|
564 | "Error preempted thread marked as not currently running, state %d, preemption %d\n", thrd->state, thrd->preempted ); |
---|
565 | /* paranoid */ #endif |
---|
566 | /* paranoid */ verifyf( thrd->link.next == 0p, "Expected null got %p", thrd->link.next ); |
---|
567 | /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd->canary ); |
---|
568 | |
---|
569 | |
---|
570 | if (thrd->preempted == __NO_PREEMPTION) thrd->state = Ready; |
---|
571 | |
---|
572 | // Dereference the thread now because once we push it, there is not guaranteed it's still valid. |
---|
573 | struct cluster * cl = thrd->curr_cluster; |
---|
574 | __STATS(bool outside = thrd->last_proc && thrd->last_proc != kernelTLS().this_processor; ) |
---|
575 | |
---|
576 | // push the thread to the cluster ready-queue |
---|
577 | push( cl, thrd ); |
---|
578 | |
---|
579 | // variable thrd is no longer safe to use |
---|
580 | thrd = 0xdeaddeaddeaddeadp; |
---|
581 | |
---|
582 | // wake the cluster using the save variable. |
---|
583 | __wake_one( cl ); |
---|
584 | |
---|
585 | #if !defined(__CFA_NO_STATISTICS__) |
---|
586 | if( kernelTLS().this_stats ) { |
---|
587 | __tls_stats()->ready.threads.threads++; |
---|
588 | if(outside) { |
---|
589 | __tls_stats()->ready.threads.extunpark++; |
---|
590 | } |
---|
591 | __push_stat( __tls_stats(), __tls_stats()->ready.threads.threads, false, "Processor", kernelTLS().this_processor ); |
---|
592 | } |
---|
593 | else { |
---|
594 | __atomic_fetch_add(&cl->stats->ready.threads.threads, 1, __ATOMIC_RELAXED); |
---|
595 | __atomic_fetch_add(&cl->stats->ready.threads.extunpark, 1, __ATOMIC_RELAXED); |
---|
596 | __push_stat( cl->stats, cl->stats->ready.threads.threads, true, "Cluster", cl ); |
---|
597 | } |
---|
598 | #endif |
---|
599 | |
---|
600 | /* paranoid */ verify( ready_schedule_islocked()); |
---|
601 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
602 | } |
---|
603 | |
---|
604 | void schedule_thread$( $thread * thrd ) { |
---|
605 | ready_schedule_lock(); |
---|
606 | __schedule_thread( thrd ); |
---|
607 | ready_schedule_unlock(); |
---|
608 | } |
---|
609 | |
---|
610 | // KERNEL ONLY |
---|
611 | static inline $thread * __next_thread(cluster * this) with( *this ) { |
---|
612 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
613 | /* paranoid */ verify( kernelTLS().this_proc_id ); |
---|
614 | |
---|
615 | ready_schedule_lock(); |
---|
616 | $thread * thrd = pop_fast( this ); |
---|
617 | ready_schedule_unlock(); |
---|
618 | |
---|
619 | /* paranoid */ verify( kernelTLS().this_proc_id ); |
---|
620 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
621 | return thrd; |
---|
622 | } |
---|
623 | |
---|
624 | // KERNEL ONLY |
---|
625 | static inline $thread * __next_thread_slow(cluster * this) with( *this ) { |
---|
626 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
627 | /* paranoid */ verify( kernelTLS().this_proc_id ); |
---|
628 | |
---|
629 | ready_schedule_lock(); |
---|
630 | $thread * thrd; |
---|
631 | for(25) { |
---|
632 | thrd = pop_slow( this ); |
---|
633 | if(thrd) goto RET; |
---|
634 | } |
---|
635 | thrd = pop_search( this ); |
---|
636 | |
---|
637 | RET: |
---|
638 | ready_schedule_unlock(); |
---|
639 | |
---|
640 | /* paranoid */ verify( kernelTLS().this_proc_id ); |
---|
641 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
642 | return thrd; |
---|
643 | } |
---|
644 | |
---|
645 | static inline bool __must_unpark( $thread * thrd ) { |
---|
646 | int old_ticket = __atomic_fetch_add(&thrd->ticket, 1, __ATOMIC_SEQ_CST); |
---|
647 | switch(old_ticket) { |
---|
648 | case TICKET_RUNNING: |
---|
649 | // Wake won the race, the thread will reschedule/rerun itself |
---|
650 | return false; |
---|
651 | case TICKET_BLOCKED: |
---|
652 | /* paranoid */ verify( ! thrd->preempted != __NO_PREEMPTION ); |
---|
653 | /* paranoid */ verify( thrd->state == Blocked ); |
---|
654 | return true; |
---|
655 | default: |
---|
656 | // This makes no sense, something is wrong abort |
---|
657 | abort("Thread %p (%s) has mismatch park/unpark\n", thrd, thrd->self_cor.name); |
---|
658 | } |
---|
659 | } |
---|
660 | |
---|
661 | void __kernel_unpark( $thread * thrd ) { |
---|
662 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
663 | /* paranoid */ verify( ready_schedule_islocked()); |
---|
664 | |
---|
665 | if( !thrd ) return; |
---|
666 | |
---|
667 | if(__must_unpark(thrd)) { |
---|
668 | // Wake lost the race, |
---|
669 | __schedule_thread( thrd ); |
---|
670 | } |
---|
671 | |
---|
672 | /* paranoid */ verify( ready_schedule_islocked()); |
---|
673 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
674 | } |
---|
675 | |
---|
676 | void unpark( $thread * thrd ) { |
---|
677 | if( !thrd ) return; |
---|
678 | |
---|
679 | if(__must_unpark(thrd)) { |
---|
680 | disable_interrupts(); |
---|
681 | // Wake lost the race, |
---|
682 | schedule_thread$( thrd ); |
---|
683 | enable_interrupts(false); |
---|
684 | } |
---|
685 | } |
---|
686 | |
---|
687 | void park( void ) { |
---|
688 | __disable_interrupts_checked(); |
---|
689 | /* paranoid */ verify( kernelTLS().this_thread->preempted == __NO_PREEMPTION ); |
---|
690 | returnToKernel(); |
---|
691 | __enable_interrupts_checked(); |
---|
692 | |
---|
693 | } |
---|
694 | |
---|
695 | extern "C" { |
---|
696 | // Leave the thread monitor |
---|
697 | // last routine called by a thread. |
---|
698 | // Should never return |
---|
699 | void __cfactx_thrd_leave() { |
---|
700 | $thread * thrd = active_thread(); |
---|
701 | $monitor * this = &thrd->self_mon; |
---|
702 | |
---|
703 | // Lock the monitor now |
---|
704 | lock( this->lock __cfaabi_dbg_ctx2 ); |
---|
705 | |
---|
706 | disable_interrupts(); |
---|
707 | |
---|
708 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
709 | /* paranoid */ verify( thrd->state == Active ); |
---|
710 | /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd->canary ); |
---|
711 | /* paranoid */ verify( kernelTLS().this_thread == thrd ); |
---|
712 | /* paranoid */ verify( thrd->context.SP ); |
---|
713 | /* paranoid */ verifyf( ((uintptr_t)thrd->context.SP) > ((uintptr_t)__get_stack(thrd->curr_cor)->limit), "ERROR : $thread %p has been corrupted.\n StackPointer too large.\n", thrd ); |
---|
714 | /* paranoid */ verifyf( ((uintptr_t)thrd->context.SP) < ((uintptr_t)__get_stack(thrd->curr_cor)->base ), "ERROR : $thread %p has been corrupted.\n StackPointer too small.\n", thrd ); |
---|
715 | |
---|
716 | thrd->state = Halting; |
---|
717 | if( TICKET_RUNNING != thrd->ticket ) { abort( "Thread terminated with pending unpark" ); } |
---|
718 | if( thrd != this->owner ) { abort( "Thread internal monitor has incorrect owner" ); } |
---|
719 | if( this->recursion != 1) { abort( "Thread internal monitor has unbalanced recursion" ); } |
---|
720 | |
---|
721 | // Leave the thread |
---|
722 | returnToKernel(); |
---|
723 | |
---|
724 | // Control flow should never reach here! |
---|
725 | abort(); |
---|
726 | } |
---|
727 | } |
---|
728 | |
---|
729 | // KERNEL ONLY |
---|
730 | bool force_yield( __Preemption_Reason reason ) { |
---|
731 | __disable_interrupts_checked(); |
---|
732 | $thread * thrd = kernelTLS().this_thread; |
---|
733 | /* paranoid */ verify(thrd->state == Active); |
---|
734 | |
---|
735 | // SKULLDUGGERY: It is possible that we are preempting this thread just before |
---|
736 | // it was going to park itself. If that is the case and it is already using the |
---|
737 | // intrusive fields then we can't use them to preempt the thread |
---|
738 | // If that is the case, abandon the preemption. |
---|
739 | bool preempted = false; |
---|
740 | if(thrd->link.next == 0p) { |
---|
741 | preempted = true; |
---|
742 | thrd->preempted = reason; |
---|
743 | returnToKernel(); |
---|
744 | } |
---|
745 | __enable_interrupts_checked( false ); |
---|
746 | return preempted; |
---|
747 | } |
---|
748 | |
---|
749 | //============================================================================================= |
---|
750 | // Kernel Idle Sleep |
---|
751 | //============================================================================================= |
---|
752 | // Wake a thread from the front if there are any |
---|
753 | static void __wake_one(cluster * this) { |
---|
754 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
755 | /* paranoid */ verify( ready_schedule_islocked() ); |
---|
756 | |
---|
757 | // Check if there is a sleeping processor |
---|
758 | processor * p; |
---|
759 | unsigned idle; |
---|
760 | unsigned total; |
---|
761 | [idle, total, p] = query_idles(this->procs); |
---|
762 | |
---|
763 | // If no one is sleeping, we are done |
---|
764 | if( idle == 0 ) return; |
---|
765 | |
---|
766 | // We found a processor, wake it up |
---|
767 | eventfd_t val; |
---|
768 | val = 1; |
---|
769 | eventfd_write( p->idle, val ); |
---|
770 | |
---|
771 | #if !defined(__CFA_NO_STATISTICS__) |
---|
772 | if( kernelTLS().this_stats ) { |
---|
773 | __tls_stats()->ready.sleep.wakes++; |
---|
774 | } |
---|
775 | else { |
---|
776 | __atomic_fetch_add(&this->stats->ready.sleep.wakes, 1, __ATOMIC_RELAXED); |
---|
777 | } |
---|
778 | #endif |
---|
779 | |
---|
780 | /* paranoid */ verify( ready_schedule_islocked() ); |
---|
781 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
782 | |
---|
783 | return; |
---|
784 | } |
---|
785 | |
---|
786 | // Unconditionnaly wake a thread |
---|
787 | void __wake_proc(processor * this) { |
---|
788 | __cfadbg_print_safe(runtime_core, "Kernel : waking Processor %p\n", this); |
---|
789 | |
---|
790 | __disable_interrupts_checked(); |
---|
791 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
792 | eventfd_t val; |
---|
793 | val = 1; |
---|
794 | eventfd_write( this->idle, val ); |
---|
795 | __enable_interrupts_checked(); |
---|
796 | } |
---|
797 | |
---|
798 | static void mark_idle(__cluster_proc_list & this, processor & proc) { |
---|
799 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
800 | lock( this ); |
---|
801 | this.idle++; |
---|
802 | /* paranoid */ verify( this.idle <= this.total ); |
---|
803 | remove(proc); |
---|
804 | insert_first(this.idles, proc); |
---|
805 | unlock( this ); |
---|
806 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
807 | } |
---|
808 | |
---|
809 | static void mark_awake(__cluster_proc_list & this, processor & proc) { |
---|
810 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
811 | lock( this ); |
---|
812 | this.idle--; |
---|
813 | /* paranoid */ verify( this.idle >= 0 ); |
---|
814 | remove(proc); |
---|
815 | insert_last(this.actives, proc); |
---|
816 | unlock( this ); |
---|
817 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
818 | } |
---|
819 | |
---|
820 | static [unsigned idle, unsigned total, * processor] query_idles( & __cluster_proc_list this ) { |
---|
821 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
822 | /* paranoid */ verify( ready_schedule_islocked() ); |
---|
823 | |
---|
824 | for() { |
---|
825 | uint64_t l = __atomic_load_n(&this.lock, __ATOMIC_SEQ_CST); |
---|
826 | if( 1 == (l % 2) ) { Pause(); continue; } |
---|
827 | unsigned idle = this.idle; |
---|
828 | unsigned total = this.total; |
---|
829 | processor * proc = &this.idles`first; |
---|
830 | // Compiler fence is unnecessary, but gcc-8 and older incorrectly reorder code without it |
---|
831 | asm volatile("": : :"memory"); |
---|
832 | if(l != __atomic_load_n(&this.lock, __ATOMIC_SEQ_CST)) { Pause(); continue; } |
---|
833 | return [idle, total, proc]; |
---|
834 | } |
---|
835 | |
---|
836 | /* paranoid */ verify( ready_schedule_islocked() ); |
---|
837 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
838 | } |
---|
839 | |
---|
840 | //============================================================================================= |
---|
841 | // Unexpected Terminating logic |
---|
842 | //============================================================================================= |
---|
843 | void __kernel_abort_msg( char * abort_text, int abort_text_size ) { |
---|
844 | $thread * thrd = __cfaabi_tls.this_thread; |
---|
845 | |
---|
846 | if(thrd) { |
---|
847 | int len = snprintf( abort_text, abort_text_size, "Error occurred while executing thread %.256s (%p)", thrd->self_cor.name, thrd ); |
---|
848 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len ); |
---|
849 | |
---|
850 | if ( &thrd->self_cor != thrd->curr_cor ) { |
---|
851 | len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", thrd->curr_cor->name, thrd->curr_cor ); |
---|
852 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len ); |
---|
853 | } |
---|
854 | else { |
---|
855 | __cfaabi_bits_write( STDERR_FILENO, ".\n", 2 ); |
---|
856 | } |
---|
857 | } |
---|
858 | else { |
---|
859 | int len = snprintf( abort_text, abort_text_size, "Error occurred outside of any thread.\n" ); |
---|
860 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len ); |
---|
861 | } |
---|
862 | } |
---|
863 | |
---|
864 | int __kernel_abort_lastframe( void ) __attribute__ ((__nothrow__)) { |
---|
865 | return get_coroutine(__cfaabi_tls.this_thread) == get_coroutine(mainThread) ? 4 : 2; |
---|
866 | } |
---|
867 | |
---|
868 | static __spinlock_t kernel_debug_lock; |
---|
869 | |
---|
870 | extern "C" { |
---|
871 | void __cfaabi_bits_acquire() { |
---|
872 | lock( kernel_debug_lock __cfaabi_dbg_ctx2 ); |
---|
873 | } |
---|
874 | |
---|
875 | void __cfaabi_bits_release() { |
---|
876 | unlock( kernel_debug_lock ); |
---|
877 | } |
---|
878 | } |
---|
879 | |
---|
880 | //============================================================================================= |
---|
881 | // Kernel Utilities |
---|
882 | //============================================================================================= |
---|
883 | #if defined(CFA_HAVE_LINUX_IO_URING_H) |
---|
884 | #include "io/types.hfa" |
---|
885 | #endif |
---|
886 | |
---|
887 | static inline bool __maybe_io_drain( processor * proc ) { |
---|
888 | bool ret = false; |
---|
889 | #if defined(CFA_HAVE_LINUX_IO_URING_H) |
---|
890 | __cfadbg_print_safe(runtime_core, "Kernel : core %p checking io for ring %d\n", proc, proc->io.ctx->fd); |
---|
891 | |
---|
892 | // Check if we should drain the queue |
---|
893 | $io_context * ctx = proc->io.ctx; |
---|
894 | unsigned head = *ctx->cq.head; |
---|
895 | unsigned tail = *ctx->cq.tail; |
---|
896 | if(head == tail) return false; |
---|
897 | ready_schedule_lock(); |
---|
898 | ret = __cfa_io_drain( proc ); |
---|
899 | ready_schedule_unlock(); |
---|
900 | #endif |
---|
901 | return ret; |
---|
902 | } |
---|
903 | |
---|
904 | //----------------------------------------------------------------------------- |
---|
905 | // Debug |
---|
906 | __cfaabi_dbg_debug_do( |
---|
907 | extern "C" { |
---|
908 | void __cfaabi_dbg_record_lock(__spinlock_t & this, const char prev_name[]) { |
---|
909 | this.prev_name = prev_name; |
---|
910 | this.prev_thrd = kernelTLS().this_thread; |
---|
911 | } |
---|
912 | } |
---|
913 | ) |
---|
914 | |
---|
915 | //----------------------------------------------------------------------------- |
---|
916 | // Debug |
---|
917 | bool threading_enabled(void) __attribute__((const)) { |
---|
918 | return true; |
---|
919 | } |
---|
920 | |
---|
921 | //----------------------------------------------------------------------------- |
---|
922 | // Statistics |
---|
923 | #if !defined(__CFA_NO_STATISTICS__) |
---|
924 | void print_halts( processor & this ) { |
---|
925 | this.print_halts = true; |
---|
926 | } |
---|
927 | |
---|
928 | void print_stats_now( cluster & this, int flags ) { |
---|
929 | __print_stats( this.stats, this.print_stats, "Cluster", this.name, (void*)&this ); |
---|
930 | } |
---|
931 | #endif |
---|
932 | // Local Variables: // |
---|
933 | // mode: c // |
---|
934 | // tab-width: 4 // |
---|
935 | // End: // |
---|