SuperTinyKernel™ RTOS 1.08.x
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
Loading...
Searching...
No Matches
stk_arch_x86-win32.cpp
Go to the documentation of this file.
1/*
2 * SuperTinyKernel(TM) RTOS: Lightweight High-Performance Deterministic C++ RTOS for Embedded Systems.
3 *
4 * Source: https://github.com/SuperTinyKernel-RTOS
5 *
6 * Copyright (c) 2022-2026 Neutron Code Limited <stk@neutroncode.com>. All Rights Reserved.
7 * License: MIT License, see LICENSE for a full text.
8 */
9
10// note: If missing, this header must be customized (get it in the root of the source folder) and
11// copied to the /include folder manually.
12#include "stk_config.h"
13
14#ifdef _STK_ARCH_X86_WIN32
15
16#include "stk_arch.h"
18
19using namespace stk;
20
21#define WIN32_LEAN_AND_MEAN
22#include <windows.h>
23#include <stdlib.h>
24#include <stdio.h>
25#include <assert.h>
26#include <list>
27#include <vector>
28
29using namespace stk;
30
31#ifndef WINAPI
32#define WINAPI __stdcall
33#endif
34
35typedef UINT MMRESULT;
36typedef MMRESULT (WINAPI * timeBeginPeriodF)(UINT uPeriod);
37static timeBeginPeriodF timeBeginPeriod = nullptr;
38
39#define STK_X86_WIN32_CRITICAL_SECTION CRITICAL_SECTION
40#define STK_X86_WIN32_CRITICAL_SECTION_INIT(SES) ::InitializeCriticalSection(SES)
41#define STK_X86_WIN32_CRITICAL_SECTION_START(SES) ::EnterCriticalSection(SES)
42#define STK_X86_WIN32_CRITICAL_SECTION_END(SES) ::LeaveCriticalSection(SES)
43#define STK_X86_WIN32_MIN_RESOLUTION (1000)
44#define STK_X86_WIN32_GET_SP(STACK) (STACK + 2) // +2 to overcome stack filler check inside Kernel (adjusting to +2 preserves 8-byte alignment)
45#define SLK_UNLOCKED hw::SpinLock::UNLOCKED
46#define SLK_LOCKED hw::SpinLock::LOCKED
47
50static __stk_forceinline bool HW_SpinLockTryLock(volatile LONG &lock)
51{
52 return (InterlockedCompareExchange(
53 reinterpret_cast<volatile LONG *>(&lock), SLK_LOCKED, SLK_UNLOCKED) == SLK_UNLOCKED);
54}
55
58static __stk_forceinline void HW_SpinLockLock(volatile LONG &lock)
59{
60 uint8_t sleep_time = 0;
61 uint32_t timeout = 0xFFFFFF;
62
63test:
64 while (!HW_SpinLockTryLock(lock))
65 {
66 if (--timeout == 0)
67 {
68 // invariant violated: the lock owner exited without releasing
70 }
71
72 for (volatile int32_t spin = 100; (spin != 0); spin--)
73 {
74 __stk_relax_cpu();
75
76 // check if became unlocked then try locking atomically again
77 if (lock == SLK_UNLOCKED)
78 goto test;
79 }
80
81 // avoid priority inversion
82 ::Sleep(sleep_time);
83 sleep_time ^= 1;
84 }
85}
86
89static __stk_forceinline void HW_SpinLockUnlock(volatile LONG &lock)
90{
91 InterlockedExchange(reinterpret_cast<volatile LONG *>(&lock), SLK_UNLOCKED);
92}
93
94struct Win32ScopedCriticalSection
95{
96 STK_X86_WIN32_CRITICAL_SECTION &m_sec;
97
98 explicit Win32ScopedCriticalSection(STK_X86_WIN32_CRITICAL_SECTION &sec) : m_sec(sec)
99 {
100 STK_X86_WIN32_CRITICAL_SECTION_START(&sec);
101 }
102 ~Win32ScopedCriticalSection()
103 {
104 STK_X86_WIN32_CRITICAL_SECTION_END(&m_sec);
105 }
106};
107
108class HiResClockQPC
109{
110 LARGE_INTEGER m_freq;
111 LARGE_INTEGER m_start;
112
113public:
114 explicit HiResClockQPC()
115 {
116 QueryPerformanceFrequency(&m_freq);
117 QueryPerformanceCounter(&m_start);
118 }
119
120 static HiResClockQPC *GetInstance()
121 {
122 // keep declaration function-local to allow compiler stripping it from the binary if
123 // it is unused by the user code
124 static HiResClockQPC clock;
125 return &clock;
126 }
127
128 Cycles GetCycles()
129 {
130 LARGE_INTEGER current;
131 QueryPerformanceCounter(&current);
132
133 // relative cycles since simulation start
134 return static_cast<Cycles>(current.QuadPart - m_start.QuadPart);
135 }
136
137 uint32_t GetFrequency()
138 {
139 return static_cast<uint32_t>(m_freq.QuadPart);
140 }
141};
142
144static struct Context final : public PlatformContext
145{
146 explicit Context()
147 : m_overrider(nullptr),
148 m_sleep_trap(nullptr),
149 m_exit_trap(nullptr),
150 m_winmm_dll(nullptr),
151 m_timer_thread(nullptr),
152 m_switch_event(nullptr),
153 m_pending_switch_id(TID_NONE),
154 m_tls(TLS_OUT_OF_INDEXES),
155 m_tasks(),
156 m_task_threads(),
157 m_timer_tid(0),
158 #if STK_TICKLESS_IDLE
159 m_sleep_ticks(0),
160 #endif
161 m_cs(),
162 m_csu_nesting(0),
163 m_started(false),
164 m_stop_signal(false)
165 {}
166
167 void Initialize(IPlatform::IEventHandler *handler, IKernelService *service, Stack *exit_trap,
168 uint32_t resolution_us)
169 {
170 InitializeBase(handler, service, exit_trap, resolution_us);
171
172 m_sleep_trap = nullptr; // set by Context::InitStack
173 m_exit_trap = nullptr; // set by Context::InitStack
174 m_winmm_dll = nullptr;
175 m_timer_thread = nullptr;
176 m_started = false;
177 m_stop_signal = false;
178 m_csu_nesting = 0;
179 m_timer_tid = 0;
180 m_pending_switch_id = TID_NONE;
181 #if STK_TICKLESS_IDLE
182 m_sleep_ticks = 0;
183 #endif
184
185 // auto-reset: used by a task's own thread to hand a forced context
186 // switch off to TimerThread, since only TimerThread (or a thread other
187 // than the one being switched) may safely call SwitchContext()
188 m_switch_event = CreateEventA(nullptr, FALSE, FALSE, nullptr);
189 STK_ASSERT(m_switch_event != nullptr);
190
191 #if STK_TLS
192 if ((m_tls = TlsAlloc()) == TLS_OUT_OF_INDEXES)
193 {
194 STK_ASSERT(false);
195 return;
196 }
197 #endif
198
199 STK_X86_WIN32_CRITICAL_SECTION_INIT(&m_cs);
200
201 LoadWindowsAPI();
202 }
203
204 virtual ~Context()
205 {
206 #if STK_TLS
207 if (m_tls != TLS_OUT_OF_INDEXES)
208 TlsFree(m_tls);
209 #endif
210
211 if (m_switch_event != nullptr)
212 {
213 CloseHandle(m_switch_event);
214 m_switch_event = nullptr;
215 }
216
217 UnloadWindowsAPI();
218 }
219
220 void LoadWindowsAPI()
221 {
222 HMODULE winmm = GetModuleHandleA("Winmm");
223 if (winmm == nullptr)
224 {
225 m_winmm_dll = winmm = LoadLibraryA("Winmm.dll");
226 }
227 STK_ASSERT(winmm != nullptr);
228
229 timeBeginPeriod = (timeBeginPeriodF)GetProcAddress(winmm, "timeBeginPeriod");
230 STK_ASSERT(timeBeginPeriod != nullptr);
231
232 timeBeginPeriod(1);
233 }
234
235 void UnloadWindowsAPI()
236 {
237 if (m_winmm_dll != nullptr)
238 {
239 FreeLibrary(m_winmm_dll);
240 m_winmm_dll = nullptr;
241 }
242 }
243
244 struct TaskContext
245 {
246 TaskContext() : m_task(nullptr), m_stack(nullptr), m_thread(nullptr), m_thread_id(0), m_exiting(false)
247 {}
248
249 void Initialize(ITask *task, Stack *stack)
250 {
251 m_task = task;
252 m_stack = stack;
253 m_thread = nullptr;
254 m_thread_id = 0;
255 m_exiting = false;
256
257 InitThread();
258 }
259
260 void InitThread()
261 {
262 // simulate stack size limitation
263 const size_t stack_size = m_task->GetStackSize() * sizeof(Word);
264
265 m_thread = CreateThread(nullptr, stack_size, &OnTaskRun, this, CREATE_SUSPENDED, &m_thread_id);
266 }
267
268 static DWORD WINAPI OnTaskRun(LPVOID param); // defined below, after Context is complete
269
270 ITask *m_task;
271 Stack *m_stack;
272 HANDLE m_thread;
273 DWORD m_thread_id;
274 volatile bool m_exiting;
275 };
276
277 void InitStack(EStackType stack_type, Stack *stack, IStackMemory *stack_memory, ITask *user_task);
278 void ConfigureTime();
279 void StartActiveTask();
280 void CreateTimerThreadAndJoin();
281 void Cleanup();
282 void ProcessTick();
283 void SwitchContext();
284 void ForceContextSwitch(TId id);
285 void OnForceContextSwitch();
286 void ReportTaskExit(TaskContext *task);
287 void Stop();
288 Word GetCallerSP() const;
289 TId GetTid() const;
290
291#if STK_TLS
292 __stk_forceinline Word GetTls()
293 {
294 return hw::PtrToWord(TlsGetValue(m_tls));
295 }
296
297 __stk_forceinline void SetTls(Word tp)
298 {
299 TlsSetValue(m_tls, hw::WordToPtr<void>(tp));
300 }
301#endif
302
303 __stk_forceinline void EnterCriticalSection()
304 {
305 STK_X86_WIN32_CRITICAL_SECTION_START(&m_cs);
306
307 if (m_csu_nesting == 0)
308 {
309 // avoid suspending self
310 if (GetCurrentThreadId() != m_timer_tid)
311 {
312 SuspendThread(m_timer_thread);
313 }
314 }
315
316 // increase nesting count within a limit
317 if (++m_csu_nesting > STK_CS_NESTINGS_MAX)
318 {
319 // invariant violated: exceeded max allowed number of recursions
320 STK_KERNEL_PANIC(KERNEL_PANIC_CS_NESTING_OVERFLOW);
321 }
322 }
323
324 __stk_forceinline void ExitCriticalSection()
325 {
326 STK_ASSERT(m_csu_nesting != 0);
327
328 --m_csu_nesting;
329
330 if (m_csu_nesting == 0)
331 {
332 // suspending self is not supported
333 if (GetCurrentThreadId() != m_timer_tid)
334 {
335 ResumeThread(m_timer_thread);
336 }
337 }
338
339 STK_X86_WIN32_CRITICAL_SECTION_END(&m_cs);
340 }
341
342 IPlatform::IEventOverrider *m_overrider;
343 Stack *m_sleep_trap;
344 Stack *m_exit_trap;
345 HMODULE m_winmm_dll;
346 HANDLE m_timer_thread;
347 HANDLE m_switch_event;
348 volatile TId m_pending_switch_id;
349 DWORD m_tls;
350 std::list<TaskContext *> m_tasks;
351 std::vector<HANDLE> m_task_threads;
352 DWORD m_timer_tid;
353#if STK_TICKLESS_IDLE
354 Timeout m_sleep_ticks;
355#endif
356 STK_X86_WIN32_CRITICAL_SECTION m_cs;
357 uint8_t m_csu_nesting;
358 bool m_started;
359 volatile bool m_stop_signal;
360}
361s_StkPlatformContext[1];
362
364static volatile EKernelPanicId g_LastPanicId = KERNEL_PANIC_NONE;
365
366__stk_attr_noinline // keep out of inlining to preserve stack frame
367__stk_attr_noreturn // never returns - a trap
369{
370 g_LastPanicId = id;
371
372 // spin forever: without a watchdog, a debugger can attach and inspect 'id'
373 for (;;)
374 {
375 __stk_relax_cpu();
376 }
377}
378
379static __stk_forceinline DWORD TicksToMs(uint64_t ticks)
380{
381 return static_cast<DWORD>((ticks * GetContext().m_tick_resolution) / 1000U);
382}
383
384static DWORD WINAPI TimerThread(LPVOID param)
385{
386 (void)param;
387
388 Context &ctx = GetContext();
389 DWORD wait_ms = TicksToMs(1U);
390 ctx.m_timer_tid = GetCurrentThreadId();
391
392 // wait_handles[0] is TimerThread's own handle: since it can only become
393 // signaled once this very thread exits, waiting on it can never actually
394 // be satisfied from inside itself - it's used purely so the call below
395 // behaves like a precise timed sleep (kept as in the original code).
396 // wait_handles[1] lets a task thread wake TimerThread early to request a
397 // forced context switch be serviced (see Context::ForceContextSwitch()).
398 HANDLE wait_handles[] = { ctx.m_timer_thread, ctx.m_switch_event };
399
400 for (;;)
401 {
402 DWORD result = WaitForMultipleObjects(STK_STATIC_ARRAY_SIZE(wait_handles), wait_handles, FALSE, wait_ms);
403
404 if (ctx.m_stop_signal)
405 {
406 break;
407 }
408
409 if (result == (WAIT_OBJECT_0 + 1))
410 {
411 // a task requested a forced context switch: SwitchContext() must
412 // never be invoked by the task thread that might itself need to
413 // be suspended, so it's serviced here instead.
414 ctx.OnForceContextSwitch();
415 continue;
416 }
417
418 STK_ASSERT(result == WAIT_TIMEOUT);
419
420 ctx.ProcessTick();
421
422 #if STK_TICKLESS_IDLE
423 wait_ms = TicksToMs(ctx.m_sleep_ticks);
424 #endif
425 }
426
427 return 0;
428}
429
430void Context::ConfigureTime()
431{
432 // Windows timers are jittery, so make resolution more coarse
433 if (m_tick_resolution < STK_X86_WIN32_MIN_RESOLUTION)
434 {
435 m_tick_resolution = STK_X86_WIN32_MIN_RESOLUTION;
436 }
437
438 // increase precision of ticks to at least 1 ms (although Windows timers will still be quite coarse and have jitter of +1 ms)
439 timeBeginPeriod(1);
440}
441
442void Context::StartActiveTask()
443{
444 STK_ASSERT(m_stack_active != nullptr);
445 TaskContext *active_task = hw::WordToPtr<TaskContext>(m_stack_active->SP);
446 STK_ASSERT(active_task != nullptr);
447
448 ResumeThread(active_task->m_thread);
449}
450
451void Context::CreateTimerThreadAndJoin()
452{
453 m_started = true;
454
455#if STK_TICKLESS_IDLE
456 m_sleep_ticks = 1;
457#endif
458
459 m_handler->OnStart(m_stack_active);
460
461 StartActiveTask();
462
463 // create tick thread with highest priority
464 m_timer_thread = CreateThread(nullptr, 0, &TimerThread, nullptr, 0, nullptr);
465 STK_ASSERT(m_timer_thread != nullptr);
466 SetThreadPriority(m_timer_thread, THREAD_PRIORITY_TIME_CRITICAL);
467
468 while (!m_task_threads.empty())
469 {
470 DWORD result = WaitForMultipleObjects((DWORD)m_task_threads.size(), m_task_threads.data(), FALSE, INFINITE);
471 STK_ASSERT(result != WAIT_TIMEOUT);
472 STK_ASSERT(result != WAIT_ABANDONED);
473 STK_ASSERT(result != WAIT_FAILED);
474
475 Win32ScopedCriticalSection __cs(m_cs);
476
477 uint32_t i = 0;
478 for (std::vector<HANDLE>::iterator itr = m_task_threads.begin(); itr != m_task_threads.end(); ++itr)
479 {
480 if (result == (WAIT_OBJECT_0 + i))
481 {
482 // note: the kernel is notified about the exit by the exiting task thread itself
483 // (see Context::ReportTaskExit), here we only drop the finished thread's handle
484 m_task_threads.erase(itr);
485 break;
486 }
487
488 ++i;
489 }
490 }
491
492 // join (never returns to the caller from here unless thread is terminated, see KERNEL_DYNAMIC),
493 // a stop signal is sent by IPlatform::Stop() by the last exiting task
494 if (m_timer_thread != nullptr)
495 {
496 WaitForSingleObject(m_timer_thread, INFINITE);
497 }
498}
499
500void Context::Cleanup()
501{
502 // close thread handles of all tasks
503 for (std::list<TaskContext *>::iterator itr = m_tasks.begin(); itr != m_tasks.end(); ++itr)
504 {
505 if ((*itr)->m_thread != nullptr)
506 {
507 CloseHandle((*itr)->m_thread);
508 (*itr)->m_thread = nullptr;
509 }
510 }
511 m_tasks.clear();
512
513 // close timer thread
514 if (m_timer_thread != nullptr)
515 {
516 CloseHandle(m_timer_thread);
517 m_timer_thread = nullptr;
518 }
519
520 // reset stop signal
521 m_stop_signal = false;
522
523 // notify kernel about a full stop
524 m_handler->OnStop();
525}
526
527void Context::ProcessTick()
528{
529 Win32ScopedCriticalSection __cs(m_cs);
530
531#if STK_TICKLESS_IDLE
532 Timeout ticks = m_sleep_ticks;
533#endif
534
535 if (m_handler->OnTick(m_stack_idle, m_stack_active
536 #if STK_TICKLESS_IDLE
537 , ticks
538 #endif
539 ))
540 {
541 SwitchContext();
542 }
543
544#if STK_TICKLESS_IDLE
545 m_sleep_ticks = ticks;
546#endif
547}
548
549void Context::SwitchContext()
550{
551 // suspend Idle thread
552 if ((m_stack_idle != m_sleep_trap) && (m_stack_idle != m_exit_trap))
553 {
554 TaskContext *idle_task = hw::WordToPtr<TaskContext>(m_stack_idle->SP);
555 STK_ASSERT(idle_task != nullptr);
556
557 // a thread that already reported its exit is only returning from its entry function:
558 // let it finish, suspending it would leave it hanging forever (see ReportTaskExit)
559 if (!idle_task->m_exiting)
560 {
561 SuspendThread(idle_task->m_thread);
562 }
563 }
564
565 // resume Active thread
566 if (m_stack_active == m_sleep_trap)
567 {
568 #if STK_TICKLESS_IDLE
569 const Timeout sleep_ticks = m_sleep_ticks;
570 #else
571 const Timeout sleep_ticks = 1;
572 #endif
573
574 if ((m_overrider == nullptr) || !m_overrider->OnSleep(sleep_ticks))
575 {
576 // pass
577 }
578 }
579 else
580 if (m_stack_active == m_exit_trap)
581 {
582 // pass
583 }
584 else
585 {
586 TaskContext *active_task = hw::WordToPtr<TaskContext>(m_stack_active->SP);
587 STK_ASSERT(active_task != nullptr);
588
589 ResumeThread(active_task->m_thread);
590 }
591}
592
593Word Context::GetCallerSP() const
594{
595 Word caller_sp = 0;
596 DWORD calling_tid = GetCurrentThreadId();
597
598 Win32ScopedCriticalSection __cs(const_cast<STK_X86_WIN32_CRITICAL_SECTION &>(m_cs));
599
600 for (std::list<TaskContext *>::const_iterator itr = m_tasks.begin(), end = m_tasks.end(); itr != end; ++itr)
601 {
602 if ((*itr)->m_thread_id == calling_tid)
603 {
604 caller_sp = hw::PtrToWord(STK_X86_WIN32_GET_SP((*itr)->m_task->GetStack()));
605 break;
606 }
607 }
608
609 // expect to find the calling task inside m_tasks
610 STK_ASSERT(caller_sp != 0);
611
612 return caller_sp;
613}
614
615TId Context::GetTid() const
616{
617 TId result;
618
619 if (m_started)
620 {
621 result = m_handler->OnGetTid(GetCallerSP());
622 }
623 else
624 {
625 result = TID_NONE;
626 }
627
628 return result;
629}
630
631void Context::ForceContextSwitch(TId id)
632{
633 if (GetCurrentThreadId() == m_timer_tid)
634 {
635 // running on TimerThread: execute forced context switch
636 if (m_handler->OnForceContextSwitch(id, m_stack_idle, m_stack_active))
637 {
638 SwitchContext();
639 }
640 }
641 else
642 {
643 // notify TimerThread to cause a context switch,
644 // note: the id must be published before the event is signaled (SetEvent() is a full barrier)
645 m_pending_switch_id = id;
646 __stk_full_memfence();
647 SetEvent(m_switch_event);
648 }
649}
650
651void Context::OnForceContextSwitch()
652{
653 Win32ScopedCriticalSection __cs(m_cs);
654
655 // read the requested id once (under m_cs) so that a concurrent request from another task
656 // cannot change it between the read and its use, a stale id is ignored by the kernel
657 const TId id = m_pending_switch_id;
658
659 if (m_handler->OnForceContextSwitch(id, m_stack_idle, m_stack_active))
660 {
661 SwitchContext();
662 }
663}
664
665void Context::ReportTaskExit(TaskContext *task)
666{
667 // note: must be called by the exiting task's own thread while it is still the scheduled task
668 // (m_task_now), as on other platforms. If the exit was reported later from another thread
669 // (after the thread had died), the kernel could have already switched to another task and
670 // would receive SLEEP_PENDING on a non-current task, which breaks the kernel's invariant
671 // that only the currently scheduled task can be pending to sleep.
672 const TId tid = stk::GetTidFromUserTask(task->m_task);
673
674 // hold m_cs (recursive) so that TimerThread cannot switch this thread out between setting
675 // m_exiting and the kernel being notified: a switch before that point suspends the thread
676 // normally (it is simply resumed later), a switch after it must not suspend the thread
677 // at all (see SwitchContext) because it is never resumed and its handle would never signal
678 {
679 Win32ScopedCriticalSection __cs(m_cs);
680
681 task->m_exiting = true;
682
683 // kernel enters the user critical section by itself
684 m_handler->OnTaskExit(task->m_stack);
685 }
686
687 // let TimerThread switch this task out now instead of waiting for the next tick,
688 // note: 'task' must not be touched past this point, its slot may be released by the kernel
689 ForceContextSwitch(tid);
690}
691
692DWORD WINAPI Context::TaskContext::OnTaskRun(LPVOID param)
693{
694 TaskContext *const self = static_cast<TaskContext *>(param);
695
696 self->m_task->Run();
697
698 GetContext().ReportTaskExit(self);
699
700 return 0; // thread terminates, SuspendThread() on a finished thread is harmless
701}
702
703void Context::Stop()
704{
705 m_stop_signal = true;
706 m_started = false;
707}
708
709void Context::InitStack(EStackType stack_type, Stack *stack, IStackMemory *stack_memory, ITask *user_task)
710{
711 InitStackMemory(stack_memory);
712
713 Word *const stack_mem = const_cast<Word *>(stack_memory->GetStack());
714 TaskContext *const ctx = reinterpret_cast<TaskContext *>(STK_X86_WIN32_GET_SP(stack_mem));
715
716 switch (stack_type)
717 {
718 case STACK_USER_TASK: {
719 ctx->Initialize(user_task, stack);
720
721 m_tasks.push_back(ctx);
722 m_task_threads.push_back(ctx->m_thread);
723 break; }
724
725 case STACK_SLEEP_TRAP: {
726 GetContext().m_sleep_trap = stack;
727 break; }
728
729 case STACK_EXIT_TRAP: {
730 GetContext().m_exit_trap = stack;
731 break; }
732
733 default: {
734 STK_ASSERT(false);
735 break; }
736 }
737
738 stack->SP = hw::PtrToWord(ctx);
739}
740
741void PlatformX86Win32::Initialize(IEventHandler *event_handler, IKernelService *service, uint32_t resolution_us,
742 Stack *exit_trap)
743{
744 GetContext().Initialize(event_handler, service, exit_trap, resolution_us);
745}
746
748{
749 GetContext().ConfigureTime();
750 GetContext().CreateTimerThreadAndJoin();
751 GetContext().Cleanup();
752}
753
755{
756 GetContext().Stop();
757}
758
760{
761 STK_ASSERT(false); // unsupported
762 return 0;
763}
764
765void PlatformX86Win32::Resume(Timeout elapsed_ticks)
766{
767 STK_ASSERT(false); // unsupported
768}
769
770void PlatformX86Win32::SetCpuFrequency(uint8_t core_id, uint32_t frequency)
771{
772 STK_ASSERT(false); // unsupported
773}
774
775void PlatformX86Win32::InitStack(EStackType stack_type, Stack *stack, IStackMemory *stack_memory, ITask *user_task)
776{
777 GetContext().InitStack(stack_type, stack, stack_memory, user_task);
778}
779
781{
782 return GetContext().m_tick_resolution;
783}
784
786{
787 return HiResClockQPC::GetInstance()->GetCycles();
788}
789
791{
792 return HiResClockQPC::GetInstance()->GetFrequency();
793}
794
796{
797 GetContext().ForceContextSwitch(id);
798}
799
801{
802 GetContext().ProcessTick();
803}
804
806{
807 if ((GetContext().m_overrider == nullptr) || !GetContext().m_overrider->OnHardFault())
808 {
810 }
811}
812
813void PlatformX86Win32::SetEventOverrider(IEventOverrider *overrider, bool non_secure)
814{
815 STK_UNUSED(non_secure);
816
817 STK_ASSERT(!GetContext().m_started);
818 GetContext().m_overrider = overrider;
819}
820
822{
823 return GetContext().GetCallerSP();
824}
825
827{
828 return GetContext().GetTid();
829}
830
831#if STK_TLS
832Word stk::hw::GetTls()
833{
834 return GetContext().GetTls();
835}
836
837void stk::hw::SetTls(Word tp)
838{
839 return GetContext().SetTls(tp);
840}
841#endif
842
844{
845 return GetContext().m_service;
846}
847
849{
850 STK_UNUSED(is_npriv);
851 GetContext().EnterCriticalSection();
852 return DEFAULT_SESSION;
853}
854
856{
857 STK_UNUSED(is_npriv);
858 GetContext().ExitCriticalSection();
859}
860
862{
863 HW_SpinLockLock(m_lock);
864}
865
867{
868 HW_SpinLockUnlock(m_lock);
869}
870
872{
873 return HW_SpinLockTryLock(m_lock);
874}
875
877{
878 return false;
879}
880
882{
883 return true;
884}
885
886stk::hw::HiResClock::HiResClock() : HiResClockState()
887{}
888
889// note: HiResClockQPC is immutable after construction (QPC frequency and origin), therefore
890// it is safely shared between all HiResClock instances
892{
893 return HiResClockQPC::GetInstance()->GetCycles();
894}
895
897{
898 return HiResClockQPC::GetInstance()->GetFrequency();
899}
900
901#endif // _STK_ARCH_X86_WIN32
Contains common inventory for platform implementation.
#define GetContext()
Get platform's context.
Hardware Abstraction Layer (HAL) declarations for the stk::hw namespace.
void STK_PANIC_HANDLER_DEFAULT(stk::EKernelPanicId id)
Default panic handler: disable interrupts, record the id, and spin in a tight loop - a defined,...
#define STK_UNUSED(X)
Explicitly marks a variable as unused to suppress compiler warnings.
Definition stk_defs.h:715
#define __stk_forceinline
Forces compiler to always inline the decorated function, regardless of optimisation level.
Definition stk_defs.h:270
#define STK_CS_NESTINGS_MAX
Maximum allowable recursion depth for critical section entry (default: 16).
Definition stk_defs.h:597
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
Definition stk_defs.h:532
#define __stk_attr_noinline
Prevents compiler from inlining the decorated function (function prefix).
Definition stk_defs.h:350
#define __stk_attr_noreturn
Declares that function never returns to its caller (function prefix).
Definition stk_defs.h:317
#define STK_STATIC_ARRAY_SIZE(ARRAY)
Get size of the static array.
Definition stk_defs.h:742
unsigned int UINT
Definition tx_api.h:124
int LONG
Definition tx_api.h:129
Namespace of STK package.
uintptr_t Word
Native processor word type.
Definition stk_common.h:206
void STK_KERNEL_PANIC(stk::EKernelPanicId id)
Called when the kernel detects an unrecoverable internal fault.
Definition stk_arch.h:184
TId GetTidFromUserTask(const ITask *task) noexcept
Get task identifier from ITask instance.
Definition stk_arch.h:775
EKernelPanicId
Identifies the source of a kernel panic.
Definition stk_common.h:60
@ KERNEL_PANIC_HRT_HARD_FAULT
Kernel running in KERNEL_HRT mode reported deadline failure of the task.
Definition stk_common.h:65
@ KERNEL_PANIC_NONE
Panic is absent (no fault).
Definition stk_common.h:61
@ KERNEL_PANIC_SPINLOCK_DEADLOCK
Spin-lock timeout expired: lock owner never released.
Definition stk_common.h:62
static bool Sleep(Timeout tick_count)
Put calling process into a sleep state.
Definition stk_helper.h:495
int32_t Timeout
Timeout time (ticks).
Definition stk_common.h:216
static constexpr TId TID_NONE
Reserved task/thread id representing zero/none thread id.
Definition stk_common.h:268
EStackType
Stack type.
Definition stk_common.h:96
@ STACK_SLEEP_TRAP
Stack of the Sleep trap.
Definition stk_common.h:98
@ STACK_USER_TASK
Stack of the user task.
Definition stk_common.h:97
@ STACK_EXIT_TRAP
Stack of the Exit trap.
Definition stk_common.h:99
uint64_t Cycles
Cycles value.
Definition stk_common.h:231
Word TId
Task (thread) id.
Definition stk_common.h:211
bool IsPrivilegedContext()
Check if caller context is Privileged.
static T * WordToPtr(Word value) noexcept
Cast a CPU register-width integer back to a pointer.
Definition stk_arch.h:232
static Word PtrToWord(T *const ptr) noexcept
Cast a pointer to a CPU register-width integer.
Definition stk_arch.h:215
bool IsInsideISR()
Check whether the CPU is currently executing inside a hardware interrupt service routine (ISR).
Base platform context for all platform implementations.
void Resume(Timeout elapsed_ticks) override
Resume scheduling after a prior Suspend() call.
void Stop() override
Stop scheduling.
void Initialize(IEventHandler *event_handler, IKernelService *service, uint32_t resolution_us, Stack *exit_trap) override
Initialize scheduler's context.
Word GetCallerSP() const override
Get caller's Stack Pointer (SP).
void Start() override
Start scheduling.
uint32_t GetSysTimerFrequency() const override
Get system timer frequency.
uint32_t GetTickResolution() const override
Get resolution of the system tick timer in microseconds. Resolution means a number of microseconds be...
void SetCpuFrequency(uint8_t core_id, uint32_t frequency) override
Notify the scheduler of a CPU core's operating frequency.
void InitStack(EStackType stack_type, Stack *stack, IStackMemory *stack_memory, ITask *user_task) override
Initialize stack memory of the user task.
void ProcessHardFault() override
Cause a hard fault of the system.
void ProcessTick() override
Process one tick.
Cycles GetSysTimerCount() const override
Get system timer count value.
void SetEventOverrider(IEventOverrider *overrider, bool non_secure) override
Set platform event overrider.
void ForceContextSwitch(TId id) override
Force context switch.
TId GetTid() const override
Get thread Id.
Timeout Suspend() override
Suspend scheduling.
static constexpr Session DEFAULT_SESSION
Default session value passed to Enter()/Exit() when the caller does not need to force a specific hand...
Definition stk_arch.h:479
static Session Enter(const Session ses=DEFAULT_SESSION)
Enter a critical section.
uint8_t Session
Opaque session token returned by Enter() and consumed by Exit().
Definition stk_arch.h:472
static void Exit(const Session ses=DEFAULT_SESSION)
Exit a critical section.
bool TryLock()
Attempt to acquire SpinLock in a single non-blocking attempt.
void Lock()
Acquire SpinLock, blocking until it is available.
void Unlock()
Release SpinLock, allowing another thread or core to acquire it.
HiResClock()
Construct a clock instance.
uint32_t GetFrequency() const
Get clock frequency.
Cycles GetCycles()
Get number of clock cycles elapsed since the time origin (see the class description).
Stack descriptor.
Definition stk_common.h:473
Word SP
Offset 0: Stack Pointer (SP) register (note: must always be at offset 0).
Definition stk_common.h:474
Interface for a stack memory region.
Definition stk_common.h:494
virtual const Word * GetStack() const =0
Get pointer to the stack memory.
Interface for a user task.
Definition stk_common.h:886
Interface for the kernel services exposed to the user processes during run-time when Kernel started s...
static IKernelService * GetInstance()
Get CPU-local instance of the kernel service.
void clock(void *argument)