Seperate kernel and userspace targets

This commit is contained in:
Jeremy Soller 2016-09-09 19:08:04 -06:00
parent 729c7fd004
commit 0b1265d87e
5 changed files with 97 additions and 66 deletions

155
Makefile
View file

@ -1,15 +1,50 @@
ARCH?=x86_64
# Kernel variables
KTARGET=$(ARCH)-unknown-none
KBUILD=build/kernel
KRUSTC=./krustc.sh
KRUSTCFLAGS=--target $(KTARGET).json -O -C soft-float
KCARGO=RUSTC="$(KRUSTC)" cargo
KCARGOFLAGS=--target $(KTARGET).json -- -O -C soft-float
# Userspace variables
TARGET=$(ARCH)-unknown-redox
BUILD=build/userspace
RUSTC=./rustc.sh
RUSTCFLAGS=--target $(TARGET).json -O -C soft-float --cfg redox
CARGO=RUSTC="$(RUSTC)" cargo
CARGOFLAGS=--target $(TARGET).json -- -O -C soft-float --cfg redox
# Default targets
.PHONY: all clean qemu bochs FORCE
all: $(KBUILD)/harddrive.bin
clean:
cargo clean
cargo clean --manifest-path libstd/Cargo.toml
cargo clean --manifest-path init/Cargo.toml
rm -rf build
FORCE:
# Emulation
QEMU=qemu-system-$(ARCH)
QEMUFLAGS=-serial mon:stdio -d guest_errors
RUSTCFLAGS=--target $(ARCH)-unknown-redox.json -O -C soft-float --cfg redox
CARGOFLAGS=--target $(ARCH)-unknown-redox.json -- -O -C soft-float --cfg redox
ifeq ($(ARCH),arm)
LD=$(ARCH)-none-eabi-ld
QEMUFLAGS+=-cpu arm1176 -machine integratorcp
QEMUFLAGS+=-nographic
$(KBUILD)/kernel.list: $(KBUILD)/kernel.bin
$(ARCH)-none-eabi-objdump -C -D $< > $@
$(KBUILD)/harddrive.bin: $(KBUILD)/kernel.bin
cp $< $@
qemu: $(KBUILD)/harddrive.bin
$(QEMU) $(QEMUFLAGS) -kernel $<
else
LD=ld
QEMUFLAGS+=-enable-kvm -cpu host -machine q35 -smp 4
@ -22,69 +57,63 @@ else
LD=$(ARCH)-elf-ld
QEMUFLAGS=
endif
endif
all: build/kernel.bin
list: build/kernel.list
run: bochs
bochs: build/harddrive.bin
bochs -f bochs.$(ARCH)
FORCE:
build/libcore.rlib: rust/src/libcore/lib.rs
mkdir -p build
./rustc.sh $(RUSTCFLAGS) -o $@ $<
build/librand.rlib: rust/src/librand/lib.rs build/libcore.rlib
./rustc.sh $(RUSTCFLAGS) -o $@ $<
build/liballoc.rlib: rust/src/liballoc/lib.rs build/libcore.rlib
./rustc.sh $(RUSTCFLAGS) -o $@ $<
build/librustc_unicode.rlib: rust/src/librustc_unicode/lib.rs build/libcore.rlib
./rustc.sh $(RUSTCFLAGS) -o $@ $<
build/libcollections.rlib: rust/src/libcollections/lib.rs build/libcore.rlib build/liballoc.rlib build/librustc_unicode.rlib
./rustc.sh $(RUSTCFLAGS) -o $@ $<
build/libstd.rlib: libstd/Cargo.toml libstd/src/** build/libcore.rlib build/liballoc.rlib build/librustc_unicode.rlib build/libcollections.rlib build/librand.rlib
RUSTC="./rustc.sh" cargo rustc --manifest-path $< $(CARGOFLAGS) -o $@
cp libstd/target/$(ARCH)-unknown-redox/debug/deps/*.rlib build
build/init: init/Cargo.toml init/src/*.rs build/libstd.rlib
RUSTC="./rustc.sh" cargo rustc --manifest-path $< $(CARGOFLAGS) -o $@
strip $@
build/libkernel.a: build/libcore.rlib build/liballoc.rlib build/libcollections.rlib build/init kernel/** FORCE
RUSTC="./rustc.sh" cargo rustc $(CARGOFLAGS) -o $@
build/kernel.bin: build/libkernel.a
$(LD) --gc-sections -z max-page-size=0x1000 -T arch/$(ARCH)/src/linker.ld -o $@ $<
strip $@
ifeq ($(ARCH),arm)
build/kernel.list: build/kernel.bin
$(ARCH)-none-eabi-objdump -C -D $< > $@
qemu: build/kernel.bin
$(QEMU) $(QEMUFLAGS) -kernel $<
else
build/kernel.list: build/kernel.bin
$(KBUILD)/kernel.list: $(KBUILD)/kernel.bin
objdump -C -M intel -D $< > $@
build/harddrive.bin: build/kernel.bin bootloader/$(ARCH)/**
nasm -f bin -o $@ -D ARCH_$(ARCH) -ibootloader/$(ARCH)/ -ibuild/ bootloader/$(ARCH)/harddrive.asm
$(KBUILD)/harddrive.bin: $(KBUILD)/kernel.bin bootloader/$(ARCH)/**
nasm -f bin -o $@ -D ARCH_$(ARCH) -ibootloader/$(ARCH)/ -i$(KBUILD)/ bootloader/$(ARCH)/harddrive.asm
qemu: build/harddrive.bin
qemu: $(KBUILD)/harddrive.bin
$(QEMU) $(QEMUFLAGS) -drive file=$<,format=raw,index=0,media=disk
endif
clean:
cargo clean
cargo clean --manifest-path libstd/Cargo.toml
cargo clean --manifest-path init/Cargo.toml
rm -rf build/*
bochs: $(KBUILD)/harddrive.bin
bochs -f bochs.$(ARCH)
# Kernel recipes
$(KBUILD)/libcore.rlib: rust/src/libcore/lib.rs
mkdir -p $(KBUILD)
$(KRUSTC) $(KRUSTCFLAGS) -o $@ $<
$(KBUILD)/librand.rlib: rust/src/librand/lib.rs $(KBUILD)/libcore.rlib
$(KRUSTC) $(KRUSTCFLAGS) -o $@ $<
$(KBUILD)/liballoc.rlib: rust/src/liballoc/lib.rs $(KBUILD)/libcore.rlib
$(KRUSTC) $(KRUSTCFLAGS) -o $@ $<
$(KBUILD)/librustc_unicode.rlib: rust/src/librustc_unicode/lib.rs $(KBUILD)/libcore.rlib
$(KRUSTC) $(KRUSTCFLAGS) -o $@ $<
$(KBUILD)/libcollections.rlib: rust/src/libcollections/lib.rs $(KBUILD)/libcore.rlib $(KBUILD)/liballoc.rlib $(KBUILD)/librustc_unicode.rlib
$(KRUSTC) $(KRUSTCFLAGS) -o $@ $<
$(KBUILD)/libkernel.a: $(KBUILD)/libcore.rlib $(KBUILD)/liballoc.rlib $(KBUILD)/libcollections.rlib $(BUILD)/init kernel/** FORCE
$(KCARGO) rustc $(KCARGOFLAGS) -o $@
$(KBUILD)/kernel.bin: $(KBUILD)/libkernel.a
$(LD) --gc-sections -z max-page-size=0x1000 -T arch/$(ARCH)/src/linker.ld -o $@ $<
# Userspace recipes
$(BUILD)/libcore.rlib: rust/src/libcore/lib.rs
mkdir -p $(BUILD)
$(RUSTC) $(RUSTCFLAGS) -o $@ $<
$(BUILD)/librand.rlib: rust/src/librand/lib.rs $(BUILD)/libcore.rlib
$(RUSTC) $(RUSTCFLAGS) -o $@ $<
$(BUILD)/liballoc.rlib: rust/src/liballoc/lib.rs $(BUILD)/libcore.rlib
$(RUSTC) $(RUSTCFLAGS) -o $@ $<
$(BUILD)/librustc_unicode.rlib: rust/src/librustc_unicode/lib.rs $(BUILD)/libcore.rlib
$(RUSTC) $(RUSTCFLAGS) -o $@ $<
$(BUILD)/libcollections.rlib: rust/src/libcollections/lib.rs $(BUILD)/libcore.rlib $(BUILD)/liballoc.rlib $(BUILD)/librustc_unicode.rlib
$(RUSTC) $(RUSTCFLAGS) -o $@ $<
$(BUILD)/libstd.rlib: libstd/Cargo.toml libstd/src/** $(BUILD)/libcore.rlib $(BUILD)/liballoc.rlib $(BUILD)/librustc_unicode.rlib $(BUILD)/libcollections.rlib $(BUILD)/librand.rlib
$(CARGO) rustc --verbose --manifest-path $< $(CARGOFLAGS) -o $@
cp libstd/target/$(TARGET)/debug/deps/*.rlib $(BUILD)
$(BUILD)/init: init/Cargo.toml init/src/*.rs $(BUILD)/libstd.rlib
$(CARGO) rustc --manifest-path $< $(CARGOFLAGS) -o $@

View file

@ -142,7 +142,7 @@ pub extern fn kmain() {
let stderr = syscall::open("debug:".as_bytes(), 0);
println!("STDERR: {:?}", stderr);
let elf = elf::Elf::from(include_bytes!("../build/init")).expect("could not load elf");
let elf = elf::Elf::from(include_bytes!("../build/userspace/init")).expect("could not load elf");
elf.run();
/*

2
krustc.sh Executable file
View file

@ -0,0 +1,2 @@
#!/bin/bash
RUST_BACKTRACE=1 rustc -L build/kernel --cfg redox $*

View file

@ -1,2 +1,2 @@
#!/bin/bash
RUST_BACKTRACE=1 rustc -L build --cfg redox $*
RUST_BACKTRACE=1 rustc -L build/userspace --cfg redox $*

View file

@ -13,7 +13,7 @@
"dynamic-linking": false,
"executables": true,
"relocation-model": "static",
"code-model": "kernel",
"code-model": "default",
"disable-redzone": true,
"eliminate-frame-pointer": false,
"exe-suffix": "",