Skip to content

Commit 1e71d6c

Browse files
committed
Syscall to count files.
1 parent b9c5764 commit 1e71d6c

13 files changed

+137
-58
lines changed

Makefile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,18 @@ drivers/frame_buffer.o \
2929
drivers/keyboard.o \
3030
drivers/pic.o \
3131
drivers/serial_port.o \
32-
filesystem.o \
3332
interrupts.o \
33+
kernel_filesystem.o \
3434
kmain.o \
3535
multiboot_utils.o \
3636
process.o \
3737
stdio.o \
3838
stdlib.o \
39-
string.o
39+
string.o \
40+
syscalls.o
4041

41-
STDLIB = stdlib/assembly_functions.o \
42-
stdlib/stdio.o
42+
STDLIB = stdlib/stdio.o \
43+
stdlib/syscalls.o
4344

4445
all: os.iso
4546

asm_interrupts.s

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ global interrupt_handler_128
772772
interrupt_handler_128:
773773
push dword 0
774774
push dword 128
775-
jmp common_interrupt_handler
775+
jmp interrupt_handler_with_return_value
776776

777777
global interrupt_handler_129
778778
interrupt_handler_129:
@@ -1566,3 +1566,33 @@ common_interrupt_handler: ; the common parts of the generic interr
15661566

15671567
; return to the code that got interrupted
15681568
iret
1569+
1570+
interrupt_handler_with_return_value:
1571+
push eax
1572+
push ebx
1573+
push ecx
1574+
push edx
1575+
push esi
1576+
push edi
1577+
push ebp
1578+
mov eax, cr2
1579+
push eax
1580+
1581+
; call the C function
1582+
call interrupt_handler
1583+
1584+
; restore the registers
1585+
add esp, 4 ; cr2
1586+
pop ebp
1587+
pop edi
1588+
pop esi
1589+
pop edx
1590+
pop ecx
1591+
pop ebx
1592+
; don't pop eax. It contains the return value
1593+
1594+
; pop eax, error_code and interrupt_number off the stack
1595+
add esp, 12
1596+
1597+
; return to the code that got interrupted
1598+
iret

filesystem.h

Lines changed: 0 additions & 18 deletions
This file was deleted.

interrupts.c

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "loader.h"
99
#include "memory.h"
1010
#include "stdio.h"
11+
#include "syscalls.h"
1112

1213
void log_stack_trace_line(uint32_t eip) {
1314
char * symbol_name = address_to_symbol_name(eip);
@@ -36,29 +37,7 @@ void log_interrupt_details(char* int_name, uint32_t error_code, uint32_t eip, st
3637
fprintf(LOG, "--------------------\n");
3738
}
3839

39-
void sys_write_to_screen(char* s) {
40-
fprintf(LOG, "sys_write_to_screen(%s)\n", s);
41-
printf(s);
42-
}
43-
44-
void handle_syscall(struct cpu_state* cpu) {
45-
uint32_t syscall_num = cpu->eax;
46-
47-
fprintf(LOG, "--------------------\nSYSCALL (%i)\n", syscall_num);
48-
49-
switch (syscall_num) {
50-
case (1):
51-
sys_write_to_screen((char*) cpu->ebx);
52-
break;
53-
default:
54-
fprintf(LOG, "Unknown syscall: %x\n", syscall_num);
55-
while(1){}
56-
}
57-
58-
fprintf(LOG, "--------------------\n");
59-
}
60-
61-
void interrupt_handler(struct cpu_state cpu, uint32_t interrupt_number, uint32_t error_code, uint32_t eip) {
40+
uint32_t interrupt_handler(struct cpu_state cpu, uint32_t interrupt_number, uint32_t error_code, uint32_t eip) {
6241
switch(interrupt_number) {
6342
case(INT_KEYBOARD):
6443
keyboard_interrupt_handler();
@@ -100,8 +79,7 @@ void interrupt_handler(struct cpu_state cpu, uint32_t interrupt_number, uint32_t
10079
break;
10180

10281
case(INT_SYSCALL):
103-
handle_syscall(&cpu);
104-
break;
82+
return handle_syscall(&cpu);
10583

10684
case(INT_OUT_OF_MEMORY):
10785
log_interrupt_details("INT_OUT_OF_MEMORY", error_code, eip, &cpu);
@@ -116,4 +94,6 @@ void interrupt_handler(struct cpu_state cpu, uint32_t interrupt_number, uint32_t
11694
}
11795

11896
pic_acknowledge();
97+
98+
return 0;
11999
}

interrupts.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ struct cpu_state {
2222
} __attribute__((packed));
2323

2424
void enable_keyboard_interrupts();
25-
void interrupt_handler(struct cpu_state cpu, uint32_t interrupt_number, uint32_t error_code, uint32_t eip);
25+
uint32_t interrupt_handler(struct cpu_state cpu, uint32_t interrupt_number, uint32_t error_code, uint32_t eip);
2626

2727
#endif /* INCLUDE_INTERRUPTS_H */

filesystem.c renamed to kernel_filesystem.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "filesystem.h"
1+
#include "kernel_filesystem.h"
22

33
#include "memory.h"
44
#include "stdio.h"
@@ -74,3 +74,7 @@ struct file_t* get_file(char* name) {
7474

7575
return 0;
7676
}
77+
78+
struct file_t* get_first_file() {
79+
return first_file;
80+
}

kernel_filesystem.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef INCLUDE_KERNEL_FILESYSTEM_H
2+
#define INCLUDE_KERNEL_FILESYSTEM_H
3+
4+
#include "types.h"
5+
#include "multiboot.h"
6+
#include "stdlib/filesystem.h"
7+
8+
void initialize_filesystem(struct module* mbinfo);
9+
struct file_t* get_file(char* name);
10+
struct file_t* get_first_file();
11+
12+
#endif /* INCLUDE_KERNEL_FILESYSTEM_H */

stdlib/assembly_functions.h

Lines changed: 0 additions & 8 deletions
This file was deleted.

stdlib/syscalls.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef INCLUDE_SYSCALLS_H
2+
#define INCLUDE_SYSCALLS_H
3+
4+
#include "types.h"
5+
#include "filesystem.h"
6+
7+
void write_to_screen(char* s);
8+
uint32_t count_files();
9+
void list_files(struct file_t buffer[]);
10+
11+
#endif /* INCLUDE_SYSCALLS_H */

stdlib/assembly_functions.s renamed to stdlib/syscalls.s

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,12 @@ write_to_screen:
88
mov ebx, [esp+4]
99
int 0x80
1010
ret ; return to the calling function
11+
12+
global count_files
13+
; count_files
14+
; stack: [esp ] return address
15+
count_files:
16+
mov eax, 2
17+
int 0x80
18+
ret ; return to the calling function
19+

0 commit comments

Comments
 (0)