Skip to content

Qwen3-0.6B inference (12 KB), bare-metal bootable OS (22 KB), and from-scratch Mini-GPT trainer with live Braille TUI (32 KB) in 100% pure x86-64 NASM assembly. Zero C, zero libc, zero dependencies.

3.0 KB
Raw Blame
#pragma once
#ifndef __VMLINUX_H__
#include <linux/types.h>
#endif

struct jev_model_weights {
    __u32 magic, model_gen;
    __u8 shift1, shift2, thresh_deny, max_uncertainty, emb_shift, reserved[3];
    __s8 emb[1024][16], W1[64][64];
    __s32 b1[64];
    __s8 W2[4][64];
    __s32 b2[4];
    __u8 sigmoid_lut[256];
} __attribute__((aligned(8)));
_Static_assert(sizeof(struct jev_model_weights) == 21280, "weight ABI");

struct jev_task_state {
    __s8 h[32];
    __u32 model_gen, protected_open_count, exec_count_since_protected;
    __u64 last_protected_ns, fork_hook_ns;
    __u8 fork_depth, inherited_taint, cgroup_monitored, binary_class;
};
struct jev_cgroup_state {
    __s8 h[32];
    __u32 model_gen, protected_open_count;
    __u64 last_protected_ns;
};
struct jev_ring_event {
    __u64 hook_ns;
    __u32 pid, ppid, model_gen;
    char comm[16], name[65];
    __u32 opens, execs;
    __u16 port;
    __u8 depth;
    __s8 h[32], x[32];
    __u8 event_type, scores[4], action, inherited_taint;
};
enum { ALLOW, DENY, ABSTAIN_DENY };
struct jev_oblivious_gbdt {
    __u8 feature[32][4];
    __s8 threshold[32][4];
    __s16 leaves[32][16];
    __u32 ntrees;
};
struct jev_tree_heads { __s16 leaves[32][16][3]; };
struct object_key { __u64 dev, ino; };
struct dst_key { __u16 family, port; __u8 addr[16]; };
struct jev_scratch {
    __s8 in[64], hidden[64];
    char name[65];
    __s32 sums[16];
    __u8 scores[4];
};
struct jev_math {
    struct jev_scratch *s;
    struct jev_model_weights *m;
};

static __inline __attribute__((always_inline)) int clamp_byte(int x) {
    return x < -128 ? -128 : x > 127 ? 127 : x;
}
// Unsigned accumulation defines two's-complement wrap even for extreme biases.
static __inline __attribute__((always_inline)) long jev_hidden(__u32 row, void *opaque) {
    struct jev_math *ctx = opaque;
    if (row >= 64) return 1;
    __u32 acc = ctx->m->b1[row];
#pragma clang loop unroll(full)
    for (int i = 0; i < 64; i++)
        acc += ctx->s->in[i] * ctx->m->W1[row][i];
    int a = (__s32)acc;
    ctx->s->hidden[row] = a > 0 ? clamp_byte(a >> (ctx->m->shift1 & 31)) : 0;
    return 0;
}
static __inline __attribute__((always_inline)) long jev_head(__u32 row, void *opaque) {
    struct jev_math *ctx = opaque;
    if (row >= 4) return 1;
    __u32 acc = ctx->m->b2[row];
#pragma clang loop unroll(full)
    for (int i = 0; i < 64; i++)
        acc += ctx->s->hidden[i] * ctx->m->W2[row][i];
    int logit = clamp_byte((__s32)acc >> (ctx->m->shift2 & 31));
    ctx->s->scores[row] = ctx->m->sigmoid_lut[(logit + 128) & 255];
    return 0;
}
static __inline __attribute__((always_inline)) long jev_gram(__u32 i, void *opaque) {
    struct jev_math *ctx = opaque;
    if (i >= 62) return 1;
    unsigned char *p = (void *)&ctx->s->name[i];
    if (!p[0] || !p[1] || !p[2]) return 1;
    __u32 h = 2166136261u;
    for (int j = 0; j < 3; j++) h = (h ^ p[j]) * 16777619u;
#pragma clang loop unroll(full)
    for (int j = 0; j < 16; j++) ctx->s->sums[j] += ctx->m->emb[h & 1023][j];
    return 0;
}