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 |
|
---|
37 | //-----------------------------------------------------------------------------
|
---|
38 | // Some assembly required
|
---|
39 | #if defined( __i386 )
|
---|
40 | // mxcr : SSE Status and Control bits (control bits are preserved across function calls)
|
---|
41 | // fcw : X87 FPU control word (preserved across function calls)
|
---|
42 | #define __x87_store \
|
---|
43 | uint32_t __mxcr; \
|
---|
44 | uint16_t __fcw; \
|
---|
45 | __asm__ volatile ( \
|
---|
46 | "stmxcsr %0\n" \
|
---|
47 | "fnstcw %1\n" \
|
---|
48 | : "=m" (__mxcr),\
|
---|
49 | "=m" (__fcw) \
|
---|
50 | )
|
---|
51 |
|
---|
52 | #define __x87_load \
|
---|
53 | __asm__ volatile ( \
|
---|
54 | "fldcw %1\n" \
|
---|
55 | "ldmxcsr %0\n" \
|
---|
56 | ::"m" (__mxcr),\
|
---|
57 | "m" (__fcw) \
|
---|
58 | )
|
---|
59 |
|
---|
60 | #elif defined( __x86_64 )
|
---|
61 | #define __x87_store \
|
---|
62 | uint32_t __mxcr; \
|
---|
63 | uint16_t __fcw; \
|
---|
64 | __asm__ volatile ( \
|
---|
65 | "stmxcsr %0\n" \
|
---|
66 | "fnstcw %1\n" \
|
---|
67 | : "=m" (__mxcr),\
|
---|
68 | "=m" (__fcw) \
|
---|
69 | )
|
---|
70 |
|
---|
71 | #define __x87_load \
|
---|
72 | __asm__ volatile ( \
|
---|
73 | "fldcw %1\n" \
|
---|
74 | "ldmxcsr %0\n" \
|
---|
75 | :: "m" (__mxcr),\
|
---|
76 | "m" (__fcw) \
|
---|
77 | )
|
---|
78 |
|
---|
79 | #elif defined( __arm__ )
|
---|
80 | #define __x87_store
|
---|
81 | #define __x87_load
|
---|
82 |
|
---|
83 | #elif defined( __aarch64__ )
|
---|
84 | #define __x87_store \
|
---|
85 | uint32_t __fpcntl[2]; \
|
---|
86 | __asm__ volatile ( \
|
---|
87 | "mrs x9, FPCR\n" \
|
---|
88 | "mrs x10, FPSR\n" \
|
---|
89 | "stp x9, x10, %0\n" \
|
---|
90 | : "=m" (__fpcntl) : : "x9", "x10" \
|
---|
91 | )
|
---|
92 |
|
---|
93 | #define __x87_load \
|
---|
94 | __asm__ volatile ( \
|
---|
95 | "ldp x9, x10, %0\n" \
|
---|
96 | "msr FPSR, x10\n" \
|
---|
97 | "msr FPCR, x9\n" \
|
---|
98 | : "=m" (__fpcntl) : : "x9", "x10" \
|
---|
99 | )
|
---|
100 |
|
---|
101 | #else
|
---|
102 | #error unsupported hardware architecture
|
---|
103 | #endif
|
---|
104 |
|
---|
105 | extern $thread * mainThread;
|
---|
106 | extern processor * mainProcessor;
|
---|
107 |
|
---|
108 | //-----------------------------------------------------------------------------
|
---|
109 | // Kernel Scheduling logic
|
---|
110 | static $thread * __next_thread(cluster * this);
|
---|
111 | static $thread * __next_thread_slow(cluster * this);
|
---|
112 | static void __run_thread(processor * this, $thread * dst);
|
---|
113 | static void __wake_one(cluster * cltr);
|
---|
114 |
|
---|
115 | static void push (__cluster_idles & idles, processor & proc);
|
---|
116 | static void remove(__cluster_idles & idles, processor & proc);
|
---|
117 | static [unsigned idle, unsigned total, * processor] query( & __cluster_idles idles );
|
---|
118 |
|
---|
119 | extern void __cfa_io_start( processor * );
|
---|
120 | extern void __cfa_io_drain( processor * );
|
---|
121 | extern void __cfa_io_flush( processor * );
|
---|
122 | extern void __cfa_io_stop ( processor * );
|
---|
123 | static inline void __maybe_io_drain( processor * );
|
---|
124 |
|
---|
125 | extern void __disable_interrupts_hard();
|
---|
126 | extern void __enable_interrupts_hard();
|
---|
127 |
|
---|
128 | //=============================================================================================
|
---|
129 | // Kernel Scheduling logic
|
---|
130 | //=============================================================================================
|
---|
131 | //Main of the processor contexts
|
---|
132 | void main(processorCtx_t & runner) {
|
---|
133 | // Because of a bug, we couldn't initialized the seed on construction
|
---|
134 | // Do it here
|
---|
135 | __cfaabi_tls.rand_seed ^= rdtscl();
|
---|
136 | __cfaabi_tls.ready_rng.fwd_seed = 25214903917_l64u * (rdtscl() ^ (uintptr_t)&runner);
|
---|
137 | __tls_rand_advance_bck();
|
---|
138 |
|
---|
139 | processor * this = runner.proc;
|
---|
140 | verify(this);
|
---|
141 |
|
---|
142 | __cfa_io_start( this );
|
---|
143 |
|
---|
144 | __cfadbg_print_safe(runtime_core, "Kernel : core %p starting\n", this);
|
---|
145 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
146 | if( this->print_halts ) {
|
---|
147 | __cfaabi_bits_print_safe( STDOUT_FILENO, "Processor : %d - %s (%p)\n", this->id, this->name, (void*)this);
|
---|
148 | }
|
---|
149 | #endif
|
---|
150 |
|
---|
151 | {
|
---|
152 | // Setup preemption data
|
---|
153 | preemption_scope scope = { this };
|
---|
154 |
|
---|
155 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
156 | unsigned long long last_tally = rdtscl();
|
---|
157 | #endif
|
---|
158 |
|
---|
159 | // if we need to run some special setup, now is the time to do it.
|
---|
160 | if(this->init.thrd) {
|
---|
161 | this->init.thrd->curr_cluster = this->cltr;
|
---|
162 | __run_thread(this, this->init.thrd);
|
---|
163 | }
|
---|
164 |
|
---|
165 | __cfadbg_print_safe(runtime_core, "Kernel : core %p started\n", this);
|
---|
166 |
|
---|
167 | $thread * readyThread = 0p;
|
---|
168 | MAIN_LOOP:
|
---|
169 | for() {
|
---|
170 | // Check if there is pending io
|
---|
171 | __maybe_io_drain( this );
|
---|
172 |
|
---|
173 | // Try to get the next thread
|
---|
174 | readyThread = __next_thread( this->cltr );
|
---|
175 |
|
---|
176 | if( !readyThread ) {
|
---|
177 | __cfa_io_flush( this );
|
---|
178 | readyThread = __next_thread_slow( this->cltr );
|
---|
179 | }
|
---|
180 |
|
---|
181 | HALT:
|
---|
182 | if( !readyThread ) {
|
---|
183 | // Don't block if we are done
|
---|
184 | if( __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST) ) break MAIN_LOOP;
|
---|
185 |
|
---|
186 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
187 | __tls_stats()->ready.sleep.halts++;
|
---|
188 | #endif
|
---|
189 |
|
---|
190 | // Push self to idle stack
|
---|
191 | push(this->cltr->idles, * this);
|
---|
192 |
|
---|
193 | // Confirm the ready-queue is empty
|
---|
194 | readyThread = __next_thread_slow( this->cltr );
|
---|
195 | if( readyThread ) {
|
---|
196 | // A thread was found, cancel the halt
|
---|
197 | remove(this->cltr->idles, * this);
|
---|
198 |
|
---|
199 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
200 | __tls_stats()->ready.sleep.cancels++;
|
---|
201 | #endif
|
---|
202 |
|
---|
203 | // continue the mai loop
|
---|
204 | break HALT;
|
---|
205 | }
|
---|
206 |
|
---|
207 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
208 | if(this->print_halts) {
|
---|
209 | __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 0\n", this->id, rdtscl());
|
---|
210 | }
|
---|
211 | #endif
|
---|
212 |
|
---|
213 | __cfadbg_print_safe(runtime_core, "Kernel : core %p waiting on eventfd %d\n", this, this->idle);
|
---|
214 |
|
---|
215 | __disable_interrupts_hard();
|
---|
216 | eventfd_t val;
|
---|
217 | eventfd_read( this->idle, &val );
|
---|
218 | __enable_interrupts_hard();
|
---|
219 |
|
---|
220 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
221 | if(this->print_halts) {
|
---|
222 | __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 1\n", this->id, rdtscl());
|
---|
223 | }
|
---|
224 | #endif
|
---|
225 |
|
---|
226 | // We were woken up, remove self from idle
|
---|
227 | remove(this->cltr->idles, * this);
|
---|
228 |
|
---|
229 | // DON'T just proceed, start looking again
|
---|
230 | continue MAIN_LOOP;
|
---|
231 | }
|
---|
232 |
|
---|
233 | /* paranoid */ verify( readyThread );
|
---|
234 |
|
---|
235 | // Reset io dirty bit
|
---|
236 | this->io.dirty = false;
|
---|
237 |
|
---|
238 | // We found a thread run it
|
---|
239 | __run_thread(this, readyThread);
|
---|
240 |
|
---|
241 | // Are we done?
|
---|
242 | if( __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST) ) break MAIN_LOOP;
|
---|
243 |
|
---|
244 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
245 | unsigned long long curr = rdtscl();
|
---|
246 | if(curr > (last_tally + 500000000)) {
|
---|
247 | __tally_stats(this->cltr->stats, __cfaabi_tls.this_stats);
|
---|
248 | last_tally = curr;
|
---|
249 | }
|
---|
250 | #endif
|
---|
251 |
|
---|
252 | if(this->io.pending && !this->io.dirty) {
|
---|
253 | __cfa_io_flush( this );
|
---|
254 | }
|
---|
255 | }
|
---|
256 |
|
---|
257 | __cfadbg_print_safe(runtime_core, "Kernel : core %p stopping\n", this);
|
---|
258 | }
|
---|
259 |
|
---|
260 | __cfa_io_stop( this );
|
---|
261 |
|
---|
262 | post( this->terminated );
|
---|
263 |
|
---|
264 |
|
---|
265 | if(this == mainProcessor) {
|
---|
266 | // HACK : the coroutine context switch expects this_thread to be set
|
---|
267 | // and it make sense for it to be set in all other cases except here
|
---|
268 | // fake it
|
---|
269 | __cfaabi_tls.this_thread = mainThread;
|
---|
270 | }
|
---|
271 |
|
---|
272 | __cfadbg_print_safe(runtime_core, "Kernel : core %p terminated\n", this);
|
---|
273 | }
|
---|
274 |
|
---|
275 | static int * __volatile_errno() __attribute__((noinline));
|
---|
276 | static int * __volatile_errno() { asm(""); return &errno; }
|
---|
277 |
|
---|
278 | // KERNEL ONLY
|
---|
279 | // runThread runs a thread by context switching
|
---|
280 | // from the processor coroutine to the target thread
|
---|
281 | static void __run_thread(processor * this, $thread * thrd_dst) {
|
---|
282 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
283 | /* paranoid */ verifyf( thrd_dst->state == Ready || thrd_dst->preempted != __NO_PREEMPTION, "state : %d, preempted %d\n", thrd_dst->state, thrd_dst->preempted);
|
---|
284 | /* paranoid */ verifyf( thrd_dst->link.next == 0p, "Expected null got %p", thrd_dst->link.next );
|
---|
285 | __builtin_prefetch( thrd_dst->context.SP );
|
---|
286 |
|
---|
287 | __cfadbg_print_safe(runtime_core, "Kernel : core %p running thread %p (%s)\n", this, thrd_dst, thrd_dst->self_cor.name);
|
---|
288 |
|
---|
289 | $coroutine * proc_cor = get_coroutine(this->runner);
|
---|
290 |
|
---|
291 | // set state of processor coroutine to inactive
|
---|
292 | verify(proc_cor->state == Active);
|
---|
293 | proc_cor->state = Blocked;
|
---|
294 |
|
---|
295 | // Actually run the thread
|
---|
296 | RUNNING: while(true) {
|
---|
297 | thrd_dst->preempted = __NO_PREEMPTION;
|
---|
298 | thrd_dst->state = Active;
|
---|
299 |
|
---|
300 | // Update global state
|
---|
301 | kernelTLS().this_thread = thrd_dst;
|
---|
302 |
|
---|
303 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
304 | /* paranoid */ verify( kernelTLS().this_thread == thrd_dst );
|
---|
305 | /* paranoid */ verify( thrd_dst->curr_cluster == this->cltr );
|
---|
306 | /* paranoid */ verify( thrd_dst->context.SP );
|
---|
307 | /* paranoid */ verify( thrd_dst->state != Halted );
|
---|
308 | /* 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
|
---|
309 | /* 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
|
---|
310 | /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd_dst->canary );
|
---|
311 |
|
---|
312 |
|
---|
313 |
|
---|
314 | // set context switch to the thread that the processor is executing
|
---|
315 | __cfactx_switch( &proc_cor->context, &thrd_dst->context );
|
---|
316 | // when __cfactx_switch returns we are back in the processor coroutine
|
---|
317 |
|
---|
318 | /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd_dst->canary );
|
---|
319 | /* 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 );
|
---|
320 | /* 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 );
|
---|
321 | /* paranoid */ verify( thrd_dst->context.SP );
|
---|
322 | /* paranoid */ verify( thrd_dst->curr_cluster == this->cltr );
|
---|
323 | /* paranoid */ verify( kernelTLS().this_thread == thrd_dst );
|
---|
324 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
325 |
|
---|
326 | // Reset global state
|
---|
327 | kernelTLS().this_thread = 0p;
|
---|
328 |
|
---|
329 | // We just finished running a thread, there are a few things that could have happened.
|
---|
330 | // 1 - Regular case : the thread has blocked and now one has scheduled it yet.
|
---|
331 | // 2 - Racy case : the thread has blocked but someone has already tried to schedule it.
|
---|
332 | // 4 - Preempted
|
---|
333 | // In case 1, we may have won a race so we can't write to the state again.
|
---|
334 | // In case 2, we lost the race so we now own the thread.
|
---|
335 |
|
---|
336 | if(unlikely(thrd_dst->preempted != __NO_PREEMPTION)) {
|
---|
337 | // The thread was preempted, reschedule it and reset the flag
|
---|
338 | __schedule_thread( thrd_dst );
|
---|
339 | break RUNNING;
|
---|
340 | }
|
---|
341 |
|
---|
342 | if(unlikely(thrd_dst->state == Halting)) {
|
---|
343 | // The thread has halted, it should never be scheduled/run again
|
---|
344 | // finish the thread
|
---|
345 | __thread_finish( thrd_dst );
|
---|
346 | break RUNNING;
|
---|
347 | }
|
---|
348 |
|
---|
349 | /* paranoid */ verify( thrd_dst->state == Active );
|
---|
350 | thrd_dst->state = Blocked;
|
---|
351 |
|
---|
352 | // set state of processor coroutine to active and the thread to inactive
|
---|
353 | int old_ticket = __atomic_fetch_sub(&thrd_dst->ticket, 1, __ATOMIC_SEQ_CST);
|
---|
354 | switch(old_ticket) {
|
---|
355 | case TICKET_RUNNING:
|
---|
356 | // This is case 1, the regular case, nothing more is needed
|
---|
357 | break RUNNING;
|
---|
358 | case TICKET_UNBLOCK:
|
---|
359 | // This is case 2, the racy case, someone tried to run this thread before it finished blocking
|
---|
360 | // In this case, just run it again.
|
---|
361 | continue RUNNING;
|
---|
362 | default:
|
---|
363 | // This makes no sense, something is wrong abort
|
---|
364 | abort();
|
---|
365 | }
|
---|
366 | }
|
---|
367 |
|
---|
368 | // Just before returning to the processor, set the processor coroutine to active
|
---|
369 | proc_cor->state = Active;
|
---|
370 |
|
---|
371 | __cfadbg_print_safe(runtime_core, "Kernel : core %p finished running thread %p\n", this, thrd_dst);
|
---|
372 |
|
---|
373 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
374 | }
|
---|
375 |
|
---|
376 | // KERNEL_ONLY
|
---|
377 | void returnToKernel() {
|
---|
378 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
379 | $coroutine * proc_cor = get_coroutine(kernelTLS().this_processor->runner);
|
---|
380 | $thread * thrd_src = kernelTLS().this_thread;
|
---|
381 |
|
---|
382 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
383 | struct processor * last_proc = kernelTLS().this_processor;
|
---|
384 | #endif
|
---|
385 |
|
---|
386 | // Run the thread on this processor
|
---|
387 | {
|
---|
388 | int local_errno = *__volatile_errno();
|
---|
389 | #if defined( __i386 ) || defined( __x86_64 )
|
---|
390 | __x87_store;
|
---|
391 | #endif
|
---|
392 | /* paranoid */ verify( proc_cor->context.SP );
|
---|
393 | /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd_src->canary );
|
---|
394 | __cfactx_switch( &thrd_src->context, &proc_cor->context );
|
---|
395 | /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd_src->canary );
|
---|
396 | #if defined( __i386 ) || defined( __x86_64 )
|
---|
397 | __x87_load;
|
---|
398 | #endif
|
---|
399 | *__volatile_errno() = local_errno;
|
---|
400 | }
|
---|
401 |
|
---|
402 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
403 | if(last_proc != kernelTLS().this_processor) {
|
---|
404 | __tls_stats()->ready.threads.migration++;
|
---|
405 | }
|
---|
406 | #endif
|
---|
407 |
|
---|
408 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
409 | /* 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 );
|
---|
410 | /* 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 );
|
---|
411 | }
|
---|
412 |
|
---|
413 | //-----------------------------------------------------------------------------
|
---|
414 | // Scheduler routines
|
---|
415 | // KERNEL ONLY
|
---|
416 | void __schedule_thread( $thread * thrd ) {
|
---|
417 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
418 | /* paranoid */ verify( kernelTLS().this_proc_id );
|
---|
419 | /* paranoid */ verify( thrd );
|
---|
420 | /* paranoid */ verify( thrd->state != Halted );
|
---|
421 | /* paranoid */ verify( thrd->curr_cluster );
|
---|
422 | /* paranoid */ #if defined( __CFA_WITH_VERIFY__ )
|
---|
423 | /* paranoid */ if( thrd->state == Blocked || thrd->state == Start ) assertf( thrd->preempted == __NO_PREEMPTION,
|
---|
424 | "Error inactive thread marked as preempted, state %d, preemption %d\n", thrd->state, thrd->preempted );
|
---|
425 | /* paranoid */ if( thrd->preempted != __NO_PREEMPTION ) assertf(thrd->state == Active,
|
---|
426 | "Error preempted thread marked as not currently running, state %d, preemption %d\n", thrd->state, thrd->preempted );
|
---|
427 | /* paranoid */ #endif
|
---|
428 | /* paranoid */ verifyf( thrd->link.next == 0p, "Expected null got %p", thrd->link.next );
|
---|
429 | /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd->canary );
|
---|
430 |
|
---|
431 |
|
---|
432 | if (thrd->preempted == __NO_PREEMPTION) thrd->state = Ready;
|
---|
433 |
|
---|
434 | ready_schedule_lock();
|
---|
435 | // Dereference the thread now because once we push it, there is not guaranteed it's still valid.
|
---|
436 | struct cluster * cl = thrd->curr_cluster;
|
---|
437 |
|
---|
438 | // push the thread to the cluster ready-queue
|
---|
439 | push( cl, thrd );
|
---|
440 |
|
---|
441 | // variable thrd is no longer safe to use
|
---|
442 |
|
---|
443 | // wake the cluster using the save variable.
|
---|
444 | __wake_one( cl );
|
---|
445 | ready_schedule_unlock();
|
---|
446 |
|
---|
447 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
448 | }
|
---|
449 |
|
---|
450 | // KERNEL ONLY
|
---|
451 | static inline $thread * __next_thread(cluster * this) with( *this ) {
|
---|
452 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
453 | /* paranoid */ verify( kernelTLS().this_proc_id );
|
---|
454 |
|
---|
455 | ready_schedule_lock();
|
---|
456 | $thread * thrd = pop( this );
|
---|
457 | ready_schedule_unlock();
|
---|
458 |
|
---|
459 | /* paranoid */ verify( kernelTLS().this_proc_id );
|
---|
460 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
461 | return thrd;
|
---|
462 | }
|
---|
463 |
|
---|
464 | // KERNEL ONLY
|
---|
465 | static inline $thread * __next_thread_slow(cluster * this) with( *this ) {
|
---|
466 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
467 | /* paranoid */ verify( kernelTLS().this_proc_id );
|
---|
468 |
|
---|
469 | ready_schedule_lock();
|
---|
470 | $thread * thrd = pop_slow( this );
|
---|
471 | ready_schedule_unlock();
|
---|
472 |
|
---|
473 | /* paranoid */ verify( kernelTLS().this_proc_id );
|
---|
474 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
475 | return thrd;
|
---|
476 | }
|
---|
477 |
|
---|
478 | void unpark( $thread * thrd ) {
|
---|
479 | if( !thrd ) return;
|
---|
480 |
|
---|
481 | int old_ticket = __atomic_fetch_add(&thrd->ticket, 1, __ATOMIC_SEQ_CST);
|
---|
482 | switch(old_ticket) {
|
---|
483 | case TICKET_RUNNING:
|
---|
484 | // Wake won the race, the thread will reschedule/rerun itself
|
---|
485 | break;
|
---|
486 | case TICKET_BLOCKED:
|
---|
487 | /* paranoid */ verify( ! thrd->preempted != __NO_PREEMPTION );
|
---|
488 | /* paranoid */ verify( thrd->state == Blocked );
|
---|
489 |
|
---|
490 | {
|
---|
491 | /* paranoid */ verify( publicTLS_get(this_proc_id) );
|
---|
492 | bool full = publicTLS_get(this_proc_id)->full_proc;
|
---|
493 | if(full) disable_interrupts();
|
---|
494 |
|
---|
495 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
496 |
|
---|
497 | // Wake lost the race,
|
---|
498 | __schedule_thread( thrd );
|
---|
499 |
|
---|
500 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
501 |
|
---|
502 | if(full) enable_interrupts( __cfaabi_dbg_ctx );
|
---|
503 | /* paranoid */ verify( publicTLS_get(this_proc_id) );
|
---|
504 | }
|
---|
505 |
|
---|
506 | break;
|
---|
507 | default:
|
---|
508 | // This makes no sense, something is wrong abort
|
---|
509 | abort("Thread %p (%s) has mismatch park/unpark\n", thrd, thrd->self_cor.name);
|
---|
510 | }
|
---|
511 | }
|
---|
512 |
|
---|
513 | void park( void ) {
|
---|
514 | /* paranoid */ verify( __preemption_enabled() );
|
---|
515 | disable_interrupts();
|
---|
516 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
517 | /* paranoid */ verify( kernelTLS().this_thread->preempted == __NO_PREEMPTION );
|
---|
518 |
|
---|
519 | returnToKernel();
|
---|
520 |
|
---|
521 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
522 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
523 | /* paranoid */ verify( __preemption_enabled() );
|
---|
524 |
|
---|
525 | }
|
---|
526 |
|
---|
527 | extern "C" {
|
---|
528 | // Leave the thread monitor
|
---|
529 | // last routine called by a thread.
|
---|
530 | // Should never return
|
---|
531 | void __cfactx_thrd_leave() {
|
---|
532 | $thread * thrd = active_thread();
|
---|
533 | $monitor * this = &thrd->self_mon;
|
---|
534 |
|
---|
535 | // Lock the monitor now
|
---|
536 | lock( this->lock __cfaabi_dbg_ctx2 );
|
---|
537 |
|
---|
538 | disable_interrupts();
|
---|
539 |
|
---|
540 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
541 | /* paranoid */ verify( thrd->state == Active );
|
---|
542 | /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd->canary );
|
---|
543 | /* paranoid */ verify( kernelTLS().this_thread == thrd );
|
---|
544 | /* paranoid */ verify( thrd->context.SP );
|
---|
545 | /* 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 );
|
---|
546 | /* 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 );
|
---|
547 |
|
---|
548 | thrd->state = Halting;
|
---|
549 | if( TICKET_RUNNING != thrd->ticket ) { abort( "Thread terminated with pending unpark" ); }
|
---|
550 | if( thrd != this->owner ) { abort( "Thread internal monitor has incorrect owner" ); }
|
---|
551 | if( this->recursion != 1) { abort( "Thread internal monitor has unbalanced recursion" ); }
|
---|
552 |
|
---|
553 | // Leave the thread
|
---|
554 | returnToKernel();
|
---|
555 |
|
---|
556 | // Control flow should never reach here!
|
---|
557 | abort();
|
---|
558 | }
|
---|
559 | }
|
---|
560 |
|
---|
561 | // KERNEL ONLY
|
---|
562 | bool force_yield( __Preemption_Reason reason ) {
|
---|
563 | /* paranoid */ verify( __preemption_enabled() );
|
---|
564 | disable_interrupts();
|
---|
565 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
566 |
|
---|
567 | $thread * thrd = kernelTLS().this_thread;
|
---|
568 | /* paranoid */ verify(thrd->state == Active);
|
---|
569 |
|
---|
570 | // SKULLDUGGERY: It is possible that we are preempting this thread just before
|
---|
571 | // it was going to park itself. If that is the case and it is already using the
|
---|
572 | // intrusive fields then we can't use them to preempt the thread
|
---|
573 | // If that is the case, abandon the preemption.
|
---|
574 | bool preempted = false;
|
---|
575 | if(thrd->link.next == 0p) {
|
---|
576 | preempted = true;
|
---|
577 | thrd->preempted = reason;
|
---|
578 | returnToKernel();
|
---|
579 | }
|
---|
580 |
|
---|
581 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
582 | enable_interrupts_noPoll();
|
---|
583 | /* paranoid */ verify( __preemption_enabled() );
|
---|
584 |
|
---|
585 | return preempted;
|
---|
586 | }
|
---|
587 |
|
---|
588 | //=============================================================================================
|
---|
589 | // Kernel Idle Sleep
|
---|
590 | //=============================================================================================
|
---|
591 | // Wake a thread from the front if there are any
|
---|
592 | static void __wake_one(cluster * this) {
|
---|
593 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
594 | /* paranoid */ verify( ready_schedule_islocked() );
|
---|
595 |
|
---|
596 | // Check if there is a sleeping processor
|
---|
597 | processor * p;
|
---|
598 | unsigned idle;
|
---|
599 | unsigned total;
|
---|
600 | [idle, total, p] = query(this->idles);
|
---|
601 |
|
---|
602 | // If no one is sleeping, we are done
|
---|
603 | if( idle == 0 ) return;
|
---|
604 |
|
---|
605 | // We found a processor, wake it up
|
---|
606 | eventfd_t val;
|
---|
607 | val = 1;
|
---|
608 | eventfd_write( p->idle, val );
|
---|
609 |
|
---|
610 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
611 | __tls_stats()->ready.sleep.wakes++;
|
---|
612 | #endif
|
---|
613 |
|
---|
614 | /* paranoid */ verify( ready_schedule_islocked() );
|
---|
615 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
616 |
|
---|
617 | return;
|
---|
618 | }
|
---|
619 |
|
---|
620 | // Unconditionnaly wake a thread
|
---|
621 | void __wake_proc(processor * this) {
|
---|
622 | __cfadbg_print_safe(runtime_core, "Kernel : waking Processor %p\n", this);
|
---|
623 |
|
---|
624 | disable_interrupts();
|
---|
625 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
626 | eventfd_t val;
|
---|
627 | val = 1;
|
---|
628 | eventfd_write( this->idle, val );
|
---|
629 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
630 | }
|
---|
631 |
|
---|
632 | static void push (__cluster_idles & this, processor & proc) {
|
---|
633 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
634 | lock( this );
|
---|
635 | this.idle++;
|
---|
636 | /* paranoid */ verify( this.idle <= this.total );
|
---|
637 |
|
---|
638 | insert_first(this.list, proc);
|
---|
639 | unlock( this );
|
---|
640 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
641 | }
|
---|
642 |
|
---|
643 | static void remove(__cluster_idles & this, processor & proc) {
|
---|
644 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
645 | lock( this );
|
---|
646 | this.idle--;
|
---|
647 | /* paranoid */ verify( this.idle >= 0 );
|
---|
648 |
|
---|
649 | remove(proc);
|
---|
650 | unlock( this );
|
---|
651 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
652 | }
|
---|
653 |
|
---|
654 | static [unsigned idle, unsigned total, * processor] query( & __cluster_idles this ) {
|
---|
655 | for() {
|
---|
656 | uint64_t l = __atomic_load_n(&this.lock, __ATOMIC_SEQ_CST);
|
---|
657 | if( 1 == (l % 2) ) { Pause(); continue; }
|
---|
658 | unsigned idle = this.idle;
|
---|
659 | unsigned total = this.total;
|
---|
660 | processor * proc = &this.list`first;
|
---|
661 | // Compiler fence is unnecessary, but gcc-8 and older incorrectly reorder code without it
|
---|
662 | asm volatile("": : :"memory");
|
---|
663 | if(l != __atomic_load_n(&this.lock, __ATOMIC_SEQ_CST)) { Pause(); continue; }
|
---|
664 | return [idle, total, proc];
|
---|
665 | }
|
---|
666 | }
|
---|
667 |
|
---|
668 | //=============================================================================================
|
---|
669 | // Unexpected Terminating logic
|
---|
670 | //=============================================================================================
|
---|
671 | void __kernel_abort_msg( char * abort_text, int abort_text_size ) {
|
---|
672 | $thread * thrd = __cfaabi_tls.this_thread;
|
---|
673 |
|
---|
674 | if(thrd) {
|
---|
675 | int len = snprintf( abort_text, abort_text_size, "Error occurred while executing thread %.256s (%p)", thrd->self_cor.name, thrd );
|
---|
676 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
|
---|
677 |
|
---|
678 | if ( &thrd->self_cor != thrd->curr_cor ) {
|
---|
679 | len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", thrd->curr_cor->name, thrd->curr_cor );
|
---|
680 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
|
---|
681 | }
|
---|
682 | else {
|
---|
683 | __cfaabi_bits_write( STDERR_FILENO, ".\n", 2 );
|
---|
684 | }
|
---|
685 | }
|
---|
686 | else {
|
---|
687 | int len = snprintf( abort_text, abort_text_size, "Error occurred outside of any thread.\n" );
|
---|
688 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
|
---|
689 | }
|
---|
690 | }
|
---|
691 |
|
---|
692 | int __kernel_abort_lastframe( void ) __attribute__ ((__nothrow__)) {
|
---|
693 | return get_coroutine(__cfaabi_tls.this_thread) == get_coroutine(mainThread) ? 4 : 2;
|
---|
694 | }
|
---|
695 |
|
---|
696 | static __spinlock_t kernel_debug_lock;
|
---|
697 |
|
---|
698 | extern "C" {
|
---|
699 | void __cfaabi_bits_acquire() {
|
---|
700 | lock( kernel_debug_lock __cfaabi_dbg_ctx2 );
|
---|
701 | }
|
---|
702 |
|
---|
703 | void __cfaabi_bits_release() {
|
---|
704 | unlock( kernel_debug_lock );
|
---|
705 | }
|
---|
706 | }
|
---|
707 |
|
---|
708 | //=============================================================================================
|
---|
709 | // Kernel Utilities
|
---|
710 | //=============================================================================================
|
---|
711 | #if defined(CFA_HAVE_LINUX_IO_URING_H)
|
---|
712 | #include "io/types.hfa"
|
---|
713 | #endif
|
---|
714 |
|
---|
715 | static inline void __maybe_io_drain( processor * proc ) {
|
---|
716 | #if defined(CFA_HAVE_LINUX_IO_URING_H)
|
---|
717 | __cfadbg_print_safe(runtime_core, "Kernel : core %p checking io for ring %d\n", proc, proc->io.ctx->fd);
|
---|
718 |
|
---|
719 | // Check if we should drain the queue
|
---|
720 | $io_context * ctx = proc->io.ctx;
|
---|
721 | unsigned head = *ctx->cq.head;
|
---|
722 | unsigned tail = *ctx->cq.tail;
|
---|
723 | if(head != tail) __cfa_io_drain( proc );
|
---|
724 | #endif
|
---|
725 | }
|
---|
726 |
|
---|
727 | //-----------------------------------------------------------------------------
|
---|
728 | // Debug
|
---|
729 | __cfaabi_dbg_debug_do(
|
---|
730 | extern "C" {
|
---|
731 | void __cfaabi_dbg_record_lock(__spinlock_t & this, const char prev_name[]) {
|
---|
732 | this.prev_name = prev_name;
|
---|
733 | this.prev_thrd = kernelTLS().this_thread;
|
---|
734 | }
|
---|
735 | }
|
---|
736 | )
|
---|
737 |
|
---|
738 | //-----------------------------------------------------------------------------
|
---|
739 | // Debug
|
---|
740 | bool threading_enabled(void) __attribute__((const)) {
|
---|
741 | return true;
|
---|
742 | }
|
---|
743 |
|
---|
744 | //-----------------------------------------------------------------------------
|
---|
745 | // Statistics
|
---|
746 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
747 | void print_halts( processor & this ) {
|
---|
748 | this.print_halts = true;
|
---|
749 | }
|
---|
750 |
|
---|
751 | void print_stats_now( cluster & this, int flags ) {
|
---|
752 | __print_stats( this.stats, this.print_stats, "Cluster", this.name, (void*)&this );
|
---|
753 | }
|
---|
754 |
|
---|
755 | extern int __print_alarm_stats;
|
---|
756 | void print_alarm_stats() {
|
---|
757 | __print_alarm_stats = -1;
|
---|
758 | }
|
---|
759 | #endif
|
---|
760 | // Local Variables: //
|
---|
761 | // mode: c //
|
---|
762 | // tab-width: 4 //
|
---|
763 | // End: //
|
---|