Makefile.src 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. HOST ?= $(shell uname -s | tr '[:upper:]' '[:lower:]')
  2. ARCH ?= ##DEFAULT_ARCH##
  3. MODE ?= debug
  4. SMP ?= 4
  5. QEMU ?= ##QEMU##
  6. GDB ?= ##GDB##
  7. FDISK ?= ##FDISK##
  8. IMG ?= ##IMAGE##
  9. COMMA := ,
  10. PROFILE = $(MODE)
  11. ifeq ($(MODE),debug)
  12. PROFILE := dev
  13. endif
  14. USER_PROGRAMS = $(shell find user-programs -type f)
  15. KERNEL_SOURCES := $(shell find src macros crates -name '*.rs' -type f)
  16. KERNEL_CARGO_MANIFESTS += $(shell find src macros crates -name Cargo.toml -type f)
  17. KERNEL_DEPS := $(KERNEL_SOURCES) $(KERNEL_CARGO_MANIFESTS)
  18. QEMU_ARGS ?= -no-reboot
  19. CARGO_FLAGS := --profile $(PROFILE) --features $(FEATURES)$(if $(SMP),$(COMMA)smp,)
  20. ifeq ($(HOST),darwin)
  21. QEMU_ACCEL ?= -accel tcg
  22. else ifeq ($(HOST),linux)
  23. ifeq ($(shell ls /dev/kvm),/dev/kvm)
  24. QEMU_ACCEL ?= -accel kvm
  25. endif
  26. endif
  27. QEMU_ARGS += $(QEMU_ACCEL)
  28. ifneq ($(DEBUG_TRAPS),)
  29. QEMU_ARGS += -d cpu_reset,int,guest_errors -D build/qemu.log
  30. endif
  31. ifneq ($(SMP),)
  32. QEMU_ARGS += -smp $(SMP)
  33. endif
  34. ifeq ($(ARCH),riscv64)
  35. BINARY_DIR_BASE := build/riscv64gc-unknown-none-elf
  36. BINARY_DIR := $(BINARY_DIR_BASE)/$(MODE)
  37. QEMU_ARGS += \
  38. -machine virt -kernel $(BINARY_DIR)/eonix_kernel \
  39. -device virtio-blk-device,drive=disk0,bus=virtio-mmio-bus.0 \
  40. -device virtio-net-device,netdev=mynet0 \
  41. -drive id=disk0,file=build/boot-riscv64.img,format=raw,if=none \
  42. -netdev user,id=mynet0 \
  43. -rtc base=utc
  44. ifneq ($(IMG),)
  45. QEMU_ARGS += \
  46. -drive id=disk1,file=$(IMG),format=raw,if=none \
  47. -device virtio-blk-device,drive=disk1,bus=virtio-mmio-bus.1
  48. endif
  49. CARGO_FLAGS += --target riscv64gc-unknown-none-elf
  50. .PHONY: build
  51. build: $(BINARY_DIR)/eonix_kernel build/boot-riscv64.img
  52. else ifeq ($(ARCH),loongarch64)
  53. BINARY_DIR_BASE := build/loongarch64-unknown-none-softfloat
  54. BINARY_DIR := $(BINARY_DIR_BASE)/$(MODE)
  55. QEMU_ARGS += \
  56. -machine virt -kernel $(BINARY_DIR)/eonix_kernel -m 1G \
  57. -device virtio-blk-pci,drive=disk0 \
  58. -device virtio-net-pci,netdev=mynet0 \
  59. -drive id=disk0,file=build/boot-loongarch64.img,format=raw,if=none \
  60. -netdev user,id=mynet0,hostfwd=tcp::5555-:5555,hostfwd=udp::5555-:5555 \
  61. -rtc base=utc
  62. ifneq ($(IMG),)
  63. QEMU_ARGS += \
  64. -drive id=disk1,file=$(IMG),format=raw,if=none \
  65. -device virtio-blk-pci,drive=disk1
  66. endif
  67. CARGO_FLAGS += --target loongarch64-unknown-none-softfloat
  68. .PHONY: build
  69. build: $(BINARY_DIR)/eonix_kernel build/boot-loongarch64.img
  70. else ifeq ($(ARCH),x86_64)
  71. BINARY_DIR_BASE := build/x86_64-unknown-none
  72. BINARY_DIR := $(BINARY_DIR_BASE)/$(MODE)
  73. QEMU_ARGS += \
  74. -machine q35 \
  75. -device ahci,id=ahci \
  76. -device ide-hd,drive=disk0,bus=ahci.0 \
  77. -device e1000e,netdev=mynet0 \
  78. -drive id=disk0,file=build/boot-x86_64.img,format=raw,if=none \
  79. -netdev user,id=mynet0
  80. ifneq ($(IMG),)
  81. QEMU_ARGS += \
  82. -drive id=disk1,file=$(IMG),format=raw,if=none \
  83. -device ide-hd,drive=disk1,bus=ahci.1
  84. endif
  85. CARGO_FLAGS += --target x86_64-unknown-none.json
  86. .PHONY: build
  87. build: $(BINARY_DIR)/eonix_kernel build/boot-x86_64.img
  88. endif
  89. .PHONY: run
  90. run: build build/kernel.sym
  91. $(QEMU) $(QEMU_ARGS) -display none -serial mon:stdio
  92. .PHONY: srun
  93. srun: build build/kernel.sym
  94. $(QEMU) $(QEMU_ARGS) -display none -S -s -serial mon:stdio
  95. .PHONY: test-run
  96. test-run: build
  97. $(QEMU) $(QEMU_ARGS) -display none -serial stdio
  98. .PHONY: clean
  99. clean:
  100. -rm -rf build
  101. -mkdir build
  102. .PHONY: clean-all
  103. clean-all: clean
  104. -rm Makefile
  105. .PHONY: debug
  106. debug: build/kernel.sym
  107. -RUST_GDB=$(GDB) rust-gdb --symbols=build/kernel.sym \
  108. -iex 'source pretty-print.py' \
  109. -iex 'set pagination off' \
  110. -iex 'set output-radix 16' \
  111. -iex 'set print asm-demangle on' \
  112. -iex 'set print pretty on' \
  113. -iex 'target remote:1234'
  114. -killall $(QEMU)
  115. .PHONY: tmux-debug
  116. tmux-debug:
  117. tmux new-session -s gbos-debug -d
  118. -tmux split-window -t gbos-debug -hf
  119. -tmux send-keys -t gbos-debug:1.1 'make srun' C-m
  120. -tmux send-keys -t gbos-debug:1.2 'make debug' C-m C-m
  121. -tmux attach -t gbos-debug
  122. tmux kill-session -t gbos-debug
  123. $(BINARY_DIR)/eonix_kernel: $(KERNEL_DEPS)
  124. CARGO_TARGET_DIR=build cargo build $(CARGO_FLAGS)
  125. build/kernel.sym: $(BINARY_DIR)/eonix_kernel
  126. CARGO_TARGET_DIR=build cargo objcopy -q $(CARGO_FLAGS) -- --only-keep-debug build/kernel.sym
  127. build/fs-%.img: user-programs/init_script_%.sh script/build-img.sh $(USER_PROGRAMS)
  128. ARCH=$* OUTPUT=$@ sh script/build-img.sh
  129. build/mbr.bin: $(BINARY_DIR)/eonix_kernel
  130. CARGO_TARGET_DIR=build cargo objcopy -q $(CARGO_FLAGS) -- -O binary -j .mbr build/mbr.bin
  131. build/stage1.bin: $(BINARY_DIR)/eonix_kernel
  132. CARGO_TARGET_DIR=build cargo objcopy -q $(CARGO_FLAGS) -- -O binary -j .stage1 build/stage1.bin
  133. build/kernel.bin: $(BINARY_DIR)/eonix_kernel
  134. CARGO_TARGET_DIR=build cargo objcopy -q $(CARGO_FLAGS) -- -O binary --strip-debug \
  135. -R .mbr -R .stage1 build/kernel.bin
  136. build/boot-x86_64.img: build/fs-x86_64.img build/mbr.bin build/stage1.bin build/kernel.bin
  137. dd if=build/mbr.bin of=$@ bs=512 count=1 conv=notrunc 2> /dev/null
  138. dd if=build/stage1.bin of=$@ bs=512 seek=1 conv=notrunc 2> /dev/null
  139. dd if=build/kernel.bin of=$@ bs=4096 seek=1 conv=notrunc 2> /dev/null
  140. dd if=$< of=$@ bs=$(shell expr 4 \* 1024 \* 1024) \
  141. seek=1 conv=notrunc 2> /dev/null
  142. sh -c 'echo n; echo; echo; echo 8192; echo; echo a; echo w' \
  143. | $(FDISK) $@ 2> /dev/null > /dev/null
  144. build/boot-riscv64.img: build/fs-riscv64.img
  145. dd if=$< of=$@ bs=$(shell expr 4 \* 1024 \* 1024) \
  146. seek=1 conv=notrunc 2> /dev/null
  147. sh -c 'echo n; echo; echo; echo 8192; echo; echo a; echo w' \
  148. | $(FDISK) $@ 2> /dev/null > /dev/null
  149. build/boot-loongarch64.img: build/fs-loongarch64.img
  150. dd if=$< of=$@ bs=$(shell expr 4 \* 1024 \* 1024) \
  151. seek=1 conv=notrunc 2> /dev/null
  152. sh -c 'echo n; echo; echo; echo 8192; echo; echo a; echo w' \
  153. | $(FDISK) $@ 2> /dev/null > /dev/null
  154. .DEFAULT_GOAL := build