aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarvin Borner2020-08-09 21:55:42 +0200
committerMarvin Borner2020-08-09 21:55:42 +0200
commitf163a5d5f6802f63092229f0f9326e5fb44b7908 (patch)
treef24ccfe1b9bd340875d534e1aa19ef8676fd4d7b
parentb6d3d341c19440f8447d8d6c6567b7ff78db3174 (diff)
Added malloc/free syscall
-rw-r--r--Makefile5
-rw-r--r--apps/Makefile2
-rw-r--r--apps/a.c2
-rw-r--r--apps/init.c6
-rw-r--r--kernel/Makefile6
-rw-r--r--kernel/features/load.c4
-rw-r--r--kernel/features/proc.c1
-rw-r--r--kernel/features/syscall.c12
-rw-r--r--lib/Makefile15
-rw-r--r--lib/inc/mem.h13
-rw-r--r--lib/inc/sys.h2
-rw-r--r--lib/mem.c2
-rwxr-xr-xrun2
13 files changed, 52 insertions, 20 deletions
diff --git a/Makefile b/Makefile
index e2c8142..d78ed89 100644
--- a/Makefile
+++ b/Makefile
@@ -3,8 +3,11 @@
all: compile clean
compile:
- @$(MAKE) --no-print-directory -C lib/
+ @$(MAKE) libc --no-print-directory -C lib/
@echo "Compiled libc"
+ @$(MAKE) clean --no-print-directory -C lib/
+ @$(MAKE) libk --no-print-directory -C lib/
+ @echo "Compiled libk"
@$(MAKE) --no-print-directory -C kernel/
@echo "Compiled kernel"
@$(MAKE) --no-print-directory -C apps/
diff --git a/apps/Makefile b/apps/Makefile
index 8c5ecac..d6c3789 100644
--- a/apps/Makefile
+++ b/apps/Makefile
@@ -5,7 +5,7 @@ CC = ../cross/opt/bin/i686-elf-gcc
LD = ../cross/opt/bin/i686-elf-ld
OC = ../cross/opt/bin/i686-elf-objcopy
-CFLAGS = $(CSFLAGS) -Wall -Wextra -nostdlib -nostdinc -ffreestanding -ffunction-sections -fno-builtin -std=c99 -m32 -pedantic-errors -I../lib/inc/ -fPIE
+CFLAGS = $(CSFLAGS) -Wall -Wextra -nostdlib -nostdinc -ffreestanding -ffunction-sections -fno-builtin -std=c99 -m32 -pedantic-errors -I../lib/inc/ -fPIE -Duserspace
all: $(COBJS)
diff --git a/apps/a.c b/apps/a.c
index 5fef114..448bedd 100644
--- a/apps/a.c
+++ b/apps/a.c
@@ -7,7 +7,7 @@
void main()
{
print("\nA loaded!\n");
- sys0(SYS_HALT);
+ sys0(SYS_LOOP);
while (1) {
print("a");
}
diff --git a/apps/init.c b/apps/init.c
index 2851e8e..813021b 100644
--- a/apps/init.c
+++ b/apps/init.c
@@ -1,13 +1,17 @@
// MIT License, Copyright (c) 2020 Marvin Borner
#include <def.h>
+#include <mem.h>
#include <print.h>
#include <sys.h>
void main()
{
print("Init loaded.\n");
- sys1(SYS_EXEC, (int)"/a");
+
+ printf("%x %d %b\n ABC %s", 42, 42, 42, "BAUM");
+ sys0(SYS_LOOP);
+ /* sys1(SYS_EXEC, (int)"/a"); */
while (1) {
print("b");
};
diff --git a/kernel/Makefile b/kernel/Makefile
index ed422a5..1d47ffd 100644
--- a/kernel/Makefile
+++ b/kernel/Makefile
@@ -22,7 +22,7 @@ AS = nasm
# Flags to make the binary smaller TODO: Remove after indirect pointer support!
CSFLAGS = -mpreferred-stack-boundary=2 -fno-asynchronous-unwind-tables -Os
-CFLAGS = $(CSFLAGS) -Wall -Wextra -nostdlib -nostdinc -ffreestanding -fno-builtin -mgeneral-regs-only -std=c99 -m32 -pedantic-errors -Wl,-ekernel_main -I../lib/inc/ -Iinc/
+CFLAGS = $(CSFLAGS) -Wall -Wextra -nostdlib -nostdinc -ffreestanding -fno-builtin -mgeneral-regs-only -std=c99 -m32 -pedantic-errors -Wl,-ekernel_main -I../lib/inc/ -Iinc/ -Dkernel
ASFLAGS = -f elf32 -O3
@@ -37,5 +37,5 @@ all: compile
compile: $(COBJS)
@mkdir -p ../build/
@$(AS) -f bin entry.asm -o ../build/boot.bin
- @$(LD) -N -ekernel_main -Ttext 0x00050000 -o ../build/kernel.bin -L../build/ $+ -lc --oformat binary
- @$(CC) $(CFLAGS) -o ../build/debug.o -L../build/ $+ -lc
+ @$(LD) -N -ekernel_main -Ttext 0x00050000 -o ../build/kernel.bin -L../build/ $+ -lk --oformat binary
+ @$(CC) $(CFLAGS) -o ../build/debug.o -L../build/ $+ -lk
diff --git a/kernel/features/load.c b/kernel/features/load.c
index cc1a094..14a3086 100644
--- a/kernel/features/load.c
+++ b/kernel/features/load.c
@@ -2,10 +2,9 @@
#include <def.h>
#include <fs.h>
-#include <load.h>
#include <mem.h>
-#include <print.h>
#include <proc.h>
+#include <str.h>
void bin_load(char *path, struct proc *proc)
{
@@ -16,4 +15,5 @@ void bin_load(char *path, struct proc *proc)
proc->regs.esp = (u32)stack;
proc->regs.useresp = (u32)stack;
proc->regs.eip = (u32)data;
+ strcpy(proc->name, path + 1);
}
diff --git a/kernel/features/proc.c b/kernel/features/proc.c
index a14aaea..64a4ac1 100644
--- a/kernel/features/proc.c
+++ b/kernel/features/proc.c
@@ -95,7 +95,6 @@ void proc_init()
root = proc_make();
bin_load("/init", root);
- strcpy(root->name, "root");
proc_print();
_eip = root->regs.eip;
diff --git a/kernel/features/syscall.c b/kernel/features/syscall.c
index 1a43b9e..eaa09b3 100644
--- a/kernel/features/syscall.c
+++ b/kernel/features/syscall.c
@@ -3,6 +3,7 @@
#include <cpu.h>
#include <interrupts.h>
#include <load.h>
+#include <mem.h>
#include <print.h>
#include <proc.h>
#include <str.h>
@@ -15,15 +16,22 @@ void syscall_handler(struct regs *r)
printf("[SYSCALL] %d\n", num);
switch (num) {
- case SYS_HALT: {
+ case SYS_LOOP: {
loop();
break;
}
+ case SYS_MALLOC: {
+ r->eax = (u32)malloc(r->eax);
+ break;
+ }
+ case SYS_FREE: {
+ free(r->eax);
+ break;
+ }
case SYS_EXEC: {
char *path = (char *)r->ebx;
struct proc *proc = proc_make();
bin_load(path, proc);
- strcpy(proc->name, path);
proc_print();
break;
}
diff --git a/lib/Makefile b/lib/Makefile
index 730ed6d..ef56b3a 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -18,11 +18,18 @@ CSFLAGS = -mpreferred-stack-boundary=2 -fno-asynchronous-unwind-tables -Os
CFLAGS = $(CSFLAGS) -Wall -Wextra -nostdlib -nostdinc -ffreestanding -fno-builtin -mgeneral-regs-only -std=c99 -m32 -pedantic-errors -Iinc/
-all: compile
-
%.o: %.c
@$(CC) -c $(CFLAGS) $< -o $@
-compile: $(COBJS)
+libc: CFLAGS += -Duserspace
+libc: $(COBJS)
+ @mkdir -p ../build/
+ @$(AR) qc ../build/libc.a $+
+
+libk: CFLAGS += -Dkernel
+libk: $(COBJS)
@mkdir -p ../build/
- @$(AR) qc ../build/libc.a $(COBJS)
+ @$(AR) qc ../build/libk.a $+
+
+clean:
+ @find . -name "*.o" -type f -delete
diff --git a/lib/inc/mem.h b/lib/inc/mem.h
index e2d574f..9399b54 100644
--- a/lib/inc/mem.h
+++ b/lib/inc/mem.h
@@ -5,8 +5,17 @@
#include <def.h>
-#define malloc(n) ((void *)((HEAP += n) - n)) // TODO: Implement real/better malloc/free
-#define free(x)
+// Huh
+#ifdef kernel
+#define malloc(n) (void *)((HEAP += n) - n) // TODO: Implement real/better malloc/free
+#define free(ptr)
+#elif defined(userspace)
+#include <sys.h>
+#define malloc(n) (void *)sys1(SYS_MALLOC, n)
+#define free(ptr) (void)(sys1(SYS_FREE, (int)ptr))
+#else
+#error "No lib target specified. Please use -Dkernel or -Duserspace"
+#endif
// TODO: Use malloc as syscall
u32 HEAP;
diff --git a/lib/inc/sys.h b/lib/inc/sys.h
index aaeb6ca..aefaead 100644
--- a/lib/inc/sys.h
+++ b/lib/inc/sys.h
@@ -4,7 +4,7 @@
#ifndef SYS_H
#define SYS_H
-enum sys { SYS_HALT, SYS_EXEC };
+enum sys { SYS_LOOP, SYS_MALLOC, SYS_FREE, SYS_EXEC };
int sys0(enum sys num);
int sys1(enum sys num, int d1);
diff --git a/lib/mem.c b/lib/mem.c
index 00b9735..58c337b 100644
--- a/lib/mem.c
+++ b/lib/mem.c
@@ -1,6 +1,8 @@
// MIT License, Copyright (c) 2020 Marvin Borner
#include <def.h>
+#include <print.h>
+#include <sys.h>
void *memcpy(void *dst, const void *src, u32 n)
{
diff --git a/run b/run
index 6787fc9..d346a84 100755
--- a/run
+++ b/run
@@ -123,7 +123,7 @@ make_sync() {
make --always-make --dry-run |
grep -wE 'gcc' |
grep -w '\-c' |
- jq -nR '[inputs|{directory:"'"$(pwd)"'/src/", command:., file: match(" [^ ]+$").string[1:]}]' \
+ jq -nR '[inputs|{directory:"'"$(pwd)"'/kernel/", command:., file: match(" [^ ]+$").string[1:]}]' \
>compile_commands.json
}