跳转至

mac配置 rcore开发环境

qemu

安装依赖

$ brew install libffi gettext glib pkg-config autoconf automake pixman ninja

下载qemu7.0源码

$ wget https://download.qemu.org/qemu-7.0.0.tar.xz

解压

$ tar xvJf qemu-7.0.0.tar.xz

编译

$ mkdir build
$ cd build
../configure --target-list=aarch64-softmmu,riscv64-softmmu --enable-cocoa --disable-sdl

完成后, 把当前目录加入到 PATH路径里

gdb客户端

下载客户端

https://static.dev.sifive.com/dev-tools/freedom-tools/v2020.12/riscv64-unknown-elf-toolchain-10.2.0-2020.12.8-x86_64-apple-darwin.tar.gz

解压后在 bin 目录下即可找到 riscv64-unknown-elf-gdb 以及另外一些常用工具 objcopy/objdump/readelf

添加到PATH路径内即可

clion的gdb配置

由于clion没办法在gdb启动时添加命令行参数设置-ex之类的, 所以我们需要通过环境变量修改启动参数

修改 ~/.gdbinit

set arch riscv:rv64
file /Users/zhangxinyu/Documents/rCore-Tutorial-v3/os/target/riscv64gc-unknown-none-elf/release/os

makefile

# Building
TARGET := riscv64gc-unknown-none-elf
MODE := release
KERNEL_ELF := target/$(TARGET)/$(MODE)/os
KERNEL_BIN := $(KERNEL_ELF).bin
DISASM_TMP := target/$(TARGET)/$(MODE)/asm

#export PATH:="/Users/zhangxinyu/Downloads/qemu-7.0.0/build/:$(PATH)"
#export PATH:=/Users/zhangxinyu/Downloads/riscv64-unknown-elf-toolchain-10.2.0-2020.12.8-x86_64-apple-darwin/bin:$(PATH)

# Building mode argument
ifeq ($(MODE), release)
    MODE_ARG := --release
endif

# BOARD
BOARD := qemu
SBI ?= rustsbi
BOOTLOADER := ../bootloader/$(SBI)-$(BOARD).bin

# KERNEL ENTRY
KERNEL_ENTRY_PA := 0x80200000

# Binutils
OBJDUMP := rust-objdump --arch-name=riscv64
OBJCOPY := rust-objcopy --binary-architecture=riscv64

# Disassembly
DISASM ?= -x

build: env $(KERNEL_BIN)

env:
    (rustup target list | grep "riscv64gc-unknown-none-elf (installed)") || rustup target add $(TARGET)
    cargo install cargo-binutils
    rustup component add rust-src
    rustup component add llvm-tools-preview

$(KERNEL_BIN): kernel
    @$(OBJCOPY) $(KERNEL_ELF) --strip-all -O binary $@

kernel:
    @echo Platform: $(BOARD)
    @cp src/linker-$(BOARD).ld src/linker.ld
    @cargo build $(MODE_ARG)
    @rm src/linker.ld

clean:
    @cargo clean

disasm: kernel
    @$(OBJDUMP) $(DISASM) $(KERNEL_ELF) | less

disasm-vim: kernel
    @$(OBJDUMP) $(DISASM) $(KERNEL_ELF) > $(DISASM_TMP)
    @vim $(DISASM_TMP)
    @rm $(DISASM_TMP)

run: run-inner

run-inner: build
    @/Users/zhangxinyu/Downloads/qemu-7.0.0/build/qemu-system-riscv64 \
        -machine virt \
        -nographic \
        -bios $(BOOTLOADER) \
        -device loader,file=$(KERNEL_BIN),addr=$(KERNEL_ENTRY_PA)

debug: build
    @tmux new-session -d "/Users/zhangxinyu/Downloads/qemu-7.0.0/build/qemu-system-riscv64 -machine virt -nographic -bios /Users/zhangxinyu/Documents/rCore-Tutorial-v3/bootloader/rustsbi-qemu.bin -device loader,file=/Users/zhangxinyu/Documents/rCore-Tutorial-v3/os/target/riscv64gc-unknown-none-elf/release/os.bin,addr=0x80200000 -s -S"

#debug: build
#   @tmux new-session -d \
#       "qemu-system-riscv64 -machine virt -nographic -bios $(BOOTLOADER) -device loader,file=$(KERNEL_BIN),addr=$(KERNEL_ENTRY_PA) -s -S" && \
#       tmux split-window -h "riscv64-unknown-elf-gdb -ex 'file $(KERNEL_ELF)' -ex 'set arch riscv:rv64' -ex 'target remote localhost:1234'" && \
#       tmux -2 attach-session -d

gdbserver: build
    @qemu-system-riscv64 -machine virt -nographic -bios $(BOOTLOADER) -device loader,file=$(KERNEL_BIN),addr=$(KERNEL_ENTRY_PA) -s -S

gdbclient:
    @riscv64-unknown-elf-gdb -ex 'file $(KERNEL_ELF)' -ex 'set arch riscv:rv64' -ex 'target remote localhost:1234'

.PHONY: build env kernel clean disasm disasm-vim run-inner gdbserver gdbclient

添加外部工具

使其执行前运行Makefile目标debug

其他命令

tmux new-session -d "/Users/zhangxinyu/Downloads/qemu-7.0.0/build/qemu-system-riscv64 -machine virt -nographic -bios /Users/zhangxinyu/Documents/rCore_stu/bootloader/rustsbi-qemu.bin -device loader,file=/Users/zhangxinyu/Documents/rCore_stu/ch1/target/riscv64gc-unknown-none-elf/release/os.bin,addr=0x80200000 -s -S"



riscv64-unknown-elf-gdb -ex 'file /Users/zhangxinyu/Documents/rCore_stu/ch4/os/target/riscv64gc-unknown-none-elf/release/os' -ex 'set arch riscv:rv64' -ex 'target remote localhost:1234'