Skip to content

Commit

Permalink
added _exit system call
Browse files Browse the repository at this point in the history
  • Loading branch information
yuvraj1803 committed May 18, 2023
1 parent e564934 commit 68a9ea0
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 3 deletions.
1 change: 1 addition & 0 deletions kernel/idt/__0x80/__0x80.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ void __0x80_init()
__0x80_add_function(__NR_3_MALLOC,__0x80_MALLOC);
__0x80_add_function(__NR_4_FREE, __0x80_FREE);
__0x80_add_function(__NR_5_EXEC, __0x80_EXEC);
__0x80_add_function(__NR_6_EXIT, __0x80_EXIT);
}


Expand Down
3 changes: 2 additions & 1 deletion kernel/idt/__0x80/__0x80.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ enum __0x80_COMMANDS{
__NR_2_PUTCH,
__NR_3_MALLOC,
__NR_4_FREE,
__NR_5_EXEC
__NR_5_EXEC,
__NR_6_EXIT

};

Expand Down
7 changes: 7 additions & 0 deletions kernel/idt/__0x80/process/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,16 @@ void* __0x80_EXEC(struct interrupt_frame* iframe){
return (void*) -ERR_PROCESS_CREATION_FAILED;
}

process_switch(_process);
task_switch(_process->task);
enter_task(&_process->task->registers);

return (void*) SUCCESS;

}

void* __0x80_EXIT(struct interrupt_frame* iframe){
process_kill(process_current());
process_back_to_gshell();
return (void*) SUCCESS;
}
1 change: 1 addition & 0 deletions kernel/idt/__0x80/process/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "../../idt.h"

void* __0x80_EXEC(struct interrupt_frame* iframe);
void* __0x80_EXIT(struct interrupt_frame* iframe);


#endif
8 changes: 7 additions & 1 deletion kernel/task/process/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ void process_switch(struct process* _process){
current_process = _process;
}

void process_back_to_gshell(){
process_switch(process_list[0]);
task_switch(process_list[0]->task);
enter_task(&process_list[0]->task->registers);
}

static int8_t process_map_memory(struct process* _process){

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

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

// mark pid as free to use
Expand Down
2 changes: 2 additions & 0 deletions kernel/task/process/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
struct process *process_get(uint16_t pid);
struct process *process_new(const char *filename);
struct process *process_current();
void process_switch(struct process* _process);
void* process_malloc(uint32_t size);
void* process_free(void* base_addr);
int process_kill(struct process* _process);
void process_back_to_gshell();

void process_init();

Expand Down
5 changes: 4 additions & 1 deletion usrspc/lib/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
OBJ = ./build/_start.s.o ./build/stdio.s.o ./build/stdlib.s.o ./build/stdio.o ./build/stdlib.o ./build/unistd.s.o \
./build/string.o
./build/string.o ./build/_exit.s.o
INC = -I./include
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

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

./build/_exit.s.o: ./_exit/_exit.s;
nasm -f elf ./_exit/_exit.s -o ./build/_exit.s.o

./build/stdio.s.o: ./stdio/stdio.s
nasm -f elf ./stdio/stdio.s -o ./build/stdio.s.o

Expand Down
16 changes: 16 additions & 0 deletions usrspc/lib/_exit/_exit.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[BITS 32]

section .asm

global _exit

_exit:

push ebp
mov ebp, esp

mov eax, 6 ; call __0x80_EXIT system call handler
int 0x80

pop ebp
ret
2 changes: 2 additions & 0 deletions usrspc/lib/_start/_start.s
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ section .asm

global _start
extern main
extern _exit

_start:

call main
call _exit
ret

0 comments on commit 68a9ea0

Please sign in to comment.