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 85*17362f3dSThomas WeißschuhQEMU_ARCH_ppc64le = ppc64 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 11695315486SThomas WeißschuhCFLAGS_i386 = $(call cc-option,-m32) 117587e9845SZhangjin WuCFLAGS_ppc = -m32 -mbig-endian -mno-vsx $(call cc-option,-mmultiple) 118faeb4e09SZhangjin WuCFLAGS_ppc64 = -m64 -mbig-endian -mno-vsx $(call cc-option,-mmultiple) 1198a5040cbSZhangjin WuCFLAGS_ppc64le = -m64 -mlittle-endian -mno-vsx $(call cc-option,-mabi=elfv2) 1200043e6f2SSven SchnelleCFLAGS_s390 = -m64 121dd58d666SWilly TarreauCFLAGS_mips = -EL 1220093c2daSThomas WeißschuhCFLAGS_STACKPROTECTOR ?= $(call cc-option,-mstack-protector-guard=global $(call cc-option,-fstack-protector-all)) 12345f65f8dSThomas WeißschuhCFLAGS ?= -Os -fno-ident -fno-asynchronous-unwind-tables -std=c89 -W -Wall -Wextra \ 124255ffeeeSThomas Weißschuh $(call cc-option,-fno-stack-protector) \ 125c6c3734fSZhangjin Wu $(CFLAGS_$(XARCH)) $(CFLAGS_STACKPROTECTOR) 126711edef8SThomas WeißschuhLDFLAGS := 127362aecb2SWilly Tarreau 1284beb9be8SZhangjin WuREPORT ?= awk '/\[OK\][\r]*$$/{p++} /\[FAIL\][\r]*$$/{if (!f) printf("\n"); f++; print;} /\[SKIPPED\][\r]*$$/{s++} \ 1297d92e893SZhangjin Wu END{ printf("\n%3d test(s): %3d passed, %3d skipped, %3d failed => status: ", p+s+f, p, s, f); \ 130c0315c79SZhangjin Wu if (f) printf("failure\n"); else if (s) printf("warning\n"); else printf("success\n");; \ 1317d92e893SZhangjin Wu printf("\nSee all results in %s\n", ARGV[1]); }' 1325ef95663SZhangjin Wu 133ffc297feSWilly Tarreauhelp: 134ffc297feSWilly Tarreau @echo "Supported targets under selftests/nolibc:" 135ffc297feSWilly Tarreau @echo " all call the \"run\" target below" 136ffc297feSWilly Tarreau @echo " help this help" 137ffc297feSWilly Tarreau @echo " sysroot create the nolibc sysroot here (uses \$$ARCH)" 138ffc297feSWilly Tarreau @echo " nolibc-test build the executable (uses \$$CC and \$$CROSS_COMPILE)" 139fc82d7dbSThomas Weißschuh @echo " libc-test build an executable using the compiler's default libc instead" 140c6c3734fSZhangjin Wu @echo " run-user runs the executable under QEMU (uses \$$XARCH, \$$TEST)" 14145839d09SThomas Weißschuh @echo " initramfs.cpio prepare the initramfs archive with nolibc-test" 14245839d09SThomas Weißschuh @echo " initramfs prepare the initramfs tree with nolibc-test" 143c6c3734fSZhangjin Wu @echo " defconfig create a fresh new default config (uses \$$XARCH)" 14445839d09SThomas Weißschuh @echo " kernel (re)build the kernel (uses \$$XARCH)" 14545839d09SThomas Weißschuh @echo " kernel-standalone (re)build the kernel with the initramfs (uses \$$XARCH)" 146c6c3734fSZhangjin Wu @echo " run runs the kernel in QEMU after building it (uses \$$XARCH, \$$TEST)" 147c6c3734fSZhangjin Wu @echo " rerun runs a previously prebuilt kernel in QEMU (uses \$$XARCH, \$$TEST)" 148ffc297feSWilly Tarreau @echo " clean clean the sysroot, initramfs, build and output files" 149ffc297feSWilly Tarreau @echo "" 150ffc297feSWilly Tarreau @echo "The output file is \"run.out\". Test ranges may be passed using \$$TEST." 151ffc297feSWilly Tarreau @echo "" 152ffc297feSWilly Tarreau @echo "Currently using the following variables:" 153ffc297feSWilly Tarreau @echo " ARCH = $(ARCH)" 154c6c3734fSZhangjin Wu @echo " XARCH = $(XARCH)" 155ffc297feSWilly Tarreau @echo " CROSS_COMPILE = $(CROSS_COMPILE)" 156ffc297feSWilly Tarreau @echo " CC = $(CC)" 157ffc297feSWilly Tarreau @echo " OUTPUT = $(OUTPUT)" 158ffc297feSWilly Tarreau @echo " TEST = $(TEST)" 159c6c3734fSZhangjin Wu @echo " QEMU_ARCH = $(if $(QEMU_ARCH),$(QEMU_ARCH),UNKNOWN_ARCH) [determined from \$$XARCH]" 160c6c3734fSZhangjin Wu @echo " IMAGE_NAME = $(if $(IMAGE_NAME),$(IMAGE_NAME),UNKNOWN_ARCH) [determined from \$$XARCH]" 161ffc297feSWilly Tarreau @echo "" 162ffc297feSWilly Tarreau 163ffc297feSWilly Tarreauall: run 164362aecb2SWilly Tarreau 165b25c5284SWilly Tarreausysroot: sysroot/$(ARCH)/include 166b25c5284SWilly Tarreau 167b25c5284SWilly Tarreausysroot/$(ARCH)/include: 1684a95be7eSWilly Tarreau $(Q)rm -rf sysroot/$(ARCH) sysroot/sysroot 169b25c5284SWilly Tarreau $(QUIET_MKDIR)mkdir -p sysroot 170b25c5284SWilly Tarreau $(Q)$(MAKE) -C ../../../include/nolibc ARCH=$(ARCH) OUTPUT=$(CURDIR)/sysroot/ headers_standalone 171b25c5284SWilly Tarreau $(Q)mv sysroot/sysroot sysroot/$(ARCH) 172b25c5284SWilly Tarreau 173850fad7dSZhangjin Wuifneq ($(NOLIBC_SYSROOT),0) 174b25c5284SWilly Tarreaunolibc-test: nolibc-test.c sysroot/$(ARCH)/include 175362aecb2SWilly Tarreau $(QUIET_CC)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ \ 176265fbb4bSThomas Weißschuh -nostdlib -nostdinc -static -Isysroot/$(ARCH)/include $< -lgcc 177850fad7dSZhangjin Wuelse 178850fad7dSZhangjin Wunolibc-test: nolibc-test.c 179850fad7dSZhangjin Wu $(QUIET_CC)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ \ 180850fad7dSZhangjin Wu -nostdlib -static -include ../../../include/nolibc/nolibc.h $< -lgcc 181850fad7dSZhangjin Wuendif 182362aecb2SWilly Tarreau 183fc82d7dbSThomas Weißschuhlibc-test: nolibc-test.c 184418c8468SZhangjin Wu $(QUIET_CC)$(HOSTCC) -o $@ $< 185fc82d7dbSThomas Weißschuh 186b8143407SZhangjin Wu# local libc-test 187cfb672f9SZhangjin Wurun-libc-test: libc-test 188cfb672f9SZhangjin Wu $(Q)./libc-test > "$(CURDIR)/run.out" || : 189cfb672f9SZhangjin Wu $(Q)$(REPORT) $(CURDIR)/run.out 190cfb672f9SZhangjin Wu 191b8143407SZhangjin Wu# local nolibc-test 192b8143407SZhangjin Wurun-nolibc-test: nolibc-test 193b8143407SZhangjin Wu $(Q)./nolibc-test > "$(CURDIR)/run.out" || : 194b8143407SZhangjin Wu $(Q)$(REPORT) $(CURDIR)/run.out 195b8143407SZhangjin Wu 196c54ba417SWilly Tarreau# qemu user-land test 197c54ba417SWilly Tarreaurun-user: nolibc-test 198c54ba417SWilly Tarreau $(Q)qemu-$(QEMU_ARCH) ./nolibc-test > "$(CURDIR)/run.out" || : 1995ef95663SZhangjin Wu $(Q)$(REPORT) $(CURDIR)/run.out 200c54ba417SWilly Tarreau 20145839d09SThomas Weißschuhinitramfs.cpio: kernel nolibc-test 20245839d09SThomas Weißschuh $(QUIET_GEN)echo 'file /init nolibc-test 755 0 0' | $(srctree)/usr/gen_init_cpio - > initramfs.cpio 20345839d09SThomas Weißschuh 204362aecb2SWilly Tarreauinitramfs: nolibc-test 205362aecb2SWilly Tarreau $(QUIET_MKDIR)mkdir -p initramfs 206362aecb2SWilly Tarreau $(call QUIET_INSTALL, initramfs/init) 207362aecb2SWilly Tarreau $(Q)cp nolibc-test initramfs/init 208362aecb2SWilly Tarreau 2095c43fd79SWilly Tarreaudefconfig: 2105c43fd79SWilly Tarreau $(Q)$(MAKE) -C $(srctree) ARCH=$(ARCH) CC=$(CC) CROSS_COMPILE=$(CROSS_COMPILE) mrproper $(DEFCONFIG) prepare 2115c43fd79SWilly Tarreau 21245839d09SThomas Weißschuhkernel: 21345839d09SThomas Weißschuh $(Q)$(MAKE) -C $(srctree) ARCH=$(ARCH) CC=$(CC) CROSS_COMPILE=$(CROSS_COMPILE) $(IMAGE_NAME) 21445839d09SThomas Weißschuh 21545839d09SThomas Weißschuhkernel-standalone: initramfs 216d248cabfSWilly Tarreau $(Q)$(MAKE) -C $(srctree) ARCH=$(ARCH) CC=$(CC) CROSS_COMPILE=$(CROSS_COMPILE) $(IMAGE_NAME) CONFIG_INITRAMFS_SOURCE=$(CURDIR)/initramfs 217d248cabfSWilly Tarreau 218662ea60eSWilly Tarreau# run the tests after building the kernel 21945839d09SThomas Weißschuhrun: kernel initramfs.cpio 22045839d09SThomas Weißschuh $(Q)qemu-system-$(QEMU_ARCH) -display none -no-reboot -kernel "$(srctree)/$(IMAGE)" -initrd initramfs.cpio -serial stdio $(QEMU_ARGS) > "$(CURDIR)/run.out" 2215ef95663SZhangjin Wu $(Q)$(REPORT) $(CURDIR)/run.out 222662ea60eSWilly Tarreau 223662ea60eSWilly Tarreau# re-run the tests from an existing kernel 224662ea60eSWilly Tarreaurerun: 22545839d09SThomas Weißschuh $(Q)qemu-system-$(QEMU_ARCH) -display none -no-reboot -kernel "$(srctree)/$(IMAGE)" -initrd initramfs.cpio -serial stdio $(QEMU_ARGS) > "$(CURDIR)/run.out" 2265ef95663SZhangjin Wu $(Q)$(REPORT) $(CURDIR)/run.out 227662ea60eSWilly Tarreau 228ce1bb82bSZhangjin Wu# report with existing test log 229ce1bb82bSZhangjin Wureport: 230ce1bb82bSZhangjin Wu $(Q)$(REPORT) $(CURDIR)/run.out 231ce1bb82bSZhangjin Wu 232362aecb2SWilly Tarreauclean: 233b25c5284SWilly Tarreau $(call QUIET_CLEAN, sysroot) 234b25c5284SWilly Tarreau $(Q)rm -rf sysroot 235362aecb2SWilly Tarreau $(call QUIET_CLEAN, nolibc-test) 236362aecb2SWilly Tarreau $(Q)rm -f nolibc-test 237fc82d7dbSThomas Weißschuh $(call QUIET_CLEAN, libc-test) 238fc82d7dbSThomas Weißschuh $(Q)rm -f libc-test 23945839d09SThomas Weißschuh $(call QUIET_CLEAN, initramfs.cpio) 24045839d09SThomas Weißschuh $(Q)rm -rf initramfs.cpio 241362aecb2SWilly Tarreau $(call QUIET_CLEAN, initramfs) 242362aecb2SWilly Tarreau $(Q)rm -rf initramfs 243662ea60eSWilly Tarreau $(call QUIET_CLEAN, run.out) 244662ea60eSWilly Tarreau $(Q)rm -rf run.out 2454a95be7eSWilly Tarreau 2464a95be7eSWilly Tarreau.PHONY: sysroot/$(ARCH)/include 247