Loading...
Searching...
No Matches
thread_arch.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2026 HAW Hamburg
3 * SPDX-License-Identifier: LGPL-2.1-only
4 */
5
6#pragma once
7
15
16#include "irq.h"
17
18#include <stdio.h>
19
20#ifdef __cplusplus
21extern "C" {
22#endif
23
24#define THREAD_API_INLINED
25
26#ifndef DOXYGEN /* Doxygen is in core/include/thread.h */
27
28/*
29 * Thanks to Martin Korth for providing this info in his GBATEK.
30 * The GBA has a BIOS that implements syscall functions.
31 * Called by using `svc $num` (THUMB) or `svc $num*0x10000` (ARM)
32 * 00h SoftReset 10h BitUnPack 20h SoundWhatever0
33 * 01h RegisterRamReset 11h LZ77UnCompReadNormalWrite8bit 21h SoundWhatever1
34 * 02h Halt 12h LZ77UnCompReadNormalWrite16bit 22h SoundWhatever2
35 * 03h Stop/Sleep 13h HuffUnCompReadNormal 23h SoundWhatever3
36 * 04h IntrWait 14h RLUnCompReadNormalWrite8bit 24h SoundWhatever4
37 * 05h VBlankIntrWait 15h RLUnCompReadNormalWrite16bit 25h MultiBoot
38 * 06h Div 16h Diff8bitUnFilterWrite8bit 26h HardReset
39 * 07h DivArm 17h Diff8bitUnFilterWrite16bit 27h CustomHalt
40 * 08h Sqrt 18h Diff16bitUnFilter 28h SoundDriverVSyncOff
41 * 09h ArcTan 19h SoundBias 29h SoundDriverVSyncOn
42 * 0Ah ArcTan2 1Ah SoundDriverInit 2Ah SoundGetJumpList
43 * 0Bh CpuSet 1Bh SoundDriverMode 2Bh+ Crash / Undefined behavior
44 * 0Ch CpuFastSet 1Ch SoundDriverMain D4h Garbage, jumps to 0x2002040 (EWRAM)
45 * 0Dh GetBiosChecksum 1Dh SoundDriverVSync
46 * 0Eh BgAffineSet 1Eh SoundChannelClear
47 * 0Fh ObjAffineSet 1Fh MidiKey2Freq
48 *
49 * We are particularly interested in the invalid syscall `D4h`. The UB of the BIOS messes
50 * a bit with our regs and then jumps to 0x2002040 in EWRAM. This UB is predictable and
51 * deterministic. By placing our own code at that address during boot (see `setup_swi:`
52 * in startup.s) we can gain access over the flow of control during a SWI. From there
53 * its the normal RIOT SWI handling.
54 * This hack was found by the uClinux people back in 2004, thanks for documenting!
55 */
56static inline __attribute__((always_inline)) void thread_yield_higher(void)
57{
58 if (irq_is_in()) {
60 }
61 else {
62 /* The GBA cpu has a bug where SWIs don't cause it to disable IRQs.
63 * So we are doing that instead. */
64 unsigned irq_state = irq_disable();
65 /* Abusing an "illegal" BIOS syscall */
66 __asm__("svc 0xd40000\n");
67 /* restore irq */
68 irq_restore(irq_state);
69 }
70}
71
72#endif /* DOXYGEN */
73
74#ifdef __cplusplus
75}
76#endif
77
MAYBE_INLINE void irq_restore(unsigned state)
This function restores the IRQ disable bit in the status register to the value contained within passe...
MAYBE_INLINE unsigned irq_disable(void)
This function sets the IRQ disable bit in the status register.
MAYBE_INLINE bool irq_is_in(void)
Check whether called from interrupt service routine.
volatile unsigned int sched_context_switch_request
Flag indicating whether a context switch is necessary after handling an interrupt.
THREAD_MAYBE_INLINE void thread_yield_higher(void)
Lets current thread yield in favor of a higher prioritized thread.
IRQ driver interface.