physics-mv
FORGE: request-bound machine realization, providers and verification for AienOS. Host, simulated and GB10 hardware paths have separate evidence.
Brought by drakestapleton
physics-mv / m16 / m16_gpu_wait.c
| 1 | #include "m16_gpu_wait.h" |
| 2 | |
| 3 | #include <string.h> |
| 4 | #include <time.h> |
| 5 | |
| 6 | static uint64_t real_now_ns(void *ctx) { |
| 7 | (void)ctx; |
| 8 | struct timespec ts; |
| 9 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 10 | return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec; |
| 11 | } |
| 12 | |
| 13 | static void real_barrier(void *ctx) { |
| 14 | (void)ctx; |
| 15 | #if defined(__aarch64__) |
| 16 | __asm__ volatile("dsb sy" ::: "memory"); |
| 17 | #else |
| 18 | __atomic_thread_fence(__ATOMIC_SEQ_CST); |
| 19 | #endif |
| 20 | } |
| 21 | |
| 22 | /* Compile-flag mutants of the real code, used only by `make mutants-m16-gpu-wait`. |
| 23 | * Each one must be killed by tests/test_m16_gpu_wait.c. Never defined in a real build. */ |
| 24 | #ifdef M16_GPU_WAIT_MUT_NO_BARRIER |
| 25 | #define WAIT_BARRIER(fn, ctx) ((void)(fn), (void)(ctx)) |
| 26 | #else |
| 27 | #define WAIT_BARRIER(fn, ctx) (fn)(ctx) |
| 28 | #endif |
| 29 | |
| 30 | static void real_relax(void *ctx) { |
| 31 | (void)ctx; |
| 32 | #if defined(__aarch64__) |
| 33 | __asm__ volatile("yield"); |
| 34 | #else |
| 35 | __asm__ volatile("" ::: "memory"); |
| 36 | #endif |
| 37 | } |
| 38 | |
| 39 | const char *m16_gpu_wait_result_name(m16_gpu_wait_result_t r) { |
| 40 | switch (r) { |
| 41 | case M16_GPU_WAIT_PASS: return "PASS"; |
| 42 | case M16_GPU_WAIT_TIMEOUT: return "TIMEOUT"; |
| 43 | case M16_GPU_WAIT_STALLED: return "STALLED"; |
| 44 | case M16_GPU_WAIT_INVALID_STATE: return "INVALID_STATE"; |
| 45 | case M16_GPU_WAIT_ABORTED: return "ABORTED"; |
| 46 | default: return "UNKNOWN"; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | const char *m16_gpu_wait_kind_name(m16_gpu_wait_kind_t k) { |
| 51 | return k == M16_GPU_WAIT_FIXED ? "fixed" : k == M16_GPU_WAIT_SEQUENCE ? "sequence" : "unknown"; |
| 52 | } |
| 53 | |
| 54 | static bool wait_core(m16_gpu_wait_kind_t kind, volatile uint32_t *word, uint32_t expected, |
| 55 | m16_gpu_wait_report_t *rpt, const m16_gpu_wait_cfg_t *cfg) { |
| 56 | m16_gpu_wait_report_t local; |
| 57 | m16_gpu_wait_cfg_t zero; |
| 58 | if (!rpt) rpt = &local; |
| 59 | memset(rpt, 0, sizeof(*rpt)); |
| 60 | rpt->wait_kind = kind; |
| 61 | rpt->expected = expected; |
| 62 | if (!cfg) { |
| 63 | memset(&zero, 0, sizeof(zero)); |
| 64 | cfg = &zero; |
| 65 | } |
| 66 | |
| 67 | uint64_t (*now_ns)(void *) = cfg->now_ns ? cfg->now_ns : real_now_ns; |
| 68 | void (*barrier)(void *) = cfg->barrier ? cfg->barrier : real_barrier; |
| 69 | void (*relax)(void *) = cfg->relax ? cfg->relax : real_relax; |
| 70 | uint64_t total_ns = 1000000ull * (cfg->total_timeout_ms ? cfg->total_timeout_ms : M16_GPU_WAIT_DEFAULT_TOTAL_MS); |
| 71 | uint64_t stall_ns = 1000000ull * (cfg->progress_timeout_ms ? cfg->progress_timeout_ms : M16_GPU_WAIT_DEFAULT_PROGRESS_MS); |
| 72 | |
| 73 | uint64_t start = now_ns(cfg->hook_ctx); |
| 74 | uint64_t hard_deadline = start + total_ns; |
| 75 | uint64_t stall_deadline = start + stall_ns; |
| 76 | rpt->start_ns = start; |
| 77 | rpt->last_progress_ns = start; |
| 78 | uint64_t now = start; |
| 79 | bool first = true; |
| 80 | |
| 81 | /* A null word is a caller bug; report it as an invalid state, not a hang. */ |
| 82 | if (!word) { |
| 83 | rpt->result = M16_GPU_WAIT_INVALID_STATE; |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | while (now < hard_deadline) { |
| 88 | uint32_t m, m2 = 0, snap[2] = {0, 0}; |
| 89 | if (cfg->abort && cfg->abort(cfg->hook_ctx)) { |
| 90 | rpt->result = M16_GPU_WAIT_ABORTED; |
| 91 | rpt->elapsed_ns = now - start; |
| 92 | return false; |
| 93 | } |
| 94 | WAIT_BARRIER(barrier, cfg->hook_ctx); |
| 95 | m = *word; |
| 96 | if (cfg->marker2) m2 = *cfg->marker2; |
| 97 | if (cfg->snapshot) cfg->snapshot(cfg->snapshot_ctx, snap); |
| 98 | rpt->polls++; |
| 99 | now = now_ns(cfg->hook_ctx); |
| 100 | |
| 101 | bool changed = m != rpt->last_observed || m2 != rpt->last_marker2 || |
| 102 | snap[0] != rpt->snap[0] || snap[1] != rpt->snap[1]; |
| 103 | rpt->last_observed = m; |
| 104 | rpt->last_marker2 = m2; |
| 105 | rpt->snap[0] = snap[0]; |
| 106 | rpt->snap[1] = snap[1]; |
| 107 | rpt->elapsed_ns = now - start; |
| 108 | |
| 109 | if (changed && !first) { |
| 110 | rpt->progress_count++; |
| 111 | rpt->last_progress_ns = now; |
| 112 | #ifndef M16_GPU_WAIT_MUT_STALL_NEVER_RESETS |
| 113 | stall_deadline = now + stall_ns; |
| 114 | #endif |
| 115 | #ifdef M16_GPU_WAIT_MUT_PROGRESS_EXTENDS_HARD |
| 116 | hard_deadline = now + total_ns; |
| 117 | #endif |
| 118 | } |
| 119 | bool ok = kind == M16_GPU_WAIT_FIXED ? (m == expected) : ((int32_t)(m - expected) >= 0); |
| 120 | if (ok && (!cfg->marker2 || m2 == cfg->marker2_want)) { |
| 121 | WAIT_BARRIER(barrier, cfg->hook_ctx); /* acquire: later CPU loads are ordered after the marker load */ |
| 122 | rpt->result = M16_GPU_WAIT_PASS; |
| 123 | return true; |
| 124 | } |
| 125 | if (cfg->valid && !cfg->valid(cfg->hook_ctx, m)) { |
| 126 | rpt->result = M16_GPU_WAIT_INVALID_STATE; |
| 127 | return false; |
| 128 | } |
| 129 | first = false; |
| 130 | if (now >= stall_deadline) { |
| 131 | rpt->result = M16_GPU_WAIT_STALLED; |
| 132 | return false; |
| 133 | } |
| 134 | relax(cfg->hook_ctx); |
| 135 | now = now_ns(cfg->hook_ctx); |
| 136 | } |
| 137 | rpt->elapsed_ns = now - start; |
| 138 | rpt->result = M16_GPU_WAIT_TIMEOUT; |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | bool m16_gpu_wait_fixed(volatile uint32_t *word, uint32_t expected, |
| 143 | m16_gpu_wait_report_t *rpt, const m16_gpu_wait_cfg_t *cfg) { |
| 144 | return wait_core(M16_GPU_WAIT_FIXED, word, expected, rpt, cfg); |
| 145 | } |
| 146 | |
| 147 | bool m16_gpu_wait_sequence(volatile uint32_t *word, uint32_t target, |
| 148 | m16_gpu_wait_report_t *rpt, const m16_gpu_wait_cfg_t *cfg) { |
| 149 | return wait_core(M16_GPU_WAIT_SEQUENCE, word, target, rpt, cfg); |
| 150 | } |
| 151 | |
| 152 | const char *m16_gpu_wait_failure_class(const m16_gpu_wait_report_t *r, uint32_t m2_want) { |
| 153 | if (r->result == M16_GPU_WAIT_INVALID_STATE) return "wrong_marker"; |
| 154 | if (r->wait_kind == M16_GPU_WAIT_FIXED && r->last_observed != 0 && r->last_observed != r->expected) |
| 155 | return "wrong_marker"; |
| 156 | if (r->last_marker2 != 0 && r->last_marker2 != m2_want) return "wrong_marker"; |
| 157 | if (r->progress_count > 0 || r->last_observed == r->expected || |
| 158 | (m2_want != 0 && r->last_marker2 == m2_want)) |
| 159 | return "late_progress"; |
| 160 | return "no_progress"; |
| 161 | } |