blob: 585201631f50b7096491a54e1af6c705b65581a7 [file] [log] [blame]
David Brazdilee5e25d2020-01-24 14:17:45 +00001#!/usr/bin/env python3
Andrew Scull18834872018-10-12 11:48:09 +01002#
Andrew Walbran692b3252019-03-07 15:51:31 +00003# Copyright 2018 The Hafnium Authors.
Andrew Scull18834872018-10-12 11:48:09 +01004#
Andrew Walbrane959ec12020-06-17 15:01:09 +01005# Use of this source code is governed by a BSD-style
6# license that can be found in the LICENSE file or at
7# https://opensource.org/licenses/BSD-3-Clause.
Andrew Scull18834872018-10-12 11:48:09 +01008
David Brazdil2df24082019-09-05 11:55:08 +01009"""Script which drives invocation of tests and parsing their output to produce
10a results report.
Andrew Scullbc7189d2018-08-14 09:35:13 +010011"""
12
13from __future__ import print_function
14
Andrew Scull3b62f2b2018-08-21 14:26:12 +010015import xml.etree.ElementTree as ET
16
Andrew Scullbc7189d2018-08-14 09:35:13 +010017import argparse
Olivier Depreza6d2e6d2020-11-06 18:09:50 +010018from abc import ABC, abstractmethod
David Brazdil2df24082019-09-05 11:55:08 +010019import collections
Andrew Scull04502e42018-09-03 14:54:52 +010020import datetime
David Brazdil4f9cf9a2020-02-06 17:34:44 +000021import importlib
Andrew Scullbc7189d2018-08-14 09:35:13 +010022import json
23import os
24import re
25import subprocess
26import sys
Andrew Walbran42bf2842020-06-05 18:50:19 +010027import time
Olivier Depreza6d2e6d2020-11-06 18:09:50 +010028import fdt
J-Alves18a25f92021-05-04 17:47:41 +010029from telnetlib import Telnet
Andrew Scullbc7189d2018-08-14 09:35:13 +010030
Andrew Scull845fc9b2019-04-03 12:44:26 +010031HFTEST_LOG_PREFIX = "[hftest] "
32HFTEST_LOG_FAILURE_PREFIX = "Failure:"
33HFTEST_LOG_FINISHED = "FINISHED"
34
David Brazdil17e76652020-01-29 14:44:19 +000035HFTEST_CTRL_GET_COMMAND_LINE = "[hftest_ctrl:get_command_line]"
36HFTEST_CTRL_FINISHED = "[hftest_ctrl:finished]"
37
David Brazdil2df24082019-09-05 11:55:08 +010038HF_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(
39 os.path.abspath(__file__))))
David Brazdil5715f042019-08-27 11:11:51 +010040DTC_SCRIPT = os.path.join(HF_ROOT, "build", "image", "dtc.py")
David Brazdil2df24082019-09-05 11:55:08 +010041FVP_BINARY = os.path.join(
Olivier Deprez9f4bad42021-06-18 12:19:07 +020042 os.path.dirname(HF_ROOT), "fvp", "Base_RevC_AEMvA_pkg", "models",
Olivier Deprez16440852022-05-05 14:55:32 +020043 "Linux64_GCC-9.3", "FVP_Base_RevC-2xAEMvA")
Olivier Depreza6d2e6d2020-11-06 18:09:50 +010044HF_PREBUILTS = os.path.join(HF_ROOT, "prebuilts")
45FVP_PREBUILTS_TFA_TRUSTY_ROOT = os.path.join(
46 HF_PREBUILTS, "linux-aarch64", "trusted-firmware-a-trusty", "fvp")
David Brazdil2df24082019-09-05 11:55:08 +010047FVP_PREBUILT_DTS = os.path.join(
Olivier Depreza6d2e6d2020-11-06 18:09:50 +010048 FVP_PREBUILTS_TFA_TRUSTY_ROOT, "fvp-base-gicv3-psci-1t.dts")
49
50FVP_PREBUILT_TFA_ROOT = os.path.join(
51 HF_PREBUILTS, "linux-aarch64", "trusted-firmware-a", "fvp")
Andrew Scull845fc9b2019-04-03 12:44:26 +010052
J-Alves852fe742021-04-22 11:59:55 +010053VM_NODE_REGEX = "vm[1-9]"
54
David Brazdil2df24082019-09-05 11:55:08 +010055def read_file(path):
56 with open(path, "r") as f:
57 return f.read()
Andrew Scull845fc9b2019-04-03 12:44:26 +010058
David Brazdil2df24082019-09-05 11:55:08 +010059def write_file(path, to_write, append=False):
60 with open(path, "a" if append else "w") as f:
61 f.write(to_write)
62
63def append_file(path, to_write):
64 write_file(path, to_write, append=True)
65
66def join_if_not_None(*args):
67 return " ".join(filter(lambda x: x, args))
68
J-Alves852fe742021-04-22 11:59:55 +010069def get_vm_node_from_manifest(dts : str):
70 """ Get VM node string from Partition's extension to Partition Manager's
71 manifest."""
72 match = re.search(VM_NODE_REGEX, dts)
73 if not match:
74 raise Exception("Partition's node is not defined in its manifest.")
75 return match.group()
76
77def correct_vm_node(dts: str, node_index : int):
78 """ The vm node is being appended to the Partition Manager manifests.
79 Ideally, these files would be reused accross various test set-ups."""
80 return dts.replace(get_vm_node_from_manifest(dts), f"vm{node_index}")
81
J-Alves8cc7dbb2021-04-16 10:38:48 +010082DT = collections.namedtuple("DT", ["dts", "dtb"])
83
David Brazdil2df24082019-09-05 11:55:08 +010084class ArtifactsManager:
85 """Class which manages folder with test artifacts."""
86
87 def __init__(self, log_dir):
88 self.created_files = []
89 self.log_dir = log_dir
90
91 # Create directory.
Andrew Scull845fc9b2019-04-03 12:44:26 +010092 try:
David Brazdil2df24082019-09-05 11:55:08 +010093 os.makedirs(self.log_dir)
94 except OSError:
95 if not os.path.isdir(self.log_dir):
96 raise
97 print("Logs saved under", log_dir)
98
99 # Create files expected by the Sponge test result parser.
100 self.sponge_log_path = self.create_file("sponge_log", ".log")
101 self.sponge_xml_path = self.create_file("sponge_log", ".xml")
102
David Brazdil623b6812019-09-09 11:41:08 +0100103 def gen_file_path(self, basename, extension):
104 """Generate path to a file in the log directory."""
105 return os.path.join(self.log_dir, basename + extension)
106
David Brazdil2df24082019-09-05 11:55:08 +0100107 def create_file(self, basename, extension):
108 """Create and touch a new file in the log folder. Ensure that no other
109 file of the same name was created by this instance of ArtifactsManager.
110 """
111 # Determine the path of the file.
David Brazdil623b6812019-09-09 11:41:08 +0100112 path = self.gen_file_path(basename, extension)
David Brazdil2df24082019-09-05 11:55:08 +0100113
114 # Check that the path is unique.
115 assert(path not in self.created_files)
116 self.created_files += [ path ]
117
118 # Touch file.
119 with open(path, "w") as f:
120 pass
121
122 return path
Andrew Scullbc7189d2018-08-14 09:35:13 +0100123
David Brazdil623b6812019-09-09 11:41:08 +0100124 def get_file(self, basename, extension):
125 """Return path to a file in the log folder. Assert that it was created
126 by this instance of ArtifactsManager."""
127 path = self.gen_file_path(basename, extension)
128 assert(path in self.created_files)
129 return path
130
Andrew Scullbc7189d2018-08-14 09:35:13 +0100131
David Brazdil2df24082019-09-05 11:55:08 +0100132# Tuple holding the arguments common to all driver constructors.
133# This is to avoid having to pass arguments from subclasses to superclasses.
134DriverArgs = collections.namedtuple("DriverArgs", [
135 "artifacts",
J-Alves8cc7dbb2021-04-16 10:38:48 +0100136 "hypervisor",
137 "spmc",
David Brazdil2df24082019-09-05 11:55:08 +0100138 "initrd",
139 "vm_args",
David Brazdil17e76652020-01-29 14:44:19 +0000140 "cpu",
J-Alves18a25f92021-05-04 17:47:41 +0100141 "partitions",
142 "global_run_name",
David Brazdil2df24082019-09-05 11:55:08 +0100143 ])
Marc Bonnici0a125632019-04-01 13:46:52 +0100144
David Brazdil2df24082019-09-05 11:55:08 +0100145# State shared between the common Driver class and its subclasses during
146# a single invocation of the target platform.
David Brazdil7325eaf2019-09-27 13:04:51 +0100147class DriverRunState:
148 def __init__(self, log_path):
149 self.log_path = log_path
150 self.ret_code = 0
Andrew Walbran98656252019-03-14 14:52:29 +0000151
David Brazdil7325eaf2019-09-27 13:04:51 +0100152 def set_ret_code(self, ret_code):
153 self.ret_code = ret_code
Andrew Walbran98656252019-03-14 14:52:29 +0000154
David Brazdil0dbb41f2019-09-09 18:03:35 +0100155class DriverRunException(Exception):
156 """Exception thrown if subprocess invoked by a driver returned non-zero
157 status code. Used to fast-exit from a driver command sequence."""
158 pass
159
160
David Brazdil2df24082019-09-05 11:55:08 +0100161class Driver:
162 """Parent class of drivers for all testable platforms."""
163
164 def __init__(self, args):
165 self.args = args
166
David Brazdil623b6812019-09-09 11:41:08 +0100167 def get_run_log(self, run_name):
168 """Return path to the main log of a given test run."""
169 return self.args.artifacts.get_file(run_name, ".log")
170
David Brazdil2df24082019-09-05 11:55:08 +0100171 def start_run(self, run_name):
172 """Hook called by Driver subclasses before they invoke the target
173 platform."""
David Brazdil7325eaf2019-09-27 13:04:51 +0100174 return DriverRunState(self.args.artifacts.create_file(run_name, ".log"))
David Brazdil2df24082019-09-05 11:55:08 +0100175
Andrew Walbranf636b842020-01-10 11:46:12 +0000176 def exec_logged(self, run_state, exec_args, cwd=None):
David Brazdil2df24082019-09-05 11:55:08 +0100177 """Run a subprocess on behalf of a Driver subclass and append its
178 stdout and stderr to the main log."""
David Brazdil0dbb41f2019-09-09 18:03:35 +0100179 assert(run_state.ret_code == 0)
David Brazdil2df24082019-09-05 11:55:08 +0100180 with open(run_state.log_path, "a") as f:
181 f.write("$ {}\r\n".format(" ".join(exec_args)))
182 f.flush()
Andrew Walbranf636b842020-01-10 11:46:12 +0000183 ret_code = subprocess.call(exec_args, stdout=f, stderr=f, cwd=cwd)
David Brazdil0dbb41f2019-09-09 18:03:35 +0100184 if ret_code != 0:
David Brazdil7325eaf2019-09-27 13:04:51 +0100185 run_state.set_ret_code(ret_code)
David Brazdil0dbb41f2019-09-09 18:03:35 +0100186 raise DriverRunException()
David Brazdil2df24082019-09-05 11:55:08 +0100187
David Brazdil0dbb41f2019-09-09 18:03:35 +0100188 def finish_run(self, run_state):
David Brazdil2df24082019-09-05 11:55:08 +0100189 """Hook called by Driver subclasses after they finished running the
190 target platform. `ret_code` argument is the return code of the main
191 command run by the driver. A corresponding log message is printed."""
192 # Decode return code and add a message to the log.
193 with open(run_state.log_path, "a") as f:
David Brazdil0dbb41f2019-09-09 18:03:35 +0100194 if run_state.ret_code == 124:
David Brazdil2df24082019-09-05 11:55:08 +0100195 f.write("\r\n{}{} timed out\r\n".format(
196 HFTEST_LOG_PREFIX, HFTEST_LOG_FAILURE_PREFIX))
David Brazdil0dbb41f2019-09-09 18:03:35 +0100197 elif run_state.ret_code != 0:
David Brazdil2df24082019-09-05 11:55:08 +0100198 f.write("\r\n{}{} process return code {}\r\n".format(
David Brazdil0dbb41f2019-09-09 18:03:35 +0100199 HFTEST_LOG_PREFIX, HFTEST_LOG_FAILURE_PREFIX,
200 run_state.ret_code))
David Brazdil2df24082019-09-05 11:55:08 +0100201
202 # Append log of this run to full test log.
203 log_content = read_file(run_state.log_path)
204 append_file(
205 self.args.artifacts.sponge_log_path,
206 log_content + "\r\n\r\n")
207 return log_content
Andrew Walbran98656252019-03-14 14:52:29 +0000208
209
David Brazdil2df24082019-09-05 11:55:08 +0100210class QemuDriver(Driver):
211 """Driver which runs tests in QEMU."""
212
Andrew Walbranf636b842020-01-10 11:46:12 +0000213 def __init__(self, args, qemu_wd, tfa):
David Brazdil2df24082019-09-05 11:55:08 +0100214 Driver.__init__(self, args)
Andrew Walbranf636b842020-01-10 11:46:12 +0000215 self.qemu_wd = qemu_wd
216 self.tfa = tfa
David Brazdil2df24082019-09-05 11:55:08 +0100217
David Brazdila2358d42020-01-27 18:51:38 +0000218 def gen_exec_args(self, test_args, is_long_running):
David Brazdil2df24082019-09-05 11:55:08 +0100219 """Generate command line arguments for QEMU."""
David Brazdil3cc24aa2019-09-27 10:24:41 +0100220 time_limit = "120s" if is_long_running else "10s"
Fuad Tabba36c8c2b2019-11-04 16:55:32 +0000221 # If no CPU configuration is selected, then test against the maximum
222 # configuration, "max", supported by QEMU.
223 cpu = self.args.cpu or "max"
David Brazdil2df24082019-09-05 11:55:08 +0100224 exec_args = [
David Brazdil3cc24aa2019-09-27 10:24:41 +0100225 "timeout", "--foreground", time_limit,
Andrew Walbranf636b842020-01-10 11:46:12 +0000226 os.path.abspath("prebuilts/linux-x64/qemu/qemu-system-aarch64"),
Andrew Walbranab4b2d52020-06-11 16:54:10 +0100227 "-no-reboot", "-machine", "virt,virtualization=on,gic-version=3",
Andrew Walbranf636b842020-01-10 11:46:12 +0000228 "-cpu", cpu, "-smp", "4", "-m", "1G",
David Brazdil2df24082019-09-05 11:55:08 +0100229 "-nographic", "-nodefaults", "-serial", "stdio",
J-Alves8cc7dbb2021-04-16 10:38:48 +0100230 "-d", "unimp", "-kernel", os.path.abspath(self.args.hypervisor),
David Brazdil2df24082019-09-05 11:55:08 +0100231 ]
232
Andrew Walbranf636b842020-01-10 11:46:12 +0000233 if self.tfa:
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100234 bl1_path = os.path.join(
235 HF_PREBUILTS, "linux-aarch64", "trusted-firmware-a-trusty",
236 "qemu", "bl1.bin")
Andrew Walbranf636b842020-01-10 11:46:12 +0000237 exec_args += ["-bios",
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100238 os.path.abspath(bl1_path),
239 "-machine", "secure=on", "-semihosting-config",
Andrew Walbranab4b2d52020-06-11 16:54:10 +0100240 "enable=on,target=native"]
Andrew Walbranf636b842020-01-10 11:46:12 +0000241
David Brazdil2df24082019-09-05 11:55:08 +0100242 if self.args.initrd:
Andrew Walbranf636b842020-01-10 11:46:12 +0000243 exec_args += ["-initrd", os.path.abspath(self.args.initrd)]
David Brazdil2df24082019-09-05 11:55:08 +0100244
245 vm_args = join_if_not_None(self.args.vm_args, test_args)
246 if vm_args:
247 exec_args += ["-append", vm_args]
248
249 return exec_args
250
David Brazdil3cc24aa2019-09-27 10:24:41 +0100251 def run(self, run_name, test_args, is_long_running):
David Brazdil2df24082019-09-05 11:55:08 +0100252 """Run test given by `test_args` in QEMU."""
253 run_state = self.start_run(run_name)
David Brazdil0dbb41f2019-09-09 18:03:35 +0100254
255 try:
David Brazdil0dbb41f2019-09-09 18:03:35 +0100256 # Execute test in QEMU..
David Brazdila2358d42020-01-27 18:51:38 +0000257 exec_args = self.gen_exec_args(test_args, is_long_running)
Andrew Walbranf636b842020-01-10 11:46:12 +0000258 self.exec_logged(run_state, exec_args,
259 cwd=self.qemu_wd)
David Brazdil0dbb41f2019-09-09 18:03:35 +0100260 except DriverRunException:
261 pass
262
263 return self.finish_run(run_state)
Andrew Scullbc7189d2018-08-14 09:35:13 +0100264
David Brazdil94fd1e92020-02-03 16:45:20 +0000265 def finish(self):
266 """Clean up after running tests."""
267 pass
268
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100269class FvpDriver(Driver, ABC):
270 """Base class for driver which runs tests in Arm FVP emulator."""
David Brazdil2df24082019-09-05 11:55:08 +0100271
272 def __init__(self, args):
Fuad Tabba36c8c2b2019-11-04 16:55:32 +0000273 if args.cpu:
274 raise ValueError("FVP emulator does not support the --cpu option.")
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100275 super().__init__(args)
David Brazdil2df24082019-09-05 11:55:08 +0100276
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100277 @property
278 @abstractmethod
279 def CPU_START_ADDRESS(self):
280 pass
David Brazdil2df24082019-09-05 11:55:08 +0100281
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100282 @property
283 @abstractmethod
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100284 def FVP_PREBUILT_BL31(self):
285 pass
286
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100287 def create_dt(self, run_name : str):
288 """Create DT related files, and return respective paths in a tuple
289 (dts,dtb)"""
J-Alves8cc7dbb2021-04-16 10:38:48 +0100290 return DT(self.args.artifacts.create_file(run_name, ".dts"),
291 self.args.artifacts.create_file(run_name, ".dtb"))
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100292
J-Alves8cc7dbb2021-04-16 10:38:48 +0100293 def compile_dt(self, run_state, dt : DT):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100294 """Compile DT calling dtc."""
295 dtc_args = [
J-Alves8cc7dbb2021-04-16 10:38:48 +0100296 DTC_SCRIPT, "compile", "-i", dt.dts, "-o", dt.dtb,
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100297 ]
298 self.exec_logged(run_state, dtc_args)
299
300 def create_uart_log(self, run_name : str, file_name : str):
301 """Create uart log file, and return path"""
302 return self.args.artifacts.create_file(run_name, file_name)
303
304 def get_img_and_ldadd(self, partitions : dict):
305 ret = []
306 for i, p in enumerate(partitions):
J-Alves852fe742021-04-22 11:59:55 +0100307 with open(p["dts"], "r") as dt:
308 dts = dt.read()
309 manifest = fdt.parse_dts(dts)
310 vm_node = get_vm_node_from_manifest(dts)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100311 load_address = manifest.get_property("load_address",
J-Alves852fe742021-04-22 11:59:55 +0100312 f"/hypervisor/{vm_node}").value
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100313 ret.append((p["img"], load_address))
314 return ret
315
316 def get_manifests_from_json(self, partitions : list):
317 manifests = ""
318 if partitions is not None:
J-Alves852fe742021-04-22 11:59:55 +0100319 for i, p in enumerate(partitions):
320 manifests += correct_vm_node(read_file(p["dts"]), i + 1)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100321 return manifests
322
323 @abstractmethod
J-Alves8cc7dbb2021-04-16 10:38:48 +0100324 def gen_dts(self, dt, test_args):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100325 """Abstract method to generate dts file. This specific to the use case
326 so should be implemented within derived driver"""
327 pass
328
329 @abstractmethod
David Brazdil2df24082019-09-05 11:55:08 +0100330 def gen_fvp_args(
J-Alves8cc7dbb2021-04-16 10:38:48 +0100331 self, is_long_running, uart0_log_path, uart1_log_path, dt):
David Brazdil2df24082019-09-05 11:55:08 +0100332 """Generate command line arguments for FVP."""
Andrew Walbranee5418e2019-11-27 17:43:05 +0000333 time_limit = "80s" if is_long_running else "40s"
David Brazdil2df24082019-09-05 11:55:08 +0100334 fvp_args = [
Andrew Walbranee5418e2019-11-27 17:43:05 +0000335 "timeout", "--foreground", time_limit,
David Brazdil2df24082019-09-05 11:55:08 +0100336 FVP_BINARY,
J-Alves10446d82021-04-26 11:52:57 +0100337 "-C", "pci.pci_smmuv3.mmu.SMMU_AIDR=2",
338 "-C", "pci.pci_smmuv3.mmu.SMMU_IDR0=0x0046123B",
339 "-C", "pci.pci_smmuv3.mmu.SMMU_IDR1=0x00600002",
340 "-C", "pci.pci_smmuv3.mmu.SMMU_IDR3=0x1714",
341 "-C", "pci.pci_smmuv3.mmu.SMMU_IDR5=0xFFFF0472",
342 "-C", "pci.pci_smmuv3.mmu.SMMU_S_IDR1=0xA0000002",
343 "-C", "pci.pci_smmuv3.mmu.SMMU_S_IDR2=0",
344 "-C", "pci.pci_smmuv3.mmu.SMMU_S_IDR3=0",
David Brazdil2df24082019-09-05 11:55:08 +0100345 "-C", "pctl.startup=0.0.0.0",
Olivier Deprezcd857002022-05-09 09:06:24 +0200346 "-C", "bp.secure_memory=1",
David Brazdil2df24082019-09-05 11:55:08 +0100347 "-C", "cluster0.NUM_CORES=4",
348 "-C", "cluster1.NUM_CORES=4",
349 "-C", "cache_state_modelled=0",
350 "-C", "bp.vis.disable_visualisation=true",
351 "-C", "bp.vis.rate_limit-enable=false",
352 "-C", "bp.terminal_0.start_telnet=false",
353 "-C", "bp.terminal_1.start_telnet=false",
354 "-C", "bp.terminal_2.start_telnet=false",
355 "-C", "bp.terminal_3.start_telnet=false",
356 "-C", "bp.pl011_uart0.untimed_fifos=1",
357 "-C", "bp.pl011_uart0.unbuffered_output=1",
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100358 "-C", f"cluster0.cpu0.RVBAR={self.CPU_START_ADDRESS}",
359 "-C", f"cluster0.cpu1.RVBAR={self.CPU_START_ADDRESS}",
360 "-C", f"cluster0.cpu2.RVBAR={self.CPU_START_ADDRESS}",
361 "-C", f"cluster0.cpu3.RVBAR={self.CPU_START_ADDRESS}",
362 "-C", f"cluster1.cpu0.RVBAR={self.CPU_START_ADDRESS}",
363 "-C", f"cluster1.cpu1.RVBAR={self.CPU_START_ADDRESS}",
364 "-C", f"cluster1.cpu2.RVBAR={self.CPU_START_ADDRESS}",
365 "-C", f"cluster1.cpu3.RVBAR={self.CPU_START_ADDRESS}",
366 "--data",
367 f"cluster0.cpu0={self.FVP_PREBUILT_BL31}@{self.CPU_START_ADDRESS}",
David Brazdil2df24082019-09-05 11:55:08 +0100368 "-C", "bp.ve_sysregs.mmbSiteDefault=0",
369 "-C", "bp.ve_sysregs.exit_on_shutdown=1",
Raghu Krishnamurthy75ebf8c2021-11-28 07:22:12 -0800370 "-C", "cluster0.has_arm_v8-5=1",
371 "-C", "cluster1.has_arm_v8-5=1",
372 "-C", "cluster0.has_branch_target_exception=1",
373 "-C", "cluster1.has_branch_target_exception=1",
Maksims Svecovsce1261f2022-03-04 15:22:58 +0000374 "-C", "cluster0.memory_tagging_support_level=2",
375 "-C", "cluster1.memory_tagging_support_level=2",
376 "-C", "bp.dram_metadata.is_enabled=1",
David Brazdil2df24082019-09-05 11:55:08 +0100377 ]
J-Alves18a25f92021-05-04 17:47:41 +0100378
379 if uart0_log_path and uart1_log_path:
380 fvp_args += [
381 "-C", f"bp.pl011_uart0.out_file={uart0_log_path}",
382 "-C", f"bp.pl011_uart1.out_file={uart1_log_path}",
383 ]
David Brazdil2df24082019-09-05 11:55:08 +0100384 return fvp_args
385
David Brazdil3cc24aa2019-09-27 10:24:41 +0100386 def run(self, run_name, test_args, is_long_running):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100387 """ Run test """
David Brazdil2df24082019-09-05 11:55:08 +0100388 run_state = self.start_run(run_name)
J-Alves8cc7dbb2021-04-16 10:38:48 +0100389 dt = self.create_dt(run_name)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100390 uart0_log_path = self.create_uart_log(run_name, ".uart0.log")
391 uart1_log_path = self.create_uart_log(run_name, ".uart1.log")
David Brazdil2df24082019-09-05 11:55:08 +0100392
David Brazdil0dbb41f2019-09-09 18:03:35 +0100393 try:
J-Alves8cc7dbb2021-04-16 10:38:48 +0100394 self.gen_dts(dt, test_args)
395 self.compile_dt(run_state, dt)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100396 fvp_args = self.gen_fvp_args(is_long_running, uart0_log_path,
J-Alves8cc7dbb2021-04-16 10:38:48 +0100397 uart1_log_path, dt)
David Brazdil0dbb41f2019-09-09 18:03:35 +0100398 self.exec_logged(run_state, fvp_args)
399 except DriverRunException:
400 pass
David Brazdil2df24082019-09-05 11:55:08 +0100401
402 # Append UART0 output to main log.
403 append_file(run_state.log_path, read_file(uart0_log_path))
David Brazdil0dbb41f2019-09-09 18:03:35 +0100404 return self.finish_run(run_state)
David Brazdil2df24082019-09-05 11:55:08 +0100405
David Brazdil94fd1e92020-02-03 16:45:20 +0000406 def finish(self):
407 """Clean up after running tests."""
408 pass
409
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100410class FvpDriverHypervisor(FvpDriver):
411 """
412 Driver which runs tests in Arm FVP emulator, with hafnium as hypervisor
413 """
414 INITRD_START= 0x84000000
Olivier Depreza516f482021-04-30 18:47:59 +0200415 INITRD_END = 0x86000000 #Default value, however may change if initrd in args
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100416
417 def __init__(self, args):
418 self.vms_in_partitions_json = args.partitions and args.partitions["VMs"]
419 super().__init__(args)
420
421 @property
422 def CPU_START_ADDRESS(self):
423 return "0x04020000"
424
425 @property
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100426 def FVP_PREBUILT_BL31(self):
427 return os.path.join(FVP_PREBUILTS_TFA_TRUSTY_ROOT, "bl31.bin")
428
429 @property
J-Alves38223dd2021-04-20 17:31:48 +0100430 def HYPERVISOR_ADDRESS(self):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100431 return "0x80000000"
432
J-Alves38223dd2021-04-20 17:31:48 +0100433 @property
434 def HYPERVISOR_DTB_ADDRESS(self):
435 return "0x82000000"
436
J-Alves8cc7dbb2021-04-16 10:38:48 +0100437 def gen_dts(self, dt, test_args):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100438 """Create a DeviceTree source which will be compiled into a DTB and
439 passed to FVP for a test run."""
440
441 vm_args = join_if_not_None(self.args.vm_args, test_args)
J-Alves8cc7dbb2021-04-16 10:38:48 +0100442 write_file(dt.dts, read_file(FVP_PREBUILT_DTS))
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100443
444 # Write the vm arguments to the partition manifest
445 to_append = f"""
446/ {{
447 chosen {{
448 bootargs = "{vm_args}";
449 stdout-path = "serial0:115200n8";
450 linux,initrd-start = <{self.INITRD_START if self.args.initrd else 0}>;
451 linux,initrd-end = <{self.INITRD_END if self.args.initrd else 0}>;
452 }};
453}};"""
454 if self.vms_in_partitions_json:
455 to_append += self.get_manifests_from_json(self.args.partitions["VMs"])
456
J-Alves8cc7dbb2021-04-16 10:38:48 +0100457 append_file(dt.dts, to_append)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100458
459 def gen_fvp_args(
J-Alves8cc7dbb2021-04-16 10:38:48 +0100460 self, is_long_running, uart0_log_path, uart1_log_path, dt, call_super = True):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100461 """Generate command line arguments for FVP."""
J-Alves38223dd2021-04-20 17:31:48 +0100462 common_args = (self, is_long_running, uart0_log_path, uart1_log_path, dt)
463 fvp_args = FvpDriver.gen_fvp_args(*common_args) if call_super else []
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100464
J-Alves8cc7dbb2021-04-16 10:38:48 +0100465 fvp_args += [
J-Alves38223dd2021-04-20 17:31:48 +0100466 "--data", f"cluster0.cpu0={dt.dtb}@{self.HYPERVISOR_DTB_ADDRESS}",
467 "--data", f"cluster0.cpu0={self.args.hypervisor}@{self.HYPERVISOR_ADDRESS}",
J-Alves8cc7dbb2021-04-16 10:38:48 +0100468 ]
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100469
470 if self.vms_in_partitions_json:
471 img_ldadd = self.get_img_and_ldadd(self.args.partitions["VMs"])
472 for img, ldadd in img_ldadd:
473 fvp_args += ["--data", f"cluster0.cpu0={img}@{hex(ldadd)}"]
474
475 if self.args.initrd:
476 fvp_args += [
477 "--data",
478 f"cluster0.cpu0={self.args.initrd}@{self.INITRD_START}"
479 ]
480 return fvp_args
481
482class FvpDriverSPMC(FvpDriver):
483 """
484 Driver which runs tests in Arm FVP emulator, with hafnium as SPMC
485 """
486 FVP_PREBUILT_SECURE_DTS = os.path.join(
487 HF_ROOT, "test", "vmapi", "fvp-base-spmc.dts")
488 HFTEST_CMD_FILE = os.path.join("/tmp/", "hftest_cmds")
489
490 def __init__(self, args):
491 if args.partitions is None or args.partitions["SPs"] is None:
J-Alves10446d82021-04-26 11:52:57 +0100492 raise Exception("Need to provide SPs in partitions_json")
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100493 super().__init__(args)
494
495 @property
496 def CPU_START_ADDRESS(self):
497 return "0x04010000"
498
499 @property
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100500 def FVP_PREBUILT_BL31(self):
501 return os.path.join(FVP_PREBUILT_TFA_ROOT, "bl31_spmd.bin")
502
503 @property
J-Alves38223dd2021-04-20 17:31:48 +0100504 def SPMC_ADDRESS(self):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100505 return "0x6000000"
506
J-Alves38223dd2021-04-20 17:31:48 +0100507 @property
508 def SPMC_DTB_ADDRESS(self):
509 return "0x0403f000"
510
J-Alves8cc7dbb2021-04-16 10:38:48 +0100511 def gen_dts(self, dt, test_args):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100512 """Create a DeviceTree source which will be compiled into a DTB and
513 passed to FVP for a test run."""
514 to_append = self.get_manifests_from_json(self.args.partitions["SPs"])
J-Alves8cc7dbb2021-04-16 10:38:48 +0100515 write_file(dt.dts, read_file(FvpDriverSPMC.FVP_PREBUILT_SECURE_DTS))
516 append_file(dt.dts, to_append)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100517
518 def gen_fvp_args(
J-Alves8cc7dbb2021-04-16 10:38:48 +0100519 self, is_long_running, uart0_log_path, uart1_log_path, dt,
J-Alves18a25f92021-05-04 17:47:41 +0100520 call_super = True, secure_ctrl = True):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100521 """Generate command line arguments for FVP."""
J-Alves38223dd2021-04-20 17:31:48 +0100522 common_args = (self, is_long_running, uart0_log_path, uart1_log_path, dt.dtb)
523 fvp_args = FvpDriver.gen_fvp_args(*common_args) if call_super else []
J-Alves8cc7dbb2021-04-16 10:38:48 +0100524
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100525 fvp_args += [
J-Alves38223dd2021-04-20 17:31:48 +0100526 "--data", f"cluster0.cpu0={dt.dtb}@{self.SPMC_DTB_ADDRESS}",
527 "--data", f"cluster0.cpu0={self.args.spmc}@{self.SPMC_ADDRESS}",
J-Alves8cc7dbb2021-04-16 10:38:48 +0100528 ]
529
J-Alves18a25f92021-05-04 17:47:41 +0100530 if secure_ctrl:
531 fvp_args += [
532 "-C", f"bp.pl011_uart0.in_file={FvpDriverSPMC.HFTEST_CMD_FILE}",
533 "-C", f"bp.pl011_uart0.shutdown_tag=\"{HFTEST_CTRL_FINISHED}\"",
534 ]
535
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100536 img_ldadd = self.get_img_and_ldadd(self.args.partitions["SPs"])
537 for img, ldadd in img_ldadd:
538 fvp_args += ["--data", f"cluster0.cpu0={img}@{hex(ldadd)}"]
539
540 return fvp_args
541
542 def run(self, run_name, test_args, is_long_running):
543 with open(FvpDriverSPMC.HFTEST_CMD_FILE, "w+") as f:
544 vm_args = join_if_not_None(self.args.vm_args, test_args)
545 f.write(f"{vm_args}\n")
546 return super().run(run_name, test_args, is_long_running)
547
548 def finish(self):
549 """Clean up after running tests."""
550 os.remove(FvpDriverSPMC.HFTEST_CMD_FILE)
David Brazdil2df24082019-09-05 11:55:08 +0100551
J-Alves38223dd2021-04-20 17:31:48 +0100552class FvpDriverBothWorlds(FvpDriverHypervisor, FvpDriverSPMC):
553 def __init__(self, args):
554 FvpDriverHypervisor.__init__(self, args)
555 FvpDriverSPMC.__init__(self, args)
556
557 @property
558 def CPU_START_ADDRESS(self):
559 return str(0x04010000)
560
561 @property
562 def FVP_PREBUILT_BL31(self):
563 return str(os.path.join(FVP_PREBUILT_TFA_ROOT, "bl31_spmd.bin"))
564
565 def create_dt(self, run_name):
566 dt = dict()
567 dt["hypervisor"] = FvpDriver.create_dt(self, run_name + "_hypervisor")
568 dt["spmc"] = FvpDriver.create_dt(self, run_name + "_spmc")
569 return dt
570
571 @property
572 def HYPERVISOR_ADDRESS(self):
573 return "0x88000000"
574
575 @property
576 def HYPERVISOR_DTB_ADDRESS(self):
Olivier Deprezefd3c672022-02-04 09:40:36 +0100577 return "0x82000000"
J-Alves38223dd2021-04-20 17:31:48 +0100578
579 def compile_dt(self, run_state, dt):
580 FvpDriver.compile_dt(self, run_state, dt["hypervisor"])
581 FvpDriver.compile_dt(self, run_state, dt["spmc"])
582
583 def gen_dts(self, dt, test_args):
584 FvpDriverHypervisor.gen_dts(self, dt["hypervisor"], test_args)
585 FvpDriverSPMC.gen_dts(self, dt["spmc"], test_args)
586
J-Alves8d9fbb92021-12-13 17:28:15 +0000587 def gen_fvp_args(self, is_long_running, uart0_log_path, uart1_log_path, dt):
J-Alves38223dd2021-04-20 17:31:48 +0100588 """Generate command line arguments for FVP."""
J-Alves8d9fbb92021-12-13 17:28:15 +0000589 common_args = (self, is_long_running, uart0_log_path, uart1_log_path)
J-Alves18a25f92021-05-04 17:47:41 +0100590 fvp_args = FvpDriverHypervisor.gen_fvp_args(*common_args, dt["hypervisor"])
591 fvp_args += FvpDriverSPMC.gen_fvp_args(*common_args, dt["spmc"], False,
592 False)
J-Alves8d9fbb92021-12-13 17:28:15 +0000593 return fvp_args
J-Alves18a25f92021-05-04 17:47:41 +0100594
595 def run(self, run_name, test_args, is_long_running):
J-Alves8d9fbb92021-12-13 17:28:15 +0000596 return FvpDriver.run(self, run_name, test_args, is_long_running)
J-Alves38223dd2021-04-20 17:31:48 +0100597
598 def finish(self):
599 """Clean up after running tests."""
J-Alves8d9fbb92021-12-13 17:28:15 +0000600 FvpDriver.finish(self)
J-Alves38223dd2021-04-20 17:31:48 +0100601
David Brazdil17e76652020-01-29 14:44:19 +0000602class SerialDriver(Driver):
603 """Driver which communicates with a device over the serial port."""
604
David Brazdil9d4ed962020-02-06 17:23:48 +0000605 def __init__(self, args, tty_file, baudrate, init_wait):
David Brazdil17e76652020-01-29 14:44:19 +0000606 Driver.__init__(self, args)
David Brazdil9d4ed962020-02-06 17:23:48 +0000607 self.tty_file = tty_file
608 self.baudrate = baudrate
David Brazdil4f9cf9a2020-02-06 17:34:44 +0000609 self.pyserial = importlib.import_module("serial")
David Brazdild8013f92020-02-03 16:40:25 +0000610
David Brazdil9d4ed962020-02-06 17:23:48 +0000611 if init_wait:
David Brazdild8013f92020-02-03 16:40:25 +0000612 input("Press ENTER and then reset the device...")
David Brazdil17e76652020-01-29 14:44:19 +0000613
David Brazdil9d4ed962020-02-06 17:23:48 +0000614 def connect(self):
David Brazdil4f9cf9a2020-02-06 17:34:44 +0000615 return self.pyserial.Serial(self.tty_file, self.baudrate, timeout=10)
David Brazdil9d4ed962020-02-06 17:23:48 +0000616
David Brazdil17e76652020-01-29 14:44:19 +0000617 def run(self, run_name, test_args, is_long_running):
618 """Communicate `test_args` to the device over the serial port."""
619 run_state = self.start_run(run_name)
620
David Brazdil9d4ed962020-02-06 17:23:48 +0000621 with self.connect() as ser:
David Brazdil17e76652020-01-29 14:44:19 +0000622 with open(run_state.log_path, "a") as f:
623 while True:
624 # Read one line from the serial port.
625 line = ser.readline().decode('utf-8')
626 if len(line) == 0:
627 # Timeout
628 run_state.set_ret_code(124)
629 input("Timeout. " +
630 "Press ENTER and then reset the device...")
631 break
632 # Write the line to the log file.
633 f.write(line)
634 if HFTEST_CTRL_GET_COMMAND_LINE in line:
635 # Device is waiting for `test_args`.
636 ser.write(test_args.encode('ascii'))
637 ser.write(b'\r')
638 elif HFTEST_CTRL_FINISHED in line:
639 # Device has finished running this test and will reboot.
640 break
J-Alves18a25f92021-05-04 17:47:41 +0100641
David Brazdil17e76652020-01-29 14:44:19 +0000642 return self.finish_run(run_state)
643
David Brazdil94fd1e92020-02-03 16:45:20 +0000644 def finish(self):
645 """Clean up after running tests."""
David Brazdil9d4ed962020-02-06 17:23:48 +0000646 with self.connect() as ser:
David Brazdil94fd1e92020-02-03 16:45:20 +0000647 while True:
648 line = ser.readline().decode('utf-8')
649 if len(line) == 0:
650 input("Timeout. Press ENTER and then reset the device...")
651 elif HFTEST_CTRL_GET_COMMAND_LINE in line:
652 # Device is waiting for a command. Instruct it to exit
653 # the test environment.
654 ser.write("exit".encode('ascii'))
655 ser.write(b'\r')
656 break
657
David Brazdil2df24082019-09-05 11:55:08 +0100658# Tuple used to return information about the results of running a set of tests.
659TestRunnerResult = collections.namedtuple("TestRunnerResult", [
660 "tests_run",
661 "tests_failed",
Andrew Walbranf9463922020-06-05 16:44:42 +0100662 "tests_skipped",
David Brazdil2df24082019-09-05 11:55:08 +0100663 ])
664
David Brazdil2df24082019-09-05 11:55:08 +0100665class TestRunner:
666 """Class which communicates with a test platform to obtain a list of
667 available tests and driving their execution."""
668
J-Alves8cc7dbb2021-04-16 10:38:48 +0100669 def __init__(self, artifacts, driver, test_set_up, suite_regex, test_regex,
Andrew Walbrane1fa70b2020-05-28 11:30:11 +0100670 skip_long_running_tests, force_long_running):
David Brazdil2df24082019-09-05 11:55:08 +0100671 self.artifacts = artifacts
672 self.driver = driver
J-Alves8cc7dbb2021-04-16 10:38:48 +0100673 self.test_set_up = test_set_up
David Brazdil3cc24aa2019-09-27 10:24:41 +0100674 self.skip_long_running_tests = skip_long_running_tests
Andrew Walbrane1fa70b2020-05-28 11:30:11 +0100675 self.force_long_running = force_long_running
David Brazdil2df24082019-09-05 11:55:08 +0100676
677 self.suite_re = re.compile(suite_regex or ".*")
678 self.test_re = re.compile(test_regex or ".*")
679
680 def extract_hftest_lines(self, raw):
681 """Extract hftest-specific lines from a raw output from an invocation
682 of the test platform."""
683 lines = []
J-Alves07be7bb2021-04-13 11:09:12 +0100684 lines_to_process = raw.splitlines()
685
686 try:
687 # If logs have logs of more than one VM, the loop below to extract
688 # lines won't work. Thus, extracting between starting and ending
689 # logs: HFTEST_CTRL_GET_COMMAND_LINE and HFTEST_CTRL_FINISHED.
690 hftest_start = lines_to_process.index(HFTEST_CTRL_GET_COMMAND_LINE) + 1
691 hftest_end = lines_to_process.index(HFTEST_CTRL_FINISHED)
692 except ValueError:
693 hftest_start = 0
694 hftest_end = len(lines_to_process)
695
696 lines_to_process = lines_to_process[hftest_start : hftest_end]
697
698 for line in lines_to_process:
J-Alves3dbb8562020-12-01 10:45:37 +0000699 match = re.search(f"^VM \d+: ", line)
700 if match is not None:
701 line = line[match.end():]
David Brazdil2df24082019-09-05 11:55:08 +0100702 if line.startswith(HFTEST_LOG_PREFIX):
703 lines.append(line[len(HFTEST_LOG_PREFIX):])
704 return lines
705
706 def get_test_json(self):
707 """Invoke the test platform and request a JSON of available test and
708 test suites."""
Andrew Walbrane1fa70b2020-05-28 11:30:11 +0100709 out = self.driver.run("json", "json", self.force_long_running)
David Brazdil2df24082019-09-05 11:55:08 +0100710 hf_out = "\n".join(self.extract_hftest_lines(out))
711 try:
712 return json.loads(hf_out)
713 except ValueError as e:
714 print(out)
715 raise e
716
717 def collect_results(self, fn, it, xml_node):
718 """Run `fn` on every entry in `it` and collect their TestRunnerResults.
719 Insert "tests" and "failures" nodes to `xml_node`."""
720 tests_run = 0
721 tests_failed = 0
Andrew Walbranf9463922020-06-05 16:44:42 +0100722 tests_skipped = 0
Andrew Walbran42bf2842020-06-05 18:50:19 +0100723 start_time = time.perf_counter()
David Brazdil2df24082019-09-05 11:55:08 +0100724 for i in it:
725 sub_result = fn(i)
726 assert(sub_result.tests_run >= sub_result.tests_failed)
727 tests_run += sub_result.tests_run
728 tests_failed += sub_result.tests_failed
Andrew Walbranf9463922020-06-05 16:44:42 +0100729 tests_skipped += sub_result.tests_skipped
Andrew Walbran42bf2842020-06-05 18:50:19 +0100730 elapsed_time = time.perf_counter() - start_time
David Brazdil2df24082019-09-05 11:55:08 +0100731
Andrew Walbranf9463922020-06-05 16:44:42 +0100732 xml_node.set("tests", str(tests_run + tests_skipped))
David Brazdil2df24082019-09-05 11:55:08 +0100733 xml_node.set("failures", str(tests_failed))
Andrew Walbranf9463922020-06-05 16:44:42 +0100734 xml_node.set("skipped", str(tests_skipped))
Andrew Walbran42bf2842020-06-05 18:50:19 +0100735 xml_node.set("time", str(elapsed_time))
Andrew Walbranf9463922020-06-05 16:44:42 +0100736 return TestRunnerResult(tests_run, tests_failed, tests_skipped)
David Brazdil2df24082019-09-05 11:55:08 +0100737
738 def is_passed_test(self, test_out):
739 """Parse the output of a test and return True if it passed."""
740 return \
741 len(test_out) > 0 and \
742 test_out[-1] == HFTEST_LOG_FINISHED and \
743 not any(l.startswith(HFTEST_LOG_FAILURE_PREFIX) for l in test_out)
744
Andrew Walbranf9463922020-06-05 16:44:42 +0100745 def get_failure_message(self, test_out):
746 """Parse the output of a test and return the message of the first
747 assertion failure."""
748 for i, line in enumerate(test_out):
749 if line.startswith(HFTEST_LOG_FAILURE_PREFIX) and i + 1 < len(test_out):
750 # The assertion message is on the line after the 'Failure:'
751 return test_out[i + 1].strip()
752
753 return None
754
Fuad Tabba36c8c2b2019-11-04 16:55:32 +0000755 def get_log_name(self, suite, test):
756 """Returns a string with a generated log name for the test."""
757 log_name = ""
758
759 cpu = self.driver.args.cpu
760 if cpu:
761 log_name += cpu + "."
762
763 log_name += suite["name"] + "." + test["name"]
764
765 return log_name
766
David Brazdil2df24082019-09-05 11:55:08 +0100767 def run_test(self, suite, test, suite_xml):
768 """Invoke the test platform and request to run a given `test` in given
769 `suite`. Create a new XML node with results under `suite_xml`.
770 Test only invoked if it matches the regex given to constructor."""
David Brazdil3cc24aa2019-09-27 10:24:41 +0100771 if not self.test_re.match(test["name"]):
Andrew Walbranf9463922020-06-05 16:44:42 +0100772 return TestRunnerResult(tests_run=0, tests_failed=0, tests_skipped=0)
David Brazdil2df24082019-09-05 11:55:08 +0100773
774 test_xml = ET.SubElement(suite_xml, "testcase")
David Brazdil3cc24aa2019-09-27 10:24:41 +0100775 test_xml.set("name", test["name"])
776 test_xml.set("classname", suite["name"])
Andrew Walbranf9463922020-06-05 16:44:42 +0100777
778 if self.skip_long_running_tests and test["is_long_running"]:
779 print(" SKIP", test["name"])
780 test_xml.set("status", "notrun")
781 skipped_xml = ET.SubElement(test_xml, "skipped")
782 skipped_xml.set("message", "Long running")
783 return TestRunnerResult(tests_run=0, tests_failed=0, tests_skipped=1)
784
785 print(" RUN", test["name"])
786 log_name = self.get_log_name(suite, test)
787
David Brazdil2df24082019-09-05 11:55:08 +0100788 test_xml.set("status", "run")
789
Andrew Walbran42bf2842020-06-05 18:50:19 +0100790 start_time = time.perf_counter()
Andrew Walbranf9463922020-06-05 16:44:42 +0100791 out = self.driver.run(
David Brazdil3cc24aa2019-09-27 10:24:41 +0100792 log_name, "run {} {}".format(suite["name"], test["name"]),
Andrew Walbranf9463922020-06-05 16:44:42 +0100793 test["is_long_running"] or self.force_long_running)
794 hftest_out = self.extract_hftest_lines(out)
Andrew Walbran42bf2842020-06-05 18:50:19 +0100795 elapsed_time = time.perf_counter() - start_time
796
797 test_xml.set("time", str(elapsed_time))
David Brazdil2df24082019-09-05 11:55:08 +0100798
Andrew Walbranf9463922020-06-05 16:44:42 +0100799 system_out_xml = ET.SubElement(test_xml, "system-out")
800 system_out_xml.text = out
801
802 if self.is_passed_test(hftest_out):
David Brazdil2df24082019-09-05 11:55:08 +0100803 print(" PASS")
Andrew Walbranf9463922020-06-05 16:44:42 +0100804 return TestRunnerResult(tests_run=1, tests_failed=0, tests_skipped=0)
David Brazdil2df24082019-09-05 11:55:08 +0100805 else:
David Brazdil623b6812019-09-09 11:41:08 +0100806 print("[x] FAIL --", self.driver.get_run_log(log_name))
David Brazdil2df24082019-09-05 11:55:08 +0100807 failure_xml = ET.SubElement(test_xml, "failure")
Andrew Walbranf9463922020-06-05 16:44:42 +0100808 failure_message = self.get_failure_message(hftest_out) or "Test failed"
809 failure_xml.set("message", failure_message)
810 failure_xml.text = '\n'.join(hftest_out)
811 return TestRunnerResult(tests_run=1, tests_failed=1, tests_skipped=0)
David Brazdil2df24082019-09-05 11:55:08 +0100812
813 def run_suite(self, suite, xml):
814 """Invoke the test platform and request to run all matching tests in
815 `suite`. Create new XML nodes with results under `xml`.
816 Suite skipped if it does not match the regex given to constructor."""
817 if not self.suite_re.match(suite["name"]):
Andrew Walbranf9463922020-06-05 16:44:42 +0100818 return TestRunnerResult(tests_run=0, tests_failed=0, tests_skipped=0)
David Brazdil2df24082019-09-05 11:55:08 +0100819
820 print(" SUITE", suite["name"])
821 suite_xml = ET.SubElement(xml, "testsuite")
822 suite_xml.set("name", suite["name"])
Andrew Walbran16ae62e2020-06-05 18:27:46 +0100823 properties_xml = ET.SubElement(suite_xml, "properties")
824
825 property_xml = ET.SubElement(properties_xml, "property")
826 property_xml.set("name", "driver")
827 property_xml.set("value", type(self.driver).__name__)
828
829 if self.driver.args.cpu:
830 property_xml = ET.SubElement(properties_xml, "property")
831 property_xml.set("name", "cpu")
832 property_xml.set("value", self.driver.args.cpu)
David Brazdil2df24082019-09-05 11:55:08 +0100833
834 return self.collect_results(
835 lambda test: self.run_test(suite, test, suite_xml),
836 suite["tests"],
837 suite_xml)
838
839 def run_tests(self):
840 """Run all suites and tests matching regexes given to constructor.
841 Write results to sponge log XML. Return the number of run and failed
842 tests."""
843
844 test_spec = self.get_test_json()
845 timestamp = datetime.datetime.now().replace(microsecond=0).isoformat()
846
847 xml = ET.Element("testsuites")
J-Alves8cc7dbb2021-04-16 10:38:48 +0100848 xml.set("name", self.test_set_up)
David Brazdil2df24082019-09-05 11:55:08 +0100849 xml.set("timestamp", timestamp)
850
851 result = self.collect_results(
852 lambda suite: self.run_suite(suite, xml),
853 test_spec["suites"],
854 xml)
855
856 # Write XML to file.
David Brazdilee5e25d2020-01-24 14:17:45 +0000857 ET.ElementTree(xml).write(self.artifacts.sponge_xml_path,
858 encoding='utf-8', xml_declaration=True)
David Brazdil2df24082019-09-05 11:55:08 +0100859
860 if result.tests_failed > 0:
861 print("[x] FAIL:", result.tests_failed, "of", result.tests_run,
862 "tests failed")
863 elif result.tests_run > 0:
864 print(" PASS: all", result.tests_run, "tests passed")
865
David Brazdil94fd1e92020-02-03 16:45:20 +0000866 # Let the driver clean up.
867 self.driver.finish()
868
David Brazdil2df24082019-09-05 11:55:08 +0100869 return result
Andrew Scullbc7189d2018-08-14 09:35:13 +0100870
Andrew Scullbc7189d2018-08-14 09:35:13 +0100871def Main():
872 parser = argparse.ArgumentParser()
J-Alves8cc7dbb2021-04-16 10:38:48 +0100873 parser.add_argument("--hypervisor")
874 parser.add_argument("--spmc")
Andrew Scull23e93a82018-10-26 14:56:04 +0100875 parser.add_argument("--log", required=True)
Andrew Walbran7559fcf2019-05-09 17:11:20 +0100876 parser.add_argument("--out_initrd")
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100877 parser.add_argument("--out_partitions")
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000878 parser.add_argument("--initrd")
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100879 parser.add_argument("--partitions_json")
Andrew Scullbc7189d2018-08-14 09:35:13 +0100880 parser.add_argument("--suite")
881 parser.add_argument("--test")
Andrew Walbranbc342d42019-02-05 16:56:02 +0000882 parser.add_argument("--vm_args")
David Brazdil17e76652020-01-29 14:44:19 +0000883 parser.add_argument("--driver", default="qemu")
884 parser.add_argument("--serial-dev", default="/dev/ttyUSB0")
885 parser.add_argument("--serial-baudrate", type=int, default=115200)
David Brazdild8013f92020-02-03 16:40:25 +0000886 parser.add_argument("--serial-no-init-wait", action="store_true")
David Brazdil3cc24aa2019-09-27 10:24:41 +0100887 parser.add_argument("--skip-long-running-tests", action="store_true")
Andrew Walbrane1fa70b2020-05-28 11:30:11 +0100888 parser.add_argument("--force-long-running", action="store_true")
Fuad Tabba36c8c2b2019-11-04 16:55:32 +0000889 parser.add_argument("--cpu",
890 help="Selects the CPU configuration for the run environment.")
Andrew Walbranf636b842020-01-10 11:46:12 +0000891 parser.add_argument("--tfa", action="store_true")
Andrew Scullbc7189d2018-08-14 09:35:13 +0100892 args = parser.parse_args()
David Brazdil2df24082019-09-05 11:55:08 +0100893
J-Alves8cc7dbb2021-04-16 10:38:48 +0100894 # Create class which will manage all test artifacts.
895 if args.hypervisor and args.spmc:
896 test_set_up = "hypervisor_and_spmc"
897 elif args.hypervisor:
898 test_set_up = "hypervisor"
899 elif args.spmc:
900 test_set_up = "spmc"
901 else:
902 raise Exception("No Hafnium image provided!\n")
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100903
J-Alves8cc7dbb2021-04-16 10:38:48 +0100904 initrd = None
905 if args.hypervisor and args.initrd:
David Brazdil0dbb41f2019-09-09 18:03:35 +0100906 initrd_dir = os.path.join(args.out_initrd, "obj", args.initrd)
907 initrd = os.path.join(initrd_dir, "initrd.img")
J-Alves8cc7dbb2021-04-16 10:38:48 +0100908 test_set_up += "_" + args.initrd
Andrew Walbranbc342d42019-02-05 16:56:02 +0000909 vm_args = args.vm_args or ""
David Brazdil2df24082019-09-05 11:55:08 +0100910
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100911 partitions = None
J-Alves18a25f92021-05-04 17:47:41 +0100912 global_run_name = None
913 if args.driver == "fvp":
914 if args.partitions_json is not None:
915 partitions_dir = os.path.join(
916 args.out_partitions, "obj", args.partitions_json)
917 partitions = json.load(open(partitions_dir, "r"))
918 global_run_name = os.path.basename(args.partitions_json).split(".")[0]
919 elif args.hypervisor:
920 if args.initrd:
921 global_run_name = os.path.basename(args.initrd)
922 else:
923 global_run_name = os.path.basename(args.hypervisor).split(".")[0]
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100924
David Brazdil2df24082019-09-05 11:55:08 +0100925 # Create class which will manage all test artifacts.
J-Alves8cc7dbb2021-04-16 10:38:48 +0100926 log_dir = os.path.join(args.log, test_set_up)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100927 artifacts = ArtifactsManager(log_dir)
David Brazdil2df24082019-09-05 11:55:08 +0100928
929 # Create a driver for the platform we want to test on.
J-Alves8cc7dbb2021-04-16 10:38:48 +0100930 driver_args = DriverArgs(artifacts, args.hypervisor, args.spmc, initrd,
J-Alves18a25f92021-05-04 17:47:41 +0100931 vm_args, args.cpu, partitions, global_run_name)
David Brazdil17e76652020-01-29 14:44:19 +0000932
J-Alves8cc7dbb2021-04-16 10:38:48 +0100933 if args.spmc:
J-Alves38223dd2021-04-20 17:31:48 +0100934 # So far only FVP supports tests for SPMC.
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100935 if args.driver != "fvp":
936 raise Exception("Secure tests can only run with fvp driver")
J-Alves38223dd2021-04-20 17:31:48 +0100937
938 if args.hypervisor:
939 driver = FvpDriverBothWorlds(driver_args)
940 else:
941 driver = FvpDriverSPMC(driver_args)
J-Alves8cc7dbb2021-04-16 10:38:48 +0100942 elif args.hypervisor:
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100943 if args.driver == "qemu":
J-Alves8cc7dbb2021-04-16 10:38:48 +0100944 out = os.path.dirname(args.hypervisor)
945 driver = QemuDriver(driver_args, out, args.tfa)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100946 elif args.driver == "fvp":
947 driver = FvpDriverHypervisor(driver_args)
948 elif args.driver == "serial":
949 driver = SerialDriver(driver_args, args.serial_dev,
950 args.serial_baudrate, not args.serial_no_init_wait)
951 else:
952 raise Exception("Unknown driver name: {}".format(args.driver))
J-Alves8cc7dbb2021-04-16 10:38:48 +0100953 else:
954 raise Exception("No Hafnium image provided!\n")
David Brazdil2df24082019-09-05 11:55:08 +0100955
956 # Create class which will drive test execution.
J-Alves8cc7dbb2021-04-16 10:38:48 +0100957 runner = TestRunner(artifacts, driver, test_set_up, args.suite, args.test,
Andrew Walbrane1fa70b2020-05-28 11:30:11 +0100958 args.skip_long_running_tests, args.force_long_running)
David Brazdil2df24082019-09-05 11:55:08 +0100959
960 # Run tests.
961 runner_result = runner.run_tests()
962
963 # Print error message if no tests were run as this is probably unexpected.
964 # Return suitable error code.
965 if runner_result.tests_run == 0:
Andrew Scullbc7189d2018-08-14 09:35:13 +0100966 print("Error: no tests match")
967 return 10
David Brazdil2df24082019-09-05 11:55:08 +0100968 elif runner_result.tests_failed > 0:
Andrew Scullbc7189d2018-08-14 09:35:13 +0100969 return 1
970 else:
David Brazdil2df24082019-09-05 11:55:08 +0100971 return 0
Andrew Scullbc7189d2018-08-14 09:35:13 +0100972
973if __name__ == "__main__":
974 sys.exit(Main())