blob: eb5c4f3bdf3be89ed728e681b4a1ce59396c49d8 [file] [log] [blame]
David Brazdile8ebf272020-07-24 10:00:31 +00001# Copyright 2020 The Android KVM Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
David Brazdilef8506d2020-07-27 19:56:16 +000015SELF := $(abspath $(lastword $(MAKEFILE_LIST)))
16
Elena Petrova467cfd92021-01-15 14:56:04 +000017CPUS ?= $(shell nproc)
18MAKEFLAGS += --jobs=$(CPUS)
David Brazdil94acb6f2021-08-23 08:35:03 +000019SHELL ?= $(shell which bash)
Elena Petrova467cfd92021-01-15 14:56:04 +000020
Fuad Tabba42a3b292020-05-13 15:04:03 +010021##
22## Folders
23##
David Brazdilacc8ea42020-04-28 16:01:08 +010024ROOT_DIR := $(CURDIR)
Fuad Tabbad99cc752020-08-17 11:31:56 +000025
David Brazdilacc8ea42020-04-28 16:01:08 +010026OUT_DIR := $(ROOT_DIR)/out
Fuad Tabbacd908402021-02-09 17:18:15 +000027
28# DIST_DIR: target directory for artifacts and logs.
29# The CI build (pKVM Busytown) uses it to separate artifacts from other
30# temporary build files, which are placed in in OUT_DIR.
31# For other users, OUT_DIR and DIST_DIR are typically the same.
32DIST_DIR ?= $(OUT_DIR)
33
34# Copy specified file or directory only if OUT_DIR and DIST_DIR aren't the same.
35#
36# Parameters:
37# $(1): Source files or directories (to be copied recursively)
38# $(2): Destination directory (is created if it doesn't exist)
39ifneq ($(abspath $(OUT_DIR)), $(abspath $(DIST_DIR)))
40define copy_to_dist
41 mkdir -p $(2) && cp -r $(1) $(2)
42endef
Fuad Tabbad99cc752020-08-17 11:31:56 +000043endif
44
Fuad Tabbaa74ba082020-05-29 11:56:18 +010045LINUX_OUT := $(OUT_DIR)/linux
Fuad Tabbacd908402021-02-09 17:18:15 +000046LINUX_DIST := $(DIST_DIR)/linux
David Brazdilef8506d2020-07-27 19:56:16 +000047SCRIPTS_DIR := $(ROOT_DIR)/build
David Brazdilacc8ea42020-04-28 16:01:08 +010048
David Brazdilf5d32762021-08-23 08:40:18 +000049include $(SCRIPTS_DIR)/toolchain.mk
50
Fuad Tabba42a3b292020-05-13 15:04:03 +010051##
52## Common options
53##
54VERBOSE ?= 0
Fuad Tabba7f167f72020-09-17 15:26:45 +010055ARCH = arm64
Fuad Tabbaa74ba082020-05-29 11:56:18 +010056
David Brazdil880a1be2021-09-30 13:13:15 +010057LINUX_OUT_IMAGE := $(LINUX_OUT)/arch/arm64/boot/Image
58LINUX_OUT_MODULES := $(LINUX_OUT)/modules_install
59LINUX_OUT_MODULES_DEP := $(LINUX_OUT)/modules_install.stamp
60
61# KERNEL_IMAGE/MODULES can be set to use own custom kernel image.
62KERNEL_IMAGE ?= $(LINUX_OUT_IMAGE)
63KERNEL_MODULES ?= $(LINUX_OUT_MODULES)
64KERNEL_MODULES_DEP ?= $(LINUX_OUT_MODULES_DEP)
Fuad Tabbaa74ba082020-05-29 11:56:18 +010065
David Brazdilacc8ea42020-04-28 16:01:08 +010066##
Fuad Tabba7f167f72020-09-17 15:26:45 +010067## Toolchain options and variables
68##
69
70# Specifies which toolchain of the prebuilt toolchains to use.
David Brazdil53a70302021-08-20 13:32:11 +000071# Can be either clang, gcc-5.1, or gcc-9.2.
Fuad Tabba67618472021-04-27 11:56:35 +010072# GCC will always use the latest version for building everything else other
73# than the kernel.
Fuad Tabba7f167f72020-09-17 15:26:45 +010074TOOLCHAIN ?= clang
David Brazdilf5d32762021-08-23 08:40:18 +000075$(eval $(call define_toolchain,$(TOOLCHAIN)))
Fuad Tabba7f167f72020-09-17 15:26:45 +010076
77##
David Brazdilacc8ea42020-04-28 16:01:08 +010078## Common targets
79##
80
Fuad Tabba6b9c08e2020-05-18 13:03:01 +010081.DEFAULT_GOAL := default
82
83.PHONY: default
David Brazdil54fb7442020-07-27 09:54:56 +000084default: kvm-unit-tests linux
Fuad Tabba6b9c08e2020-05-18 13:03:01 +010085
David Brazdilacc8ea42020-04-28 16:01:08 +010086.PHONY: clean
Andrew Walbranb8b247c2021-11-12 14:10:34 +000087clean: bl_clean bl33_clean buildroot_clean kvm-unit-tests_clean linux_clean kselftest_clean tfa_clean
David Brazdilacc8ea42020-04-28 16:01:08 +010088
Fuad Tabba42a3b292020-05-13 15:04:03 +010089.PHONY: distclean
David Brazdilf770f602020-07-27 09:53:28 +000090distclean:
Fuad Tabbacd908402021-02-09 17:18:15 +000091 - rm -rf $(OUT_DIR) $(DIST_DIR)
Fuad Tabba42a3b292020-05-13 15:04:03 +010092
David Brazdila8f60f12021-10-04 18:41:47 +010093FORCE:
94
Fuad Tabba42a3b292020-05-13 15:04:03 +010095.PHONY: all
David Brazdile7f36f12021-09-29 13:37:51 +010096all: buildroot kvm-unit-tests linux
Fuad Tabba42a3b292020-05-13 15:04:03 +010097
98.PHONY: help
99help:
100 @echo 'Generic Targets:'
Fuad Tabba6b9c08e2020-05-18 13:03:01 +0100101 @echo ' default - builds all generic targets except for buildroot'
102 @echo ' all - builds all generic targets'
Andrew Walbranb8b247c2021-11-12 14:10:34 +0000103 @echo ' bl - builds U-Boot and TF-A and copies them to the bl output directory'
Andrew Walbran7cfab332021-11-09 14:35:55 +0000104 @echo ' bl33 - U-Boot as BL33'
Fuad Tabba42a3b292020-05-13 15:04:03 +0100105 @echo ' buildroot - the buildroot rootfs image as well as the host qemu'
David Brazdil54fb7442020-07-27 09:54:56 +0000106 @echo ' kvm-unit-tests - the kvm-unit-tests'
David Brazdila8f60f12021-10-04 18:41:47 +0100107 @echo ' linux - the Linux kernel'
108 @echo ' kselftest - the Linux kernel selftests'
Andrew Walbranb8b247c2021-11-12 14:10:34 +0000109 @echo ' tfa - Arm Trusted Firmware-A'
Fuad Tabba42a3b292020-05-13 15:04:03 +0100110 @echo ''
111 @echo 'Clean Targets:'
112 @echo ' clean - cleans all, but keeps the prebuilts'
113 @echo ' distclean - distclean for all targets'
Andrew Walbranb8b247c2021-11-12 14:10:34 +0000114 @echo ' bl_clean - TF-A, U-Boot and the bl output directory'
Andrew Walbran7cfab332021-11-09 14:35:55 +0000115 @echo ' bl33_clean - the U-Boot BL33'
Fuad Tabba42a3b292020-05-13 15:04:03 +0100116 @echo ' buildroot_clean - the buildroot rootfs image as well as the host qemu'
David Brazdil54fb7442020-07-27 09:54:56 +0000117 @echo ' kvm-unit-tests_clean - the kvm-unit-tests'
David Brazdila8f60f12021-10-04 18:41:47 +0100118 @echo ' linux_clean - the Linux kernel'
119 @echo ' kselftest_clean - the Linux kernel selftests'
Andrew Walbranb8b247c2021-11-12 14:10:34 +0000120 @echo ' tfa_clean - Arm Trusted Firmware-A'
Fuad Tabba42a3b292020-05-13 15:04:03 +0100121 @echo ''
David Brazdilef8506d2020-07-27 19:56:16 +0000122 @echo 'Test Targets:'
123 @echo ' test - runs all tests in qemu-aarch64 simulated environment'
124 @echo ' test-kut - runs all kvm-unit-tests in qemu-aarch64 simulated environment'
David Brazdilb2162fe2021-10-04 18:52:09 +0100125 @echo ' test-ksft - runs all kselftests in qemu-aarch64 simulated environment'
David Brazdilef8506d2020-07-27 19:56:16 +0000126 @echo ' test-list - lists all test targets'
127 @echo ''
Pierre-Clément Tosiaa7c3902021-09-22 14:46:06 +0200128 @echo 'Test Parameters:'
Pierre-Clément Tosia4c90ba2021-09-22 14:28:04 +0200129 @echo ' CROSVM_TAR_GZ - path to a custom cvd-host_package.tar.gz'
130 @echo ' (default: uses a pre-built version)'
Pierre-Clément Tosiaa7c3902021-09-22 14:46:06 +0200131 @echo ' VM_IMAGE - path to an image to be used by the VMM to boot the VM'
132 @echo ' (default: uses the host image)'
133 @echo ''
Pierre-Clément Tosi4768c6c2021-08-02 09:53:29 +0000134 @echo 'Test Flags:'
135 @echo ' VERBOSE=[0|1] - prints the verbose output of the scripts running the test'
136 @echo ' (default: 0)'
137 @echo ' QUIET=[0|1] - redirects the output to a file instead of stdout'
138 @echo ' (default: 1)'
139 @echo ' GDB=[0|1] - makes the started test wait for a GDB connection'
140 @echo ' (default: 0)'
141 @echo ''
Fuad Tabba42a3b292020-05-13 15:04:03 +0100142 @echo 'Misc Targets:'
Fuad Tabba6b9c08e2020-05-18 13:03:01 +0100143 @echo ' update-prebuilts - generates/update the prebuilt rootfs image (aarch64) and the qemu host (aarch64)'
Fuad Tabba42a3b292020-05-13 15:04:03 +0100144
David Brazdilacc8ea42020-04-28 16:01:08 +0100145
146##
147## Buildroot
148##
149
Fuad Tabba714a6922020-10-29 15:12:23 +0000150BR_DEFCONFIG ?= qemu_aarch64_virt_kvmunittests_defconfig
Fuad Tabba42a3b292020-05-13 15:04:03 +0100151BR_VERBOSE ?= $(VERBOSE)
David Brazdilacc8ea42020-04-28 16:01:08 +0100152
153BR_SRC := $(ROOT_DIR)/buildroot
154BR_OUT := $(OUT_DIR)/buildroot
155BR_MAKE := $(MAKE) -C $(BR_SRC) V=$(BR_VERBOSE) O=$(BR_OUT)
156
157.PHONY: buildroot
158buildroot:
Fuad Tabba42a3b292020-05-13 15:04:03 +0100159 + $(BR_MAKE) $(BR_DEFCONFIG)
160 + $(BR_MAKE) all
David Brazdilacc8ea42020-04-28 16:01:08 +0100161
162.PHONY: buildroot_clean
163buildroot_clean:
Fuad Tabba42a3b292020-05-13 15:04:03 +0100164 + $(BR_MAKE) clean
165
Andrew Walbranb8b247c2021-11-12 14:10:34 +0000166# Bootloader stages come from TF-A, U-Boot, or just an empty file.
167BL_DIR := $(OUT_DIR)/bl
168BL1_BIN := $(BL_DIR)/bl1.bin
169BL2_BIN := $(BL_DIR)/bl2.bin
170BL31_BIN := $(BL_DIR)/bl31.bin
171BL32_BIN := $(BL_DIR)/bl32.bin
172BL33_BIN := $(BL_DIR)/bl33.bin
173
Andrew Walbran7cfab332021-11-09 14:35:55 +0000174##
175## U-Boot
176##
177UBOOT_SRC := $(ROOT_DIR)/u-boot
178UBOOT_OUT := $(OUT_DIR)/u-boot
Andrew Walbran7cfab332021-11-09 14:35:55 +0000179UBOOT_DEFCONFIG ?= qemu_arm64_defconfig
180UBOOT_CONFIG := $(UBOOT_OUT)/.config
181UBOOT_TOOLCHAIN ?= gcc-9.2
182UBOOT_VERBOSE ?= $(VERBOSE)
183$(eval $(call define_toolchain,$(UBOOT_TOOLCHAIN),UBOOT_))
184UBOOT_MAKE := \
185 PATH=$(UBOOT_TOOLCHAIN_BIN):$(PATH) \
186 CROSS_COMPILE=aarch64-linux-gnu- \
187 $(MAKE) \
188 V=$(UBOOT_VERBOSE) \
189 -C $(UBOOT_SRC) \
190 O=$(UBOOT_OUT)
191
192.PHONY: bl33
Andrew Walbranb8b247c2021-11-12 14:10:34 +0000193bl33: $(UBOOT_OUT)/u-boot.bin
Andrew Walbran7cfab332021-11-09 14:35:55 +0000194
195$(UBOOT_CONFIG): $(UBOOT_SRC)/configs/$(UBOOT_DEFCONFIG)
196 + $(UBOOT_MAKE) $(UBOOT_DEFCONFIG)
197 $(LINUX_SRC)/scripts/config --file $(UBOOT_CONFIG) --set-val CONFIG_BOOTDELAY 0
198 $(LINUX_SRC)/scripts/config --file $(UBOOT_CONFIG) --set-str CONFIG_BOOTCOMMAND \
199 "qfw load 0x40000000 0x50000000 ; booti 0x40000000 - $$\{fdtcontroladdr\}"
200
201$(UBOOT_OUT)/u-boot.bin: $(UBOOT_CONFIG) FORCE
202 + $(UBOOT_MAKE)
203
Andrew Walbran7cfab332021-11-09 14:35:55 +0000204.PHONY: bl33_clean
205bl33_clean:
206 + $(UBOOT_MAKE) distclean
Andrew Walbranb8b247c2021-11-12 14:10:34 +0000207
208##
209## TF-A
210##
211TFA_SRC := $(ROOT_DIR)/trusted-firmware-a
212TFA_OUT := $(OUT_DIR)/trusted-firmware-a
213TFA_TOOLCHAIN ?= gcc-9.2
214TFA_VERBOSE ?= $(VERBOSE)
215$(eval $(call define_toolchain,$(TFA_TOOLCHAIN),TFA_))
216TFA_MAKE := \
217 PATH=$(TFA_TOOLCHAIN_BIN):$(PATH) \
218 $(MAKE) \
219 CROSS_COMPILE="$(TFA_TARGET)-" \
220 PLAT=qemu \
221 ENABLE_SVE_FOR_NS=1 \
222 QEMU_USE_GIC_DRIVER=QEMU_GICV3 \
223 ARM_LINUX_KERNEL_AS_BL33=1 \
224 SPD=trusty \
225 TRUSTY_SPD_WITH_GENERIC_SERVICES=1 \
226 V=$(TFA_VERBOSE) \
227 -C $(TFA_SRC) \
228 BUILD_BASE=$(TFA_OUT)
229
230.PHONY: tfa
231tfa: $(TFA_OUT)/qemu/release/bl1.bin $(TFA_OUT)/qemu/release/bl2.bin $(TFA_OUT)/qemu/release/bl31.bin
232
233$(TFA_OUT)/qemu/release/bl1.bin $(TFA_OUT)/qemu/release/bl2.bin $(TFA_OUT)/qemu/release/bl31.bin &: FORCE
234 + $(TFA_MAKE) all
235
236.PHONY: tfa_clean
237tfa_clean:
238 rm -rf $(TFA_OUT)
239
240##
241## Copy bootloader stages to BL_DIR
242##
243
244$(BL_DIR):
245 mkdir -p $@
246
247$(BL1_BIN) $(BL2_BIN) $(BL31_BIN): $(BL_DIR)/bl%.bin: $(TFA_OUT)/qemu/release/bl%.bin | $(BL_DIR)
248 cp $< $@
249
250# An empty BL32 is enough for us, it just needs to exist.
251$(BL32_BIN): | $(BL_DIR)
252 touch $@
253
254.PHONY: bl
255bl: $(BL1_BIN) $(BL2_BIN) $(BL31_BIN) $(BL32_BIN) $(BL33_BIN)
256
257$(BL33_BIN): $(UBOOT_OUT)/u-boot.bin | $(BL_DIR)
258 cp $< $@
259
260.PHONY: bl_clean
261bl_clean: tfa_clean bl33_clean
262 rm -rf $(BL_DIR)
Fuad Tabba42a3b292020-05-13 15:04:03 +0100263
264##
David Brazdil54fb7442020-07-27 09:54:56 +0000265## kvm-unit-tests
Fuad Tabba42a3b292020-05-13 15:04:03 +0100266##
Fuad Tabba42a3b292020-05-13 15:04:03 +0100267KUT_SRC := $(ROOT_DIR)/kvm-unit-tests
268KUT_OUT := $(OUT_DIR)/kvm-unit-tests
Fuad Tabbacd908402021-02-09 17:18:15 +0000269KUT_DIST := $(DIST_DIR)/kvm-unit-tests
David Brazdilef8506d2020-07-27 19:56:16 +0000270KUT_STAMP := $(KUT_OUT)/kvm-unit-tests.stamp
Fuad Tabba62b21192020-05-13 11:57:26 +0100271
David Brazdilf5d32762021-08-23 08:40:18 +0000272ifneq ($(TOOLCHAIN_IS_LEGACY),1)
273KUT_TOOLCHAIN ?= $(TOOLCHAIN)
274else
275KUT_TOOLCHAIN ?= gcc-9.2
276endif
277$(eval $(call define_toolchain,$(KUT_TOOLCHAIN),KUT_))
278
279ifeq ($(KUT_TOOLCHAIN_IS_LLVM),1)
280KUT_CFLAGS += --target=$(KUT_TARGET) -fpic
281endif
Fuad Tabba42a3b292020-05-13 15:04:03 +0100282
David Brazdil54fb7442020-07-27 09:54:56 +0000283.PHONY: kvm-unit-tests
David Brazdilef8506d2020-07-27 19:56:16 +0000284kvm-unit-tests $(KUT_STAMP):
David Brazdil8f61ea52020-07-27 09:50:56 +0000285 mkdir -p $(KUT_OUT)
286 cd $(KUT_OUT) && \
287 $(KUT_SRC)/configure \
288 --erratatxt=$(KUT_SRC)/errata.txt \
Fuad Tabba7f167f72020-09-17 15:26:45 +0100289 --prefix=$(KUT_OUT) --arch=$(ARCH) \
David Brazdilf5d32762021-08-23 08:40:18 +0000290 --cc=$(KUT_CC) --ld=$(KUT_LD) \
291 --objcopy=$(KUT_OBJCOPY) --objdump=$(KUT_OBJDUMP)
292 COMMON_CFLAGS="$(KUT_CFLAGS)" \
David Brazdile6c26432020-07-27 16:17:44 +0000293 $(MAKE) -C $(KUT_OUT) standalone
Fuad Tabbacd908402021-02-09 17:18:15 +0000294 $(call copy_to_dist, $(KUT_OUT)/tests, $(KUT_DIST))
David Brazdilef8506d2020-07-27 19:56:16 +0000295 touch $(KUT_STAMP)
Fuad Tabba42a3b292020-05-13 15:04:03 +0100296
David Brazdil54fb7442020-07-27 09:54:56 +0000297.PHONY: kvm-unit-tests_clean
298kvm-unit-tests_clean:
David Brazdilef8506d2020-07-27 19:56:16 +0000299 - rm -f $(KUT_STAMP)
David Brazdil8f61ea52020-07-27 09:50:56 +0000300 - $(MAKE) -C $(KUT_OUT) clean
Fuad Tabba42a3b292020-05-13 15:04:03 +0100301
302
303##
304## Linux kernel
305##
306LINUX_DEFCONFIG ?= defconfig
307LINUX_VERBOSE ?= $(VERBOSE)
Fuad Tabba6b9c08e2020-05-18 13:03:01 +0100308LINUX_SRC ?= $(ROOT_DIR)/linux
Fuad Tabbacd908402021-02-09 17:18:15 +0000309LINUX_VMLINUX = $(LINUX_OUT)/vmlinux
David Brazdilf5d32762021-08-23 08:40:18 +0000310LINUX_CONFIG := $(LINUX_OUT)/.config
Fuad Tabbacd908402021-02-09 17:18:15 +0000311LINUX_DIST_FILES = $(LINUX_CONFIG) $(LINUX_VMLINUX) $(KERNEL_IMAGE)
David Brazdil999861a2021-10-04 18:41:32 +0100312LINUX_LSMOD_TXT := $(LINUX_OUT)/lsmod.txt
Fuad Tabba42a3b292020-05-13 15:04:03 +0100313
David Brazdilf5d32762021-08-23 08:40:18 +0000314LINUX_TOOLCHAIN ?= $(TOOLCHAIN)
315$(eval $(call define_toolchain,$(LINUX_TOOLCHAIN),LINUX_))
316
317ifeq ($(LINUX_TOOLCHAIN_IS_LLVM),1)
318LINUX_EXTRA += LLVM=1 LLVM_IAS=1
319endif
320
Fuad Tabba6b9c08e2020-05-18 13:03:01 +0100321LINUX_MAKE := \
Fuad Tabba67618472021-04-27 11:56:35 +0100322 PATH=$(LINUX_TOOLCHAIN_BIN):$(PATH) \
Fuad Tabba7f167f72020-09-17 15:26:45 +0100323 ARCH=$(ARCH) \
Fuad Tabba67618472021-04-27 11:56:35 +0100324 CROSS_COMPILE="$(LINUX_TARGET)-" \
Fuad Tabba6b9c08e2020-05-18 13:03:01 +0100325 $(MAKE) \
Fuad Tabba67618472021-04-27 11:56:35 +0100326 $(LINUX_EXTRA) \
David Brazdilf5d32762021-08-23 08:40:18 +0000327 CC=$(LINUX_CC) \
328 LD=$(LINUX_LD) \
329 OBJCOPY=$(LINUX_OBJCOPY) \
330 OBJDUMP=$(LINUX_OBJDUMP) \
Fuad Tabba62b21192020-05-13 11:57:26 +0100331 -C $(LINUX_SRC) \
332 V=$(LINUX_VERBOSE) \
333 O=$(LINUX_OUT)
Fuad Tabba42a3b292020-05-13 15:04:03 +0100334
David Brazdil999861a2021-10-04 18:41:32 +0100335LINUX_MODULES := \
336 TEST_PRINTF
337
David Brazdilef8506d2020-07-27 19:56:16 +0000338.PHONY: linux_defconfig
339linux_defconfig $(LINUX_CONFIG):
Fuad Tabba6b9c08e2020-05-18 13:03:01 +0100340 + $(LINUX_MAKE) $(LINUX_DEFCONFIG)
David Brazdil999861a2021-10-04 18:41:32 +0100341 # Disable all modules, then selectively enable those in LINUX_MODULES.
342 @ echo > $(LINUX_LSMOD_TXT)
343 + $(LINUX_MAKE) LSMOD=$(LINUX_LSMOD_TXT) localmodconfig
344 $(LINUX_SRC)/scripts/config --file $(LINUX_CONFIG) $(addprefix -m CONFIG_,$(LINUX_MODULES))
David Brazdilef8506d2020-07-27 19:56:16 +0000345
David Brazdila7e35662020-12-03 09:40:38 +0000346.PHONY: linux_menuconfig
347linux_menuconfig: $(LINUX_CONFIG)
348 + $(LINUX_MAKE) menuconfig
349
David Brazdilef8506d2020-07-27 19:56:16 +0000350.PHONY: linux
351linux: $(LINUX_CONFIG)
Fuad Tabba990105b2020-05-12 15:39:52 +0100352 + $(LINUX_MAKE)
Fuad Tabbacd908402021-02-09 17:18:15 +0000353 $(call copy_to_dist, $(LINUX_DIST_FILES), $(LINUX_DIST))
Fuad Tabba42a3b292020-05-13 15:04:03 +0100354
Fuad Tabba6b9c08e2020-05-18 13:03:01 +0100355.PHONY: linux_image
David Brazdil880a1be2021-09-30 13:13:15 +0100356linux_image $(LINUX_OUT_IMAGE): $(LINUX_CONFIG)
David Brazdilef8506d2020-07-27 19:56:16 +0000357 + $(LINUX_MAKE) Image.gz
Fuad Tabbacd908402021-02-09 17:18:15 +0000358 $(call copy_to_dist, $(LINUX_DIST_FILES), $(LINUX_DIST))
David Brazdilef8506d2020-07-27 19:56:16 +0000359
David Brazdil880a1be2021-09-30 13:13:15 +0100360.PHONY: linux_modules
361linux_modules $(LINUX_OUT_MODULES_DEP): $(LINUX_OUT_IMAGE)
362 @ rm -rf $(LINUX_OUT_MODULES)
363 + $(LINUX_MAKE) modules
364 + $(LINUX_MAKE) INSTALL_MOD_PATH=$(LINUX_OUT_MODULES) modules_install
365 @ find $(LINUX_OUT_MODULES) -type f | sort | \
366 xargs sha1sum > $(LINUX_OUT_MODULES_DEP).tmp
367 @ cmp $(LINUX_OUT_MODULES_DEP).tmp $(LINUX_OUT_MODULES_DEP) || \
368 mv $(LINUX_OUT_MODULES_DEP).tmp $(LINUX_OUT_MODULES_DEP)
369
Fuad Tabba42a3b292020-05-13 15:04:03 +0100370.PHONY: linux_clean
371linux_clean:
Fuad Tabba42a3b292020-05-13 15:04:03 +0100372 + $(LINUX_MAKE) mrproper
373
374
375##
David Brazdila8f60f12021-10-04 18:41:47 +0100376## Linux kselftest
377##
378KSFT_VERBOSE ?= $(VERBOSE)
379KSFT_SRC ?= $(LINUX_SRC)
380KSFT_SRC_STAMP ?= $(KSFT_OUT)/ksefltest_src.stamp
381KSFT_OUT ?= $(OUT_DIR)/kselftest
382KSFT_OUT_CONFIG ?= $(KSFT_OUT)/.config
383KSFT_INSTALL_DIR ?= $(KSFT_OUT)/tools/testing/selftests/kselftest_install
384KSFT_STAMP ?= $(KSFT_INSTALL_DIR)/kselftest-list.txt
385
386KSFT_TESTS := \
387 lib:printf.sh \
388 kvm:kvm_create_max_vcpus \
389 kvm:set_memory_region_test \
390 kvm:steal_time
391
392ifneq ($(TOOLCHAIN_IS_LLVM),1)
393KSFT_TOOLCHAIN ?= $(TOOLCHAIN)
394else
395KSFT_TOOLCHAIN ?= gcc-9.2
396endif
397$(eval $(call define_toolchain,$(KSFT_TOOLCHAIN),KSFT_))
398
399KSFT_MAKE := \
400 PATH=$(KSFT_TOOLCHAIN_BIN):$(PATH) \
401 ARCH=$(ARCH) \
402 CROSS_COMPILE="$(KSFT_TARGET)-" \
403 $(MAKE) \
404 -C $(KSFT_OUT) \
405 V=$(KSFT_VERBOSE)
406
407KSFT_MAKE_TARGETS := $(sort $(foreach t,$(KSFT_TESTS),$(firstword $(subst :, ,$(t)))))
408
409# Copy the source to another folder because 'kselftest-install' clobbers it.
410# Stamp is touched if any files (not directories) were synced.
411$(KSFT_SRC_STAMP): FORCE
412 @ mkdir -p $(KSFT_OUT) $(@D)
413 @ LIST="$$(rsync -a --info=name $(KSFT_SRC)/ $(KSFT_OUT) | grep -vE '/$$')"; \
414 [ -f $@ -a -z "$$LIST" ] || touch $@
415
416.PHONY: kselftest
417kselftest $(KSFT_STAMP): $(KSFT_SRC_STAMP) $(LINUX_CONFIG)
418 @ cp $(LINUX_CONFIG) $(KSFT_OUT_CONFIG)
419 + $(KSFT_MAKE) olddefconfig prepare
420 + $(KSFT_MAKE) TARGETS="$(KSFT_MAKE_TARGETS)" kselftest-install
421
422.PHONY: kselftest_clean
423kselftest_clean:
424 @ rm -rf $(KSFT_OUT)
425
426
427##
Fuad Tabba62b21192020-05-13 11:57:26 +0100428## Generating/Updating prebuilts
429##
430
Pierre-Clément Tosi58b789a2021-09-27 13:01:17 +0200431PREBUILTS_CROSVM_EXT4 := prebuilts/linux-aarch64/images/crosvm.ext4
David Brazdile3446db2021-02-02 20:58:49 +0000432PREBUILTS_ROOTFS_EXT4 := prebuilts/linux-aarch64/images/rootfs.ext4
433PREBUILTS_QEMU_ROM := prebuilts/linux-aarch64/images/efi-virtio.rom
434PREBUILTS_QEMU_BIN := prebuilts/linux-x86/qemu
435
Pierre-Clément Tosi58b789a2021-09-27 13:01:17 +0200436CUSTOM_CROSVM_EXT4 := $(OUT_DIR)/images/crosvm.ext4
437
Pierre-Clément Tosia4c90ba2021-09-22 14:28:04 +0200438ifeq ($(CROSVM_TAR_GZ),)
Pierre-Clément Tosi58b789a2021-09-27 13:01:17 +0200439CROSVM_EXT4 := $(PREBUILTS_CROSVM_EXT4)
Pierre-Clément Tosia4c90ba2021-09-22 14:28:04 +0200440else
Pierre-Clément Tosi58b789a2021-09-27 13:01:17 +0200441CROSVM_EXT4 := $(CUSTOM_CROSVM_EXT4)
Pierre-Clément Tosia4c90ba2021-09-22 14:28:04 +0200442endif
Pierre-Clément Tosi609555c2021-09-22 14:13:20 +0200443
Fuad Tabba7f167f72020-09-17 15:26:45 +0100444.PHONY:update-prebuilts
Fuad Tabba62b21192020-05-13 11:57:26 +0100445update-prebuilts: \
David Brazdile3446db2021-02-02 20:58:49 +0000446 $(PREBUILTS_ROOTFS_EXT4) \
Pierre-Clément Tosi609555c2021-09-22 14:13:20 +0200447 update-prebuilts-crosvm \
David Brazdile3446db2021-02-02 20:58:49 +0000448 $(PREBUILTS_QEMU_ROM) \
449 $(PREBUILTS_QEMU_BIN)
Fuad Tabba62b21192020-05-13 11:57:26 +0100450
David Brazdile3446db2021-02-02 20:58:49 +0000451$(PREBUILTS_ROOTFS_EXT4): buildroot
David Brazdil4f0603f2021-02-02 16:18:18 +0000452 mkdir -p $(shell dirname $@)
Fuad Tabba62b21192020-05-13 11:57:26 +0100453 cp $(BR_OUT)/images/rootfs.ext4 $@
David Brazdil4f0603f2021-02-02 16:18:18 +0000454
David Brazdile3446db2021-02-02 20:58:49 +0000455$(PREBUILTS_QEMU_ROM): buildroot
David Brazdil4f0603f2021-02-02 16:18:18 +0000456 mkdir -p $(shell dirname $@)
Fuad Tabba62b21192020-05-13 11:57:26 +0100457 cp $(BR_OUT)/per-package/host-qemu/host/share/qemu/efi-virtio.rom $@
458
David Brazdil4f0603f2021-02-02 16:18:18 +0000459CF_DOWNLOAD_AOSP_SH := $(ROOT_DIR)/build/cuttlefish/download-aosp.sh
Pierre-Clément Tosi609555c2021-09-22 14:13:20 +0200460
461# A PHONY target is necessary for allowing the user to update the archive
462# because, as we are downloading it from a remote server, make dependencies are
463# unable to express the local-->remote dependency:
464.PHONY: update-prebuilts-crosvm
465update-prebuilts-crosvm $(PREBUILTS_CROSVM_TAR_GZ):
466 @rm -f $(PREBUILTS_CROSVM_TAR_GZ) # else, wget will create "$@.1"
467 cd $(dir $(PREBUILTS_CROSVM_TAR_GZ)) && $(CF_DOWNLOAD_AOSP_SH) -XCa arm64
468 @touch -c $(PREBUILTS_CROSVM_TAR_GZ) # wget doesn't set timestamp to now
469
David Brazdil4f0603f2021-02-02 16:18:18 +0000470CROSVM_IMG_SIZE_MB := 1024
471
Pierre-Clément Tosi58b789a2021-09-27 13:01:17 +0200472$(CUSTOM_CROSVM_EXT4): TMP_DIR := $(shell mktemp -d)
473$(CUSTOM_CROSVM_EXT4): $(CROSVM_TAR_GZ)
Pierre-Clément Tosi609555c2021-09-22 14:13:20 +0200474 mkdir -p $(@D)
475 cd $(TMP_DIR) && tar xzvf $(abspath $<)
David Brazdil4f0603f2021-02-02 16:18:18 +0000476 dd if=/dev/zero of=$@.tmp bs=1M count=$(CROSVM_IMG_SIZE_MB)
477 mkfs.ext4 -d $(TMP_DIR) $@.tmp
478 rm -rf $(TMP_DIR)
479 mv $@.tmp $@
480
Fuad Tabbace135522020-08-26 10:15:47 +0100481# The qemu binary
David Brazdile3446db2021-02-02 20:58:49 +0000482.PHONY:$(PREBUILTS_QEMU_BIN)
483$(PREBUILTS_QEMU_BIN):
Fuad Tabba62b21192020-05-13 11:57:26 +0100484 - rm -rf $@
Fuad Tabba7f167f72020-09-17 15:26:45 +0100485 ${SCRIPTS_DIR}/aarch64/build_qemu.sh $(OUT_DIR)/qemu ${TOOLCHAIN_BIN}
Fuad Tabba62b21192020-05-13 11:57:26 +0100486 mkdir -p $@/bin
Fuad Tabbace135522020-08-26 10:15:47 +0100487 cp $(OUT_DIR)/qemu/qemu-system-aarch64 $@/bin
Fuad Tabba62b21192020-05-13 11:57:26 +0100488
Fuad Tabba62b21192020-05-13 11:57:26 +0100489##
David Brazdilef8506d2020-07-27 19:56:16 +0000490## Run tests
Fuad Tabba62b21192020-05-13 11:57:26 +0100491##
492
Pierre-Clément Tosid1813062021-07-30 15:50:41 +0000493KVM_MODES := vhe nvhe pkvm
David Brazdilb2162fe2021-10-04 18:52:09 +0100494
Pierre-Clément Tosid1813062021-07-30 15:50:41 +0000495hyphensuffix = $(lastword $(subst -, ,$(1)))
David Brazdilb2162fe2021-10-04 18:52:09 +0100496space := $(subst ,, )
497# Like wordlist but takes negative upper bounds (-2 means remove last two words)
498revwordlist = $(wordlist $(1),$(shell echo '(($(2) - 1 + $(words $(3))) % $(words $(3))) + 1' | bc),$(3))
499hyphenlist = $(subst $(space),-,$(call revwordlist,$(1),$(2),$(subst -, ,$(3))))
Pierre-Clément Tosid1813062021-07-30 15:50:41 +0000500
Pierre-Clément Tosi253cd132021-08-02 08:49:39 +0000501RUN_TEST_SH := $(SCRIPTS_DIR)/kvm-unit-tests/run_test.sh
502RUN_TEST = $(RUN_TEST_SH) \
503 -d $@ \
504 -M $(call hyphensuffix,$@) \
505 -k "$(KERNEL_IMAGE)" \
Pierre-Clément Tosi253cd132021-08-02 08:49:39 +0000506 $(RUN_TEST_ARGS) "$(RUN_TEST_HOST_SH)" \
David Brazdil880a1be2021-09-30 13:13:15 +0100507 -- \
508 -D modules:${KERNEL_MODULES}/lib/modules \
509 $(RUN_TEST_QEMU_ARGS)
Pierre-Clément Tosi253cd132021-08-02 08:49:39 +0000510
Pierre-Clément Tosi4768c6c2021-08-02 09:53:29 +0000511ifeq ($(VERBOSE),1)
512RUN_TEST_ARGS += -v
513endif
514
515ifneq ($(QUIET),0)
516RUN_TEST_ARGS += -o "$(RUN_TEST_LOG_DIR)/$@.log"
517endif
518
519ifeq ($(GDB),1)
520# RUN_TEST_SH defaults to no-timeout when -G is passed but -t forces one:
521RUN_TEST_ARGS += -G
522else
523# Without -G, RUN_TEST_SH defaults to a non-zero timeout duration.
524# RUN_TEST_TIMEOUT should be use if a specific value is wanted (see 'man timeout').
525# In particular, RUN_TEST_TIMEOUT=0 prevents the test from timing out.
526RUN_TEST_ARGS += $(if $(RUN_TEST_TIMEOUT),-t ,)$(RUN_TEST_TIMEOUT)
527endif
528
David Brazdilef8506d2020-07-27 19:56:16 +0000529# Root test targets. Dynamically generated per-test targets
530# will add themselves as dependencies of these.
531.PHONY: test test-list
532test:
533test-list:
Fuad Tabba62b21192020-05-13 11:57:26 +0100534
David Brazdilef8506d2020-07-27 19:56:16 +0000535#
536# kvm-unit-tests
537#
538
Pierre-Clément Tosi253cd132021-08-02 08:49:39 +0000539KUT_RUN_TEST = $(RUN_TEST_SH)
David Brazdilef8506d2020-07-27 19:56:16 +0000540KUT_GEN_MAKEFILE := $(SCRIPTS_DIR)/kvm-unit-tests/gen_makefile.sh
541KUT_MAKEFILE := $(OUT_DIR)/Makefile.kvm-unit-tests
542KUT_TEST_DIR := $(KUT_OUT)/tests
Fuad Tabbacd908402021-02-09 17:18:15 +0000543KUT_LOG_DIR := $(DIST_DIR)/logs/kvm-unit-tests/
Fuad Tabba62b21192020-05-13 11:57:26 +0100544
Fuad Tabba42a3b292020-05-13 15:04:03 +0100545# Exclude tests that require user interaction or are known to fail.
David Brazdilef8506d2020-07-27 19:56:16 +0000546KUT_EXCLUDE := (.+migrat.+)|(gicv2-.+)|(pmu-event-introspection)|(micro-bench)
Fuad Tabba42a3b292020-05-13 15:04:03 +0100547
David Brazdilef8506d2020-07-27 19:56:16 +0000548# Generate a Makefile with targets per test and configuration.
549# Pass in variable/target names from this Makefile that the
550# generated Makefile will refer to.
551$(KUT_MAKEFILE): $(SELF) $(KUT_GEN_MAKEFILE) $(KUT_STAMP)
552 @ mkdir -p $(shell dirname $@)
553 @ $(KUT_GEN_MAKEFILE) \
554 $(KUT_TEST_DIR) "$(KUT_EXCLUDE)" \
555 KERNEL_IMAGE KUT_LOG_DIR test test-list \
556 > $@.tmp
557 @ mv $@.tmp $@
558include $(KUT_MAKEFILE)
David Brazdil19d0b7a2021-02-02 18:15:44 +0000559
560#
David Brazdil490370a2021-03-10 22:55:02 +0000561# Basic boot tests in the various arm64 KVM modes
562#
563
Pierre-Clément Tosid1813062021-07-30 15:50:41 +0000564BOOT_TESTS := $(addprefix test-boot-,$(KVM_MODES))
David Brazdil490370a2021-03-10 22:55:02 +0000565
David Brazdil490370a2021-03-10 22:55:02 +0000566.PHONY: test-boot
567test-boot: $(BOOT_TESTS)
568
Pierre-Clément Tosi63d40872021-07-30 16:23:48 +0000569TEST_LISTS += test-list-boot
570test-list-boot: LISTED_TESTS = $(BOOT_TESTS)
David Brazdil490370a2021-03-10 22:55:02 +0000571
Pierre-Clément Tosi48436862021-07-30 16:34:25 +0000572TESTS += $(BOOT_TESTS)
Pierre-Clément Tosi253cd132021-08-02 08:49:39 +0000573$(BOOT_TESTS): RUN_TEST_LOG_DIR = $(DIST_DIR)/logs/boot
574$(BOOT_TESTS): RUN_TEST_HOST_SH = $(SCRIPTS_DIR)/aarch64/test_boot.sh
575$(BOOT_TESTS): RUN_TEST_QEMU_ARGS = -a "boot_test_mode=$(call hyphensuffix,$@)"
David Brazdil490370a2021-03-10 22:55:02 +0000576
577#
David Brazdil19d0b7a2021-02-02 18:15:44 +0000578# CrosVM Hello World test
579#
580
Pierre-Clément Tosid1813062021-07-30 15:50:41 +0000581CROSVM_HELLOWORLD_TESTS := $(addprefix test-crosvm-helloworld-,$(KVM_MODES))
David Brazdil19d0b7a2021-02-02 18:15:44 +0000582
Pierre-Clément Tosiaa7c3902021-09-22 14:46:06 +0200583VM_IMAGE ?= $(KERNEL_IMAGE)
584
David Brazdil19d0b7a2021-02-02 18:15:44 +0000585.PHONY: test-crosvm-helloworld
586test-crosvm-helloworld: $(CROSVM_HELLOWORLD_TESTS)
587
Pierre-Clément Tosi63d40872021-07-30 16:23:48 +0000588TEST_LISTS += test-list-crosvm-helloworld
589test-list-crosvm-helloworld: LISTED_TESTS = $(CROSVM_HELLOWORLD_TESTS)
David Brazdil19d0b7a2021-02-02 18:15:44 +0000590
Pierre-Clément Tosi48436862021-07-30 16:34:25 +0000591TESTS += $(CROSVM_HELLOWORLD_TESTS)
Pierre-Clément Tosi609555c2021-09-22 14:13:20 +0200592$(CROSVM_HELLOWORLD_TESTS): $(CROSVM_EXT4)
Pierre-Clément Tosi253cd132021-08-02 08:49:39 +0000593$(CROSVM_HELLOWORLD_TESTS): RUN_TEST_LOG_DIR = $(DIST_DIR)/logs/crosvm/helloworld
594$(CROSVM_HELLOWORLD_TESTS): RUN_TEST_HOST_SH = $(SCRIPTS_DIR)/crosvm/helloworld/host.sh
Pierre-Clément Tosi253cd132021-08-02 08:49:39 +0000595$(CROSVM_HELLOWORLD_TESTS): RUN_TEST_QEMU_ARGS = \
596 -s 4 \
597 -R $(SCRIPTS_DIR)/crosvm/helloworld/guest.sh \
Pierre-Clément Tosiaa7c3902021-09-22 14:46:06 +0200598 -R $(VM_IMAGE) \
Pierre-Clément Tosi253cd132021-08-02 08:49:39 +0000599 -R $(PREBUILTS_ROOTFS_EXT4) \
Pierre-Clément Tosi609555c2021-09-22 14:13:20 +0200600 -R $(CROSVM_EXT4)
Pierre-Clément Tosi4768c6c2021-08-02 09:53:29 +0000601$(CROSVM_HELLOWORLD_TESTS): RUN_TEST_TIMEOUT := 300s
Pierre-Clément Tosi63d40872021-07-30 16:23:48 +0000602
David Brazdilb2162fe2021-10-04 18:52:09 +0100603#
604# Linux kselftests
605#
606
607KSFT_TEST_TARGETS := $(foreach t,$(KSFT_TESTS),$(foreach m,$(KVM_MODES),\
608 test-ksft-$(subst :,-,$(t))-$(m)))
609
610.PHONY: test-ksft
611test-ksft: $(KSFT_TEST_TARGETS)
612
613
614TEST_LISTS += test-list-ksft
615test-list-ksft: LISTED_TESTS = $(KSFT_TEST_TARGETS)
616
617TESTS += $(KSFT_TEST_TARGETS)
618$(KSFT_TEST_TARGETS): $(KSFT_STAMP)
619$(KSFT_TEST_TARGETS): RUN_TEST_LOG_DIR = $(DIST_DIR)/logs/kselftest
620$(KSFT_TEST_TARGETS): RUN_TEST_HOST_SH = $(SCRIPTS_DIR)/kselftest/host.sh
621$(KSFT_TEST_TARGETS): RUN_TEST_QEMU_ARGS = \
622 -D kselftest:$(KSFT_INSTALL_DIR) \
623 -a 'ksft=$(call hyphenlist,3,3,$@):$(call hyphenlist,4,-1,$@)'
624
Pierre-Clément Tosi48436862021-07-30 16:34:25 +0000625.PHONY test: $(TESTS)
David Brazdil880a1be2021-09-30 13:13:15 +0100626$(TESTS): $(KERNEL_IMAGE) $(KERNEL_MODULES_DEP)
Pierre-Clément Tosi253cd132021-08-02 08:49:39 +0000627 @ $(RUN_TEST)
Pierre-Clément Tosi63d40872021-07-30 16:23:48 +0000628.PHONY test-list: $(TEST_LISTS)
629$(TEST_LISTS):
630 @ for x in $(LISTED_TESTS); do echo $$x; done