aboutsummaryrefslogtreecommitdiff
path: root/syscall.jai
diff options
context:
space:
mode:
Diffstat (limited to 'syscall.jai')
-rw-r--r--syscall.jai69
1 files changed, 69 insertions, 0 deletions
diff --git a/syscall.jai b/syscall.jai
new file mode 100644
index 0000000..46a9d84
--- /dev/null
+++ b/syscall.jai
@@ -0,0 +1,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);
+ }
+}
+}