Add two syscalls.
This commit is contained in:
parent
069625a6e0
commit
a6598886f0
4
Makefile
4
Makefile
@ -47,8 +47,8 @@ debug-nox: image.bin
|
|||||||
-ex "break _start" \
|
-ex "break _start" \
|
||||||
-ex "continue"
|
-ex "continue"
|
||||||
|
|
||||||
fs.img: kernel.bin tools/mkfs user/false
|
fs.img: kernel.bin tools/mkfs user/false user/greet
|
||||||
tools/mkfs $@ $< user/false
|
tools/mkfs $@ $< user/false user/greet
|
||||||
|
|
||||||
LDFLAGS=-m elf_i386
|
LDFLAGS=-m elf_i386
|
||||||
|
|
||||||
|
|||||||
22
cpu/idt.c
22
cpu/idt.c
@ -1,5 +1,7 @@
|
|||||||
#include "isr.h"
|
#include "isr.h"
|
||||||
#include "gdt.h"
|
#include "gdt.h"
|
||||||
|
#include "../syscall.h"
|
||||||
|
#include "../proc.h"
|
||||||
#include "../drivers/port.h"
|
#include "../drivers/port.h"
|
||||||
#include "../console.h"
|
#include "../console.h"
|
||||||
|
|
||||||
@ -83,6 +85,26 @@ void register_interrupt_handler(uint8_t i, isr_t handler) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void trap(registers_t *r) {
|
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) {
|
if (r->int_no < 32) {
|
||||||
const char* msg = "Reserved";
|
const char* msg = "Reserved";
|
||||||
if (r->int_no < ARRLEN(exception_messages)) {
|
if (r->int_no < ARRLEN(exception_messages)) {
|
||||||
|
|||||||
@ -18,7 +18,6 @@ enum {
|
|||||||
IRQ13,
|
IRQ13,
|
||||||
IRQ14,
|
IRQ14,
|
||||||
IRQ15,
|
IRQ15,
|
||||||
T_SYSCALL = 0x84,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Struct which aggregates many registers.
|
/* Struct which aggregates many registers.
|
||||||
|
|||||||
@ -8,8 +8,8 @@ alltraps:
|
|||||||
pushl %gs
|
pushl %gs
|
||||||
pushal
|
pushal
|
||||||
|
|
||||||
//mov $10, %ax
|
mov $0x10, %ax
|
||||||
//mov %ax, %ds
|
mov %ax, %ds
|
||||||
|
|
||||||
# Call trap(tf), where tf=%esp
|
# Call trap(tf), where tf=%esp
|
||||||
pushl %esp
|
pushl %esp
|
||||||
|
|||||||
6
proc.c
6
proc.c
@ -67,3 +67,9 @@ void run_elf(const char* name) {
|
|||||||
|
|
||||||
// process has finished
|
// process has finished
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_Noreturn void killproc() {
|
||||||
|
void* task_stack;
|
||||||
|
swtch(&task_stack, vm->kernel_thread);
|
||||||
|
__builtin_unreachable();
|
||||||
|
}
|
||||||
|
|||||||
1
proc.h
1
proc.h
@ -1,3 +1,4 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
void run_elf(const char* name);
|
void run_elf(const char* name);
|
||||||
|
_Noreturn void killproc();
|
||||||
|
|||||||
9
syscall.h
Normal file
9
syscall.h
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
enum {
|
||||||
|
T_SYSCALL = 0x84,
|
||||||
|
SYS_exit = 0,
|
||||||
|
SYS_greet = 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
int syscall(int call, int arg);
|
||||||
@ -1,8 +1,15 @@
|
|||||||
|
#include "../syscall.h"
|
||||||
|
|
||||||
int main();
|
int main();
|
||||||
|
|
||||||
|
int syscall(int call, int arg) {
|
||||||
|
asm("int $0x84": "+a"(call) : "b"(arg));
|
||||||
|
return call;
|
||||||
|
}
|
||||||
|
|
||||||
_Noreturn
|
_Noreturn
|
||||||
void _exit(int exit_status) {
|
void _exit(int exit_status) {
|
||||||
asm("int $0x84": : "a"(0), "b"(exit_status));
|
syscall(SYS_exit, exit_status);
|
||||||
__builtin_unreachable();
|
__builtin_unreachable();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
7
user/greet.c
Normal file
7
user/greet.c
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
#include "../syscall.h"
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
syscall(SYS_greet, 0);
|
||||||
|
syscall(SYS_greet, 0);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user