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