Compare commits

..

4 Commits

5 changed files with 107 additions and 17 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;
}
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

View File

@ -71,20 +71,35 @@ int32_t openat(int32_t fd, const char* filename, uint32_t flags, uint32_t mode)
: "r"(fd), "r"(filename), "r"(flags), "r"(mode)
: "a0", "a1", "a2", "a3", "v0"
);
/*
asm volatile (
"move $a0, %1\n"
"move $a1, %2\n"
"move $a2, %3\n"
"li $v0, 4005\n"
"syscall\n"
"move %0, $v0\n"
: "=r"(file)
: "r"(filename), "r"(flags), "r"(mode)
: "a0", "a1", "a2", "a3", "v0"
);
*/
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

View File

@ -55,7 +55,7 @@ uint32_t fork() {
int32_t openat(int32_t fd, const char* filename, uint32_t flags, uint32_t mode) {
int32_t file;
asm volatile (
"mov %%r10, %5\n"
"mov %5, %%r10\n"
"syscall\n"
: "=a"(file)
: "a"(SYS_OPENAT), "D"(fd), "S"(filename), "d"(flags), "r"((uint64_t)mode) // RDI, RSI, RDX are used in x86-64 for write args
@ -64,4 +64,28 @@ int32_t openat(int32_t fd, const char* filename, uint32_t flags, uint32_t mode)
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__ */

View File

@ -28,9 +28,16 @@ int main() {
// Test the read syscall
#define INPUT_BUFFER_LEN 4096
char input_buffer[INPUT_BUFFER_LEN] = {0};
int32_t fout = openc("outfile", O_RDWR | O_CREAT | O_TRUNC, 0664);
write(STDOUT, "Enter some text:", 16);
intptr_t n_read = read(STDIN, input_buffer, INPUT_BUFFER_LEN);
write(STDOUT, input_buffer, n_read);
if(fout > 0) {
write(fout, input_buffer, n_read);
fsync(fout);
close(fout);
}
// Test the open syscall
int32_t file = open("/proc/version", O_RDONLY);

22
sys.h
View File

@ -13,11 +13,27 @@ intptr_t read(int32_t fd, const void* buf, intptr_t size);
#define O_WRONLY 01
#define O_RDWR 02
#define AT_FDCWD -100
#if defined(__mips__)
// linux/arch/mips/include/uapi/asm/fcntl.h
#define O_APPEND 0x0008
#define O_CREAT 0x0100 /* not fcntl */
#define O_TRUNC 0x0200 /* not fcntl */
#define O_EXCL 0x0400 /* not fcntl */
#define O_NOCTTY 0x0800 /* not fcntl */
#else
// linux/include/uapi/asm-generic/fcntl.h
#define O_CREAT 1<<6
#define O_EXCL 1<<7
#define O_NOCTTY 1<<8
#define O_TRUNC 1<<9
#define O_APPEND 1<<10
#endif
int32_t openat(int32_t fd, const char* filename, uint32_t flags, uint32_t mode);
#define open(...) openat(AT_FDCWD, __VA_ARGS__, 0)
//inline int32_t openat(int32_t fd, const char* filename, uint32_t flags) {return openat(fd, filename, flags, 0);}
//inline int32_t open(const char* filename, uint32_t flags, uint32_t mode) {return openat(AT_FDCWD, filename, flags, mode);}
//inline int32_t open(const char* filename, uint32_t flags) {return openat(AT_FDCWD, filename, flags, 0);}
#define openc(...) openat(AT_FDCWD, __VA_ARGS__)
int close(unsigned int fd);
int fsync(unsigned int fd);
uint32_t fork();