From 9fed595abc9bc2e68f416ee62f1389b0df3cee72 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Mon, 20 Jul 2026 16:43:00 -0400 Subject: [PATCH] Makefile: add module support Signed-off-by: Michael Shaw --- Makefile | 8 ++++++++ amd64/amd64_init.c | 1 + amd64/exception.c | 7 +++---- amd64/include/arch/msr.h | 1 + amd64/kernel/proc.c | 3 +++ scripts/Makefile.build | 37 +++++++++++++++++++++++++++++++++---- usr.bin/utest/utest.c | 10 +--------- 7 files changed, 50 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index 9deb99a..672235e 100644 --- a/Makefile +++ b/Makefile @@ -63,15 +63,19 @@ obj-y := $(KDIRS) QEMUFLAGS := -m 16G +MODULE_CFLAGS := -DMODULE + export Q export V export obj-y +export obj-m export CC export LD export QEMUFLAGS export CFLAGS export LDFLAGS export INITRAMFS +export MODULE_CFLAGS PHONY: default default: @@ -94,6 +98,10 @@ $(KERNEL): $(KOBJ) linker.ld $(Q)echo " LD $@" $(Q)$(LD) $(KOBJ) $(LDFLAGS) -T linker.ld -o $@ +.PHONY: modules +modules: + $(Q)$(MAKE) --no-print-directory -f scripts/Makefile.build _modules obj=. + .PHONY: clean clean: $(Q)$(MAKE) --no-print-directory -f scripts/Makefile.build _clean obj=. diff --git a/amd64/amd64_init.c b/amd64/amd64_init.c index 553970c..80c7ce7 100644 --- a/amd64/amd64_init.c +++ b/amd64/amd64_init.c @@ -49,6 +49,7 @@ void arch_init() gdt_init(); idt_init(); + wrmsr(IA32_EFER, rdmsr(IA32_EFER) | 1); /* SCE */ wrmsr(IA32_STAR, ((u64)GDT_SEGMENT_CODE_RING0 << 48) | ((u64)GDT_SEGMENT_CODE_RING3 << 32)); wrmsr(IA32_LSTAR, (u64)_syscall_enter); wrmsr(IA32_FMASK, 0x3F7FD5); diff --git a/amd64/exception.c b/amd64/exception.c index 319cf90..30a8c42 100644 --- a/amd64/exception.c +++ b/amd64/exception.c @@ -40,9 +40,8 @@ static const char *exception_names[] = { void exception_handler(u64 no, u64 err) { if (no == 0x0e) { - if (kernel_init_ready) - panic("(Page fault) addr: 0x%16x err: %x\n", cr2_read(), err); + panic("(Page fault) addr: 0x%16x err: %x\n", cr2_read(), err); } - if (!kernel_init_ready) - panic("(Exception) %s(%02xh)\n", exception_names[no], no); + + panic("(Exception) %s(%02xh) err: %x\n", exception_names[no], no, err); } diff --git a/amd64/include/arch/msr.h b/amd64/include/arch/msr.h index ae81a36..bbdbd23 100644 --- a/amd64/include/arch/msr.h +++ b/amd64/include/arch/msr.h @@ -4,6 +4,7 @@ #include +#define IA32_EFER 0xC0000080 #define IA32_STAR 0xC0000081 #define IA32_LSTAR 0xC0000082 #define IA32_FMASK 0xC0000084 diff --git a/amd64/kernel/proc.c b/amd64/kernel/proc.c index 4bb4742..e9637b4 100644 --- a/amd64/kernel/proc.c +++ b/amd64/kernel/proc.c @@ -36,6 +36,9 @@ void enter_userspace_init(struct thread *thread, uaddr_t entry) tls->cur_thread = thread; + void *kstack = kalloc(PAGE_SIZE, ALLOC_KERN); + tls->kernel_rsp = (vaddr_t)kstack + PAGE_SIZE; + pgd_load(thread->proc->pgd); thread->state = PROC_STATE_RUNNING; diff --git a/scripts/Makefile.build b/scripts/Makefile.build index 4d1c76b..3a716e6 100644 --- a/scripts/Makefile.build +++ b/scripts/Makefile.build @@ -11,6 +11,7 @@ export obj-y := +obj-m := subdir-y := include $(obj)/Makefile @@ -20,7 +21,14 @@ real-objs-y := subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y))) real-objs-y := $(addprefix $(obj)/, $(filter-out %/, $(obj-y))) subdir-objs-y := $(foreach d,$(subdir-y),$(obj)/$(d)/built-in.o) -deps := $(real-objs-y:.o=.d) + +subdir-m := $(patsubst %/,%,$(filter %/, $(obj-m))) +mod-names := $(basename $(filter-out %/, $(obj-m))) +mod-targets := $(addprefix $(obj)/, $(addsuffix .ko, $(mod-names))) +mod-objs := $(foreach m,$(mod-names), \ + $(addprefix $(obj)/, $(or $($(m)-objs),$($(m)-y),$(m).o))) + +deps := $(real-objs-y:.o=.d) $(mod-objs:.o=.d) ifeq ($(obj),.) _all: $(subdir-objs-y) @@ -40,15 +48,36 @@ $(obj)/built-in.o: $(real-objs-y) $(subdir-objs-y) $(Q)echo " LD $@" $(Q)$(LD) -r -o $@ $(real-objs-y) $(foreach d,$(subdir-y),$(obj)/$(d)/built-in.o) +# --- Module support --- + +define mod_rule +$(obj)/$(1).ko: $(addprefix $(obj)/, $(or $($(1)-objs),$($(1)-y),$(1).o)) + $$(Q)echo " MODLD $$@" + $$(Q)$$(LD) -r -o $$@ $$^ +endef + +$(foreach m,$(mod-names),$(eval $(call mod_rule,$(m)))) + +define mod_cflags +$(addprefix $(obj)/, $(or $($(1)-objs),$($(1)-y),$(1).o)): CFLAGS += $(MODULE_CFLAGS) +endef + +$(foreach m,$(mod-names),$(eval $(call mod_cflags,$(m)))) + +.PHONY: _modules +_modules: $(mod-targets) $(subdir-m) .PHONY: _clean -_clean: $(subdir-y) +_clean: $(subdir-y) $(subdir-m) $(Q)echo " CLEAN $(obj)" - @rm -rf $(obj)/built-in.o $(real-objs-y) $(deps) - + @rm -rf $(obj)/built-in.o $(real-objs-y) $(deps) $(mod-objs) $(mod-targets) + $(subdir-y): FORCE $(Q)$(MAKE) --no-print-directory -f scripts/Makefile.build $(MAKECMDGOALS) obj=$(obj)/$@ +$(subdir-m): FORCE + $(Q)$(MAKE) --no-print-directory -f scripts/Makefile.build $(if $(filter _clean,$(MAKECMDGOALS)),_clean,_modules) obj=$(obj)/$@ + $(obj)/%/built-in.o: FORCE $(Q)$(MAKE) --no-print-directory -f scripts/Makefile.build obj=$(patsubst %/,%,$(dir $@)) diff --git a/usr.bin/utest/utest.c b/usr.bin/utest/utest.c index 61a134b..f94f97e 100644 --- a/usr.bin/utest/utest.c +++ b/usr.bin/utest/utest.c @@ -21,14 +21,6 @@ size_t strlen(const char *c) void _start() { - int fd = open("/dev/ttyS0", 0); - if (fd < 0) - goto loop; - - char msg[] = "Hello, userspace!\n"; - write(fd, msg, strlen(msg)); - -loop: while (1) - continue; + ; }