Skip to content

bindings/native/gaspardos_x64_syscall.h

Native syscall ABI and transport used by the C++ bindings. Preserve the bindings/native/ layout alongside bindings/cpp/.

Source files · Download gaspardos_x64_syscall.h

#ifndef GASPARDOS_X64_USER_SYSCALL_H
#define GASPARDOS_X64_USER_SYSCALL_H
#include <stdint.h>
#include <stddef.h>
#ifdef __cplusplus
#define GO64_ASSERT_JOIN_(a, b) a##b
#define GO64_ASSERT_JOIN(a, b)  GO64_ASSERT_JOIN_(a, b)
#define GO64_ABI_ASSERT(test, message) \
    typedef char GO64_ASSERT_JOIN(go64_abi_, __LINE__)[(test) ? 1 : -1]
#define GO64_NORETURN __attribute__((noreturn))
extern "C"
{
#else
#define GO64_ABI_ASSERT(test, message) _Static_assert(test, message)
#define GO64_NORETURN                  __attribute__((noreturn))
#endif
/* Optional startup record passed in RDI by DCL RUN; argv includes argv[0]
 * and a terminating NULL. Record and strings live above initial RSP. */
#define GO64_START_MAGIC UINT64_C(0x474f363441524753)

    typedef struct
    {
        uint64_t magic, argc;
        char**   argv;
    } GO64_StartInfo;
#if !defined(__x86_64__) || defined(_WIN32)
#error "This API requires x86-64 System V, not i686 or Windows x64 ABI"
#endif
    /* Native LP64: do not compile with -fshort-wchar or structure packing. */
    GO64_ABI_ASSERT(sizeof(long) == 8, "LP64 long required");

    typedef struct
    {
        uint64_t id, parent_id, bytes, reference_count, metadata_count;
        uint16_t name[128], name_length;
        uint8_t  is_directory;
    } GO64_FSInfo;

    typedef struct
    {
        uint16_t family, port;
        uint8_t  address[4];
    } GO64_SocketAddress;

    typedef struct
    {
        int32_t  x, y, width, height;
        uint32_t background;
        char     title[64];
    } GO64_WindowOptions;

    typedef struct
    {
        int32_t  x, y, width, height;
        uint32_t color;
        char     text[128];
    } GO64_Drawing;

    /* Stride is in pixels. One copied tile, at most 256x256 pixels.
       RGBX8888 = 0xXXBBGGRR; the kernel forces the high byte opaque. */
    typedef struct
    {
        int32_t         x, y;
        uint32_t        width, height, stride, format;
        const uint32_t* pixels;
    } GO64_Blit;

#define GO64_BLIT_RGBX8888 1u
#define GO64_BLIT_TILE     256u

    typedef struct
    {
        int32_t  type, x, y, width, height;
        uint32_t buttons, key, modifiers;
    } GO64_Event;

    GO64_ABI_ASSERT(sizeof(void*) == 8 && sizeof(size_t) == 8, "LP64 pointer/size ABI required");
    GO64_ABI_ASSERT(sizeof(wchar_t) == 4, "32-bit wchar_t required");
    GO64_ABI_ASSERT(sizeof(GO64_FSInfo) == 304, "FSInfo ABI");
    GO64_ABI_ASSERT(sizeof(GO64_SocketAddress) == 8, "socket ABI");
    GO64_ABI_ASSERT(sizeof(GO64_WindowOptions) == 84, "window ABI");
    GO64_ABI_ASSERT(sizeof(GO64_Drawing) == 148, "drawing ABI");
    GO64_ABI_ASSERT(sizeof(GO64_Blit) == 32 && offsetof(GO64_Blit, pixels) == 24, "blit ABI");
    GO64_ABI_ASSERT(sizeof(GO64_Event) == 32, "event ABI");

    enum
    {
        GO64_RDONLY   = 0,
        GO64_WRONLY   = 1,
        GO64_RDWR     = 2,
        GO64_CREAT    = 64,
        GO64_EXCL     = 128,
        GO64_TRUNC    = 512,
        GO64_APPEND   = 1024,
        GO64_AF_INET  = 2,
        GO64_STREAM   = 1,
        GO64_DGRAM    = 2,
        GO64_READY    = 100,
        GO64_ERROR    = 101,
        GO64_OVERFLOW = 102
    };

    /* RAX=number/result, RDI/RSI/RDX/R10/R8/R9=args; RCX/R11 destroyed. */
    static inline int64_t GO64_syscall6(
        uint64_t number, uint64_t a, uint64_t b, uint64_t c, uint64_t d, uint64_t e, uint64_t f)
    {
        register uint64_t r10 __asm__("r10") = d;
        register uint64_t r8 __asm__("r8")   = e;
        register uint64_t r9 __asm__("r9")   = f;
        __asm__ volatile("syscall"
                         : "+a"(number)
                         : "D"(a), "S"(b), "d"(c), "r"(r10), "r"(r8), "r"(r9)
                         : "rcx", "r11", "memory", "cc");
        return (int64_t) number;
    }

#define GO64_SYS_TICKS 0
    int64_t GO64_ticks(void);
    int64_t GO64_asm_ticks(void);
#define GO64_SYS_CONSOLE_WRITE 1
    int64_t GO64_console_write(const void* buffer, uint64_t size);
    int64_t GO64_asm_console_write(const void* buffer, uint64_t size);
#define GO64_SYS_YIELD 2
    int64_t GO64_yield(void);
    int64_t GO64_asm_yield(void);
#define GO64_SYS_EXIT 3
    GO64_NORETURN void GO64_exit(void);
    GO64_NORETURN void GO64_asm_exit(void);
#define GO64_SYS_ALLOC 4
    int64_t GO64_alloc(uint64_t size);
    int64_t GO64_asm_alloc(uint64_t size);
#define GO64_SYS_FREE 5
    int64_t GO64_free(void* pointer);
    int64_t GO64_asm_free(void* pointer);
#define GO64_SYS_FS_OPEN 100
    int64_t GO64_fs_open(const wchar_t* path, uint64_t flags);
    int64_t GO64_asm_fs_open(const wchar_t* path, uint64_t flags);
#define GO64_SYS_FS_CLOSE 101
    int64_t GO64_fs_close(int64_t fd);
    int64_t GO64_asm_fs_close(int64_t fd);
#define GO64_SYS_FS_READ 102
    int64_t GO64_fs_read(int64_t fd, void* buffer, uint64_t size);
    int64_t GO64_asm_fs_read(int64_t fd, void* buffer, uint64_t size);
#define GO64_SYS_FS_WRITE 103
    int64_t GO64_fs_write(int64_t fd, const void* buffer, uint64_t size);
    int64_t GO64_asm_fs_write(int64_t fd, const void* buffer, uint64_t size);
#define GO64_SYS_FS_SEEK 104
    int64_t GO64_fs_seek(int64_t fd, int64_t offset, uint64_t origin);
    int64_t GO64_asm_fs_seek(int64_t fd, int64_t offset, uint64_t origin);
#define GO64_SYS_FS_STAT 105
    int64_t GO64_fs_stat(const wchar_t* path, GO64_FSInfo* info);
    int64_t GO64_asm_fs_stat(const wchar_t* path, GO64_FSInfo* info);
#define GO64_SYS_FS_FSTAT 106
    int64_t GO64_fs_fstat(int64_t fd, GO64_FSInfo* info);
    int64_t GO64_asm_fs_fstat(int64_t fd, GO64_FSInfo* info);
#define GO64_SYS_FS_MKDIR 107
    int64_t GO64_fs_mkdir(const wchar_t* path);
    int64_t GO64_asm_fs_mkdir(const wchar_t* path);
#define GO64_SYS_FS_UNLINK 108
    int64_t GO64_fs_unlink(const wchar_t* path);
    int64_t GO64_asm_fs_unlink(const wchar_t* path);
#define GO64_SYS_FS_RMDIR 109
    int64_t GO64_fs_rmdir(const wchar_t* path);
    int64_t GO64_asm_fs_rmdir(const wchar_t* path);
#define GO64_SYS_FS_TRUNCATE 110
    int64_t GO64_fs_truncate(int64_t fd, uint64_t size);
    int64_t GO64_asm_fs_truncate(int64_t fd, uint64_t size);
#define GO64_SYS_FS_READDIR 111
    int64_t GO64_fs_readdir(int64_t fd, GO64_FSInfo* info);
    int64_t GO64_asm_fs_readdir(int64_t fd, GO64_FSInfo* info);
#define GO64_SYS_FS_RENAME 112
    int64_t GO64_fs_rename(const wchar_t* oldpath, const wchar_t* newpath);
    int64_t GO64_asm_fs_rename(const wchar_t* oldpath, const wchar_t* newpath);
#define GO64_SYS_FS_FCNTL 113
    int64_t GO64_fs_fcntl(int64_t fd, uint64_t command, uint64_t flags);
    int64_t GO64_asm_fs_fcntl(int64_t fd, uint64_t command, uint64_t flags);
#define GO64_SYS_SOCKET 120
    int64_t GO64_socket(uint64_t family, uint64_t type, uint64_t protocol);
    int64_t GO64_asm_socket(uint64_t family, uint64_t type, uint64_t protocol);
#define GO64_SYS_BIND 121
    int64_t GO64_bind(int64_t fd, const GO64_SocketAddress* address, uint64_t address_size);
    int64_t GO64_asm_bind(int64_t fd, const GO64_SocketAddress* address, uint64_t address_size);
#define GO64_SYS_LISTEN 122
    int64_t GO64_listen(int64_t fd, int64_t backlog);
    int64_t GO64_asm_listen(int64_t fd, int64_t backlog);
#define GO64_SYS_ACCEPT 123
    int64_t GO64_accept(int64_t fd, GO64_SocketAddress* address, uint32_t* address_size);
    int64_t GO64_asm_accept(int64_t fd, GO64_SocketAddress* address, uint32_t* address_size);
#define GO64_SYS_CONNECT 124
    int64_t GO64_connect(int64_t fd, const GO64_SocketAddress* address, uint64_t address_size);
    int64_t GO64_asm_connect(int64_t fd, const GO64_SocketAddress* address, uint64_t address_size);
#define GO64_SYS_SEND 125
    int64_t GO64_send(int64_t fd, const void* buffer, uint64_t size, uint64_t flags);
    int64_t GO64_asm_send(int64_t fd, const void* buffer, uint64_t size, uint64_t flags);
#define GO64_SYS_RECV 126
    int64_t GO64_recv(int64_t fd, void* buffer, uint64_t size, uint64_t flags);
    int64_t GO64_asm_recv(int64_t fd, void* buffer, uint64_t size, uint64_t flags);
#define GO64_SYS_SENDTO 127
    int64_t GO64_sendto(int64_t                   fd,
                        const void*               buffer,
                        uint64_t                  size,
                        uint64_t                  flags,
                        const GO64_SocketAddress* address,
                        uint64_t                  address_size);
    int64_t GO64_asm_sendto(int64_t                   fd,
                            const void*               buffer,
                            uint64_t                  size,
                            uint64_t                  flags,
                            const GO64_SocketAddress* address,
                            uint64_t                  address_size);
#define GO64_SYS_RECVFROM 128
    int64_t GO64_recvfrom(int64_t             fd,
                          void*               buffer,
                          uint64_t            size,
                          uint64_t            flags,
                          GO64_SocketAddress* address,
                          uint32_t*           address_size);
    int64_t GO64_asm_recvfrom(int64_t             fd,
                              void*               buffer,
                              uint64_t            size,
                              uint64_t            flags,
                              GO64_SocketAddress* address,
                              uint32_t*           address_size);
#define GO64_SYS_GETSOCKNAME 129
    int64_t GO64_getsockname(int64_t fd, GO64_SocketAddress* address, uint32_t* address_size);
    int64_t GO64_asm_getsockname(int64_t fd, GO64_SocketAddress* address, uint32_t* address_size);
#define GO64_SYS_GETPEERNAME 130
    int64_t GO64_getpeername(int64_t fd, GO64_SocketAddress* address, uint32_t* address_size);
    int64_t GO64_asm_getpeername(int64_t fd, GO64_SocketAddress* address, uint32_t* address_size);
#define GO64_SYS_SOCKET_CLOSE 131
    int64_t GO64_socket_close(int64_t fd);
    int64_t GO64_asm_socket_close(int64_t fd);
#define GO64_SYS_LOG 132
    int64_t GO64_log(const char* text);
    int64_t GO64_asm_log(const char* text);
#define GO64_SYS_WINDOW_CREATE 140
    int64_t GO64_window_create(const GO64_WindowOptions* options);
    int64_t GO64_asm_window_create(const GO64_WindowOptions* options);
#define GO64_SYS_WINDOW_DESTROY 141
    int64_t GO64_window_destroy(uint64_t window);
    int64_t GO64_asm_window_destroy(uint64_t window);
#define GO64_SYS_WINDOW_FILL 142
    int64_t GO64_window_fill(uint64_t window, const GO64_Drawing* drawing);
    int64_t GO64_asm_window_fill(uint64_t window, const GO64_Drawing* drawing);
#define GO64_SYS_WINDOW_TEXT 143
    int64_t GO64_window_text(uint64_t window, const GO64_Drawing* drawing);
    int64_t GO64_asm_window_text(uint64_t window, const GO64_Drawing* drawing);
#define GO64_SYS_WINDOW_EVENT 144
    int64_t GO64_window_event(uint64_t window, GO64_Event* event, uint64_t mode);
    int64_t GO64_asm_window_event(uint64_t window, GO64_Event* event, uint64_t mode);
#define GO64_SYS_WINDOW_FOCUS 145
    int64_t GO64_window_focus(uint64_t window);
    int64_t GO64_asm_window_focus(uint64_t window);
#define GO64_SYS_WINDOW_BLIT 146
    int64_t GO64_window_blit(uint64_t window, const GO64_Blit* tile);
    int64_t GO64_asm_window_blit(uint64_t window, const GO64_Blit* tile);
#define GO64_SYS_PROCESS_SPAWN 150
    int64_t GO64_process_spawn(const void*        image,
                               uint64_t           size,
                               uint64_t           argc,
                               const char* const* argv);
    int64_t GO64_asm_process_spawn(const void*        image,
                                   uint64_t           size,
                                   uint64_t           argc,
                                   const char* const* argv);
#define GO64_SYS_PROCESS_READ 151
    int64_t GO64_process_read(void* buffer, uint64_t size);
    int64_t GO64_asm_process_read(void* buffer, uint64_t size);
#define GO64_SYS_PROCESS_STATUS 152
    int64_t GO64_process_status(void);
    int64_t GO64_asm_process_status(void);
#define GO64_SYS_PROCESS_GETCWD 153
#define GO64_SYS_PROCESS_CHDIR 154
    int64_t GO64_process_getcwd(char* buffer, uint64_t size);
    int64_t GO64_process_chdir(const char* path);
    int64_t GO64_asm_process_getcwd(char* buffer, uint64_t size);
    int64_t GO64_asm_process_chdir(const char* path);
    int64_t GO64_asm_syscall6(uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t);
#ifdef __cplusplus
}
#endif
#endif