Skip to content

Forward argv on new process creation #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ binaries: $(bt_stage1) $(bt_stage2) $(kernel_core) $(rm_static)
SECTOR_COUNT_BT_STAGE1 = 1
SECTOR_COUNT_SHARED_LIBRARY = 1
SECTOR_COUNT_BT_STAGE2 = 11
SECTOR_COUNT_KERNEL = 44
SECTOR_COUNT_KERNEL = 47

SECTOR_START_BT_STAGE1 = 0
SECTOR_START_SHARED_LIBRARY = $(shell expr $(SECTOR_START_BT_STAGE1) + $(SECTOR_COUNT_BT_STAGE1) )
Expand Down Expand Up @@ -141,6 +141,7 @@ include $(SRC_DRIVERS)/pic/Makefile.mk

include $(SRC_DIR)/fs/Makefile.mk
include $(SRC_DIR)/memmgr/tables/Makefile.mk
include $(SRC_DIR)/memmgr/stackguard/Makefile.mk

include $(SRC_LIB)/app/Makefile.mk
include $(SRC_LIB_DS)/Makefile.mk
Expand Down
3 changes: 2 additions & 1 deletion include/fuzzy/kernel/interrupts/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ void interrupt_pit_enable();

void interrupt_register_0x20_irq0_pit();

int create_infant_process_irq0_stack(int ds_ss_es_fs);
// return new user_sp
int create_infant_process_irq0_stack(int user_datasegment, int user_sp);
9 changes: 5 additions & 4 deletions include/fuzzy/kernel/process/process.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#pragma once
#include <process.h>
#include<stddef.h>

#define MAX_PROCESS 10
// GDT_TABLE_SIZE = MAX_PROCESS*2+3
#define GDT_TABLE_SIZE 23
#define GDT_TABLE_SIZE MAX_PROCESS*2+3

typedef char ARGV[PROCESS_MAX_ARGC][PROCESS_MAX_ARG_LEN];

