From 0cbec918e054c4750cab3fdd0924b22a6dec96b3 Mon Sep 17 00:00:00 2001 From: Jordan Carlin Date: Mon, 20 Jan 2025 14:27:03 -0800 Subject: [PATCH 01/11] Add svade to device trees --- linux/devicetree/wally-artya7.dts | 2 +- linux/devicetree/wally-vcu108.dts | 2 +- linux/devicetree/wally-vcu118.dts | 2 +- linux/devicetree/wally-virt.dts | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/linux/devicetree/wally-artya7.dts b/linux/devicetree/wally-artya7.dts index d6ecc02b9..56a12de31 100644 --- a/linux/devicetree/wally-artya7.dts +++ b/linux/devicetree/wally-artya7.dts @@ -31,7 +31,7 @@ status = "okay"; compatible = "riscv"; riscv,isa-base = "rv64i"; - riscv,isa-extensions = "i", "m", "a", "f", "d", "c", "sstc", "svadu", "svinval", "svnapot", "svpbmt", "zba", "zbb", "zbc", "zbs", "zca", "zcb", "zcd", "zfa", "zfh", "zkn", "zkt", "zicbom", "zicboz", "zicntr", "zicond", "zicsr", "zifencei", "zihpm"; + riscv,isa-extensions = "i", "m", "a", "f", "d", "c", "sstc", "svade", "svadu", "svinval", "svnapot", "svpbmt", "zba", "zbb", "zbc", "zbs", "zca", "zcb", "zcd", "zfa", "zfh", "zkn", "zkt", "zicbom", "zicboz", "zicntr", "zicond", "zicsr", "zifencei", "zihpm"; riscv,cboz-block-size = <64>; riscv,cbom-block-size = <64>; mmu-type = "riscv,sv48"; diff --git a/linux/devicetree/wally-vcu108.dts b/linux/devicetree/wally-vcu108.dts index 5158e05a2..57e236c9c 100644 --- a/linux/devicetree/wally-vcu108.dts +++ b/linux/devicetree/wally-vcu108.dts @@ -31,7 +31,7 @@ status = "okay"; compatible = "riscv"; riscv,isa-base = "rv64i"; - riscv,isa-extensions = "i", "m", "a", "f", "d", "c", "sstc", "svadu", "svinval", "svnapot", "svpbmt", "zba", "zbb", "zbc", "zbs", "zca", "zcb", "zcd", "zfa", "zfh", "zkn", "zkt", "zicbom", "zicboz", "zicntr", "zicond", "zicsr", "zifencei", "zihpm"; + riscv,isa-extensions = "i", "m", "a", "f", "d", "c", "sstc", "svade", "svadu", "svinval", "svnapot", "svpbmt", "zba", "zbb", "zbc", "zbs", "zca", "zcb", "zcd", "zfa", "zfh", "zkn", "zkt", "zicbom", "zicboz", "zicntr", "zicond", "zicsr", "zifencei", "zihpm"; riscv,cboz-block-size = <64>; riscv,cbom-block-size = <64>; mmu-type = "riscv,sv48"; diff --git a/linux/devicetree/wally-vcu118.dts b/linux/devicetree/wally-vcu118.dts index 89f917635..bdfafa2d0 100644 --- a/linux/devicetree/wally-vcu118.dts +++ b/linux/devicetree/wally-vcu118.dts @@ -31,7 +31,7 @@ status = "okay"; compatible = "riscv"; riscv,isa-base = "rv64i"; - riscv,isa-extensions = "i", "m", "a", "f", "d", "c", "sstc", "svadu", "svinval", "svnapot", "svpbmt", "zba", "zbb", "zbc", "zbs", "zca", "zcb", "zcd", "zfa", "zfh", "zkn", "zkt", "zicbom", "zicboz", "zicntr", "zicond", "zicsr", "zifencei", "zihpm"; + riscv,isa-extensions = "i", "m", "a", "f", "d", "c", "sstc", "svade", "svadu", "svinval", "svnapot", "svpbmt", "zba", "zbb", "zbc", "zbs", "zca", "zcb", "zcd", "zfa", "zfh", "zkn", "zkt", "zicbom", "zicboz", "zicntr", "zicond", "zicsr", "zifencei", "zihpm"; riscv,cboz-block-size = <64>; riscv,cbom-block-size = <64>; mmu-type = "riscv,sv48"; diff --git a/linux/devicetree/wally-virt.dts b/linux/devicetree/wally-virt.dts index c250e6920..b23d31015 100644 --- a/linux/devicetree/wally-virt.dts +++ b/linux/devicetree/wally-virt.dts @@ -31,7 +31,7 @@ status = "okay"; compatible = "riscv"; riscv,isa-base = "rv64i"; - riscv,isa-extensions = "i", "m", "a", "f", "d", "c", "sstc", "svadu", "svinval", "svnapot", "svpbmt", "zba", "zbb", "zbc", "zbs", "zca", "zcb", "zcd", "zfa", "zfh", "zkn", "zkt", "zicbom", "zicboz", "zicntr", "zicond", "zicsr", "zifencei", "zihpm"; + riscv,isa-extensions = "i", "m", "a", "f", "d", "c", "sstc", "svade", "svadu", "svinval", "svnapot", "svpbmt", "zba", "zbb", "zbc", "zbs", "zca", "zcb", "zcd", "zfa", "zfh", "zkn", "zkt", "zicbom", "zicboz", "zicntr", "zicond", "zicsr", "zifencei", "zihpm"; riscv,cboz-block-size = <64>; riscv,cbom-block-size = <64>; mmu-type = "riscv,sv48"; From ff026540ffe77fc5acafef50c565832cb14eb4d2 Mon Sep 17 00:00:00 2001 From: Jordan Carlin Date: Mon, 20 Jan 2025 17:17:43 -0800 Subject: [PATCH 02/11] Add qemuBoot.sh script for linux QEMU boot --- linux/qemuBoot.sh | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100755 linux/qemuBoot.sh diff --git a/linux/qemuBoot.sh b/linux/qemuBoot.sh new file mode 100755 index 000000000..b756ad34a --- /dev/null +++ b/linux/qemuBoot.sh @@ -0,0 +1,10 @@ +#!/bin/bash +BUILDROOT="${BUILDROOT:-$RISCV/buildroot}" +IMAGES="$BUILDROOT"/output/images +qemu-system-riscv64 \ + -M virt -m 256M -nographic \ + -bios "$IMAGES"/fw_jump.bin \ + -kernel "$IMAGES"/Image \ + -initrd "$IMAGES"/rootfs.cpio \ + -dtb "$IMAGES"/wally-virt.dtb \ + -cpu rva22s64,zicond=true,zfa=true,zfh=true,zcb=true,zbc=true,zkn=true,sstc=true,svadu=true,svnapot=true From acf886651e88f57b98f330146e45d82bbaaad00e Mon Sep 17 00:00:00 2001 From: Jordan Carlin Date: Mon, 20 Jan 2025 19:16:55 -0800 Subject: [PATCH 03/11] Simplify genInitMem.sh using qemuBoot.sh --- linux/genInitMem.sh | 33 +++++++++------------------------ linux/qemuBoot.sh | 8 +++++++- 2 files changed, 16 insertions(+), 25 deletions(-) diff --git a/linux/genInitMem.sh b/linux/genInitMem.sh index b77445add..65e5c9825 100755 --- a/linux/genInitMem.sh +++ b/linux/genInitMem.sh @@ -1,14 +1,12 @@ #!/bin/bash set -e tcpPort=1235 -imageDir=$RISCV/buildroot/output/images tvDir=$RISCV/linux-testvectors rawRamFile="$tvDir/ramGDB.bin" ramFile="$tvDir/ram.bin" rawBootmemFile="$tvDir/bootmemGDB.bin" bootmemFile="$tvDir/bootmem.bin" rawUntrimmedBootmemFile="$tvDir/untrimmedBootmemFileGDB.bin" -DEVICE_TREE=${imageDir}/wally-virt.dtb if ! mkdir -p "$tvDir"; then echo "Error: unable to create linux testvector directory $tvDir!">&2 @@ -26,24 +24,15 @@ if ! test -w "$tvDir"; then fi echo "Launching QEMU in replay mode!" -(qemu-system-riscv64 \ --M virt -m 256M -dtb "$DEVICE_TREE" \ --nographic \ --bios "$imageDir"/fw_jump.bin -kernel "$imageDir"/Image -append "root=/dev/vda ro" -initrd "$imageDir"/rootfs.cpio \ --gdb tcp::$tcpPort -S) \ -& riscv64-unknown-elf-gdb --quiet \ --ex "set pagination off" \ --ex "set logging overwrite on" \ --ex "set logging redirect on" \ --ex "set confirm off" \ --ex "target extended-remote :$tcpPort" \ --ex "maintenance packet Qqemu.PhyMemMode:1" \ --ex "printf \"Creating $rawBootmemFile\n\"" \ --ex "dump binary memory $rawBootmemFile 0x1000 0x1fff" \ --ex "printf \"Creating $rawRamFile\n\"" \ --ex "dump binary memory $rawRamFile 0x80000000 0x8fffffff" \ --ex "kill" \ --ex "q" +./qemuBoot.sh --gdb $tcpPort & +riscv64-unknown-elf-gdb -batch \ + -ex "target remote :$tcpPort" \ + -ex "maintenance packet Qqemu.PhyMemMode:1" \ + -ex "printf \"Creating $rawBootmemFile\n\"" \ + -ex "dump binary memory $rawBootmemFile 0x1000 0x1fff" \ + -ex "printf \"Creating $rawRamFile\n\"" \ + -ex "dump binary memory $rawRamFile 0x80000000 0x8fffffff" \ + -ex "kill" \ echo "Changing Endianness" # Extend files to 8 byte multiple @@ -55,7 +44,3 @@ objcopy --reverse-bytes=8 -F binary "$rawBootmemFile" "$bootmemFile" rm -f "$rawRamFile" "$rawBootmemFile" "$rawUntrimmedBootmemFile" echo "genInitMem.sh completed!" -echo "You may want to restrict write access to $tvDir now and give cad ownership of it." -echo "Run the following:" -echo " sudo chown -R cad:cad $tvDir" -echo " sudo chmod -R go-w $tvDir" diff --git a/linux/qemuBoot.sh b/linux/qemuBoot.sh index b756ad34a..b5f130fc6 100755 --- a/linux/qemuBoot.sh +++ b/linux/qemuBoot.sh @@ -1,10 +1,16 @@ #!/bin/bash BUILDROOT="${BUILDROOT:-$RISCV/buildroot}" IMAGES="$BUILDROOT"/output/images + +if [[ "$1" == "--gdb" && -n "$2" ]]; then + GDB_FLAG="-gdb tcp::$2 -S" +fi + qemu-system-riscv64 \ -M virt -m 256M -nographic \ -bios "$IMAGES"/fw_jump.bin \ -kernel "$IMAGES"/Image \ -initrd "$IMAGES"/rootfs.cpio \ -dtb "$IMAGES"/wally-virt.dtb \ - -cpu rva22s64,zicond=true,zfa=true,zfh=true,zcb=true,zbc=true,zkn=true,sstc=true,svadu=true,svnapot=true + -cpu rva22s64,zicond=true,zfa=true,zfh=true,zcb=true,zbc=true,zkn=true,sstc=true,svadu=true,svnapot=true \ + $GDB_FLAG From 6dd5b9b2686b5d528861e1ca33780bde5b3cd1dd Mon Sep 17 00:00:00 2001 From: Jordan Carlin Date: Mon, 20 Jan 2025 19:21:00 -0800 Subject: [PATCH 04/11] Add header to qemuBoot --- linux/qemuBoot.sh | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/linux/qemuBoot.sh b/linux/qemuBoot.sh index b5f130fc6..9cef11bc5 100755 --- a/linux/qemuBoot.sh +++ b/linux/qemuBoot.sh @@ -1,4 +1,30 @@ #!/bin/bash +########################################### +## Boot linux on QEMU configured to match Wally +## +## Written: Jordan Carlin, jcarlin@hmc.edu +## Created: 20 January 2025 +## Modified: +## +## A component of the CORE-V-WALLY configurable RISC-V project. +## https://github.com/openhwgroup/cvw +## +## Copyright (C) 2021-25 Harvey Mudd College & Oklahoma State University +## +## SPDX-License-Identifier: Apache-2.0 WITH SHL-2.1 +## +## Licensed under the Solderpad Hardware License v 2.1 (the “License”); you may not use this file +## except in compliance with the License, or, at your option, the Apache License version 2.0. You +## may obtain a copy of the License at +## +## https:##solderpad.org/licenses/SHL-2.1/ +## +## Unless required by applicable law or agreed to in writing, any work distributed under the +## License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, +## either express or implied. See the License for the specific language governing permissions +## and limitations under the License. +################################################################################################ + BUILDROOT="${BUILDROOT:-$RISCV/buildroot}" IMAGES="$BUILDROOT"/output/images From 408e7365f97502136083a2aab17e1ba90867b9c9 Mon Sep 17 00:00:00 2001 From: Jordan Carlin Date: Tue, 21 Jan 2025 01:19:36 -0800 Subject: [PATCH 05/11] Integrate genInitMem.sh directly into Makefile and simplify --- linux/Makefile | 89 +++++++++++++++++++++++++++------------------ linux/genInitMem.sh | 46 ----------------------- 2 files changed, 54 insertions(+), 81 deletions(-) delete mode 100755 linux/genInitMem.sh diff --git a/linux/Makefile b/linux/Makefile index c0b6c3511..8852c02ad 100644 --- a/linux/Makefile +++ b/linux/Makefile @@ -1,25 +1,26 @@ +# .SECONDEXPANSION: + BUILDROOT := buildroot IMAGES := ${BUILDROOT}/output/images WALLYLINUX := $(WALLY)/linux DIS := ${IMAGES}/disassembly BR2_EXTERNAL_TREE := $(WALLYLINUX)/br2-external-tree +LINUX_TEST_VECTORS := $(RISCV)/linux-testvectors # set sudo if needed depending on $RISCV SUDO := $(shell mkdir -p $(RISCV)/.test > /dev/null 2>&1 || echo sudo) # Device tree files -DTS ?= $(shell find devicetree -type f -regex ".*\.dts" | sort) -DTB := $(DTS:%.dts=%.dtb) -DTB := $(foreach name, $(DTB), $(IMAGES)/$(shell basename $(name))) +DTS ?= $(wildcard devicetree/*.dts) +DTB := $(foreach name, $(DTS:%.dts=%.dtb), $(IMAGES)/$(notdir $(name))) # Disassembly stuff BINARIES := fw_jump.elf vmlinux busybox -OBJDUMPS := $(foreach name, $(BINARIES), $(basename $(name) .elf)) -OBJDUMPS := $(foreach name, $(OBJDUMPS), $(DIS)/$(name).objdump) +OBJDUMPS := $(foreach name, $(basename $(BINARIES) .elf), $(DIS)/$(name).objdump) -.PHONY: all generate disassemble install clean cleanDTB check_write_permissions check_environment +.PHONY: all check_environment check_write_permissions config Image disassemble generate install dumptvs clean cleanDTB -all: check_environment check_write_permissions clean download Image disassemble install dumptvs +all: check_write_permissions clean config Image disassemble install dumptvs check_environment: $(RISCV) ifeq ($(findstring :$(RISCV)/lib:,:$(LD_LIBRARY_PATH):),) @@ -28,7 +29,7 @@ ifeq ($(findstring :$(RISCV)/lib:,:$(LD_LIBRARY_PATH):),) && exit 1) endif -check_write_permissions: +check_write_permissions: check_environment ifeq ($(SUDO), sudo) @echo "Cannot write to '$(RISCV)'." \ "Using sudo (you may be prompted for your password several times throughout the install)" @@ -38,24 +39,44 @@ endif && exit 1) @$(SUDO) rm -r $(RISCV)/.test -Image: check_environment +Image: check_environment $(BUILDROOT) bash -c "unset LD_LIBRARY_PATH; $(MAKE) -C $(BUILDROOT)" - $(MAKE) generate +# $(MAKE) generate @echo "Buildroot Image successfully generated." -install: check_write_permissions check_environment +install: check_write_permissions $(SUDO) rm -rf $(RISCV)/$(BUILDROOT) $(SUDO) mv $(BUILDROOT) $(RISCV)/$(BUILDROOT) @echo "Buildroot successfully installed." -dumptvs: check_write_permissions check_environment - $(SUDO) mkdir -p $(RISCV)/linux-testvectors - ./genInitMem.sh - @echo "Testvectors successfully generated." +TCP_PORT=1235 +RAW_RAM_FILE=${LINUX_TEST_VECTORS}/ramGDB.bin +RAM_FILE=${LINUX_TEST_VECTORS}/ram.bin +RAW_BOOTMEM_FILE=${LINUX_TEST_VECTORS}/bootmemGDB.bin +BOOTMEM_FILE=${LINUX_TEST_VECTORS}/bootmem.bin + +MEM_FILES=${RAM_FILE} ${BOOTMEM_FILE} + +dumptvs: check_write_permissions ${MEM_FILES} + +${LINUX_TEST_VECTORS}/%.bin: ${LINUX_TEST_VECTORS}/%GDB.bin + truncate -s %8 $^ # Extend file to 8 byte multiple + objcopy --reverse-bytes=8 -F binary $^ $@ # Reverse bytes + +${LINUX_TEST_VECTORS}/%GDB.bin: | $(LINUX_TEST_VECTORS) + ${WALLYLINUX}/qemuBoot.sh --gdb ${TCP_PORT} & + riscv64-unknown-elf-gdb -batch \ + -ex "target remote :${TCP_PORT}" \ + -ex "maintenance packet Qqemu.PhyMemMode:1" \ + -ex "printf \"Creating ${RAW_BOOTMEM_FILE}\n\"" \ + -ex "dump binary memory ${RAW_BOOTMEM_FILE} 0x1000 0x1fff" \ + -ex "printf \"Creating ${RAW_RAM_FILE}\n\"" \ + -ex "dump binary memory ${RAW_RAM_FILE} 0x80000000 0x8fffffff" \ + -ex "kill" \ generate: $(DTB) $(IMAGES) -$(IMAGES)/%.dtb: ./devicetree/%.dts +$(IMAGES)/%.dtb: ${WALLYLINUX}/devicetree/%.dts dtc -I dts -O dtb $< > $@ $(IMAGES): @@ -68,42 +89,40 @@ $(RISCV): # Disassembly rules --------------------------------------------------- disassemble: check_environment - rm -rf $(BUILDROOT)/output/images/disassembly - find $(BUILDROOT)/output/build/linux-* -maxdepth 1 -name "vmlinux" | xargs cp -t $(BUILDROOT)/output/images/ - mkdir -p $(DIS) - $(MAKE) $(OBJDUMPS) - # extract rootfs - mkdir -p $(BUILDROOT)/output/images/disassembly/rootfs - @echo "Ignore error about dev/console when extracting rootfs from rootfs.cpio" - -cpio -i -D $(BUILDROOT)/output/images/disassembly/rootfs < $(BUILDROOT)/output/images/rootfs.cpio - @echo "Disassembly successfully completed." + cp $(BUILDROOT)/output/build/linux-*/vmlinux $(IMAGES) + $(MAKE) $(OBJDUMPS) $(DIS)/rootfs -$(DIS)/%.objdump: $(IMAGES)/%.elf +$(DIS)/rootfs: $(IMAGES)/rootfs.cpio + @echo "Ignore error about dev/console when extracting rootfs from rootfs.cpio" + mkdir -p $(DIS)/rootfs + -cpio -i -D $(DIS)/rootfs < $(IMAGES)rootfs.cpio + +$(DIS)/%.objdump: $(IMAGES)/%.elf | $(DIS) riscv64-unknown-elf-objdump -DS $< >> $@ $(WALLY)/bin/extractFunctionRadix.sh $@ -$(DIS)/%.objdump: $(IMAGES)/% +$(DIS)/%.objdump: $(IMAGES)/% | $(DIS) riscv64-unknown-elf-objdump -S $< >> $@ $(WALLY)/bin/extractFunctionRadix.sh $@ $(IMAGES)/vmlinux: - linuxDir=$$(find $(BUILDROOT)/output/build -maxdepth 2 -type d -regex ".*/linux-[0-9]+\.[0-9]+\.[0-9]+$$") ;\ - cp $$linuxDir/vmlinux $@ ;\ + cp $(BUILDROOT)/output/build/linux-*/vmlinux $@ $(IMAGES)/busybox: - busyboxDir=$$(find $(BUILDROOT)/output/build -maxdepth 2 -type d -regex ".*/busybox-[0-9]+\.[0-9]+\.[0-9]+$$") ;\ - cp $$busyboxDir/busybox $@ ;\ + cp $(BUILDROOT)/output/build/busybox-*/busybox $@ -# Generating new Buildroot directories -------------------------------- -download: $(BUILDROOT) +config: $(BUILDROOT) $(BR2_EXTERNAL_TREE)/configs/wally_defconfig $(MAKE) -C $(BUILDROOT) wally_defconfig BR2_EXTERNAL=$(BR2_EXTERNAL_TREE) - @echo "Buildroot successfully download." $(BUILDROOT): git clone https://github.com/buildroot/buildroot.git $@ cd $@; git checkout 2024.11.x -# --------------------------------------------------------------------- +$(LINUX_TEST_VECTORS): + $(SUDO) mkdir -p $@ + +$(DIS): + mkdir -p $@ cleanDTB: rm -f $(IMAGES)/*.dtb diff --git a/linux/genInitMem.sh b/linux/genInitMem.sh deleted file mode 100755 index 65e5c9825..000000000 --- a/linux/genInitMem.sh +++ /dev/null @@ -1,46 +0,0 @@ -#!/bin/bash -set -e -tcpPort=1235 -tvDir=$RISCV/linux-testvectors -rawRamFile="$tvDir/ramGDB.bin" -ramFile="$tvDir/ram.bin" -rawBootmemFile="$tvDir/bootmemGDB.bin" -bootmemFile="$tvDir/bootmem.bin" -rawUntrimmedBootmemFile="$tvDir/untrimmedBootmemFileGDB.bin" - -if ! mkdir -p "$tvDir"; then - echo "Error: unable to create linux testvector directory $tvDir!">&2 - echo "Please try running as sudo.">&2 - exit 1 -fi -if ! test -w "$tvDir"; then - echo "Using sudo to gain access to $tvDir" - if ! sudo chmod -R a+rw "$tvDir"; then - echo "Error: insuffcient write privileges for linux testvector directory $tvDir !">&2 - echo "Please chmod it. For example:">&2 - echo " sudo chmod -R a+rw $tvDir">&2 - exit 1 - fi -fi - -echo "Launching QEMU in replay mode!" -./qemuBoot.sh --gdb $tcpPort & -riscv64-unknown-elf-gdb -batch \ - -ex "target remote :$tcpPort" \ - -ex "maintenance packet Qqemu.PhyMemMode:1" \ - -ex "printf \"Creating $rawBootmemFile\n\"" \ - -ex "dump binary memory $rawBootmemFile 0x1000 0x1fff" \ - -ex "printf \"Creating $rawRamFile\n\"" \ - -ex "dump binary memory $rawRamFile 0x80000000 0x8fffffff" \ - -ex "kill" \ - -echo "Changing Endianness" -# Extend files to 8 byte multiple -truncate -s %8 "$rawRamFile" -truncate -s %8 "$rawBootmemFile" -# Reverse bytes -objcopy --reverse-bytes=8 -F binary "$rawRamFile" "$ramFile" -objcopy --reverse-bytes=8 -F binary "$rawBootmemFile" "$bootmemFile" -rm -f "$rawRamFile" "$rawBootmemFile" "$rawUntrimmedBootmemFile" - -echo "genInitMem.sh completed!" From 2616b1c667de91b049d1cf03807dede8ca4e8cc2 Mon Sep 17 00:00:00 2001 From: Jordan Carlin Date: Tue, 21 Jan 2025 20:14:01 -0800 Subject: [PATCH 06/11] Overhaul linux makefile --- linux/Makefile | 127 +++++++++--------- linux/README.MD | 2 +- .../board/wally/post_image.sh | 5 + .../br2-external-tree/configs/wally_defconfig | 1 + 4 files changed, 70 insertions(+), 65 deletions(-) create mode 100644 linux/br2-external-tree/board/wally/post_image.sh diff --git a/linux/Makefile b/linux/Makefile index 8852c02ad..3c0c996d3 100644 --- a/linux/Makefile +++ b/linux/Makefile @@ -1,27 +1,32 @@ -# .SECONDEXPANSION: - -BUILDROOT := buildroot -IMAGES := ${BUILDROOT}/output/images -WALLYLINUX := $(WALLY)/linux -DIS := ${IMAGES}/disassembly +BUILDROOT := buildroot +IMAGE_DIR := ${BUILDROOT}/output/images +DISASSEMBLY_DIR := ${IMAGE_DIR}/disassembly +WALLYLINUX := $(WALLY)/linux BR2_EXTERNAL_TREE := $(WALLYLINUX)/br2-external-tree -LINUX_TEST_VECTORS := $(RISCV)/linux-testvectors +LINUX_TESTVECTORS := $(RISCV)/linux-testvectors -# set sudo if needed depending on $RISCV -SUDO := $(shell mkdir -p $(RISCV)/.test > /dev/null 2>&1 || echo sudo) +BUILDROOT_OUTPUTS := Image fw_jump.bin fw_jump.elf rootfs.cpio vmlinux busybox # Device tree files DTS ?= $(wildcard devicetree/*.dts) -DTB := $(foreach name, $(DTS:%.dts=%.dtb), $(IMAGES)/$(notdir $(name))) +DTB := $(foreach name, $(DTS:%.dts=%.dtb), $(IMAGE_DIR)/$(notdir $(name))) -# Disassembly stuff +# Disassembly files BINARIES := fw_jump.elf vmlinux busybox -OBJDUMPS := $(foreach name, $(basename $(BINARIES) .elf), $(DIS)/$(name).objdump) +OBJDUMPS := $(foreach name, $(basename $(BINARIES) .elf), $(DISASSEMBLY_DIR)/$(name).objdump) -.PHONY: all check_environment check_write_permissions config Image disassemble generate install dumptvs clean cleanDTB +# Testvector files +RAW_RAM_FILE := ${LINUX_TESTVECTORS}/ramGDB.bin +RAM_FILE := ${LINUX_TESTVECTORS}/ram.bin +RAW_BOOTMEM_FILE := ${LINUX_TESTVECTORS}/bootmemGDB.bin +BOOTMEM_FILE := ${LINUX_TESTVECTORS}/bootmem.bin -all: check_write_permissions clean config Image disassemble install dumptvs +.PHONY: all check_environment check_write_permissions config build disassemble devicetrees install dumptvs clean cleanDTB +# Default target +all: check_write_permissions clean config build disassemble install dumptvs + +# Check if the environment variables are set correctly check_environment: $(RISCV) ifeq ($(findstring :$(RISCV)/lib:,:$(LD_LIBRARY_PATH):),) @(echo "ERROR: Your environment variables are not set correctly." >&2 \ @@ -29,6 +34,8 @@ ifeq ($(findstring :$(RISCV)/lib:,:$(LD_LIBRARY_PATH):),) && exit 1) endif +# Check if the user has write permissions to the RISCV directory, potentially using sudo +SUDO := $(shell mkdir -p $(RISCV)/.test > /dev/null 2>&1 || echo sudo) check_write_permissions: check_environment ifeq ($(SUDO), sudo) @echo "Cannot write to '$(RISCV)'." \ @@ -39,31 +46,30 @@ endif && exit 1) @$(SUDO) rm -r $(RISCV)/.test -Image: check_environment $(BUILDROOT) - bash -c "unset LD_LIBRARY_PATH; $(MAKE) -C $(BUILDROOT)" -# $(MAKE) generate - @echo "Buildroot Image successfully generated." +# Build buildroot and device tree binaries +build: $(BUILDROOT_OUTPUTS) devicetrees +# Build buildroot itself +# LD_LIBRARY_PATH must be unset to avoid conflicts between the host and cross compiler +$(BUILDROOT_OUTPUTS): check_environment $(BUILDROOT) + bash -c "unset LD_LIBRARY_PATH; $(MAKE) -C $(BUILDROOT)" + +# Install buildroot to $RISCV install: check_write_permissions $(SUDO) rm -rf $(RISCV)/$(BUILDROOT) $(SUDO) mv $(BUILDROOT) $(RISCV)/$(BUILDROOT) - @echo "Buildroot successfully installed." -TCP_PORT=1235 -RAW_RAM_FILE=${LINUX_TEST_VECTORS}/ramGDB.bin -RAM_FILE=${LINUX_TEST_VECTORS}/ram.bin -RAW_BOOTMEM_FILE=${LINUX_TEST_VECTORS}/bootmemGDB.bin -BOOTMEM_FILE=${LINUX_TEST_VECTORS}/bootmem.bin +# Generate linux boot testvectors +dumptvs: ${RAM_FILE} ${BOOTMEM_FILE} -MEM_FILES=${RAM_FILE} ${BOOTMEM_FILE} - -dumptvs: check_write_permissions ${MEM_FILES} - -${LINUX_TEST_VECTORS}/%.bin: ${LINUX_TEST_VECTORS}/%GDB.bin +# Format QEMU memory dumps for use as testvectors +${LINUX_TESTVECTORS}/%.bin: ${LINUX_TESTVECTORS}/%GDB.bin truncate -s %8 $^ # Extend file to 8 byte multiple objcopy --reverse-bytes=8 -F binary $^ $@ # Reverse bytes -${LINUX_TEST_VECTORS}/%GDB.bin: | $(LINUX_TEST_VECTORS) +# Generate memory dumps from QEMU buildroot boot +TCP_PORT := 1235 +${LINUX_TESTVECTORS}/%GDB.bin: | $(LINUX_TESTVECTORS) ${WALLYLINUX}/qemuBoot.sh --gdb ${TCP_PORT} & riscv64-unknown-elf-gdb -batch \ -ex "target remote :${TCP_PORT}" \ @@ -72,60 +78,53 @@ ${LINUX_TEST_VECTORS}/%GDB.bin: | $(LINUX_TEST_VECTORS) -ex "dump binary memory ${RAW_BOOTMEM_FILE} 0x1000 0x1fff" \ -ex "printf \"Creating ${RAW_RAM_FILE}\n\"" \ -ex "dump binary memory ${RAW_RAM_FILE} 0x80000000 0x8fffffff" \ - -ex "kill" \ + -ex "kill" -generate: $(DTB) $(IMAGES) - -$(IMAGES)/%.dtb: ${WALLYLINUX}/devicetree/%.dts +# Generate device tree binaries +devicetrees: $(DTB) +$(IMAGE_DIR)/%.dtb: ${WALLYLINUX}/devicetree/%.dts dtc -I dts -O dtb $< > $@ -$(IMAGES): - @ echo "No output/images directory in buildroot." - @ echo "Run make --jobs in buildroot directory before generating device tree binaries."; exit 1 +# Create disassembly files +disassemble: check_environment $(OBJDUMPS) $(DISASSEMBLY_DIR)/rootfs -$(RISCV): - @ echo "ERROR: No $(RISCV) directory. Make sure you have installed the Wally Toolchain." - @ echo "and sourced setup.sh" - -# Disassembly rules --------------------------------------------------- -disassemble: check_environment - cp $(BUILDROOT)/output/build/linux-*/vmlinux $(IMAGES) - $(MAKE) $(OBJDUMPS) $(DIS)/rootfs - -$(DIS)/rootfs: $(IMAGES)/rootfs.cpio +# Extract rootfs +$(DISASSEMBLY_DIR)/rootfs: $(IMAGE_DIR)/rootfs.cpio @echo "Ignore error about dev/console when extracting rootfs from rootfs.cpio" - mkdir -p $(DIS)/rootfs - -cpio -i -D $(DIS)/rootfs < $(IMAGES)rootfs.cpio + mkdir -p $(DISASSEMBLY_DIR)/rootfs + -cpio -i -D $(DISASSEMBLY_DIR)/rootfs -F $(IMAGE_DIR)rootfs.cpio -$(DIS)/%.objdump: $(IMAGES)/%.elf | $(DIS) - riscv64-unknown-elf-objdump -DS $< >> $@ +# Disassemble binaries +objdump_flags = $(if $(findstring .elf,$<),-SD,-S) # Add -D flag for .elf files +$(DISASSEMBLY_DIR)/%.objdump: $(IMAGE_DIR)/% | $(DISASSEMBLY_DIR) + riscv64-unknown-elf-objdump $(objdump_flags) $< >> $@ $(WALLY)/bin/extractFunctionRadix.sh $@ -$(DIS)/%.objdump: $(IMAGES)/% | $(DIS) - riscv64-unknown-elf-objdump -S $< >> $@ - $(WALLY)/bin/extractFunctionRadix.sh $@ - -$(IMAGES)/vmlinux: - cp $(BUILDROOT)/output/build/linux-*/vmlinux $@ - -$(IMAGES)/busybox: - cp $(BUILDROOT)/output/build/busybox-*/busybox $@ - +# Load wally buildroot configuration config: $(BUILDROOT) $(BR2_EXTERNAL_TREE)/configs/wally_defconfig $(MAKE) -C $(BUILDROOT) wally_defconfig BR2_EXTERNAL=$(BR2_EXTERNAL_TREE) +# Clone buildroot and checkout the correct version $(BUILDROOT): git clone https://github.com/buildroot/buildroot.git $@ cd $@; git checkout 2024.11.x -$(LINUX_TEST_VECTORS): +# Create directories +$(LINUX_TESTVECTORS): check_write_permissions $(SUDO) mkdir -p $@ -$(DIS): +$(DISASSEMBLY_DIR): mkdir -p $@ +# Remove device tree binaries cleanDTB: - rm -f $(IMAGES)/*.dtb + rm -f $(IMAGE_DIR)/*.dtb +# Remove buildroot directory clean: rm -rf $(BUILDROOT) + +# Check if the RISCV environment variable is set +$(RISCV): + @ echo "ERROR: No $(RISCV) directory. Make sure you have installed the Wally Toolchain." + @ echo "and sourced setup.sh" diff --git a/linux/README.MD b/linux/README.MD index b7a031ce6..2e8405f70 100644 --- a/linux/README.MD +++ b/linux/README.MD @@ -35,7 +35,7 @@ The device tree files for the various FPGAs Wally supports, as well as QEMU's de They are built automatically using the main `make` command. To build the device tree binaries (.dtb) from the device tree sources (.dts) separately, we can build all of them at once using: ```bash -$ make generate # optionally override BUILDROOT +$ make devicetrees # optionally override BUILDROOT ``` The .dts files will end up in the `/output/images` folder of your chosen buildroot directory. diff --git a/linux/br2-external-tree/board/wally/post_image.sh b/linux/br2-external-tree/board/wally/post_image.sh new file mode 100644 index 000000000..53544d8b3 --- /dev/null +++ b/linux/br2-external-tree/board/wally/post_image.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +# Copy linux and busybox binaries (with symbol info) to images directory +cp "$BUILD_DIR"/linux-*/vmlinux "$BINARIES_DIR"/vmlinux +cp "$BUILD_DIR"busybox-*/busybox "$BINARIES_DIR"/busybox diff --git a/linux/br2-external-tree/configs/wally_defconfig b/linux/br2-external-tree/configs/wally_defconfig index 24d82e9d0..8759588a7 100644 --- a/linux/br2-external-tree/configs/wally_defconfig +++ b/linux/br2-external-tree/configs/wally_defconfig @@ -19,6 +19,7 @@ BR2_TARGET_GENERIC_ISSUE="Greetings! This RISC-V Linux image was built for Wally BR2_ROOTFS_DEVICE_TABLE_SUPPORTS_EXTENDED_ATTRIBUTES=y BR2_SYSTEM_DHCP="eth0" BR2_ROOTFS_OVERLAY="$(BR2_EXTERNAL_WALLY_PATH)/board/wally/rootfs_overlay" +BR2_ROOTFS_POST_IMAGE_SCRIPT="$(BR2_EXTERNAL_WALLY_PATH)/board/wally/post_image.sh" BR2_LINUX_KERNEL=y BR2_LINUX_KERNEL_CUSTOM_VERSION=y BR2_LINUX_KERNEL_CUSTOM_VERSION_VALUE="6.12.8" From f3a16e90a9d9b243435e7f2b922c448f072aecf4 Mon Sep 17 00:00:00 2001 From: Jordan Carlin Date: Tue, 21 Jan 2025 20:33:25 -0800 Subject: [PATCH 07/11] A little more simplification --- linux/Makefile | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/linux/Makefile b/linux/Makefile index 3c0c996d3..45588e7f7 100644 --- a/linux/Makefile +++ b/linux/Makefile @@ -91,8 +91,7 @@ disassemble: check_environment $(OBJDUMPS) $(DISASSEMBLY_DIR)/rootfs # Extract rootfs $(DISASSEMBLY_DIR)/rootfs: $(IMAGE_DIR)/rootfs.cpio @echo "Ignore error about dev/console when extracting rootfs from rootfs.cpio" - mkdir -p $(DISASSEMBLY_DIR)/rootfs - -cpio -i -D $(DISASSEMBLY_DIR)/rootfs -F $(IMAGE_DIR)rootfs.cpio + -cpio -id -D $(DISASSEMBLY_DIR)/rootfs -F $(IMAGE_DIR)rootfs.cpio # Disassemble binaries objdump_flags = $(if $(findstring .elf,$<),-SD,-S) # Add -D flag for .elf files From 3e9f2d0b276446dbe09cce3f2715b45f720c0a24 Mon Sep 17 00:00:00 2001 From: Jordan Carlin Date: Tue, 21 Jan 2025 22:58:33 -0800 Subject: [PATCH 08/11] Fix a few more linux build issues --- linux/Makefile | 13 +++++++++---- linux/br2-external-tree/board/wally/post_image.sh | 2 +- linux/br2-external-tree/configs/wally_defconfig | 1 + 3 files changed, 11 insertions(+), 5 deletions(-) mode change 100644 => 100755 linux/br2-external-tree/board/wally/post_image.sh diff --git a/linux/Makefile b/linux/Makefile index 45588e7f7..aed0ec9e3 100644 --- a/linux/Makefile +++ b/linux/Makefile @@ -6,6 +6,7 @@ BR2_EXTERNAL_TREE := $(WALLYLINUX)/br2-external-tree LINUX_TESTVECTORS := $(RISCV)/linux-testvectors BUILDROOT_OUTPUTS := Image fw_jump.bin fw_jump.elf rootfs.cpio vmlinux busybox +BUILDROOT_OUTPUTS := $(foreach name, $(BUILDROOT_OUTPUTS), $(IMAGE_DIR)/$(name)) # Device tree files DTS ?= $(wildcard devicetree/*.dts) @@ -51,7 +52,7 @@ build: $(BUILDROOT_OUTPUTS) devicetrees # Build buildroot itself # LD_LIBRARY_PATH must be unset to avoid conflicts between the host and cross compiler -$(BUILDROOT_OUTPUTS): check_environment $(BUILDROOT) +$(BUILDROOT_OUTPUTS) $(IMAGE_DIR): check_environment $(BUILDROOT) bash -c "unset LD_LIBRARY_PATH; $(MAKE) -C $(BUILDROOT)" # Install buildroot to $RISCV @@ -82,7 +83,7 @@ ${LINUX_TESTVECTORS}/%GDB.bin: | $(LINUX_TESTVECTORS) # Generate device tree binaries devicetrees: $(DTB) -$(IMAGE_DIR)/%.dtb: ${WALLYLINUX}/devicetree/%.dts +$(IMAGE_DIR)/%.dtb: ${WALLYLINUX}/devicetree/%.dts | $(IMAGE_DIR) dtc -I dts -O dtb $< > $@ # Create disassembly files @@ -94,9 +95,13 @@ $(DISASSEMBLY_DIR)/rootfs: $(IMAGE_DIR)/rootfs.cpio -cpio -id -D $(DISASSEMBLY_DIR)/rootfs -F $(IMAGE_DIR)rootfs.cpio # Disassemble binaries -objdump_flags = $(if $(findstring .elf,$<),-SD,-S) # Add -D flag for .elf files $(DISASSEMBLY_DIR)/%.objdump: $(IMAGE_DIR)/% | $(DISASSEMBLY_DIR) - riscv64-unknown-elf-objdump $(objdump_flags) $< >> $@ + riscv64-unknown-elf-objdump -S $< >> $@ + $(WALLY)/bin/extractFunctionRadix.sh $@ + +# Disassemble binaries ending in .elf +$(DISASSEMBLY_DIR)/%.objdump: $(IMAGE_DIR)/%.elf | $(DISASSEMBLY_DIR) + riscv64-unknown-elf-objdump -SD $< >> $@ $(WALLY)/bin/extractFunctionRadix.sh $@ # Load wally buildroot configuration diff --git a/linux/br2-external-tree/board/wally/post_image.sh b/linux/br2-external-tree/board/wally/post_image.sh old mode 100644 new mode 100755 index 53544d8b3..e3db7525b --- a/linux/br2-external-tree/board/wally/post_image.sh +++ b/linux/br2-external-tree/board/wally/post_image.sh @@ -2,4 +2,4 @@ # Copy linux and busybox binaries (with symbol info) to images directory cp "$BUILD_DIR"/linux-*/vmlinux "$BINARIES_DIR"/vmlinux -cp "$BUILD_DIR"busybox-*/busybox "$BINARIES_DIR"/busybox +cp "$BUILD_DIR"/busybox-*/busybox "$BINARIES_DIR"/busybox diff --git a/linux/br2-external-tree/configs/wally_defconfig b/linux/br2-external-tree/configs/wally_defconfig index 8759588a7..bdae60a6f 100644 --- a/linux/br2-external-tree/configs/wally_defconfig +++ b/linux/br2-external-tree/configs/wally_defconfig @@ -10,6 +10,7 @@ BR2_GNU_MIRROR="http://ftpmirror.gnu.org" BR2_ENABLE_DEBUG=y BR2_DEBUG_3=y # BR2_STRIP_strip is not set +BR2_PER_PACKAGE_DIRECTORIES=y # BR2_PIC_PIE is not set BR2_SSP_NONE=y BR2_RELRO_NONE=y From aaaefbaf0dd7bab2cbc57badb94f8078dfeb8e37 Mon Sep 17 00:00:00 2001 From: Jordan Carlin Date: Wed, 22 Jan 2025 15:17:55 -0800 Subject: [PATCH 09/11] Fix typo --- linux/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/linux/Makefile b/linux/Makefile index aed0ec9e3..78582ba29 100644 --- a/linux/Makefile +++ b/linux/Makefile @@ -92,7 +92,7 @@ disassemble: check_environment $(OBJDUMPS) $(DISASSEMBLY_DIR)/rootfs # Extract rootfs $(DISASSEMBLY_DIR)/rootfs: $(IMAGE_DIR)/rootfs.cpio @echo "Ignore error about dev/console when extracting rootfs from rootfs.cpio" - -cpio -id -D $(DISASSEMBLY_DIR)/rootfs -F $(IMAGE_DIR)rootfs.cpio + -cpio -id -D $(DISASSEMBLY_DIR)/rootfs -F $(IMAGE_DIR)/rootfs.cpio # Disassemble binaries $(DISASSEMBLY_DIR)/%.objdump: $(IMAGE_DIR)/% | $(DISASSEMBLY_DIR) From c854da75f93ae2ed62e98ce6670224bd5ae5d491 Mon Sep 17 00:00:00 2001 From: Jordan Carlin Date: Thu, 23 Jan 2025 00:43:35 -0800 Subject: [PATCH 10/11] Update buildroot short instruction count --- bin/regression-wally | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/regression-wally b/bin/regression-wally index 185f03826..e52a89edb 100755 --- a/bin/regression-wally +++ b/bin/regression-wally @@ -39,7 +39,7 @@ tests = [ # Separate test for short buildroot run through OpenSBI UART output tests_buildrootshort = [ - ["buildroot", ["buildroot"], [f"+INSTR_LIMIT=1400000"], # Instruction limit gets to first OpenSBI UART output + ["buildroot", ["buildroot"], [f"+INSTR_LIMIT=1600000"], # Instruction limit gets to first OpenSBI UART output "OpenSBI v", "buildroot_uart.out"] ] From be6f1d9a2b26b60319cd618f479558f0925ebf6e Mon Sep 17 00:00:00 2001 From: Jordan Carlin Date: Thu, 23 Jan 2025 00:44:38 -0800 Subject: [PATCH 11/11] Check that WALLY is set in linux Makefile --- linux/Makefile | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/linux/Makefile b/linux/Makefile index 78582ba29..58cb6c69b 100644 --- a/linux/Makefile +++ b/linux/Makefile @@ -28,7 +28,7 @@ BOOTMEM_FILE := ${LINUX_TESTVECTORS}/bootmem.bin all: check_write_permissions clean config build disassemble install dumptvs # Check if the environment variables are set correctly -check_environment: $(RISCV) +check_environment: $(RISCV) $(WALLY) ifeq ($(findstring :$(RISCV)/lib:,:$(LD_LIBRARY_PATH):),) @(echo "ERROR: Your environment variables are not set correctly." >&2 \ && echo "Make sure to source setup.sh or install buildroot using the wally-tool-chain-install.sh script." >&2 \ @@ -132,3 +132,7 @@ clean: $(RISCV): @ echo "ERROR: No $(RISCV) directory. Make sure you have installed the Wally Toolchain." @ echo "and sourced setup.sh" + +# Check if the WALLY environment variable is set +$(WALLY): + @ echo "ERROR: $$WALLY is not set. Make sure you have sourced setup.sh"