syscalls: implement read syscall

This commit is contained in:
2025-06-21 17:09:34 +03:00
parent bf7d96865a
commit bb096f826a
6 changed files with 65 additions and 26 deletions
BIN
View File
Binary file not shown.
+36 -24
View File
@@ -1,33 +1,45 @@
section .data section .data
file_path db '/ebalo/huy.txt', 0 ; Specific file path, null-terminated file_path db '/ebalo/huy.txt', 0 ; File path
message db 'Hello, World from KatauOS!', 10, 0 ; Message to write, with newline message db 'Hello, World from KatauOS!', 10, 0 ; Message with newline
message_len equ $ - message - 1 ; Length of message (14 bytes, excluding null) message_len equ $ - message - 1 ; Length excluding null terminator
section .bss
buffer resb 256 ; Buffer for reading file content
read_count resd 1 ; Store bytes read count
section .text section .text
global _start global _start
_start: _start:
; Open the file ; Reopen file for reading
mov eax, 5 ; System call number for sys_open mov eax, 5 ; sys_open
mov ebx, file_path ; Pointer to file path mov ebx, file_path
mov ecx, 65 ; Flags: O_WRONLY (1) | O_CREAT (64) mov ecx, 0 ; O_RDONLY
mov edx, 0x1A4 ; Mode: 0644 (rw-r--r--) mov edx, 0 ; Mode (unused)
int 0x80 ; Invoke system call int 0x80
mov edi, eax ; Save file descriptor in edi mov edi, eax ; Save new descriptor
; Write to the file ; Read file content
mov eax, 4 ; System call number for sys_write mov eax, 3 ; sys_read
mov ebx, edi ; File descriptor mov ebx, edi
mov ecx, message ; Pointer to message mov ecx, buffer
mov edx, message_len ; Number of bytes to write mov edx, 256
int 0x80 ; Invoke system call int 0x80
mov [read_count], eax ; Save bytes read count
; Close the file ; Write to stdout
mov eax, 6 ; System call number for sys_close mov eax, 4 ; sys_write
mov ebx, edi ; File descriptor mov ebx, 1 ; stdout
int 0x80 ; Invoke system call mov ecx, buffer
mov edx, [read_count] ; Use actual bytes read
int 0x80
; Exit the program ; Close file
mov eax, 1 ; System call number for sys_exit mov eax, 6 ; sys_close
xor ebx, ebx ; Return status 0 mov ebx, edi
int 0x80 ; Invoke system call int 0x80
; Exit
mov eax, 1 ; sys_exit
xor ebx, ebx ; Return 0
int 0x80
BIN
View File
Binary file not shown.
+1 -1
View File
@@ -218,7 +218,7 @@ void kmain(unsigned int magic, unsigned int info)
printf("huy1 points to: 0x%X huy2 points to:0x%X\n", huy, huy2); printf("huy1 points to: 0x%X huy2 points to:0x%X\n", huy, huy2);
printf("huy1: %s\nhuy2: %s", huy, huy2); printf("huy1: %s\nhuy2: %s", huy, huy2);
exec_from_file("/boot/katauos.bin"); exec_from_file("/ebalo/aaa");
while(1) while(1)
{ {
+1 -1
View File
@@ -68,7 +68,7 @@ void exec_from_file(const char* filename)
uint8_t* file; uint8_t* file;
int result = read_file("/ebalo/aaa", &file); int result = read_file(filename, &file);
debug_log("result: %X\n", result); debug_log("result: %X\n", result);
debug_log("buffer: %s\n", file); debug_log("buffer: %s\n", file);
+27
View File
@@ -46,6 +46,22 @@ const char* flags_to_mode_str(int flags) {
return mode; return mode;
} }
int sys_read(TrapFrame *tf)
{
if(tf->ebx < 3)
{
//TODO: implement stdin handling here
}
else
{
if(current->file_descriptors[tf->ebx] == NULL)
{
return -EBADF;
}
return fat_fread(current->file_descriptors[tf->ebx], (void*)tf->ecx, tf->edx);
}
}
int sys_write(TrapFrame *tf) int sys_write(TrapFrame *tf)
{ {
if(tf->ebx < 3) if(tf->ebx < 3)
@@ -53,7 +69,9 @@ int sys_write(TrapFrame *tf)
//stdin, stdout or stderr //stdin, stdout or stderr
if(tf->ebx == STDOUT_FILENO) if(tf->ebx == STDOUT_FILENO)
{ {
debug_log("\n=================SYS_WRITE output to stdout=====================\n");
print((char*)tf->ecx, tf->edx); print((char*)tf->ecx, tf->edx);
debug_log("\n=================END OF THAT SHIT=====================\n");
tf->ecx++; tf->ecx++;
} }
} }
@@ -102,9 +120,12 @@ int sys_open(TrapFrame *tf)
int sys_close(TrapFrame *tf) int sys_close(TrapFrame *tf)
{ {
int fd = tf->ebx; int fd = tf->ebx;
debug_log("SYS_CLOSE: fd = 0x%X", fd);
if(current->file_descriptors[fd] != NULL) if(current->file_descriptors[fd] != NULL)
{ {
debug_log("SYS_CLOSE: current->file_descriptors[fd] != NULL");
int result = fat_fclose(current->file_descriptors[fd]); int result = fat_fclose(current->file_descriptors[fd]);
debug_log("SYS_CLOSE: fat_fclose result: 0x%X", result);
free(current->file_descriptors[fd]); free(current->file_descriptors[fd]);
current->file_descriptors[fd] = NULL; current->file_descriptors[fd] = NULL;
return result; return result;
@@ -120,6 +141,12 @@ void handle_syscall(TrapFrame *tf)
debug_log("ECX: %s ", tf->ecx); debug_log("ECX: %s ", tf->ecx);
debug_log("EDX: %X\n", tf->edx); debug_log("EDX: %X\n", tf->edx);
if(tf->eax == 3)//read
{
int r = sys_read(tf);
tf->eax = r;
}
if(tf->eax == 4)//write if(tf->eax == 4)//write
{ {
int r = sys_write(tf); int r = sys_write(tf);