should be right? #20

Merged
xamidev merged 5 commits from sys_fix into main 2026-05-08 12:59:06 +02:00
8 changed files with 94 additions and 37 deletions
Showing only changes of commit c061da4d81 - Show all commits
+5 -5
View File
@@ -21,10 +21,6 @@ CC_FLAGS=-Wall -Wextra -std=gnu99 -nostdlib -ffreestanding -fstack-protector -fn
LD := x86_64-elf-ld LD := x86_64-elf-ld
$(ELFFILE): $(BUILDDIR) $(OBJFILES) $(ELFFILE): $(BUILDDIR) $(OBJFILES)
nasm -f bin user/hello.S -o $(BUILDDIR)/hello
nasm -f bin user/pedicel.S -o $(BUILDDIR)/pedicel
tar cvf $(BUILDDIR)/initfs.tar -C $(BUILDDIR) hello pedicel
nasm -f elf64 src/arch/x86/idt.S -o $(BUILDDIR)/idt_stub.o nasm -f elf64 src/arch/x86/idt.S -o $(BUILDDIR)/idt_stub.o
$(LD) -o $(ELFFILE) -T linker.ld $(OBJFILES) $(BUILDDIR)/idt_stub.o $(LD) -o $(ELFFILE) -T linker.ld $(OBJFILES) $(BUILDDIR)/idt_stub.o
# Get the symbols for debugging # Get the symbols for debugging
@@ -45,7 +41,11 @@ limine/limine:
git clone https://github.com/limine-bootloader/limine.git --branch=v9.x-binary --depth=1 git clone https://github.com/limine-bootloader/limine.git --branch=v9.x-binary --depth=1
$(MAKE) -C limine $(MAKE) -C limine
initfs: .PHONY: user
user:
nasm -f bin user/hello.S -o $(BUILDDIR)/hello
nasm -f bin user/pedicel.S -o $(BUILDDIR)/pedicel
tar cvf $(BUILDDIR)/initfs.tar -C $(BUILDDIR) hello pedicel -C ../user wow.txt
build-iso: limine/limine $(ELFFILE) build-iso: limine/limine $(ELFFILE)
rm -rf iso_root rm -rf iso_root
+6 -2
View File
@@ -41,8 +41,12 @@ The recommended hardware to run PepperOS is the following:
## III. Syscall table ## III. Syscall table
The syscall interface in the Pepper kernel uses the System V ABI convention for argument order. The syscall interface in the Pepper kernel uses the System V ABI convention for argument order.
It vaguely mimics Unix-like systems.
Name | Number (%rax) | arg0 (%rdi) | arg1 (%rsi) | arg2 (%rdx) | Name | Number (%rax) | arg0 (%rdi) | arg1 (%rsi) | arg2 (%rdx) |
|---|---|---|---|---| |---|---|---|---|---|
| sys_write | 1 | unsigned int fd | const char* buf | size_t count | | | sys_read | 0 | unsigned int fd | char* buf | size_t count |
| sys_exit | 60 | int error_code | | | | | sys_write | 1 | unsigned int fd | const char* buf | size_t count |
| sys_open | 2 | const char* filename | int flags | |
| sys_close | 3 | unsigned int fd | | |
| sys_exit | 60 | int error_code | | |
+1 -1
View File
@@ -11,7 +11,7 @@
int initfs_init(struct limine_file* tar_file); int initfs_init(struct limine_file* tar_file);
int tar_lookup(unsigned char* archive, char* filename, char** out); int tar_lookup(unsigned char* archive, char* filename, char** out);
int tar_read(char* filename, char** buf);
int tar_exists(const char* filename); int tar_exists(const char* filename);
int tar_read(char* filename, char* out, int count, int offset);
#endif #endif
+2 -1
View File
@@ -14,7 +14,8 @@ enum ErrorCodes {
EIO, // Input/output error EIO, // Input/output error
ENOENT, // No entry ENOENT, // No entry
EBADFD, // Bad file descriptor EBADFD, // Bad file descriptor
EMFILE // Too many open files EMFILE, // Too many open files
EINVAL // Invalid argument
}; };
#define CLEAR_INTERRUPTS __asm__ volatile("cli") #define CLEAR_INTERRUPTS __asm__ volatile("cli")
+25 -18
View File
@@ -70,10 +70,12 @@ int sys_read(unsigned int fd, char* buf, size_t count)
return -EBADFD; // File descriptor wasn't open return -EBADFD; // File descriptor wasn't open
} }
// Here fd refers to a valid opened file.. // Here fd refers to a valid opened file..
int sz = tar_read(current_process->fdt[fd].filename,&buf); int sz = tar_read(current_process->fdt[fd].filename, buf, count,
current_process->fdt[fd].cursor);
if (sz == 0) { if (sz == 0) {
return -ENOENT; return -ENOENT;
} else { } else {
current_process->fdt[fd].cursor += sz;
return sz; return sz;
} }
} }
@@ -82,27 +84,25 @@ int sys_read(unsigned int fd, char* buf, size_t count)
} }
// TODO: Should have a return value: number of bytes written on success, -1 on error (errno set) // TODO: Should have a return value: number of bytes written on success, -1 on error (errno set)
void sys_write(unsigned int fd, const char* buf, size_t count) int sys_write(unsigned int fd, const char* buf, size_t count)
{ {
switch (fd) { switch (fd) {
case 1: //stdout case 1: //stdout
for (size_t i=0; i<count; i++) {
internal_putc(buf[i], NULL);
}
break;
case 2: //stderr case 2: //stderr
for (size_t i=0; i<count; i++) { for (size_t i=0; i<count; i++) {
internal_putc(buf[i], NULL); internal_putc(buf[i], NULL);
} }
break; return count;
default:
return -EBADFD;
} }
} }
void sys_exit(int error_code) int sys_exit(int error_code)
{ {
current_process->status = DEAD; current_process->status = DEAD;
DEBUG("exiting process PID=%u name=%s", current_process->pid, current_process->name); DEBUG("(pid=%u, name=%s)", current_process->pid, current_process->name);
return error_code;
} }
/* /*
@@ -125,29 +125,36 @@ void sys_exit(int error_code)
*/ */
struct cpu_status* syscall_handler(struct cpu_status* regs) struct cpu_status* syscall_handler(struct cpu_status* regs)
{ {
DEBUG("Syscall %lx with (arg0=%lx arg1=%lx)", regs->rax, regs->rdi, regs->rsi);
switch (regs->rax) switch (regs->rax)
{ {
case 0: //sys_read case 0:
DEBUG("sys_read(fd=%u, buf=%p, count=%u)", regs->rdi, regs->rsi, regs->rdx);
regs->rax = sys_read(regs->rdi, (char*)regs->rsi, regs->rdx); regs->rax = sys_read(regs->rdi, (char*)regs->rsi, regs->rdx);
break; break;
case 1: //sys_write case 1:
sys_write(regs->rdi, (char*)regs->rsi, regs->rdx); DEBUG("sys_write(fd=%u, buf=%p, count=%u)", regs->rdi, regs->rsi, regs->rdx);
regs->rax = sys_write(regs->rdi, (char*)regs->rsi, regs->rdx);
break; break;
case 2: case 2:
DEBUG("sys_open(filename=%s, flags=%u)", regs->rdi, regs->rsi);
regs->rax = sys_open((const char*)regs->rdi, regs->rsi); regs->rax = sys_open((const char*)regs->rdi, regs->rsi);
break; break;
case 3: case 3:
DEBUG("sys_close(fd=%u)", regs->rdi);
regs->rax = sys_close(regs->rdi); regs->rax = sys_close(regs->rdi);
break; break;
case 60: //sys_exit case 60:
sys_exit(regs->rdi); DEBUG("sys_exit(error_code=%d)", regs->rdi);
regs->rax = sys_exit(regs->rdi);
break; break;
default: // bad syscall default:
DEBUG("Bad syscall! (rax=%p, rdi=%p, rsi=%p, rdx=%p)",
regs->rax, regs->rdi, regs->rsi, regs->rdx);
regs->rax = 0xbad515ca11; regs->rax = 0xbad515ca11;
break; break;
} }
DEBUG("returned rax=%p (%u)", regs->rax, regs->rax);
return regs; return regs;
} }
+25 -7
View File
@@ -42,7 +42,7 @@ int tar_oct2bin(unsigned char* str, int size)
* *
* Return: * Return:
* $filesize - size of the file, if found * $filesize - size of the file, if found
* $0 - file not found * $-ENOENT - file not found
*/ */
int tar_lookup(unsigned char* archive, char* filename, char** out) int tar_lookup(unsigned char* archive, char* filename, char** out)
{ {
@@ -56,21 +56,39 @@ int tar_lookup(unsigned char* archive, char* filename, char** out)
} }
ptr += (((filesize + 511) / 512) + 1) * 512; ptr += (((filesize + 511) / 512) + 1) * 512;
} }
return 0; return -ENOENT;
} }
/* /*
* tar_read - read a file in the TAR file * tar_read - read a file in the TAR archive
* @filename: file to read (absolute path) * @filename: file to read (absolute path)
* @out: out buffer (if file is found) * @out: where to store file data if found
* @count: amount of bytes to read
* @offset: read from byte offset (0 for none)
* *
* Return: * Return:
* $filesize - size of the file, if found * $filesize - size of the file, if found
* $0 - file not found * $-ENOENT - file not found
*/ */
int tar_read(char* filename, char** buf) int tar_read(char* filename, char* out, int count, int offset)
{ {
return tar_lookup(archive_start_addr, filename, buf); char* file_data;
int filesize = tar_lookup(archive_start_addr, filename, &file_data);
if (filesize <= 0) {
return filesize;
}
if (offset >= filesize) {
return -EINVAL;
}
int remaining = filesize - offset;
int to_read = remaining < count ? remaining : count;
memcpy(out, file_data + offset, to_read);
return to_read;
} }
/* /*
+28 -2
View File
@@ -1,7 +1,8 @@
bits 64 bits 64
section .data section .data
hello db 0x0A, 0x0D, "User program 2 speaking", 0x0A, 0x0D, 0 hello db 0x0A, 0x0D, "TEST PROGRAM...", 0x0A, 0x0D, 0
filename db "wow.txt", 0
section .text section .text
@@ -9,7 +10,29 @@ _start:
mov rax, 0x1 ;sys_write mov rax, 0x1 ;sys_write
mov rdi, 0x1 ;stdout mov rdi, 0x1 ;stdout
lea rsi, [rel hello] lea rsi, [rel hello]
mov rdx, 27 ;count mov rdx, 19 ;count
int 0x80
; Open a file
mov rax, 0x2 ;sys_open
lea rdi, [rel filename] ;filename
mov rsi, 0x0 ;flags
int 0x80
mov rdi, rax ;fd
mov rax, 0x0 ;sys_read
lea rsi, [rel buf] ;buf
mov rdx, 33 ;count
int 0x80
mov rax, 0x1 ;sys_write
mov rdi, 0x1 ;stdout
lea rsi, [rel buf] ;buf
mov rdx, 33 ;count
int 0x80
mov rax, 0x3 ;sys_close
mov rdi, 0x3 ;fd
int 0x80 int 0x80
; when we are ready to have an os specific toolchain, ; when we are ready to have an os specific toolchain,
@@ -23,3 +46,6 @@ _start:
.loop: .loop:
jmp .loop jmp .loop
section .bss
buf resb 10
+1
View File
@@ -0,0 +1 @@
hi from a file opened in usermode