luajitos

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

baremetal_compat.h (3934B)


      1 /*
      2  * baremetal_compat.h - Bare metal compatibility layer
      3  *
      4  * Define BARE_METAL to enable bare metal mode
      5  *
      6  * You must provide:
      7  * - baremetal_get_ticks() - Returns monotonic tick count (uint64_t)
      8  * - baremetal_get_cycles() - Returns CPU cycle count (uint64_t)
      9  * - Memory allocator (malloc/free) or define USE_CUSTOM_ALLOCATOR
     10  */
     11 
     12 #ifndef BAREMETAL_COMPAT_H
     13 #define BAREMETAL_COMPAT_H
     14 
     15 #include <stdint.h>
     16 #include <stddef.h>
     17 
     18 #ifdef BARE_METAL
     19 
     20 /* ============================================================================
     21  * Time Functions
     22  * ========================================================================= */
     23 
     24 /* User must implement these functions for their platform */
     25 extern uint64_t baremetal_get_ticks(void);    /* Monotonic tick counter */
     26 extern uint64_t baremetal_get_cycles(void);   /* CPU cycle counter (e.g., RDTSC) */
     27 extern uint64_t baremetal_get_cpu_temp(void); /* CPU temperature/thermal sensor */
     28 
     29 /* Bare metal time structure */
     30 struct baremetal_timespec {
     31     uint64_t tv_sec;
     32     uint64_t tv_nsec;
     33 };
     34 
     35 /* Clock types */
     36 #define CLOCK_MONOTONIC 0
     37 #define CLOCK_REALTIME 1
     38 
     39 /* Get time implementation for bare metal */
     40 static inline int clock_gettime(int clk_id, struct baremetal_timespec *ts) {
     41     if (!ts) return -1;
     42 
     43     if (clk_id == CLOCK_MONOTONIC) {
     44         /* Use monotonic tick counter */
     45         uint64_t ticks = baremetal_get_ticks();
     46         ts->tv_sec = ticks / 1000000000ULL;
     47         ts->tv_nsec = ticks % 1000000000ULL;
     48     } else {
     49         /* Use CPU cycle counter for additional entropy */
     50         uint64_t cycles = baremetal_get_cycles();
     51         ts->tv_sec = cycles / 1000000000ULL;
     52         ts->tv_nsec = cycles % 1000000000ULL;
     53     }
     54 
     55     return 0;
     56 }
     57 
     58 /* Simple time() replacement */
     59 static inline uint64_t time(void *unused) {
     60     (void)unused;
     61     return baremetal_get_ticks() / 1000000000ULL;
     62 }
     63 
     64 /* Define timespec to our bare metal version */
     65 #define timespec baremetal_timespec
     66 
     67 /* ============================================================================
     68  * I/O Functions (stubbed out)
     69  * ========================================================================= */
     70 
     71 /* Stub out I/O functions - they do nothing in bare metal */
     72 #define fprintf(stream, ...) ((void)0)
     73 #define printf(...) ((void)0)
     74 #define perror(s) ((void)0)
     75 
     76 /* ============================================================================
     77  * Standard Library
     78  * ========================================================================= */
     79 
     80 /* If using custom allocator, declare these */
     81 #ifdef USE_CUSTOM_ALLOCATOR
     82 extern void* baremetal_malloc(size_t size);
     83 extern void baremetal_free(void *ptr);
     84 extern void* baremetal_calloc(size_t nmemb, size_t size);
     85 extern void* baremetal_realloc(void *ptr, size_t size);
     86 
     87 #define malloc(s) baremetal_malloc(s)
     88 #define free(p) baremetal_free(p)
     89 #define calloc(n, s) baremetal_calloc(n, s)
     90 #define realloc(p, s) baremetal_realloc(p, s)
     91 #endif
     92 
     93 /* ============================================================================
     94  * Example Implementations
     95  * ========================================================================= */
     96 
     97 /*
     98  * Example for x86_64 bare metal:
     99  *
    100  * uint64_t baremetal_get_cycles(void) {
    101  *     uint32_t lo, hi;
    102  *     __asm__ __volatile__("rdtsc" : "=a"(lo), "=d"(hi));
    103  *     return ((uint64_t)hi << 32) | lo;
    104  * }
    105  *
    106  * uint64_t baremetal_get_ticks(void) {
    107  *     // Use PIT, HPET, or TSC with known frequency
    108  *     return baremetal_get_cycles();  // Simple fallback
    109  * }
    110  */
    111 
    112 /*
    113  * Example for ARM bare metal:
    114  *
    115  * uint64_t baremetal_get_cycles(void) {
    116  *     uint64_t val;
    117  *     __asm__ __volatile__("mrs %0, cntvct_el0" : "=r"(val));
    118  *     return val;
    119  * }
    120  *
    121  * uint64_t baremetal_get_ticks(void) {
    122  *     return baremetal_get_cycles();
    123  * }
    124  */
    125 
    126 #else /* !BARE_METAL */
    127 
    128 /* Standard hosted environment - use normal includes */
    129 #include <time.h>
    130 #include <stdio.h>
    131 
    132 #endif /* BARE_METAL */
    133 
    134 #endif /* BAREMETAL_COMPAT_H */