Implemented the basis for syscalls

A software interrupt with vector 0x50 will cause a syscall to start executing.
The EAX register will hold the syscall_num.
Other registers and the stack can be used to hold further arguments.
This commit is contained in:
Nigel Barink 2023-02-27 00:34:30 +01:00
parent 2d0bb16fad
commit 5781f730d9
7 changed files with 141 additions and 143 deletions

View File

@ -3,6 +3,9 @@
#include "../drivers/ps-2/keyboard.h"
#include "../i386/processor.h"
#include "../memory/VirtualMemoryManager.h"
#include "../syscalls.h"
IDT_entry idt_table[256];
IDT_ptr idt_ptr;
@ -18,7 +21,7 @@ void set_id_entry (uint8_t num , uint32_t base, uint16_t sel, uint8_t flags){
void irs_handler (registers* regs) {
uint32_t FaultingAddress;
//printf("(IRS) Interrupt number: %d \r", regs.int_no);
printf("(IRS) Interrupt number: %d \n EAX: ", regs->int_no, regs->eax);
switch (regs->int_no)
{
case 0:
@ -259,6 +262,29 @@ void irs_handler (registers* regs) {
printf("EBP: 0x%x\n", regs->ebp);
break;
case 50:
printf("SYSTEMCALL\n");
printf("EAX 0x%x\n", regs->eax);
switch (regs->eax) {
case 0x0:
printf("test!\n");
break;
case 0x5:
sys_open();
break;
case 0x10:
sys_read((FILE*)regs->ebx, (char*)regs->ecx);
break;
case 0x20:
sys_write((FILE*)regs->ebx, (const char*)regs->ecx, regs->edx);
break;
case 0x666:
sys_version();
break;
};
break;
default:
// PANIC!!!
break;
@ -269,9 +295,6 @@ void irs_handler (registers* regs) {
}
void irq_handler (registers regs) {
switch (regs.int_no) {
case 0:
pit_tick++;
@ -328,7 +351,7 @@ void irq_handler (registers regs) {
}
void initidt(){
// Initialise the IDT pointer
// Initialize the IDT pointer
idt_ptr.length = sizeof(IDT_entry) * 255;
idt_ptr.base = (uint32_t)&idt_table;
@ -371,7 +394,7 @@ void initidt(){
set_id_entry(30, (uint32_t) irs30 , 0x08, 0x8E);
set_id_entry(31, (uint32_t) irs31 , 0x08, 0x8E);
set_id_entry(0x50, (uint32_t) irs50, 0x08, 0x8E);
//print_serial("Remapping PIC\n");
PIC_remap(0x20, 0x28);

View File

@ -70,6 +70,7 @@ extern "C" {
extern void irs29 ();
extern void irs30 ();
extern void irs31 ();
extern void irs50();

View File

@ -1,115 +1,32 @@
.globl irq0
irq0:
cli
push $0
push $0
jmp irq_common
.globl irq1
irq1:
cli
push $0
push $1
jmp irq_common
.macro IRQ NAME, VECTOR
.globl irq\NAME
irq\NAME:
cli
push $0
push \VECTOR
jmp irq_common
.endm
.globl irq2
irq2:
cli
push $0
push $2
jmp irq_common
.globl irq3
irq3:
cli
push $0
push $3
jmp irq_common
.globl irq4
irq4:
cli
push $0
push $4
jmp irq_common
.globl irq5
irq5:
cli
push $0
push $5
jmp irq_common
.globl irq6
irq6:
cli
push $0
push $6
jmp irq_common
.globl irq7
irq7:
cli
push $0
push $7
jmp irq_common
.globl irq8
irq8:
cli
push $0
push $8
jmp irq_common
.globl irq9
irq9:
cli
push $0
push $9
jmp irq_common
.globl irq10
irq10:
cli
push $0
push $10
jmp irq_common
.globl irq11
irq11:
cli
push $0
push $11
jmp irq_common
IRQ 0 $0
IRQ 1 $1
IRQ 2 $2
IRQ 3 $3
IRQ 4 $4
IRQ 5 $5
IRQ 6 $6
IRQ 7 $7
IRQ 8 $8
IRQ 9 $9
IRQ 10 $10
IRQ 11 $11
IRQ 12 $12
IRQ 13 $13
IRQ 14 $14
IRQ 15 $15
.globl irq12
irq12:
cli
push $0
push $12
jmp irq_common
.globl irq13
irq13:
cli
push $0
push $13
jmp irq_common
.globl irq14
irq14:
cli
push $0
push $14
jmp irq_common
.globl irq15
irq15:
cli
push $0
push $15
jmp irq_common
irq_common:
pusha

View File

@ -53,7 +53,7 @@ ISR_NOERRORCODE 28 $28
ISR_NOERRORCODE 29 $29
ISR_NOERRORCODE 30 $30
ISR_NOERRORCODE 31 $31
ISR_NOERRORCODE 50 $50
irs_common:
pusha # Pushes edi,esi,ebp,esp,ebx,edx,ecx,eax
@ -74,7 +74,7 @@ irs_common:
call irs_handler
pop %eax
pop %eax // pop stack pointer
pop %ebx // reload ther orignal data segment descriptor
mov %bx, %ds
mov %bx, %es
@ -82,6 +82,6 @@ irs_common:
mov %bx, %gs
popa
add $12, %esp # cleans push error and irs code
add $8, %esp # cleans push error and irs code
iret # pops 5 things at once: CS, EIP, EFLAGS, SS, and ESP

View File

@ -9,23 +9,37 @@
#include "drivers/vga/VBE.h"
#include "pci/pci.h"
#include "drivers/pit/pit.h"
#include "acpi/acpi.h"
#include "i386/processor.h"
#include "terminal/kterm.h"
#include "interrupts/idt.h"
#include "serial.h"
#include "storage/vfs/vfs.h"
#include "storage/filesystems/FAT/FAT.h"
#include "../CoreLib/Memory.h"
#include "acpi/acpi.h"
#include "memory/VirtualMemoryManager.h"
extern BootInfoBlock* BIB;
extern "C" void LoadGlobalDescriptorTable();
extern "C" void jump_usermode();
extern BootInfoBlock* BIB;
void initBootDrive(){
printf("Boot device: 0x%x\n", BIB->bootDeviceID);
unsigned int part3 = BIB->bootDeviceID & 0xFF;
unsigned int part2 = (BIB->bootDeviceID & 0xFF00) >> 8;
unsigned int part1 = (BIB->bootDeviceID & 0xFF0000) >> 16;
unsigned int drive = (BIB->bootDeviceID & 0xFF000000) >> 24;
if (drive == 0x80 )
printf("booted from disk!\n");
if(drive == 0x00)
printf("booted from floppy disk\n");
printf("Part1: %d, Part2: %d, Part3: %d\n", part1, part2 , part3);
ATAPIO::Identify(ATAPIO_PORT::Primary, DEVICE_DRIVE::MASTER);
}
extern "C" void kernel ()
{
init_serial();
kterm_init();
@ -42,32 +56,21 @@ extern "C" void kernel ()
initHeap();
pit_initialise();
ACPI::initialize();
PCI::Scan();
processor::initialize();
processor::enable_protectedMode();
printf("Boot device: 0x%x\n", BIB->bootDeviceID);
unsigned int part3 = BIB->bootDeviceID & 0xFF;
unsigned int part2 = (BIB->bootDeviceID & 0xFF00) >> 8;
unsigned int part1 = (BIB->bootDeviceID & 0xFF0000) >> 16;
unsigned int drive = (BIB->bootDeviceID & 0xFF000000) >> 24;
if (drive == 0x80 )
printf("booted from disk!\n");
if(drive == 0x00)
printf("booted from floppy disk\n");
printf("Part1: %d, Part2: %d, Part3: %d\n", part1, part2 , part3);
ATAPIO::Identify(ATAPIO_PORT::Primary, DEVICE_DRIVE::MASTER);
initBootDrive();
VirtualFileSystem::initialize();
printf("Lets open hello.txt\n");
auto fontFile = VirtualFileSystem::open("/HELLO TXT", 0);
if(fontFile->flags == 0){
uint16_t* contents = (uint16_t*) malloc(sizeof(uint16_t) * 256);
fontFile->read(fontFile, contents, 512);
for(int i =0 ; i < fontFile->root->size; i++ ){
#ifdef VFS_EXAMPLE
auto fontFile = VirtualFileSystem::open("/FONT PSF", 0);
if(grubcfg->flags == 0){
uint16_t* contents = (uint16_t*) malloc(sizeof(uint16_t) * 256);
grubcfg->read(grubcfg, contents, 512);
for(int i =0 ; i < grubcfg->root->size; i++ ){
kterm_put(contents[i] & 0x00ff);
kterm_put(contents[i] >> 8);
}
@ -76,8 +79,7 @@ extern "C" void kernel ()
}else{
printf("Could not find file\n");
}
#endif
#ifdef USERMODE_RELEASE
// Lets jump into user mode

View File

@ -6,6 +6,8 @@
bool isRunning = true;
extern "C" void startSuperVisorTerminal()
{
/*
* Show a little banner for cuteness
*/
@ -62,7 +64,8 @@ extern "C" void startSuperVisorTerminal()
{
// Show version information
printf("========= Version ========\n");
printf("Kernel v%d\n", 0);
asm volatile ("movl $0x666, %eax; int $0x50");
}
if(strncmp("CLEAR", command, characterCount) == 0)

52
kernel/syscalls.h Normal file
View File

@ -0,0 +1,52 @@
//
// Created by nigel on 26/02/23.
//
#pragma once
#include "terminal/kterm.h"
#include "storage/vfs/vfs_types.h"
void sys_version (){
printf("KERNEL VERSION v0.4\n");
}
void sys_open(){
}
void sys_read(FILE* file, char* data){
file->read(file, data, 512);
}
void sys_write(FILE* path, const char* data, size_t size){
}
// NOTE: this should become our standard!
void syscall_handler(int syscall_no , uint32_t* args... ){
switch(syscall_no){
case 0x0:
printf("test!\n");
break;
case 0x5:
// SYS_OPEN
// sys_open();
break;
case 0x10:
// SYS_READ
// sys_read((FILE*)args[1], (char*) args[2] );
break;
case 0x20:
// SYS_WRITE
//sys_write((FILE*)args[1], (const char*) args[2], (size_t)args[3]);
break;
case 0x666:
// SYS_VERSION
sys_version();
break;
}
}