universalisos/microkernel/include/uos_compiler.h
Fábio Coutada 62e8454f91 build: add UEFI ISO builder, test boot scripts, EIM config
- make-uefi-iso.sh: UEFI ISO image builder
- test_boot.S, test_boot.ld: test boot assembly and linker script
- eim_config.toml: EIM (External Interface Module) configuration
- edk2-ovmf RPM for UEFI firmware
- GAP_ANALYSIS_PIKEOS_PARITY.md: PikeOS parity gap analysis
2026-07-15 15:33:07 +01:00

136 lines
4.8 KiB
C

/*
* UniversalisOS Microkernel — Compiler Abstraction
*
* Hides GCC/Clang/IAR/MSVC differences behind uos_* macros.
*/
#ifndef UOS_COMPILER_H
#define UOS_COMPILER_H
/* === Alignment === */
#define UOS_ALIGN(x, a) (((x) + ((a) - 1)) & ~((a) - 1))
#define UOS_ALIGN_DOWN(x, a) ((x) & ~((a) - 1))
#define UOS_IS_ALIGNED(x, a) (((x) & ((a) - 1)) == 0)
/* === Section attributes === */
#if defined(__GNUC__) || defined(__clang__)
#define UOS_SECTION(s) __attribute__((section(s)))
#define UOS_ALIGNED(a) __attribute__((aligned(a)))
#define UOS_UNUSED __attribute__((unused))
#define UOS_NORETURN __attribute__((noreturn))
#define UOS_WEAK __attribute__((weak))
#define UOS_PACKED __attribute__((packed))
#define UOS_INLINE static inline __attribute__((always_inline))
#define UOS_NOINLINE __attribute__((noinline))
#define UOS_LIKELY(x) __builtin_expect(!!(x), 1)
#define UOS_UNLIKELY(x) __builtin_expect(!!(x), 0)
#elif defined(__ICCARM__) /* IAR */
#define UOS_SECTION(s) @ s
#define UOS_ALIGNED(a) _Pragma(#a)
#define UOS_UNUSED
#define UOS_NORETURN __noreturn
#define UOS_WEAK __weak
#define UOS_PACKED __packed
#define UOS_INLINE static inline
#define UOS_NOINLINE
#define UOS_LIKELY(x) (x)
#define UOS_UNLIKELY(x) (x)
#else
#error "Unsupported compiler — add compiler abstraction macros"
#endif
/* === Interrupt control (ARM) === */
#if defined(__arm__) || defined(__aarch64__)
#define UOS_DISABLE_IRQ() do { __asm__ volatile("cpsid i" ::: "memory"); } while(0)
#define UOS_ENABLE_IRQ() do { __asm__ volatile("cpsie i" ::: "memory"); } while(0)
#define UOS_DSB() do { __asm__ volatile("dsb" ::: "memory"); } while(0)
#define UOS_ISB() do { __asm__ volatile("isb" ::: "memory"); } while(0)
#define UOS_WFI() do { __asm__ volatile("wfi"); } while(0)
#define UOS_WFE() do { __asm__ volatile("wfe"); } while(0)
#elif defined(__riscv)
#define UOS_DISABLE_IRQ() do { __asm__ volatile("csrci mstatus, 0x8" ::: "memory"); } while(0)
#define UOS_ENABLE_IRQ() do { __asm__ volatile("csrsi mstatus, 0x8" ::: "memory"); } while(0)
#define UOS_DSB()
#define UOS_ISB()
#define UOS_WFI() do { __asm__ volatile("wfi"); } while(0)
#define UOS_WFE()
#elif defined(__AVR__)
#include <avr/interrupt.h>
#define UOS_DISABLE_IRQ() cli()
#define UOS_ENABLE_IRQ() sei()
#define UOS_DSB()
#define UOS_ISB()
#define UOS_WFI() do { __asm__ volatile("sleep"); } while(0)
#define UOS_WFE()
#elif defined(__XTENSA__)
#define UOS_DISABLE_IRQ() do { __asm__ volatile("rsil a2, 15" ::: "memory"); } while(0)
#define UOS_ENABLE_IRQ() do { __asm__ volatile("rsil a2, 0" ::: "memory"); } while(0)
#define UOS_DSB()
#define UOS_ISB()
#define UOS_WFI() do { __asm__ volatile("waiti 0"); } while(0)
#define UOS_WFE()
#else
#define UOS_DISABLE_IRQ()
#define UOS_ENABLE_IRQ()
#define UOS_DSB()
#define UOS_ISB()
#define UOS_WFI()
#define UOS_WFE()
#endif
/* === Critical section (saved state) === */
typedef uint32_t uos_irq_state_t;
UOS_INLINE uos_irq_state_t uos_critical_enter(void) {
uos_irq_state_t state;
#if defined(__arm__) || defined(__aarch64__)
__asm__ volatile("mrs %0, primask\ncpsid i" : "=r"(state) :: "memory");
#elif defined(__riscv)
__asm__ volatile("csrrci %0, mstatus, 0x8" : "=r"(state) :: "memory");
#elif defined(__AVR__)
state = SREG; cli();
#elif defined(__XTENSA__)
__asm__ volatile("rsil %0, 15" : "=r"(state) :: "memory");
#else
state = 0;
#endif
return state;
}
UOS_INLINE void uos_critical_exit(uos_irq_state_t state) {
#if defined(__arm__) || defined(__aarch64__)
__asm__ volatile("msr primask, %0" :: "r"(state) : "memory");
#elif defined(__riscv)
__asm__ volatile("csrw mstatus, %0" :: "r"(state) : "memory");
#elif defined(__AVR__)
SREG = state;
#elif defined(__XTENSA__)
__asm__ volatile("wsr %0, ps" :: "r"(state) : "memory");
#else
(void)state;
#endif
}
/* === Bitmap operations (used by scheduler) === */
UOS_INLINE uint32_t uos_clz(uint32_t x) {
#if defined(__GNUC__) || defined(__clang__)
return __builtin_clz(x);
#else
uint32_t n = 0;
if (x == 0) return 32;
if (!(x & 0xFFFF0000)) { n += 16; x <<= 16; }
if (!(x & 0xFF000000)) { n += 8; x <<= 8; }
if (!(x & 0xF0000000)) { n += 4; x <<= 4; }
if (!(x & 0xC0000000)) { n += 2; x <<= 2; }
if (!(x & 0x80000000)) { n += 1; }
return n;
#endif
}
/* === Compile-time assertion === */
#define UOS_STATIC_ASSERT(cond, msg) _Static_assert(cond, msg)
/* === Container of macro (like Linux container_of) === */
#define UOS_CONTAINER_OF(ptr, type, member) \
((type *)((char *)(ptr) - offsetof(type, member)))
#endif /* UOS_COMPILER_H */