1362aecb2SWilly Tarreau# SPDX-License-Identifier: GPL-2.0 2362aecb2SWilly Tarreau# Makefile for nolibc tests 3362aecb2SWilly Tarreauinclude ../../../scripts/Makefile.include 4077b70fbSThomas Weißschuh# We need this for the "cc-option" macro. 5077b70fbSThomas Weißschuhinclude ../../../build/Build.include 6362aecb2SWilly Tarreau 7362aecb2SWilly Tarreau# we're in ".../tools/testing/selftests/nolibc" 8362aecb2SWilly Tarreauifeq ($(srctree),) 9362aecb2SWilly Tarreausrctree := $(patsubst %/tools/testing/selftests/,%,$(dir $(CURDIR))) 10362aecb2SWilly Tarreauendif 11362aecb2SWilly Tarreau 12362aecb2SWilly Tarreauifeq ($(ARCH),) 13362aecb2SWilly Tarreauinclude $(srctree)/scripts/subarch.include 14362aecb2SWilly TarreauARCH = $(SUBARCH) 15362aecb2SWilly Tarreauendif 16362aecb2SWilly Tarreau 17c6c3734fSZhangjin Wu# XARCH extends the kernel's ARCH with a few variants of the same 18c6c3734fSZhangjin Wu# architecture that only differ by the configuration, the toolchain 19c6c3734fSZhangjin Wu# and the Qemu program used. It is copied as-is into ARCH except for 20c6c3734fSZhangjin Wu# a few specific values which are mapped like this: 21c6c3734fSZhangjin Wu# 22c6c3734fSZhangjin Wu# XARCH | ARCH | config 23c6c3734fSZhangjin Wu# -------------|-----------|------------------------- 24c6c3734fSZhangjin Wu# ppc | powerpc | 32 bits 25c6c3734fSZhangjin Wu# ppc64 | powerpc | 64 bits big endian 26c6c3734fSZhangjin Wu# ppc64le | powerpc | 64 bits little endian 27c6c3734fSZhangjin Wu# 28c6c3734fSZhangjin Wu# It is recommended to only use XARCH, though it does not harm if 29c6c3734fSZhangjin Wu# ARCH is already set. For simplicity, ARCH is sufficient for all 30c6c3734fSZhangjin Wu# architectures where both are equal. 31c6c3734fSZhangjin Wu 32c6c3734fSZhangjin Wu# configure default variants for target kernel supported architectures 33587e9845SZhangjin WuXARCH_powerpc = ppc 34c6c3734fSZhangjin WuXARCH = $(or $(XARCH_$(ARCH)),$(ARCH)) 35c6c3734fSZhangjin Wu 36c6c3734fSZhangjin Wu# map from user input variants to their kernel supported architectures 37587e9845SZhangjin WuARCH_ppc = powerpc 38faeb4e09SZhangjin WuARCH_ppc64 = powerpc 398a5040cbSZhangjin WuARCH_ppc64le = powerpc 40c6c3734fSZhangjin WuARCH := $(or $(ARCH_$(XARCH)),$(XARCH)) 41c6c3734fSZhangjin Wu 42d248cabfSWilly Tarreau# kernel image names by architecture 43d248cabfSWilly TarreauIMAGE_i386 = arch/x86/boot/bzImage 44f9b06695SWilly TarreauIMAGE_x86_64 = arch/x86/boot/bzImage 45d248cabfSWilly TarreauIMAGE_x86 = arch/x86/boot/bzImage 46d248cabfSWilly TarreauIMAGE_arm64 = arch/arm64/boot/Image 47d248cabfSWilly TarreauIMAGE_arm = arch/arm/boot/zImage 48d248cabfSWilly TarreauIMAGE_mips = vmlinuz 49587e9845SZhangjin WuIMAGE_ppc = vmlinux 50faeb4e09SZhangjin WuIMAGE_ppc64 = vmlinux 518a5040cbSZhangjin WuIMAGE_ppc64le = arch/powerpc/boot/zImage 52d248cabfSWilly TarreauIMAGE_riscv = arch/riscv/boot/Image 530043e6f2SSven SchnelleIMAGE_s390 = arch/s390/boot/bzImage 546cd77defSFeiyang ChenIMAGE_loongarch = arch/loongarch/boot/vmlinuz.efi 55c6c3734fSZhangjin WuIMAGE = $(IMAGE_$(XARCH)) 56d248cabfSWilly TarreauIMAGE_NAME = $(notdir $(IMAGE)) 57d248cabfSWilly Tarreau 585c43fd79SWilly Tarreau# default kernel configurations that appear to be usable 595c43fd79SWilly TarreauDEFCONFIG_i386 = defconfig 60f9b06695SWilly TarreauDEFCONFIG_x86_64 = defconfig 615c43fd79SWilly TarreauDEFCONFIG_x86 = defconfig 625c43fd79SWilly TarreauDEFCONFIG_arm64 = defconfig 635c43fd79SWilly TarreauDEFCONFIG_arm = multi_v7_defconfig 645c43fd79SWilly TarreauDEFCONFIG_mips = malta_defconfig 65587e9845SZhangjin WuDEFCONFIG_ppc = pmac32_defconfig 66faeb4e09SZhangjin WuDEFCONFIG_ppc64 = powernv_be_defconfig 678a5040cbSZhangjin WuDEFCONFIG_ppc64le = powernv_defconfig 685c43fd79SWilly TarreauDEFCONFIG_riscv = defconfig 690043e6f2SSven SchnelleDEFCONFIG_s390 = defconfig 706cd77defSFeiyang ChenDEFCONFIG_loongarch = defconfig 71c6c3734fSZhangjin WuDEFCONFIG = $(DEFCONFIG_$(XARCH)) 725c43fd79SWilly Tarreau 73662ea60eSWilly Tarreau# optional tests to run (default = all) 74662ea60eSWilly TarreauTEST = 75662ea60eSWilly Tarreau 76662ea60eSWilly Tarreau# QEMU_ARCH: arch names used by qemu 77662ea60eSWilly TarreauQEMU_ARCH_i386 = i386 78f9b06695SWilly TarreauQEMU_ARCH_x86_64 = x86_64 79662ea60eSWilly TarreauQEMU_ARCH_x86 = x86_64 80662ea60eSWilly TarreauQEMU_ARCH_arm64 = aarch64 81662ea60eSWilly TarreauQEMU_ARCH_arm = arm 82662ea60eSWilly TarreauQEMU_ARCH_mips = mipsel # works with malta_defconfig 83587e9845SZhangjin WuQEMU_ARCH_ppc = ppc 84faeb4e09SZhangjin WuQEMU_ARCH_ppc64 = ppc64 858a5040cbSZhangjin WuQEMU_ARCH_ppc64le = ppc64le 86662ea60eSWilly TarreauQEMU_ARCH_riscv = riscv64 870043e6f2SSven SchnelleQEMU_ARCH_s390 = s390x 886cd77defSFeiyang ChenQEMU_ARCH_loongarch = loongarch64 89c6c3734fSZhangjin WuQEMU_ARCH = $(QEMU_ARCH_$(XARCH)) 90662ea60eSWilly Tarreau 91662ea60eSWilly Tarreau# QEMU_ARGS : some arch-specific args to pass to qemu 92662ea60eSWilly TarreauQEMU_ARGS_i386 = -M pc -append "console=ttyS0,9600 i8042.noaux panic=-1 $(TEST:%=NOLIBC_TEST=%)" 93f9b06695SWilly TarreauQEMU_ARGS_x86_64 = -M pc -append "console=ttyS0,9600 i8042.noaux panic=-1 $(TEST:%=NOLIBC_TEST=%)" 94662ea60eSWilly TarreauQEMU_ARGS_x86 = -M pc -append "console=ttyS0,9600 i8042.noaux panic=-1 $(TEST:%=NOLIBC_TEST=%)" 95662ea60eSWilly TarreauQEMU_ARGS_arm64 = -M virt -cpu cortex-a53 -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)" 96662ea60eSWilly TarreauQEMU_ARGS_arm = -M virt -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)" 97662ea60eSWilly TarreauQEMU_ARGS_mips = -M malta -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)" 98587e9845SZhangjin WuQEMU_ARGS_ppc = -M g3beige -append "console=ttyS0 panic=-1 $(TEST:%=NOLIBC_TEST=%)" 99faeb4e09SZhangjin WuQEMU_ARGS_ppc64 = -M powernv -append "console=hvc0 panic=-1 $(TEST:%=NOLIBC_TEST=%)" 1008a5040cbSZhangjin WuQEMU_ARGS_ppc64le = -M powernv -append "console=hvc0 panic=-1 $(TEST:%=NOLIBC_TEST=%)" 101662ea60eSWilly TarreauQEMU_ARGS_riscv = -M virt -append "console=ttyS0 panic=-1 $(TEST:%=NOLIBC_TEST=%)" 1020043e6f2SSven SchnelleQEMU_ARGS_s390 = -M s390-ccw-virtio -m 1G -append "console=ttyS0 panic=-1 $(TEST:%=NOLIBC_TEST=%)" 1036cd77defSFeiyang ChenQEMU_ARGS_loongarch = -M virt -append "console=ttyS0,115200 panic=-1 $(TEST:%=NOLIBC_TEST=%)" 104c6c3734fSZhangjin WuQEMU_ARGS = $(QEMU_ARGS_$(XARCH)) $(QEMU_ARGS_EXTRA) 105662ea60eSWilly Tarreau 106362aecb2SWilly Tarreau# OUTPUT is only set when run from the main makefile, otherwise 107362aecb2SWilly Tarreau# it defaults to this nolibc directory. 108362aecb2SWilly TarreauOUTPUT ?= $(CURDIR)/ 109362aecb2SWilly Tarreau 110362aecb2SWilly Tarreauifeq ($(V),1) 111362aecb2SWilly TarreauQ= 112362aecb2SWilly Tarreauelse 113362aecb2SWilly TarreauQ=@ 114362aecb2SWilly Tarreauendif 115362aecb2SWilly Tarreau 116587e9845SZhangjin WuCFLAGS_ppc = -m32 -mbig-endian -mno-vsx $(call cc-option,-mmultiple) 117faeb4e09SZhangjin WuCFLAGS_ppc64 = -m64 -mbig-endian -mno-vsx $(call cc-option,-mmultiple) 1188a5040cbSZhangjin WuCFLAGS_ppc64le = -m64 -mlittle-endian -mno-vsx $(call cc-option,-mabi=elfv2) 1190043e6f2SSven SchnelleCFLAGS_s390 = -m64 120dd58d666SWilly TarreauCFLAGS_mips = -EL 1210093c2daSThomas WeißschuhCFLAGS_STACKPROTECTOR ?= $(call cc-option,-mstack-protector-guard=global $(call cc-option,-fstack-protector-all)) 12245f65f8dSThomas WeißschuhCFLAGS ?= -Os -fno-ident -fno-asynchronous-unwind-tables -std=c89 -W -Wall -Wextra \ 123255ffeeeSThomas Weißschuh $(call cc-option,-fno-stack-protector) \ 124c6c3734fSZhangjin Wu $(CFLAGS_$(XARCH)) $(CFLAGS_STACKPROTECTOR) 125711edef8SThomas WeißschuhLDFLAGS := 126362aecb2SWilly Tarreau 1274beb9be8SZhangjin WuREPORT ?= awk '/\[OK\][\r]*$$/{p++} /\[FAIL\][\r]*$$/{if (!f) printf("\n"); f++; print;} /\[SKIPPED\][\r]*$$/{s++} \ 1287d92e893SZhangjin Wu END{ printf("\n%3d test(s): %3d passed, %3d skipped, %3d failed => status: ", p+s+f, p, s, f); \ 129c0315c79SZhangjin Wu if (f) printf("failure\n"); else if (s) printf("warning\n"); else printf("success\n");; \ 1307d92e893SZhangjin Wu printf("\nSee all results in %s\n", ARGV[1]); }' 1315ef95663SZhangjin Wu 132ffc297feSWilly Tarreauhelp: 133ffc297feSWilly Tarreau @echo "Supported targets under selftests/nolibc:" 134ffc297feSWilly Tarreau @echo " all call the \"run\" target below" 135ffc297feSWilly Tarreau @echo " help this help" 136ffc297feSWilly Tarreau @echo " sysroot create the nolibc sysroot here (uses \$$ARCH)" 137ffc297feSWilly Tarreau @echo " nolibc-test build the executable (uses \$$CC and \$$CROSS_COMPILE)" 138fc82d7dbSThomas Weißschuh @echo " libc-test build an executable using the compiler's default libc instead" 139c6c3734fSZhangjin Wu @echo " run-user runs the executable under QEMU (uses \$$XARCH, \$$TEST)" 140ffc297feSWilly Tarreau @echo " initramfs prepare the initramfs with nolibc-test" 141c6c3734fSZhangjin Wu @echo " defconfig create a fresh new default config (uses \$$XARCH)" 142c6c3734fSZhangjin Wu @echo " kernel (re)build the kernel with the initramfs (uses \$$XARCH)" 143c6c3734fSZhangjin Wu @echo " run runs the kernel in QEMU after building it (uses \$$XARCH, \$$TEST)" 144c6c3734fSZhangjin Wu @echo " rerun runs a previously prebuilt kernel in QEMU (uses \$$XARCH, \$$TEST)" 145ffc297feSWilly Tarreau @echo " clean clean the sysroot, initramfs, build and output files" 146ffc297feSWilly Tarreau @echo "" 147ffc297feSWilly Tarreau @echo "The output file is \"run.out\". Test ranges may be passed using \$$TEST." 148ffc297feSWilly Tarreau @echo "" 149ffc297feSWilly Tarreau @echo "Currently using the following variables:" 150ffc297feSWilly Tarreau @echo " ARCH = $(ARCH)" 151c6c3734fSZhangjin Wu @echo " XARCH = $(XARCH)" 152ffc297feSWilly Tarreau @echo " CROSS_COMPILE = $(CROSS_COMPILE)" 153ffc297feSWilly Tarreau @echo " CC = $(CC)" 154ffc297feSWilly Tarreau @echo " OUTPUT = $(OUTPUT)" 155ffc297feSWilly Tarreau @echo " TEST = $(TEST)" 156c6c3734fSZhangjin Wu @echo " QEMU_ARCH = $(if $(QEMU_ARCH),$(QEMU_ARCH),UNKNOWN_ARCH) [determined from \$$XARCH]" 157c6c3734fSZhangjin Wu @echo " IMAGE_NAME = $(if $(IMAGE_NAME),$(IMAGE_NAME),UNKNOWN_ARCH) [determined from \$$XARCH]" 158ffc297feSWilly Tarreau @echo "" 159ffc297feSWilly Tarreau 160ffc297feSWilly Tarreauall: run 161362aecb2SWilly Tarreau 162b25c5284SWilly Tarreausysroot: sysroot/$(ARCH)/include 163b25c5284SWilly Tarreau 164b25c5284SWilly Tarreausysroot/$(ARCH)/include: 1654a95be7eSWilly Tarreau $(Q)rm -rf sysroot/$(ARCH) sysroot/sysroot 166b25c5284SWilly Tarreau $(QUIET_MKDIR)mkdir -p sysroot 167b25c5284SWilly Tarreau $(Q)$(MAKE) -C ../../../include/nolibc ARCH=$(ARCH) OUTPUT=$(CURDIR)/sysroot/ headers_standalone 168b25c5284SWilly Tarreau $(Q)mv sysroot/sysroot sysroot/$(ARCH) 169b25c5284SWilly Tarreau 170850fad7dSZhangjin Wuifneq ($(NOLIBC_SYSROOT),0) 171b25c5284SWilly Tarreaunolibc-test: nolibc-test.c sysroot/$(ARCH)/include 172362aecb2SWilly Tarreau $(QUIET_CC)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ \ 173b25c5284SWilly Tarreau -nostdlib -static -Isysroot/$(ARCH)/include $< -lgcc 174850fad7dSZhangjin Wuelse 175850fad7dSZhangjin Wunolibc-test: nolibc-test.c 176850fad7dSZhangjin Wu $(QUIET_CC)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ \ 177850fad7dSZhangjin Wu -nostdlib -static -include ../../../include/nolibc/nolibc.h $< -lgcc 178850fad7dSZhangjin Wuendif 179362aecb2SWilly Tarreau 180fc82d7dbSThomas Weißschuhlibc-test: nolibc-test.c 181*418c8468SZhangjin Wu $(QUIET_CC)$(HOSTCC) -o $@ $< 182fc82d7dbSThomas Weißschuh 183b8143407SZhangjin Wu# local libc-test 184cfb672f9SZhangjin Wurun-libc-test: libc-test 185cfb672f9SZhangjin Wu $(Q)./libc-test > "$(CURDIR)/run.out" || : 186cfb672f9SZhangjin Wu $(Q)$(REPORT) $(CURDIR)/run.out 187cfb672f9SZhangjin Wu 188b8143407SZhangjin Wu# local nolibc-test 189b8143407SZhangjin Wurun-nolibc-test: nolibc-test 190b8143407SZhangjin Wu $(Q)./nolibc-test > "$(CURDIR)/run.out" || : 191b8143407SZhangjin Wu $(Q)$(REPORT) $(CURDIR)/run.out 192b8143407SZhangjin Wu 193c54ba417SWilly Tarreau# qemu user-land test 194c54ba417SWilly Tarreaurun-user: nolibc-test 195c54ba417SWilly Tarreau $(Q)qemu-$(QEMU_ARCH) ./nolibc-test > "$(CURDIR)/run.out" || : 1965ef95663SZhangjin Wu $(Q)$(REPORT) $(CURDIR)/run.out 197c54ba417SWilly Tarreau 198362aecb2SWilly Tarreauinitramfs: nolibc-test 199362aecb2SWilly Tarreau $(QUIET_MKDIR)mkdir -p initramfs 200362aecb2SWilly Tarreau $(call QUIET_INSTALL, initramfs/init) 201362aecb2SWilly Tarreau $(Q)cp nolibc-test initramfs/init 202362aecb2SWilly Tarreau 2035c43fd79SWilly Tarreaudefconfig: 2045c43fd79SWilly Tarreau $(Q)$(MAKE) -C $(srctree) ARCH=$(ARCH) CC=$(CC) CROSS_COMPILE=$(CROSS_COMPILE) mrproper $(DEFCONFIG) prepare 2055c43fd79SWilly Tarreau 206d248cabfSWilly Tarreaukernel: initramfs 207d248cabfSWilly Tarreau $(Q)$(MAKE) -C $(srctree) ARCH=$(ARCH) CC=$(CC) CROSS_COMPILE=$(CROSS_COMPILE) $(IMAGE_NAME) CONFIG_INITRAMFS_SOURCE=$(CURDIR)/initramfs 208d248cabfSWilly Tarreau 209662ea60eSWilly Tarreau# run the tests after building the kernel 210662ea60eSWilly Tarreaurun: kernel 211662ea60eSWilly Tarreau $(Q)qemu-system-$(QEMU_ARCH) -display none -no-reboot -kernel "$(srctree)/$(IMAGE)" -serial stdio $(QEMU_ARGS) > "$(CURDIR)/run.out" 2125ef95663SZhangjin Wu $(Q)$(REPORT) $(CURDIR)/run.out 213662ea60eSWilly Tarreau 214662ea60eSWilly Tarreau# re-run the tests from an existing kernel 215662ea60eSWilly Tarreaurerun: 216662ea60eSWilly Tarreau $(Q)qemu-system-$(QEMU_ARCH) -display none -no-reboot -kernel "$(srctree)/$(IMAGE)" -serial stdio $(QEMU_ARGS) > "$(CURDIR)/run.out" 2175ef95663SZhangjin Wu $(Q)$(REPORT) $(CURDIR)/run.out 218662ea60eSWilly Tarreau 219ce1bb82bSZhangjin Wu# report with existing test log 220ce1bb82bSZhangjin Wureport: 221ce1bb82bSZhangjin Wu $(Q)$(REPORT) $(CURDIR)/run.out 222ce1bb82bSZhangjin Wu 223362aecb2SWilly Tarreauclean: 224b25c5284SWilly Tarreau $(call QUIET_CLEAN, sysroot) 225b25c5284SWilly Tarreau $(Q)rm -rf sysroot 226362aecb2SWilly Tarreau $(call QUIET_CLEAN, nolibc-test) 227362aecb2SWilly Tarreau $(Q)rm -f nolibc-test 228fc82d7dbSThomas Weißschuh $(call QUIET_CLEAN, libc-test) 229fc82d7dbSThomas Weißschuh $(Q)rm -f libc-test 230362aecb2SWilly Tarreau $(call QUIET_CLEAN, initramfs) 231362aecb2SWilly Tarreau $(Q)rm -rf initramfs 232662ea60eSWilly Tarreau $(call QUIET_CLEAN, run.out) 233662ea60eSWilly Tarreau $(Q)rm -rf run.out 2344a95be7eSWilly Tarreau 2354a95be7eSWilly Tarreau.PHONY: sysroot/$(ARCH)/include 236