Add two syscalls.

This commit is contained in:
Alexander Myltsev 2022-12-14 16:50:10 +03:00
parent 069625a6e0
commit a6598886f0
9 changed files with 57 additions and 6 deletions

View File

@ -47,8 +47,8 @@ debug-nox: image.bin
-ex "break _start" \
-ex "continue"
fs.img: kernel.bin tools/mkfs user/false
tools/mkfs $@ $< user/false
fs.img: kernel.bin tools/mkfs user/false user/greet
tools/mkfs $@ $< user/false user/greet
LDFLAGS=-m elf_i386

View File

@ -1,5 +1,7 @@
#include "isr.h"
#include "gdt.h"
#include "../syscall.h"
#include "../proc.h"
#include "../drivers/port.h"
#include "../console.h"
@ -83,6 +85,26 @@ void register_interrupt_handler(uint8_t i, isr_t handler) {
}
void trap(registers_t *r) {
if (r->int_no == T_SYSCALL) {
switch (r->eax) {
case SYS_exit:
if (r->ebx == 0) {
printk("* success\n");
} else {
printk("* failure\n");
}
killproc();
case SYS_greet:
printk("Hello world!\n");
r->eax = 0;
break;
default:
printk("Unknown syscall\n");
r->eax = -1;
}
return;
}
if (r->int_no < 32) {
const char* msg = "Reserved";
if (r->int_no < ARRLEN(exception_messages)) {

View File

@ -18,7 +18,6 @@ enum {
IRQ13,
IRQ14,
IRQ15,
T_SYSCALL = 0x84,
};
/* Struct which aggregates many registers.

View File

@ -8,8 +8,8 @@ alltraps:
pushl %gs
pushal
//mov $10, %ax
//mov %ax, %ds
mov $0x10, %ax
mov %ax, %ds
# Call trap(tf), where tf=%esp
pushl %esp

6
proc.c
View File

@ -67,3 +67,9 @@ void run_elf(const char* name) {
// process has finished
}
_Noreturn void killproc() {
void* task_stack;
swtch(&task_stack, vm->kernel_thread);
__builtin_unreachable();
}

1
proc.h
View File

@ -1,3 +1,4 @@
#pragma once
void run_elf(const char* name);
_Noreturn void killproc();

9
syscall.h Normal file
View File

@ -0,0 +1,9 @@
#pragma once
enum {
T_SYSCALL = 0x84,
SYS_exit = 0,
SYS_greet = 1,
};
int syscall(int call, int arg);

View File

@ -1,8 +1,15 @@
#include "../syscall.h"
int main();
int syscall(int call, int arg) {
asm("int $0x84": "+a"(call) : "b"(arg));
return call;
}
_Noreturn
void _exit(int exit_status) {
asm("int $0x84": : "a"(0), "b"(exit_status));
syscall(SYS_exit, exit_status);
__builtin_unreachable();
}

7
user/greet.c Normal file
View File

@ -0,0 +1,7 @@
#include "../syscall.h"
int main(void) {
syscall(SYS_greet, 0);
syscall(SYS_greet, 0);
return 0;
}