David Brazdil | ee5e25d | 2020-01-24 14:17:45 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 2 | # |
Andrew Walbran | 692b325 | 2019-03-07 15:51:31 +0000 | [diff] [blame] | 3 | # Copyright 2018 The Hafnium Authors. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 4 | # |
Andrew Walbran | e959ec1 | 2020-06-17 15:01:09 +0100 | [diff] [blame] | 5 | # 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 Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 8 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 9 | """Script which drives invocation of tests and parsing their output to produce |
| 10 | a results report. |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 11 | """ |
| 12 | |
| 13 | from __future__ import print_function |
| 14 | |
Andrew Scull | 3b62f2b | 2018-08-21 14:26:12 +0100 | [diff] [blame] | 15 | import xml.etree.ElementTree as ET |
| 16 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 17 | import argparse |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 18 | from abc import ABC, abstractmethod |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 19 | import collections |
Andrew Scull | 04502e4 | 2018-09-03 14:54:52 +0100 | [diff] [blame] | 20 | import datetime |
David Brazdil | 4f9cf9a | 2020-02-06 17:34:44 +0000 | [diff] [blame] | 21 | import importlib |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 22 | import json |
| 23 | import os |
| 24 | import re |
| 25 | import subprocess |
| 26 | import sys |
Andrew Walbran | 42bf284 | 2020-06-05 18:50:19 +0100 | [diff] [blame] | 27 | import time |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 28 | import fdt |
J-Alves | 18a25f9 | 2021-05-04 17:47:41 +0100 | [diff] [blame] | 29 | from telnetlib import Telnet |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 30 | |
Andrew Scull | 845fc9b | 2019-04-03 12:44:26 +0100 | [diff] [blame] | 31 | HFTEST_LOG_PREFIX = "[hftest] " |
| 32 | HFTEST_LOG_FAILURE_PREFIX = "Failure:" |
| 33 | HFTEST_LOG_FINISHED = "FINISHED" |
| 34 | |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame] | 35 | HFTEST_CTRL_GET_COMMAND_LINE = "[hftest_ctrl:get_command_line]" |
| 36 | HFTEST_CTRL_FINISHED = "[hftest_ctrl:finished]" |
| 37 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 38 | HF_ROOT = os.path.dirname(os.path.dirname(os.path.dirname( |
| 39 | os.path.abspath(__file__)))) |
David Brazdil | 5715f04 | 2019-08-27 11:11:51 +0100 | [diff] [blame] | 40 | DTC_SCRIPT = os.path.join(HF_ROOT, "build", "image", "dtc.py") |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 41 | FVP_BINARY = os.path.join( |
| 42 | os.path.dirname(HF_ROOT), "fvp", "Base_RevC_AEMv8A_pkg", "models", |
Olivier Deprez | e415304 | 2020-10-02 15:24:59 +0200 | [diff] [blame] | 43 | "Linux64_GCC-6.4", "FVP_Base_RevC-2xAEMv8A") |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 44 | HF_PREBUILTS = os.path.join(HF_ROOT, "prebuilts") |
| 45 | FVP_PREBUILTS_TFA_TRUSTY_ROOT = os.path.join( |
| 46 | HF_PREBUILTS, "linux-aarch64", "trusted-firmware-a-trusty", "fvp") |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 47 | FVP_PREBUILT_DTS = os.path.join( |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 48 | FVP_PREBUILTS_TFA_TRUSTY_ROOT, "fvp-base-gicv3-psci-1t.dts") |
| 49 | |
| 50 | FVP_PREBUILT_TFA_ROOT = os.path.join( |
| 51 | HF_PREBUILTS, "linux-aarch64", "trusted-firmware-a", "fvp") |
Andrew Scull | 845fc9b | 2019-04-03 12:44:26 +0100 | [diff] [blame] | 52 | |
J-Alves | 852fe74 | 2021-04-22 11:59:55 +0100 | [diff] [blame] | 53 | VM_NODE_REGEX = "vm[1-9]" |
| 54 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 55 | def read_file(path): |
| 56 | with open(path, "r") as f: |
| 57 | return f.read() |
Andrew Scull | 845fc9b | 2019-04-03 12:44:26 +0100 | [diff] [blame] | 58 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 59 | def write_file(path, to_write, append=False): |
| 60 | with open(path, "a" if append else "w") as f: |
| 61 | f.write(to_write) |
| 62 | |
| 63 | def append_file(path, to_write): |
| 64 | write_file(path, to_write, append=True) |
| 65 | |
| 66 | def join_if_not_None(*args): |
| 67 | return " ".join(filter(lambda x: x, args)) |
| 68 | |
J-Alves | 852fe74 | 2021-04-22 11:59:55 +0100 | [diff] [blame] | 69 | def 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 | |
| 77 | def 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-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 82 | DT = collections.namedtuple("DT", ["dts", "dtb"]) |
| 83 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 84 | class 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 Scull | 845fc9b | 2019-04-03 12:44:26 +0100 | [diff] [blame] | 92 | try: |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 93 | 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 Brazdil | 623b681 | 2019-09-09 11:41:08 +0100 | [diff] [blame] | 103 | 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 Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 107 | 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 Brazdil | 623b681 | 2019-09-09 11:41:08 +0100 | [diff] [blame] | 112 | path = self.gen_file_path(basename, extension) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 113 | |
| 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 Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 123 | |
David Brazdil | 623b681 | 2019-09-09 11:41:08 +0100 | [diff] [blame] | 124 | 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 Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 131 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 132 | # Tuple holding the arguments common to all driver constructors. |
| 133 | # This is to avoid having to pass arguments from subclasses to superclasses. |
| 134 | DriverArgs = collections.namedtuple("DriverArgs", [ |
| 135 | "artifacts", |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 136 | "hypervisor", |
| 137 | "spmc", |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 138 | "initrd", |
| 139 | "vm_args", |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame] | 140 | "cpu", |
J-Alves | 18a25f9 | 2021-05-04 17:47:41 +0100 | [diff] [blame] | 141 | "partitions", |
| 142 | "global_run_name", |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 143 | ]) |
Marc Bonnici | 0a12563 | 2019-04-01 13:46:52 +0100 | [diff] [blame] | 144 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 145 | # State shared between the common Driver class and its subclasses during |
| 146 | # a single invocation of the target platform. |
David Brazdil | 7325eaf | 2019-09-27 13:04:51 +0100 | [diff] [blame] | 147 | class DriverRunState: |
| 148 | def __init__(self, log_path): |
| 149 | self.log_path = log_path |
| 150 | self.ret_code = 0 |
Andrew Walbran | 9865625 | 2019-03-14 14:52:29 +0000 | [diff] [blame] | 151 | |
David Brazdil | 7325eaf | 2019-09-27 13:04:51 +0100 | [diff] [blame] | 152 | def set_ret_code(self, ret_code): |
| 153 | self.ret_code = ret_code |
Andrew Walbran | 9865625 | 2019-03-14 14:52:29 +0000 | [diff] [blame] | 154 | |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 155 | class 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 Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 161 | class Driver: |
| 162 | """Parent class of drivers for all testable platforms.""" |
| 163 | |
| 164 | def __init__(self, args): |
| 165 | self.args = args |
| 166 | |
David Brazdil | 623b681 | 2019-09-09 11:41:08 +0100 | [diff] [blame] | 167 | 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 Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 171 | def start_run(self, run_name): |
| 172 | """Hook called by Driver subclasses before they invoke the target |
| 173 | platform.""" |
David Brazdil | 7325eaf | 2019-09-27 13:04:51 +0100 | [diff] [blame] | 174 | return DriverRunState(self.args.artifacts.create_file(run_name, ".log")) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 175 | |
Andrew Walbran | f636b84 | 2020-01-10 11:46:12 +0000 | [diff] [blame] | 176 | def exec_logged(self, run_state, exec_args, cwd=None): |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 177 | """Run a subprocess on behalf of a Driver subclass and append its |
| 178 | stdout and stderr to the main log.""" |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 179 | assert(run_state.ret_code == 0) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 180 | with open(run_state.log_path, "a") as f: |
| 181 | f.write("$ {}\r\n".format(" ".join(exec_args))) |
| 182 | f.flush() |
Andrew Walbran | f636b84 | 2020-01-10 11:46:12 +0000 | [diff] [blame] | 183 | ret_code = subprocess.call(exec_args, stdout=f, stderr=f, cwd=cwd) |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 184 | if ret_code != 0: |
David Brazdil | 7325eaf | 2019-09-27 13:04:51 +0100 | [diff] [blame] | 185 | run_state.set_ret_code(ret_code) |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 186 | raise DriverRunException() |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 187 | |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 188 | def finish_run(self, run_state): |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 189 | """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 Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 194 | if run_state.ret_code == 124: |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 195 | f.write("\r\n{}{} timed out\r\n".format( |
| 196 | HFTEST_LOG_PREFIX, HFTEST_LOG_FAILURE_PREFIX)) |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 197 | elif run_state.ret_code != 0: |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 198 | f.write("\r\n{}{} process return code {}\r\n".format( |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 199 | HFTEST_LOG_PREFIX, HFTEST_LOG_FAILURE_PREFIX, |
| 200 | run_state.ret_code)) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 201 | |
| 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 Walbran | 9865625 | 2019-03-14 14:52:29 +0000 | [diff] [blame] | 208 | |
| 209 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 210 | class QemuDriver(Driver): |
| 211 | """Driver which runs tests in QEMU.""" |
| 212 | |
Andrew Walbran | f636b84 | 2020-01-10 11:46:12 +0000 | [diff] [blame] | 213 | def __init__(self, args, qemu_wd, tfa): |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 214 | Driver.__init__(self, args) |
Andrew Walbran | f636b84 | 2020-01-10 11:46:12 +0000 | [diff] [blame] | 215 | self.qemu_wd = qemu_wd |
| 216 | self.tfa = tfa |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 217 | |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame] | 218 | def gen_exec_args(self, test_args, is_long_running): |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 219 | """Generate command line arguments for QEMU.""" |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 220 | time_limit = "120s" if is_long_running else "10s" |
Fuad Tabba | 36c8c2b | 2019-11-04 16:55:32 +0000 | [diff] [blame] | 221 | # 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 Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 224 | exec_args = [ |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 225 | "timeout", "--foreground", time_limit, |
Andrew Walbran | f636b84 | 2020-01-10 11:46:12 +0000 | [diff] [blame] | 226 | os.path.abspath("prebuilts/linux-x64/qemu/qemu-system-aarch64"), |
Andrew Walbran | a081a29 | 2020-01-23 10:08:42 +0000 | [diff] [blame] | 227 | "-machine", "virt,virtualization=on,gic-version=3", |
Andrew Walbran | f636b84 | 2020-01-10 11:46:12 +0000 | [diff] [blame] | 228 | "-cpu", cpu, "-smp", "4", "-m", "1G", |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 229 | "-nographic", "-nodefaults", "-serial", "stdio", |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 230 | "-d", "unimp", "-kernel", os.path.abspath(self.args.hypervisor), |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 231 | ] |
| 232 | |
Andrew Walbran | f636b84 | 2020-01-10 11:46:12 +0000 | [diff] [blame] | 233 | if self.tfa: |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 234 | bl1_path = os.path.join( |
| 235 | HF_PREBUILTS, "linux-aarch64", "trusted-firmware-a-trusty", |
| 236 | "qemu", "bl1.bin") |
Andrew Walbran | f636b84 | 2020-01-10 11:46:12 +0000 | [diff] [blame] | 237 | exec_args += ["-bios", |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 238 | os.path.abspath(bl1_path), |
| 239 | "-machine", "secure=on", "-semihosting-config", |
Andrew Walbran | f636b84 | 2020-01-10 11:46:12 +0000 | [diff] [blame] | 240 | "enable,target=native"] |
| 241 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 242 | if self.args.initrd: |
Andrew Walbran | f636b84 | 2020-01-10 11:46:12 +0000 | [diff] [blame] | 243 | exec_args += ["-initrd", os.path.abspath(self.args.initrd)] |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 244 | |
| 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 Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 251 | def run(self, run_name, test_args, is_long_running): |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 252 | """Run test given by `test_args` in QEMU.""" |
| 253 | run_state = self.start_run(run_name) |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 254 | |
| 255 | try: |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 256 | # Execute test in QEMU.. |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame] | 257 | exec_args = self.gen_exec_args(test_args, is_long_running) |
Andrew Walbran | f636b84 | 2020-01-10 11:46:12 +0000 | [diff] [blame] | 258 | self.exec_logged(run_state, exec_args, |
| 259 | cwd=self.qemu_wd) |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 260 | except DriverRunException: |
| 261 | pass |
| 262 | |
| 263 | return self.finish_run(run_state) |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 264 | |
David Brazdil | 94fd1e9 | 2020-02-03 16:45:20 +0000 | [diff] [blame] | 265 | def finish(self): |
| 266 | """Clean up after running tests.""" |
| 267 | pass |
| 268 | |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 269 | class FvpDriver(Driver, ABC): |
| 270 | """Base class for driver which runs tests in Arm FVP emulator.""" |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 271 | |
| 272 | def __init__(self, args): |
Fuad Tabba | 36c8c2b | 2019-11-04 16:55:32 +0000 | [diff] [blame] | 273 | if args.cpu: |
| 274 | raise ValueError("FVP emulator does not support the --cpu option.") |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 275 | super().__init__(args) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 276 | |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 277 | @property |
| 278 | @abstractmethod |
| 279 | def CPU_START_ADDRESS(self): |
| 280 | pass |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 281 | |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 282 | @property |
| 283 | @abstractmethod |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 284 | def FVP_PREBUILT_BL31(self): |
| 285 | pass |
| 286 | |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 287 | def create_dt(self, run_name : str): |
| 288 | """Create DT related files, and return respective paths in a tuple |
| 289 | (dts,dtb)""" |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 290 | return DT(self.args.artifacts.create_file(run_name, ".dts"), |
| 291 | self.args.artifacts.create_file(run_name, ".dtb")) |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 292 | |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 293 | def compile_dt(self, run_state, dt : DT): |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 294 | """Compile DT calling dtc.""" |
| 295 | dtc_args = [ |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 296 | DTC_SCRIPT, "compile", "-i", dt.dts, "-o", dt.dtb, |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 297 | ] |
| 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-Alves | 852fe74 | 2021-04-22 11:59:55 +0100 | [diff] [blame] | 307 | 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 Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 311 | load_address = manifest.get_property("load_address", |
J-Alves | 852fe74 | 2021-04-22 11:59:55 +0100 | [diff] [blame] | 312 | f"/hypervisor/{vm_node}").value |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 313 | 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-Alves | 852fe74 | 2021-04-22 11:59:55 +0100 | [diff] [blame] | 319 | for i, p in enumerate(partitions): |
| 320 | manifests += correct_vm_node(read_file(p["dts"]), i + 1) |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 321 | return manifests |
| 322 | |
| 323 | @abstractmethod |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 324 | def gen_dts(self, dt, test_args): |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 325 | """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 Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 330 | def gen_fvp_args( |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 331 | self, is_long_running, uart0_log_path, uart1_log_path, dt): |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 332 | """Generate command line arguments for FVP.""" |
Andrew Walbran | ee5418e | 2019-11-27 17:43:05 +0000 | [diff] [blame] | 333 | time_limit = "80s" if is_long_running else "40s" |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 334 | fvp_args = [ |
Andrew Walbran | ee5418e | 2019-11-27 17:43:05 +0000 | [diff] [blame] | 335 | "timeout", "--foreground", time_limit, |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 336 | FVP_BINARY, |
J-Alves | 10446d8 | 2021-04-26 11:52:57 +0100 | [diff] [blame] | 337 | "-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 Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 345 | "-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 Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 358 | "-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 Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 368 | "-C", "bp.ve_sysregs.mmbSiteDefault=0", |
| 369 | "-C", "bp.ve_sysregs.exit_on_shutdown=1", |
| 370 | ] |
J-Alves | 18a25f9 | 2021-05-04 17:47:41 +0100 | [diff] [blame] | 371 | |
| 372 | if uart0_log_path and uart1_log_path: |
| 373 | fvp_args += [ |
| 374 | "-C", f"bp.pl011_uart0.out_file={uart0_log_path}", |
| 375 | "-C", f"bp.pl011_uart1.out_file={uart1_log_path}", |
| 376 | ] |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 377 | return fvp_args |
| 378 | |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 379 | def run(self, run_name, test_args, is_long_running): |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 380 | """ Run test """ |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 381 | run_state = self.start_run(run_name) |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 382 | dt = self.create_dt(run_name) |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 383 | uart0_log_path = self.create_uart_log(run_name, ".uart0.log") |
| 384 | uart1_log_path = self.create_uart_log(run_name, ".uart1.log") |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 385 | |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 386 | try: |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 387 | self.gen_dts(dt, test_args) |
| 388 | self.compile_dt(run_state, dt) |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 389 | fvp_args = self.gen_fvp_args(is_long_running, uart0_log_path, |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 390 | uart1_log_path, dt) |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 391 | self.exec_logged(run_state, fvp_args) |
| 392 | except DriverRunException: |
| 393 | pass |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 394 | |
| 395 | # Append UART0 output to main log. |
| 396 | append_file(run_state.log_path, read_file(uart0_log_path)) |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 397 | return self.finish_run(run_state) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 398 | |
David Brazdil | 94fd1e9 | 2020-02-03 16:45:20 +0000 | [diff] [blame] | 399 | def finish(self): |
| 400 | """Clean up after running tests.""" |
| 401 | pass |
| 402 | |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 403 | class FvpDriverHypervisor(FvpDriver): |
| 404 | """ |
| 405 | Driver which runs tests in Arm FVP emulator, with hafnium as hypervisor |
| 406 | """ |
| 407 | INITRD_START= 0x84000000 |
Olivier Deprez | a516f48 | 2021-04-30 18:47:59 +0200 | [diff] [blame] | 408 | INITRD_END = 0x86000000 #Default value, however may change if initrd in args |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 409 | |
| 410 | def __init__(self, args): |
| 411 | self.vms_in_partitions_json = args.partitions and args.partitions["VMs"] |
| 412 | super().__init__(args) |
| 413 | |
| 414 | @property |
| 415 | def CPU_START_ADDRESS(self): |
| 416 | return "0x04020000" |
| 417 | |
| 418 | @property |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 419 | def FVP_PREBUILT_BL31(self): |
| 420 | return os.path.join(FVP_PREBUILTS_TFA_TRUSTY_ROOT, "bl31.bin") |
| 421 | |
| 422 | @property |
J-Alves | 38223dd | 2021-04-20 17:31:48 +0100 | [diff] [blame] | 423 | def HYPERVISOR_ADDRESS(self): |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 424 | return "0x80000000" |
| 425 | |
J-Alves | 38223dd | 2021-04-20 17:31:48 +0100 | [diff] [blame] | 426 | @property |
| 427 | def HYPERVISOR_DTB_ADDRESS(self): |
| 428 | return "0x82000000" |
| 429 | |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 430 | def gen_dts(self, dt, test_args): |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 431 | """Create a DeviceTree source which will be compiled into a DTB and |
| 432 | passed to FVP for a test run.""" |
| 433 | |
| 434 | vm_args = join_if_not_None(self.args.vm_args, test_args) |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 435 | write_file(dt.dts, read_file(FVP_PREBUILT_DTS)) |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 436 | |
| 437 | # Write the vm arguments to the partition manifest |
| 438 | to_append = f""" |
| 439 | / {{ |
| 440 | chosen {{ |
| 441 | bootargs = "{vm_args}"; |
| 442 | stdout-path = "serial0:115200n8"; |
| 443 | linux,initrd-start = <{self.INITRD_START if self.args.initrd else 0}>; |
| 444 | linux,initrd-end = <{self.INITRD_END if self.args.initrd else 0}>; |
| 445 | }}; |
| 446 | }};""" |
| 447 | if self.vms_in_partitions_json: |
| 448 | to_append += self.get_manifests_from_json(self.args.partitions["VMs"]) |
| 449 | |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 450 | append_file(dt.dts, to_append) |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 451 | |
| 452 | def gen_fvp_args( |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 453 | self, is_long_running, uart0_log_path, uart1_log_path, dt, call_super = True): |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 454 | """Generate command line arguments for FVP.""" |
J-Alves | 38223dd | 2021-04-20 17:31:48 +0100 | [diff] [blame] | 455 | common_args = (self, is_long_running, uart0_log_path, uart1_log_path, dt) |
| 456 | fvp_args = FvpDriver.gen_fvp_args(*common_args) if call_super else [] |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 457 | |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 458 | fvp_args += [ |
J-Alves | 38223dd | 2021-04-20 17:31:48 +0100 | [diff] [blame] | 459 | "--data", f"cluster0.cpu0={dt.dtb}@{self.HYPERVISOR_DTB_ADDRESS}", |
| 460 | "--data", f"cluster0.cpu0={self.args.hypervisor}@{self.HYPERVISOR_ADDRESS}", |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 461 | ] |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 462 | |
| 463 | if self.vms_in_partitions_json: |
| 464 | img_ldadd = self.get_img_and_ldadd(self.args.partitions["VMs"]) |
| 465 | for img, ldadd in img_ldadd: |
| 466 | fvp_args += ["--data", f"cluster0.cpu0={img}@{hex(ldadd)}"] |
| 467 | |
| 468 | if self.args.initrd: |
| 469 | fvp_args += [ |
| 470 | "--data", |
| 471 | f"cluster0.cpu0={self.args.initrd}@{self.INITRD_START}" |
| 472 | ] |
| 473 | return fvp_args |
| 474 | |
| 475 | class FvpDriverSPMC(FvpDriver): |
| 476 | """ |
| 477 | Driver which runs tests in Arm FVP emulator, with hafnium as SPMC |
| 478 | """ |
| 479 | FVP_PREBUILT_SECURE_DTS = os.path.join( |
| 480 | HF_ROOT, "test", "vmapi", "fvp-base-spmc.dts") |
| 481 | HFTEST_CMD_FILE = os.path.join("/tmp/", "hftest_cmds") |
| 482 | |
| 483 | def __init__(self, args): |
| 484 | if args.partitions is None or args.partitions["SPs"] is None: |
J-Alves | 10446d8 | 2021-04-26 11:52:57 +0100 | [diff] [blame] | 485 | raise Exception("Need to provide SPs in partitions_json") |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 486 | super().__init__(args) |
| 487 | |
| 488 | @property |
| 489 | def CPU_START_ADDRESS(self): |
| 490 | return "0x04010000" |
| 491 | |
| 492 | @property |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 493 | def FVP_PREBUILT_BL31(self): |
| 494 | return os.path.join(FVP_PREBUILT_TFA_ROOT, "bl31_spmd.bin") |
| 495 | |
| 496 | @property |
J-Alves | 38223dd | 2021-04-20 17:31:48 +0100 | [diff] [blame] | 497 | def SPMC_ADDRESS(self): |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 498 | return "0x6000000" |
| 499 | |
J-Alves | 38223dd | 2021-04-20 17:31:48 +0100 | [diff] [blame] | 500 | @property |
| 501 | def SPMC_DTB_ADDRESS(self): |
| 502 | return "0x0403f000" |
| 503 | |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 504 | def gen_dts(self, dt, test_args): |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 505 | """Create a DeviceTree source which will be compiled into a DTB and |
| 506 | passed to FVP for a test run.""" |
| 507 | to_append = self.get_manifests_from_json(self.args.partitions["SPs"]) |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 508 | write_file(dt.dts, read_file(FvpDriverSPMC.FVP_PREBUILT_SECURE_DTS)) |
| 509 | append_file(dt.dts, to_append) |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 510 | |
| 511 | def gen_fvp_args( |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 512 | self, is_long_running, uart0_log_path, uart1_log_path, dt, |
J-Alves | 18a25f9 | 2021-05-04 17:47:41 +0100 | [diff] [blame] | 513 | call_super = True, secure_ctrl = True): |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 514 | """Generate command line arguments for FVP.""" |
J-Alves | 38223dd | 2021-04-20 17:31:48 +0100 | [diff] [blame] | 515 | common_args = (self, is_long_running, uart0_log_path, uart1_log_path, dt.dtb) |
| 516 | fvp_args = FvpDriver.gen_fvp_args(*common_args) if call_super else [] |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 517 | |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 518 | fvp_args += [ |
J-Alves | 38223dd | 2021-04-20 17:31:48 +0100 | [diff] [blame] | 519 | "--data", f"cluster0.cpu0={dt.dtb}@{self.SPMC_DTB_ADDRESS}", |
| 520 | "--data", f"cluster0.cpu0={self.args.spmc}@{self.SPMC_ADDRESS}", |
| 521 | "-C", "cluster0.has_arm_v8-5=1", |
| 522 | "-C", "cluster1.has_arm_v8-5=1", |
| 523 | "-C", "cluster0.has_branch_target_exception=1", |
| 524 | "-C", "cluster1.has_branch_target_exception=1", |
| 525 | "-C", "cluster0.restriction_on_speculative_execution=2", |
| 526 | "-C", "cluster1.restriction_on_speculative_execution=2", |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 527 | ] |
| 528 | |
J-Alves | 18a25f9 | 2021-05-04 17:47:41 +0100 | [diff] [blame] | 529 | if secure_ctrl: |
| 530 | fvp_args += [ |
| 531 | "-C", f"bp.pl011_uart0.in_file={FvpDriverSPMC.HFTEST_CMD_FILE}", |
| 532 | "-C", f"bp.pl011_uart0.shutdown_tag=\"{HFTEST_CTRL_FINISHED}\"", |
| 533 | ] |
| 534 | |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 535 | img_ldadd = self.get_img_and_ldadd(self.args.partitions["SPs"]) |
| 536 | for img, ldadd in img_ldadd: |
| 537 | fvp_args += ["--data", f"cluster0.cpu0={img}@{hex(ldadd)}"] |
| 538 | |
| 539 | return fvp_args |
| 540 | |
| 541 | def run(self, run_name, test_args, is_long_running): |
| 542 | with open(FvpDriverSPMC.HFTEST_CMD_FILE, "w+") as f: |
| 543 | vm_args = join_if_not_None(self.args.vm_args, test_args) |
| 544 | f.write(f"{vm_args}\n") |
| 545 | return super().run(run_name, test_args, is_long_running) |
| 546 | |
| 547 | def finish(self): |
| 548 | """Clean up after running tests.""" |
| 549 | os.remove(FvpDriverSPMC.HFTEST_CMD_FILE) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 550 | |
J-Alves | 38223dd | 2021-04-20 17:31:48 +0100 | [diff] [blame] | 551 | class FvpDriverBothWorlds(FvpDriverHypervisor, FvpDriverSPMC): |
| 552 | def __init__(self, args): |
| 553 | FvpDriverHypervisor.__init__(self, args) |
| 554 | FvpDriverSPMC.__init__(self, args) |
| 555 | |
J-Alves | 18a25f9 | 2021-05-04 17:47:41 +0100 | [diff] [blame] | 556 | # Create and build dt |
| 557 | dt = self.create_dt(args.global_run_name) |
| 558 | self.gen_dts(dt, "") |
| 559 | run_state = self.start_run(args.global_run_name + "_dt_compile") |
| 560 | self.compile_dt(run_state, dt) |
| 561 | |
| 562 | # Create file to capture model stdout and stderr |
| 563 | fvp_out = args.artifacts.create_file(args.global_run_name, ".model.log") |
| 564 | self.fvp_out_f = open(fvp_out, "a") |
| 565 | |
| 566 | # Generate the FVP model arguments |
| 567 | self.fvp_args = self.gen_fvp_args(None, None, dt) |
| 568 | |
| 569 | self.process = None |
| 570 | |
| 571 | |
J-Alves | 38223dd | 2021-04-20 17:31:48 +0100 | [diff] [blame] | 572 | @property |
| 573 | def CPU_START_ADDRESS(self): |
| 574 | return str(0x04010000) |
| 575 | |
| 576 | @property |
| 577 | def FVP_PREBUILT_BL31(self): |
| 578 | return str(os.path.join(FVP_PREBUILT_TFA_ROOT, "bl31_spmd.bin")) |
| 579 | |
| 580 | def create_dt(self, run_name): |
| 581 | dt = dict() |
| 582 | dt["hypervisor"] = FvpDriver.create_dt(self, run_name + "_hypervisor") |
| 583 | dt["spmc"] = FvpDriver.create_dt(self, run_name + "_spmc") |
| 584 | return dt |
| 585 | |
| 586 | @property |
| 587 | def HYPERVISOR_ADDRESS(self): |
| 588 | return "0x88000000" |
| 589 | |
| 590 | @property |
| 591 | def HYPERVISOR_DTB_ADDRESS(self): |
| 592 | return "0x80000000" |
| 593 | |
| 594 | def compile_dt(self, run_state, dt): |
| 595 | FvpDriver.compile_dt(self, run_state, dt["hypervisor"]) |
| 596 | FvpDriver.compile_dt(self, run_state, dt["spmc"]) |
| 597 | |
| 598 | def gen_dts(self, dt, test_args): |
| 599 | FvpDriverHypervisor.gen_dts(self, dt["hypervisor"], test_args) |
| 600 | FvpDriverSPMC.gen_dts(self, dt["spmc"], test_args) |
| 601 | |
J-Alves | 18a25f9 | 2021-05-04 17:47:41 +0100 | [diff] [blame] | 602 | def gen_fvp_args(self, uart0_log_path, uart1_log_path, dt): |
J-Alves | 38223dd | 2021-04-20 17:31:48 +0100 | [diff] [blame] | 603 | """Generate command line arguments for FVP.""" |
J-Alves | 18a25f9 | 2021-05-04 17:47:41 +0100 | [diff] [blame] | 604 | common_args = (self, None, uart0_log_path, uart1_log_path) |
| 605 | fvp_args = FvpDriverHypervisor.gen_fvp_args(*common_args, dt["hypervisor"]) |
| 606 | fvp_args += FvpDriverSPMC.gen_fvp_args(*common_args, dt["spmc"], False, |
| 607 | False) |
| 608 | # Timeout arguments are expected to be at the beggining of fvp args. |
| 609 | # With this driver, the timeouts are going to be managed via the telnet |
| 610 | # APIs. Therefore, removing from list of command arguments: |
| 611 | return fvp_args[3:] |
| 612 | |
| 613 | def process_start(self): |
| 614 | self.process = subprocess.Popen(self.fvp_args, |
| 615 | stdout = self.fvp_out_f, |
| 616 | stderr = self.fvp_out_f) |
| 617 | # Sleep 1 sec so connect to model via telnet doesn't fail |
| 618 | time.sleep(1.0) |
| 619 | |
| 620 | def process_terminate(self): |
| 621 | """ Terminate fvp model's process, and reset internal field """ |
| 622 | self.process.terminate() |
| 623 | # To give the system time to terminate the process |
| 624 | time.sleep(1.0) |
| 625 | self.process = None |
| 626 | |
| 627 | def run(self, run_name, test_args, is_long_running): |
| 628 | """ Run test """ |
| 629 | run_state = self.start_run(run_name) |
| 630 | assert(run_state.ret_code == 0) |
| 631 | |
| 632 | if self.process is None: |
| 633 | self.process_start() |
| 634 | |
| 635 | test_log = f"{' '.join(self.fvp_args)}\n" |
| 636 | |
| 637 | try: |
| 638 | with Telnet("localhost", 5000) as comm: |
| 639 | # Obtaining HFTEST_CTRL_GET_COMMAND_LINE in logs should be quick |
| 640 | test_log += comm.read_until( |
| 641 | HFTEST_CTRL_GET_COMMAND_LINE.encode("ascii"), |
| 642 | timeout=5.0).decode("ascii") |
| 643 | |
| 644 | if HFTEST_CTRL_GET_COMMAND_LINE in test_log: |
| 645 | # Send command to instruct partition to execute test |
| 646 | comm.write(f"{test_args}\n".encode("ascii")) |
| 647 | |
| 648 | timeout = 80.0 if is_long_running else 10.0 |
| 649 | test_log += comm.read_until(HFTEST_CTRL_FINISHED.encode("ascii"), |
| 650 | timeout=timeout).decode("ascii") |
| 651 | else: |
| 652 | print("VM not ready to fetch test command") |
| 653 | except ConnectionError as e: |
| 654 | self.finish() |
| 655 | raise e |
| 656 | |
| 657 | # Check wether test went well: |
| 658 | if HFTEST_CTRL_FINISHED not in test_log: |
| 659 | # Terminate process, so it is restarted on the next call to this |
| 660 | # function. In this case, the test binaries didn't reset/reboot the |
| 661 | # system for the execution of the next test. |
| 662 | self.process_terminate() |
| 663 | run_state.set_ret_code(124) |
| 664 | |
| 665 | with open(run_state.log_path, "a") as f: |
| 666 | f.write(test_log) |
| 667 | |
| 668 | return self.finish_run(run_state) |
J-Alves | 38223dd | 2021-04-20 17:31:48 +0100 | [diff] [blame] | 669 | |
| 670 | def finish(self): |
| 671 | """Clean up after running tests.""" |
J-Alves | 18a25f9 | 2021-05-04 17:47:41 +0100 | [diff] [blame] | 672 | if self.process is not None: |
| 673 | self.process_terminate() |
| 674 | self.fvp_out_f.close() |
J-Alves | 38223dd | 2021-04-20 17:31:48 +0100 | [diff] [blame] | 675 | |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame] | 676 | class SerialDriver(Driver): |
| 677 | """Driver which communicates with a device over the serial port.""" |
| 678 | |
David Brazdil | 9d4ed96 | 2020-02-06 17:23:48 +0000 | [diff] [blame] | 679 | def __init__(self, args, tty_file, baudrate, init_wait): |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame] | 680 | Driver.__init__(self, args) |
David Brazdil | 9d4ed96 | 2020-02-06 17:23:48 +0000 | [diff] [blame] | 681 | self.tty_file = tty_file |
| 682 | self.baudrate = baudrate |
David Brazdil | 4f9cf9a | 2020-02-06 17:34:44 +0000 | [diff] [blame] | 683 | self.pyserial = importlib.import_module("serial") |
David Brazdil | d8013f9 | 2020-02-03 16:40:25 +0000 | [diff] [blame] | 684 | |
David Brazdil | 9d4ed96 | 2020-02-06 17:23:48 +0000 | [diff] [blame] | 685 | if init_wait: |
David Brazdil | d8013f9 | 2020-02-03 16:40:25 +0000 | [diff] [blame] | 686 | input("Press ENTER and then reset the device...") |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame] | 687 | |
David Brazdil | 9d4ed96 | 2020-02-06 17:23:48 +0000 | [diff] [blame] | 688 | def connect(self): |
David Brazdil | 4f9cf9a | 2020-02-06 17:34:44 +0000 | [diff] [blame] | 689 | return self.pyserial.Serial(self.tty_file, self.baudrate, timeout=10) |
David Brazdil | 9d4ed96 | 2020-02-06 17:23:48 +0000 | [diff] [blame] | 690 | |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame] | 691 | def run(self, run_name, test_args, is_long_running): |
| 692 | """Communicate `test_args` to the device over the serial port.""" |
| 693 | run_state = self.start_run(run_name) |
| 694 | |
David Brazdil | 9d4ed96 | 2020-02-06 17:23:48 +0000 | [diff] [blame] | 695 | with self.connect() as ser: |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame] | 696 | with open(run_state.log_path, "a") as f: |
| 697 | while True: |
| 698 | # Read one line from the serial port. |
| 699 | line = ser.readline().decode('utf-8') |
| 700 | if len(line) == 0: |
| 701 | # Timeout |
| 702 | run_state.set_ret_code(124) |
| 703 | input("Timeout. " + |
| 704 | "Press ENTER and then reset the device...") |
| 705 | break |
| 706 | # Write the line to the log file. |
| 707 | f.write(line) |
| 708 | if HFTEST_CTRL_GET_COMMAND_LINE in line: |
| 709 | # Device is waiting for `test_args`. |
| 710 | ser.write(test_args.encode('ascii')) |
| 711 | ser.write(b'\r') |
| 712 | elif HFTEST_CTRL_FINISHED in line: |
| 713 | # Device has finished running this test and will reboot. |
| 714 | break |
J-Alves | 18a25f9 | 2021-05-04 17:47:41 +0100 | [diff] [blame] | 715 | |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame] | 716 | return self.finish_run(run_state) |
| 717 | |
David Brazdil | 94fd1e9 | 2020-02-03 16:45:20 +0000 | [diff] [blame] | 718 | def finish(self): |
| 719 | """Clean up after running tests.""" |
David Brazdil | 9d4ed96 | 2020-02-06 17:23:48 +0000 | [diff] [blame] | 720 | with self.connect() as ser: |
David Brazdil | 94fd1e9 | 2020-02-03 16:45:20 +0000 | [diff] [blame] | 721 | while True: |
| 722 | line = ser.readline().decode('utf-8') |
| 723 | if len(line) == 0: |
| 724 | input("Timeout. Press ENTER and then reset the device...") |
| 725 | elif HFTEST_CTRL_GET_COMMAND_LINE in line: |
| 726 | # Device is waiting for a command. Instruct it to exit |
| 727 | # the test environment. |
| 728 | ser.write("exit".encode('ascii')) |
| 729 | ser.write(b'\r') |
| 730 | break |
| 731 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 732 | # Tuple used to return information about the results of running a set of tests. |
| 733 | TestRunnerResult = collections.namedtuple("TestRunnerResult", [ |
| 734 | "tests_run", |
| 735 | "tests_failed", |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 736 | "tests_skipped", |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 737 | ]) |
| 738 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 739 | class TestRunner: |
| 740 | """Class which communicates with a test platform to obtain a list of |
| 741 | available tests and driving their execution.""" |
| 742 | |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 743 | def __init__(self, artifacts, driver, test_set_up, suite_regex, test_regex, |
Andrew Walbran | e1fa70b | 2020-05-28 11:30:11 +0100 | [diff] [blame] | 744 | skip_long_running_tests, force_long_running): |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 745 | self.artifacts = artifacts |
| 746 | self.driver = driver |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 747 | self.test_set_up = test_set_up |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 748 | self.skip_long_running_tests = skip_long_running_tests |
Andrew Walbran | e1fa70b | 2020-05-28 11:30:11 +0100 | [diff] [blame] | 749 | self.force_long_running = force_long_running |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 750 | |
| 751 | self.suite_re = re.compile(suite_regex or ".*") |
| 752 | self.test_re = re.compile(test_regex or ".*") |
| 753 | |
| 754 | def extract_hftest_lines(self, raw): |
| 755 | """Extract hftest-specific lines from a raw output from an invocation |
| 756 | of the test platform.""" |
| 757 | lines = [] |
J-Alves | 07be7bb | 2021-04-13 11:09:12 +0100 | [diff] [blame] | 758 | lines_to_process = raw.splitlines() |
| 759 | |
| 760 | try: |
| 761 | # If logs have logs of more than one VM, the loop below to extract |
| 762 | # lines won't work. Thus, extracting between starting and ending |
| 763 | # logs: HFTEST_CTRL_GET_COMMAND_LINE and HFTEST_CTRL_FINISHED. |
| 764 | hftest_start = lines_to_process.index(HFTEST_CTRL_GET_COMMAND_LINE) + 1 |
| 765 | hftest_end = lines_to_process.index(HFTEST_CTRL_FINISHED) |
| 766 | except ValueError: |
| 767 | hftest_start = 0 |
| 768 | hftest_end = len(lines_to_process) |
| 769 | |
| 770 | lines_to_process = lines_to_process[hftest_start : hftest_end] |
| 771 | |
| 772 | for line in lines_to_process: |
J-Alves | 3dbb856 | 2020-12-01 10:45:37 +0000 | [diff] [blame] | 773 | match = re.search(f"^VM \d+: ", line) |
| 774 | if match is not None: |
| 775 | line = line[match.end():] |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 776 | if line.startswith(HFTEST_LOG_PREFIX): |
| 777 | lines.append(line[len(HFTEST_LOG_PREFIX):]) |
| 778 | return lines |
| 779 | |
| 780 | def get_test_json(self): |
| 781 | """Invoke the test platform and request a JSON of available test and |
| 782 | test suites.""" |
Andrew Walbran | e1fa70b | 2020-05-28 11:30:11 +0100 | [diff] [blame] | 783 | out = self.driver.run("json", "json", self.force_long_running) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 784 | hf_out = "\n".join(self.extract_hftest_lines(out)) |
| 785 | try: |
| 786 | return json.loads(hf_out) |
| 787 | except ValueError as e: |
| 788 | print(out) |
| 789 | raise e |
| 790 | |
| 791 | def collect_results(self, fn, it, xml_node): |
| 792 | """Run `fn` on every entry in `it` and collect their TestRunnerResults. |
| 793 | Insert "tests" and "failures" nodes to `xml_node`.""" |
| 794 | tests_run = 0 |
| 795 | tests_failed = 0 |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 796 | tests_skipped = 0 |
Andrew Walbran | 42bf284 | 2020-06-05 18:50:19 +0100 | [diff] [blame] | 797 | start_time = time.perf_counter() |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 798 | for i in it: |
| 799 | sub_result = fn(i) |
| 800 | assert(sub_result.tests_run >= sub_result.tests_failed) |
| 801 | tests_run += sub_result.tests_run |
| 802 | tests_failed += sub_result.tests_failed |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 803 | tests_skipped += sub_result.tests_skipped |
Andrew Walbran | 42bf284 | 2020-06-05 18:50:19 +0100 | [diff] [blame] | 804 | elapsed_time = time.perf_counter() - start_time |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 805 | |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 806 | xml_node.set("tests", str(tests_run + tests_skipped)) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 807 | xml_node.set("failures", str(tests_failed)) |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 808 | xml_node.set("skipped", str(tests_skipped)) |
Andrew Walbran | 42bf284 | 2020-06-05 18:50:19 +0100 | [diff] [blame] | 809 | xml_node.set("time", str(elapsed_time)) |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 810 | return TestRunnerResult(tests_run, tests_failed, tests_skipped) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 811 | |
| 812 | def is_passed_test(self, test_out): |
| 813 | """Parse the output of a test and return True if it passed.""" |
| 814 | return \ |
| 815 | len(test_out) > 0 and \ |
| 816 | test_out[-1] == HFTEST_LOG_FINISHED and \ |
| 817 | not any(l.startswith(HFTEST_LOG_FAILURE_PREFIX) for l in test_out) |
| 818 | |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 819 | def get_failure_message(self, test_out): |
| 820 | """Parse the output of a test and return the message of the first |
| 821 | assertion failure.""" |
| 822 | for i, line in enumerate(test_out): |
| 823 | if line.startswith(HFTEST_LOG_FAILURE_PREFIX) and i + 1 < len(test_out): |
| 824 | # The assertion message is on the line after the 'Failure:' |
| 825 | return test_out[i + 1].strip() |
| 826 | |
| 827 | return None |
| 828 | |
Fuad Tabba | 36c8c2b | 2019-11-04 16:55:32 +0000 | [diff] [blame] | 829 | def get_log_name(self, suite, test): |
| 830 | """Returns a string with a generated log name for the test.""" |
| 831 | log_name = "" |
| 832 | |
| 833 | cpu = self.driver.args.cpu |
| 834 | if cpu: |
| 835 | log_name += cpu + "." |
| 836 | |
| 837 | log_name += suite["name"] + "." + test["name"] |
| 838 | |
| 839 | return log_name |
| 840 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 841 | def run_test(self, suite, test, suite_xml): |
| 842 | """Invoke the test platform and request to run a given `test` in given |
| 843 | `suite`. Create a new XML node with results under `suite_xml`. |
| 844 | Test only invoked if it matches the regex given to constructor.""" |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 845 | if not self.test_re.match(test["name"]): |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 846 | return TestRunnerResult(tests_run=0, tests_failed=0, tests_skipped=0) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 847 | |
| 848 | test_xml = ET.SubElement(suite_xml, "testcase") |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 849 | test_xml.set("name", test["name"]) |
| 850 | test_xml.set("classname", suite["name"]) |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 851 | |
| 852 | if self.skip_long_running_tests and test["is_long_running"]: |
| 853 | print(" SKIP", test["name"]) |
| 854 | test_xml.set("status", "notrun") |
| 855 | skipped_xml = ET.SubElement(test_xml, "skipped") |
| 856 | skipped_xml.set("message", "Long running") |
| 857 | return TestRunnerResult(tests_run=0, tests_failed=0, tests_skipped=1) |
| 858 | |
| 859 | print(" RUN", test["name"]) |
| 860 | log_name = self.get_log_name(suite, test) |
| 861 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 862 | test_xml.set("status", "run") |
| 863 | |
Andrew Walbran | 42bf284 | 2020-06-05 18:50:19 +0100 | [diff] [blame] | 864 | start_time = time.perf_counter() |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 865 | out = self.driver.run( |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 866 | log_name, "run {} {}".format(suite["name"], test["name"]), |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 867 | test["is_long_running"] or self.force_long_running) |
| 868 | hftest_out = self.extract_hftest_lines(out) |
Andrew Walbran | 42bf284 | 2020-06-05 18:50:19 +0100 | [diff] [blame] | 869 | elapsed_time = time.perf_counter() - start_time |
| 870 | |
| 871 | test_xml.set("time", str(elapsed_time)) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 872 | |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 873 | system_out_xml = ET.SubElement(test_xml, "system-out") |
| 874 | system_out_xml.text = out |
| 875 | |
| 876 | if self.is_passed_test(hftest_out): |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 877 | print(" PASS") |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 878 | return TestRunnerResult(tests_run=1, tests_failed=0, tests_skipped=0) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 879 | else: |
David Brazdil | 623b681 | 2019-09-09 11:41:08 +0100 | [diff] [blame] | 880 | print("[x] FAIL --", self.driver.get_run_log(log_name)) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 881 | failure_xml = ET.SubElement(test_xml, "failure") |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 882 | failure_message = self.get_failure_message(hftest_out) or "Test failed" |
| 883 | failure_xml.set("message", failure_message) |
| 884 | failure_xml.text = '\n'.join(hftest_out) |
| 885 | return TestRunnerResult(tests_run=1, tests_failed=1, tests_skipped=0) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 886 | |
| 887 | def run_suite(self, suite, xml): |
| 888 | """Invoke the test platform and request to run all matching tests in |
| 889 | `suite`. Create new XML nodes with results under `xml`. |
| 890 | Suite skipped if it does not match the regex given to constructor.""" |
| 891 | if not self.suite_re.match(suite["name"]): |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 892 | return TestRunnerResult(tests_run=0, tests_failed=0, tests_skipped=0) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 893 | |
| 894 | print(" SUITE", suite["name"]) |
| 895 | suite_xml = ET.SubElement(xml, "testsuite") |
| 896 | suite_xml.set("name", suite["name"]) |
Andrew Walbran | 16ae62e | 2020-06-05 18:27:46 +0100 | [diff] [blame] | 897 | properties_xml = ET.SubElement(suite_xml, "properties") |
| 898 | |
| 899 | property_xml = ET.SubElement(properties_xml, "property") |
| 900 | property_xml.set("name", "driver") |
| 901 | property_xml.set("value", type(self.driver).__name__) |
| 902 | |
| 903 | if self.driver.args.cpu: |
| 904 | property_xml = ET.SubElement(properties_xml, "property") |
| 905 | property_xml.set("name", "cpu") |
| 906 | property_xml.set("value", self.driver.args.cpu) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 907 | |
| 908 | return self.collect_results( |
| 909 | lambda test: self.run_test(suite, test, suite_xml), |
| 910 | suite["tests"], |
| 911 | suite_xml) |
| 912 | |
| 913 | def run_tests(self): |
| 914 | """Run all suites and tests matching regexes given to constructor. |
| 915 | Write results to sponge log XML. Return the number of run and failed |
| 916 | tests.""" |
| 917 | |
| 918 | test_spec = self.get_test_json() |
| 919 | timestamp = datetime.datetime.now().replace(microsecond=0).isoformat() |
| 920 | |
| 921 | xml = ET.Element("testsuites") |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 922 | xml.set("name", self.test_set_up) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 923 | xml.set("timestamp", timestamp) |
| 924 | |
| 925 | result = self.collect_results( |
| 926 | lambda suite: self.run_suite(suite, xml), |
| 927 | test_spec["suites"], |
| 928 | xml) |
| 929 | |
| 930 | # Write XML to file. |
David Brazdil | ee5e25d | 2020-01-24 14:17:45 +0000 | [diff] [blame] | 931 | ET.ElementTree(xml).write(self.artifacts.sponge_xml_path, |
| 932 | encoding='utf-8', xml_declaration=True) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 933 | |
| 934 | if result.tests_failed > 0: |
| 935 | print("[x] FAIL:", result.tests_failed, "of", result.tests_run, |
| 936 | "tests failed") |
| 937 | elif result.tests_run > 0: |
| 938 | print(" PASS: all", result.tests_run, "tests passed") |
| 939 | |
David Brazdil | 94fd1e9 | 2020-02-03 16:45:20 +0000 | [diff] [blame] | 940 | # Let the driver clean up. |
| 941 | self.driver.finish() |
| 942 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 943 | return result |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 944 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 945 | def Main(): |
| 946 | parser = argparse.ArgumentParser() |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 947 | parser.add_argument("--hypervisor") |
| 948 | parser.add_argument("--spmc") |
Andrew Scull | 23e93a8 | 2018-10-26 14:56:04 +0100 | [diff] [blame] | 949 | parser.add_argument("--log", required=True) |
Andrew Walbran | 7559fcf | 2019-05-09 17:11:20 +0100 | [diff] [blame] | 950 | parser.add_argument("--out_initrd") |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 951 | parser.add_argument("--out_partitions") |
Andrew Scull | 7fd4bb7 | 2018-12-08 23:40:12 +0000 | [diff] [blame] | 952 | parser.add_argument("--initrd") |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 953 | parser.add_argument("--partitions_json") |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 954 | parser.add_argument("--suite") |
| 955 | parser.add_argument("--test") |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 956 | parser.add_argument("--vm_args") |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame] | 957 | parser.add_argument("--driver", default="qemu") |
| 958 | parser.add_argument("--serial-dev", default="/dev/ttyUSB0") |
| 959 | parser.add_argument("--serial-baudrate", type=int, default=115200) |
David Brazdil | d8013f9 | 2020-02-03 16:40:25 +0000 | [diff] [blame] | 960 | parser.add_argument("--serial-no-init-wait", action="store_true") |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 961 | parser.add_argument("--skip-long-running-tests", action="store_true") |
Andrew Walbran | e1fa70b | 2020-05-28 11:30:11 +0100 | [diff] [blame] | 962 | parser.add_argument("--force-long-running", action="store_true") |
Fuad Tabba | 36c8c2b | 2019-11-04 16:55:32 +0000 | [diff] [blame] | 963 | parser.add_argument("--cpu", |
| 964 | help="Selects the CPU configuration for the run environment.") |
Andrew Walbran | f636b84 | 2020-01-10 11:46:12 +0000 | [diff] [blame] | 965 | parser.add_argument("--tfa", action="store_true") |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 966 | args = parser.parse_args() |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 967 | |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 968 | # Create class which will manage all test artifacts. |
| 969 | if args.hypervisor and args.spmc: |
| 970 | test_set_up = "hypervisor_and_spmc" |
| 971 | elif args.hypervisor: |
| 972 | test_set_up = "hypervisor" |
| 973 | elif args.spmc: |
| 974 | test_set_up = "spmc" |
| 975 | else: |
| 976 | raise Exception("No Hafnium image provided!\n") |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 977 | |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 978 | initrd = None |
| 979 | if args.hypervisor and args.initrd: |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 980 | initrd_dir = os.path.join(args.out_initrd, "obj", args.initrd) |
| 981 | initrd = os.path.join(initrd_dir, "initrd.img") |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 982 | test_set_up += "_" + args.initrd |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 983 | vm_args = args.vm_args or "" |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 984 | |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 985 | partitions = None |
J-Alves | 18a25f9 | 2021-05-04 17:47:41 +0100 | [diff] [blame] | 986 | global_run_name = None |
| 987 | if args.driver == "fvp": |
| 988 | if args.partitions_json is not None: |
| 989 | partitions_dir = os.path.join( |
| 990 | args.out_partitions, "obj", args.partitions_json) |
| 991 | partitions = json.load(open(partitions_dir, "r")) |
| 992 | global_run_name = os.path.basename(args.partitions_json).split(".")[0] |
| 993 | elif args.hypervisor: |
| 994 | if args.initrd: |
| 995 | global_run_name = os.path.basename(args.initrd) |
| 996 | else: |
| 997 | global_run_name = os.path.basename(args.hypervisor).split(".")[0] |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 998 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 999 | # Create class which will manage all test artifacts. |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 1000 | log_dir = os.path.join(args.log, test_set_up) |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 1001 | artifacts = ArtifactsManager(log_dir) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 1002 | |
| 1003 | # Create a driver for the platform we want to test on. |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 1004 | driver_args = DriverArgs(artifacts, args.hypervisor, args.spmc, initrd, |
J-Alves | 18a25f9 | 2021-05-04 17:47:41 +0100 | [diff] [blame] | 1005 | vm_args, args.cpu, partitions, global_run_name) |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame] | 1006 | |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 1007 | if args.spmc: |
J-Alves | 38223dd | 2021-04-20 17:31:48 +0100 | [diff] [blame] | 1008 | # So far only FVP supports tests for SPMC. |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 1009 | if args.driver != "fvp": |
| 1010 | raise Exception("Secure tests can only run with fvp driver") |
J-Alves | 38223dd | 2021-04-20 17:31:48 +0100 | [diff] [blame] | 1011 | |
| 1012 | if args.hypervisor: |
| 1013 | driver = FvpDriverBothWorlds(driver_args) |
| 1014 | else: |
| 1015 | driver = FvpDriverSPMC(driver_args) |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 1016 | elif args.hypervisor: |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 1017 | if args.driver == "qemu": |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 1018 | out = os.path.dirname(args.hypervisor) |
| 1019 | driver = QemuDriver(driver_args, out, args.tfa) |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 1020 | elif args.driver == "fvp": |
| 1021 | driver = FvpDriverHypervisor(driver_args) |
| 1022 | elif args.driver == "serial": |
| 1023 | driver = SerialDriver(driver_args, args.serial_dev, |
| 1024 | args.serial_baudrate, not args.serial_no_init_wait) |
| 1025 | else: |
| 1026 | raise Exception("Unknown driver name: {}".format(args.driver)) |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 1027 | else: |
| 1028 | raise Exception("No Hafnium image provided!\n") |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 1029 | |
| 1030 | # Create class which will drive test execution. |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 1031 | runner = TestRunner(artifacts, driver, test_set_up, args.suite, args.test, |
Andrew Walbran | e1fa70b | 2020-05-28 11:30:11 +0100 | [diff] [blame] | 1032 | args.skip_long_running_tests, args.force_long_running) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 1033 | |
| 1034 | # Run tests. |
| 1035 | runner_result = runner.run_tests() |
| 1036 | |
| 1037 | # Print error message if no tests were run as this is probably unexpected. |
| 1038 | # Return suitable error code. |
| 1039 | if runner_result.tests_run == 0: |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 1040 | print("Error: no tests match") |
| 1041 | return 10 |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 1042 | elif runner_result.tests_failed > 0: |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 1043 | return 1 |
| 1044 | else: |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 1045 | return 0 |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 1046 | |
| 1047 | if __name__ == "__main__": |
| 1048 | sys.exit(Main()) |