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