Skip to content

Commit 68a9ea0

Browse files
committed
added _exit system call
1 parent e564934 commit 68a9ea0

File tree

9 files changed

+42
-3
lines changed

9 files changed

+42
-3
lines changed

kernel/idt/__0x80/__0x80.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ void __0x80_init()
3131
__0x80_add_function(__NR_3_MALLOC,__0x80_MALLOC);
3232
__0x80_add_function(__NR_4_FREE, __0x80_FREE);
3333
__0x80_add_function(__NR_5_EXEC, __0x80_EXEC);
34+
__0x80_add_function(__NR_6_EXIT, __0x80_EXIT);
3435
}
3536

3637

kernel/idt/__0x80/__0x80.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ enum __0x80_COMMANDS{
1818
__NR_2_PUTCH,
1919
__NR_3_MALLOC,
2020
__NR_4_FREE,
21-
__NR_5_EXEC
21+
__NR_5_EXEC,
22+
__NR_6_EXIT
2223

2324
};
2425

kernel/idt/__0x80/process/process.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,16 @@ void* __0x80_EXEC(struct interrupt_frame* iframe){
1919
return (void*) -ERR_PROCESS_CREATION_FAILED;
2020
}
2121

22+
process_switch(_process);
2223
task_switch(_process->task);
2324
enter_task(&_process->task->registers);
2425

2526
return (void*) SUCCESS;
2627

28+
}
29+
30+
void* __0x80_EXIT(struct interrupt_frame* iframe){
31+
process_kill(process_current());
32+
process_back_to_gshell();
33+
return (void*) SUCCESS;
2734
}

kernel/idt/__0x80/process/process.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "../../idt.h"
55

66
void* __0x80_EXEC(struct interrupt_frame* iframe);
7+
void* __0x80_EXIT(struct interrupt_frame* iframe);
78

89

910
#endif

kernel/task/process/process.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ void process_switch(struct process* _process){
164164
current_process = _process;
165165
}
166166

167+
void process_back_to_gshell(){
168+
process_switch(process_list[0]);
169+
task_switch(process_list[0]->task);
170+
enter_task(&process_list[0]->task->registers);
171+
}
172+
167173
static int8_t process_map_memory(struct process* _process){
168174

169175
// if file type is binary
@@ -312,7 +318,7 @@ int process_kill(struct process* _process){
312318

313319
// free all data dynamically allocated by the process.
314320
for(int allocation = 0;allocation < PROCESS_MAX_PROCESS_MEM_ALLOCATIONS; allocation++){
315-
process_free(_process->mem_allocations[allocation]);
321+
if(_process->mem_allocations[allocation] != 0) _process->mem_allocations[allocation];
316322
}
317323

318324
// mark pid as free to use

kernel/task/process/process.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
struct process *process_get(uint16_t pid);
1515
struct process *process_new(const char *filename);
1616
struct process *process_current();
17+
void process_switch(struct process* _process);
1718
void* process_malloc(uint32_t size);
1819
void* process_free(void* base_addr);
1920
int process_kill(struct process* _process);
21+
void process_back_to_gshell();
2022

2123
void process_init();
2224

usrspc/lib/Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
OBJ = ./build/_start.s.o ./build/stdio.s.o ./build/stdlib.s.o ./build/stdio.o ./build/stdlib.o ./build/unistd.s.o \
2-
./build/string.o
2+
./build/string.o ./build/_exit.s.o
33
INC = -I./include
44
FLAGS = -g -ffreestanding -falign-jumps -falign-functions -falign-labels -falign-loops -fstrength-reduce -fomit-frame-pointer -finline-functions -Wno-unused-function -fno-builtin -Werror -Wno-unused-label -Wno-cpp -Wno-unused-parameter -Wno-unused-variable -Wno-unused-value -nostdlib -nostartfiles -nodefaultlibs -Wall -O0 -Iinc
55

@@ -9,6 +9,9 @@ all: $(OBJ)
99
./build/_start.s.o: ./_start/_start.s
1010
nasm -f elf ./_start/_start.s -o ./build/_start.s.o
1111

12+
./build/_exit.s.o: ./_exit/_exit.s;
13+
nasm -f elf ./_exit/_exit.s -o ./build/_exit.s.o
14+
1215
./build/stdio.s.o: ./stdio/stdio.s
1316
nasm -f elf ./stdio/stdio.s -o ./build/stdio.s.o
1417

usrspc/lib/_exit/_exit.s

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[BITS 32]
2+
3+
section .asm
4+
5+
global _exit
6+
7+
_exit:
8+
9+
push ebp
10+
mov ebp, esp
11+
12+
mov eax, 6 ; call __0x80_EXIT system call handler
13+
int 0x80
14+
15+
pop ebp
16+
ret

usrspc/lib/_start/_start.s

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ section .asm
44

55
global _start
66
extern main
7+
extern _exit
78

89
_start:
910

1011
call main
12+
call _exit
1113
ret

0 commit comments

Comments
 (0)