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->unique_id, this->name, (void*)this); |
---|
166 | } |
---|
167 | #endif |
---|
168 | |
---|
169 | { |
---|
170 | // Setup preemption data |
---|
171 | preemption_scope scope = { this }; |
---|
172 | |
---|
173 | // if we need to run some special setup, now is the time to do it. |
---|
174 | if(this->init.thrd) { |
---|
175 | this->init.thrd->curr_cluster = this->cltr; |
---|
176 | __run_thread(this, this->init.thrd); |
---|
177 | } |
---|
178 | |
---|
179 | __cfadbg_print_safe(runtime_core, "Kernel : core %p started\n", this); |
---|
180 | |
---|
181 | $thread * readyThread = 0p; |
---|
182 | MAIN_LOOP: |
---|
183 | for() { |
---|
184 | #if 1 |
---|
185 | // Check if there is pending io |
---|
186 | __maybe_io_drain( this ); |
---|
187 | |
---|
188 | // Try to get the next thread |
---|
189 | readyThread = __next_thread( this->cltr ); |
---|
190 | |
---|
191 | if( !readyThread ) { |
---|
192 | __cfa_io_flush( this ); |
---|
193 | readyThread = __next_thread_slow( this->cltr ); |
---|
194 | } |
---|
195 | |
---|
196 | HALT: |
---|
197 | if( !readyThread ) { |
---|
198 | // Don't block if we are done |
---|
199 | if( __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST) ) break MAIN_LOOP; |
---|
200 | |
---|
201 | #if !defined(__CFA_NO_STATISTICS__) |
---|
202 | __tls_stats()->ready.sleep.halts++; |
---|
203 | #endif |
---|
204 | |
---|
205 | // Push self to idle stack |
---|
206 | mark_idle(this->cltr->procs, * this); |
---|
207 | |
---|
208 | // Confirm the ready-queue is empty |
---|
209 | readyThread = __next_thread_slow( this->cltr ); |
---|
210 | if( readyThread ) { |
---|
211 | // A thread was found, cancel the halt |
---|
212 | mark_awake(this->cltr->procs, * this); |
---|
213 | |
---|
214 | #if !defined(__CFA_NO_STATISTICS__) |
---|
215 | __tls_stats()->ready.sleep.cancels++; |
---|
216 | #endif |
---|
217 | |
---|
218 | // continue the mai loop |
---|
219 | break HALT; |
---|
220 | } |
---|
221 | |
---|
222 | #if !defined(__CFA_NO_STATISTICS__) |
---|
223 | if(this->print_halts) { |
---|
224 | __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 0\n", this->unique_id, rdtscl()); |
---|
225 | } |
---|
226 | #endif |
---|
227 | |
---|
228 | __cfadbg_print_safe(runtime_core, "Kernel : core %p waiting on eventfd %d\n", this, this->idle); |
---|
229 | |
---|
230 | __disable_interrupts_hard(); |
---|
231 | eventfd_t val; |
---|
232 | eventfd_read( this->idle, &val ); |
---|
233 | __enable_interrupts_hard(); |
---|
234 | |
---|
235 | #if !defined(__CFA_NO_STATISTICS__) |
---|
236 | if(this->print_halts) { |
---|
237 | __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 1\n", this->unique_id, rdtscl()); |
---|
238 | } |
---|
239 | #endif |
---|
240 | |
---|
241 | // We were woken up, remove self from idle |
---|
242 | mark_awake(this->cltr->procs, * this); |
---|
243 | |
---|
244 | // DON'T just proceed, start looking again |
---|
245 | continue MAIN_LOOP; |
---|
246 | } |
---|
247 | |
---|
248 | /* paranoid */ verify( readyThread ); |
---|
249 | |
---|
250 | // Reset io dirty bit |
---|
251 | this->io.dirty = false; |
---|
252 | |
---|
253 | // We found a thread run it |
---|
254 | __run_thread(this, readyThread); |
---|
255 | |
---|
256 | // Are we done? |
---|
257 | if( __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST) ) break MAIN_LOOP; |
---|
258 | |
---|
259 | if(this->io.pending && !this->io.dirty) { |
---|
260 | __cfa_io_flush( this ); |
---|
261 | } |
---|
262 | |
---|
263 | #else |
---|
264 | |
---|
265 | SEARCH: { |
---|
266 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
267 | /* paranoid */ verify( kernelTLS().this_proc_id ); |
---|
268 | |
---|
269 | // First, lock the scheduler since we are searching for a thread |
---|
270 | ready_schedule_lock(); |
---|
271 | |
---|
272 | // Try to get the next thread |
---|
273 | readyThread = pop_fast( this->cltr ); |
---|
274 | if(readyThread) { ready_schedule_unlock(); break SEARCH; } |
---|
275 | |
---|
276 | // If we can't find a thread, might as well flush any outstanding I/O |
---|
277 | if(this->io.pending) { __cfa_io_flush( this ); } |
---|
278 | |
---|
279 | // Spin a little on I/O, just in case |
---|
280 | for(25) { |
---|
281 | __maybe_io_drain( this ); |
---|
282 | readyThread = pop_fast( this->cltr ); |
---|
283 | if(readyThread) { ready_schedule_unlock(); break SEARCH; } |
---|
284 | } |
---|
285 | |
---|
286 | // no luck, try stealing a few times |
---|
287 | for(25) { |
---|
288 | if( __maybe_io_drain( this ) ) { |
---|
289 | readyThread = pop_fast( this->cltr ); |
---|
290 | } else { |
---|
291 | readyThread = pop_slow( this->cltr ); |
---|
292 | } |
---|
293 | if(readyThread) { ready_schedule_unlock(); break SEARCH; } |
---|
294 | } |
---|
295 | |
---|
296 | // still no luck, search for a thread |
---|
297 | readyThread = pop_search( this->cltr ); |
---|
298 | if(readyThread) { ready_schedule_unlock(); break SEARCH; } |
---|
299 | |
---|
300 | // Don't block if we are done |
---|
301 | if( __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST) ) break MAIN_LOOP; |
---|
302 | |
---|
303 | __STATS( __tls_stats()->ready.sleep.halts++; ) |
---|
304 | |
---|
305 | // Push self to idle stack |
---|
306 | ready_schedule_unlock(); |
---|
307 | mark_idle(this->cltr->procs, * this); |
---|
308 | ready_schedule_lock(); |
---|
309 | |
---|
310 | // Confirm the ready-queue is empty |
---|
311 | __maybe_io_drain( this ); |
---|
312 | readyThread = pop_search( this->cltr ); |
---|
313 | ready_schedule_unlock(); |
---|
314 | |
---|
315 | if( readyThread ) { |
---|
316 | // A thread was found, cancel the halt |
---|
317 | mark_awake(this->cltr->procs, * this); |
---|
318 | |
---|
319 | __STATS( __tls_stats()->ready.sleep.cancels++; ) |
---|
320 | |
---|
321 | // continue the main loop |
---|
322 | break SEARCH; |
---|
323 | } |
---|
324 | |
---|
325 | __STATS( if(this->print_halts) __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 0\n", this->id, rdtscl()); ) |
---|
326 | __cfadbg_print_safe(runtime_core, "Kernel : core %p waiting on eventfd %d\n", this, this->idle); |
---|
327 | |
---|
328 | // __disable_interrupts_hard(); |
---|
329 | eventfd_t val; |
---|
330 | eventfd_read( this->idle, &val ); |
---|
331 | // __enable_interrupts_hard(); |
---|
332 | |
---|
333 | __STATS( if(this->print_halts) __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 1\n", this->id, rdtscl()); ) |
---|
334 | |
---|
335 | // We were woken up, remove self from idle |
---|
336 | mark_awake(this->cltr->procs, * this); |
---|
337 | |
---|
338 | // DON'T just proceed, start looking again |
---|
339 | continue MAIN_LOOP; |
---|
340 | } |
---|
341 | |
---|
342 | RUN_THREAD: |
---|
343 | /* paranoid */ verify( kernelTLS().this_proc_id ); |
---|
344 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
345 | /* paranoid */ verify( readyThread ); |
---|
346 | |
---|
347 | // Reset io dirty bit |
---|
348 | this->io.dirty = false; |
---|
349 | |
---|
350 | // We found a thread run it |
---|
351 | __run_thread(this, readyThread); |
---|
352 | |
---|
353 | // Are we done? |
---|
354 | if( __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST) ) break MAIN_LOOP; |
---|
355 | |
---|
356 | #if !defined(__CFA_NO_STATISTICS__) |
---|
357 | unsigned long long curr = rdtscl(); |
---|
358 | if(curr > (last_tally + 500000000)) { |
---|
359 | __tally_stats(this->cltr->stats, __cfaabi_tls.this_stats); |
---|
360 | last_tally = curr; |
---|
361 | } |
---|
362 | #endif |
---|
363 | |
---|
364 | if(this->io.pending && !this->io.dirty) { |
---|
365 | __cfa_io_flush( this ); |
---|
366 | } |
---|
367 | |
---|
368 | ready_schedule_lock(); |
---|
369 | __maybe_io_drain( this ); |
---|
370 | ready_schedule_unlock(); |
---|
371 | #endif |
---|
372 | } |
---|
373 | |
---|
374 | __cfadbg_print_safe(runtime_core, "Kernel : core %p stopping\n", this); |
---|
375 | } |
---|
376 | |
---|
377 | __cfa_io_stop( this ); |
---|
378 | |
---|
379 | post( this->terminated ); |
---|
380 | |
---|
381 | if(this == mainProcessor) { |
---|
382 | // HACK : the coroutine context switch expects this_thread to be set |
---|
383 | // and it make sense for it to be set in all other cases except here |
---|
384 | // fake it |
---|
385 | __cfaabi_tls.this_thread = mainThread; |
---|
386 | } |
---|
387 | |
---|
388 | __cfadbg_print_safe(runtime_core, "Kernel : core %p terminated\n", this); |
---|
389 | } |
---|
390 | |
---|
391 | static int * __volatile_errno() __attribute__((noinline)); |
---|
392 | static int * __volatile_errno() { asm(""); return &errno; } |
---|
393 | |
---|
394 | // KERNEL ONLY |
---|
395 | // runThread runs a thread by context switching |
---|
396 | // from the processor coroutine to the target thread |
---|
397 | static void __run_thread(processor * this, $thread * thrd_dst) { |
---|
398 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
399 | /* paranoid */ verifyf( thrd_dst->state == Ready || thrd_dst->preempted != __NO_PREEMPTION, "state : %d, preempted %d\n", thrd_dst->state, thrd_dst->preempted); |
---|
400 | /* paranoid */ verifyf( thrd_dst->link.next == 0p, "Expected null got %p", thrd_dst->link.next ); |
---|
401 | __builtin_prefetch( thrd_dst->context.SP ); |
---|
402 | |
---|
403 | __cfadbg_print_safe(runtime_core, "Kernel : core %p running thread %p (%s)\n", this, thrd_dst, thrd_dst->self_cor.name); |
---|
404 | |
---|
405 | $coroutine * proc_cor = get_coroutine(this->runner); |
---|
406 | |
---|
407 | // set state of processor coroutine to inactive |
---|
408 | verify(proc_cor->state == Active); |
---|
409 | proc_cor->state = Blocked; |
---|
410 | |
---|
411 | // Actually run the thread |
---|
412 | RUNNING: while(true) { |
---|
413 | thrd_dst->preempted = __NO_PREEMPTION; |
---|
414 | thrd_dst->state = Active; |
---|
415 | |
---|
416 | // Update global state |
---|
417 | kernelTLS().this_thread = thrd_dst; |
---|
418 | |
---|
419 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
420 | /* paranoid */ verify( kernelTLS().this_thread == thrd_dst ); |
---|
421 | /* paranoid */ verify( thrd_dst->curr_cluster == this->cltr ); |
---|
422 | /* paranoid */ verify( thrd_dst->context.SP ); |
---|
423 | /* paranoid */ verify( thrd_dst->state != Halted ); |
---|
424 | /* 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 |
---|
425 | /* 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 |
---|
426 | /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd_dst->canary ); |
---|
427 | |
---|
428 | |
---|
429 | |
---|
430 | // set context switch to the thread that the processor is executing |
---|
431 | __cfactx_switch( &proc_cor->context, &thrd_dst->context ); |
---|
432 | // when __cfactx_switch returns we are back in the processor coroutine |
---|
433 | |
---|
434 | /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd_dst->canary ); |
---|
435 | /* 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 ); |
---|
436 | /* 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 ); |
---|
437 | /* paranoid */ verify( thrd_dst->context.SP ); |
---|
438 | /* paranoid */ verify( thrd_dst->curr_cluster == this->cltr ); |
---|
439 | /* paranoid */ verify( kernelTLS().this_thread == thrd_dst ); |
---|
440 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
441 | |
---|
442 | // Reset global state |
---|
443 | kernelTLS().this_thread = 0p; |
---|
444 | |
---|
445 | // We just finished running a thread, there are a few things that could have happened. |
---|
446 | // 1 - Regular case : the thread has blocked and now one has scheduled it yet. |
---|
447 | // 2 - Racy case : the thread has blocked but someone has already tried to schedule it. |
---|
448 | // 4 - Preempted |
---|
449 | // In case 1, we may have won a race so we can't write to the state again. |
---|
450 | // In case 2, we lost the race so we now own the thread. |
---|
451 | |
---|
452 | if(unlikely(thrd_dst->preempted != __NO_PREEMPTION)) { |
---|
453 | // The thread was preempted, reschedule it and reset the flag |
---|
454 | schedule_thread$( thrd_dst ); |
---|
455 | break RUNNING; |
---|
456 | } |
---|
457 | |
---|
458 | if(unlikely(thrd_dst->state == Halting)) { |
---|
459 | // The thread has halted, it should never be scheduled/run again |
---|
460 | // finish the thread |
---|
461 | __thread_finish( thrd_dst ); |
---|
462 | break RUNNING; |
---|
463 | } |
---|
464 | |
---|
465 | /* paranoid */ verify( thrd_dst->state == Active ); |
---|
466 | thrd_dst->state = Blocked; |
---|
467 | |
---|
468 | // set state of processor coroutine to active and the thread to inactive |
---|
469 | int old_ticket = __atomic_fetch_sub(&thrd_dst->ticket, 1, __ATOMIC_SEQ_CST); |
---|
470 | switch(old_ticket) { |
---|
471 | case TICKET_RUNNING: |
---|
472 | // This is case 1, the regular case, nothing more is needed |
---|
473 | break RUNNING; |
---|
474 | case TICKET_UNBLOCK: |
---|
475 | #if !defined(__CFA_NO_STATISTICS__) |
---|
476 | __tls_stats()->ready.threads.threads++; |
---|
477 | __push_stat( __tls_stats(), __tls_stats()->ready.threads.threads, false, "Processor", this ); |
---|
478 | #endif |
---|
479 | // This is case 2, the racy case, someone tried to run this thread before it finished blocking |
---|
480 | // In this case, just run it again. |
---|
481 | continue RUNNING; |
---|
482 | default: |
---|
483 | // This makes no sense, something is wrong abort |
---|
484 | abort(); |
---|
485 | } |
---|
486 | } |
---|
487 | |
---|
488 | // Just before returning to the processor, set the processor coroutine to active |
---|
489 | proc_cor->state = Active; |
---|
490 | |
---|
491 | __cfadbg_print_safe(runtime_core, "Kernel : core %p finished running thread %p\n", this, thrd_dst); |
---|
492 | |
---|
493 | #if !defined(__CFA_NO_STATISTICS__) |
---|
494 | __tls_stats()->ready.threads.threads--; |
---|
495 | __push_stat( __tls_stats(), __tls_stats()->ready.threads.threads, false, "Processor", this ); |
---|
496 | #endif |
---|
497 | |
---|
498 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
499 | } |
---|
500 | |
---|
501 | // KERNEL_ONLY |
---|
502 | void returnToKernel() { |
---|
503 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
504 | $coroutine * proc_cor = get_coroutine(kernelTLS().this_processor->runner); |
---|
505 | $thread * thrd_src = kernelTLS().this_thread; |
---|
506 | |
---|
507 | __STATS( thrd_src->last_proc = kernelTLS().this_processor; ) |
---|
508 | |
---|
509 | // Run the thread on this processor |
---|
510 | { |
---|
511 | int local_errno = *__volatile_errno(); |
---|
512 | #if defined( __i386 ) || defined( __x86_64 ) |
---|
513 | __x87_store; |
---|
514 | #endif |
---|
515 | /* paranoid */ verify( proc_cor->context.SP ); |
---|
516 | /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd_src->canary ); |
---|
517 | __cfactx_switch( &thrd_src->context, &proc_cor->context ); |
---|
518 | /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd_src->canary ); |
---|
519 | #if defined( __i386 ) || defined( __x86_64 ) |
---|
520 | __x87_load; |
---|
521 | #endif |
---|
522 | *__volatile_errno() = local_errno; |
---|
523 | } |
---|
524 | |
---|
525 | #if !defined(__CFA_NO_STATISTICS__) |
---|
526 | /* paranoid */ verify( thrd_src->last_proc != 0p ); |
---|
527 | if(thrd_src->last_proc != kernelTLS().this_processor) { |
---|
528 | __tls_stats()->ready.threads.migration++; |
---|
529 | } |
---|
530 | #endif |
---|
531 | |
---|
532 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
533 | /* 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 ); |
---|
534 | /* 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 ); |
---|
535 | } |
---|
536 | |
---|
537 | //----------------------------------------------------------------------------- |
---|
538 | // Scheduler routines |
---|
539 | // KERNEL ONLY |
---|
540 | static void __schedule_thread( $thread * thrd ) { |
---|
541 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
542 | /* paranoid */ verify( ready_schedule_islocked()); |
---|
543 | /* paranoid */ verify( thrd ); |
---|
544 | /* paranoid */ verify( thrd->state != Halted ); |
---|
545 | /* paranoid */ verify( thrd->curr_cluster ); |
---|
546 | /* paranoid */ #if defined( __CFA_WITH_VERIFY__ ) |
---|
547 | /* paranoid */ if( thrd->state == Blocked || thrd->state == Start ) assertf( thrd->preempted == __NO_PREEMPTION, |
---|
548 | "Error inactive thread marked as preempted, state %d, preemption %d\n", thrd->state, thrd->preempted ); |
---|
549 | /* paranoid */ if( thrd->preempted != __NO_PREEMPTION ) assertf(thrd->state == Active, |
---|
550 | "Error preempted thread marked as not currently running, state %d, preemption %d\n", thrd->state, thrd->preempted ); |
---|
551 | /* paranoid */ #endif |
---|
552 | /* paranoid */ verifyf( thrd->link.next == 0p, "Expected null got %p", thrd->link.next ); |
---|
553 | /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd->canary ); |
---|
554 | |
---|
555 | |
---|
556 | if (thrd->preempted == __NO_PREEMPTION) thrd->state = Ready; |
---|
557 | |
---|
558 | // Dereference the thread now because once we push it, there is not guaranteed it's still valid. |
---|
559 | struct cluster * cl = thrd->curr_cluster; |
---|
560 | __STATS(bool outside = thrd->last_proc && thrd->last_proc != kernelTLS().this_processor; ) |
---|
561 | |
---|
562 | // push the thread to the cluster ready-queue |
---|
563 | push( cl, thrd ); |
---|
564 | |
---|
565 | // variable thrd is no longer safe to use |
---|
566 | thrd = 0xdeaddeaddeaddeadp; |
---|
567 | |
---|
568 | // wake the cluster using the save variable. |
---|
569 | __wake_one( cl ); |
---|
570 | |
---|
571 | #if !defined(__CFA_NO_STATISTICS__) |
---|
572 | if( kernelTLS().this_stats ) { |
---|
573 | __tls_stats()->ready.threads.threads++; |
---|
574 | if(outside) { |
---|
575 | __tls_stats()->ready.threads.extunpark++; |
---|
576 | } |
---|
577 | __push_stat( __tls_stats(), __tls_stats()->ready.threads.threads, false, "Processor", kernelTLS().this_processor ); |
---|
578 | } |
---|
579 | else { |
---|
580 | __atomic_fetch_add(&cl->stats->ready.threads.threads, 1, __ATOMIC_RELAXED); |
---|
581 | __atomic_fetch_add(&cl->stats->ready.threads.extunpark, 1, __ATOMIC_RELAXED); |
---|
582 | __push_stat( cl->stats, cl->stats->ready.threads.threads, true, "Cluster", cl ); |
---|
583 | } |
---|
584 | #endif |
---|
585 | |
---|
586 | /* paranoid */ verify( ready_schedule_islocked()); |
---|
587 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
588 | } |
---|
589 | |
---|
590 | void schedule_thread$( $thread * thrd ) { |
---|
591 | ready_schedule_lock(); |
---|
592 | __schedule_thread( thrd ); |
---|
593 | ready_schedule_unlock(); |
---|
594 | } |
---|
595 | |
---|
596 | // KERNEL ONLY |
---|
597 | static inline $thread * __next_thread(cluster * this) with( *this ) { |
---|
598 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
599 | |
---|
600 | ready_schedule_lock(); |
---|
601 | $thread * thrd = pop_fast( this ); |
---|
602 | ready_schedule_unlock(); |
---|
603 | |
---|
604 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
605 | return thrd; |
---|
606 | } |
---|
607 | |
---|
608 | // KERNEL ONLY |
---|
609 | static inline $thread * __next_thread_slow(cluster * this) with( *this ) { |
---|
610 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
611 | |
---|
612 | ready_schedule_lock(); |
---|
613 | $thread * thrd; |
---|
614 | for(25) { |
---|
615 | thrd = pop_slow( this ); |
---|
616 | if(thrd) goto RET; |
---|
617 | } |
---|
618 | thrd = pop_search( this ); |
---|
619 | |
---|
620 | RET: |
---|
621 | ready_schedule_unlock(); |
---|
622 | |
---|
623 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
624 | return thrd; |
---|
625 | } |
---|
626 | |
---|
627 | static inline bool __must_unpark( $thread * thrd ) { |
---|
628 | int old_ticket = __atomic_fetch_add(&thrd->ticket, 1, __ATOMIC_SEQ_CST); |
---|
629 | switch(old_ticket) { |
---|
630 | case TICKET_RUNNING: |
---|
631 | // Wake won the race, the thread will reschedule/rerun itself |
---|
632 | return false; |
---|
633 | case TICKET_BLOCKED: |
---|
634 | /* paranoid */ verify( ! thrd->preempted != __NO_PREEMPTION ); |
---|
635 | /* paranoid */ verify( thrd->state == Blocked ); |
---|
636 | return true; |
---|
637 | default: |
---|
638 | // This makes no sense, something is wrong abort |
---|
639 | abort("Thread %p (%s) has mismatch park/unpark\n", thrd, thrd->self_cor.name); |
---|
640 | } |
---|
641 | } |
---|
642 | |
---|
643 | void __kernel_unpark( $thread * thrd ) { |
---|
644 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
645 | /* paranoid */ verify( ready_schedule_islocked()); |
---|
646 | |
---|
647 | if( !thrd ) return; |
---|
648 | |
---|
649 | if(__must_unpark(thrd)) { |
---|
650 | // Wake lost the race, |
---|
651 | __schedule_thread( thrd ); |
---|
652 | } |
---|
653 | |
---|
654 | /* paranoid */ verify( ready_schedule_islocked()); |
---|
655 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
656 | } |
---|
657 | |
---|
658 | void unpark( $thread * thrd ) { |
---|
659 | if( !thrd ) return; |
---|
660 | |
---|
661 | if(__must_unpark(thrd)) { |
---|
662 | disable_interrupts(); |
---|
663 | // Wake lost the race, |
---|
664 | schedule_thread$( thrd ); |
---|
665 | enable_interrupts(false); |
---|
666 | } |
---|
667 | } |
---|
668 | |
---|
669 | void park( void ) { |
---|
670 | __disable_interrupts_checked(); |
---|
671 | /* paranoid */ verify( kernelTLS().this_thread->preempted == __NO_PREEMPTION ); |
---|
672 | returnToKernel(); |
---|
673 | __enable_interrupts_checked(); |
---|
674 | |
---|
675 | } |
---|
676 | |
---|
677 | extern "C" { |
---|
678 | // Leave the thread monitor |
---|
679 | // last routine called by a thread. |
---|
680 | // Should never return |
---|
681 | void __cfactx_thrd_leave() { |
---|
682 | $thread * thrd = active_thread(); |
---|
683 | $monitor * this = &thrd->self_mon; |
---|
684 | |
---|
685 | // Lock the monitor now |
---|
686 | lock( this->lock __cfaabi_dbg_ctx2 ); |
---|
687 | |
---|
688 | disable_interrupts(); |
---|
689 | |
---|
690 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
691 | /* paranoid */ verify( thrd->state == Active ); |
---|
692 | /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd->canary ); |
---|
693 | /* paranoid */ verify( kernelTLS().this_thread == thrd ); |
---|
694 | /* paranoid */ verify( thrd->context.SP ); |
---|
695 | /* 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 ); |
---|
696 | /* 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 ); |
---|
697 | |
---|
698 | thrd->state = Halting; |
---|
699 | if( TICKET_RUNNING != thrd->ticket ) { abort( "Thread terminated with pending unpark" ); } |
---|
700 | if( thrd != this->owner ) { abort( "Thread internal monitor has incorrect owner" ); } |
---|
701 | if( this->recursion != 1) { abort( "Thread internal monitor has unbalanced recursion" ); } |
---|
702 | |
---|
703 | // Leave the thread |
---|
704 | returnToKernel(); |
---|
705 | |
---|
706 | // Control flow should never reach here! |
---|
707 | abort(); |
---|
708 | } |
---|
709 | } |
---|
710 | |
---|
711 | // KERNEL ONLY |
---|
712 | bool force_yield( __Preemption_Reason reason ) { |
---|
713 | __disable_interrupts_checked(); |
---|
714 | $thread * thrd = kernelTLS().this_thread; |
---|
715 | /* paranoid */ verify(thrd->state == Active); |
---|
716 | |
---|
717 | // SKULLDUGGERY: It is possible that we are preempting this thread just before |
---|
718 | // it was going to park itself. If that is the case and it is already using the |
---|
719 | // intrusive fields then we can't use them to preempt the thread |
---|
720 | // If that is the case, abandon the preemption. |
---|
721 | bool preempted = false; |
---|
722 | if(thrd->link.next == 0p) { |
---|
723 | preempted = true; |
---|
724 | thrd->preempted = reason; |
---|
725 | returnToKernel(); |
---|
726 | } |
---|
727 | __enable_interrupts_checked( false ); |
---|
728 | return preempted; |
---|
729 | } |
---|
730 | |
---|
731 | //============================================================================================= |
---|
732 | // Kernel Idle Sleep |
---|
733 | //============================================================================================= |
---|
734 | // Wake a thread from the front if there are any |
---|
735 | static void __wake_one(cluster * this) { |
---|
736 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
737 | /* paranoid */ verify( ready_schedule_islocked() ); |
---|
738 | |
---|
739 | // Check if there is a sleeping processor |
---|
740 | processor * p; |
---|
741 | unsigned idle; |
---|
742 | unsigned total; |
---|
743 | [idle, total, p] = query_idles(this->procs); |
---|
744 | |
---|
745 | // If no one is sleeping, we are done |
---|
746 | if( idle == 0 ) return; |
---|
747 | |
---|
748 | // We found a processor, wake it up |
---|
749 | eventfd_t val; |
---|
750 | val = 1; |
---|
751 | eventfd_write( p->idle, val ); |
---|
752 | |
---|
753 | #if !defined(__CFA_NO_STATISTICS__) |
---|
754 | if( kernelTLS().this_stats ) { |
---|
755 | __tls_stats()->ready.sleep.wakes++; |
---|
756 | } |
---|
757 | else { |
---|
758 | __atomic_fetch_add(&this->stats->ready.sleep.wakes, 1, __ATOMIC_RELAXED); |
---|
759 | } |
---|
760 | #endif |
---|
761 | |
---|
762 | /* paranoid */ verify( ready_schedule_islocked() ); |
---|
763 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
764 | |
---|
765 | return; |
---|
766 | } |
---|
767 | |
---|
768 | // Unconditionnaly wake a thread |
---|
769 | void __wake_proc(processor * this) { |
---|
770 | __cfadbg_print_safe(runtime_core, "Kernel : waking Processor %p\n", this); |
---|
771 | |
---|
772 | __disable_interrupts_checked(); |
---|
773 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
774 | eventfd_t val; |
---|
775 | val = 1; |
---|
776 | eventfd_write( this->idle, val ); |
---|
777 | __enable_interrupts_checked(); |
---|
778 | } |
---|
779 | |
---|
780 | static void mark_idle(__cluster_proc_list & this, processor & proc) { |
---|
781 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
782 | lock( this ); |
---|
783 | this.idle++; |
---|
784 | /* paranoid */ verify( this.idle <= this.total ); |
---|
785 | remove(proc); |
---|
786 | insert_first(this.idles, proc); |
---|
787 | unlock( this ); |
---|
788 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
789 | } |
---|
790 | |
---|
791 | static void mark_awake(__cluster_proc_list & this, processor & proc) { |
---|
792 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
793 | lock( this ); |
---|
794 | this.idle--; |
---|
795 | /* paranoid */ verify( this.idle >= 0 ); |
---|
796 | remove(proc); |
---|
797 | insert_last(this.actives, proc); |
---|
798 | unlock( this ); |
---|
799 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
800 | } |
---|
801 | |
---|
802 | static [unsigned idle, unsigned total, * processor] query_idles( & __cluster_proc_list this ) { |
---|
803 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
804 | /* paranoid */ verify( ready_schedule_islocked() ); |
---|
805 | |
---|
806 | for() { |
---|
807 | uint64_t l = __atomic_load_n(&this.lock, __ATOMIC_SEQ_CST); |
---|
808 | if( 1 == (l % 2) ) { Pause(); continue; } |
---|
809 | unsigned idle = this.idle; |
---|
810 | unsigned total = this.total; |
---|
811 | processor * proc = &this.idles`first; |
---|
812 | // Compiler fence is unnecessary, but gcc-8 and older incorrectly reorder code without it |
---|
813 | asm volatile("": : :"memory"); |
---|
814 | if(l != __atomic_load_n(&this.lock, __ATOMIC_SEQ_CST)) { Pause(); continue; } |
---|
815 | return [idle, total, proc]; |
---|
816 | } |
---|
817 | |
---|
818 | /* paranoid */ verify( ready_schedule_islocked() ); |
---|
819 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
820 | } |
---|
821 | |
---|
822 | //============================================================================================= |
---|
823 | // Unexpected Terminating logic |
---|
824 | //============================================================================================= |
---|
825 | void __kernel_abort_msg( char * abort_text, int abort_text_size ) { |
---|
826 | $thread * thrd = __cfaabi_tls.this_thread; |
---|
827 | |
---|
828 | if(thrd) { |
---|
829 | int len = snprintf( abort_text, abort_text_size, "Error occurred while executing thread %.256s (%p)", thrd->self_cor.name, thrd ); |
---|
830 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len ); |
---|
831 | |
---|
832 | if ( &thrd->self_cor != thrd->curr_cor ) { |
---|
833 | len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", thrd->curr_cor->name, thrd->curr_cor ); |
---|
834 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len ); |
---|
835 | } |
---|
836 | else { |
---|
837 | __cfaabi_bits_write( STDERR_FILENO, ".\n", 2 ); |
---|
838 | } |
---|
839 | } |
---|
840 | else { |
---|
841 | int len = snprintf( abort_text, abort_text_size, "Error occurred outside of any thread.\n" ); |
---|
842 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len ); |
---|
843 | } |
---|
844 | } |
---|
845 | |
---|
846 | int __kernel_abort_lastframe( void ) __attribute__ ((__nothrow__)) { |
---|
847 | return get_coroutine(__cfaabi_tls.this_thread) == get_coroutine(mainThread) ? 4 : 2; |
---|
848 | } |
---|
849 | |
---|
850 | static __spinlock_t kernel_debug_lock; |
---|
851 | |
---|
852 | extern "C" { |
---|
853 | void __cfaabi_bits_acquire() { |
---|
854 | lock( kernel_debug_lock __cfaabi_dbg_ctx2 ); |
---|
855 | } |
---|
856 | |
---|
857 | void __cfaabi_bits_release() { |
---|
858 | unlock( kernel_debug_lock ); |
---|
859 | } |
---|
860 | } |
---|
861 | |
---|
862 | //============================================================================================= |
---|
863 | // Kernel Utilities |
---|
864 | //============================================================================================= |
---|
865 | #if defined(CFA_HAVE_LINUX_IO_URING_H) |
---|
866 | #include "io/types.hfa" |
---|
867 | #endif |
---|
868 | |
---|
869 | static inline bool __maybe_io_drain( processor * proc ) { |
---|
870 | bool ret = false; |
---|
871 | #if defined(CFA_HAVE_LINUX_IO_URING_H) |
---|
872 | __cfadbg_print_safe(runtime_core, "Kernel : core %p checking io for ring %d\n", proc, proc->io.ctx->fd); |
---|
873 | |
---|
874 | // Check if we should drain the queue |
---|
875 | $io_context * ctx = proc->io.ctx; |
---|
876 | unsigned head = *ctx->cq.head; |
---|
877 | unsigned tail = *ctx->cq.tail; |
---|
878 | if(head == tail) return false; |
---|
879 | ready_schedule_lock(); |
---|
880 | ret = __cfa_io_drain( proc ); |
---|
881 | ready_schedule_unlock(); |
---|
882 | #endif |
---|
883 | return ret; |
---|
884 | } |
---|
885 | |
---|
886 | //----------------------------------------------------------------------------- |
---|
887 | // Debug |
---|
888 | __cfaabi_dbg_debug_do( |
---|
889 | extern "C" { |
---|
890 | void __cfaabi_dbg_record_lock(__spinlock_t & this, const char prev_name[]) { |
---|
891 | this.prev_name = prev_name; |
---|
892 | this.prev_thrd = kernelTLS().this_thread; |
---|
893 | } |
---|
894 | } |
---|
895 | ) |
---|
896 | |
---|
897 | //----------------------------------------------------------------------------- |
---|
898 | // Debug |
---|
899 | bool threading_enabled(void) __attribute__((const)) { |
---|
900 | return true; |
---|
901 | } |
---|
902 | |
---|
903 | //----------------------------------------------------------------------------- |
---|
904 | // Statistics |
---|
905 | #if !defined(__CFA_NO_STATISTICS__) |
---|
906 | void print_halts( processor & this ) { |
---|
907 | this.print_halts = true; |
---|
908 | } |
---|
909 | |
---|
910 | static void crawl_list( cluster * cltr, dlist(processor, processor) & list, unsigned count ) { |
---|
911 | /* paranoid */ verify( cltr->stats ); |
---|
912 | |
---|
913 | processor * it = &list`first; |
---|
914 | for(unsigned i = 0; i < count; i++) { |
---|
915 | /* paranoid */ verifyf( it, "Unexpected null iterator, at index %u of %u\n", i, count); |
---|
916 | /* paranoid */ verify( it->local_data->this_stats ); |
---|
917 | __tally_stats( cltr->stats, it->local_data->this_stats ); |
---|
918 | it = &(*it)`next; |
---|
919 | } |
---|
920 | } |
---|
921 | |
---|
922 | void crawl_cluster_stats( cluster & this ) { |
---|
923 | // Stop the world, otherwise stats could get really messed-up |
---|
924 | // this doesn't solve all problems but does solve many |
---|
925 | // so it's probably good enough |
---|
926 | uint_fast32_t last_size = ready_mutate_lock(); |
---|
927 | |
---|
928 | crawl_list(&this, this.procs.actives, this.procs.total - this.procs.idle); |
---|
929 | crawl_list(&this, this.procs.idles , this.procs.idle ); |
---|
930 | |
---|
931 | // Unlock the RWlock |
---|
932 | ready_mutate_unlock( last_size ); |
---|
933 | } |
---|
934 | |
---|
935 | |
---|
936 | void print_stats_now( cluster & this, int flags ) { |
---|
937 | crawl_cluster_stats( this ); |
---|
938 | __print_stats( this.stats, this.print_stats, "Cluster", this.name, (void*)&this ); |
---|
939 | } |
---|
940 | #endif |
---|
941 | // Local Variables: // |
---|
942 | // mode: c // |
---|
943 | // tab-width: 4 // |
---|
944 | // End: // |
---|