aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarvin Borner2021-05-18 18:37:19 +0200
committerMarvin Borner2021-05-18 18:37:50 +0200
commit4b4bfec8a312132acc84b5166998d0cfa7c01931 (patch)
treeeeb494cf539b4bfdb6dae23a34f5f1c5d7167667
parenteb13f2a8f536fecf918699bc19b3087a78a417d6 (diff)
Improved entire building chain (especially debugging)
-rw-r--r--.build.mk28
-rw-r--r--.github/workflows/build.yml10
-rw-r--r--Makefile27
-rw-r--r--apps/Makefile5
-rw-r--r--apps/chess/Makefile3
-rw-r--r--apps/idle/Makefile3
-rw-r--r--apps/init/Makefile3
-rw-r--r--apps/test/Makefile3
-rw-r--r--apps/wm/Makefile3
-rw-r--r--kernel/Makefile3
-rw-r--r--kernel/drivers/fb.c5
-rw-r--r--kernel/features/proc.c6
-rw-r--r--libs/libc/alloc.c62
-rw-r--r--libs/libc/inc/crypto.h2
-rw-r--r--libs/libc/inc/def.h1
-rw-r--r--libs/libc/inc/mem.h43
-rw-r--r--libs/libc/inc/str.h20
-rwxr-xr-xrun13
18 files changed, 169 insertions, 71 deletions
diff --git a/.build.mk b/.build.mk
new file mode 100644
index 0000000..8d34dbb
--- /dev/null
+++ b/.build.mk
@@ -0,0 +1,28 @@
+# MIT License, Copyright (c) 2021 Marvin Borner
+
+# All preprocessor flags - enable using the custom config group below
+ALL_PREPROCESSOR_FLAGS = \
+ DEBUG_ALLOC \
+ DEBUG_SCHEDULER
+
+# Default configs
+CONFIG_CACHE ?=
+CONFIG_EXTRA_CFLAGS ?=
+CONFIG_USE_PIE ?=
+
+# Specific config groups
+ifeq ($(CONFIG), debug)
+ CONFIG_OPTIMIZATION ?= -Ofast
+ CONFIG_EXTRA_CFLAGS ?= -Wno-error -ggdb3 -s -fsanitize=undefined -fstack-protector-all
+ CONFIG_CACHE ?= ccache
+else ifeq ($(CONFIG), dev)
+ CONFIG_OPTIMIZATION ?= -finline -finline-functions -Ofast
+ CONFIG_CACHE ?= ccache
+else ifeq ($(CONFIG), release)
+ CONFIG_OPTIMIZATION ?= -finline -finline-functions -Ofast
+ CONFIG_STRIP ?= true
+ CONFIG_CACHE ?= ccache
+else ifeq ($(CONFIG), custom)
+ DEBUG_ALLOC ?= true
+ DEBUG_SCHEDULER ?= true
+endif
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 6e88c24..cb9660e 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -16,18 +16,18 @@ jobs:
uses: actions/cache@v1
with:
path: cross
- key: toller-compiler
+ key: toolchain
- name: Build cross compiler
if: steps.cache-cross.outputs.cache-hit != 'true'
run: sh run cross -y
- name: Build
- run: sh run build -y
+ run: MELVIX_CONFIG=release sh run build -y
- name: Upload as artifact
uses: actions/upload-artifact@v2
with:
name: disk-img
path: build/disk.img
- - name: Test without debug mode
- run: MELVIX_DEBUG=0 sh run test -y
+ - name: Test with release mode
+ run: MELVIX_CONFIG=release sh run test -y
- name: Test with debug mode
- run: MELVIX_DEBUG=1 sh run test -y
+ run: MELVIX_CONFIG=debug sh run test -y
diff --git a/Makefile b/Makefile
index 456659a..a6debed 100644
--- a/Makefile
+++ b/Makefile
@@ -1,28 +1,33 @@
# MIT License, Copyright (c) 2020 Marvin Borner
-CFLAGS_OPTIMIZATION = -finline -finline-functions -Ofast
+CONFIG ?= dev
+include .build.mk
+
+define PREPROCESSOR_FLAG_TEMPLATE =
+-D$(1)=$(if $(filter $($(1)),true),1,0)
+endef
+
+PREPROCESSOR_FLAGS = $(foreach flag, $(ALL_PREPROCESSOR_FLAGS), $(call PREPROCESSOR_FLAG_TEMPLATE,$(flag)))
+
CFLAGS_WARNINGS = -Wall -Wextra -Werror -Wshadow -Wpointer-arith -Wwrite-strings -Wredundant-decls -Wnested-externs -Wformat=2 -Wmissing-declarations -Wstrict-prototypes -Wmissing-prototypes -Wcast-qual -Wswitch-default -Wswitch-enum -Wlogical-op -Wunreachable-code -Wundef -Wold-style-definition -Wvla -pedantic-errors
-CFLAGS_DEFAULT = $(CFLAGS_WARNINGS) $(CFLAGS_OPTIMIZATION) -std=c99 -m32 -nostdlib -nostdinc -fno-builtin -fno-profile-generate -fno-omit-frame-pointer -fno-common -fno-asynchronous-unwind-tables -mno-red-zone -mno-80387 -mno-mmx -mno-sse -mno-sse2
+CFLAGS_DEFAULT = $(CFLAGS_WARNINGS) -std=c99 -m32 -nostdlib -nostdinc -fno-builtin -fno-profile-generate -fno-omit-frame-pointer -fno-common -fno-asynchronous-unwind-tables -mno-red-zone -mno-80387 -mno-mmx -mno-sse -mno-sse2 $(CONFIG_OPTIMIZATION) $(CONFIG_EXTRA_CFLAGS) $(PREPROCESSOR_FLAGS)
-CC = ccache $(PWD)/cross/opt/bin/i686-elf-gcc
-LD = ccache $(PWD)/cross/opt/bin/i686-elf-ld
-OC = ccache $(PWD)/cross/opt/bin/i686-elf-objcopy
-ST = ccache $(PWD)/cross/opt/bin/i686-elf-strip
-AS = ccache nasm
+CC = $(CONFIG_CACHE) $(PWD)/cross/opt/bin/i686-elf-gcc
+LD = $(CONFIG_CACHE) $(PWD)/cross/opt/bin/i686-elf-ld
+OC = $(CONFIG_CACHE) $(PWD)/cross/opt/bin/i686-elf-objcopy
+ST = $(CONFIG_CACHE) $(PWD)/cross/opt/bin/i686-elf-strip
+AS = $(CONFIG_CACHE) nasm
BUILD = $(PWD)/build/
KERNEL = $(PWD)/kernel/
LIBS = $(PWD)/libs/
-ifeq ($(DEBUG), 1)
- CFLAGS_DEFAULT += -Wno-error -ggdb3 -s -fsanitize=undefined -fstack-protector-all
-endif
-
all: compile
export
compile:
+ @echo Using '$(CONFIG)' config
@$(MAKE) clean --no-print-directory -C libs/libc/
@$(MAKE) libc --no-print-directory -C libs/libc/
@echo "Compiled libc"
diff --git a/apps/Makefile b/apps/Makefile
index 98bec47..2884040 100644
--- a/apps/Makefile
+++ b/apps/Makefile
@@ -1,11 +1,12 @@
# MIT License, Copyright (c) 2020 Marvin Borner
-CFLAGS = $(CFLAGS_DEFAULT) -I$(LIBS)/ -I$(LIBS)/libc/inc/ -pie -fPIE -fPIC -DUSER
+CFLAGS = $(CFLAGS_DEFAULT) -I$(LIBS)/ -I$(LIBS)/libc/inc/ -DUSER
LDFLAGS = --section-start=.text=0x42000000 -L$(BUILD)
DIRS = $(wildcard */.)
-ifneq ($(DEBUG), 1)
+ifeq ($(CONFIG_USER_PIE), true)
LDFLAGS += -pie -no-dynamic-linker
+ CFLAGS += -pie -fPIE -fPIC
endif
export
diff --git a/apps/chess/Makefile b/apps/chess/Makefile
index b538807..e84f65f 100644
--- a/apps/chess/Makefile
+++ b/apps/chess/Makefile
@@ -5,6 +5,9 @@ OBJS = chess.o
all: $(OBJS)
@mkdir -p $(BUILD)/apps/chess/
@$(LD) -o $(BUILD)/apps/chess/exec $(LDFLAGS) $^ -lgui -ltxt -lc
+ifeq ($(CONFIG_STRIP), true)
+ @$(ST) --strip-all $(BUILD)/apps/chess/exec
+endif
clean:
@$(RM) -f $(OBJS)
diff --git a/apps/idle/Makefile b/apps/idle/Makefile
index 28145b4..f4f9730 100644
--- a/apps/idle/Makefile
+++ b/apps/idle/Makefile
@@ -5,6 +5,9 @@ OBJS = idle.o
all: $(OBJS)
@mkdir -p $(BUILD)/apps/idle/
@$(LD) -o $(BUILD)/apps/idle/exec $(LDFLAGS) $^ -lc
+ifeq ($(CONFIG_STRIP), true)
+ @$(ST) --strip-all $(BUILD)/apps/idle/exec
+endif
clean:
@$(RM) -f $(OBJS)
diff --git a/apps/init/Makefile b/apps/init/Makefile
index 3547797..68038d0 100644
--- a/apps/init/Makefile
+++ b/apps/init/Makefile
@@ -5,6 +5,9 @@ OBJS = init.o
all: $(OBJS)
@mkdir -p $(BUILD)/apps/init/
@$(LD) -o $(BUILD)/apps/init/exec $(LDFLAGS) $^ -lc
+ifeq ($(CONFIG_STRIP), true)
+ @$(ST) --strip-all $(BUILD)/apps/init/exec
+endif
clean:
@$(RM) -f $(OBJS)
diff --git a/apps/test/Makefile b/apps/test/Makefile
index 78a913d..e80b54b 100644
--- a/apps/test/Makefile
+++ b/apps/test/Makefile
@@ -5,6 +5,9 @@ OBJS = test.o fuzz.o
all: $(OBJS)
@mkdir -p $(BUILD)/apps/test/
@$(LD) -o $(BUILD)/apps/test/exec $(LDFLAGS) $^ -lc
+ifeq ($(CONFIG_STRIP), true)
+ @$(ST) --strip-all $(BUILD)/apps/test/exec
+endif
clean:
@$(RM) -f $(OBJS)
diff --git a/apps/wm/Makefile b/apps/wm/Makefile
index abceef7..8fb0096 100644
--- a/apps/wm/Makefile
+++ b/apps/wm/Makefile
@@ -5,6 +5,9 @@ OBJS = wm.o
all: $(OBJS)
@mkdir -p $(BUILD)/apps/wm/
@$(LD) -o $(BUILD)/apps/wm/exec $(LDFLAGS) $^ -lgui -ltxt -lc
+ifeq ($(CONFIG_STRIP), true)
+ @$(ST) --strip-all $(BUILD)/apps/wm/exec
+endif
clean:
@$(RM) -f $(OBJS)
diff --git a/kernel/Makefile b/kernel/Makefile
index 01fe3c1..14fc055 100644
--- a/kernel/Makefile
+++ b/kernel/Makefile
@@ -42,3 +42,6 @@ all: compile
compile: $(COBJS)
@mkdir -p $(BUILD)/apps/kernel/
@$(LD) -N -z max-page-size=0x1000 -eboot_entry -Tlink.ld -o $(BUILD)/apps/kernel/exec -L$(BUILD) $+ -lk
+ifeq ($(CONFIG_STRIP), true)
+ @$(ST) --strip-all $(BUILD)/apps/kernel/exec
+endif
diff --git a/kernel/drivers/fb.c b/kernel/drivers/fb.c
index 8122654..6a81434 100644
--- a/kernel/drivers/fb.c
+++ b/kernel/drivers/fb.c
@@ -29,7 +29,10 @@ PROTECTED static struct vbe_basic *vbe = NULL;
static u32 fb_map_buffer(struct page_dir *dir)
{
assert(vbe);
- return virtual_alloc(dir, memory_range_around((u32)vbe->fb, FB_SIZE), MEMORY_USER).base;
+ struct memory_range r =
+ virtual_alloc(dir, memory_range_around((u32)vbe->fb, FB_SIZE), MEMORY_USER);
+ printf("FB: %x+%x\n", r.base, r.size);
+ return r.base;
}
static u32 fb_owner = 0;
diff --git a/kernel/features/proc.c b/kernel/features/proc.c
index c6de9c0..574df68 100644
--- a/kernel/features/proc.c
+++ b/kernel/features/proc.c
@@ -58,6 +58,12 @@ HOT FLATTEN void scheduler(struct regs *regs)
memory_switch_dir(PROC(current)->page_dir);
memcpy(regs, &PROC(current)->regs, sizeof(*regs));
+#if DEBUG_SCHEDULER
+ if (current != idle_proc)
+ printf("%s (%d): eip %x esp %x useresp %x\n", PROC(current)->name,
+ PROC(current)->pid, regs->eip, regs->esp, regs->useresp);
+#endif
+
locked = 0;
}
diff --git a/libs/libc/alloc.c b/libs/libc/alloc.c
index 4913479..73fdf8b 100644
--- a/libs/libc/alloc.c
+++ b/libs/libc/alloc.c
@@ -382,16 +382,38 @@ static void *_realloc(void *ptr, u32 size)
#define FUNC log
#endif
-void *zalloc(u32 size)
+void *realloc_debug(void *ptr, u32 size, const char *file, int line, const char *func,
+ const char *inp)
{
- void *ret = malloc(size);
- memset(ret, 0, size);
+ assert(size < (100 << 20)); // Don't brag with memory pls
+ void *ret = _realloc(ptr, size);
+
+#if DEBUG_ALLOC
+ FUNC(PREFIX "REALLOC\t%s:%d: %s: 0x%x %dB (%s)\n", file, line, func, ret, size, inp);
+#else
+ UNUSED(file);
+ UNUSED(line);
+ UNUSED(func);
+ UNUSED(inp);
+#endif
return ret;
}
-void *realloc(void *ptr, u32 size)
+void *zalloc_debug(u32 size, const char *file, int line, const char *func, const char *inp)
{
- return _realloc(ptr, size);
+ assert(size < (100 << 20)); // Don't brag with memory pls
+ void *ret = _malloc(size);
+ memset(ret, 0, size);
+
+#if DEBUG_ALLOC
+ FUNC(PREFIX "ZALLOC\t%s:%d: %s: 0x%x %dB (%s)\n", file, line, func, ret, size, inp);
+#else
+ UNUSED(file);
+ UNUSED(line);
+ UNUSED(func);
+ UNUSED(inp);
+#endif
+ return ret;
}
void *malloc_debug(u32 size, const char *file, int line, const char *func, const char *inp)
@@ -399,13 +421,14 @@ void *malloc_debug(u32 size, const char *file, int line, const char *func, const
assert(size < (100 << 20)); // Don't brag with memory pls
void *ret = _malloc(size);
- (void)file;
- (void)line;
- (void)func;
- (void)inp;
- /* #ifdef KERNEL */
- /* FUNC(PREFIX "MALLOC\t%s:%d: %s: 0x%x %dB (%s)\n", file, line, func, ret, size, inp); */
- /* #endif */
+#if DEBUG_ALLOC
+ FUNC(PREFIX "MALLOC\t%s:%d: %s: 0x%x %dB (%s)\n", file, line, func, ret, size, inp);
+#else
+ UNUSED(file);
+ UNUSED(line);
+ UNUSED(func);
+ UNUSED(inp);
+#endif
return ret;
}
@@ -413,11 +436,12 @@ void free_debug(void *ptr, const char *file, int line, const char *func, const c
{
_free(ptr);
- (void)file;
- (void)line;
- (void)func;
- (void)inp;
- /* #ifdef KERNEL */
- /* FUNC(PREFIX "FREE\t%s:%d: %s: 0x%x (%s)\n", file, line, func, ptr, inp); */
- /* #endif */
+#if DEBUG_ALLOC
+ FUNC(PREFIX "FREE\t%s:%d: %s: 0x%x (%s)\n", file, line, func, ptr, inp);
+#else
+ UNUSED(file);
+ UNUSED(line);
+ UNUSED(func);
+ UNUSED(inp);
+#endif
}
diff --git a/libs/libc/inc/crypto.h b/libs/libc/inc/crypto.h
index b672c50..a67bb97 100644
--- a/libs/libc/inc/crypto.h
+++ b/libs/libc/inc/crypto.h
@@ -9,7 +9,7 @@ void md5(const void *initial_msg, u32 initial_len, u8 digest[16]) NONNULL;
u32 crc32(u32 crc, const void *buf, u32 size) NONNULL;
#ifdef KERNEL
-u32 crc32_user(u32 crc, const void *buf, u32 size) NONNULL;
+INLINE u32 crc32_user(u32 crc, const void *buf, u32 size) NONNULL;
#endif
#endif
diff --git a/libs/libc/inc/def.h b/libs/libc/inc/def.h
index 191a818..7338242 100644
--- a/libs/libc/inc/def.h
+++ b/libs/libc/inc/def.h
@@ -47,6 +47,7 @@ typedef unsigned long long u64;
#define ATTR __attribute__
#define NORETURN ATTR((noreturn))
+#define INLINE ATTR((gnu_inline)) inline
#define NOINLINE ATTR((noinline))
#define DEPRECATED ATTR((deprecated))
#define NONNULL ATTR((nonnull))
diff --git a/libs/libc/inc/mem.h b/libs/libc/inc/mem.h
index 75174c9..a25feaf 100644
--- a/libs/libc/inc/mem.h
+++ b/libs/libc/inc/mem.h
@@ -5,22 +5,35 @@
#include <def.h>
+// malloc
ATTR((malloc))
ATTR((alloc_size(1)))
-RET_NONNULL void *malloc_debug(u32 size, const char *file, int line, const char *func,
- const char *inp) NONNULL;
-ATTR((malloc)) ATTR((alloc_size(2))) RET_NONNULL void *realloc(void *ptr, u32 size);
-ATTR((malloc)) ATTR((alloc_size(1))) RET_NONNULL void *zalloc(u32 size);
-void free_debug(void *ptr, const char *file, int line, const char *func, const char *inp) NONNULL;
+INLINE RET_NONNULL void *malloc_debug(u32 size, const char *file, int line, const char *func,
+ const char *inp) NONNULL;
+// realloc
+ATTR((malloc))
+ATTR((alloc_size(2)))
+INLINE RET_NONNULL void *realloc_debug(void *ptr, u32 size, const char *file, int line,
+ const char *func, const char *inp);
+
+// zalloc
+ATTR((malloc))
+ATTR((alloc_size(1)))
+RET_NONNULL
+INLINE void *zalloc_debug(u32 size, const char *file, int line, const char *func, const char *inp);
+
+// free
+INLINE void free_debug(void *ptr, const char *file, int line, const char *func,
+ const char *inp) NONNULL;
+
+// Debug wrappers
+#define realloc(ptr, size) \
+ realloc_debug((void *)ptr, (u32)(size), __FILE__, __LINE__, __func__, #size)
+#define zalloc(size) zalloc_debug((u32)(size), __FILE__, __LINE__, __func__, #size)
#define malloc(size) malloc_debug((u32)(size), __FILE__, __LINE__, __func__, #size)
#define free(ptr) free_debug((void *)(ptr), __FILE__, __LINE__, __func__, #ptr)
-#ifdef KERNEL
-#define STACK_SIZE (1 << 20) // 1MiB
-#elif defined(USER)
-#endif
-
void *memcpy(void *dest, const void *src, u32 n) NONNULL;
void *memset(void *dest, u32 val, u32 n) NONNULL;
void *memchr(void *src, char c, u32 n) NONNULL;
@@ -28,11 +41,11 @@ s32 memcmp(const void *s1, const void *s2, u32 n) NONNULL;
u8 mememp(const u8 *buf, u32 n) NONNULL;
#ifdef KERNEL
-void *memcpy_user(void *dest, const void *src, u32 n) NONNULL;
-void *memset_user(void *dest, u32 val, u32 n) NONNULL;
-void *memchr_user(void *src, char c, u32 n) NONNULL;
-s32 memcmp_user(const void *s1, const void *s2, u32 n) NONNULL;
-u8 mememp_user(const u8 *buf, u32 n) NONNULL;
+INLINE void *memcpy_user(void *dest, const void *src, u32 n) NONNULL;
+INLINE void *memset_user(void *dest, u32 val, u32 n) NONNULL;
+INLINE void *memchr_user(void *src, char c, u32 n) NONNULL;
+INLINE s32 memcmp_user(const void *s1, const void *s2, u32 n) NONNULL;
+INLINE u8 mememp_user(const u8 *buf, u32 n) NONNULL;
#endif
#endif
diff --git a/libs/libc/inc/str.h b/libs/libc/inc/str.h
index f024aaa..d4c197e 100644
--- a/libs/libc/inc/str.h
+++ b/libs/libc/inc/str.h
@@ -18,16 +18,16 @@ ATTR((malloc)) char *strdup(const char *s) NONNULL;
#ifdef KERNEL
-PURE u32 strlen_user(const char *s) NONNULL;
-PURE u32 strnlen_user(const char *s, u32 max) NONNULL;
-u32 strlcpy_user(char *dst, const char *src, u32 size) NONNULL;
-PURE char *strchr_user(char *s, char c) NONNULL;
-PURE char *strrchr_user(char *s, char c) NONNULL;
-u32 strlcat_user(char *dst, const char *src, u32 size) NONNULL;
-s32 strcmp_user(const char *s1, const char *s2) NONNULL;
-s32 strncmp_user(const char *s1, const char *s2, u32 n) NONNULL;
-char *strinv_user(char *s) NONNULL;
-ATTR((malloc)) char *strdup_user(const char *s) NONNULL;
+INLINE PURE u32 strlen_user(const char *s) NONNULL;
+INLINE PURE u32 strnlen_user(const char *s, u32 max) NONNULL;
+INLINE u32 strlcpy_user(char *dst, const char *src, u32 size) NONNULL;
+INLINE PURE char *strchr_user(char *s, char c) NONNULL;
+INLINE PURE char *strrchr_user(char *s, char c) NONNULL;
+INLINE u32 strlcat_user(char *dst, const char *src, u32 size) NONNULL;
+INLINE s32 strcmp_user(const char *s1, const char *s2) NONNULL;
+INLINE s32 strncmp_user(const char *s1, const char *s2, u32 n) NONNULL;
+INLINE char *strinv_user(char *s) NONNULL;
+INLINE ATTR((malloc)) char *strdup_user(const char *s) NONNULL;
#endif
diff --git a/run b/run
index 8d647eb..87a3817 100755
--- a/run
+++ b/run
@@ -5,6 +5,8 @@ set -e
cd "$(dirname "$0")"
+MELVIX_CONFIG="${MELVIX_CONFIG:-dev}"
+
MAKE=make
NPROC=nproc
SUDO=sudo
@@ -140,11 +142,7 @@ make_build() {
rm -rf build/*
printf "\nBuilding...\n"
- if [ "$mode" = "debug" ] || [ "$MELVIX_DEBUG" = "1" ]; then
- $MAKE -j $($NPROC) DEBUG=1
- else
- $MAKE -j $($NPROC) DEBUG=0
- fi
+ $MAKE -j $($NPROC) CONFIG="$MELVIX_CONFIG" $MELVIX_MAKE
# Prepare Grub
mkdir -p disk/boot/grub/
@@ -223,7 +221,7 @@ make_disasm() {
}
make_addr() {
- printf "Info: Make sure that you've turned the debug build on (e.g. with MELVIX_DEBUG=1)\n\n"
+ printf "Info: Make sure that you've turned the debug build on (e.g. with MELVIX_CONFIG=debug)\n\n"
if [ -z "$2" ]; then
echo "Usage: './run addr kernel 0x50042'"
exit 1
@@ -318,5 +316,6 @@ else
printf "disk\t\tPrepares the userspace disk (e.g. fonts)\n"
printf "*\t\tAnything else prints this help\n"
printf "\t\tWhen no option is set, Melvix gets built and emulated using QEMU (cross+clean+build)\n\n"
- echo "Set the environment variable 'MELVIX_DEBUG=1' for persistent debug builds"
+ echo "You can set the environment variable 'MELVIX_CONFIG' to a config group defined in '.build.mk'"
+ echo "Set the environment variable 'MELVIX_MAKE=\"CONFIG_FOO=bar\"' to override build configs"
fi