From 50cc83db21e08850cb869b02f24ef19cf52d32d5 Mon Sep 17 00:00:00 2001 From: Minep Date: Sun, 23 Oct 2022 01:23:11 +0100 Subject: [PATCH] fix: bugs related to O2 optimization fix: change default optimization level to O1 as temp workaround. --- README.md | 5 ++++- lunaix-os/config/make-cc | 2 +- lunaix-os/includes/arch/x86/interrupts.h | 2 +- lunaix-os/includes/lunaix/spike.h | 5 +++-- lunaix-os/includes/lunaix/syscall.h | 4 +++- lunaix-os/kernel/fs/mount.c | 4 +++- lunaix-os/kernel/tty/lxconsole.c | 2 +- lunaix-os/makefile | 9 ++++++--- 8 files changed, 22 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 0cc76fe..aa08848 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,8 @@ LunaixOS - 一个简单的,详细的,POSIX兼容的(但愿!),带有 本项目支持的make命令: | 命令 | 用途 | |---|---| -| `make all` | 构建镜像(`-O2`) | +| `make all` | 构建镜像(`-O1`) | +| `make instable` | 构建镜像(`-O2`)**※** | | `make all-debug` | 构建适合调试用的镜像(`-Og`) | | `make run` | 使用QEMU运行build目录下的镜像| | `make debug-qemu` | 构建并使用QEMU进行调试 | @@ -71,6 +72,8 @@ LunaixOS - 一个简单的,详细的,POSIX兼容的(但愿!),带有 | `make debug-qemu-vscode` | 用于vscode整合 | | `make clean` | 删除build目录 | +**※:由于在`-O2`模式下,GCC会进行大量的优化,这导致LunaixOS会出现一些非常奇怪、离谱的bug,从而影响到基本运行。调试这些bug需要大量的汗水与时间,属于日后维护的任务。所以,在目前,LunaixOS将默认最大的优化程度设置为比较安全的`-O1`。`make instable`仅用于方便日后的对该问题的进一步调试。** + ## 运行以及Issue 运行该操作系统需要一个虚拟磁盘镜像,可以使用如下命令快速创建一个: diff --git a/lunaix-os/config/make-cc b/lunaix-os/config/make-cc index 7a1899c..71f3b76 100644 --- a/lunaix-os/config/make-cc +++ b/lunaix-os/config/make-cc @@ -3,7 +3,7 @@ AS := i686-elf-as ARCH_OPT := -D__ARCH_IA32 -O := -O2 +O := -O1 W := -Wall -Wextra -Wno-unknown-pragmas \ -Wno-unused-function \ -Wno-unused-but-set-variable \ diff --git a/lunaix-os/includes/arch/x86/interrupts.h b/lunaix-os/includes/arch/x86/interrupts.h index 822b99d..d25fbe3 100644 --- a/lunaix-os/includes/arch/x86/interrupts.h +++ b/lunaix-os/includes/arch/x86/interrupts.h @@ -21,7 +21,7 @@ typedef struct reg32 fs; reg32 gs; reg32 esp; - } registers; + } __attribute__((packed)) registers; unsigned int vector; unsigned int err_code; diff --git a/lunaix-os/includes/lunaix/spike.h b/lunaix-os/includes/lunaix/spike.h index d9c234d..298593d 100644 --- a/lunaix-os/includes/lunaix/spike.h +++ b/lunaix-os/includes/lunaix/spike.h @@ -71,7 +71,7 @@ spin() ; } -#ifdef __LUNAIXOS_DEBUG__ +#ifndef __LUNAIXOS_NASSERT__ #define assert(cond) \ if (!(cond)) { \ __assert_fail(#cond, __FILE__, __LINE__); \ @@ -87,7 +87,8 @@ __assert_fail(const char* expr, const char* file, unsigned int line) #else #define assert(cond) (void)(cond); // assert nothing #define assert_msg(cond, msg) (void)(cond); // assert nothing -#endif + +#endif // __LUNAIXOS_NASSERT__ void panick(const char* msg); diff --git a/lunaix-os/includes/lunaix/syscall.h b/lunaix-os/includes/lunaix/syscall.h index 8a523ad..6169781 100644 --- a/lunaix-os/includes/lunaix/syscall.h +++ b/lunaix-os/includes/lunaix/syscall.h @@ -136,8 +136,10 @@ syscall_install(); } #define __LXSYSCALL2_VARG(rettype, name, t1, p1, t2, p2) \ - static rettype name(__PARAM_MAP2(t1, p1, t2, p2), ...) \ + __attribute__((noinline)) static rettype name( \ + __PARAM_MAP2(t1, p1, t2, p2), ...) \ { \ + /* No inlining! This depends on the call frame assumption */ \ void* _last = (void*)&p2 + sizeof(void*); \ asm("\n" ::"b"(p1), "c"(p2), "d"(_last)); \ ___DOINT33(__SYSCALL_##name, rettype) \ diff --git a/lunaix-os/kernel/fs/mount.c b/lunaix-os/kernel/fs/mount.c index c1e9784..ca80238 100644 --- a/lunaix-os/kernel/fs/mount.c +++ b/lunaix-os/kernel/fs/mount.c @@ -2,6 +2,7 @@ #include #include #include +#include #include struct llist_header all_mnts = { .next = &all_mnts, .prev = &all_mnts }; @@ -18,12 +19,12 @@ vfs_create_mount(struct v_mount* parent, struct v_dnode* mnt_point) llist_append(&all_mnts, &mnt->list); mutex_init(&mnt->lock); - mnt_mkbusy(parent); mnt->parent = parent; mnt->mnt_point = mnt_point; mnt->super_block = mnt_point->super_block; if (parent) { + mnt_mkbusy(parent); mutex_lock(&mnt->parent->lock); llist_append(&parent->submnts, &mnt->sibmnts); mutex_unlock(&mnt->parent->lock); @@ -84,6 +85,7 @@ mnt_chillax(struct v_mount* mnt) int vfs_mount_root(const char* fs_name, struct device* device) { + extern struct v_dnode* vfs_sysroot; int errno = 0; if (vfs_sysroot->mnt && (errno = vfs_unmount_at(vfs_sysroot))) { return errno; diff --git a/lunaix-os/kernel/tty/lxconsole.c b/lunaix-os/kernel/tty/lxconsole.c index ad3fc42..3404450 100644 --- a/lunaix-os/kernel/tty/lxconsole.c +++ b/lunaix-os/kernel/tty/lxconsole.c @@ -27,7 +27,7 @@ console_flush(); static waitq_t lx_reader; static volatile char ttychr; -static pid_t fg_pgid = 0; +static volatile pid_t fg_pgid = 0; inline void print_control_code(const char cntrl) diff --git a/lunaix-os/makefile b/lunaix-os/makefile index 27ce9b7..2735946 100644 --- a/lunaix-os/makefile +++ b/lunaix-os/makefile @@ -20,16 +20,16 @@ $(ISO_DIR): $(OBJECT_DIR)/%.S.o: %.S @mkdir -p $(@D) - @echo " BUILD: $<" + @echo " CC $<" @$(CC) $(INCLUDES) -c $< -o $@ $(OBJECT_DIR)/%.c.o: %.c @mkdir -p $(@D) - @echo " BUILD: $<" + @echo " CC $<" @$(CC) $(INCLUDES) -c $< -o $@ $(CFLAGS) $(BIN_DIR)/$(OS_BIN): $(OBJECT_DIR) $(BIN_DIR) $(SRC) - @echo " LINK: $(BIN_DIR)/$(OS_BIN)" + @echo " LD $(BIN_DIR)/$(OS_BIN)" @$(CC) -T link/linker.ld -o $(BIN_DIR)/$(OS_BIN) $(SRC) $(LDFLAGS) $(BUILD_DIR)/$(OS_ISO): $(ISO_DIR) $(BIN_DIR)/$(OS_BIN) GRUB_TEMPLATE @@ -39,6 +39,9 @@ $(BUILD_DIR)/$(OS_ISO): $(ISO_DIR) $(BIN_DIR)/$(OS_BIN) GRUB_TEMPLATE all: clean $(BUILD_DIR)/$(OS_ISO) +instable: O := -O2 +instable: all + all-debug: O := -Og all-debug: CFLAGS := -g -std=gnu99 -ffreestanding $(O) $(W) $(ARCH_OPT) -D__LUNAIXOS_DEBUG__ all-debug: LDFLAGS := -g -ffreestanding $(O) -nostdlib -lgcc -- 2.27.0