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