runtime.c @ 01ba812a486a default tip

More binary primitives
author Steve Losh <steve@stevelosh.com>
date Wed, 10 Mar 2021 22:01:41 -0500
parents 37b7eecfdf6e
children (none)
#include <stdio.h>
#include <stdint.h>
#include <sys/mman.h>
#include <unistd.h>

// Bools
#define bool_f 0x1F
#define bool_t 0x3F

// Fixnums
#define fx_mask  0x03
#define fx_tag   0x00
#define fx_shift 2

// Characters
#define ch_mask  0xFF
#define ch_tag   0x0F
#define ch_shift 8

// Nil
#define nil 0x2F

typedef uint64_t ptr;

ptr scheme_entry(char*);

static void print_ptr(ptr x) {
    if ((x & fx_mask) == fx_tag) {
        printf("%ld", ((int64_t)x >> fx_shift));
    } else if ((x & ch_mask) == ch_tag) {
        printf("#\\%c", ((int)x >> ch_shift));
    } else if (x == bool_f) {
        printf("#f");
    } else if (x == bool_t) {
        printf("#t");
    } else if (x == nil) {
        printf("()");
    } else {
        printf("#<unknown #x%016lx>", x);
    }

    printf("\n");
}

static char* allocate_protected_space(int size) {
    int page = getpagesize();
    int status;
    int aligned_size = ((size + page - 1) / page) * page;

    char* p = mmap(0, aligned_size + 2 * page,
                   PROT_READ | PROT_WRITE,
                   MAP_ANONYMOUS | MAP_PRIVATE,
                   0, 0);
   if (p == MAP_FAILED) _exit(4);

   status = mprotect(p, page, PROT_NONE);
   if (status != 0) _exit(5);

   status = mprotect(p + page + aligned_size, page, PROT_NONE);
   if (status != 0) _exit(6);

   return (p + page);
}

static void deallocate_protected_space(char* p, int size) {
    int page = getpagesize();
    int status;
    int aligned_size = ((size + page - 1) / page) * page;

    status = munmap(p - page, aligned_size + 2 * page);
    if (status != 0) _exit(7);
}

int main(int argc, char** argv) {
    int stack_size = (16 * 4096);
    char* stack_top = allocate_protected_space(stack_size);
    char* stack_base = stack_top + stack_size;
    print_ptr(scheme_entry(stack_base));
    deallocate_protected_space(stack_top, stack_size);
    return 0;
}