1 # SPDX-License-Identifier: GPL-2.0
2 # Makefile for nolibc tests
3 include ../../../scripts/Makefile.include
4 
5 # we're in ".../tools/testing/selftests/nolibc"
6 ifeq ($(srctree),)
7 srctree := $(patsubst %/tools/testing/selftests/,%,$(dir $(CURDIR)))
8 endif
9 
10 ifeq ($(ARCH),)
11 include $(srctree)/scripts/subarch.include
12 ARCH = $(SUBARCH)
13 endif
14 
15 # kernel image names by architecture
16 IMAGE_i386    = arch/x86/boot/bzImage
17 IMAGE_x86_64  = arch/x86/boot/bzImage
18 IMAGE_x86     = arch/x86/boot/bzImage
19 IMAGE_arm64   = arch/arm64/boot/Image
20 IMAGE_arm     = arch/arm/boot/zImage
21 IMAGE_mips    = vmlinuz
22 IMAGE_riscv   = arch/riscv/boot/Image
23 IMAGE_s390    = arch/s390/boot/bzImage
24 IMAGE         = $(IMAGE_$(ARCH))
25 IMAGE_NAME    = $(notdir $(IMAGE))
26 
27 # default kernel configurations that appear to be usable
28 DEFCONFIG_i386    = defconfig
29 DEFCONFIG_x86_64  = defconfig
30 DEFCONFIG_x86     = defconfig
31 DEFCONFIG_arm64   = defconfig
32 DEFCONFIG_arm     = multi_v7_defconfig
33 DEFCONFIG_mips    = malta_defconfig
34 DEFCONFIG_riscv   = defconfig
35 DEFCONFIG_s390    = defconfig
36 DEFCONFIG         = $(DEFCONFIG_$(ARCH))
37 
38 # optional tests to run (default = all)
39 TEST =
40 
41 # QEMU_ARCH: arch names used by qemu
42 QEMU_ARCH_i386    = i386
43 QEMU_ARCH_x86_64  = x86_64
44 QEMU_ARCH_x86     = x86_64
45 QEMU_ARCH_arm64   = aarch64
46 QEMU_ARCH_arm     = arm
47 QEMU_ARCH_mips    = mipsel  # works with malta_defconfig
48 QEMU_ARCH_riscv   = riscv64
49 QEMU_ARCH_s390    = s390x
50 QEMU_ARCH         = $(QEMU_ARCH_$(ARCH))
51 
52 # QEMU_ARGS : some arch-specific args to pass to qemu
53 QEMU_ARGS_i386    = -M pc -append "console=ttyS0,9600 i8042.noaux panic=-1 $(TEST:%=NOLIBC_TEST=%)"
54 QEMU_ARGS_x86_64  = -M pc -append "console=ttyS0,9600 i8042.noaux panic=-1 $(TEST:%=NOLIBC_TEST=%)"
55 QEMU_ARGS_x86     = -M pc -append "console=ttyS0,9600 i8042.noaux panic=-1 $(TEST:%=NOLIBC_TEST=%)"
56 QEMU_ARGS_arm64   = -M virt -cpu cortex-a53 -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)"
57 QEMU_ARGS_arm     = -M virt -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)"
58 QEMU_ARGS_mips    = -M malta -append "panic=-1 $(TEST:%=NOLIBC_TEST=%)"
59 QEMU_ARGS_riscv   = -M virt -append "console=ttyS0 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
60 QEMU_ARGS_s390    = -M s390-ccw-virtio -m 1G -append "console=ttyS0 panic=-1 $(TEST:%=NOLIBC_TEST=%)"
61 QEMU_ARGS         = $(QEMU_ARGS_$(ARCH))
62 
63 # OUTPUT is only set when run from the main makefile, otherwise
64 # it defaults to this nolibc directory.
65 OUTPUT ?= $(CURDIR)/
66 
67 ifeq ($(V),1)
68 Q=
69 else
70 Q=@
71 endif
72 
73 CFLAGS_s390 = -m64
74 CFLAGS  ?= -Os -fno-ident -fno-asynchronous-unwind-tables $(CFLAGS_$(ARCH))
75 LDFLAGS := -s
76 
77 help:
78 	@echo "Supported targets under selftests/nolibc:"
79 	@echo "  all          call the \"run\" target below"
80 	@echo "  help         this help"
81 	@echo "  sysroot      create the nolibc sysroot here (uses \$$ARCH)"
82 	@echo "  nolibc-test  build the executable (uses \$$CC and \$$CROSS_COMPILE)"
83 	@echo "  run-user     runs the executable under QEMU (uses \$$ARCH, \$$TEST)"
84 	@echo "  initramfs    prepare the initramfs with nolibc-test"
85 	@echo "  defconfig    create a fresh new default config (uses \$$ARCH)"
86 	@echo "  kernel       (re)build the kernel with the initramfs (uses \$$ARCH)"
87 	@echo "  run          runs the kernel in QEMU after building it (uses \$$ARCH, \$$TEST)"
88 	@echo "  rerun        runs a previously prebuilt kernel in QEMU (uses \$$ARCH, \$$TEST)"
89 	@echo "  clean        clean the sysroot, initramfs, build and output files"
90 	@echo ""
91 	@echo "The output file is \"run.out\". Test ranges may be passed using \$$TEST."
92 	@echo ""
93 	@echo "Currently using the following variables:"
94 	@echo "  ARCH          = $(ARCH)"
95 	@echo "  CROSS_COMPILE = $(CROSS_COMPILE)"
96 	@echo "  CC            = $(CC)"
97 	@echo "  OUTPUT        = $(OUTPUT)"
98 	@echo "  TEST          = $(TEST)"
99 	@echo "  QEMU_ARCH     = $(if $(QEMU_ARCH),$(QEMU_ARCH),UNKNOWN_ARCH) [determined from \$$ARCH]"
100 	@echo "  IMAGE_NAME    = $(if $(IMAGE_NAME),$(IMAGE_NAME),UNKNOWN_ARCH) [determined from \$$ARCH]"
101 	@echo ""
102 
103 all: run
104 
105 sysroot: sysroot/$(ARCH)/include
106 
107 sysroot/$(ARCH)/include:
108 	$(Q)rm -rf sysroot/$(ARCH) sysroot/sysroot
109 	$(QUIET_MKDIR)mkdir -p sysroot
110 	$(Q)$(MAKE) -C ../../../include/nolibc ARCH=$(ARCH) OUTPUT=$(CURDIR)/sysroot/ headers_standalone
111 	$(Q)mv sysroot/sysroot sysroot/$(ARCH)
112 
113 nolibc-test: nolibc-test.c sysroot/$(ARCH)/include
114 	$(QUIET_CC)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ \
115 	  -nostdlib -static -Isysroot/$(ARCH)/include $< -lgcc
116 
117 # qemu user-land test
118 run-user: nolibc-test
119 	$(Q)qemu-$(QEMU_ARCH) ./nolibc-test > "$(CURDIR)/run.out" || :
120 	$(Q)grep -w FAIL "$(CURDIR)/run.out" && echo "See all results in $(CURDIR)/run.out" || echo "$$(grep -c ^[0-9].*OK $(CURDIR)/run.out) test(s) passed."
121 
122 initramfs: nolibc-test
123 	$(QUIET_MKDIR)mkdir -p initramfs
124 	$(call QUIET_INSTALL, initramfs/init)
125 	$(Q)cp nolibc-test initramfs/init
126 
127 defconfig:
128 	$(Q)$(MAKE) -C $(srctree) ARCH=$(ARCH) CC=$(CC) CROSS_COMPILE=$(CROSS_COMPILE) mrproper $(DEFCONFIG) prepare
129 
130 kernel: initramfs
131 	$(Q)$(MAKE) -C $(srctree) ARCH=$(ARCH) CC=$(CC) CROSS_COMPILE=$(CROSS_COMPILE) $(IMAGE_NAME) CONFIG_INITRAMFS_SOURCE=$(CURDIR)/initramfs
132 
133 # run the tests after building the kernel
134 run: kernel
135 	$(Q)qemu-system-$(QEMU_ARCH) -display none -no-reboot -kernel "$(srctree)/$(IMAGE)" -serial stdio $(QEMU_ARGS) > "$(CURDIR)/run.out"
136 	$(Q)grep -w FAIL "$(CURDIR)/run.out" && echo "See all results in $(CURDIR)/run.out" || echo "$$(grep -c ^[0-9].*OK $(CURDIR)/run.out) test(s) passed."
137 
138 # re-run the tests from an existing kernel
139 rerun:
140 	$(Q)qemu-system-$(QEMU_ARCH) -display none -no-reboot -kernel "$(srctree)/$(IMAGE)" -serial stdio $(QEMU_ARGS) > "$(CURDIR)/run.out"
141 	$(Q)grep -w FAIL "$(CURDIR)/run.out" && echo "See all results in $(CURDIR)/run.out" || echo "$$(grep -c ^[0-9].*OK $(CURDIR)/run.out) test(s) passed."
142 
143 clean:
144 	$(call QUIET_CLEAN, sysroot)
145 	$(Q)rm -rf sysroot
146 	$(call QUIET_CLEAN, nolibc-test)
147 	$(Q)rm -f nolibc-test
148 	$(call QUIET_CLEAN, initramfs)
149 	$(Q)rm -rf initramfs
150 	$(call QUIET_CLEAN, run.out)
151 	$(Q)rm -rf run.out
152 
153 .PHONY: sysroot/$(ARCH)/include
154