blob: 18fafcd366fccabd06f90a10af65e2afa9f1e994 [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
Olivier Depreze30c36f2022-11-22 11:26:47 +010029import platform
J-Alvesacdbb862023-01-31 17:14:55 +000030import tempfile
Andrew Scullbc7189d2018-08-14 09:35:13 +010031
Olivier Depreze30c36f2022-11-22 11:26:47 +010032MACHINE = platform.machine()
Olivier Depreze30c36f2022-11-22 11:26:47 +010033
Andrew Scull845fc9b2019-04-03 12:44:26 +010034HFTEST_LOG_PREFIX = "[hftest] "
35HFTEST_LOG_FAILURE_PREFIX = "Failure:"
36HFTEST_LOG_FINISHED = "FINISHED"
37
David Brazdil17e76652020-01-29 14:44:19 +000038HFTEST_CTRL_GET_COMMAND_LINE = "[hftest_ctrl:get_command_line]"
39HFTEST_CTRL_FINISHED = "[hftest_ctrl:finished]"
40
David Brazdil2df24082019-09-05 11:55:08 +010041HF_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(
42 os.path.abspath(__file__))))
David Brazdil5715f042019-08-27 11:11:51 +010043DTC_SCRIPT = os.path.join(HF_ROOT, "build", "image", "dtc.py")
David Brazdil2df24082019-09-05 11:55:08 +010044FVP_BINARY = os.path.join(
Olivier Deprez9f4bad42021-06-18 12:19:07 +020045 os.path.dirname(HF_ROOT), "fvp", "Base_RevC_AEMvA_pkg", "models",
Olivier Deprez78d94eb2023-01-31 09:02:32 +000046 "Linux64_armv8l_GCC-9.3" if MACHINE == "aarch64" else "Linux64_GCC-9.3",
47 "FVP_Base_RevC-2xAEMvA")
Olivier Depreza6d2e6d2020-11-06 18:09:50 +010048HF_PREBUILTS = os.path.join(HF_ROOT, "prebuilts")
Olivier Deprez78d94eb2023-01-31 09:02:32 +000049QEMU_PREBUILTS = os.path.join(HF_PREBUILTS,
50 "linux-" + ("x64" if MACHINE == "x86_64" else MACHINE),
51 "qemu", "qemu-system-aarch64")
Olivier Deprez0c5e7dc2023-01-17 10:29:04 +010052FVP_PREBUILTS_TFA_ROOT = os.path.join(
53 HF_PREBUILTS, "linux-aarch64", "trusted-firmware-a", "fvp")
David Brazdil2df24082019-09-05 11:55:08 +010054FVP_PREBUILT_DTS = os.path.join(
Olivier Deprez0c5e7dc2023-01-17 10:29:04 +010055 FVP_PREBUILTS_TFA_ROOT, "fvp-base-gicv3-psci-1t.dts")
Olivier Depreza6d2e6d2020-11-06 18:09:50 +010056
Olivier Deprez1b1c4b62023-01-17 09:56:32 +010057FVP_PREBUILT_TFA_SPMD_ROOT = os.path.join(
58 HF_PREBUILTS, "linux-aarch64", "trusted-firmware-a-spmd", "fvp")
Andrew Scull845fc9b2019-04-03 12:44:26 +010059
Shruti Gupta2b1e0cd2022-12-20 17:45:24 +000060FVP_PREBUILTS_TFA_EL3_SPMC_ROOT = os.path.join(
61 HF_PREBUILTS, "linux-aarch64", "trusted-firmware-a-el3-spmc")
J-Alves852fe742021-04-22 11:59:55 +010062VM_NODE_REGEX = "vm[1-9]"
63
Olivier Deprez3917deb2023-01-19 11:08:43 +010064QEMU_CPU_MAX = "max,pauth-impdef=true"
65
David Brazdil2df24082019-09-05 11:55:08 +010066def read_file(path):
67 with open(path, "r") as f:
68 return f.read()
Andrew Scull845fc9b2019-04-03 12:44:26 +010069
David Brazdil2df24082019-09-05 11:55:08 +010070def write_file(path, to_write, append=False):
71 with open(path, "a" if append else "w") as f:
72 f.write(to_write)
73
74def append_file(path, to_write):
75 write_file(path, to_write, append=True)
76
77def join_if_not_None(*args):
78 return " ".join(filter(lambda x: x, args))
79
J-Alves852fe742021-04-22 11:59:55 +010080def get_vm_node_from_manifest(dts : str):
81 """ Get VM node string from Partition's extension to Partition Manager's
82 manifest."""
83 match = re.search(VM_NODE_REGEX, dts)
84 if not match:
85 raise Exception("Partition's node is not defined in its manifest.")
86 return match.group()
87
88def correct_vm_node(dts: str, node_index : int):
89 """ The vm node is being appended to the Partition Manager manifests.
90 Ideally, these files would be reused accross various test set-ups."""
91 return dts.replace(get_vm_node_from_manifest(dts), f"vm{node_index}")
92
J-Alves8cc7dbb2021-04-16 10:38:48 +010093DT = collections.namedtuple("DT", ["dts", "dtb"])
94
David Brazdil2df24082019-09-05 11:55:08 +010095class ArtifactsManager:
96 """Class which manages folder with test artifacts."""
97
98 def __init__(self, log_dir):
99 self.created_files = []
100 self.log_dir = log_dir
101
102 # Create directory.
Andrew Scull845fc9b2019-04-03 12:44:26 +0100103 try:
David Brazdil2df24082019-09-05 11:55:08 +0100104 os.makedirs(self.log_dir)
105 except OSError:
106 if not os.path.isdir(self.log_dir):
107 raise
108 print("Logs saved under", log_dir)
109
110 # Create files expected by the Sponge test result parser.
111 self.sponge_log_path = self.create_file("sponge_log", ".log")
112 self.sponge_xml_path = self.create_file("sponge_log", ".xml")
113
David Brazdil623b6812019-09-09 11:41:08 +0100114 def gen_file_path(self, basename, extension):
115 """Generate path to a file in the log directory."""
116 return os.path.join(self.log_dir, basename + extension)
117
David Brazdil2df24082019-09-05 11:55:08 +0100118 def create_file(self, basename, extension):
119 """Create and touch a new file in the log folder. Ensure that no other
120 file of the same name was created by this instance of ArtifactsManager.
121 """
122 # Determine the path of the file.
David Brazdil623b6812019-09-09 11:41:08 +0100123 path = self.gen_file_path(basename, extension)
David Brazdil2df24082019-09-05 11:55:08 +0100124
125 # Check that the path is unique.
126 assert(path not in self.created_files)
127 self.created_files += [ path ]
128
129 # Touch file.
130 with open(path, "w") as f:
131 pass
132
133 return path
Andrew Scullbc7189d2018-08-14 09:35:13 +0100134
David Brazdil623b6812019-09-09 11:41:08 +0100135 def get_file(self, basename, extension):
136 """Return path to a file in the log folder. Assert that it was created
137 by this instance of ArtifactsManager."""
138 path = self.gen_file_path(basename, extension)
139 assert(path in self.created_files)
140 return path
141
Andrew Scullbc7189d2018-08-14 09:35:13 +0100142
David Brazdil2df24082019-09-05 11:55:08 +0100143# Tuple holding the arguments common to all driver constructors.
144# This is to avoid having to pass arguments from subclasses to superclasses.
145DriverArgs = collections.namedtuple("DriverArgs", [
146 "artifacts",
J-Alves8cc7dbb2021-04-16 10:38:48 +0100147 "hypervisor",
148 "spmc",
David Brazdil2df24082019-09-05 11:55:08 +0100149 "initrd",
150 "vm_args",
David Brazdil17e76652020-01-29 14:44:19 +0000151 "cpu",
J-Alves18a25f92021-05-04 17:47:41 +0100152 "partitions",
153 "global_run_name",
David Brazdil2df24082019-09-05 11:55:08 +0100154 ])
Marc Bonnici0a125632019-04-01 13:46:52 +0100155
David Brazdil2df24082019-09-05 11:55:08 +0100156# State shared between the common Driver class and its subclasses during
157# a single invocation of the target platform.
David Brazdil7325eaf2019-09-27 13:04:51 +0100158class DriverRunState:
159 def __init__(self, log_path):
160 self.log_path = log_path
161 self.ret_code = 0
Andrew Walbran98656252019-03-14 14:52:29 +0000162
David Brazdil7325eaf2019-09-27 13:04:51 +0100163 def set_ret_code(self, ret_code):
164 self.ret_code = ret_code
Andrew Walbran98656252019-03-14 14:52:29 +0000165
David Brazdil0dbb41f2019-09-09 18:03:35 +0100166class DriverRunException(Exception):
167 """Exception thrown if subprocess invoked by a driver returned non-zero
168 status code. Used to fast-exit from a driver command sequence."""
169 pass
170
171
David Brazdil2df24082019-09-05 11:55:08 +0100172class Driver:
173 """Parent class of drivers for all testable platforms."""
174
175 def __init__(self, args):
176 self.args = args
177
David Brazdil623b6812019-09-09 11:41:08 +0100178 def get_run_log(self, run_name):
179 """Return path to the main log of a given test run."""
180 return self.args.artifacts.get_file(run_name, ".log")
181
David Brazdil2df24082019-09-05 11:55:08 +0100182 def start_run(self, run_name):
183 """Hook called by Driver subclasses before they invoke the target
184 platform."""
David Brazdil7325eaf2019-09-27 13:04:51 +0100185 return DriverRunState(self.args.artifacts.create_file(run_name, ".log"))
David Brazdil2df24082019-09-05 11:55:08 +0100186
Andrew Walbranf636b842020-01-10 11:46:12 +0000187 def exec_logged(self, run_state, exec_args, cwd=None):
David Brazdil2df24082019-09-05 11:55:08 +0100188 """Run a subprocess on behalf of a Driver subclass and append its
189 stdout and stderr to the main log."""
David Brazdil0dbb41f2019-09-09 18:03:35 +0100190 assert(run_state.ret_code == 0)
David Brazdil2df24082019-09-05 11:55:08 +0100191 with open(run_state.log_path, "a") as f:
192 f.write("$ {}\r\n".format(" ".join(exec_args)))
193 f.flush()
Andrew Walbranf636b842020-01-10 11:46:12 +0000194 ret_code = subprocess.call(exec_args, stdout=f, stderr=f, cwd=cwd)
David Brazdil0dbb41f2019-09-09 18:03:35 +0100195 if ret_code != 0:
David Brazdil7325eaf2019-09-27 13:04:51 +0100196 run_state.set_ret_code(ret_code)
David Brazdil0dbb41f2019-09-09 18:03:35 +0100197 raise DriverRunException()
David Brazdil2df24082019-09-05 11:55:08 +0100198
David Brazdil0dbb41f2019-09-09 18:03:35 +0100199 def finish_run(self, run_state):
David Brazdil2df24082019-09-05 11:55:08 +0100200 """Hook called by Driver subclasses after they finished running the
201 target platform. `ret_code` argument is the return code of the main
202 command run by the driver. A corresponding log message is printed."""
203 # Decode return code and add a message to the log.
204 with open(run_state.log_path, "a") as f:
David Brazdil0dbb41f2019-09-09 18:03:35 +0100205 if run_state.ret_code == 124:
David Brazdil2df24082019-09-05 11:55:08 +0100206 f.write("\r\n{}{} timed out\r\n".format(
207 HFTEST_LOG_PREFIX, HFTEST_LOG_FAILURE_PREFIX))
David Brazdil0dbb41f2019-09-09 18:03:35 +0100208 elif run_state.ret_code != 0:
David Brazdil2df24082019-09-05 11:55:08 +0100209 f.write("\r\n{}{} process return code {}\r\n".format(
David Brazdil0dbb41f2019-09-09 18:03:35 +0100210 HFTEST_LOG_PREFIX, HFTEST_LOG_FAILURE_PREFIX,
211 run_state.ret_code))
David Brazdil2df24082019-09-05 11:55:08 +0100212
213 # Append log of this run to full test log.
214 log_content = read_file(run_state.log_path)
215 append_file(
216 self.args.artifacts.sponge_log_path,
217 log_content + "\r\n\r\n")
218 return log_content
Andrew Walbran98656252019-03-14 14:52:29 +0000219
220
David Brazdil2df24082019-09-05 11:55:08 +0100221class QemuDriver(Driver):
222 """Driver which runs tests in QEMU."""
223
Andrew Walbranf636b842020-01-10 11:46:12 +0000224 def __init__(self, args, qemu_wd, tfa):
David Brazdil2df24082019-09-05 11:55:08 +0100225 Driver.__init__(self, args)
Andrew Walbranf636b842020-01-10 11:46:12 +0000226 self.qemu_wd = qemu_wd
227 self.tfa = tfa
David Brazdil2df24082019-09-05 11:55:08 +0100228
David Brazdila2358d42020-01-27 18:51:38 +0000229 def gen_exec_args(self, test_args, is_long_running):
David Brazdil2df24082019-09-05 11:55:08 +0100230 """Generate command line arguments for QEMU."""
David Brazdil3cc24aa2019-09-27 10:24:41 +0100231 time_limit = "120s" if is_long_running else "10s"
Fuad Tabba36c8c2b2019-11-04 16:55:32 +0000232 # If no CPU configuration is selected, then test against the maximum
233 # configuration, "max", supported by QEMU.
Olivier Deprez3917deb2023-01-19 11:08:43 +0100234 if not self.args.cpu or self.args.cpu == "max":
235 cpu = QEMU_CPU_MAX
236 else:
237 cpu = self.args.cpu
238
David Brazdil2df24082019-09-05 11:55:08 +0100239 exec_args = [
David Brazdil3cc24aa2019-09-27 10:24:41 +0100240 "timeout", "--foreground", time_limit,
Olivier Depreze30c36f2022-11-22 11:26:47 +0100241 QEMU_PREBUILTS,
Olivier Deprez5373f232022-11-23 09:57:19 +0100242 "-no-reboot", "-machine", "virt-6.2,virtualization=on,gic-version=3",
J-Alves871e3732022-05-31 17:10:50 +0100243 "-cpu", cpu, "-smp", "8", "-m", "1G",
David Brazdil2df24082019-09-05 11:55:08 +0100244 "-nographic", "-nodefaults", "-serial", "stdio",
J-Alves8cc7dbb2021-04-16 10:38:48 +0100245 "-d", "unimp", "-kernel", os.path.abspath(self.args.hypervisor),
David Brazdil2df24082019-09-05 11:55:08 +0100246 ]
247
Andrew Walbranf636b842020-01-10 11:46:12 +0000248 if self.tfa:
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100249 bl1_path = os.path.join(
Olivier Deprez0c5e7dc2023-01-17 10:29:04 +0100250 HF_PREBUILTS, "linux-aarch64", "trusted-firmware-a",
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100251 "qemu", "bl1.bin")
Andrew Walbranf636b842020-01-10 11:46:12 +0000252 exec_args += ["-bios",
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100253 os.path.abspath(bl1_path),
254 "-machine", "secure=on", "-semihosting-config",
Andrew Walbranab4b2d52020-06-11 16:54:10 +0100255 "enable=on,target=native"]
Andrew Walbranf636b842020-01-10 11:46:12 +0000256
David Brazdil2df24082019-09-05 11:55:08 +0100257 if self.args.initrd:
Andrew Walbranf636b842020-01-10 11:46:12 +0000258 exec_args += ["-initrd", os.path.abspath(self.args.initrd)]
David Brazdil2df24082019-09-05 11:55:08 +0100259
260 vm_args = join_if_not_None(self.args.vm_args, test_args)
261 if vm_args:
262 exec_args += ["-append", vm_args]
263
264 return exec_args
265
J-Alves67c31912023-02-02 13:52:50 +0000266 def run(self, run_name, test_args, is_long_running, debug = False,
267 show_output = False):
David Brazdil2df24082019-09-05 11:55:08 +0100268 """Run test given by `test_args` in QEMU."""
J-Alves67c31912023-02-02 13:52:50 +0000269 # TODO: use 'debug' and 'show_output' flags.
David Brazdil2df24082019-09-05 11:55:08 +0100270 run_state = self.start_run(run_name)
David Brazdil0dbb41f2019-09-09 18:03:35 +0100271
272 try:
David Brazdil0dbb41f2019-09-09 18:03:35 +0100273 # Execute test in QEMU..
David Brazdila2358d42020-01-27 18:51:38 +0000274 exec_args = self.gen_exec_args(test_args, is_long_running)
Andrew Walbranf636b842020-01-10 11:46:12 +0000275 self.exec_logged(run_state, exec_args,
276 cwd=self.qemu_wd)
David Brazdil0dbb41f2019-09-09 18:03:35 +0100277 except DriverRunException:
278 pass
279
280 return self.finish_run(run_state)
Andrew Scullbc7189d2018-08-14 09:35:13 +0100281
David Brazdil94fd1e92020-02-03 16:45:20 +0000282 def finish(self):
283 """Clean up after running tests."""
284 pass
285
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100286class FvpDriver(Driver, ABC):
287 """Base class for driver which runs tests in Arm FVP emulator."""
David Brazdil2df24082019-09-05 11:55:08 +0100288
289 def __init__(self, args):
Fuad Tabba36c8c2b2019-11-04 16:55:32 +0000290 if args.cpu:
291 raise ValueError("FVP emulator does not support the --cpu option.")
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100292 super().__init__(args)
David Brazdil2df24082019-09-05 11:55:08 +0100293
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100294 @property
295 @abstractmethod
296 def CPU_START_ADDRESS(self):
297 pass
David Brazdil2df24082019-09-05 11:55:08 +0100298
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100299 @property
300 @abstractmethod
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100301 def FVP_PREBUILT_BL31(self):
302 pass
303
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100304 def create_dt(self, run_name : str):
305 """Create DT related files, and return respective paths in a tuple
306 (dts,dtb)"""
J-Alves8cc7dbb2021-04-16 10:38:48 +0100307 return DT(self.args.artifacts.create_file(run_name, ".dts"),
308 self.args.artifacts.create_file(run_name, ".dtb"))
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100309
J-Alves8cc7dbb2021-04-16 10:38:48 +0100310 def compile_dt(self, run_state, dt : DT):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100311 """Compile DT calling dtc."""
312 dtc_args = [
J-Alves8cc7dbb2021-04-16 10:38:48 +0100313 DTC_SCRIPT, "compile", "-i", dt.dts, "-o", dt.dtb,
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100314 ]
315 self.exec_logged(run_state, dtc_args)
316
317 def create_uart_log(self, run_name : str, file_name : str):
318 """Create uart log file, and return path"""
319 return self.args.artifacts.create_file(run_name, file_name)
320
321 def get_img_and_ldadd(self, partitions : dict):
322 ret = []
323 for i, p in enumerate(partitions):
J-Alves852fe742021-04-22 11:59:55 +0100324 with open(p["dts"], "r") as dt:
325 dts = dt.read()
326 manifest = fdt.parse_dts(dts)
327 vm_node = get_vm_node_from_manifest(dts)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100328 load_address = manifest.get_property("load_address",
J-Alves852fe742021-04-22 11:59:55 +0100329 f"/hypervisor/{vm_node}").value
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100330 ret.append((p["img"], load_address))
331 return ret
332
333 def get_manifests_from_json(self, partitions : list):
334 manifests = ""
335 if partitions is not None:
J-Alves852fe742021-04-22 11:59:55 +0100336 for i, p in enumerate(partitions):
337 manifests += correct_vm_node(read_file(p["dts"]), i + 1)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100338 return manifests
339
340 @abstractmethod
J-Alves8cc7dbb2021-04-16 10:38:48 +0100341 def gen_dts(self, dt, test_args):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100342 """Abstract method to generate dts file. This specific to the use case
343 so should be implemented within derived driver"""
344 pass
345
346 @abstractmethod
David Brazdil2df24082019-09-05 11:55:08 +0100347 def gen_fvp_args(
J-Alves67c31912023-02-02 13:52:50 +0000348 self, is_long_running, uart0_log_path, uart1_log_path, dt,
349 debug = False, show_output = False):
David Brazdil2df24082019-09-05 11:55:08 +0100350 """Generate command line arguments for FVP."""
J-Alves67c31912023-02-02 13:52:50 +0000351 show_output = debug or show_output
Andrew Walbranee5418e2019-11-27 17:43:05 +0000352 time_limit = "80s" if is_long_running else "40s"
J-Alves67c31912023-02-02 13:52:50 +0000353 fvp_args = []
354
355 if not show_output:
356 fvp_args = [
357 "timeout", "--foreground", time_limit,
358 ]
359
360 fvp_args += [
David Brazdil2df24082019-09-05 11:55:08 +0100361 FVP_BINARY,
J-Alves10446d82021-04-26 11:52:57 +0100362 "-C", "pci.pci_smmuv3.mmu.SMMU_AIDR=2",
363 "-C", "pci.pci_smmuv3.mmu.SMMU_IDR0=0x0046123B",
364 "-C", "pci.pci_smmuv3.mmu.SMMU_IDR1=0x00600002",
365 "-C", "pci.pci_smmuv3.mmu.SMMU_IDR3=0x1714",
366 "-C", "pci.pci_smmuv3.mmu.SMMU_IDR5=0xFFFF0472",
367 "-C", "pci.pci_smmuv3.mmu.SMMU_S_IDR1=0xA0000002",
368 "-C", "pci.pci_smmuv3.mmu.SMMU_S_IDR2=0",
369 "-C", "pci.pci_smmuv3.mmu.SMMU_S_IDR3=0",
David Brazdil2df24082019-09-05 11:55:08 +0100370 "-C", "pctl.startup=0.0.0.0",
Olivier Deprezcd857002022-05-09 09:06:24 +0200371 "-C", "bp.secure_memory=1",
David Brazdil2df24082019-09-05 11:55:08 +0100372 "-C", "cluster0.NUM_CORES=4",
373 "-C", "cluster1.NUM_CORES=4",
374 "-C", "cache_state_modelled=0",
David Brazdil2df24082019-09-05 11:55:08 +0100375 "-C", "bp.vis.rate_limit-enable=false",
David Brazdil2df24082019-09-05 11:55:08 +0100376 "-C", "bp.pl011_uart0.untimed_fifos=1",
377 "-C", "bp.pl011_uart0.unbuffered_output=1",
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100378 "-C", f"cluster0.cpu0.RVBAR={self.CPU_START_ADDRESS}",
379 "-C", f"cluster0.cpu1.RVBAR={self.CPU_START_ADDRESS}",
380 "-C", f"cluster0.cpu2.RVBAR={self.CPU_START_ADDRESS}",
381 "-C", f"cluster0.cpu3.RVBAR={self.CPU_START_ADDRESS}",
382 "-C", f"cluster1.cpu0.RVBAR={self.CPU_START_ADDRESS}",
383 "-C", f"cluster1.cpu1.RVBAR={self.CPU_START_ADDRESS}",
384 "-C", f"cluster1.cpu2.RVBAR={self.CPU_START_ADDRESS}",
385 "-C", f"cluster1.cpu3.RVBAR={self.CPU_START_ADDRESS}",
386 "--data",
387 f"cluster0.cpu0={self.FVP_PREBUILT_BL31}@{self.CPU_START_ADDRESS}",
David Brazdil2df24082019-09-05 11:55:08 +0100388 "-C", "bp.ve_sysregs.mmbSiteDefault=0",
Raghu Krishnamurthy75ebf8c2021-11-28 07:22:12 -0800389 "-C", "cluster0.has_arm_v8-5=1",
390 "-C", "cluster1.has_arm_v8-5=1",
391 "-C", "cluster0.has_branch_target_exception=1",
392 "-C", "cluster1.has_branch_target_exception=1",
Maksims Svecovsce1261f2022-03-04 15:22:58 +0000393 "-C", "cluster0.memory_tagging_support_level=2",
394 "-C", "cluster1.memory_tagging_support_level=2",
395 "-C", "bp.dram_metadata.is_enabled=1",
Raghu Krishnamurthye2eae292022-08-10 22:38:41 -0700396 "-C", "cluster0.gicv3.extended-interrupt-range-support=1",
397 "-C", "cluster1.gicv3.extended-interrupt-range-support=1",
398 "-C", "gic_distributor.extended-ppi-count=64",
399 "-C", "gic_distributor.extended-spi-count=1024",
400 "-C", "gic_distributor.ARE-fixed-to-one=1",
David Brazdil2df24082019-09-05 11:55:08 +0100401 ]
J-Alves18a25f92021-05-04 17:47:41 +0100402
403 if uart0_log_path and uart1_log_path:
404 fvp_args += [
405 "-C", f"bp.pl011_uart0.out_file={uart0_log_path}",
406 "-C", f"bp.pl011_uart1.out_file={uart1_log_path}",
407 ]
J-Alves67c31912023-02-02 13:52:50 +0000408
409 if not show_output:
410 fvp_args += [
411 "-C", "bp.vis.disable_visualisation=true",
412 "-C", "bp.terminal_0.start_telnet=false",
413 "-C", "bp.terminal_1.start_telnet=false",
414 "-C", "bp.terminal_2.start_telnet=false",
415 "-C", "bp.terminal_3.start_telnet=false",
416 "-C", "bp.ve_sysregs.exit_on_shutdown=1",
417 ]
418
419 if debug:
420 fvp_args += [
421 "-I", "-p",
422 ]
David Brazdil2df24082019-09-05 11:55:08 +0100423 return fvp_args
424
J-Alves67c31912023-02-02 13:52:50 +0000425 def run(self, run_name, test_args, is_long_running, debug = False,
426 show_output = False):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100427 """ Run test """
David Brazdil2df24082019-09-05 11:55:08 +0100428 run_state = self.start_run(run_name)
J-Alves8cc7dbb2021-04-16 10:38:48 +0100429 dt = self.create_dt(run_name)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100430 uart0_log_path = self.create_uart_log(run_name, ".uart0.log")
431 uart1_log_path = self.create_uart_log(run_name, ".uart1.log")
David Brazdil2df24082019-09-05 11:55:08 +0100432
David Brazdil0dbb41f2019-09-09 18:03:35 +0100433 try:
J-Alves8cc7dbb2021-04-16 10:38:48 +0100434 self.gen_dts(dt, test_args)
435 self.compile_dt(run_state, dt)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100436 fvp_args = self.gen_fvp_args(is_long_running, uart0_log_path,
J-Alves67c31912023-02-02 13:52:50 +0000437 uart1_log_path, dt, debug=debug,
438 show_output=show_output)
David Brazdil0dbb41f2019-09-09 18:03:35 +0100439 self.exec_logged(run_state, fvp_args)
440 except DriverRunException:
441 pass
David Brazdil2df24082019-09-05 11:55:08 +0100442
443 # Append UART0 output to main log.
444 append_file(run_state.log_path, read_file(uart0_log_path))
David Brazdil0dbb41f2019-09-09 18:03:35 +0100445 return self.finish_run(run_state)
David Brazdil2df24082019-09-05 11:55:08 +0100446
David Brazdil94fd1e92020-02-03 16:45:20 +0000447 def finish(self):
448 """Clean up after running tests."""
449 pass
450
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100451class FvpDriverHypervisor(FvpDriver):
452 """
453 Driver which runs tests in Arm FVP emulator, with hafnium as hypervisor
454 """
455 INITRD_START= 0x84000000
Olivier Depreza516f482021-04-30 18:47:59 +0200456 INITRD_END = 0x86000000 #Default value, however may change if initrd in args
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100457
458 def __init__(self, args):
459 self.vms_in_partitions_json = args.partitions and args.partitions["VMs"]
460 super().__init__(args)
461
462 @property
463 def CPU_START_ADDRESS(self):
464 return "0x04020000"
465
466 @property
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100467 def FVP_PREBUILT_BL31(self):
Olivier Deprez0c5e7dc2023-01-17 10:29:04 +0100468 return os.path.join(FVP_PREBUILTS_TFA_ROOT, "bl31.bin")
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100469
470 @property
J-Alves38223dd2021-04-20 17:31:48 +0100471 def HYPERVISOR_ADDRESS(self):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100472 return "0x80000000"
473
J-Alves38223dd2021-04-20 17:31:48 +0100474 @property
475 def HYPERVISOR_DTB_ADDRESS(self):
476 return "0x82000000"
477
J-Alves8cc7dbb2021-04-16 10:38:48 +0100478 def gen_dts(self, dt, test_args):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100479 """Create a DeviceTree source which will be compiled into a DTB and
480 passed to FVP for a test run."""
481
482 vm_args = join_if_not_None(self.args.vm_args, test_args)
J-Alves8cc7dbb2021-04-16 10:38:48 +0100483 write_file(dt.dts, read_file(FVP_PREBUILT_DTS))
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100484
485 # Write the vm arguments to the partition manifest
486 to_append = f"""
487/ {{
488 chosen {{
489 bootargs = "{vm_args}";
490 stdout-path = "serial0:115200n8";
491 linux,initrd-start = <{self.INITRD_START if self.args.initrd else 0}>;
492 linux,initrd-end = <{self.INITRD_END if self.args.initrd else 0}>;
493 }};
494}};"""
495 if self.vms_in_partitions_json:
496 to_append += self.get_manifests_from_json(self.args.partitions["VMs"])
497
J-Alves8cc7dbb2021-04-16 10:38:48 +0100498 append_file(dt.dts, to_append)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100499
500 def gen_fvp_args(
J-Alves67c31912023-02-02 13:52:50 +0000501 self, is_long_running, uart0_log_path, uart1_log_path, dt,
502 debug = False, show_output = False):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100503 """Generate command line arguments for FVP."""
J-Alves67c31912023-02-02 13:52:50 +0000504 common_args = (self, is_long_running, uart0_log_path, uart1_log_path, dt,
505 debug, show_output)
506 fvp_args = FvpDriver.gen_fvp_args(*common_args)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100507
J-Alves8cc7dbb2021-04-16 10:38:48 +0100508 fvp_args += [
J-Alves38223dd2021-04-20 17:31:48 +0100509 "--data", f"cluster0.cpu0={dt.dtb}@{self.HYPERVISOR_DTB_ADDRESS}",
510 "--data", f"cluster0.cpu0={self.args.hypervisor}@{self.HYPERVISOR_ADDRESS}",
J-Alves8cc7dbb2021-04-16 10:38:48 +0100511 ]
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100512
513 if self.vms_in_partitions_json:
514 img_ldadd = self.get_img_and_ldadd(self.args.partitions["VMs"])
515 for img, ldadd in img_ldadd:
516 fvp_args += ["--data", f"cluster0.cpu0={img}@{hex(ldadd)}"]
517
518 if self.args.initrd:
519 fvp_args += [
520 "--data",
521 f"cluster0.cpu0={self.args.initrd}@{self.INITRD_START}"
522 ]
523 return fvp_args
524
525class FvpDriverSPMC(FvpDriver):
526 """
527 Driver which runs tests in Arm FVP emulator, with hafnium as SPMC
528 """
529 FVP_PREBUILT_SECURE_DTS = os.path.join(
530 HF_ROOT, "test", "vmapi", "fvp-base-spmc.dts")
J-Alvesacdbb862023-01-31 17:14:55 +0000531 hftest_cmd_file = tempfile.NamedTemporaryFile(mode="w+")
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100532
533 def __init__(self, args):
534 if args.partitions is None or args.partitions["SPs"] is None:
J-Alves10446d82021-04-26 11:52:57 +0100535 raise Exception("Need to provide SPs in partitions_json")
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100536 super().__init__(args)
537
538 @property
539 def CPU_START_ADDRESS(self):
540 return "0x04010000"
541
542 @property
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100543 def FVP_PREBUILT_BL31(self):
Olivier Deprez1b1c4b62023-01-17 09:56:32 +0100544 return os.path.join(FVP_PREBUILT_TFA_SPMD_ROOT, "bl31.bin")
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100545
546 @property
J-Alves38223dd2021-04-20 17:31:48 +0100547 def SPMC_ADDRESS(self):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100548 return "0x6000000"
549
J-Alves38223dd2021-04-20 17:31:48 +0100550 @property
551 def SPMC_DTB_ADDRESS(self):
552 return "0x0403f000"
553
J-Alves8cc7dbb2021-04-16 10:38:48 +0100554 def gen_dts(self, dt, test_args):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100555 """Create a DeviceTree source which will be compiled into a DTB and
556 passed to FVP for a test run."""
557 to_append = self.get_manifests_from_json(self.args.partitions["SPs"])
J-Alves8cc7dbb2021-04-16 10:38:48 +0100558 write_file(dt.dts, read_file(FvpDriverSPMC.FVP_PREBUILT_SECURE_DTS))
559 append_file(dt.dts, to_append)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100560
Shruti Gupta2b1e0cd2022-12-20 17:45:24 +0000561 def secure_ctrl_fvp_args(self, secure_ctrl):
562 fvp_args = ""
563 if secure_ctrl:
564 fvp_args = [
565 "-C", f"bp.pl011_uart0.in_file={FvpDriverSPMC.hftest_cmd_file.name}",
566 "-C", f"bp.pl011_uart0.shutdown_tag=\"{HFTEST_CTRL_FINISHED}\"",
567 ]
568 return fvp_args
569
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100570 def gen_fvp_args(
J-Alves8cc7dbb2021-04-16 10:38:48 +0100571 self, is_long_running, uart0_log_path, uart1_log_path, dt,
J-Alves67c31912023-02-02 13:52:50 +0000572 call_super = True, secure_ctrl = True, debug = False, show_output = False):
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100573 """Generate command line arguments for FVP."""
J-Alves67c31912023-02-02 13:52:50 +0000574 common_args = (self, is_long_running, uart0_log_path, uart1_log_path, dt.dtb,
575 debug, show_output)
J-Alves38223dd2021-04-20 17:31:48 +0100576 fvp_args = FvpDriver.gen_fvp_args(*common_args) if call_super else []
J-Alves8cc7dbb2021-04-16 10:38:48 +0100577
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100578 fvp_args += [
J-Alves38223dd2021-04-20 17:31:48 +0100579 "--data", f"cluster0.cpu0={dt.dtb}@{self.SPMC_DTB_ADDRESS}",
580 "--data", f"cluster0.cpu0={self.args.spmc}@{self.SPMC_ADDRESS}",
J-Alves8cc7dbb2021-04-16 10:38:48 +0100581 ]
582
Shruti Gupta2b1e0cd2022-12-20 17:45:24 +0000583 fvp_args += FvpDriverSPMC.secure_ctrl_fvp_args(self, secure_ctrl)
J-Alves18a25f92021-05-04 17:47:41 +0100584
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100585 img_ldadd = self.get_img_and_ldadd(self.args.partitions["SPs"])
586 for img, ldadd in img_ldadd:
587 fvp_args += ["--data", f"cluster0.cpu0={img}@{hex(ldadd)}"]
588
589 return fvp_args
590
J-Alves67c31912023-02-02 13:52:50 +0000591 def run(self, run_name, test_args, is_long_running, debug = False, show_output = False):
J-Alvesacdbb862023-01-31 17:14:55 +0000592 vm_args = join_if_not_None(self.args.vm_args, test_args)
593 FvpDriverSPMC.hftest_cmd_file.write(f"{vm_args}\n")
594 FvpDriverSPMC.hftest_cmd_file.seek(0)
J-Alves67c31912023-02-02 13:52:50 +0000595 return super().run(run_name, test_args, is_long_running, debug, show_output)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100596
597 def finish(self):
598 """Clean up after running tests."""
J-Alvesacdbb862023-01-31 17:14:55 +0000599 FvpDriverSPMC.hftest_cmd_file.close()
David Brazdil2df24082019-09-05 11:55:08 +0100600
J-Alves38223dd2021-04-20 17:31:48 +0100601class FvpDriverBothWorlds(FvpDriverHypervisor, FvpDriverSPMC):
602 def __init__(self, args):
603 FvpDriverHypervisor.__init__(self, args)
604 FvpDriverSPMC.__init__(self, args)
605
606 @property
607 def CPU_START_ADDRESS(self):
608 return str(0x04010000)
609
610 @property
611 def FVP_PREBUILT_BL31(self):
Olivier Deprez1b1c4b62023-01-17 09:56:32 +0100612 return str(os.path.join(FVP_PREBUILT_TFA_SPMD_ROOT, "bl31.bin"))
J-Alves38223dd2021-04-20 17:31:48 +0100613
614 def create_dt(self, run_name):
615 dt = dict()
616 dt["hypervisor"] = FvpDriver.create_dt(self, run_name + "_hypervisor")
617 dt["spmc"] = FvpDriver.create_dt(self, run_name + "_spmc")
618 return dt
619
620 @property
621 def HYPERVISOR_ADDRESS(self):
622 return "0x88000000"
623
624 @property
625 def HYPERVISOR_DTB_ADDRESS(self):
Olivier Deprezefd3c672022-02-04 09:40:36 +0100626 return "0x82000000"
J-Alves38223dd2021-04-20 17:31:48 +0100627
628 def compile_dt(self, run_state, dt):
629 FvpDriver.compile_dt(self, run_state, dt["hypervisor"])
630 FvpDriver.compile_dt(self, run_state, dt["spmc"])
631
632 def gen_dts(self, dt, test_args):
633 FvpDriverHypervisor.gen_dts(self, dt["hypervisor"], test_args)
634 FvpDriverSPMC.gen_dts(self, dt["spmc"], test_args)
635
J-Alves67c31912023-02-02 13:52:50 +0000636 def gen_fvp_args(self, is_long_running, uart0_log_path, uart1_log_path, dt,
637 debug = False, show_output = False):
638
J-Alves38223dd2021-04-20 17:31:48 +0100639 """Generate command line arguments for FVP."""
J-Alves8d9fbb92021-12-13 17:28:15 +0000640 common_args = (self, is_long_running, uart0_log_path, uart1_log_path)
J-Alves67c31912023-02-02 13:52:50 +0000641 fvp_args = FvpDriverHypervisor.gen_fvp_args(*common_args, dt["hypervisor"],
642 debug, show_output)
J-Alves18a25f92021-05-04 17:47:41 +0100643 fvp_args += FvpDriverSPMC.gen_fvp_args(*common_args, dt["spmc"], False,
644 False)
J-Alves8d9fbb92021-12-13 17:28:15 +0000645 return fvp_args
J-Alves18a25f92021-05-04 17:47:41 +0100646
J-Alves67c31912023-02-02 13:52:50 +0000647 def run(self, run_name, test_args, is_long_running, debug = False,
648 show_output = False):
649
650 return FvpDriver.run(self, run_name, test_args, is_long_running,
651 debug, show_output)
J-Alves38223dd2021-04-20 17:31:48 +0100652
653 def finish(self):
654 """Clean up after running tests."""
J-Alves8d9fbb92021-12-13 17:28:15 +0000655 FvpDriver.finish(self)
J-Alves38223dd2021-04-20 17:31:48 +0100656
Shruti Gupta2b1e0cd2022-12-20 17:45:24 +0000657class FvpDriverEL3SPMC(FvpDriverSPMC):
658 """
659 Driver which runs tests in Arm FVP emulator, with EL3 as SPMC
660 """
661
662 def __init__(self, args):
663 self.vms_in_partitions_json = args.partitions and args.partitions["SPs"]
664 self.args = args
665
666 @property
667 def CPU_START_ADDRESS(self):
668 return "0x04003000"
669
670 @property
671 def FVP_PREBUILT_BL31(self):
672 return os.path.join(FVP_PREBUILTS_TFA_EL3_SPMC_ROOT, "bl31.bin")
673
674 def sp_partition_manifest_fvp_args(self):
675 img_ldadd = self.get_img_and_ldadd(self.args.partitions["SPs"])
676
677 # Expect only one tuple with img and load address, as EL3 SPMC only supports
678 # one SP.
679 assert(len(img_ldadd) == 1)
680 img, ldadd = img_ldadd[0]
681 fvp_args = ["--data", f"cluster0.cpu0={img}@{ldadd}"]
682
683 # Even though FF-A manifest is part of the SP PKG we need to load at a specific
684 # location. Fetch the respective dtb file and load at the following address.
685 SP_DTB_ADDRESS = "0x0403f000"
686 output_path = os.path.dirname(os.path.dirname(img))
687 partition_manifest = f"{output_path}/partition-manifest.dtb"
688 fvp_args += ["--data", f"cluster0.cpu0={partition_manifest}@{SP_DTB_ADDRESS}"]
689 return fvp_args
690
691 def gen_fvp_args(
692 self, is_long_running, uart0_log_path, uart1_log_path, dt,
693 call_super = True, secure_ctrl = True, debug = False, show_output = False):
694 """Generate command line arguments for FVP."""
695 common_args = (self, is_long_running, uart0_log_path, uart1_log_path, dt.dtb,
696 debug, show_output)
697 fvp_args = FvpDriver.gen_fvp_args(*common_args) if call_super else []
698
699 fvp_args += FvpDriverSPMC.secure_ctrl_fvp_args(self, secure_ctrl)
700
701 fvp_args += self.sp_partition_manifest_fvp_args()
702
703 return fvp_args
704
David Brazdil17e76652020-01-29 14:44:19 +0000705class SerialDriver(Driver):
706 """Driver which communicates with a device over the serial port."""
707
David Brazdil9d4ed962020-02-06 17:23:48 +0000708 def __init__(self, args, tty_file, baudrate, init_wait):
David Brazdil17e76652020-01-29 14:44:19 +0000709 Driver.__init__(self, args)
David Brazdil9d4ed962020-02-06 17:23:48 +0000710 self.tty_file = tty_file
711 self.baudrate = baudrate
David Brazdil4f9cf9a2020-02-06 17:34:44 +0000712 self.pyserial = importlib.import_module("serial")
David Brazdild8013f92020-02-03 16:40:25 +0000713
David Brazdil9d4ed962020-02-06 17:23:48 +0000714 if init_wait:
David Brazdild8013f92020-02-03 16:40:25 +0000715 input("Press ENTER and then reset the device...")
David Brazdil17e76652020-01-29 14:44:19 +0000716
David Brazdil9d4ed962020-02-06 17:23:48 +0000717 def connect(self):
David Brazdil4f9cf9a2020-02-06 17:34:44 +0000718 return self.pyserial.Serial(self.tty_file, self.baudrate, timeout=10)
David Brazdil9d4ed962020-02-06 17:23:48 +0000719
David Brazdil17e76652020-01-29 14:44:19 +0000720 def run(self, run_name, test_args, is_long_running):
721 """Communicate `test_args` to the device over the serial port."""
722 run_state = self.start_run(run_name)
723
David Brazdil9d4ed962020-02-06 17:23:48 +0000724 with self.connect() as ser:
David Brazdil17e76652020-01-29 14:44:19 +0000725 with open(run_state.log_path, "a") as f:
726 while True:
727 # Read one line from the serial port.
728 line = ser.readline().decode('utf-8')
729 if len(line) == 0:
730 # Timeout
731 run_state.set_ret_code(124)
732 input("Timeout. " +
733 "Press ENTER and then reset the device...")
734 break
735 # Write the line to the log file.
736 f.write(line)
737 if HFTEST_CTRL_GET_COMMAND_LINE in line:
738 # Device is waiting for `test_args`.
739 ser.write(test_args.encode('ascii'))
740 ser.write(b'\r')
741 elif HFTEST_CTRL_FINISHED in line:
742 # Device has finished running this test and will reboot.
743 break
J-Alves18a25f92021-05-04 17:47:41 +0100744
David Brazdil17e76652020-01-29 14:44:19 +0000745 return self.finish_run(run_state)
746
David Brazdil94fd1e92020-02-03 16:45:20 +0000747 def finish(self):
748 """Clean up after running tests."""
David Brazdil9d4ed962020-02-06 17:23:48 +0000749 with self.connect() as ser:
David Brazdil94fd1e92020-02-03 16:45:20 +0000750 while True:
751 line = ser.readline().decode('utf-8')
752 if len(line) == 0:
753 input("Timeout. Press ENTER and then reset the device...")
754 elif HFTEST_CTRL_GET_COMMAND_LINE in line:
755 # Device is waiting for a command. Instruct it to exit
756 # the test environment.
757 ser.write("exit".encode('ascii'))
758 ser.write(b'\r')
759 break
760
David Brazdil2df24082019-09-05 11:55:08 +0100761# Tuple used to return information about the results of running a set of tests.
762TestRunnerResult = collections.namedtuple("TestRunnerResult", [
763 "tests_run",
764 "tests_failed",
Andrew Walbranf9463922020-06-05 16:44:42 +0100765 "tests_skipped",
David Brazdil2df24082019-09-05 11:55:08 +0100766 ])
767
David Brazdil2df24082019-09-05 11:55:08 +0100768class TestRunner:
769 """Class which communicates with a test platform to obtain a list of
770 available tests and driving their execution."""
771
J-Alves8cc7dbb2021-04-16 10:38:48 +0100772 def __init__(self, artifacts, driver, test_set_up, suite_regex, test_regex,
J-Alves67c31912023-02-02 13:52:50 +0000773 skip_long_running_tests, force_long_running, debug, show_output):
David Brazdil2df24082019-09-05 11:55:08 +0100774 self.artifacts = artifacts
775 self.driver = driver
J-Alves8cc7dbb2021-04-16 10:38:48 +0100776 self.test_set_up = test_set_up
David Brazdil3cc24aa2019-09-27 10:24:41 +0100777 self.skip_long_running_tests = skip_long_running_tests
Andrew Walbrane1fa70b2020-05-28 11:30:11 +0100778 self.force_long_running = force_long_running
J-Alves67c31912023-02-02 13:52:50 +0000779 self.debug = debug
780 self.show_output = show_output
David Brazdil2df24082019-09-05 11:55:08 +0100781
782 self.suite_re = re.compile(suite_regex or ".*")
783 self.test_re = re.compile(test_regex or ".*")
784
785 def extract_hftest_lines(self, raw):
786 """Extract hftest-specific lines from a raw output from an invocation
787 of the test platform."""
788 lines = []
J-Alves07be7bb2021-04-13 11:09:12 +0100789 lines_to_process = raw.splitlines()
790
791 try:
792 # If logs have logs of more than one VM, the loop below to extract
793 # lines won't work. Thus, extracting between starting and ending
794 # logs: HFTEST_CTRL_GET_COMMAND_LINE and HFTEST_CTRL_FINISHED.
795 hftest_start = lines_to_process.index(HFTEST_CTRL_GET_COMMAND_LINE) + 1
796 hftest_end = lines_to_process.index(HFTEST_CTRL_FINISHED)
797 except ValueError:
798 hftest_start = 0
799 hftest_end = len(lines_to_process)
800
801 lines_to_process = lines_to_process[hftest_start : hftest_end]
802
803 for line in lines_to_process:
J-Alves3dbb8562020-12-01 10:45:37 +0000804 match = re.search(f"^VM \d+: ", line)
805 if match is not None:
806 line = line[match.end():]
David Brazdil2df24082019-09-05 11:55:08 +0100807 if line.startswith(HFTEST_LOG_PREFIX):
808 lines.append(line[len(HFTEST_LOG_PREFIX):])
809 return lines
810
811 def get_test_json(self):
812 """Invoke the test platform and request a JSON of available test and
813 test suites."""
Andrew Walbrane1fa70b2020-05-28 11:30:11 +0100814 out = self.driver.run("json", "json", self.force_long_running)
David Brazdil2df24082019-09-05 11:55:08 +0100815 hf_out = "\n".join(self.extract_hftest_lines(out))
816 try:
817 return json.loads(hf_out)
818 except ValueError as e:
819 print(out)
820 raise e
821
822 def collect_results(self, fn, it, xml_node):
823 """Run `fn` on every entry in `it` and collect their TestRunnerResults.
824 Insert "tests" and "failures" nodes to `xml_node`."""
825 tests_run = 0
826 tests_failed = 0
Andrew Walbranf9463922020-06-05 16:44:42 +0100827 tests_skipped = 0
Andrew Walbran42bf2842020-06-05 18:50:19 +0100828 start_time = time.perf_counter()
David Brazdil2df24082019-09-05 11:55:08 +0100829 for i in it:
830 sub_result = fn(i)
831 assert(sub_result.tests_run >= sub_result.tests_failed)
832 tests_run += sub_result.tests_run
833 tests_failed += sub_result.tests_failed
Andrew Walbranf9463922020-06-05 16:44:42 +0100834 tests_skipped += sub_result.tests_skipped
Andrew Walbran42bf2842020-06-05 18:50:19 +0100835 elapsed_time = time.perf_counter() - start_time
David Brazdil2df24082019-09-05 11:55:08 +0100836
Andrew Walbranf9463922020-06-05 16:44:42 +0100837 xml_node.set("tests", str(tests_run + tests_skipped))
David Brazdil2df24082019-09-05 11:55:08 +0100838 xml_node.set("failures", str(tests_failed))
Andrew Walbranf9463922020-06-05 16:44:42 +0100839 xml_node.set("skipped", str(tests_skipped))
Andrew Walbran42bf2842020-06-05 18:50:19 +0100840 xml_node.set("time", str(elapsed_time))
Andrew Walbranf9463922020-06-05 16:44:42 +0100841 return TestRunnerResult(tests_run, tests_failed, tests_skipped)
David Brazdil2df24082019-09-05 11:55:08 +0100842
843 def is_passed_test(self, test_out):
844 """Parse the output of a test and return True if it passed."""
845 return \
846 len(test_out) > 0 and \
847 test_out[-1] == HFTEST_LOG_FINISHED and \
848 not any(l.startswith(HFTEST_LOG_FAILURE_PREFIX) for l in test_out)
849
Andrew Walbranf9463922020-06-05 16:44:42 +0100850 def get_failure_message(self, test_out):
851 """Parse the output of a test and return the message of the first
852 assertion failure."""
853 for i, line in enumerate(test_out):
854 if line.startswith(HFTEST_LOG_FAILURE_PREFIX) and i + 1 < len(test_out):
855 # The assertion message is on the line after the 'Failure:'
856 return test_out[i + 1].strip()
857
858 return None
859
Fuad Tabba36c8c2b2019-11-04 16:55:32 +0000860 def get_log_name(self, suite, test):
861 """Returns a string with a generated log name for the test."""
862 log_name = ""
863
864 cpu = self.driver.args.cpu
865 if cpu:
866 log_name += cpu + "."
867
868 log_name += suite["name"] + "." + test["name"]
869
870 return log_name
871
David Brazdil2df24082019-09-05 11:55:08 +0100872 def run_test(self, suite, test, suite_xml):
873 """Invoke the test platform and request to run a given `test` in given
874 `suite`. Create a new XML node with results under `suite_xml`.
875 Test only invoked if it matches the regex given to constructor."""
David Brazdil3cc24aa2019-09-27 10:24:41 +0100876 if not self.test_re.match(test["name"]):
Andrew Walbranf9463922020-06-05 16:44:42 +0100877 return TestRunnerResult(tests_run=0, tests_failed=0, tests_skipped=0)
David Brazdil2df24082019-09-05 11:55:08 +0100878
879 test_xml = ET.SubElement(suite_xml, "testcase")
David Brazdil3cc24aa2019-09-27 10:24:41 +0100880 test_xml.set("name", test["name"])
881 test_xml.set("classname", suite["name"])
Andrew Walbranf9463922020-06-05 16:44:42 +0100882
J-Alvesd459b562022-12-05 14:56:33 +0000883 if (self.skip_long_running_tests and test["is_long_running"]) or test["skip_test"]:
Andrew Walbranf9463922020-06-05 16:44:42 +0100884 print(" SKIP", test["name"])
885 test_xml.set("status", "notrun")
886 skipped_xml = ET.SubElement(test_xml, "skipped")
887 skipped_xml.set("message", "Long running")
888 return TestRunnerResult(tests_run=0, tests_failed=0, tests_skipped=1)
889
J-Alves67c31912023-02-02 13:52:50 +0000890 action_log = "DEBUG" if self.debug else "RUN"
891 print(f" {action_log}", test["name"])
Andrew Walbranf9463922020-06-05 16:44:42 +0100892 log_name = self.get_log_name(suite, test)
893
David Brazdil2df24082019-09-05 11:55:08 +0100894 test_xml.set("status", "run")
895
Andrew Walbran42bf2842020-06-05 18:50:19 +0100896 start_time = time.perf_counter()
Andrew Walbranf9463922020-06-05 16:44:42 +0100897 out = self.driver.run(
David Brazdil3cc24aa2019-09-27 10:24:41 +0100898 log_name, "run {} {}".format(suite["name"], test["name"]),
J-Alves67c31912023-02-02 13:52:50 +0000899 test["is_long_running"] or self.force_long_running,
900 self.debug, self.show_output)
901
Andrew Walbranf9463922020-06-05 16:44:42 +0100902 hftest_out = self.extract_hftest_lines(out)
Andrew Walbran42bf2842020-06-05 18:50:19 +0100903 elapsed_time = time.perf_counter() - start_time
904
905 test_xml.set("time", str(elapsed_time))
David Brazdil2df24082019-09-05 11:55:08 +0100906
Andrew Walbranf9463922020-06-05 16:44:42 +0100907 system_out_xml = ET.SubElement(test_xml, "system-out")
908 system_out_xml.text = out
909
910 if self.is_passed_test(hftest_out):
David Brazdil2df24082019-09-05 11:55:08 +0100911 print(" PASS")
Andrew Walbranf9463922020-06-05 16:44:42 +0100912 return TestRunnerResult(tests_run=1, tests_failed=0, tests_skipped=0)
David Brazdil2df24082019-09-05 11:55:08 +0100913 else:
David Brazdil623b6812019-09-09 11:41:08 +0100914 print("[x] FAIL --", self.driver.get_run_log(log_name))
David Brazdil2df24082019-09-05 11:55:08 +0100915 failure_xml = ET.SubElement(test_xml, "failure")
Andrew Walbranf9463922020-06-05 16:44:42 +0100916 failure_message = self.get_failure_message(hftest_out) or "Test failed"
917 failure_xml.set("message", failure_message)
918 failure_xml.text = '\n'.join(hftest_out)
919 return TestRunnerResult(tests_run=1, tests_failed=1, tests_skipped=0)
David Brazdil2df24082019-09-05 11:55:08 +0100920
921 def run_suite(self, suite, xml):
922 """Invoke the test platform and request to run all matching tests in
923 `suite`. Create new XML nodes with results under `xml`.
924 Suite skipped if it does not match the regex given to constructor."""
925 if not self.suite_re.match(suite["name"]):
Andrew Walbranf9463922020-06-05 16:44:42 +0100926 return TestRunnerResult(tests_run=0, tests_failed=0, tests_skipped=0)
David Brazdil2df24082019-09-05 11:55:08 +0100927
928 print(" SUITE", suite["name"])
929 suite_xml = ET.SubElement(xml, "testsuite")
930 suite_xml.set("name", suite["name"])
Andrew Walbran16ae62e2020-06-05 18:27:46 +0100931 properties_xml = ET.SubElement(suite_xml, "properties")
932
933 property_xml = ET.SubElement(properties_xml, "property")
934 property_xml.set("name", "driver")
935 property_xml.set("value", type(self.driver).__name__)
936
937 if self.driver.args.cpu:
938 property_xml = ET.SubElement(properties_xml, "property")
939 property_xml.set("name", "cpu")
940 property_xml.set("value", self.driver.args.cpu)
David Brazdil2df24082019-09-05 11:55:08 +0100941
942 return self.collect_results(
943 lambda test: self.run_test(suite, test, suite_xml),
944 suite["tests"],
945 suite_xml)
946
947 def run_tests(self):
948 """Run all suites and tests matching regexes given to constructor.
949 Write results to sponge log XML. Return the number of run and failed
950 tests."""
951
952 test_spec = self.get_test_json()
953 timestamp = datetime.datetime.now().replace(microsecond=0).isoformat()
954
955 xml = ET.Element("testsuites")
J-Alves8cc7dbb2021-04-16 10:38:48 +0100956 xml.set("name", self.test_set_up)
David Brazdil2df24082019-09-05 11:55:08 +0100957 xml.set("timestamp", timestamp)
958
959 result = self.collect_results(
960 lambda suite: self.run_suite(suite, xml),
961 test_spec["suites"],
962 xml)
963
964 # Write XML to file.
David Brazdilee5e25d2020-01-24 14:17:45 +0000965 ET.ElementTree(xml).write(self.artifacts.sponge_xml_path,
966 encoding='utf-8', xml_declaration=True)
David Brazdil2df24082019-09-05 11:55:08 +0100967
968 if result.tests_failed > 0:
969 print("[x] FAIL:", result.tests_failed, "of", result.tests_run,
970 "tests failed")
971 elif result.tests_run > 0:
972 print(" PASS: all", result.tests_run, "tests passed")
973
David Brazdil94fd1e92020-02-03 16:45:20 +0000974 # Let the driver clean up.
975 self.driver.finish()
976
David Brazdil2df24082019-09-05 11:55:08 +0100977 return result
Andrew Scullbc7189d2018-08-14 09:35:13 +0100978
Andrew Scullbc7189d2018-08-14 09:35:13 +0100979def Main():
980 parser = argparse.ArgumentParser()
J-Alves8cc7dbb2021-04-16 10:38:48 +0100981 parser.add_argument("--hypervisor")
982 parser.add_argument("--spmc")
Shruti Gupta2b1e0cd2022-12-20 17:45:24 +0000983 parser.add_argument("--el3_spmc", action="store_true")
Andrew Scull23e93a82018-10-26 14:56:04 +0100984 parser.add_argument("--log", required=True)
Andrew Walbran7559fcf2019-05-09 17:11:20 +0100985 parser.add_argument("--out_initrd")
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100986 parser.add_argument("--out_partitions")
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000987 parser.add_argument("--initrd")
Olivier Depreza6d2e6d2020-11-06 18:09:50 +0100988 parser.add_argument("--partitions_json")
Andrew Scullbc7189d2018-08-14 09:35:13 +0100989 parser.add_argument("--suite")
990 parser.add_argument("--test")
Andrew Walbranbc342d42019-02-05 16:56:02 +0000991 parser.add_argument("--vm_args")
David Brazdil17e76652020-01-29 14:44:19 +0000992 parser.add_argument("--driver", default="qemu")
993 parser.add_argument("--serial-dev", default="/dev/ttyUSB0")
994 parser.add_argument("--serial-baudrate", type=int, default=115200)
David Brazdild8013f92020-02-03 16:40:25 +0000995 parser.add_argument("--serial-no-init-wait", action="store_true")
David Brazdil3cc24aa2019-09-27 10:24:41 +0100996 parser.add_argument("--skip-long-running-tests", action="store_true")
Andrew Walbrane1fa70b2020-05-28 11:30:11 +0100997 parser.add_argument("--force-long-running", action="store_true")
J-Alves67c31912023-02-02 13:52:50 +0000998 parser.add_argument("--debug", action="store_true",
999 help="Makes platforms stall waiting for debugger connection.")
1000 parser.add_argument("--show-output", action="store_true")
Fuad Tabba36c8c2b2019-11-04 16:55:32 +00001001 parser.add_argument("--cpu",
1002 help="Selects the CPU configuration for the run environment.")
Andrew Walbranf636b842020-01-10 11:46:12 +00001003 parser.add_argument("--tfa", action="store_true")
Andrew Scullbc7189d2018-08-14 09:35:13 +01001004 args = parser.parse_args()
David Brazdil2df24082019-09-05 11:55:08 +01001005
J-Alves8cc7dbb2021-04-16 10:38:48 +01001006 # Create class which will manage all test artifacts.
1007 if args.hypervisor and args.spmc:
1008 test_set_up = "hypervisor_and_spmc"
1009 elif args.hypervisor:
1010 test_set_up = "hypervisor"
1011 elif args.spmc:
1012 test_set_up = "spmc"
Shruti Gupta2b1e0cd2022-12-20 17:45:24 +00001013 elif args.el3_spmc:
1014 test_set_up = "el3_spmc"
J-Alves8cc7dbb2021-04-16 10:38:48 +01001015 else:
1016 raise Exception("No Hafnium image provided!\n")
Olivier Depreza6d2e6d2020-11-06 18:09:50 +01001017
J-Alves8cc7dbb2021-04-16 10:38:48 +01001018 initrd = None
1019 if args.hypervisor and args.initrd:
David Brazdil0dbb41f2019-09-09 18:03:35 +01001020 initrd_dir = os.path.join(args.out_initrd, "obj", args.initrd)
1021 initrd = os.path.join(initrd_dir, "initrd.img")
J-Alves8cc7dbb2021-04-16 10:38:48 +01001022 test_set_up += "_" + args.initrd
Andrew Walbranbc342d42019-02-05 16:56:02 +00001023 vm_args = args.vm_args or ""
David Brazdil2df24082019-09-05 11:55:08 +01001024
Olivier Depreza6d2e6d2020-11-06 18:09:50 +01001025 partitions = None
J-Alves18a25f92021-05-04 17:47:41 +01001026 global_run_name = None
1027 if args.driver == "fvp":
1028 if args.partitions_json is not None:
1029 partitions_dir = os.path.join(
1030 args.out_partitions, "obj", args.partitions_json)
1031 partitions = json.load(open(partitions_dir, "r"))
1032 global_run_name = os.path.basename(args.partitions_json).split(".")[0]
1033 elif args.hypervisor:
1034 if args.initrd:
1035 global_run_name = os.path.basename(args.initrd)
1036 else:
1037 global_run_name = os.path.basename(args.hypervisor).split(".")[0]
Olivier Depreza6d2e6d2020-11-06 18:09:50 +01001038
David Brazdil2df24082019-09-05 11:55:08 +01001039 # Create class which will manage all test artifacts.
J-Alves8cc7dbb2021-04-16 10:38:48 +01001040 log_dir = os.path.join(args.log, test_set_up)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +01001041 artifacts = ArtifactsManager(log_dir)
David Brazdil2df24082019-09-05 11:55:08 +01001042
1043 # Create a driver for the platform we want to test on.
J-Alves8cc7dbb2021-04-16 10:38:48 +01001044 driver_args = DriverArgs(artifacts, args.hypervisor, args.spmc, initrd,
J-Alves18a25f92021-05-04 17:47:41 +01001045 vm_args, args.cpu, partitions, global_run_name)
David Brazdil17e76652020-01-29 14:44:19 +00001046
Shruti Gupta2b1e0cd2022-12-20 17:45:24 +00001047 if args.el3_spmc:
J-Alves38223dd2021-04-20 17:31:48 +01001048 # So far only FVP supports tests for SPMC.
Olivier Depreza6d2e6d2020-11-06 18:09:50 +01001049 if args.driver != "fvp":
1050 raise Exception("Secure tests can only run with fvp driver")
Shruti Gupta2b1e0cd2022-12-20 17:45:24 +00001051 driver = FvpDriverEL3SPMC(driver_args)
1052 elif args.spmc:
1053 # So far only FVP supports tests for SPMC.
1054 if args.driver != "fvp":
1055 raise Exception("Secure tests can only run with fvp driver")
J-Alves38223dd2021-04-20 17:31:48 +01001056 if args.hypervisor:
1057 driver = FvpDriverBothWorlds(driver_args)
1058 else:
1059 driver = FvpDriverSPMC(driver_args)
J-Alves8cc7dbb2021-04-16 10:38:48 +01001060 elif args.hypervisor:
Olivier Depreza6d2e6d2020-11-06 18:09:50 +01001061 if args.driver == "qemu":
J-Alves8cc7dbb2021-04-16 10:38:48 +01001062 out = os.path.dirname(args.hypervisor)
1063 driver = QemuDriver(driver_args, out, args.tfa)
Olivier Depreza6d2e6d2020-11-06 18:09:50 +01001064 elif args.driver == "fvp":
1065 driver = FvpDriverHypervisor(driver_args)
1066 elif args.driver == "serial":
1067 driver = SerialDriver(driver_args, args.serial_dev,
1068 args.serial_baudrate, not args.serial_no_init_wait)
1069 else:
1070 raise Exception("Unknown driver name: {}".format(args.driver))
J-Alves8cc7dbb2021-04-16 10:38:48 +01001071 else:
1072 raise Exception("No Hafnium image provided!\n")
David Brazdil2df24082019-09-05 11:55:08 +01001073
1074 # Create class which will drive test execution.
J-Alves8cc7dbb2021-04-16 10:38:48 +01001075 runner = TestRunner(artifacts, driver, test_set_up, args.suite, args.test,
J-Alves67c31912023-02-02 13:52:50 +00001076 args.skip_long_running_tests, args.force_long_running, args.debug, args.show_output)
David Brazdil2df24082019-09-05 11:55:08 +01001077
1078 # Run tests.
1079 runner_result = runner.run_tests()
1080
1081 # Print error message if no tests were run as this is probably unexpected.
1082 # Return suitable error code.
1083 if runner_result.tests_run == 0:
Andrew Scullbc7189d2018-08-14 09:35:13 +01001084 print("Error: no tests match")
1085 return 10
David Brazdil2df24082019-09-05 11:55:08 +01001086 elif runner_result.tests_failed > 0:
Andrew Scullbc7189d2018-08-14 09:35:13 +01001087 return 1
1088 else:
David Brazdil2df24082019-09-05 11:55:08 +01001089 return 0
Andrew Scullbc7189d2018-08-14 09:35:13 +01001090
1091if __name__ == "__main__":
1092 sys.exit(Main())