[e660761] | 1 | //
|
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2020 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/startup.cfa --
|
---|
| 8 | //
|
---|
| 9 | // Author : Thierry Delisle
|
---|
| 10 | // Created On : Thu Jul 30 15:12:54 2020
|
---|
| 11 | // Last Modified By :
|
---|
| 12 | // Last Modified On :
|
---|
| 13 | // Update Count :
|
---|
| 14 | //
|
---|
| 15 |
|
---|
| 16 | #define __cforall_thread__
|
---|
| 17 |
|
---|
| 18 | // C Includes
|
---|
| 19 | #include <errno.h> // errno
|
---|
| 20 | #include <string.h> // strerror
|
---|
| 21 | #include <unistd.h> // sysconf
|
---|
| 22 | extern "C" {
|
---|
| 23 | #include <limits.h> // PTHREAD_STACK_MIN
|
---|
| 24 | #include <sys/mman.h> // mprotect
|
---|
| 25 | #include <sys/resource.h> // getrlimit
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | // CFA Includes
|
---|
| 29 | #include "kernel_private.hfa"
|
---|
| 30 | #include "startup.hfa" // STARTUP_PRIORITY_XXX
|
---|
[bfcf6b9] | 31 | #include "math.hfa"
|
---|
[e660761] | 32 |
|
---|
[97229d6] | 33 | #define CFA_PROCESSOR_USE_MMAP 0
|
---|
| 34 |
|
---|
[e660761] | 35 | //-----------------------------------------------------------------------------
|
---|
| 36 | // Some assembly required
|
---|
| 37 | #if defined( __i386 )
|
---|
[88cafe7] | 38 | #define CtxGet( ctx ) __asm__ volatile ( \
|
---|
| 39 | "movl %%esp,%0\n" \
|
---|
| 40 | "movl %%ebp,%1\n" \
|
---|
| 41 | : "=rm" (ctx.SP), \
|
---|
| 42 | "=rm" (ctx.FP) \
|
---|
| 43 | )
|
---|
[e660761] | 44 | #elif defined( __x86_64 )
|
---|
[88cafe7] | 45 | #define CtxGet( ctx ) __asm__ volatile ( \
|
---|
| 46 | "movq %%rsp,%0\n" \
|
---|
| 47 | "movq %%rbp,%1\n" \
|
---|
| 48 | : "=rm" (ctx.SP), \
|
---|
| 49 | "=rm" (ctx.FP) \
|
---|
| 50 | )
|
---|
| 51 | #elif defined( __aarch64__ )
|
---|
| 52 | #define CtxGet( ctx ) __asm__ volatile ( \
|
---|
| 53 | "mov %0, sp\n" \
|
---|
| 54 | "mov %1, fp\n" \
|
---|
| 55 | : "=rm" (ctx.SP), \
|
---|
| 56 | "=rm" (ctx.FP) \
|
---|
| 57 | )
|
---|
[e660761] | 58 | #else
|
---|
| 59 | #error unknown hardware architecture
|
---|
| 60 | #endif
|
---|
| 61 |
|
---|
| 62 | //-----------------------------------------------------------------------------
|
---|
| 63 | // Start and stop routine for the kernel, declared first to make sure they run first
|
---|
| 64 | static void __kernel_startup (void) __attribute__(( constructor( STARTUP_PRIORITY_KERNEL ) ));
|
---|
| 65 | static void __kernel_shutdown(void) __attribute__(( destructor ( STARTUP_PRIORITY_KERNEL ) ));
|
---|
| 66 |
|
---|
| 67 | //-----------------------------------------------------------------------------
|
---|
| 68 | // Static Forward Declarations
|
---|
| 69 | struct current_stack_info_t;
|
---|
| 70 |
|
---|
| 71 | static void * __invoke_processor(void * arg);
|
---|
| 72 | static void __kernel_first_resume( processor * this );
|
---|
| 73 | static void __kernel_last_resume ( processor * this );
|
---|
| 74 | static void init(processor & this, const char name[], cluster & _cltr);
|
---|
| 75 | static void deinit(processor & this);
|
---|
| 76 | static void doregister( struct cluster & cltr );
|
---|
| 77 | static void unregister( struct cluster & cltr );
|
---|
| 78 | static void ?{}( $coroutine & this, current_stack_info_t * info);
|
---|
| 79 | static void ?{}( $thread & this, current_stack_info_t * info);
|
---|
| 80 | static void ?{}(processorCtx_t & this) {}
|
---|
| 81 | static void ?{}(processorCtx_t & this, processor * proc, current_stack_info_t * info);
|
---|
[da3963a] | 82 | static void ?{}(__bin_sem_t & this);
|
---|
| 83 | static void ^?{}(__bin_sem_t & this);
|
---|
[e660761] | 84 |
|
---|
[f2384c9a] | 85 | #if defined(__CFA_WITH_VERIFY__)
|
---|
| 86 | static bool verify_fwd_bck_rng(void);
|
---|
| 87 | #endif
|
---|
| 88 |
|
---|
[e660761] | 89 | //-----------------------------------------------------------------------------
|
---|
| 90 | // Forward Declarations for other modules
|
---|
| 91 | extern void __kernel_alarm_startup(void);
|
---|
| 92 | extern void __kernel_alarm_shutdown(void);
|
---|
| 93 | extern void __kernel_io_startup (void);
|
---|
| 94 | extern void __kernel_io_shutdown(void);
|
---|
| 95 |
|
---|
| 96 | //-----------------------------------------------------------------------------
|
---|
| 97 | // Other Forward Declarations
|
---|
[1eb239e4] | 98 | extern void __wake_proc(processor *);
|
---|
[e660761] | 99 |
|
---|
| 100 | //-----------------------------------------------------------------------------
|
---|
| 101 | // Kernel storage
|
---|
| 102 | KERNEL_STORAGE(cluster, mainCluster);
|
---|
| 103 | KERNEL_STORAGE(processor, mainProcessor);
|
---|
| 104 | KERNEL_STORAGE($thread, mainThread);
|
---|
| 105 | KERNEL_STORAGE(__stack_t, mainThreadCtx);
|
---|
| 106 | KERNEL_STORAGE(io_context, mainPollerThread);
|
---|
| 107 | KERNEL_STORAGE(__scheduler_RWLock_t, __scheduler_lock);
|
---|
| 108 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
| 109 | KERNEL_STORAGE(__stats_t, mainProcStats);
|
---|
| 110 | #endif
|
---|
| 111 |
|
---|
| 112 | cluster * mainCluster;
|
---|
| 113 | processor * mainProcessor;
|
---|
| 114 | $thread * mainThread;
|
---|
| 115 | __scheduler_RWLock_t * __scheduler_lock;
|
---|
| 116 |
|
---|
| 117 | extern "C" {
|
---|
| 118 | struct { __dllist_t(cluster) list; __spinlock_t lock; } __cfa_dbg_global_clusters;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[dd92fe9] | 121 | extern size_t __page_size;
|
---|
[28c35e2] | 122 | extern int __map_prot;
|
---|
[e660761] | 123 |
|
---|
| 124 | //-----------------------------------------------------------------------------
|
---|
| 125 | // Global state
|
---|
[8fc652e0] | 126 | thread_local struct KernelThreadData __cfaabi_tls __attribute__ ((tls_model ( "initial-exec" ))) @= {
|
---|
[e660761] | 127 | NULL, // cannot use 0p
|
---|
| 128 | NULL,
|
---|
| 129 | NULL,
|
---|
[e873838] | 130 | NULL,
|
---|
[e660761] | 131 | { 1, false, false },
|
---|
| 132 | };
|
---|
| 133 |
|
---|
| 134 | //-----------------------------------------------------------------------------
|
---|
| 135 | // Struct to steal stack
|
---|
| 136 | struct current_stack_info_t {
|
---|
| 137 | __stack_t * storage; // pointer to stack object
|
---|
| 138 | void * base; // base of stack
|
---|
| 139 | void * limit; // stack grows towards stack limit
|
---|
| 140 | void * context; // address of cfa_context_t
|
---|
| 141 | };
|
---|
| 142 |
|
---|
| 143 | void ?{}( current_stack_info_t & this ) {
|
---|
| 144 | __stack_context_t ctx;
|
---|
| 145 | CtxGet( ctx );
|
---|
| 146 | this.base = ctx.FP;
|
---|
| 147 |
|
---|
| 148 | rlimit r;
|
---|
| 149 | getrlimit( RLIMIT_STACK, &r);
|
---|
| 150 | size_t size = r.rlim_cur;
|
---|
| 151 |
|
---|
| 152 | this.limit = (void *)(((intptr_t)this.base) - size);
|
---|
| 153 | this.context = &storage_mainThreadCtx;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 |
|
---|
| 157 |
|
---|
| 158 | //=============================================================================================
|
---|
| 159 | // Kernel Setup logic
|
---|
| 160 | //=============================================================================================
|
---|
| 161 | //-----------------------------------------------------------------------------
|
---|
| 162 | // Kernel boot procedures
|
---|
| 163 | static void __kernel_startup(void) {
|
---|
[8fc652e0] | 164 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
[e660761] | 165 | __cfadbg_print_safe(runtime_core, "Kernel : Starting\n");
|
---|
| 166 |
|
---|
| 167 | __cfa_dbg_global_clusters.list{ __get };
|
---|
| 168 | __cfa_dbg_global_clusters.lock{};
|
---|
| 169 |
|
---|
[f2384c9a] | 170 | /* paranoid */ verify( verify_fwd_bck_rng() );
|
---|
| 171 |
|
---|
[e660761] | 172 | // Initialize the global scheduler lock
|
---|
| 173 | __scheduler_lock = (__scheduler_RWLock_t*)&storage___scheduler_lock;
|
---|
| 174 | (*__scheduler_lock){};
|
---|
| 175 |
|
---|
| 176 | // Initialize the main cluster
|
---|
| 177 | mainCluster = (cluster *)&storage_mainCluster;
|
---|
| 178 | (*mainCluster){"Main Cluster", 0};
|
---|
| 179 |
|
---|
| 180 | __cfadbg_print_safe(runtime_core, "Kernel : Main cluster ready\n");
|
---|
| 181 |
|
---|
| 182 | // Start by initializing the main thread
|
---|
| 183 | // SKULLDUGGERY: the mainThread steals the process main thread
|
---|
| 184 | // which will then be scheduled by the mainProcessor normally
|
---|
| 185 | mainThread = ($thread *)&storage_mainThread;
|
---|
| 186 | current_stack_info_t info;
|
---|
| 187 | info.storage = (__stack_t*)&storage_mainThreadCtx;
|
---|
| 188 | (*mainThread){ &info };
|
---|
| 189 |
|
---|
| 190 | __cfadbg_print_safe(runtime_core, "Kernel : Main thread ready\n");
|
---|
| 191 |
|
---|
| 192 |
|
---|
| 193 |
|
---|
| 194 | // Construct the processor context of the main processor
|
---|
| 195 | void ?{}(processorCtx_t & this, processor * proc) {
|
---|
| 196 | (this.__cor){ "Processor" };
|
---|
| 197 | this.__cor.starter = 0p;
|
---|
| 198 | this.proc = proc;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | void ?{}(processor & this) with( this ) {
|
---|
| 202 | ( this.idle ){};
|
---|
[454f478] | 203 | ( this.terminated ){};
|
---|
[e660761] | 204 | ( this.runner ){};
|
---|
| 205 | init( this, "Main Processor", *mainCluster );
|
---|
| 206 | kernel_thread = pthread_self();
|
---|
| 207 |
|
---|
| 208 | runner{ &this };
|
---|
| 209 | __cfadbg_print_safe(runtime_core, "Kernel : constructed main processor context %p\n", &runner);
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | // Initialize the main processor and the main processor ctx
|
---|
| 213 | // (the coroutine that contains the processing control flow)
|
---|
| 214 | mainProcessor = (processor *)&storage_mainProcessor;
|
---|
| 215 | (*mainProcessor){};
|
---|
| 216 |
|
---|
| 217 | //initialize the global state variables
|
---|
[8fc652e0] | 218 | __cfaabi_tls.this_processor = mainProcessor;
|
---|
| 219 | __cfaabi_tls.this_proc_id = (__processor_id_t*)mainProcessor;
|
---|
| 220 | __cfaabi_tls.this_thread = mainThread;
|
---|
[e660761] | 221 |
|
---|
| 222 | #if !defined( __CFA_NO_STATISTICS__ )
|
---|
[8fc652e0] | 223 | __cfaabi_tls.this_stats = (__stats_t *)& storage_mainProcStats;
|
---|
| 224 | __init_stats( __cfaabi_tls.this_stats );
|
---|
[e660761] | 225 | #endif
|
---|
| 226 |
|
---|
| 227 | // Enable preemption
|
---|
| 228 | __kernel_alarm_startup();
|
---|
| 229 |
|
---|
| 230 | // Start IO
|
---|
| 231 | __kernel_io_startup();
|
---|
| 232 |
|
---|
| 233 | // Add the main thread to the ready queue
|
---|
| 234 | // once resume is called on mainProcessor->runner the mainThread needs to be scheduled like any normal thread
|
---|
[e873838] | 235 | __schedule_thread(mainThread);
|
---|
[e660761] | 236 |
|
---|
| 237 | // SKULLDUGGERY: Force a context switch to the main processor to set the main thread's context to the current UNIX
|
---|
| 238 | // context. Hence, the main thread does not begin through __cfactx_invoke_thread, like all other threads. The trick here is that
|
---|
| 239 | // mainThread is on the ready queue when this call is made.
|
---|
[8fc652e0] | 240 | __kernel_first_resume( __cfaabi_tls.this_processor );
|
---|
[e660761] | 241 |
|
---|
| 242 |
|
---|
| 243 | // THE SYSTEM IS NOW COMPLETELY RUNNING
|
---|
| 244 |
|
---|
| 245 |
|
---|
| 246 | // SKULLDUGGERY: The constructor for the mainCluster will call alloc with a dimension of 0
|
---|
| 247 | // malloc *can* return a non-null value, we should free it if that is the case
|
---|
| 248 | free( mainCluster->io.ctxs );
|
---|
| 249 |
|
---|
| 250 | // Now that the system is up, finish creating systems that need threading
|
---|
| 251 | mainCluster->io.ctxs = (io_context *)&storage_mainPollerThread;
|
---|
| 252 | mainCluster->io.cnt = 1;
|
---|
| 253 | (*mainCluster->io.ctxs){ *mainCluster };
|
---|
| 254 |
|
---|
| 255 | __cfadbg_print_safe(runtime_core, "Kernel : Started\n--------------------------------------------------\n\n");
|
---|
| 256 |
|
---|
[8fc652e0] | 257 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
[e660761] | 258 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
[8fc652e0] | 259 | /* paranoid */ verify( __preemption_enabled() );
|
---|
| 260 |
|
---|
[e660761] | 261 | }
|
---|
| 262 |
|
---|
| 263 | static void __kernel_shutdown(void) {
|
---|
| 264 | //Before we start shutting things down, wait for systems that need threading to shutdown
|
---|
| 265 | ^(*mainCluster->io.ctxs){};
|
---|
| 266 | mainCluster->io.cnt = 0;
|
---|
| 267 | mainCluster->io.ctxs = 0p;
|
---|
| 268 |
|
---|
[8fc652e0] | 269 | /* paranoid */ verify( __preemption_enabled() );
|
---|
[e660761] | 270 | disable_interrupts();
|
---|
[8fc652e0] | 271 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
[e660761] | 272 |
|
---|
| 273 | __cfadbg_print_safe(runtime_core, "\n--------------------------------------------------\nKernel : Shutting down\n");
|
---|
| 274 |
|
---|
| 275 | // SKULLDUGGERY: Notify the mainProcessor it needs to terminates.
|
---|
| 276 | // When its coroutine terminates, it return control to the mainThread
|
---|
| 277 | // which is currently here
|
---|
| 278 | __atomic_store_n(&mainProcessor->do_terminate, true, __ATOMIC_RELEASE);
|
---|
[8fc652e0] | 279 | __kernel_last_resume( __cfaabi_tls.this_processor );
|
---|
[e660761] | 280 | mainThread->self_cor.state = Halted;
|
---|
| 281 |
|
---|
| 282 | // THE SYSTEM IS NOW COMPLETELY STOPPED
|
---|
| 283 |
|
---|
| 284 | // Disable preemption
|
---|
| 285 | __kernel_alarm_shutdown();
|
---|
| 286 |
|
---|
| 287 | // Stop IO
|
---|
| 288 | __kernel_io_shutdown();
|
---|
| 289 |
|
---|
| 290 | // Destroy the main processor and its context in reverse order of construction
|
---|
| 291 | // These were manually constructed so we need manually destroy them
|
---|
| 292 | void ^?{}(processor & this) with( this ){
|
---|
| 293 | deinit( this );
|
---|
| 294 |
|
---|
| 295 | /* paranoid */ verify( this.do_terminate == true );
|
---|
| 296 | __cfaabi_dbg_print_safe("Kernel : destroyed main processor context %p\n", &runner);
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | ^(*mainProcessor){};
|
---|
| 300 |
|
---|
| 301 | // Final step, destroy the main thread since it is no longer needed
|
---|
| 302 |
|
---|
| 303 | // Since we provided a stack to this taxk it will not destroy anything
|
---|
| 304 | /* paranoid */ verify(mainThread->self_cor.stack.storage == (__stack_t*)(((uintptr_t)&storage_mainThreadCtx)| 0x1));
|
---|
| 305 | ^(*mainThread){};
|
---|
| 306 |
|
---|
| 307 | ^(*mainCluster){};
|
---|
| 308 |
|
---|
| 309 | ^(*__scheduler_lock){};
|
---|
| 310 |
|
---|
| 311 | ^(__cfa_dbg_global_clusters.list){};
|
---|
| 312 | ^(__cfa_dbg_global_clusters.lock){};
|
---|
| 313 |
|
---|
| 314 | __cfadbg_print_safe(runtime_core, "Kernel : Shutdown complete\n");
|
---|
| 315 | }
|
---|
| 316 |
|
---|
| 317 | //=============================================================================================
|
---|
| 318 | // Kernel Initial Scheduling logic
|
---|
| 319 | //=============================================================================================
|
---|
| 320 |
|
---|
| 321 | // Context invoker for processors
|
---|
| 322 | // This is the entry point for processors (kernel threads) *except* for the main processor
|
---|
| 323 | // It effectively constructs a coroutine by stealing the pthread stack
|
---|
| 324 | static void * __invoke_processor(void * arg) {
|
---|
| 325 | #if !defined( __CFA_NO_STATISTICS__ )
|
---|
| 326 | __stats_t local_stats;
|
---|
| 327 | __init_stats( &local_stats );
|
---|
[8fc652e0] | 328 | __cfaabi_tls.this_stats = &local_stats;
|
---|
[e660761] | 329 | #endif
|
---|
| 330 |
|
---|
| 331 | processor * proc = (processor *) arg;
|
---|
[8fc652e0] | 332 | __cfaabi_tls.this_processor = proc;
|
---|
| 333 | __cfaabi_tls.this_proc_id = (__processor_id_t*)proc;
|
---|
| 334 | __cfaabi_tls.this_thread = 0p;
|
---|
| 335 | __cfaabi_tls.preemption_state.[enabled, disable_count] = [false, 1];
|
---|
[e660761] | 336 | // SKULLDUGGERY: We want to create a context for the processor coroutine
|
---|
| 337 | // which is needed for the 2-step context switch. However, there is no reason
|
---|
| 338 | // to waste the perfectly valid stack create by pthread.
|
---|
| 339 | current_stack_info_t info;
|
---|
| 340 | __stack_t ctx;
|
---|
| 341 | info.storage = &ctx;
|
---|
| 342 | (proc->runner){ proc, &info };
|
---|
| 343 |
|
---|
| 344 | __cfaabi_dbg_print_safe("Coroutine : created stack %p\n", get_coroutine(proc->runner)->stack.storage);
|
---|
| 345 |
|
---|
| 346 | //Set global state
|
---|
[8fc652e0] | 347 | __cfaabi_tls.this_thread = 0p;
|
---|
[e660761] | 348 |
|
---|
| 349 | //We now have a proper context from which to schedule threads
|
---|
| 350 | __cfadbg_print_safe(runtime_core, "Kernel : core %p created (%p, %p)\n", proc, &proc->runner, &ctx);
|
---|
| 351 |
|
---|
| 352 | // SKULLDUGGERY: Since the coroutine doesn't have its own stack, we can't
|
---|
| 353 | // resume it to start it like it normally would, it will just context switch
|
---|
| 354 | // back to here. Instead directly call the main since we already are on the
|
---|
| 355 | // appropriate stack.
|
---|
| 356 | get_coroutine(proc->runner)->state = Active;
|
---|
| 357 | main( proc->runner );
|
---|
| 358 | get_coroutine(proc->runner)->state = Halted;
|
---|
| 359 |
|
---|
| 360 | // Main routine of the core returned, the core is now fully terminated
|
---|
| 361 | __cfadbg_print_safe(runtime_core, "Kernel : core %p main ended (%p)\n", proc, &proc->runner);
|
---|
| 362 |
|
---|
| 363 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
| 364 | __tally_stats(proc->cltr->stats, &local_stats);
|
---|
| 365 | if( 0 != proc->print_stats ) {
|
---|
[1b033b8] | 366 | __print_stats( &local_stats, proc->print_stats, "Processor ", proc->name, (void*)proc );
|
---|
[e660761] | 367 | }
|
---|
| 368 | #endif
|
---|
| 369 |
|
---|
| 370 | return 0p;
|
---|
| 371 | }
|
---|
| 372 |
|
---|
| 373 | static void __kernel_first_resume( processor * this ) {
|
---|
| 374 | $thread * src = mainThread;
|
---|
| 375 | $coroutine * dst = get_coroutine(this->runner);
|
---|
| 376 |
|
---|
[8fc652e0] | 377 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
[e660761] | 378 |
|
---|
[8fc652e0] | 379 | __cfaabi_tls.this_thread->curr_cor = dst;
|
---|
[e660761] | 380 | __stack_prepare( &dst->stack, 65000 );
|
---|
| 381 | __cfactx_start(main, dst, this->runner, __cfactx_invoke_coroutine);
|
---|
| 382 |
|
---|
[8fc652e0] | 383 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
[e660761] | 384 |
|
---|
| 385 | dst->last = &src->self_cor;
|
---|
| 386 | dst->starter = dst->starter ? dst->starter : &src->self_cor;
|
---|
| 387 |
|
---|
| 388 | // make sure the current state is still correct
|
---|
| 389 | /* paranoid */ verify(src->state == Ready);
|
---|
| 390 |
|
---|
| 391 | // context switch to specified coroutine
|
---|
| 392 | verify( dst->context.SP );
|
---|
| 393 | __cfactx_switch( &src->context, &dst->context );
|
---|
| 394 | // when __cfactx_switch returns we are back in the src coroutine
|
---|
| 395 |
|
---|
| 396 | mainThread->curr_cor = &mainThread->self_cor;
|
---|
| 397 |
|
---|
| 398 | // make sure the current state has been update
|
---|
| 399 | /* paranoid */ verify(src->state == Active);
|
---|
| 400 |
|
---|
[8fc652e0] | 401 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
[e660761] | 402 | }
|
---|
| 403 |
|
---|
| 404 | // KERNEL_ONLY
|
---|
| 405 | static void __kernel_last_resume( processor * this ) {
|
---|
| 406 | $coroutine * src = &mainThread->self_cor;
|
---|
| 407 | $coroutine * dst = get_coroutine(this->runner);
|
---|
| 408 |
|
---|
[8fc652e0] | 409 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
| 410 | /* paranoid */ verify( dst->starter == src );
|
---|
| 411 | /* paranoid */ verify( dst->context.SP );
|
---|
[e660761] | 412 |
|
---|
| 413 | // SKULLDUGGERY in debug the processors check that the
|
---|
| 414 | // stack is still within the limit of the stack limits after running a thread.
|
---|
| 415 | // that check doesn't make sense if we context switch to the processor using the
|
---|
| 416 | // coroutine semantics. Since this is a special case, use the current context
|
---|
| 417 | // info to populate these fields.
|
---|
| 418 | __cfaabi_dbg_debug_do(
|
---|
| 419 | __stack_context_t ctx;
|
---|
| 420 | CtxGet( ctx );
|
---|
| 421 | mainThread->context.SP = ctx.SP;
|
---|
| 422 | mainThread->context.FP = ctx.FP;
|
---|
| 423 | )
|
---|
| 424 |
|
---|
| 425 | // context switch to the processor
|
---|
| 426 | __cfactx_switch( &src->context, &dst->context );
|
---|
| 427 | }
|
---|
| 428 |
|
---|
| 429 |
|
---|
| 430 | //=============================================================================================
|
---|
| 431 | // Kernel Object Constructors logic
|
---|
| 432 | //=============================================================================================
|
---|
| 433 | //-----------------------------------------------------------------------------
|
---|
| 434 | // Main thread construction
|
---|
| 435 | static void ?{}( $coroutine & this, current_stack_info_t * info) with( this ) {
|
---|
| 436 | stack.storage = info->storage;
|
---|
| 437 | with(*stack.storage) {
|
---|
| 438 | limit = info->limit;
|
---|
| 439 | base = info->base;
|
---|
| 440 | }
|
---|
| 441 | __attribute__((may_alias)) intptr_t * istorage = (intptr_t*) &stack.storage;
|
---|
| 442 | *istorage |= 0x1;
|
---|
| 443 | name = "Main Thread";
|
---|
| 444 | state = Start;
|
---|
| 445 | starter = 0p;
|
---|
| 446 | last = 0p;
|
---|
| 447 | cancellation = 0p;
|
---|
| 448 | }
|
---|
| 449 |
|
---|
| 450 | static void ?{}( $thread & this, current_stack_info_t * info) with( this ) {
|
---|
[6a77224] | 451 | ticket = TICKET_RUNNING;
|
---|
[e660761] | 452 | state = Start;
|
---|
| 453 | self_cor{ info };
|
---|
| 454 | curr_cor = &self_cor;
|
---|
| 455 | curr_cluster = mainCluster;
|
---|
| 456 | self_mon.owner = &this;
|
---|
| 457 | self_mon.recursion = 1;
|
---|
| 458 | self_mon_p = &self_mon;
|
---|
| 459 | link.next = 0p;
|
---|
| 460 | link.prev = 0p;
|
---|
[b4b63e8] | 461 | #if defined( __CFA_WITH_VERIFY__ )
|
---|
[ac12f1f] | 462 | canary = 0x0D15EA5E0D15EA5Ep;
|
---|
[b4b63e8] | 463 | #endif
|
---|
[e660761] | 464 |
|
---|
| 465 | node.next = 0p;
|
---|
| 466 | node.prev = 0p;
|
---|
| 467 | doregister(curr_cluster, this);
|
---|
| 468 |
|
---|
| 469 | monitors{ &self_mon_p, 1, (fptr_t)0 };
|
---|
| 470 | }
|
---|
| 471 |
|
---|
| 472 | //-----------------------------------------------------------------------------
|
---|
| 473 | // Processor
|
---|
| 474 | // Construct the processor context of non-main processors
|
---|
| 475 | static void ?{}(processorCtx_t & this, processor * proc, current_stack_info_t * info) {
|
---|
| 476 | (this.__cor){ info };
|
---|
| 477 | this.proc = proc;
|
---|
| 478 | }
|
---|
| 479 |
|
---|
| 480 | static void init(processor & this, const char name[], cluster & _cltr) with( this ) {
|
---|
| 481 | this.name = name;
|
---|
| 482 | this.cltr = &_cltr;
|
---|
[58d64a4] | 483 | full_proc = true;
|
---|
[e660761] | 484 | do_terminate = false;
|
---|
| 485 | preemption_alarm = 0p;
|
---|
| 486 | pending_preemption = false;
|
---|
| 487 |
|
---|
| 488 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
| 489 | print_stats = 0;
|
---|
| 490 | print_halts = false;
|
---|
| 491 | #endif
|
---|
| 492 |
|
---|
[1eb239e4] | 493 | lock( this.cltr->idles );
|
---|
| 494 | int target = this.cltr->idles.total += 1u;
|
---|
| 495 | unlock( this.cltr->idles );
|
---|
[e660761] | 496 |
|
---|
| 497 | id = doregister((__processor_id_t*)&this);
|
---|
| 498 |
|
---|
| 499 | // Lock the RWlock so no-one pushes/pops while we are changing the queue
|
---|
| 500 | uint_fast32_t last_size = ready_mutate_lock();
|
---|
| 501 |
|
---|
| 502 | // Adjust the ready queue size
|
---|
| 503 | ready_queue_grow( cltr, target );
|
---|
| 504 |
|
---|
| 505 | // Unlock the RWlock
|
---|
| 506 | ready_mutate_unlock( last_size );
|
---|
| 507 |
|
---|
| 508 | __cfadbg_print_safe(runtime_core, "Kernel : core %p created\n", &this);
|
---|
| 509 | }
|
---|
| 510 |
|
---|
| 511 | // Not a ctor, it just preps the destruction but should not destroy members
|
---|
| 512 | static void deinit(processor & this) {
|
---|
[1eb239e4] | 513 | lock( this.cltr->idles );
|
---|
| 514 | int target = this.cltr->idles.total -= 1u;
|
---|
| 515 | unlock( this.cltr->idles );
|
---|
[e660761] | 516 |
|
---|
| 517 | // Lock the RWlock so no-one pushes/pops while we are changing the queue
|
---|
| 518 | uint_fast32_t last_size = ready_mutate_lock();
|
---|
| 519 |
|
---|
| 520 | // Adjust the ready queue size
|
---|
| 521 | ready_queue_shrink( this.cltr, target );
|
---|
| 522 |
|
---|
| 523 | // Unlock the RWlock
|
---|
| 524 | ready_mutate_unlock( last_size );
|
---|
| 525 |
|
---|
| 526 | // Finally we don't need the read_lock any more
|
---|
| 527 | unregister((__processor_id_t*)&this);
|
---|
| 528 | }
|
---|
| 529 |
|
---|
| 530 | void ?{}(processor & this, const char name[], cluster & _cltr) {
|
---|
| 531 | ( this.idle ){};
|
---|
[454f478] | 532 | ( this.terminated ){};
|
---|
[e660761] | 533 | ( this.runner ){};
|
---|
[62502cc4] | 534 |
|
---|
| 535 | disable_interrupts();
|
---|
| 536 | init( this, name, _cltr );
|
---|
| 537 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
[e660761] | 538 |
|
---|
| 539 | __cfadbg_print_safe(runtime_core, "Kernel : Starting core %p\n", &this);
|
---|
| 540 |
|
---|
| 541 | this.stack = __create_pthread( &this.kernel_thread, __invoke_processor, (void *)&this );
|
---|
| 542 |
|
---|
| 543 | }
|
---|
| 544 |
|
---|
[bfcf6b9] | 545 | extern size_t __page_size;
|
---|
[e660761] | 546 | void ^?{}(processor & this) with( this ){
|
---|
| 547 | if( ! __atomic_load_n(&do_terminate, __ATOMIC_ACQUIRE) ) {
|
---|
| 548 | __cfadbg_print_safe(runtime_core, "Kernel : core %p signaling termination\n", &this);
|
---|
| 549 |
|
---|
| 550 | __atomic_store_n(&do_terminate, true, __ATOMIC_RELAXED);
|
---|
| 551 | __wake_proc( &this );
|
---|
| 552 |
|
---|
[454f478] | 553 | wait( terminated );
|
---|
[8fc652e0] | 554 | /* paranoid */ verify( active_processor() != &this);
|
---|
[e660761] | 555 | }
|
---|
| 556 |
|
---|
[bfcf6b9] | 557 | __destroy_pthread( kernel_thread, this.stack, 0p );
|
---|
[e660761] | 558 |
|
---|
[62502cc4] | 559 | disable_interrupts();
|
---|
| 560 | deinit( this );
|
---|
| 561 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
[e660761] | 562 | }
|
---|
| 563 |
|
---|
| 564 | //-----------------------------------------------------------------------------
|
---|
| 565 | // Cluster
|
---|
[1eb239e4] | 566 | static void ?{}(__cluster_idles & this) {
|
---|
| 567 | this.lock = 0;
|
---|
| 568 | this.idle = 0;
|
---|
| 569 | this.total = 0;
|
---|
| 570 | (this.list){};
|
---|
| 571 | }
|
---|
| 572 |
|
---|
[e660761] | 573 | void ?{}(cluster & this, const char name[], Duration preemption_rate, unsigned num_io, const io_context_params & io_params) with( this ) {
|
---|
| 574 | this.name = name;
|
---|
| 575 | this.preemption_rate = preemption_rate;
|
---|
| 576 | ready_queue{};
|
---|
| 577 |
|
---|
| 578 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
| 579 | print_stats = 0;
|
---|
| 580 | stats = alloc();
|
---|
| 581 | __init_stats( stats );
|
---|
| 582 | #endif
|
---|
| 583 |
|
---|
| 584 | threads{ __get };
|
---|
| 585 |
|
---|
| 586 | doregister(this);
|
---|
| 587 |
|
---|
| 588 | // Lock the RWlock so no-one pushes/pops while we are changing the queue
|
---|
[772411a] | 589 | disable_interrupts();
|
---|
[e660761] | 590 | uint_fast32_t last_size = ready_mutate_lock();
|
---|
| 591 |
|
---|
| 592 | // Adjust the ready queue size
|
---|
| 593 | ready_queue_grow( &this, 0 );
|
---|
| 594 |
|
---|
| 595 | // Unlock the RWlock
|
---|
| 596 | ready_mutate_unlock( last_size );
|
---|
[772411a] | 597 | enable_interrupts_noPoll(); // Don't poll, could be in main cluster
|
---|
| 598 |
|
---|
[e660761] | 599 |
|
---|
| 600 | this.io.cnt = num_io;
|
---|
| 601 | this.io.ctxs = aalloc(num_io);
|
---|
| 602 | for(i; this.io.cnt) {
|
---|
| 603 | (this.io.ctxs[i]){ this, io_params };
|
---|
| 604 | }
|
---|
| 605 | }
|
---|
| 606 |
|
---|
| 607 | void ^?{}(cluster & this) {
|
---|
| 608 | for(i; this.io.cnt) {
|
---|
| 609 | ^(this.io.ctxs[i]){ true };
|
---|
| 610 | }
|
---|
| 611 | free(this.io.ctxs);
|
---|
| 612 |
|
---|
| 613 | // Lock the RWlock so no-one pushes/pops while we are changing the queue
|
---|
[772411a] | 614 | disable_interrupts();
|
---|
[e660761] | 615 | uint_fast32_t last_size = ready_mutate_lock();
|
---|
| 616 |
|
---|
| 617 | // Adjust the ready queue size
|
---|
| 618 | ready_queue_shrink( &this, 0 );
|
---|
| 619 |
|
---|
| 620 | // Unlock the RWlock
|
---|
| 621 | ready_mutate_unlock( last_size );
|
---|
[772411a] | 622 | enable_interrupts_noPoll(); // Don't poll, could be in main cluster
|
---|
[e660761] | 623 |
|
---|
| 624 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
| 625 | if( 0 != this.print_stats ) {
|
---|
[1b033b8] | 626 | __print_stats( this.stats, this.print_stats, "Cluster", this.name, (void*)&this );
|
---|
[e660761] | 627 | }
|
---|
| 628 | free( this.stats );
|
---|
| 629 | #endif
|
---|
| 630 |
|
---|
| 631 | unregister(this);
|
---|
| 632 | }
|
---|
| 633 |
|
---|
| 634 | //=============================================================================================
|
---|
| 635 | // Miscellaneous Initialization
|
---|
| 636 | //=============================================================================================
|
---|
| 637 | //-----------------------------------------------------------------------------
|
---|
| 638 | // Global Queues
|
---|
| 639 | static void doregister( cluster & cltr ) {
|
---|
| 640 | lock ( __cfa_dbg_global_clusters.lock __cfaabi_dbg_ctx2);
|
---|
| 641 | push_front( __cfa_dbg_global_clusters.list, cltr );
|
---|
| 642 | unlock ( __cfa_dbg_global_clusters.lock );
|
---|
| 643 | }
|
---|
| 644 |
|
---|
| 645 | static void unregister( cluster & cltr ) {
|
---|
| 646 | lock ( __cfa_dbg_global_clusters.lock __cfaabi_dbg_ctx2);
|
---|
| 647 | remove( __cfa_dbg_global_clusters.list, cltr );
|
---|
| 648 | unlock( __cfa_dbg_global_clusters.lock );
|
---|
| 649 | }
|
---|
| 650 |
|
---|
| 651 | void doregister( cluster * cltr, $thread & thrd ) {
|
---|
| 652 | lock (cltr->thread_list_lock __cfaabi_dbg_ctx2);
|
---|
| 653 | cltr->nthreads += 1;
|
---|
| 654 | push_front(cltr->threads, thrd);
|
---|
| 655 | unlock (cltr->thread_list_lock);
|
---|
| 656 | }
|
---|
| 657 |
|
---|
| 658 | void unregister( cluster * cltr, $thread & thrd ) {
|
---|
| 659 | lock (cltr->thread_list_lock __cfaabi_dbg_ctx2);
|
---|
| 660 | remove(cltr->threads, thrd );
|
---|
| 661 | cltr->nthreads -= 1;
|
---|
| 662 | unlock(cltr->thread_list_lock);
|
---|
| 663 | }
|
---|
| 664 |
|
---|
| 665 | static void check( int ret, const char func[] ) {
|
---|
| 666 | if ( ret ) { // pthread routines return errno values
|
---|
| 667 | abort( "%s : internal error, error(%d) %s.", func, ret, strerror( ret ) );
|
---|
| 668 | } // if
|
---|
| 669 | } // Abort
|
---|
| 670 |
|
---|
| 671 | void * __create_pthread( pthread_t * pthread, void * (*start)(void *), void * arg ) {
|
---|
| 672 | pthread_attr_t attr;
|
---|
| 673 |
|
---|
| 674 | check( pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute
|
---|
| 675 |
|
---|
| 676 | size_t stacksize;
|
---|
| 677 | // default stack size, normally defined by shell limit
|
---|
| 678 | check( pthread_attr_getstacksize( &attr, &stacksize ), "pthread_attr_getstacksize" );
|
---|
| 679 | assert( stacksize >= PTHREAD_STACK_MIN );
|
---|
| 680 |
|
---|
| 681 | void * stack;
|
---|
[97229d6] | 682 | #if CFA_PROCESSOR_USE_MMAP
|
---|
| 683 | stacksize = ceiling( stacksize, __page_size ) + __page_size;
|
---|
[dd92fe9] | 684 | stack = mmap(0p, stacksize, __map_prot, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
|
---|
[97229d6] | 685 | if(stack == ((void*)-1)) {
|
---|
| 686 | abort( "pthread stack creation : internal error, mmap failure, error(%d) %s.", errno, strerror( errno ) );
|
---|
| 687 | }
|
---|
| 688 | if ( mprotect( stack, __page_size, PROT_NONE ) == -1 ) {
|
---|
| 689 | abort( "pthread stack creation : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) );
|
---|
| 690 | } // if
|
---|
| 691 | #else
|
---|
| 692 | __cfaabi_dbg_debug_do(
|
---|
| 693 | stack = memalign( __page_size, stacksize + __page_size );
|
---|
| 694 | // pthread has no mechanism to create the guard page in user supplied stack.
|
---|
| 695 | if ( mprotect( stack, __page_size, PROT_NONE ) == -1 ) {
|
---|
| 696 | abort( "mprotect : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) );
|
---|
| 697 | } // if
|
---|
| 698 | );
|
---|
| 699 | __cfaabi_dbg_no_debug_do(
|
---|
| 700 | stack = malloc( stacksize );
|
---|
| 701 | );
|
---|
| 702 | #endif
|
---|
| 703 |
|
---|
[e660761] | 704 |
|
---|
| 705 | check( pthread_attr_setstack( &attr, stack, stacksize ), "pthread_attr_setstack" );
|
---|
| 706 |
|
---|
| 707 | check( pthread_create( pthread, &attr, start, arg ), "pthread_create" );
|
---|
| 708 | return stack;
|
---|
[88cafe7] | 709 | }
|
---|
[f2384c9a] | 710 |
|
---|
[bfcf6b9] | 711 | void __destroy_pthread( pthread_t pthread, void * stack, void ** retval ) {
|
---|
| 712 | int err = pthread_join( pthread, retval );
|
---|
| 713 | if( err != 0 ) abort("KERNEL ERROR: joining pthread %p caused error %s\n", (void*)pthread, strerror(err));
|
---|
| 714 |
|
---|
[97229d6] | 715 | #if CFA_PROCESSOR_USE_MMAP
|
---|
| 716 | pthread_attr_t attr;
|
---|
[bfcf6b9] | 717 |
|
---|
[97229d6] | 718 | check( pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute
|
---|
[bfcf6b9] | 719 |
|
---|
[97229d6] | 720 | size_t stacksize;
|
---|
| 721 | // default stack size, normally defined by shell limit
|
---|
| 722 | check( pthread_attr_getstacksize( &attr, &stacksize ), "pthread_attr_getstacksize" );
|
---|
| 723 | assert( stacksize >= PTHREAD_STACK_MIN );
|
---|
| 724 | stacksize += __page_size;
|
---|
[bfcf6b9] | 725 |
|
---|
[97229d6] | 726 | if(munmap(stack, stacksize) == -1) {
|
---|
| 727 | abort( "pthread stack destruction : internal error, munmap failure, error(%d) %s.", errno, strerror( errno ) );
|
---|
| 728 | }
|
---|
| 729 | #else
|
---|
[72a3aff] | 730 | __cfaabi_dbg_debug_do(
|
---|
| 731 | // pthread has no mechanism to create the guard page in user supplied stack.
|
---|
[28c35e2] | 732 | if ( mprotect( stack, __page_size, __map_prot ) == -1 ) {
|
---|
[72a3aff] | 733 | abort( "mprotect : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) );
|
---|
| 734 | } // if
|
---|
| 735 | );
|
---|
[97229d6] | 736 | free( stack );
|
---|
| 737 | #endif
|
---|
[bfcf6b9] | 738 | }
|
---|
| 739 |
|
---|
[da3963a] | 740 | extern "C" {
|
---|
| 741 | char * strerror(int);
|
---|
| 742 | }
|
---|
| 743 | #define CHECKED(x) { int err = x; if( err != 0 ) abort("KERNEL ERROR: Operation \"" #x "\" return error %d - %s\n", err, strerror(err)); }
|
---|
| 744 |
|
---|
| 745 | static void ?{}(__bin_sem_t & this) with( this ) {
|
---|
| 746 | // Create the mutex with error checking
|
---|
| 747 | pthread_mutexattr_t mattr;
|
---|
| 748 | pthread_mutexattr_init( &mattr );
|
---|
| 749 | pthread_mutexattr_settype( &mattr, PTHREAD_MUTEX_ERRORCHECK_NP);
|
---|
| 750 | pthread_mutex_init(&lock, &mattr);
|
---|
| 751 |
|
---|
| 752 | pthread_cond_init (&cond, (const pthread_condattr_t *)0p); // workaround trac#208: cast should not be required
|
---|
| 753 | val = 0;
|
---|
| 754 | }
|
---|
| 755 |
|
---|
| 756 | static void ^?{}(__bin_sem_t & this) with( this ) {
|
---|
| 757 | CHECKED( pthread_mutex_destroy(&lock) );
|
---|
| 758 | CHECKED( pthread_cond_destroy (&cond) );
|
---|
| 759 | }
|
---|
| 760 |
|
---|
| 761 | #undef CHECKED
|
---|
[bfcf6b9] | 762 |
|
---|
[f2384c9a] | 763 | #if defined(__CFA_WITH_VERIFY__)
|
---|
| 764 | static bool verify_fwd_bck_rng(void) {
|
---|
[8fc652e0] | 765 | __cfaabi_tls.ready_rng.fwd_seed = 25214903917_l64u * (rdtscl() ^ (uintptr_t)&verify_fwd_bck_rng);
|
---|
[f2384c9a] | 766 |
|
---|
| 767 | unsigned values[10];
|
---|
| 768 | for(i; 10) {
|
---|
| 769 | values[i] = __tls_rand_fwd();
|
---|
| 770 | }
|
---|
| 771 |
|
---|
| 772 | __tls_rand_advance_bck();
|
---|
| 773 |
|
---|
| 774 | for ( i; 9 -~= 0 ) {
|
---|
| 775 | if(values[i] != __tls_rand_bck()) {
|
---|
| 776 | return false;
|
---|
| 777 | }
|
---|
| 778 | }
|
---|
| 779 |
|
---|
| 780 | return true;
|
---|
| 781 | }
|
---|
[e67a82d] | 782 | #endif
|
---|