aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordam <dam@gudinoff>2023-02-26 19:06:42 +0000
committerdam <dam@gudinoff>2023-02-26 19:06:42 +0000
commita17d91de4bbbdf01f1a157d9825fd13db6be72da (patch)
tree5669e33be4eb847c1ff44f82b380c6e5ad0562ca
parentffbcc2d1556dcd55f53abb320c5be72951506f9f (diff)
downloadtask-time-tracker-a17d91de4bbbdf01f1a157d9825fd13db6be72da.tar.zst
task-time-tracker-a17d91de4bbbdf01f1a157d9825fd13db6be72da.zip
Improved syscall prototypes.
-rw-r--r--syscall.jai101
1 files changed, 60 insertions, 41 deletions
diff --git a/syscall.jai b/syscall.jai
index e30dc2f..e5dee41 100644
--- a/syscall.jai
+++ b/syscall.jai
@@ -11,27 +11,32 @@ main :: () {
//sys_exit(-1);
print("> open dummy\n");
- fd, error_open := sys_open("./dummy");
+ fd, error_open := sys_open("./dummy", O_RDONLY, O_RDONLY);
print("> opened dummy with fd % and errno %\n", fd, error_open);
if fd < 0 return;
- LOOPER :: "\\|/-";
- idx := 0;
- loop: string;
- loop.count = 1;
- for 0..1 {
- sleep_milliseconds(1000);
- loop.data = *LOOPER.data[idx];
- idx = (idx + 1) % LOOPER.count;
- print("\rlooping %", loop);
- }
-
+
+ //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);
+ //}
+
+
+ size, error_lseek := sys_lseek(fd, 0, SEEK_END);
+ print("\r> dummy has size % with error %\n", size, error_lseek);
+
error_close := sys_close(fd);
print("\r> close(%) with errno %\n", fd, error_close);
print("---\n");
error :s64;
- fd, error = sys_open("./fail");
+ fd, error = sys_open("./fail", O_RDONLY, O_RDONLY);
print("$ open(fail) returned fd % and error %\n", fd, error);
error = sys_close(456);
print("$ close(456) returned error %\n", error);
@@ -42,10 +47,45 @@ main :: () {
+sys_lseek :: (file_descriptor: s64, offset: s64, whence: s64) -> offset_location: s64, error: s64 {
+ print("\r# sys_lseek\n");
+ #if OS == .WINDOWS {
+ print("TODO Implement this\n"); // TODO Implement this.
+ }
+ else #if OS == .LINUX {
+ SYS_LSEEK :: 8;
+ result: s64 = ---;
+ #if CPU == .X64 {
+ print("\r# LINUX:ASM\n");
+ #asm SYSCALL_SYSRET {
+ mov rax: gpr === a, SYS_LSEEK;
+ mov rdi: gpr === di, file_descriptor;
+ mov rsi: gpr === si, offset;
+ mov rdx: gpr === d, whence;
+ //syscall rcx, r11, rax, rdi, rsi, rdx; // TODO Cleanup?
+ syscall
+ rcx: gpr === c, // Implicitly used by syscall to store RIP
+ r11: gpr === 11, // Implicitly used by syscall to store RFLAGS
+ rax, rdi, rsi, rdx;
+ mov result, rax;
+ }
+ if result < 0
+ return -1, -result; // Error.
+ else
+ return result, 0; // OK.
+ } else {
+ print("\r# LINUX:LIBC\n");
+ result = syscall(SYS_LSEEK, file_descriptor, offset, whence);
+ if result < 0
+ return -1, errno(); // Error.
+ else
+ return result, 0; // OK.
+ }
+ }
+}
-
-sys_open :: (path: string) -> file_descriptor: s64, error: s64 {
-
+sys_open :: (path: string, flags: s64, mode: s64) -> file_descriptor: s64, error: s64 {
+ print("\r# sys_open\n");
#if OS == .WINDOWS {
print("TODO Implement this\n"); // TODO Implement this.
}
@@ -58,8 +98,8 @@ sys_open :: (path: string) -> file_descriptor: s64, error: s64 {
#asm SYSCALL_SYSRET {
mov rax: gpr === a, SYS_OPEN;
mov rdi: gpr === di, data;
- mov rsi: gpr === si, O_RDONLY;
- mov rdx: gpr === d, O_RDONLY;
+ mov rsi: gpr === si, flags;
+ mov rdx: gpr === d, mode; // TODO Is this really used?
//syscall rcx, r11, rax, rdi, rsi, rdx; // TODO Cleanup?
syscall
rcx: gpr === c, // Implicitly used by syscall to store RIP
@@ -73,7 +113,7 @@ sys_open :: (path: string) -> file_descriptor: s64, error: s64 {
return result, 0; // OK.
} else {
print("\r# LINUX:LIBC\n");
- result = syscall(SYS_OPEN, data, O_RDONLY);
+ result = syscall(SYS_OPEN, data, flags, mode);
if result < 0
return -1, errno(); // Error.
else
@@ -83,7 +123,7 @@ sys_open :: (path: string) -> file_descriptor: s64, error: s64 {
}
sys_close :: (file_descriptor: s64) -> error: s64 {
-
+ print("\r# sys_close\n");
#if OS == .WINDOWS {
print("TODO Implement this\n"); // TODO Implement this.
}
@@ -109,24 +149,3 @@ sys_close :: (file_descriptor: s64) -> error: s64 {
}
}
}
-
-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);
- }
-}
-}