Makefile: add module support
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
parent
7cc7da0f54
commit
9fed595abc
7 changed files with 50 additions and 17 deletions
8
Makefile
8
Makefile
|
|
@ -63,15 +63,19 @@ obj-y := $(KDIRS)
|
||||||
|
|
||||||
QEMUFLAGS := -m 16G
|
QEMUFLAGS := -m 16G
|
||||||
|
|
||||||
|
MODULE_CFLAGS := -DMODULE
|
||||||
|
|
||||||
export Q
|
export Q
|
||||||
export V
|
export V
|
||||||
export obj-y
|
export obj-y
|
||||||
|
export obj-m
|
||||||
export CC
|
export CC
|
||||||
export LD
|
export LD
|
||||||
export QEMUFLAGS
|
export QEMUFLAGS
|
||||||
export CFLAGS
|
export CFLAGS
|
||||||
export LDFLAGS
|
export LDFLAGS
|
||||||
export INITRAMFS
|
export INITRAMFS
|
||||||
|
export MODULE_CFLAGS
|
||||||
|
|
||||||
PHONY: default
|
PHONY: default
|
||||||
default:
|
default:
|
||||||
|
|
@ -94,6 +98,10 @@ $(KERNEL): $(KOBJ) linker.ld
|
||||||
$(Q)echo " LD $@"
|
$(Q)echo " LD $@"
|
||||||
$(Q)$(LD) $(KOBJ) $(LDFLAGS) -T linker.ld -o $@
|
$(Q)$(LD) $(KOBJ) $(LDFLAGS) -T linker.ld -o $@
|
||||||
|
|
||||||
|
.PHONY: modules
|
||||||
|
modules:
|
||||||
|
$(Q)$(MAKE) --no-print-directory -f scripts/Makefile.build _modules obj=.
|
||||||
|
|
||||||
.PHONY: clean
|
.PHONY: clean
|
||||||
clean:
|
clean:
|
||||||
$(Q)$(MAKE) --no-print-directory -f scripts/Makefile.build _clean obj=.
|
$(Q)$(MAKE) --no-print-directory -f scripts/Makefile.build _clean obj=.
|
||||||
|
|
|
||||||
|
|
@ -49,6 +49,7 @@ void arch_init()
|
||||||
|
|
||||||
gdt_init();
|
gdt_init();
|
||||||
idt_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_STAR, ((u64)GDT_SEGMENT_CODE_RING0 << 48) | ((u64)GDT_SEGMENT_CODE_RING3 << 32));
|
||||||
wrmsr(IA32_LSTAR, (u64)_syscall_enter);
|
wrmsr(IA32_LSTAR, (u64)_syscall_enter);
|
||||||
wrmsr(IA32_FMASK, 0x3F7FD5);
|
wrmsr(IA32_FMASK, 0x3F7FD5);
|
||||||
|
|
|
||||||
|
|
@ -40,9 +40,8 @@ static const char *exception_names[] = {
|
||||||
void exception_handler(u64 no, u64 err)
|
void exception_handler(u64 no, u64 err)
|
||||||
{
|
{
|
||||||
if (no == 0x0e) {
|
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);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
#include <kernel/types.h>
|
#include <kernel/types.h>
|
||||||
|
|
||||||
|
#define IA32_EFER 0xC0000080
|
||||||
#define IA32_STAR 0xC0000081
|
#define IA32_STAR 0xC0000081
|
||||||
#define IA32_LSTAR 0xC0000082
|
#define IA32_LSTAR 0xC0000082
|
||||||
#define IA32_FMASK 0xC0000084
|
#define IA32_FMASK 0xC0000084
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,9 @@ void enter_userspace_init(struct thread *thread, uaddr_t entry)
|
||||||
|
|
||||||
tls->cur_thread = thread;
|
tls->cur_thread = thread;
|
||||||
|
|
||||||
|
void *kstack = kalloc(PAGE_SIZE, ALLOC_KERN);
|
||||||
|
tls->kernel_rsp = (vaddr_t)kstack + PAGE_SIZE;
|
||||||
|
|
||||||
pgd_load(thread->proc->pgd);
|
pgd_load(thread->proc->pgd);
|
||||||
|
|
||||||
thread->state = PROC_STATE_RUNNING;
|
thread->state = PROC_STATE_RUNNING;
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@
|
||||||
export
|
export
|
||||||
|
|
||||||
obj-y :=
|
obj-y :=
|
||||||
|
obj-m :=
|
||||||
subdir-y :=
|
subdir-y :=
|
||||||
|
|
||||||
include $(obj)/Makefile
|
include $(obj)/Makefile
|
||||||
|
|
@ -20,7 +21,14 @@ real-objs-y :=
|
||||||
subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y)))
|
subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y)))
|
||||||
real-objs-y := $(addprefix $(obj)/, $(filter-out %/, $(obj-y)))
|
real-objs-y := $(addprefix $(obj)/, $(filter-out %/, $(obj-y)))
|
||||||
subdir-objs-y := $(foreach d,$(subdir-y),$(obj)/$(d)/built-in.o)
|
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),.)
|
ifeq ($(obj),.)
|
||||||
_all: $(subdir-objs-y)
|
_all: $(subdir-objs-y)
|
||||||
|
|
@ -40,15 +48,36 @@ $(obj)/built-in.o: $(real-objs-y) $(subdir-objs-y)
|
||||||
$(Q)echo " LD $@"
|
$(Q)echo " LD $@"
|
||||||
$(Q)$(LD) -r -o $@ $(real-objs-y) $(foreach d,$(subdir-y),$(obj)/$(d)/built-in.o)
|
$(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
|
.PHONY: _clean
|
||||||
_clean: $(subdir-y)
|
_clean: $(subdir-y) $(subdir-m)
|
||||||
$(Q)echo " CLEAN $(obj)"
|
$(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
|
$(subdir-y): FORCE
|
||||||
$(Q)$(MAKE) --no-print-directory -f scripts/Makefile.build $(MAKECMDGOALS) obj=$(obj)/$@
|
$(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
|
$(obj)/%/built-in.o: FORCE
|
||||||
$(Q)$(MAKE) --no-print-directory -f scripts/Makefile.build obj=$(patsubst %/,%,$(dir $@))
|
$(Q)$(MAKE) --no-print-directory -f scripts/Makefile.build obj=$(patsubst %/,%,$(dir $@))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,14 +21,6 @@ size_t strlen(const char *c)
|
||||||
|
|
||||||
void _start()
|
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)
|
while (1)
|
||||||
continue;
|
;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue