1
0
mirror of https://github.com/rcore-os/rCore.git synced 2024-11-26 01:43:29 +04:00
rCore/kernel/Makefile

364 lines
9.2 KiB
Makefile
Raw Normal View History

2019-03-26 07:23:29 +04:00
# Commands:
# make build Build
# make run Build and run in QEMU
# make justrun Run the last build
# make test Build and run in QEMU with specified program
# make justtest Run the last build with specified program
2019-03-26 07:23:29 +04:00
# make doc Generate docs
# make asm Open the deassemble file of the last build
# make header Open 'objdump -h' of the last build
# make addr2line Use addr2line to recover line info in backtrace
# make clean Clean
2018-08-07 12:11:48 +04:00
#
# Options:
2020-02-01 09:19:10 +04:00
# ARCH = x86_64 | riscv32 | riscv64 | aarch64 | mipsel
# D = int | in_asm | ... QEMU debug info
# MODE = debug | release
2018-08-07 12:11:48 +04:00
# LOG = off | error | warn | info | debug | trace
2020-02-01 09:19:10 +04:00
# USER_IMG = <sfsimg> Image path of user programs
# SMP = 1 | 2 | ... SMP core number
# GRAPHIC = on | off | console Enable/disable qemu graphical output, or print console to graphic output
# BOARD = qemu Run on QEMU
# | pc [ x86_64 only] Run on real pc
# | u540 [riscv64 only] Run on HiFive U540, use Sv39
# | k210 [riscv64 only] Run on K210, use Sv39
# | rocket_chip [riscv64 only] Run on Rocket Chip, use Sv39
# | raspi3 [aarch64 only] Run on Raspberry Pi 3 Model B/B+
# NET = on | off [ x86_64 only] Enable NIC
# PCI_PASSTHRU = 0000:00:00.1 [ x86_64 only] Passthrough the specified PCI device
# INIT = /bin/ls [riscv64 only] Run specified program instead of user shell
# EXTRA_NIC = on | off [ x86_64 only] Add an additional e1000 nic
# FEATURES = profile | ... Add additional features
ARCH ?= riscv64
MODE ?= release
LOG ?=
2020-02-01 09:19:10 +04:00
GRAPHIC ?= off
SMP ?= 4
PCI_PASSTHRU ?=
INIT ?=
EXTRA_NIC ?= off
qemu := qemu-system-$(ARCH)
target := $(ARCH)
build_path := target/$(target)/$(MODE)
2019-03-18 14:20:27 +04:00
kernel := $(build_path)/rcore
kernel_img := $(build_path)/kernel.img
ESP := $(build_path)/esp
OVMF := ../rboot/OVMF.fd
2018-11-28 20:39:44 +04:00
user_dir := ../user
2018-12-31 14:44:16 +04:00
### export environments ###
2020-02-01 09:19:10 +04:00
export USER_IMG = $(user_dir)/build/$(ARCH).img
export USER_QCOW2 = $(user_dir)/build/$(ARCH).qcow2
2020-02-01 09:19:10 +04:00
ifeq ($(ARCH), aarch64)
BOARD ?= raspi3
else ifeq ($(ARCH), mipsel)
BOARD ?= malta
2020-01-29 12:28:52 +04:00
else
2020-02-01 09:19:10 +04:00
BOARD ?= qemu
endif
2019-05-08 07:02:23 +04:00
# currently only mipsel architecture and rocket-chip need DTB linked to the kernel
2020-02-01 09:19:10 +04:00
ifeq ($(ARCH), mipsel)
DTB := src/arch/$(ARCH)/board/$(BOARD)/device.dtb
endif
2020-02-01 09:19:10 +04:00
ifeq ($(BOARD), rocket_chip)
DTB := src/arch/riscv/board/$(BOARD)/device.dtb
2019-05-07 04:40:25 +04:00
endif
# mipssim does not support SMP
2020-02-01 09:19:10 +04:00
ifeq ($(BOARD), mipssim)
SMP := 1
endif
### qemu options ###
2018-09-18 14:05:37 +04:00
qemu_opts := \
2020-02-01 09:19:10 +04:00
-smp cores=$(SMP)
2019-04-04 21:16:26 +04:00
qemu_net_opts :=
2018-11-29 17:39:43 +04:00
2020-02-01 09:19:10 +04:00
ifeq ($(ARCH), x86_64)
2018-11-29 17:39:43 +04:00
qemu_opts += \
-bios $(OVMF) \
-drive format=raw,file=fat:rw:$(ESP) \
2019-04-16 20:45:44 +04:00
-serial mon:stdio \
-m 4G \
-device isa-debug-exit
2020-02-01 09:19:10 +04:00
ifeq ($(BOARD), qemu)
qemu_opts += \
-drive format=qcow2,file=$(USER_QCOW2),media=disk,cache=writeback,id=sfsimg,if=none \
2019-03-30 06:03:08 +04:00
-device ahci,id=ahci0 \
-device ide-drive,drive=sfsimg,bus=ahci0.0
endif
2020-02-01 09:19:10 +04:00
ifeq ($(PCI_PASSTHRU), )
qemu_net_opts += \
2019-04-04 21:16:26 +04:00
-netdev type=tap,id=net0,script=no,downscript=no \
-device e1000e,netdev=net0
else
qemu_opts += \
-machine accel=kvm
qemu_net_opts += \
2020-02-01 09:19:10 +04:00
-device vfio-pci,host=$(PCI_PASSTHRU)
qemu_ui_opts += \
-vga std
endif
2020-02-01 09:19:10 +04:00
ifeq ($(EXTRA_NIC), on)
2019-04-02 04:27:02 +04:00
qemu_net_opts += \
-netdev type=tap,id=net1,script=no,downscript=no \
-device e1000e,netdev=net1
endif
2018-11-29 17:39:43 +04:00
2020-02-01 09:19:10 +04:00
else ifeq ($(ARCH), riscv32)
2018-11-29 17:39:43 +04:00
qemu_opts += \
2018-10-21 17:35:28 +04:00
-machine virt \
2019-04-25 21:14:40 +04:00
-serial mon:stdio \
-bios default \
2019-04-19 20:15:22 +04:00
-device loader,addr=0x80400000,file=$(kernel_img) \
-drive file=$(USER_QCOW2),format=qcow2,id=sfs \
2019-08-25 19:45:58 +04:00
-device virtio-blk-device,drive=sfs \
-device virtio-gpu-device \
-device virtio-mouse-device
qemu_net_opts += \
2019-04-04 21:16:26 +04:00
-netdev type=tap,id=net0,script=no,downscript=no \
-device virtio-net-device,netdev=net0
2020-02-01 09:19:10 +04:00
else ifeq ($(ARCH), riscv64)
ifeq ($(BOARD), u540)
2018-12-28 18:20:21 +04:00
qemu_opts += \
-machine virt \
2019-04-25 21:14:40 +04:00
-serial mon:stdio \
-bios ../tools/opensbi/fu540.elf \
2019-04-19 20:15:22 +04:00
-device loader,addr=0x80200000,file=$(kernel_img)
else
qemu_opts += \
-machine virt \
2019-04-25 21:14:40 +04:00
-serial mon:stdio \
-bios default \
2019-04-19 20:15:22 +04:00
-device loader,addr=0x80200000,file=$(kernel_img) \
-drive file=$(USER_QCOW2),format=qcow2,id=sfs \
2019-08-25 19:45:58 +04:00
-device virtio-blk-device,drive=sfs \
-device virtio-gpu-device \
-device virtio-mouse-device
2019-04-19 20:15:22 +04:00
endif
qemu_net_opts += \
2019-04-04 21:16:26 +04:00
-netdev type=tap,id=net0,script=no,downscript=no \
-device virtio-net-device,netdev=net0
2020-02-01 09:19:10 +04:00
else ifeq ($(ARCH), aarch64)
2018-11-29 17:39:43 +04:00
qemu_opts += \
2020-02-01 09:19:10 +04:00
-machine $(BOARD) \
-smp 4 \
-serial null -serial mon:stdio \
2019-05-29 14:38:59 +04:00
-kernel $(kernel_img) \
-drive file=$(USER_QCOW2),format=qcow2,id=sfs
2020-02-01 09:19:10 +04:00
else ifeq ($(ARCH), mipsel)
ifeq ($(BOARD), malta)
qemu_opts += \
2020-02-01 09:19:10 +04:00
-machine $(BOARD) -device VGA \
-serial null -serial null -serial mon:stdio \
-kernel $(kernel_img)
endif
2020-02-01 09:19:10 +04:00
ifeq ($(BOARD), mipssim)
qemu_opts += \
2020-02-01 09:19:10 +04:00
-machine $(BOARD) \
-serial mon:stdio \
-kernel $(kernel_img)
endif
endif
2020-02-01 09:19:10 +04:00
ifdef D
qemu_opts += -d $(D)
2018-07-04 12:23:11 +04:00
endif
2018-04-09 17:20:47 +04:00
2020-02-01 09:19:10 +04:00
ifeq ($(GRAPHIC), off)
qemu_opts += -nographic
endif
2020-02-01 09:19:10 +04:00
ifeq ($(NET), on)
qemu_opts += $(qemu_net_opts)
qemu := sudo $(qemu)
endif
### build args ###
2020-02-01 09:19:10 +04:00
ifeq ($(GRAPHIC), off)
FEATURES += nographic
endif
2020-02-01 09:19:10 +04:00
ifeq ($(GRAPHIC), console)
FEATURES += consolegraphic
endif
2020-02-01 09:19:10 +04:00
ifneq ($(INIT), )
FEATURES += run_cmdline
endif
2020-02-01 09:19:10 +04:00
FEATURES += board_$(BOARD)
2020-02-01 09:19:10 +04:00
build_args := \
-Z build-std=core,alloc \
--target targets/$(target).json \
--features "$(FEATURES)"
2019-04-30 17:32:32 +04:00
2020-02-01 09:19:10 +04:00
ifeq ($(MODE), release)
2018-11-29 17:39:43 +04:00
build_args += --release
endif
### prefix ###
2020-02-01 09:19:10 +04:00
ifeq ($(ARCH), x86_64)
ifeq ($(shell uname), Darwin)
prefix := x86_64-linux-musl-
2018-11-19 21:08:39 +04:00
endif
2020-02-01 09:19:10 +04:00
else ifeq ($(ARCH), riscv32)
prefix := riscv64-linux-musl-
2020-02-01 09:19:10 +04:00
else ifeq ($(ARCH), riscv64)
prefix := riscv64-linux-musl-
2020-02-01 09:19:10 +04:00
else ifeq ($(ARCH), mipsel)
2019-04-07 21:00:26 +04:00
prefix ?= mipsel-linux-musln32-
2020-02-01 09:19:10 +04:00
else ifeq ($(ARCH), aarch64)
2019-08-25 19:45:58 +04:00
prefix ?= aarch64-linux-musl-
2018-11-19 21:08:39 +04:00
endif
gdb := gdb
2020-02-01 08:47:43 +04:00
sysroot := $(shell rustc --print sysroot)
objdump := $(shell find $(sysroot) -name llvm-objdump) -print-imm-hex
objcopy := $(shell find $(sysroot) -name llvm-objcopy)
strip := $(shell find $(sysroot) -name llvm-strip)
dtc := dtc
hostcc := gcc
.PHONY: all clean build asm doc debug kernel sfsimg install run justrun test justtest
2017-04-11 11:02:09 +04:00
all: kernel
2017-04-11 11:02:09 +04:00
clean:
2018-09-18 14:05:37 +04:00
@cargo clean
2018-12-02 15:56:29 +04:00
@cd $(user_dir) && make clean
2017-04-11 11:02:09 +04:00
doc:
2020-02-01 08:47:43 +04:00
@cargo doc $(build_args) --no-deps --document-private-items
2018-12-28 18:20:21 +04:00
run: build justrun
test: build justtest
2018-07-04 12:23:11 +04:00
2018-12-02 15:51:52 +04:00
justrun:
@$(qemu) $(qemu_opts)
justtest:
# unavailable now
2020-02-01 09:19:10 +04:00
@#$(qemu) $(filter-out -serial mon:stdio, $(qemu_opts)) --append $(INIT) -serial file:../tests/stdout -monitor null
2019-03-04 11:22:19 +04:00
debug: $(kernel) $(kernel_img)
@$(qemu) $(qemu_opts) -s -S &
2018-10-26 18:02:24 +04:00
@sleep 1
@$(gdb) $(kernel) -x ../tools/gdbinit
2019-03-04 11:22:19 +04:00
build: $(kernel_img)
2018-04-09 17:20:47 +04:00
asm:
2019-03-16 11:44:57 +04:00
@$(objdump) -d $(kernel) | less
2018-04-12 19:44:25 +04:00
header:
2018-07-13 21:11:47 +04:00
@$(objdump) -h $(kernel)
sym:
@$(objdump) -t $(kernel) | less
### device tree process ###
%.dtb: %.dts
@echo Generating device tree file $@
@$(dtc) -I dts -O dtb -o $@ $<
@rm -rf src/arch/${arch}/boot/dtb.gen.s
### bootloader and kernel image ###
bootloader: $(kernel)
2020-02-01 09:19:10 +04:00
ifeq ($(ARCH), x86_64)
@cd ../rboot && make build
endif
2019-03-04 11:22:19 +04:00
$(kernel_img): kernel bootloader
2020-02-01 09:19:10 +04:00
ifeq ($(ARCH), x86_64)
@mkdir -p $(ESP)/EFI/rCore $(ESP)/EFI/Boot
@cp ../rboot/target/x86_64-unknown-uefi/release/rboot.efi $(ESP)/EFI/Boot/BootX64.efi
@cp ../rboot/rboot.conf $(ESP)/EFI/Boot/rboot.conf
@cp $(kernel) $(ESP)/EFI/rCore/kernel.elf
2020-02-01 09:19:10 +04:00
else ifeq ($(ARCH), $(filter $(ARCH), riscv32 riscv64))
2019-04-19 20:15:22 +04:00
@$(objcopy) $(kernel) --strip-all -O binary $@
2020-02-01 09:19:10 +04:00
else ifeq ($(ARCH), aarch64)
@$(objcopy) $(kernel) --strip-all -O binary $@
2020-02-01 09:19:10 +04:00
else ifeq ($(ARCH), mipsel)
# qemu-system-mipsel accepts ELF file only, so objcopy is not needed
@$(strip) $(kernel) -o $@
2018-08-07 12:11:48 +04:00
endif
2018-07-04 12:23:11 +04:00
2020-02-01 09:19:10 +04:00
kernel: $(DTB)
@echo Building $(ARCH) kernel
ifeq ($(ARCH), $(filter $(ARCH), riscv32 riscv64))
ifeq ($(BOARD), k210)
@cp src/arch/riscv/board/k210/linker.ld src/arch/riscv/boot/linker64.ld
2020-02-01 09:19:10 +04:00
else ifeq ($(BOARD), rocket_chip)
@cp src/arch/riscv/board/rocket_chip/linker.ld src/arch/riscv/boot/linker64.ld
2019-04-21 19:47:00 +04:00
else
@cp src/arch/riscv/board/u540/linker.ld src/arch/riscv/boot/linker64.ld
2019-04-21 19:47:00 +04:00
endif
@-patch -p0 -N -b \
2020-02-01 08:47:43 +04:00
$(sysroot)/lib/rustlib/src/rust/src/libcore/sync/atomic.rs \
src/arch/riscv/atomic.patch
2020-02-01 09:19:10 +04:00
else ifeq ($(ARCH), mipsel)
@for file in context entry trap ; do \
2020-02-01 09:19:10 +04:00
$(hostcc) -Dboard_$(BOARD) -E src/arch/$(ARCH)/boot/$${file}.S -o src/arch/$(ARCH)/boot/$${file}.gen.s ; \
done
2020-02-01 09:19:10 +04:00
$(hostcc) -Dboard_$(BOARD) -E src/arch/$(ARCH)/boot/linker.ld.S -o src/arch/$(ARCH)/boot/linker.ld
2018-09-18 14:05:37 +04:00
endif
2020-02-01 08:47:43 +04:00
@cargo build $(build_args)
2018-04-02 11:28:32 +04:00
2018-11-28 20:39:44 +04:00
### user programs ###
2018-12-02 15:51:52 +04:00
sfsimg:
2018-11-28 20:39:44 +04:00
@cd $(user_dir) && make sfsimg
### install ###
2020-02-01 09:19:10 +04:00
ifeq ($(BOARD), raspi3)
sd_card ?=
ifeq ($(shell uname), Darwin)
sd_card := /Volumes/boot
else ifeq ($(shell uname), Linux)
sd_card := /media/$(shell whoami)/boot
endif
ifdef sd_card
.PHONY:
2019-03-04 11:22:19 +04:00
install: $(kernel_img)
cp $(kernel_img) $(sd_card)/kernel8.img
sudo umount $(sd_card)
endif
endif
2018-12-31 21:53:33 +04:00
2020-02-01 09:19:10 +04:00
ifeq ($(BOARD), u540)
2019-03-18 14:20:27 +04:00
.PHONY:
install: $(kernel_img)
@$(objcopy) -S -O binary ../tools/opensbi/fu540.elf $(build_path)/bin
2019-04-21 19:47:00 +04:00
@dd if=$< of=$(build_path)/bin bs=0x20000 seek=16
@../tools/u540/mkimg.sh $(build_path)/bin $(build_path)/u540.img
endif
2020-02-01 09:19:10 +04:00
ifeq ($(BOARD), k210)
2019-04-21 19:47:00 +04:00
.PHONY:
install: $(kernel_img)
@$(objcopy) -S -O binary ../tools/opensbi/k210.elf $(build_path)/k210.img
@dd if=$< of=$(build_path)/k210.img bs=0x10000 seek=1
@python3 ../tools/k210/kflash.py -b 600000 $(build_path)/k210.img
2019-03-18 14:20:27 +04:00
endif
.PHONY:
addr2line:
2020-02-01 09:19:10 +04:00
@python3.7 ../tools/addr2line.py $(prefix)addr2line $(ARCH) $(MODE)