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

345 lines
8.4 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 runnet Build and run in QEMU with nic
# make justrunnet Run the last build with nic
# make runui Build and run in QEMU with gui
# make justrunui Run the last build with gui
# make runtest Build and run in QEMU with specified program
# make justruntest 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:
2019-03-31 18:04:22 +04:00
# arch = x86_64 | riscv32 | riscv64 | aarch64 | mipsel
# d = int | in_asm | ... QEMU debug info
2018-08-07 12:11:48 +04:00
# mode = debug | release
# LOG = off | error | warn | info | debug | trace
# SFSIMG = <sfsimg> SFS image path of user programs
# smp = 1 | 2 | ... SMP core number
# graphic = on | off enable/disable qemu graphical output
# board = none Running on QEMU
2019-02-15 12:10:12 +04:00
# | u540 Only available on riscv64, run on HiFive U540, use Sv39
# | raspi3 Only available on aarch64, run on Raspberry Pi 3 Model B/B+
# pci_passthru = 0000:00:00.1 Only available on x86_64, passthrough the specified PCI device
# init = /bin/ls Only available on riscv64, run specified program instead of user shell
2019-03-18 14:20:27 +04:00
arch ?= riscv64
board ?= none
2018-08-07 12:11:48 +04:00
mode ?= debug
LOG ?= debug
graphic ?= off
smp ?= 4
pci_passthru ?=
init ?=
2018-08-07 12:11:48 +04:00
2018-12-31 21:53:33 +04:00
target := $(arch)
2019-03-18 14:20:27 +04:00
build_path := target/$(target)/$(mode)
kernel := $(build_path)/rcore
kernel_img := $(build_path)/kernel.img
bootimage := $(build_path)/bootimage.bin
2019-03-04 11:22:19 +04:00
bootloader_dir = ../bootloader
bootloader := $(bootloader_dir)/target/$(target)/$(mode)/rcore-bootloader
2018-12-31 21:53:33 +04:00
bbl_path := $(PWD)/../riscv-pk
2018-11-28 20:39:44 +04:00
user_dir := ../user
2018-12-31 14:44:16 +04:00
### export environments ###
2018-11-28 20:39:44 +04:00
export ARCH = $(arch)
2019-02-15 13:00:34 +04:00
export BOARD = $(board)
export SMP = $(smp)
export DTB = $(dtb)
2019-03-23 20:49:48 +04:00
export SFSIMG = $(user_dir)/build/$(arch).qcow2
ifeq ($(arch), aarch64)
board := raspi3
export SFSIMG = $(user_dir)/build/$(arch).img
else ifeq ($(arch), mipsel)
board := malta
endif
dtb := src/arch/$(arch)/boot/dts/$(board).dtb
### qemu options ###
2018-09-18 14:05:37 +04:00
qemu_opts := \
-smp cores=$(smp)
qemu_net_opts := \
-netdev type=tap,id=net0,script=no,downscript=no
2018-11-29 17:39:43 +04:00
ifeq ($(arch), x86_64)
qemu_opts += \
-drive format=raw,file=$(bootimage) \
-drive format=qcow2,file=$(SFSIMG),media=disk,cache=writeback \
2018-11-29 17:39:43 +04:00
-serial mon:stdio \
-m 4G \
2018-11-29 17:39:43 +04:00
-device isa-debug-exit
ifeq ($(pci_passthru), )
qemu_net_opts += \
-device e1000e,netdev=net0
else
qemu_opts += \
-machine ubuntu,accel=kvm
qemu_net_opts += \
-device vfio-pci,host=$(pci_passthru)
endif
2018-11-29 17:39:43 +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-03-04 11:22:19 +04:00
-kernel $(kernel_img) \
2019-03-26 04:28:10 +04:00
-drive file=$(SFSIMG),format=qcow2,id=sfs \
-device virtio-blk-device,drive=sfs
qemu_net_opts += \
-device virtio-net-device,netdev=net0
2018-12-28 18:20:21 +04:00
else ifeq ($(arch), riscv64)
qemu_opts += \
-machine virt \
2019-03-04 11:22:19 +04:00
-kernel $(kernel_img) \
2019-03-26 04:28:10 +04:00
-drive file=$(SFSIMG),format=qcow2,id=sfs \
-device virtio-blk-device,drive=sfs
qemu_net_opts += \
-device virtio-net-device,netdev=net0
else ifeq ($(arch), aarch64)
2018-11-29 17:39:43 +04:00
qemu_opts += \
-machine $(board) \
-serial null -serial mon:stdio \
2019-03-04 11:22:19 +04:00
-kernel $(kernel_img)
else ifeq ($(arch), mipsel)
qemu_opts += \
-machine $(board) \
-serial null -serial null -serial mon:stdio \
-kernel $(kernel_img)
endif
ifdef d
2018-11-29 17:39:43 +04:00
qemu_opts += -d $(d)
2018-07-04 12:23:11 +04:00
endif
2018-04-09 17:20:47 +04:00
ifeq ($(graphic), off)
qemu_opts += -nographic
endif
### build args ###
ifneq ($(graphic), on)
features += nographic
endif
ifneq ($(init), )
features += run_cmdline
endif
ifeq ($(board), raspi3)
# qemu only has generic timer
# TODO: configure system/generic timer automatically
raspi3_timer ?= generic
ifeq ($(raspi3_timer), generic)
features += raspi3_use_generic_timer
endif
endif
ifeq ($(board), u540)
features += sv39
riscv_pk_args += --enable-sv39
endif
ifneq ($(board), none)
2018-11-29 17:39:43 +04:00
features += board_$(board)
endif
2018-12-31 21:53:33 +04:00
build_args := --target targets/$(target).json --features "$(features)"
ifeq ($(mode), release)
2018-11-29 17:39:43 +04:00
build_args += --release
endif
2018-04-19 11:57:34 +04:00
2018-04-02 13:48:07 +04:00
### prefix ###
2018-11-19 21:08:39 +04:00
ifeq ($(arch), x86_64)
ifeq ($(uname), Darwin)
prefix := x86_64-elf-
endif
else ifeq ($(arch), riscv32)
prefix := riscv64-unknown-elf-
2018-12-28 18:20:21 +04:00
else ifeq ($(arch), riscv64)
prefix := riscv64-unknown-elf-
2019-03-31 18:04:22 +04:00
else ifeq ($(arch), mipsel)
prefix ?= mipsel-linux-gnu-
2018-11-19 21:08:39 +04:00
else ifeq ($(arch), aarch64)
prefix ?= aarch64-none-elf-
ifeq (,$(shell which $(prefix)ld))
prefix := aarch64-elf-
endif
2018-11-19 21:08:39 +04:00
endif
ld := $(prefix)ld
objdump := $(prefix)objdump
objcopy := $(prefix)objcopy
cc := $(prefix)gcc
as := $(prefix)as
2019-03-04 11:22:19 +04:00
gdb := $(prefix)gdb
2019-03-05 20:46:01 +04:00
strip := $(prefix)strip
dtc := dtc
2019-03-05 20:46:01 +04:00
export CC = $(cc)
2018-04-02 13:48:07 +04:00
.PHONY: all clean build asm doc debug kernel sfsimg install run justrun runnet justrunnet runui justrunui runtest justruntest
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
2019-03-04 11:22:19 +04:00
@cd $(bootloader_dir) && make clean
2018-12-02 15:56:29 +04:00
@cd $(user_dir) && make clean
2017-04-11 11:02:09 +04:00
doc:
@cargo rustdoc -- --document-private-items
2018-12-28 18:20:21 +04:00
run: build justrun
runnet: build justrunnet
runui: build justrunui
runtest: build justruntest
2018-07-04 12:23:11 +04:00
2018-12-02 15:51:52 +04:00
justrun:
2018-12-28 18:20:21 +04:00
@qemu-system-$(arch) $(qemu_opts)
2017-04-11 11:02:09 +04:00
justrunnet: build
@sudo qemu-system-$(arch) $(qemu_opts) $(qemu_net_opts)
justrunui: build
@qemu-system-$(arch) $(qemu_opts) \
-device virtio-gpu-device \
-device virtio-mouse-device
justruntest: build
@qemu-system-$(arch) $(qemu_opts) --append $(init) -serial file:../tests/stdout -monitor null
2019-03-04 11:22:19 +04:00
debug: $(kernel) $(kernel_img)
@qemu-system-$(arch) $(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
$(dtc) -I dts -O dtb -o $@ $<
### bootloader and kernel image ###
2019-03-05 20:46:01 +04:00
$(bootloader): $(kernel)
2019-03-04 11:22:19 +04:00
ifeq ($(arch), aarch64)
need_bootloader := true
endif
ifeq ($(need_bootloader), true)
2019-03-04 11:22:19 +04:00
@echo Building $(arch) bootloader
2019-03-05 20:46:01 +04:00
@$(strip) $(kernel) -o $(kernel)_stripped
@cd $(bootloader_dir) && make arch=$(arch) mode=$(mode) payload=../kernel/$(kernel)_stripped
@rm $(kernel)_stripped
2019-03-04 11:22:19 +04:00
endif
2019-03-05 20:46:01 +04:00
$(kernel_img): kernel $(bootloader)
ifeq ($(arch), riscv32)
2018-12-31 21:53:33 +04:00
@mkdir -p target/$(target)/bbl && \
cd target/$(target)/bbl && \
$(bbl_path)/configure \
$(riscv_pk_args) \
--with-arch=rv32imac \
--disable-fp-emulation \
--host=riscv64-unknown-elf \
--with-payload=$(abspath $(kernel)) && \
2019-03-17 20:18:03 +04:00
make -j && \
2018-12-31 21:53:33 +04:00
cp bbl $(abspath $@)
2018-12-28 18:20:21 +04:00
else ifeq ($(arch), riscv64)
2018-12-31 21:53:33 +04:00
@mkdir -p target/$(target)/bbl && \
cd target/$(target)/bbl && \
$(bbl_path)/configure \
$(riscv_pk_args) \
2018-12-28 18:20:21 +04:00
--with-arch=rv64imac \
--disable-fp-emulation \
--host=riscv64-unknown-elf \
--with-payload=$(abspath $(kernel)) && \
2019-03-17 20:18:03 +04:00
make -j && \
2018-12-31 21:53:33 +04:00
cp bbl $(abspath $@)
else ifeq ($(arch), aarch64)
2019-03-05 20:46:01 +04:00
@$(objcopy) $(bootloader) --strip-all -O binary $@
2019-03-31 18:04:22 +04:00
else ifeq ($(arch), mipsel)
# qemu-system-mipsel accepts ELF file only, so don't use objcopy
@cp $(kernel) $(kernel)_orig
@$(strip) $(kernel) -o $(kernel)
2018-08-07 12:11:48 +04:00
endif
2018-07-04 12:23:11 +04:00
kernel: $(dtb)
2019-03-04 11:22:19 +04:00
@echo Building $(arch) kernel
2018-12-28 18:20:21 +04:00
ifeq ($(arch), x86_64)
2018-09-18 14:05:37 +04:00
@bootimage build $(build_args)
2019-03-18 14:20:27 +04:00
@mv target/x86_64/bootimage.bin $(bootimage)
2018-12-28 18:20:21 +04:00
else ifeq ($(arch), riscv32)
@-patch -p0 -N -b \
$(shell rustc --print sysroot)/lib/rustlib/src/rust/src/libcore/sync/atomic.rs \
src/arch/riscv32/atomic.patch
2019-01-03 15:37:04 +04:00
@cargo xbuild $(build_args)
2018-12-28 18:20:21 +04:00
else ifeq ($(arch), riscv64)
2019-02-15 13:00:34 +04:00
@cp src/arch/riscv32/board/u540/linker.ld src/arch/riscv32/boot/linker64.ld
@-patch -p0 -N -b \
$(shell rustc --print sysroot)/lib/rustlib/src/rust/src/libcore/sync/atomic.rs \
src/arch/riscv32/atomic.patch
@cargo xbuild $(build_args)
2018-12-28 18:20:21 +04:00
else ifeq ($(arch), aarch64)
2019-01-03 15:37:04 +04:00
@cargo xbuild $(build_args)
2019-03-31 18:04:22 +04:00
else ifeq ($(arch), mipsel)
@cargo xbuild $(build_args)
2018-09-18 14:05:37 +04:00
endif
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 ###
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
2019-03-18 14:20:27 +04:00
ifeq ($(board), u540)
.PHONY:
install: $(kernel_img)
@$(objcopy) -S -O binary --change-addresses -0x80000000 $< $(build_path)/bin
2019-03-19 14:23:10 +04:00
@../tools/u540/mkimg.sh $(build_path)/bin $(build_path)/sd.img
2019-03-18 14:20:27 +04:00
endif
.PHONY:
addr2line:
@python3 ../tools/addr2line.py $(prefix)addr2line $(arch) $(mode)