Add support for fflush and close syscalls

This commit is contained in:
Lucas Schumacher 2025-04-27 02:48:55 -04:00
parent f07d793e6c
commit adfdaa27f7
5 changed files with 84 additions and 0 deletions

View File

@ -99,4 +99,32 @@ int32_t openat(int32_t fd, const char* filename, uint32_t flags, uint32_t mode)
return (int32_t)rtn; return (int32_t)rtn;
} }
int close(unsigned int fd){
long int rtn;
asm (
"mov x0, %1\n"
"mov x8, #57\n"
"svc #0\n"
"mov %0, x0\n"
: "=r"(rtn)
: "r" ((long)fd)
: "x0", "x8"
);
return (int)rtn;
}
int fsync(unsigned int fd){
long int rtn;
asm (
"mov x0, %1\n"
"mov x8, #82\n"
"svc #0\n"
"mov %0, x0\n"
: "=r"(rtn)
: "r" ((long)fd)
: "x0", "x8"
);
return (int)rtn;
}
#endif #endif

View File

@ -74,4 +74,32 @@ int32_t openat(int32_t fd, const char* filename, uint32_t flags, uint32_t mode)
return file; return file;
} }
int close(unsigned int fd){
int rtn = 0;
asm (
"move $a0, %1\n"
"li $v0, 4006\n"
"syscall\n"
"move %0, $v0\n"
: "=r"(rtn)
: "r" (fd)
: "a0", "v0"
);
return rtn;
}
int fsync(unsigned int fd){
int rtn = 0;
asm (
"move $a0, %1\n"
"li $v0, 4118\n"
"syscall\n"
"move %0, $v0\n"
: "=r"(rtn)
: "r" (fd)
: "a0", "v0"
);
return rtn;
}
#endif #endif

View File

@ -64,4 +64,28 @@ int32_t openat(int32_t fd, const char* filename, uint32_t flags, uint32_t mode)
return file; return file;
} }
#define SYS_CLOSE 3
int close(unsigned int fd) {
int rtn;
asm volatile(
"syscall"
: "=a"(rtn)
: "a"(SYS_CLOSE), "D"(fd)
: "rcx", "r11"
);
return rtn;
}
#define SYS_FSYNC 74
int fsync(unsigned int fd) {
int rtn;
asm volatile(
"syscall"
: "=a"(rtn)
: "a"(SYS_FSYNC), "D"(fd)
: "rcx", "r11"
);
return rtn;
}
#endif /* ifdef __x86_64__ */ #endif /* ifdef __x86_64__ */

View File

@ -35,6 +35,8 @@ int main() {
write(STDOUT, input_buffer, n_read); write(STDOUT, input_buffer, n_read);
if(fout > 0) { if(fout > 0) {
write(fout, input_buffer, n_read); write(fout, input_buffer, n_read);
fsync(fout);
close(fout);
} }
// Test the open syscall // Test the open syscall

2
sys.h
View File

@ -32,6 +32,8 @@ intptr_t read(int32_t fd, const void* buf, intptr_t size);
int32_t openat(int32_t fd, const char* filename, uint32_t flags, uint32_t mode); int32_t openat(int32_t fd, const char* filename, uint32_t flags, uint32_t mode);
#define open(...) openat(AT_FDCWD, __VA_ARGS__, 0) #define open(...) openat(AT_FDCWD, __VA_ARGS__, 0)
#define openc(...) openat(AT_FDCWD, __VA_ARGS__) #define openc(...) openat(AT_FDCWD, __VA_ARGS__)
int close(unsigned int fd);
int fsync(unsigned int fd);
uint32_t fork(); uint32_t fork();