Makefile.src 5.6 KB

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