blob: 55a84eb83615ee1ffee1f73b1beef55654860a5d [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",
David Brazdil2df24082019-09-05 11:55:08 +0100378 ]
J-Alves18a25f92021-05-04 17:47:41 +0100379
380 if uart0_log_path and uart1_log_path:
381 fvp_args += [
382 "-C", f"bp.pl011_uart0.out_file={uart0_log_path}",
383 "-C", f"bp.pl011_uart1.out_file={uart1_log_path}",
384 ]
David Brazdil2df24082019-09-05 11:55:08 +0100385 return fvp_args
386
David Brazdil3cc24aa2019-09-27 10:24:41 +0100387 def run(self, run_name, test_args, is_long_running):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100388 """ Run test """
David Brazdil2df24082019-09-05 11:55:08 +0100389 run_state = self.start_run(run_name)
J-Alves8cc7dbb2021-04-16 10:38:48 +0100390 dt = self.create_dt(run_name)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100391 uart0_log_path = self.create_uart_log(run_name, ".uart0.log")
392 uart1_log_path = self.create_uart_log(run_name, ".uart1.log")
David Brazdil2df24082019-09-05 11:55:08 +0100393
David Brazdil0dbb41f2019-09-09 18:03:35 +0100394 try:
J-Alves8cc7dbb2021-04-16 10:38:48 +0100395 self.gen_dts(dt, test_args)
396 self.compile_dt(run_state, dt)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100397 fvp_args = self.gen_fvp_args(is_long_running, uart0_log_path,
J-Alves8cc7dbb2021-04-16 10:38:48 +0100398 uart1_log_path, dt)
David Brazdil0dbb41f2019-09-09 18:03:35 +0100399 self.exec_logged(run_state, fvp_args)
400 except DriverRunException:
401 pass
David Brazdil2df24082019-09-05 11:55:08 +0100402
403 # Append UART0 output to main log.
404 append_file(run_state.log_path, read_file(uart0_log_path))
David Brazdil0dbb41f2019-09-09 18:03:35 +0100405 return self.finish_run(run_state)
David Brazdil2df24082019-09-05 11:55:08 +0100406
David Brazdil94fd1e92020-02-03 16:45:20 +0000407 def finish(self):
408 """Clean up after running tests."""
409 pass
410
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100411class FvpDriverHypervisor(FvpDriver):
412 """
413 Driver which runs tests in Arm FVP emulator, with hafnium as hypervisor
414 """
415 INITRD_START= 0x84000000
Olivier Depreza516f482021-04-30 18:47:59 +0200416 INITRD_END = 0x86000000 #Default value, however may change if initrd in args
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100417
418 def __init__(self, args):
419 self.vms_in_partitions_json = args.partitions and args.partitions["VMs"]
420 super().__init__(args)
421
422 @property
423 def CPU_START_ADDRESS(self):
424 return "0x04020000"
425
426 @property
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100427 def FVP_PREBUILT_BL31(self):
428 return os.path.join(FVP_PREBUILTS_TFA_TRUSTY_ROOT, "bl31.bin")
429
430 @property
J-Alves38223dd2021-04-20 17:31:48 +0100431 def HYPERVISOR_ADDRESS(self):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100432 return "0x80000000"
433
J-Alves38223dd2021-04-20 17:31:48 +0100434 @property
435 def HYPERVISOR_DTB_ADDRESS(self):
436 return "0x82000000"
437
J-Alves8cc7dbb2021-04-16 10:38:48 +0100438 def gen_dts(self, dt, test_args):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100439 """Create a DeviceTree source which will be compiled into a DTB and
440 passed to FVP for a test run."""
441
442 vm_args = join_if_not_None(self.args.vm_args, test_args)
J-Alves8cc7dbb2021-04-16 10:38:48 +0100443 write_file(dt.dts, read_file(FVP_PREBUILT_DTS))
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100444
445 # Write the vm arguments to the partition manifest
446 to_append = f"""
447/ {{
448 chosen {{
449 bootargs = "{vm_args}";
450 stdout-path = "serial0:115200n8";
451 linux,initrd-start = <{self.INITRD_START if self.args.initrd else 0}>;
452 linux,initrd-end = <{self.INITRD_END if self.args.initrd else 0}>;
453 }};
454}};"""
455 if self.vms_in_partitions_json:
456 to_append += self.get_manifests_from_json(self.args.partitions["VMs"])
457
J-Alves8cc7dbb2021-04-16 10:38:48 +0100458 append_file(dt.dts, to_append)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100459
460 def gen_fvp_args(
J-Alves8cc7dbb2021-04-16 10:38:48 +0100461 self, is_long_running, uart0_log_path, uart1_log_path, dt, call_super = True):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100462 """Generate command line arguments for FVP."""
J-Alves38223dd2021-04-20 17:31:48 +0100463 common_args = (self, is_long_running, uart0_log_path, uart1_log_path, dt)
464 fvp_args = FvpDriver.gen_fvp_args(*common_args) if call_super else []
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100465
J-Alves8cc7dbb2021-04-16 10:38:48 +0100466 fvp_args += [
J-Alves38223dd2021-04-20 17:31:48 +0100467 "--data", f"cluster0.cpu0={dt.dtb}@{self.HYPERVISOR_DTB_ADDRESS}",
468 "--data", f"cluster0.cpu0={self.args.hypervisor}@{self.HYPERVISOR_ADDRESS}",
J-Alves8cc7dbb2021-04-16 10:38:48 +0100469 ]
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100470
471 if self.vms_in_partitions_json:
472 img_ldadd = self.get_img_and_ldadd(self.args.partitions["VMs"])
473 for img, ldadd in img_ldadd:
474 fvp_args += ["--data", f"cluster0.cpu0={img}@{hex(ldadd)}"]
475
476 if self.args.initrd:
477 fvp_args += [
478 "--data",
479 f"cluster0.cpu0={self.args.initrd}@{self.INITRD_START}"
480 ]
481 return fvp_args
482
483class FvpDriverSPMC(FvpDriver):
484 """
485 Driver which runs tests in Arm FVP emulator, with hafnium as SPMC
486 """
487 FVP_PREBUILT_SECURE_DTS = os.path.join(
488 HF_ROOT, "test", "vmapi", "fvp-base-spmc.dts")
489 HFTEST_CMD_FILE = os.path.join("/tmp/", "hftest_cmds")
490
491 def __init__(self, args):
492 if args.partitions is None or args.partitions["SPs"] is None:
J-Alves10446d82021-04-26 11:52:57 +0100493 raise Exception("Need to provide SPs in partitions_json")
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100494 super().__init__(args)
495
496 @property
497 def CPU_START_ADDRESS(self):
498 return "0x04010000"
499
500 @property
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100501 def FVP_PREBUILT_BL31(self):
502 return os.path.join(FVP_PREBUILT_TFA_ROOT, "bl31_spmd.bin")
503
504 @property
J-Alves38223dd2021-04-20 17:31:48 +0100505 def SPMC_ADDRESS(self):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100506 return "0x6000000"
507
J-Alves38223dd2021-04-20 17:31:48 +0100508 @property
509 def SPMC_DTB_ADDRESS(self):
510 return "0x0403f000"
511
J-Alves8cc7dbb2021-04-16 10:38:48 +0100512 def gen_dts(self, dt, test_args):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100513 """Create a DeviceTree source which will be compiled into a DTB and
514 passed to FVP for a test run."""
515 to_append = self.get_manifests_from_json(self.args.partitions["SPs"])
J-Alves8cc7dbb2021-04-16 10:38:48 +0100516 write_file(dt.dts, read_file(FvpDriverSPMC.FVP_PREBUILT_SECURE_DTS))
517 append_file(dt.dts, to_append)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100518
519 def gen_fvp_args(
J-Alves8cc7dbb2021-04-16 10:38:48 +0100520 self, is_long_running, uart0_log_path, uart1_log_path, dt,
J-Alves18a25f92021-05-04 17:47:41 +0100521 call_super = True, secure_ctrl = True):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100522 """Generate command line arguments for FVP."""
J-Alves38223dd2021-04-20 17:31:48 +0100523 common_args = (self, is_long_running, uart0_log_path, uart1_log_path, dt.dtb)
524 fvp_args = FvpDriver.gen_fvp_args(*common_args) if call_super else []
J-Alves8cc7dbb2021-04-16 10:38:48 +0100525
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100526 fvp_args += [
J-Alves38223dd2021-04-20 17:31:48 +0100527 "--data", f"cluster0.cpu0={dt.dtb}@{self.SPMC_DTB_ADDRESS}",
528 "--data", f"cluster0.cpu0={self.args.spmc}@{self.SPMC_ADDRESS}",
J-Alves8cc7dbb2021-04-16 10:38:48 +0100529 ]
530
J-Alves18a25f92021-05-04 17:47:41 +0100531 if secure_ctrl:
532 fvp_args += [
533 "-C", f"bp.pl011_uart0.in_file={FvpDriverSPMC.HFTEST_CMD_FILE}",
534 "-C", f"bp.pl011_uart0.shutdown_tag=\"{HFTEST_CTRL_FINISHED}\"",
535 ]
536
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100537 img_ldadd = self.get_img_and_ldadd(self.args.partitions["SPs"])
538 for img, ldadd in img_ldadd:
539 fvp_args += ["--data", f"cluster0.cpu0={img}@{hex(ldadd)}"]
540
541 return fvp_args
542
543 def run(self, run_name, test_args, is_long_running):
544 with open(FvpDriverSPMC.HFTEST_CMD_FILE, "w+") as f:
545 vm_args = join_if_not_None(self.args.vm_args, test_args)
546 f.write(f"{vm_args}\n")
547 return super().run(run_name, test_args, is_long_running)
548
549 def finish(self):
550 """Clean up after running tests."""
551 os.remove(FvpDriverSPMC.HFTEST_CMD_FILE)
David Brazdil2df24082019-09-05 11:55:08 +0100552
J-Alves38223dd2021-04-20 17:31:48 +0100553class FvpDriverBothWorlds(FvpDriverHypervisor, FvpDriverSPMC):
554 def __init__(self, args):
555 FvpDriverHypervisor.__init__(self, args)
556 FvpDriverSPMC.__init__(self, args)
557
558 @property
559 def CPU_START_ADDRESS(self):
560 return str(0x04010000)
561
562 @property
563 def FVP_PREBUILT_BL31(self):
564 return str(os.path.join(FVP_PREBUILT_TFA_ROOT, "bl31_spmd.bin"))
565
566 def create_dt(self, run_name):
567 dt = dict()
568 dt["hypervisor"] = FvpDriver.create_dt(self, run_name + "_hypervisor")
569 dt["spmc"] = FvpDriver.create_dt(self, run_name + "_spmc")
570 return dt
571
572 @property
573 def HYPERVISOR_ADDRESS(self):
574 return "0x88000000"
575
576 @property
577 def HYPERVISOR_DTB_ADDRESS(self):
Olivier Deprezefd3c672022-02-04 09:40:36 +0100578 return "0x82000000"
J-Alves38223dd2021-04-20 17:31:48 +0100579
580 def compile_dt(self, run_state, dt):
581 FvpDriver.compile_dt(self, run_state, dt["hypervisor"])
582 FvpDriver.compile_dt(self, run_state, dt["spmc"])
583
584 def gen_dts(self, dt, test_args):
585 FvpDriverHypervisor.gen_dts(self, dt["hypervisor"], test_args)
586 FvpDriverSPMC.gen_dts(self, dt["spmc"], test_args)
587
J-Alves8d9fbb92021-12-13 17:28:15 +0000588 def gen_fvp_args(self, is_long_running, uart0_log_path, uart1_log_path, dt):
J-Alves38223dd2021-04-20 17:31:48 +0100589 """Generate command line arguments for FVP."""
J-Alves8d9fbb92021-12-13 17:28:15 +0000590 common_args = (self, is_long_running, uart0_log_path, uart1_log_path)
J-Alves18a25f92021-05-04 17:47:41 +0100591 fvp_args = FvpDriverHypervisor.gen_fvp_args(*common_args, dt["hypervisor"])
592 fvp_args += FvpDriverSPMC.gen_fvp_args(*common_args, dt["spmc"], False,
593 False)
J-Alves8d9fbb92021-12-13 17:28:15 +0000594 return fvp_args
J-Alves18a25f92021-05-04 17:47:41 +0100595
596 def run(self, run_name, test_args, is_long_running):
J-Alves8d9fbb92021-12-13 17:28:15 +0000597 return FvpDriver.run(self, run_name, test_args, is_long_running)
J-Alves38223dd2021-04-20 17:31:48 +0100598
599 def finish(self):
600 """Clean up after running tests."""
J-Alves8d9fbb92021-12-13 17:28:15 +0000601 FvpDriver.finish(self)
J-Alves38223dd2021-04-20 17:31:48 +0100602
David Brazdil17e76652020-01-29 14:44:19 +0000603class SerialDriver(Driver):
604 """Driver which communicates with a device over the serial port."""
605
David Brazdil9d4ed962020-02-06 17:23:48 +0000606 def __init__(self, args, tty_file, baudrate, init_wait):
David Brazdil17e76652020-01-29 14:44:19 +0000607 Driver.__init__(self, args)
David Brazdil9d4ed962020-02-06 17:23:48 +0000608 self.tty_file = tty_file
609 self.baudrate = baudrate
David Brazdil4f9cf9a2020-02-06 17:34:44 +0000610 self.pyserial = importlib.import_module("serial")
David Brazdild8013f92020-02-03 16:40:25 +0000611
David Brazdil9d4ed962020-02-06 17:23:48 +0000612 if init_wait:
David Brazdild8013f92020-02-03 16:40:25 +0000613 input("Press ENTER and then reset the device...")
David Brazdil17e76652020-01-29 14:44:19 +0000614
David Brazdil9d4ed962020-02-06 17:23:48 +0000615 def connect(self):
David Brazdil4f9cf9a2020-02-06 17:34:44 +0000616 return self.pyserial.Serial(self.tty_file, self.baudrate, timeout=10)
David Brazdil9d4ed962020-02-06 17:23:48 +0000617
David Brazdil17e76652020-01-29 14:44:19 +0000618 def run(self, run_name, test_args, is_long_running):
619 """Communicate `test_args` to the device over the serial port."""
620 run_state = self.start_run(run_name)
621
David Brazdil9d4ed962020-02-06 17:23:48 +0000622 with self.connect() as ser:
David Brazdil17e76652020-01-29 14:44:19 +0000623 with open(run_state.log_path, "a") as f:
624 while True:
625 # Read one line from the serial port.
626 line = ser.readline().decode('utf-8')
627 if len(line) == 0:
628 # Timeout
629 run_state.set_ret_code(124)
630 input("Timeout. " +
631 "Press ENTER and then reset the device...")
632 break
633 # Write the line to the log file.
634 f.write(line)
635 if HFTEST_CTRL_GET_COMMAND_LINE in line:
636 # Device is waiting for `test_args`.
637 ser.write(test_args.encode('ascii'))
638 ser.write(b'\r')
639 elif HFTEST_CTRL_FINISHED in line:
640 # Device has finished running this test and will reboot.
641 break
J-Alves18a25f92021-05-04 17:47:41 +0100642
David Brazdil17e76652020-01-29 14:44:19 +0000643 return self.finish_run(run_state)
644
David Brazdil94fd1e92020-02-03 16:45:20 +0000645 def finish(self):
646 """Clean up after running tests."""
David Brazdil9d4ed962020-02-06 17:23:48 +0000647 with self.connect() as ser:
David Brazdil94fd1e92020-02-03 16:45:20 +0000648 while True:
649 line = ser.readline().decode('utf-8')
650 if len(line) == 0:
651 input("Timeout. Press ENTER and then reset the device...")
652 elif HFTEST_CTRL_GET_COMMAND_LINE in line:
653 # Device is waiting for a command. Instruct it to exit
654 # the test environment.
655 ser.write("exit".encode('ascii'))
656 ser.write(b'\r')
657 break
658
David Brazdil2df24082019-09-05 11:55:08 +0100659# Tuple used to return information about the results of running a set of tests.
660TestRunnerResult = collections.namedtuple("TestRunnerResult", [
661 "tests_run",
662 "tests_failed",
Andrew Walbranf9463922020-06-05 16:44:42 +0100663 "tests_skipped",
David Brazdil2df24082019-09-05 11:55:08 +0100664 ])
665
David Brazdil2df24082019-09-05 11:55:08 +0100666class TestRunner:
667 """Class which communicates with a test platform to obtain a list of
668 available tests and driving their execution."""
669
J-Alves8cc7dbb2021-04-16 10:38:48 +0100670 def __init__(self, artifacts, driver, test_set_up, suite_regex, test_regex,
Andrew Walbrane1fa70b2020-05-28 11:30:11 +0100671 skip_long_running_tests, force_long_running):
David Brazdil2df24082019-09-05 11:55:08 +0100672 self.artifacts = artifacts
673 self.driver = driver
J-Alves8cc7dbb2021-04-16 10:38:48 +0100674 self.test_set_up = test_set_up
David Brazdil3cc24aa2019-09-27 10:24:41 +0100675 self.skip_long_running_tests = skip_long_running_tests
Andrew Walbrane1fa70b2020-05-28 11:30:11 +0100676 self.force_long_running = force_long_running
David Brazdil2df24082019-09-05 11:55:08 +0100677
678 self.suite_re = re.compile(suite_regex or ".*")
679 self.test_re = re.compile(test_regex or ".*")
680
681 def extract_hftest_lines(self, raw):
682 """Extract hftest-specific lines from a raw output from an invocation
683 of the test platform."""
684 lines = []
J-Alves07be7bb2021-04-13 11:09:12 +0100685 lines_to_process = raw.splitlines()
686
687 try:
688 # If logs have logs of more than one VM, the loop below to extract
689 # lines won't work. Thus, extracting between starting and ending
690 # logs: HFTEST_CTRL_GET_COMMAND_LINE and HFTEST_CTRL_FINISHED.
691 hftest_start = lines_to_process.index(HFTEST_CTRL_GET_COMMAND_LINE) + 1
692 hftest_end = lines_to_process.index(HFTEST_CTRL_FINISHED)
693 except ValueError:
694 hftest_start = 0
695 hftest_end = len(lines_to_process)
696
697 lines_to_process = lines_to_process[hftest_start : hftest_end]
698
699 for line in lines_to_process:
J-Alves3dbb8562020-12-01 10:45:37 +0000700 match = re.search(f"^VM \d+: ", line)
701 if match is not None:
702 line = line[match.end():]
David Brazdil2df24082019-09-05 11:55:08 +0100703 if line.startswith(HFTEST_LOG_PREFIX):
704 lines.append(line[len(HFTEST_LOG_PREFIX):])
705 return lines
706
707 def get_test_json(self):
708 """Invoke the test platform and request a JSON of available test and
709 test suites."""
Andrew Walbrane1fa70b2020-05-28 11:30:11 +0100710 out = self.driver.run("json", "json", self.force_long_running)
David Brazdil2df24082019-09-05 11:55:08 +0100711 hf_out = "\n".join(self.extract_hftest_lines(out))
712 try:
713 return json.loads(hf_out)
714 except ValueError as e:
715 print(out)
716 raise e
717
718 def collect_results(self, fn, it, xml_node):
719 """Run `fn` on every entry in `it` and collect their TestRunnerResults.
720 Insert "tests" and "failures" nodes to `xml_node`."""
721 tests_run = 0
722 tests_failed = 0
Andrew Walbranf9463922020-06-05 16:44:42 +0100723 tests_skipped = 0
Andrew Walbran42bf2842020-06-05 18:50:19 +0100724 start_time = time.perf_counter()
David Brazdil2df24082019-09-05 11:55:08 +0100725 for i in it:
726 sub_result = fn(i)
727 assert(sub_result.tests_run >= sub_result.tests_failed)
728 tests_run += sub_result.tests_run
729 tests_failed += sub_result.tests_failed
Andrew Walbranf9463922020-06-05 16:44:42 +0100730 tests_skipped += sub_result.tests_skipped
Andrew Walbran42bf2842020-06-05 18:50:19 +0100731 elapsed_time = time.perf_counter() - start_time
David Brazdil2df24082019-09-05 11:55:08 +0100732
Andrew Walbranf9463922020-06-05 16:44:42 +0100733 xml_node.set("tests", str(tests_run + tests_skipped))
David Brazdil2df24082019-09-05 11:55:08 +0100734 xml_node.set("failures", str(tests_failed))
Andrew Walbranf9463922020-06-05 16:44:42 +0100735 xml_node.set("skipped", str(tests_skipped))
Andrew Walbran42bf2842020-06-05 18:50:19 +0100736 xml_node.set("time", str(elapsed_time))
Andrew Walbranf9463922020-06-05 16:44:42 +0100737 return TestRunnerResult(tests_run, tests_failed, tests_skipped)
David Brazdil2df24082019-09-05 11:55:08 +0100738
739 def is_passed_test(self, test_out):
740 """Parse the output of a test and return True if it passed."""
741 return \
742 len(test_out) > 0 and \
743 test_out[-1] == HFTEST_LOG_FINISHED and \
744 not any(l.startswith(HFTEST_LOG_FAILURE_PREFIX) for l in test_out)
745
Andrew Walbranf9463922020-06-05 16:44:42 +0100746 def get_failure_message(self, test_out):
747 """Parse the output of a test and return the message of the first
748 assertion failure."""
749 for i, line in enumerate(test_out):
750 if line.startswith(HFTEST_LOG_FAILURE_PREFIX) and i + 1 < len(test_out):
751 # The assertion message is on the line after the 'Failure:'
752 return test_out[i + 1].strip()
753
754 return None
755
Fuad Tabba36c8c2b2019-11-04 16:55:32 +0000756 def get_log_name(self, suite, test):
757 """Returns a string with a generated log name for the test."""
758 log_name = ""
759
760 cpu = self.driver.args.cpu
761 if cpu:
762 log_name += cpu + "."
763
764 log_name += suite["name"] + "." + test["name"]
765
766 return log_name
767
David Brazdil2df24082019-09-05 11:55:08 +0100768 def run_test(self, suite, test, suite_xml):
769 """Invoke the test platform and request to run a given `test` in given
770 `suite`. Create a new XML node with results under `suite_xml`.
771 Test only invoked if it matches the regex given to constructor."""
David Brazdil3cc24aa2019-09-27 10:24:41 +0100772 if not self.test_re.match(test["name"]):
Andrew Walbranf9463922020-06-05 16:44:42 +0100773 return TestRunnerResult(tests_run=0, tests_failed=0, tests_skipped=0)
David Brazdil2df24082019-09-05 11:55:08 +0100774
775 test_xml = ET.SubElement(suite_xml, "testcase")
David Brazdil3cc24aa2019-09-27 10:24:41 +0100776 test_xml.set("name", test["name"])
777 test_xml.set("classname", suite["name"])
Andrew Walbranf9463922020-06-05 16:44:42 +0100778
779 if self.skip_long_running_tests and test["is_long_running"]:
780 print(" SKIP", test["name"])
781 test_xml.set("status", "notrun")
782 skipped_xml = ET.SubElement(test_xml, "skipped")
783 skipped_xml.set("message", "Long running")
784 return TestRunnerResult(tests_run=0, tests_failed=0, tests_skipped=1)
785
786 print(" RUN", test["name"])
787 log_name = self.get_log_name(suite, test)
788
David Brazdil2df24082019-09-05 11:55:08 +0100789 test_xml.set("status", "run")
790
Andrew Walbran42bf2842020-06-05 18:50:19 +0100791 start_time = time.perf_counter()
Andrew Walbranf9463922020-06-05 16:44:42 +0100792 out = self.driver.run(
David Brazdil3cc24aa2019-09-27 10:24:41 +0100793 log_name, "run {} {}".format(suite["name"], test["name"]),
Andrew Walbranf9463922020-06-05 16:44:42 +0100794 test["is_long_running"] or self.force_long_running)
795 hftest_out = self.extract_hftest_lines(out)
Andrew Walbran42bf2842020-06-05 18:50:19 +0100796 elapsed_time = time.perf_counter() - start_time
797
798 test_xml.set("time", str(elapsed_time))
David Brazdil2df24082019-09-05 11:55:08 +0100799
Andrew Walbranf9463922020-06-05 16:44:42 +0100800 system_out_xml = ET.SubElement(test_xml, "system-out")
801 system_out_xml.text = out
802
803 if self.is_passed_test(hftest_out):
David Brazdil2df24082019-09-05 11:55:08 +0100804 print(" PASS")
Andrew Walbranf9463922020-06-05 16:44:42 +0100805 return TestRunnerResult(tests_run=1, tests_failed=0, tests_skipped=0)
David Brazdil2df24082019-09-05 11:55:08 +0100806 else:
David Brazdil623b6812019-09-09 11:41:08 +0100807 print("[x] FAIL --", self.driver.get_run_log(log_name))
David Brazdil2df24082019-09-05 11:55:08 +0100808 failure_xml = ET.SubElement(test_xml, "failure")
Andrew Walbranf9463922020-06-05 16:44:42 +0100809 failure_message = self.get_failure_message(hftest_out) or "Test failed"
810 failure_xml.set("message", failure_message)
811 failure_xml.text = '\n'.join(hftest_out)
812 return TestRunnerResult(tests_run=1, tests_failed=1, tests_skipped=0)
David Brazdil2df24082019-09-05 11:55:08 +0100813
814 def run_suite(self, suite, xml):
815 """Invoke the test platform and request to run all matching tests in
816 `suite`. Create new XML nodes with results under `xml`.
817 Suite skipped if it does not match the regex given to constructor."""
818 if not self.suite_re.match(suite["name"]):
Andrew Walbranf9463922020-06-05 16:44:42 +0100819 return TestRunnerResult(tests_run=0, tests_failed=0, tests_skipped=0)
David Brazdil2df24082019-09-05 11:55:08 +0100820
821 print(" SUITE", suite["name"])
822 suite_xml = ET.SubElement(xml, "testsuite")
823 suite_xml.set("name", suite["name"])
Andrew Walbran16ae62e2020-06-05 18:27:46 +0100824 properties_xml = ET.SubElement(suite_xml, "properties")
825
826 property_xml = ET.SubElement(properties_xml, "property")
827 property_xml.set("name", "driver")
828 property_xml.set("value", type(self.driver).__name__)
829
830 if self.driver.args.cpu:
831 property_xml = ET.SubElement(properties_xml, "property")
832 property_xml.set("name", "cpu")
833 property_xml.set("value", self.driver.args.cpu)
David Brazdil2df24082019-09-05 11:55:08 +0100834
835 return self.collect_results(
836 lambda test: self.run_test(suite, test, suite_xml),
837 suite["tests"],
838 suite_xml)
839
840 def run_tests(self):
841 """Run all suites and tests matching regexes given to constructor.
842 Write results to sponge log XML. Return the number of run and failed
843 tests."""
844
845 test_spec = self.get_test_json()
846 timestamp = datetime.datetime.now().replace(microsecond=0).isoformat()
847
848 xml = ET.Element("testsuites")
J-Alves8cc7dbb2021-04-16 10:38:48 +0100849 xml.set("name", self.test_set_up)
David Brazdil2df24082019-09-05 11:55:08 +0100850 xml.set("timestamp", timestamp)
851
852 result = self.collect_results(
853 lambda suite: self.run_suite(suite, xml),
854 test_spec["suites"],
855 xml)
856
857 # Write XML to file.
David Brazdilee5e25d2020-01-24 14:17:45 +0000858 ET.ElementTree(xml).write(self.artifacts.sponge_xml_path,
859 encoding='utf-8', xml_declaration=True)
David Brazdil2df24082019-09-05 11:55:08 +0100860
861 if result.tests_failed > 0:
862 print("[x] FAIL:", result.tests_failed, "of", result.tests_run,
863 "tests failed")
864 elif result.tests_run > 0:
865 print(" PASS: all", result.tests_run, "tests passed")
866
David Brazdil94fd1e92020-02-03 16:45:20 +0000867 # Let the driver clean up.
868 self.driver.finish()
869
David Brazdil2df24082019-09-05 11:55:08 +0100870 return result
Andrew Scullbc7189d2018-08-14 09:35:13 +0100871
Andrew Scullbc7189d2018-08-14 09:35:13 +0100872def Main():
873 parser = argparse.ArgumentParser()
J-Alves8cc7dbb2021-04-16 10:38:48 +0100874 parser.add_argument("--hypervisor")
875 parser.add_argument("--spmc")
Andrew Scull23e93a82018-10-26 14:56:04 +0100876 parser.add_argument("--log", required=True)
Andrew Walbran7559fcf2019-05-09 17:11:20 +0100877 parser.add_argument("--out_initrd")
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100878 parser.add_argument("--out_partitions")
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000879 parser.add_argument("--initrd")
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100880 parser.add_argument("--partitions_json")
Andrew Scullbc7189d2018-08-14 09:35:13 +0100881 parser.add_argument("--suite")
882 parser.add_argument("--test")
Andrew Walbranbc342d42019-02-05 16:56:02 +0000883 parser.add_argument("--vm_args")
David Brazdil17e76652020-01-29 14:44:19 +0000884 parser.add_argument("--driver", default="qemu")
885 parser.add_argument("--serial-dev", default="/dev/ttyUSB0")
886 parser.add_argument("--serial-baudrate", type=int, default=115200)
David Brazdild8013f92020-02-03 16:40:25 +0000887 parser.add_argument("--serial-no-init-wait", action="store_true")
David Brazdil3cc24aa2019-09-27 10:24:41 +0100888 parser.add_argument("--skip-long-running-tests", action="store_true")
Andrew Walbrane1fa70b2020-05-28 11:30:11 +0100889 parser.add_argument("--force-long-running", action="store_true")
Fuad Tabba36c8c2b2019-11-04 16:55:32 +0000890 parser.add_argument("--cpu",
891 help="Selects the CPU configuration for the run environment.")
Andrew Walbranf636b842020-01-10 11:46:12 +0000892 parser.add_argument("--tfa", action="store_true")
Andrew Scullbc7189d2018-08-14 09:35:13 +0100893 args = parser.parse_args()
David Brazdil2df24082019-09-05 11:55:08 +0100894
J-Alves8cc7dbb2021-04-16 10:38:48 +0100895 # Create class which will manage all test artifacts.
896 if args.hypervisor and args.spmc:
897 test_set_up = "hypervisor_and_spmc"
898 elif args.hypervisor:
899 test_set_up = "hypervisor"
900 elif args.spmc:
901 test_set_up = "spmc"
902 else:
903 raise Exception("No Hafnium image provided!\n")
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100904
J-Alves8cc7dbb2021-04-16 10:38:48 +0100905 initrd = None
906 if args.hypervisor and args.initrd:
David Brazdil0dbb41f2019-09-09 18:03:35 +0100907 initrd_dir = os.path.join(args.out_initrd, "obj", args.initrd)
908 initrd = os.path.join(initrd_dir, "initrd.img")
J-Alves8cc7dbb2021-04-16 10:38:48 +0100909 test_set_up += "_" + args.initrd
Andrew Walbranbc342d42019-02-05 16:56:02 +0000910 vm_args = args.vm_args or ""
David Brazdil2df24082019-09-05 11:55:08 +0100911
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100912 partitions = None
J-Alves18a25f92021-05-04 17:47:41 +0100913 global_run_name = None
914 if args.driver == "fvp":
915 if args.partitions_json is not None:
916 partitions_dir = os.path.join(
917 args.out_partitions, "obj", args.partitions_json)
918 partitions = json.load(open(partitions_dir, "r"))
919 global_run_name = os.path.basename(args.partitions_json).split(".")[0]
920 elif args.hypervisor:
921 if args.initrd:
922 global_run_name = os.path.basename(args.initrd)
923 else:
924 global_run_name = os.path.basename(args.hypervisor).split(".")[0]
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100925
David Brazdil2df24082019-09-05 11:55:08 +0100926 # Create class which will manage all test artifacts.
J-Alves8cc7dbb2021-04-16 10:38:48 +0100927 log_dir = os.path.join(args.log, test_set_up)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100928 artifacts = ArtifactsManager(log_dir)
David Brazdil2df24082019-09-05 11:55:08 +0100929
930 # Create a driver for the platform we want to test on.
J-Alves8cc7dbb2021-04-16 10:38:48 +0100931 driver_args = DriverArgs(artifacts, args.hypervisor, args.spmc, initrd,
J-Alves18a25f92021-05-04 17:47:41 +0100932 vm_args, args.cpu, partitions, global_run_name)
David Brazdil17e76652020-01-29 14:44:19 +0000933
J-Alves8cc7dbb2021-04-16 10:38:48 +0100934 if args.spmc:
J-Alves38223dd2021-04-20 17:31:48 +0100935 # So far only FVP supports tests for SPMC.
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100936 if args.driver != "fvp":
937 raise Exception("Secure tests can only run with fvp driver")
J-Alves38223dd2021-04-20 17:31:48 +0100938
939 if args.hypervisor:
940 driver = FvpDriverBothWorlds(driver_args)
941 else:
942 driver = FvpDriverSPMC(driver_args)
J-Alves8cc7dbb2021-04-16 10:38:48 +0100943 elif args.hypervisor:
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100944 if args.driver == "qemu":
J-Alves8cc7dbb2021-04-16 10:38:48 +0100945 out = os.path.dirname(args.hypervisor)
946 driver = QemuDriver(driver_args, out, args.tfa)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100947 elif args.driver == "fvp":
948 driver = FvpDriverHypervisor(driver_args)
949 elif args.driver == "serial":
950 driver = SerialDriver(driver_args, args.serial_dev,
951 args.serial_baudrate, not args.serial_no_init_wait)
952 else:
953 raise Exception("Unknown driver name: {}".format(args.driver))
J-Alves8cc7dbb2021-04-16 10:38:48 +0100954 else:
955 raise Exception("No Hafnium image provided!\n")
David Brazdil2df24082019-09-05 11:55:08 +0100956
957 # Create class which will drive test execution.
J-Alves8cc7dbb2021-04-16 10:38:48 +0100958 runner = TestRunner(artifacts, driver, test_set_up, args.suite, args.test,
Andrew Walbrane1fa70b2020-05-28 11:30:11 +0100959 args.skip_long_running_tests, args.force_long_running)
David Brazdil2df24082019-09-05 11:55:08 +0100960
961 # Run tests.
962 runner_result = runner.run_tests()
963
964 # Print error message if no tests were run as this is probably unexpected.
965 # Return suitable error code.
966 if runner_result.tests_run == 0:
Andrew Scullbc7189d2018-08-14 09:35:13 +0100967 print("Error: no tests match")
968 return 10
David Brazdil2df24082019-09-05 11:55:08 +0100969 elif runner_result.tests_failed > 0:
Andrew Scullbc7189d2018-08-14 09:35:13 +0100970 return 1
971 else:
David Brazdil2df24082019-09-05 11:55:08 +0100972 return 0
Andrew Scullbc7189d2018-08-14 09:35:13 +0100973
974if __name__ == "__main__":
975 sys.exit(Main())