blob: 17660ce8226852be1e396f9367540a332e796af9 [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",
43 "Linux64_GCC-6.4", "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",
346 "-C", "bp.secure_memory=0",
347 "-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",
374 "-C", "cluster0.restriction_on_speculative_execution=2",
375 "-C", "cluster1.restriction_on_speculative_execution=2",
376 "-C", "cluster0.restriction_on_speculative_execution_aarch32=2",
377 "-C", "cluster1.restriction_on_speculative_execution_aarch32=2",
Maksims Svecovsce1261f2022-03-04 15:22:58 +0000378 "-C", "cluster0.memory_tagging_support_level=2",
379 "-C", "cluster1.memory_tagging_support_level=2",
380 "-C", "bp.dram_metadata.is_enabled=1",
David Brazdil2df24082019-09-05 11:55:08 +0100381 ]
J-Alves18a25f92021-05-04 17:47:41 +0100382
383 if uart0_log_path and uart1_log_path:
384 fvp_args += [
385 "-C", f"bp.pl011_uart0.out_file={uart0_log_path}",
386 "-C", f"bp.pl011_uart1.out_file={uart1_log_path}",
387 ]
David Brazdil2df24082019-09-05 11:55:08 +0100388 return fvp_args
389
David Brazdil3cc24aa2019-09-27 10:24:41 +0100390 def run(self, run_name, test_args, is_long_running):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100391 """ Run test """
David Brazdil2df24082019-09-05 11:55:08 +0100392 run_state = self.start_run(run_name)
J-Alves8cc7dbb2021-04-16 10:38:48 +0100393 dt = self.create_dt(run_name)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100394 uart0_log_path = self.create_uart_log(run_name, ".uart0.log")
395 uart1_log_path = self.create_uart_log(run_name, ".uart1.log")
David Brazdil2df24082019-09-05 11:55:08 +0100396
David Brazdil0dbb41f2019-09-09 18:03:35 +0100397 try:
J-Alves8cc7dbb2021-04-16 10:38:48 +0100398 self.gen_dts(dt, test_args)
399 self.compile_dt(run_state, dt)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100400 fvp_args = self.gen_fvp_args(is_long_running, uart0_log_path,
J-Alves8cc7dbb2021-04-16 10:38:48 +0100401 uart1_log_path, dt)
David Brazdil0dbb41f2019-09-09 18:03:35 +0100402 self.exec_logged(run_state, fvp_args)
403 except DriverRunException:
404 pass
David Brazdil2df24082019-09-05 11:55:08 +0100405
406 # Append UART0 output to main log.
407 append_file(run_state.log_path, read_file(uart0_log_path))
David Brazdil0dbb41f2019-09-09 18:03:35 +0100408 return self.finish_run(run_state)
David Brazdil2df24082019-09-05 11:55:08 +0100409
David Brazdil94fd1e92020-02-03 16:45:20 +0000410 def finish(self):
411 """Clean up after running tests."""
412 pass
413
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100414class FvpDriverHypervisor(FvpDriver):
415 """
416 Driver which runs tests in Arm FVP emulator, with hafnium as hypervisor
417 """
418 INITRD_START= 0x84000000
Olivier Depreza516f482021-04-30 18:47:59 +0200419 INITRD_END = 0x86000000 #Default value, however may change if initrd in args
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100420
421 def __init__(self, args):
422 self.vms_in_partitions_json = args.partitions and args.partitions["VMs"]
423 super().__init__(args)
424
425 @property
426 def CPU_START_ADDRESS(self):
427 return "0x04020000"
428
429 @property
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100430 def FVP_PREBUILT_BL31(self):
431 return os.path.join(FVP_PREBUILTS_TFA_TRUSTY_ROOT, "bl31.bin")
432
433 @property
J-Alves38223dd2021-04-20 17:31:48 +0100434 def HYPERVISOR_ADDRESS(self):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100435 return "0x80000000"
436
J-Alves38223dd2021-04-20 17:31:48 +0100437 @property
438 def HYPERVISOR_DTB_ADDRESS(self):
439 return "0x82000000"
440
J-Alves8cc7dbb2021-04-16 10:38:48 +0100441 def gen_dts(self, dt, test_args):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100442 """Create a DeviceTree source which will be compiled into a DTB and
443 passed to FVP for a test run."""
444
445 vm_args = join_if_not_None(self.args.vm_args, test_args)
J-Alves8cc7dbb2021-04-16 10:38:48 +0100446 write_file(dt.dts, read_file(FVP_PREBUILT_DTS))
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100447
448 # Write the vm arguments to the partition manifest
449 to_append = f"""
450/ {{
451 chosen {{
452 bootargs = "{vm_args}";
453 stdout-path = "serial0:115200n8";
454 linux,initrd-start = <{self.INITRD_START if self.args.initrd else 0}>;
455 linux,initrd-end = <{self.INITRD_END if self.args.initrd else 0}>;
456 }};
457}};"""
458 if self.vms_in_partitions_json:
459 to_append += self.get_manifests_from_json(self.args.partitions["VMs"])
460
J-Alves8cc7dbb2021-04-16 10:38:48 +0100461 append_file(dt.dts, to_append)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100462
463 def gen_fvp_args(
J-Alves8cc7dbb2021-04-16 10:38:48 +0100464 self, is_long_running, uart0_log_path, uart1_log_path, dt, call_super = True):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100465 """Generate command line arguments for FVP."""
J-Alves38223dd2021-04-20 17:31:48 +0100466 common_args = (self, is_long_running, uart0_log_path, uart1_log_path, dt)
467 fvp_args = FvpDriver.gen_fvp_args(*common_args) if call_super else []
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100468
J-Alves8cc7dbb2021-04-16 10:38:48 +0100469 fvp_args += [
J-Alves38223dd2021-04-20 17:31:48 +0100470 "--data", f"cluster0.cpu0={dt.dtb}@{self.HYPERVISOR_DTB_ADDRESS}",
471 "--data", f"cluster0.cpu0={self.args.hypervisor}@{self.HYPERVISOR_ADDRESS}",
J-Alves8cc7dbb2021-04-16 10:38:48 +0100472 ]
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100473
474 if self.vms_in_partitions_json:
475 img_ldadd = self.get_img_and_ldadd(self.args.partitions["VMs"])
476 for img, ldadd in img_ldadd:
477 fvp_args += ["--data", f"cluster0.cpu0={img}@{hex(ldadd)}"]
478
479 if self.args.initrd:
480 fvp_args += [
481 "--data",
482 f"cluster0.cpu0={self.args.initrd}@{self.INITRD_START}"
483 ]
484 return fvp_args
485
486class FvpDriverSPMC(FvpDriver):
487 """
488 Driver which runs tests in Arm FVP emulator, with hafnium as SPMC
489 """
490 FVP_PREBUILT_SECURE_DTS = os.path.join(
491 HF_ROOT, "test", "vmapi", "fvp-base-spmc.dts")
492 HFTEST_CMD_FILE = os.path.join("/tmp/", "hftest_cmds")
493
494 def __init__(self, args):
495 if args.partitions is None or args.partitions["SPs"] is None:
J-Alves10446d82021-04-26 11:52:57 +0100496 raise Exception("Need to provide SPs in partitions_json")
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100497 super().__init__(args)
498
499 @property
500 def CPU_START_ADDRESS(self):
501 return "0x04010000"
502
503 @property
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100504 def FVP_PREBUILT_BL31(self):
505 return os.path.join(FVP_PREBUILT_TFA_ROOT, "bl31_spmd.bin")
506
507 @property
J-Alves38223dd2021-04-20 17:31:48 +0100508 def SPMC_ADDRESS(self):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100509 return "0x6000000"
510
J-Alves38223dd2021-04-20 17:31:48 +0100511 @property
512 def SPMC_DTB_ADDRESS(self):
513 return "0x0403f000"
514
J-Alves8cc7dbb2021-04-16 10:38:48 +0100515 def gen_dts(self, dt, test_args):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100516 """Create a DeviceTree source which will be compiled into a DTB and
517 passed to FVP for a test run."""
518 to_append = self.get_manifests_from_json(self.args.partitions["SPs"])
J-Alves8cc7dbb2021-04-16 10:38:48 +0100519 write_file(dt.dts, read_file(FvpDriverSPMC.FVP_PREBUILT_SECURE_DTS))
520 append_file(dt.dts, to_append)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100521
522 def gen_fvp_args(
J-Alves8cc7dbb2021-04-16 10:38:48 +0100523 self, is_long_running, uart0_log_path, uart1_log_path, dt,
J-Alves18a25f92021-05-04 17:47:41 +0100524 call_super = True, secure_ctrl = True):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100525 """Generate command line arguments for FVP."""
J-Alves38223dd2021-04-20 17:31:48 +0100526 common_args = (self, is_long_running, uart0_log_path, uart1_log_path, dt.dtb)
527 fvp_args = FvpDriver.gen_fvp_args(*common_args) if call_super else []
J-Alves8cc7dbb2021-04-16 10:38:48 +0100528
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100529 fvp_args += [
J-Alves38223dd2021-04-20 17:31:48 +0100530 "--data", f"cluster0.cpu0={dt.dtb}@{self.SPMC_DTB_ADDRESS}",
531 "--data", f"cluster0.cpu0={self.args.spmc}@{self.SPMC_ADDRESS}",
J-Alves8cc7dbb2021-04-16 10:38:48 +0100532 ]
533
J-Alves18a25f92021-05-04 17:47:41 +0100534 if secure_ctrl:
535 fvp_args += [
536 "-C", f"bp.pl011_uart0.in_file={FvpDriverSPMC.HFTEST_CMD_FILE}",
537 "-C", f"bp.pl011_uart0.shutdown_tag=\"{HFTEST_CTRL_FINISHED}\"",
538 ]
539
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100540 img_ldadd = self.get_img_and_ldadd(self.args.partitions["SPs"])
541 for img, ldadd in img_ldadd:
542 fvp_args += ["--data", f"cluster0.cpu0={img}@{hex(ldadd)}"]
543
544 return fvp_args
545
546 def run(self, run_name, test_args, is_long_running):
547 with open(FvpDriverSPMC.HFTEST_CMD_FILE, "w+") as f:
548 vm_args = join_if_not_None(self.args.vm_args, test_args)
549 f.write(f"{vm_args}\n")
550 return super().run(run_name, test_args, is_long_running)
551
552 def finish(self):
553 """Clean up after running tests."""
554 os.remove(FvpDriverSPMC.HFTEST_CMD_FILE)
David Brazdil2df24082019-09-05 11:55:08 +0100555
J-Alves38223dd2021-04-20 17:31:48 +0100556class FvpDriverBothWorlds(FvpDriverHypervisor, FvpDriverSPMC):
557 def __init__(self, args):
558 FvpDriverHypervisor.__init__(self, args)
559 FvpDriverSPMC.__init__(self, args)
560
561 @property
562 def CPU_START_ADDRESS(self):
563 return str(0x04010000)
564
565 @property
566 def FVP_PREBUILT_BL31(self):
567 return str(os.path.join(FVP_PREBUILT_TFA_ROOT, "bl31_spmd.bin"))
568
569 def create_dt(self, run_name):
570 dt = dict()
571 dt["hypervisor"] = FvpDriver.create_dt(self, run_name + "_hypervisor")
572 dt["spmc"] = FvpDriver.create_dt(self, run_name + "_spmc")
573 return dt
574
575 @property
576 def HYPERVISOR_ADDRESS(self):
577 return "0x88000000"
578
579 @property
580 def HYPERVISOR_DTB_ADDRESS(self):
Olivier Deprezefd3c672022-02-04 09:40:36 +0100581 return "0x82000000"
J-Alves38223dd2021-04-20 17:31:48 +0100582
583 def compile_dt(self, run_state, dt):
584 FvpDriver.compile_dt(self, run_state, dt["hypervisor"])
585 FvpDriver.compile_dt(self, run_state, dt["spmc"])
586
587 def gen_dts(self, dt, test_args):
588 FvpDriverHypervisor.gen_dts(self, dt["hypervisor"], test_args)
589 FvpDriverSPMC.gen_dts(self, dt["spmc"], test_args)
590
J-Alves8d9fbb92021-12-13 17:28:15 +0000591 def gen_fvp_args(self, is_long_running, uart0_log_path, uart1_log_path, dt):
J-Alves38223dd2021-04-20 17:31:48 +0100592 """Generate command line arguments for FVP."""
J-Alves8d9fbb92021-12-13 17:28:15 +0000593 common_args = (self, is_long_running, uart0_log_path, uart1_log_path)
J-Alves18a25f92021-05-04 17:47:41 +0100594 fvp_args = FvpDriverHypervisor.gen_fvp_args(*common_args, dt["hypervisor"])
595 fvp_args += FvpDriverSPMC.gen_fvp_args(*common_args, dt["spmc"], False,
596 False)
J-Alves8d9fbb92021-12-13 17:28:15 +0000597 return fvp_args
J-Alves18a25f92021-05-04 17:47:41 +0100598
599 def run(self, run_name, test_args, is_long_running):
J-Alves8d9fbb92021-12-13 17:28:15 +0000600 return FvpDriver.run(self, run_name, test_args, is_long_running)
J-Alves38223dd2021-04-20 17:31:48 +0100601
602 def finish(self):
603 """Clean up after running tests."""
J-Alves8d9fbb92021-12-13 17:28:15 +0000604 FvpDriver.finish(self)
J-Alves38223dd2021-04-20 17:31:48 +0100605
David Brazdil17e76652020-01-29 14:44:19 +0000606class SerialDriver(Driver):
607 """Driver which communicates with a device over the serial port."""
608
David Brazdil9d4ed962020-02-06 17:23:48 +0000609 def __init__(self, args, tty_file, baudrate, init_wait):
David Brazdil17e76652020-01-29 14:44:19 +0000610 Driver.__init__(self, args)
David Brazdil9d4ed962020-02-06 17:23:48 +0000611 self.tty_file = tty_file
612 self.baudrate = baudrate
David Brazdil4f9cf9a2020-02-06 17:34:44 +0000613 self.pyserial = importlib.import_module("serial")
David Brazdild8013f92020-02-03 16:40:25 +0000614
David Brazdil9d4ed962020-02-06 17:23:48 +0000615 if init_wait:
David Brazdild8013f92020-02-03 16:40:25 +0000616 input("Press ENTER and then reset the device...")
David Brazdil17e76652020-01-29 14:44:19 +0000617
David Brazdil9d4ed962020-02-06 17:23:48 +0000618 def connect(self):
David Brazdil4f9cf9a2020-02-06 17:34:44 +0000619 return self.pyserial.Serial(self.tty_file, self.baudrate, timeout=10)
David Brazdil9d4ed962020-02-06 17:23:48 +0000620
David Brazdil17e76652020-01-29 14:44:19 +0000621 def run(self, run_name, test_args, is_long_running):
622 """Communicate `test_args` to the device over the serial port."""
623 run_state = self.start_run(run_name)
624
David Brazdil9d4ed962020-02-06 17:23:48 +0000625 with self.connect() as ser:
David Brazdil17e76652020-01-29 14:44:19 +0000626 with open(run_state.log_path, "a") as f:
627 while True:
628 # Read one line from the serial port.
629 line = ser.readline().decode('utf-8')
630 if len(line) == 0:
631 # Timeout
632 run_state.set_ret_code(124)
633 input("Timeout. " +
634 "Press ENTER and then reset the device...")
635 break
636 # Write the line to the log file.
637 f.write(line)
638 if HFTEST_CTRL_GET_COMMAND_LINE in line:
639 # Device is waiting for `test_args`.
640 ser.write(test_args.encode('ascii'))
641 ser.write(b'\r')
642 elif HFTEST_CTRL_FINISHED in line:
643 # Device has finished running this test and will reboot.
644 break
J-Alves18a25f92021-05-04 17:47:41 +0100645
David Brazdil17e76652020-01-29 14:44:19 +0000646 return self.finish_run(run_state)
647
David Brazdil94fd1e92020-02-03 16:45:20 +0000648 def finish(self):
649 """Clean up after running tests."""
David Brazdil9d4ed962020-02-06 17:23:48 +0000650 with self.connect() as ser:
David Brazdil94fd1e92020-02-03 16:45:20 +0000651 while True:
652 line = ser.readline().decode('utf-8')
653 if len(line) == 0:
654 input("Timeout. Press ENTER and then reset the device...")
655 elif HFTEST_CTRL_GET_COMMAND_LINE in line:
656 # Device is waiting for a command. Instruct it to exit
657 # the test environment.
658 ser.write("exit".encode('ascii'))
659 ser.write(b'\r')
660 break
661
David Brazdil2df24082019-09-05 11:55:08 +0100662# Tuple used to return information about the results of running a set of tests.
663TestRunnerResult = collections.namedtuple("TestRunnerResult", [
664 "tests_run",
665 "tests_failed",
Andrew Walbranf9463922020-06-05 16:44:42 +0100666 "tests_skipped",
David Brazdil2df24082019-09-05 11:55:08 +0100667 ])
668
David Brazdil2df24082019-09-05 11:55:08 +0100669class TestRunner:
670 """Class which communicates with a test platform to obtain a list of
671 available tests and driving their execution."""
672
J-Alves8cc7dbb2021-04-16 10:38:48 +0100673 def __init__(self, artifacts, driver, test_set_up, suite_regex, test_regex,
Andrew Walbrane1fa70b2020-05-28 11:30:11 +0100674 skip_long_running_tests, force_long_running):
David Brazdil2df24082019-09-05 11:55:08 +0100675 self.artifacts = artifacts
676 self.driver = driver
J-Alves8cc7dbb2021-04-16 10:38:48 +0100677 self.test_set_up = test_set_up
David Brazdil3cc24aa2019-09-27 10:24:41 +0100678 self.skip_long_running_tests = skip_long_running_tests
Andrew Walbrane1fa70b2020-05-28 11:30:11 +0100679 self.force_long_running = force_long_running
David Brazdil2df24082019-09-05 11:55:08 +0100680
681 self.suite_re = re.compile(suite_regex or ".*")
682 self.test_re = re.compile(test_regex or ".*")
683
684 def extract_hftest_lines(self, raw):
685 """Extract hftest-specific lines from a raw output from an invocation
686 of the test platform."""
687 lines = []
J-Alves07be7bb2021-04-13 11:09:12 +0100688 lines_to_process = raw.splitlines()
689
690 try:
691 # If logs have logs of more than one VM, the loop below to extract
692 # lines won't work. Thus, extracting between starting and ending
693 # logs: HFTEST_CTRL_GET_COMMAND_LINE and HFTEST_CTRL_FINISHED.
694 hftest_start = lines_to_process.index(HFTEST_CTRL_GET_COMMAND_LINE) + 1
695 hftest_end = lines_to_process.index(HFTEST_CTRL_FINISHED)
696 except ValueError:
697 hftest_start = 0
698 hftest_end = len(lines_to_process)
699
700 lines_to_process = lines_to_process[hftest_start : hftest_end]
701
702 for line in lines_to_process:
J-Alves3dbb8562020-12-01 10:45:37 +0000703 match = re.search(f"^VM \d+: ", line)
704 if match is not None:
705 line = line[match.end():]
David Brazdil2df24082019-09-05 11:55:08 +0100706 if line.startswith(HFTEST_LOG_PREFIX):
707 lines.append(line[len(HFTEST_LOG_PREFIX):])
708 return lines
709
710 def get_test_json(self):
711 """Invoke the test platform and request a JSON of available test and
712 test suites."""
Andrew Walbrane1fa70b2020-05-28 11:30:11 +0100713 out = self.driver.run("json", "json", self.force_long_running)
David Brazdil2df24082019-09-05 11:55:08 +0100714 hf_out = "\n".join(self.extract_hftest_lines(out))
715 try:
716 return json.loads(hf_out)
717 except ValueError as e:
718 print(out)
719 raise e
720
721 def collect_results(self, fn, it, xml_node):
722 """Run `fn` on every entry in `it` and collect their TestRunnerResults.
723 Insert "tests" and "failures" nodes to `xml_node`."""
724 tests_run = 0
725 tests_failed = 0
Andrew Walbranf9463922020-06-05 16:44:42 +0100726 tests_skipped = 0
Andrew Walbran42bf2842020-06-05 18:50:19 +0100727 start_time = time.perf_counter()
David Brazdil2df24082019-09-05 11:55:08 +0100728 for i in it:
729 sub_result = fn(i)
730 assert(sub_result.tests_run >= sub_result.tests_failed)
731 tests_run += sub_result.tests_run
732 tests_failed += sub_result.tests_failed
Andrew Walbranf9463922020-06-05 16:44:42 +0100733 tests_skipped += sub_result.tests_skipped
Andrew Walbran42bf2842020-06-05 18:50:19 +0100734 elapsed_time = time.perf_counter() - start_time
David Brazdil2df24082019-09-05 11:55:08 +0100735
Andrew Walbranf9463922020-06-05 16:44:42 +0100736 xml_node.set("tests", str(tests_run + tests_skipped))
David Brazdil2df24082019-09-05 11:55:08 +0100737 xml_node.set("failures", str(tests_failed))
Andrew Walbranf9463922020-06-05 16:44:42 +0100738 xml_node.set("skipped", str(tests_skipped))
Andrew Walbran42bf2842020-06-05 18:50:19 +0100739 xml_node.set("time", str(elapsed_time))
Andrew Walbranf9463922020-06-05 16:44:42 +0100740 return TestRunnerResult(tests_run, tests_failed, tests_skipped)
David Brazdil2df24082019-09-05 11:55:08 +0100741
742 def is_passed_test(self, test_out):
743 """Parse the output of a test and return True if it passed."""
744 return \
745 len(test_out) > 0 and \
746 test_out[-1] == HFTEST_LOG_FINISHED and \
747 not any(l.startswith(HFTEST_LOG_FAILURE_PREFIX) for l in test_out)
748
Andrew Walbranf9463922020-06-05 16:44:42 +0100749 def get_failure_message(self, test_out):
750 """Parse the output of a test and return the message of the first
751 assertion failure."""
752 for i, line in enumerate(test_out):
753 if line.startswith(HFTEST_LOG_FAILURE_PREFIX) and i + 1 < len(test_out):
754 # The assertion message is on the line after the 'Failure:'
755 return test_out[i + 1].strip()
756
757 return None
758
Fuad Tabba36c8c2b2019-11-04 16:55:32 +0000759 def get_log_name(self, suite, test):
760 """Returns a string with a generated log name for the test."""
761 log_name = ""
762
763 cpu = self.driver.args.cpu
764 if cpu:
765 log_name += cpu + "."
766
767 log_name += suite["name"] + "." + test["name"]
768
769 return log_name
770
David Brazdil2df24082019-09-05 11:55:08 +0100771 def run_test(self, suite, test, suite_xml):
772 """Invoke the test platform and request to run a given `test` in given
773 `suite`. Create a new XML node with results under `suite_xml`.
774 Test only invoked if it matches the regex given to constructor."""
David Brazdil3cc24aa2019-09-27 10:24:41 +0100775 if not self.test_re.match(test["name"]):
Andrew Walbranf9463922020-06-05 16:44:42 +0100776 return TestRunnerResult(tests_run=0, tests_failed=0, tests_skipped=0)
David Brazdil2df24082019-09-05 11:55:08 +0100777
778 test_xml = ET.SubElement(suite_xml, "testcase")
David Brazdil3cc24aa2019-09-27 10:24:41 +0100779 test_xml.set("name", test["name"])
780 test_xml.set("classname", suite["name"])
Andrew Walbranf9463922020-06-05 16:44:42 +0100781
782 if self.skip_long_running_tests and test["is_long_running"]:
783 print(" SKIP", test["name"])
784 test_xml.set("status", "notrun")
785 skipped_xml = ET.SubElement(test_xml, "skipped")
786 skipped_xml.set("message", "Long running")
787 return TestRunnerResult(tests_run=0, tests_failed=0, tests_skipped=1)
788
789 print(" RUN", test["name"])
790 log_name = self.get_log_name(suite, test)
791
David Brazdil2df24082019-09-05 11:55:08 +0100792 test_xml.set("status", "run")
793
Andrew Walbran42bf2842020-06-05 18:50:19 +0100794 start_time = time.perf_counter()
Andrew Walbranf9463922020-06-05 16:44:42 +0100795 out = self.driver.run(
David Brazdil3cc24aa2019-09-27 10:24:41 +0100796 log_name, "run {} {}".format(suite["name"], test["name"]),
Andrew Walbranf9463922020-06-05 16:44:42 +0100797 test["is_long_running"] or self.force_long_running)
798 hftest_out = self.extract_hftest_lines(out)
Andrew Walbran42bf2842020-06-05 18:50:19 +0100799 elapsed_time = time.perf_counter() - start_time
800
801 test_xml.set("time", str(elapsed_time))
David Brazdil2df24082019-09-05 11:55:08 +0100802
Andrew Walbranf9463922020-06-05 16:44:42 +0100803 system_out_xml = ET.SubElement(test_xml, "system-out")
804 system_out_xml.text = out
805
806 if self.is_passed_test(hftest_out):
David Brazdil2df24082019-09-05 11:55:08 +0100807 print(" PASS")
Andrew Walbranf9463922020-06-05 16:44:42 +0100808 return TestRunnerResult(tests_run=1, tests_failed=0, tests_skipped=0)
David Brazdil2df24082019-09-05 11:55:08 +0100809 else:
David Brazdil623b6812019-09-09 11:41:08 +0100810 print("[x] FAIL --", self.driver.get_run_log(log_name))
David Brazdil2df24082019-09-05 11:55:08 +0100811 failure_xml = ET.SubElement(test_xml, "failure")
Andrew Walbranf9463922020-06-05 16:44:42 +0100812 failure_message = self.get_failure_message(hftest_out) or "Test failed"
813 failure_xml.set("message", failure_message)
814 failure_xml.text = '\n'.join(hftest_out)
815 return TestRunnerResult(tests_run=1, tests_failed=1, tests_skipped=0)
David Brazdil2df24082019-09-05 11:55:08 +0100816
817 def run_suite(self, suite, xml):
818 """Invoke the test platform and request to run all matching tests in
819 `suite`. Create new XML nodes with results under `xml`.
820 Suite skipped if it does not match the regex given to constructor."""
821 if not self.suite_re.match(suite["name"]):
Andrew Walbranf9463922020-06-05 16:44:42 +0100822 return TestRunnerResult(tests_run=0, tests_failed=0, tests_skipped=0)
David Brazdil2df24082019-09-05 11:55:08 +0100823
824 print(" SUITE", suite["name"])
825 suite_xml = ET.SubElement(xml, "testsuite")
826 suite_xml.set("name", suite["name"])
Andrew Walbran16ae62e2020-06-05 18:27:46 +0100827 properties_xml = ET.SubElement(suite_xml, "properties")
828
829 property_xml = ET.SubElement(properties_xml, "property")
830 property_xml.set("name", "driver")
831 property_xml.set("value", type(self.driver).__name__)
832
833 if self.driver.args.cpu:
834 property_xml = ET.SubElement(properties_xml, "property")
835 property_xml.set("name", "cpu")
836 property_xml.set("value", self.driver.args.cpu)
David Brazdil2df24082019-09-05 11:55:08 +0100837
838 return self.collect_results(
839 lambda test: self.run_test(suite, test, suite_xml),
840 suite["tests"],
841 suite_xml)
842
843 def run_tests(self):
844 """Run all suites and tests matching regexes given to constructor.
845 Write results to sponge log XML. Return the number of run and failed
846 tests."""
847
848 test_spec = self.get_test_json()
849 timestamp = datetime.datetime.now().replace(microsecond=0).isoformat()
850
851 xml = ET.Element("testsuites")
J-Alves8cc7dbb2021-04-16 10:38:48 +0100852 xml.set("name", self.test_set_up)
David Brazdil2df24082019-09-05 11:55:08 +0100853 xml.set("timestamp", timestamp)
854
855 result = self.collect_results(
856 lambda suite: self.run_suite(suite, xml),
857 test_spec["suites"],
858 xml)
859
860 # Write XML to file.
David Brazdilee5e25d2020-01-24 14:17:45 +0000861 ET.ElementTree(xml).write(self.artifacts.sponge_xml_path,
862 encoding='utf-8', xml_declaration=True)
David Brazdil2df24082019-09-05 11:55:08 +0100863
864 if result.tests_failed > 0:
865 print("[x] FAIL:", result.tests_failed, "of", result.tests_run,
866 "tests failed")
867 elif result.tests_run > 0:
868 print(" PASS: all", result.tests_run, "tests passed")
869
David Brazdil94fd1e92020-02-03 16:45:20 +0000870 # Let the driver clean up.
871 self.driver.finish()
872
David Brazdil2df24082019-09-05 11:55:08 +0100873 return result
Andrew Scullbc7189d2018-08-14 09:35:13 +0100874
Andrew Scullbc7189d2018-08-14 09:35:13 +0100875def Main():
876 parser = argparse.ArgumentParser()
J-Alves8cc7dbb2021-04-16 10:38:48 +0100877 parser.add_argument("--hypervisor")
878 parser.add_argument("--spmc")
Andrew Scull23e93a82018-10-26 14:56:04 +0100879 parser.add_argument("--log", required=True)
Andrew Walbran7559fcf2019-05-09 17:11:20 +0100880 parser.add_argument("--out_initrd")
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100881 parser.add_argument("--out_partitions")
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000882 parser.add_argument("--initrd")
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100883 parser.add_argument("--partitions_json")
Andrew Scullbc7189d2018-08-14 09:35:13 +0100884 parser.add_argument("--suite")
885 parser.add_argument("--test")
Andrew Walbranbc342d42019-02-05 16:56:02 +0000886 parser.add_argument("--vm_args")
David Brazdil17e76652020-01-29 14:44:19 +0000887 parser.add_argument("--driver", default="qemu")
888 parser.add_argument("--serial-dev", default="/dev/ttyUSB0")
889 parser.add_argument("--serial-baudrate", type=int, default=115200)
David Brazdild8013f92020-02-03 16:40:25 +0000890 parser.add_argument("--serial-no-init-wait", action="store_true")
David Brazdil3cc24aa2019-09-27 10:24:41 +0100891 parser.add_argument("--skip-long-running-tests", action="store_true")
Andrew Walbrane1fa70b2020-05-28 11:30:11 +0100892 parser.add_argument("--force-long-running", action="store_true")
Fuad Tabba36c8c2b2019-11-04 16:55:32 +0000893 parser.add_argument("--cpu",
894 help="Selects the CPU configuration for the run environment.")
Andrew Walbranf636b842020-01-10 11:46:12 +0000895 parser.add_argument("--tfa", action="store_true")
Andrew Scullbc7189d2018-08-14 09:35:13 +0100896 args = parser.parse_args()
David Brazdil2df24082019-09-05 11:55:08 +0100897
J-Alves8cc7dbb2021-04-16 10:38:48 +0100898 # Create class which will manage all test artifacts.
899 if args.hypervisor and args.spmc:
900 test_set_up = "hypervisor_and_spmc"
901 elif args.hypervisor:
902 test_set_up = "hypervisor"
903 elif args.spmc:
904 test_set_up = "spmc"
905 else:
906 raise Exception("No Hafnium image provided!\n")
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100907
J-Alves8cc7dbb2021-04-16 10:38:48 +0100908 initrd = None
909 if args.hypervisor and args.initrd:
David Brazdil0dbb41f2019-09-09 18:03:35 +0100910 initrd_dir = os.path.join(args.out_initrd, "obj", args.initrd)
911 initrd = os.path.join(initrd_dir, "initrd.img")
J-Alves8cc7dbb2021-04-16 10:38:48 +0100912 test_set_up += "_" + args.initrd
Andrew Walbranbc342d42019-02-05 16:56:02 +0000913 vm_args = args.vm_args or ""
David Brazdil2df24082019-09-05 11:55:08 +0100914
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100915 partitions = None
J-Alves18a25f92021-05-04 17:47:41 +0100916 global_run_name = None
917 if args.driver == "fvp":
918 if args.partitions_json is not None:
919 partitions_dir = os.path.join(
920 args.out_partitions, "obj", args.partitions_json)
921 partitions = json.load(open(partitions_dir, "r"))
922 global_run_name = os.path.basename(args.partitions_json).split(".")[0]
923 elif args.hypervisor:
924 if args.initrd:
925 global_run_name = os.path.basename(args.initrd)
926 else:
927 global_run_name = os.path.basename(args.hypervisor).split(".")[0]
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100928
David Brazdil2df24082019-09-05 11:55:08 +0100929 # Create class which will manage all test artifacts.
J-Alves8cc7dbb2021-04-16 10:38:48 +0100930 log_dir = os.path.join(args.log, test_set_up)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100931 artifacts = ArtifactsManager(log_dir)
David Brazdil2df24082019-09-05 11:55:08 +0100932
933 # Create a driver for the platform we want to test on.
J-Alves8cc7dbb2021-04-16 10:38:48 +0100934 driver_args = DriverArgs(artifacts, args.hypervisor, args.spmc, initrd,
J-Alves18a25f92021-05-04 17:47:41 +0100935 vm_args, args.cpu, partitions, global_run_name)
David Brazdil17e76652020-01-29 14:44:19 +0000936
J-Alves8cc7dbb2021-04-16 10:38:48 +0100937 if args.spmc:
J-Alves38223dd2021-04-20 17:31:48 +0100938 # So far only FVP supports tests for SPMC.
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100939 if args.driver != "fvp":
940 raise Exception("Secure tests can only run with fvp driver")
J-Alves38223dd2021-04-20 17:31:48 +0100941
942 if args.hypervisor:
943 driver = FvpDriverBothWorlds(driver_args)
944 else:
945 driver = FvpDriverSPMC(driver_args)
J-Alves8cc7dbb2021-04-16 10:38:48 +0100946 elif args.hypervisor:
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100947 if args.driver == "qemu":
J-Alves8cc7dbb2021-04-16 10:38:48 +0100948 out = os.path.dirname(args.hypervisor)
949 driver = QemuDriver(driver_args, out, args.tfa)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100950 elif args.driver == "fvp":
951 driver = FvpDriverHypervisor(driver_args)
952 elif args.driver == "serial":
953 driver = SerialDriver(driver_args, args.serial_dev,
954 args.serial_baudrate, not args.serial_no_init_wait)
955 else:
956 raise Exception("Unknown driver name: {}".format(args.driver))
J-Alves8cc7dbb2021-04-16 10:38:48 +0100957 else:
958 raise Exception("No Hafnium image provided!\n")
David Brazdil2df24082019-09-05 11:55:08 +0100959
960 # Create class which will drive test execution.
J-Alves8cc7dbb2021-04-16 10:38:48 +0100961 runner = TestRunner(artifacts, driver, test_set_up, args.suite, args.test,
Andrew Walbrane1fa70b2020-05-28 11:30:11 +0100962 args.skip_long_running_tests, args.force_long_running)
David Brazdil2df24082019-09-05 11:55:08 +0100963
964 # Run tests.
965 runner_result = runner.run_tests()
966
967 # Print error message if no tests were run as this is probably unexpected.
968 # Return suitable error code.
969 if runner_result.tests_run == 0:
Andrew Scullbc7189d2018-08-14 09:35:13 +0100970 print("Error: no tests match")
971 return 10
David Brazdil2df24082019-09-05 11:55:08 +0100972 elif runner_result.tests_failed > 0:
Andrew Scullbc7189d2018-08-14 09:35:13 +0100973 return 1
974 else:
David Brazdil2df24082019-09-05 11:55:08 +0100975 return 0
Andrew Scullbc7189d2018-08-14 09:35:13 +0100976
977if __name__ == "__main__":
978 sys.exit(Main())