enum process_state{
STATE_COLD = 0, // STATE_COLD must be 0
Expand Down Expand Up @@ -36,7 +38,7 @@ int get_idt_ds_entry(int process_id);
int get_idt_reverse_pid_lookup(int cs);

// process create or kill
int process_create();
int process_create(int argc, char *argv[]);
void process_kill(int user_ds, int status);

// scheduler
Expand All @@ -45,7 +47,6 @@ void process_kill(int user_ds, int status);
// cs:eip with ss:esp (updated or not).
void process_scheduler(int *_e_ip, int *_e_cs, int *_e_sp, int *_e_ss);


// user space <-> kernel space data transfer helper
extern void syscall_strncpy_user_to_kernel(int user_ds, char *src_es_address, char *dest_ds_address, size_t size);
extern void syscall_strncpy_kernel_to_user(int user_ds, char *dest_address, char *src_address, size_t size);
1 change: 1 addition & 0 deletions include/fuzzy/memmgr/layout.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

// START_ENSURE_SAME_layout_asm
#define MEMORY_APP_SIZE 0x20000
#define STACKINIT_APP (MEMORY_APP_SIZE-4)
// END_ENSURE_SAME_layout_asm

// Most of the memory layout relies on the fact the kernel is first app
Expand Down
9 changes: 9 additions & 0 deletions include/fuzzy/memmgr/stackguard/stackguard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#define __MACRO_TO_STRING_INTERNAL(x) #x
#define MACRO_TO_STRING(x) __MACRO_TO_STRING_INTERNAL(x)
#define __STR__LINE__ MACRO_TO_STRING(__LINE__)

#define VERIFY_STACKGUARD() (verify_stack_guard(__FILE__ "[" __STR__LINE__ "]; verify_stack_guard failed"))

void verify_stack_guard(char err_message[]);
9 changes: 4 additions & 5 deletions memory_layout.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
|--------- |-------- |-------- |-------------------------------------------- |
| 0x00000 | fill | 31KB | RESERVED |
| 0x7C00 | 0x7DFF | 512B | BOOTLOADER STAGE 1 |
| 0x7E00 | 0x7EFF | 256B | SHARED STATIC CODE for real_mode library |
| 0x7F00 | 0x7FFF | 256B | SHARED STATIC MEMORY for real_mode library |
| 0x7E00 | 0x7FFF | 512B | SHARED STATIC CODE for real_mode library |
| 0x8000 | 0xBFFF | - | BOOT LOADER STAGE 2 + own stack |
| 0xC000 | 0x10200 | - | KERNEL |
| 0x20000 | 0x2FFFF | - | Application 0 |
| 0x30000 | 0x3FFFF | - | Application 1 |
| 0x10000 | 0x2FFFF | - | KERNEL |
| 0x30000 | 0x4FFFF | - | Application 0 |
| 0x50000 | 0x6FFFF | - | Application 1 |
...
| - | 0xFFFFF | - | 20-bit memory limit |
8 changes: 6 additions & 2 deletions src/kernel/Makefile.mk
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ $(SELF_BUILD_DIR)/%_asm.o: $(SELF_SRC_DIR)/%.asm
mkdir -p $(dir $@)
$(NASM) -o $@ -i $(SRC_REALMODE)/ $<

$(kernel_core).elf: $(SELF_BUILD_DIR)/core_asm.o $(SELF_BUILD_DIR)/panic_asm.o $(SELF_BUILD_ALL_C) \
$(kernel_core).elf: $(SELF_BUILD_DIR)/core_asm.o \
$(SELF_BUILD_DIR)/panic_asm.o \
$(SELF_BUILD_ALL_C) \
$(BUILD_KERNEL)/interrupts/libinterrupts \
$(BUILD_KERNEL)/syscall/libsyscall \
$(BUILD_KERNEL)/process/libprocess \
Expand All @@ -26,7 +28,9 @@ $(kernel_core).elf: $(SELF_BUILD_DIR)/core_asm.o $(SELF_BUILD_DIR)/panic_asm.o $
$(BUILD_LIB_DS)/libds \
$(BUILD_DRIVERS)/disk/libdisk \
$(BUILD_DIR)/real_mode/librealmodeclient \
$(BUILD_USR_LIB)/libfuzzyc
$(BUILD_USR_LIB)/libfuzzyc \
$(BUILD_DIR)/memmgr/stackguard/libstackguard # stackguard must be the last one

mkdir -p $(dir $@)
$(KERNEL_LD) -o $@ $^

Expand Down
8 changes: 6 additions & 2 deletions src/kernel/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
#include <fuzzy/kernel/interrupts/interrupts.h>
#include <fuzzy/kernel/interrupts/timer.h>
#include <fuzzy/kernel/process/process.h>
#include <fuzzy/memmgr/stackguard/stackguard.h>

#include <string.h>
#include <process.h>
#include <stddef.h>
#include <sys/syscall.h>
#include <conio.h>

Expand All @@ -32,6 +34,7 @@ void kernel_core_entry() {
set_color_bg(C_BLUE);
set_color_fg(C_WHITE);
print_rectangle(0, 0, TEXT_WINDOW_WIDTH-1, TEXT_WINDOW_HEIGHT-1);
move_xy(0, 0);
print_log("Initializing Kernel");


Expand All @@ -48,8 +51,9 @@ void kernel_core_entry() {

clrscr();

int init_pid = spawn(INIT_APPNAME);
print_log("init process created: %d", init_pid);
VERIFY_STACKGUARD();
int init_pid = spawnl(INIT_APPNAME, INIT_APPNAME, NULL);
print_log("init process got created: %d", init_pid);

// interrupt_pit_enable();
while (1);
Expand Down
9 changes: 8 additions & 1 deletion src/kernel/interrupts/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,17 @@ extern void _interrupt_handler_0x1D_exception();
extern void _interrupt_handler_0x1E_exception();
extern void _interrupt_handler_0x1F_exception();

void interrupt_handler_0x00_0x1F_exception(int id,int ip, int cs) {
void interrupt_handler_0x00_0x1F_exception(int id, int ip, int cs) {
switch (id) {
case 0x0D:
PANIC(id, "[hw_exception] general_protection_fault");
}
PANIC(id, "[hw_exception] triggered: no handler");
}

void interrupt_handler_0x0D_general_protection_fault(int id, int ip, int cs) {
}

void interrupt_register_0x00_0x1F_exceptions() {
print_log("[interrupts] register 0x00-0x1F exceptions");
populate_idt_entry_32bit(0x00, (unsigned int)_interrupt_handler_0x00_exception, 0, 0);
Expand Down
6 changes: 3 additions & 3 deletions src/kernel/interrupts/timer.asm
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ global create_infant_process_irq0_stack
push edi

push ds
mov ecx, [ebp+0x08] ; arg0
mov ecx, [ebp+0x08] ; arg0: ss
mov eax, [ebp+0x0C] ; arg1; user stack esp

mov ds, ecx
sub eax, 4

; user initial stack
mov eax, STACKINIT_APP
; kernel offset
xor ecx, ecx
mov [eax-0], ecx ; user: eflag
Expand Down
2 changes: 2 additions & 0 deletions src/kernel/interrupts/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <fuzzy/kernel/interrupts/interrupts.h>
#include <fuzzy/kernel/interrupts/timer.h>
#include <fuzzy/kernel/process/process.h>
#include <fuzzy/memmgr/stackguard/stackguard.h>

#include <lib/utils/logging.h>

Expand Down Expand Up @@ -39,6 +40,7 @@ void irq0_pit_handler(int *e_ip, int *e_cs, int *e_sp, int *e_ss) {
timer_add_ticks(ticks_jumped);
int newtime_ms = get_time_since_boot_ms();
process_scheduler(e_ip, e_cs, e_sp, e_ss);
VERIFY_STACKGUARD();
}

void interrupt_pit_enable() {
Expand Down
40 changes: 37 additions & 3 deletions src/kernel/process/allocation.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
#include <fuzzy/memmgr/layout.h>
#include <fuzzy/kernel/panic.h>

#include <process.h>

/* Process ID
* pid 0 : kernel core
* pid 1 : user app 1
* pid 2 : user app 2 and so on...
*
* Process Cycle
* - process_create()
* - process_create(...)
* - process_load_from_disk()
* - OR process_load_from_ram()
*/
Expand Down Expand Up @@ -88,7 +90,38 @@ void process_scheduler_init() {
load_gdt_table(&gdtr);
}

int process_create() {
// return new user_sp
static int create_infant_process_argv_stack(int user_ds, int user_sp,
int argc, char *argv[]) {
// assumes pointer are 4 bytes and are same as int

user_sp = user_sp - sizeof(ARGV);
char *__us_argv_data = user_sp;
syscall_strncpy_kernel_to_user(user_ds, __us_argv_data, argv, sizeof(ARGV));

// assumes argc < PROCESS_MAX_ARGC
char *ks_to_us_argv[PROCESS_MAX_ARGC] = {NULL};
for (int i = 0; i < argc; i++) {
ks_to_us_argv[i] = __us_argv_data + i*PROCESS_MAX_ARG_LEN;
}


user_sp = user_sp - sizeof(ks_to_us_argv);
char *__us_argv_list = user_sp;
syscall_strncpy_kernel_to_user(user_ds, __us_argv_list, ks_to_us_argv, sizeof(ks_to_us_argv));

user_sp = user_sp - sizeof(argc);
char *__us_argv = user_sp;
syscall_strncpy_kernel_to_user(user_ds, __us_argv, &__us_argv_list, sizeof(__us_argv_list));

user_sp = user_sp - sizeof(argc);
char *__us_argc = user_sp;
syscall_strncpy_kernel_to_user(user_ds, __us_argc, &argc, sizeof(argc));

return user_sp;
}

int process_create(int argc, char *argv[]) {
// returnd pid >= 0 if success
int pid = -1;
for (int i = 0; i < MAX_PROCESS; ++i) {
Expand Down Expand Up @@ -129,7 +162,8 @@ int process_create() {
process->ip = 0;
// initially ds == ss
process->ss = get_gdt_number_from_entry_id(idt_ds_entry);
process->sp = create_infant_process_irq0_stack(process->ss);
process->sp = create_infant_process_argv_stack(process->ss, STACKINIT_APP, argc, argv);
process->sp = create_infant_process_irq0_stack(process->ss, process->sp);
return pid;
}

Expand Down
34 changes: 25 additions & 9 deletions src/kernel/process/process.c
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
#include <fuzzy/fs/ffs.h>
#include <fuzzy/kernel/process/process.h>
#include <fuzzy/kernel/interrupts/timer.h>
#include <fuzzy/memmgr/stackguard/stackguard.h>

#include <process.h>

#include <lib/utils/logging.h>
#include <lib/utils/output.h>

extern int call_main(int cs, int ds, int argc, char *argv[]);

int process_spawn(int lba_index, int sector_count) {
int process_spawn(int lba_index, int sector_count, int argc, char *argv[]) {
print_info("[process_spawn] create");
int pid = process_create();
int pid = process_create(argc, argv);
if(pid<0) {
print_log("Failed to reserved a new pid");
return -1;
}
print_info("[process_spawn] loading, pid: %d", pid);

int err = process_load_from_disk(pid, lba_index, sector_count);
if (err) {
print_log("Failed to load app in memory, Error: ", err);
Expand All @@ -25,6 +25,7 @@ int process_spawn(int lba_index, int sector_count) {
print_info("[process_spawn] ready, pid: %d", pid);
struct Process *process = get_process(pid);
process->state = STATE_READY;
VERIFY_STACKGUARD();
return 0;
}

Expand All @@ -39,17 +40,31 @@ int syscall_1_process_exit(int user_ds, int status) {
}

int syscall_1_process_spawn_lba_sc(int lba_start, int sector_count) {
return process_spawn(lba_start, sector_count);
int fake_argc = 1;
ARGV fake_argv={"fake_spawn", NULL};
return process_spawn(lba_start, sector_count, fake_argc, fake_argv);
}

int syscall_1_process_exec_lba_sc(int lba_start, int sector_count) {
return process_exec(lba_start, sector_count);
}

int syscall_1_process_spawn_fname(int user_ds, char *_us_filename) {
int syscall_1_process_spawn_fname(int user_ds, char *_us_filename, char *_us_argv[]) {
// User must send all PROCESS_MAX_ARGC arguments.
char *argv_with_uspointer[PROCESS_MAX_ARGC];
char filename[FS_FFS_FILENAME_LIMIT];
int argc = 0; // kernel mode
ARGV argv={0}; // kernel mode
syscall_strncpy_user_to_kernel(user_ds, _us_filename, filename, sizeof(filename));

syscall_strncpy_user_to_kernel(user_ds, _us_argv, argv_with_uspointer, sizeof(argv_with_uspointer));
// if src string is NULL, then dst string also should be null.
for (int i = 0; i < PROCESS_MAX_ARGC - 1; i++) {
if(argv_with_uspointer[i]==NULL) {
break;
}
syscall_strncpy_user_to_kernel(user_ds, argv_with_uspointer[i], argv[i], sizeof(argv[i]));
argc++;
}

union FFSFileEntry entry;
int file_id = file_handler_find(filename, &entry);
Expand All @@ -58,7 +73,8 @@ int syscall_1_process_spawn_fname(int user_ds, char *_us_filename) {

int lba_start = resolve_abs_lba(FFS_UNIQUE_PARITION_ID, entry.content.start_block_id);
int sector_count = (entry.content.filesize + FS_BLOCK_SIZE -1)/FS_BLOCK_SIZE;
return syscall_1_process_spawn_lba_sc(lba_start, sector_count);

return process_spawn(lba_start, sector_count, argc, argv);
}

int syscall_1_process(int operation, int a0, int a1, int a2, int a3, int user_ds) {
Expand All @@ -71,7 +87,7 @@ int syscall_1_process(int operation, int a0, int a1, int a2, int a3, int user_ds
case SYSCALL_PROCESS_SUB_EXEC_LBA_SC:
return syscall_1_process_exec_lba_sc(a0, a1);
case SYSCALL_PROCESS_SUB_SPAWN_FNAME:
return syscall_1_process_spawn_fname(user_ds, (char*)a0);
return syscall_1_process_spawn_fname(user_ds, (char*)a0, (char**)a1);
}
return -1;
}
4 changes: 3 additions & 1 deletion src/kernel/syscall/file_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ int fh_switch(int operation) {
}

int file_handler_find(char *filename, union FFSFileEntry *entry) {
// search for filename case insensitive as keyboard driver currently
// doesn't support shift or caps lock.
int file_id = 0;
while (file_id < FS_FFS_FILEENTRY_COUNT) {
int err = fetch_file_entry(
FFS_UNIQUE_PARITION_ID, file_id, entry);
if(!err && strcmp(filename, entry->content.filename)==0) {
if(!err && strcmpi(filename, entry->content.filename)==0) {
// match
return file_id;
}
Expand Down
10 changes: 10 additions & 0 deletions src/memmgr/stackguard/Makefile.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
$(SELF_BUILD_DIR)/%.o: $(SELF_SRC_DIR)/%.c $(BUILD_USR_INCLUDE_ALL)
mkdir -p $(dir $@)
$(KERNEL_CC) -c -o $@ $<

$(SELF_BUILD_DIR)/%_asm.o: $(SELF_SRC_DIR)/%.asm
mkdir -p $(dir $@)
nasm -o $@ -f elf32 $<

$(SELF_BUILD_DIR)/libstackguard: $(SELF_BUILD_ALL_C) $(SELF_BUILD_ALL_ASM)
ar rc $@ $^
Loading