universalisos/microkernel/include/uos_api.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

310 lines
9.7 KiB
C

/*
* UniversalisOS Microkernel — Universal Kernel API
*
* The ONE API. Every function, every type, every constant uses uos_* prefix.
* Personality shells (FreeRTOS, ThreadX, Zephyr, POSIX) wrap these.
*/
#ifndef UOS_API_H
#define UOS_API_H
#include "uos_types.h"
#include "uos_config.h"
#include "uos_compiler.h"
#include "uos_object.h"
/* ========================================================================
* Task Management
* ======================================================================== */
/* Task states */
#define UOS_TASK_DELETED 0
#define UOS_TASK_READY 1
#define UOS_TASK_RUNNING 2
#define UOS_TASK_BLOCKED 3
#define UOS_TASK_SUSPENDED 4
/* Maximum tasks (compile-time) */
#ifndef UOS_MAX_TASKS
#define UOS_MAX_TASKS 8
#endif
/* Task stack alignment */
#ifndef UOS_STACK_ALIGN
#define UOS_STACK_ALIGN 8
#endif
/* Forward declaration */
typedef struct uos_task uos_task_t;
/* Task entry function */
typedef void (*uos_task_entry_t)(void* arg);
/* Task structure (Tier 0: all static) */
struct uos_task {
uos_object_t obj; /* Must be first */
uos_prio_t priority;
uos_prio_t orig_priority; /* HRT: for PI restore across multiple mutexes */
volatile uint8_t state;
void* stack_ptr; /* Current stack pointer (saved by context switch) */
uos_size_t stack_size;
void* stack_base; /* Bottom of stack (for overflow detection) */
uos_task_entry_t entry;
void* arg;
/* Wait state — what the task is blocked on */
void* wait_obj; /* Semaphore, mutex, queue, etc. */
uos_status_t wait_result;
/* Linked list pointers for ready/blocked queues */
uos_task_t* next;
uos_task_t* prev;
/* Tick at which delay expires (0 = not delayed) */
uos_tick_t delay_until;
/* HRT: Execution budget monitoring (WCET enforcement) */
uos_tick_t exec_budget; /* Max ticks per activation (0 = unlimited) */
uos_tick_t exec_consumed; /* Ticks consumed since last activation */
/* HRT: Inter-arrival time monitoring */
uos_tick_t min_interarrival; /* Min ticks between activations (0 = unchecked) */
uos_tick_t last_activation; /* Tick of last activation */
#if UOS_TIER >= 1
/* MPU region index for this task's stack */
uint8_t mpu_region;
#endif
#if UOS_TIER >= 2
/* Page table for this task's address space */
void* page_table;
#endif
};
/*
* Create a task.
*
* @param name Human-readable name (for debug)
* @param priority Priority (0 = highest, UOS_PRIO_LOWEST = lowest)
* @param entry Task entry function
* @param arg Argument passed to entry
* @param stack Pointer to stack memory (caller provides)
* @param stack_size Size of stack in bytes
* @return Pointer to task, or NULL on failure
*/
uos_task_t* uos_task_create(const char* name, uos_prio_t priority,
uos_task_entry_t entry, void* arg,
void* stack, uos_size_t stack_size);
/*
* Delete a task. Removes it from all queues.
*/
uos_status_t uos_task_delete(uos_task_t* task);
/*
* Yield the current task. Triggers a context switch to the next
* ready task of equal or higher priority.
*/
uos_status_t uos_task_yield(void);
/*
* Suspend a task. It will not run until resumed.
*/
uos_status_t uos_task_suspend(uos_task_t* task);
/*
* Resume a suspended task.
*/
uos_status_t uos_task_resume(uos_task_t* task);
/*
* Get the currently running task.
*/
uos_task_t* uos_task_self(void);
/*
* Get/set task priority.
*/
uos_prio_t uos_task_get_priority(uos_task_t* task);
uos_status_t uos_task_set_priority(uos_task_t* task, uos_prio_t prio);
/* HRT: Stack canary functions — overflow detection and high-water mark */
uint32_t uos_task_stack_used(const uos_task_t* task);
bool uos_task_stack_overflow(const uos_task_t* task);
/* ========================================================================
* Scheduling
* ======================================================================== */
/*
* Start the scheduler. Never returns.
* Must be called after creating at least one task.
*/
uos_status_t uos_sched_start(void) UOS_NORETURN;
/*
* Get the current tick count.
*/
uos_tick_t uos_tick_get(void);
/* Tick handler — called from timer ISR */
void uos_tick_handler(void);
/*
* Delay the current task for a number of ticks.
*/
uos_status_t uos_tick_delay(uos_tick_t ticks);
/*
* Convert milliseconds to ticks.
*/
uos_tick_t uos_ms_to_ticks(uint32_t ms);
/* ========================================================================
* Semaphores
* ======================================================================== */
typedef struct uos_sem {
uos_object_t obj; /* Must be first */
volatile uos_count_t count;
uos_count_t max_count;
uos_task_t* wait_head; /* Head of waiting task list */
} uos_sem_t;
uos_status_t uos_sem_init(uos_sem_t* sem, const char* name, uos_count_t count);
uos_status_t uos_sem_destroy(uos_sem_t* sem);
uos_status_t uos_sem_wait(uos_sem_t* sem, uos_tick_t timeout);
uos_status_t uos_sem_post(uos_sem_t* sem);
uos_status_t uos_sem_post_from_isr(uos_sem_t* sem);
/* ========================================================================
* Mutexes (Tier 1+ only — needs priority inheritance)
* ======================================================================== */
#if 1 /* Mutex available at Tier 0 */
typedef struct uos_mutex {
uos_object_t obj;
uos_task_t* owner;
uos_count_t lock_count; /* For recursive mutex */
bool recursive;
uos_task_t* wait_head;
uos_prio_t orig_prio; /* HRT: owner's original priority for PI restore */
} uos_mutex_t;
uos_status_t uos_mutex_init(uos_mutex_t* mutex, const char* name, bool recursive);
uos_status_t uos_mutex_destroy(uos_mutex_t* mutex);
uos_status_t uos_mutex_lock(uos_mutex_t* mutex, uos_tick_t timeout);
uos_status_t uos_mutex_unlock(uos_mutex_t* mutex);
#endif /* UOS_TIER >= 1 */
/* ========================================================================
* Message Queues (Tier 1+ — needs dynamic memory)
* ======================================================================== */
#if UOS_TIER >= 1
typedef struct uos_queue {
uos_object_t obj;
void* buffer;
uos_size_t msg_size;
uos_count_t max_msgs;
volatile uos_count_t count;
uos_count_t head;
uos_count_t tail;
uos_task_t* send_waiters;
uos_task_t* recv_waiters;
} uos_queue_t;
uos_queue_t* uos_queue_create(const char* name, uos_size_t msg_size, uos_count_t max_msgs);
uos_status_t uos_queue_delete(uos_queue_t* queue);
uos_status_t uos_queue_send(uos_queue_t* queue, const void* msg, uos_tick_t timeout);
uos_status_t uos_queue_receive(uos_queue_t* queue, void* msg, uos_tick_t timeout);
uos_status_t uos_queue_send_from_isr(uos_queue_t* queue, const void* msg);
#endif /* UOS_TIER >= 1 */
/* ========================================================================
* Event Flags
* ======================================================================== */
typedef struct uos_event {
uos_object_t obj;
volatile uos_flags_t flags;
uos_task_t* wait_head;
} uos_event_t;
uos_status_t uos_event_init(uos_event_t* event, const char* name);
uos_status_t uos_event_destroy(uos_event_t* event);
uos_status_t uos_event_set(uos_event_t* event, uos_flags_t flags);
uos_status_t uos_event_clear(uos_event_t* event, uos_flags_t flags);
uos_status_t uos_event_wait(uos_event_t* event, uos_flags_t flags,
uos_flags_t* actual, uos_tick_t timeout);
/* ======================================================================== */
/* Software Timers */
/* ======================================================================== */
typedef struct uos_timer {
uos_object_t obj;
uos_tick_t period;
uos_tick_t next_fire;
bool periodic;
bool active;
void (*callback)(void*);
void* cb_arg;
struct uos_timer* next;
} uos_timer_t;
uos_timer_t* uos_timer_create(const char* name, uos_tick_t period,
void (*callback)(void*), void* arg, bool periodic);
uos_status_t uos_timer_start(uos_timer_t* timer);
uos_status_t uos_timer_stop(uos_timer_t* timer);
uos_status_t uos_timer_delete(uos_timer_t* timer);
/* ========================================================================
* Interrupt Management
* ======================================================================== */
typedef void (*uos_isr_t)(void* arg);
typedef struct {
uos_isr_t handler;
void* arg;
} uos_irq_entry_t;
uos_status_t uos_irq_attach(uint32_t irq, uos_isr_t handler, void* arg);
uos_status_t uos_irq_enable(uint32_t irq);
uos_status_t uos_irq_disable(uint32_t irq);
/* ========================================================================
* Kernel Initialization
* ======================================================================== */
/*
* Initialize the kernel. Must be called before any other uos_* function.
* Sets up scheduler, tick timer, idle task.
*/
void uos_init(void);
/*
* Static task declaration macro (Tier 0 — no dynamic allocation).
* Declares a task and its stack as static variables.
*/
#define UOS_TASK_DEF(name, prio, stack_size) \
static uint8_t name##_stack_mem[UOS_ALIGN(stack_size, UOS_STACK_ALIGN)] \
UOS_ALIGNED(UOS_STACK_ALIGN); \
static uos_task_t name##_task
/*
* Static semaphore declaration macro.
*/
#define UOS_SEM_DEF(name) \
static uos_sem_t name##_sem
#endif /* UOS_API_H */