aboutsummaryrefslogtreecommitdiff
path: root/syscall.jai
blob: 46a9d849f22410e3f13fcccb34562984196252ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#import "Basic";
#import "POSIX";

// This prototype is suposed to allow me to use the "open" posix syscall
// that returns a file-descriptor (fd) that can then be used by the mmap
// syscall that allows to map a file to memory.
//
// Syscall info: https://filippo.io/linux-syscall-table/

main :: () {
    print("starting syscall\n");

    sys_exit(-1);
    fd := sys_open("./dummy");
    print("fd is %\nerrno is %\n", fd, errno());
    
    print("done syscall\n");
    LOOPER :: "\\|/-";
    idx := 0;
    loop: string;
    loop.count = 1;
    for 0..3 {
        sleep_milliseconds(1000);
        loop.data = *LOOPER.data[idx];
        idx = (idx + 1) % LOOPER.count;
        print("\rlooping %", loop);
    }
    
    sys_close(fd);
    print("\rclosed\n");
    
    for 0..3 {
        sleep_milliseconds(1000);
        loop.data = *LOOPER.data[idx];
        idx = (idx + 1) % LOOPER.count;
        print("\rlooping %", loop);
    }
}

sys_open :: (path: string) -> fd: s64 {
    SYS_OPEN :: 2;
    return syscall(SYS_OPEN, cast(*s8)path.data, O_RDONLY);
}

sys_close :: (fd: s64) {
    SYS_CLOSE :: 3;
    syscall(SYS_CLOSE, fd);
}

sys_exit :: (status: s32) {
#if OS == .WINDOWS {
    // @Note TerminateProcess may be better, as it can't deadlock
    ExitProcess :: (error_code: u32) #foreign kernel32;
    ExitProcess(xx errno);
}
else #if OS == .LINUX {
    SYS_EXIT_GROUP :: 231;
    #if CPU==.X64 {
        print("\rLINUX-ASM\n");
        #asm SYSCALL_SYSRET {
            mov eax: gpr === a,  SYS_EXIT_GROUP;
            mov out: gpr === di,  status;
            syscall t1:, t2:, eax, out;
        };
    } else {
        syscall(SYS_EXIT_GROUP, status);
    }
}
}