Makefile.src 5.5 KB

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