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( |
Olivier Deprez | 9f4bad4 | 2021-06-18 12:19:07 +0200 | [diff] [blame^] | 42 | os.path.dirname(HF_ROOT), "fvp", "Base_RevC_AEMvA_pkg", "models", |
| 43 | "Linux64_GCC-6.4", "FVP_Base_RevC-2xAEMvA") |
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 | ab4b2d5 | 2020-06-11 16:54:10 +0100 | [diff] [blame] | 227 | "-no-reboot", "-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 | ab4b2d5 | 2020-06-11 16:54:10 +0100 | [diff] [blame] | 240 | "enable=on,target=native"] |
Andrew Walbran | f636b84 | 2020-01-10 11:46:12 +0000 | [diff] [blame] | 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", |
Raghu Krishnamurthy | ccae47d | 2021-06-15 20:10:39 -0700 | [diff] [blame] | 370 | "-C", "cluster0.has_arm_v8-1=1", |
| 371 | "-C", "cluster1.has_arm_v8-1=1", |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 372 | ] |
J-Alves | 18a25f9 | 2021-05-04 17:47:41 +0100 | [diff] [blame] | 373 | |
| 374 | if uart0_log_path and uart1_log_path: |
| 375 | fvp_args += [ |
| 376 | "-C", f"bp.pl011_uart0.out_file={uart0_log_path}", |
| 377 | "-C", f"bp.pl011_uart1.out_file={uart1_log_path}", |
| 378 | ] |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 379 | return fvp_args |
| 380 | |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 381 | def run(self, run_name, test_args, is_long_running): |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 382 | """ Run test """ |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 383 | run_state = self.start_run(run_name) |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 384 | dt = self.create_dt(run_name) |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 385 | uart0_log_path = self.create_uart_log(run_name, ".uart0.log") |
| 386 | uart1_log_path = self.create_uart_log(run_name, ".uart1.log") |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 387 | |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 388 | try: |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 389 | self.gen_dts(dt, test_args) |
| 390 | self.compile_dt(run_state, dt) |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 391 | fvp_args = self.gen_fvp_args(is_long_running, uart0_log_path, |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 392 | uart1_log_path, dt) |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 393 | self.exec_logged(run_state, fvp_args) |
| 394 | except DriverRunException: |
| 395 | pass |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 396 | |
| 397 | # Append UART0 output to main log. |
| 398 | append_file(run_state.log_path, read_file(uart0_log_path)) |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 399 | return self.finish_run(run_state) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 400 | |
David Brazdil | 94fd1e9 | 2020-02-03 16:45:20 +0000 | [diff] [blame] | 401 | def finish(self): |
| 402 | """Clean up after running tests.""" |
| 403 | pass |
| 404 | |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 405 | class FvpDriverHypervisor(FvpDriver): |
| 406 | """ |
| 407 | Driver which runs tests in Arm FVP emulator, with hafnium as hypervisor |
| 408 | """ |
| 409 | INITRD_START= 0x84000000 |
Olivier Deprez | a516f48 | 2021-04-30 18:47:59 +0200 | [diff] [blame] | 410 | INITRD_END = 0x86000000 #Default value, however may change if initrd in args |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 411 | |
| 412 | def __init__(self, args): |
| 413 | self.vms_in_partitions_json = args.partitions and args.partitions["VMs"] |
| 414 | super().__init__(args) |
| 415 | |
| 416 | @property |
| 417 | def CPU_START_ADDRESS(self): |
| 418 | return "0x04020000" |
| 419 | |
| 420 | @property |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 421 | def FVP_PREBUILT_BL31(self): |
| 422 | return os.path.join(FVP_PREBUILTS_TFA_TRUSTY_ROOT, "bl31.bin") |
| 423 | |
| 424 | @property |
J-Alves | 38223dd | 2021-04-20 17:31:48 +0100 | [diff] [blame] | 425 | def HYPERVISOR_ADDRESS(self): |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 426 | return "0x80000000" |
| 427 | |
J-Alves | 38223dd | 2021-04-20 17:31:48 +0100 | [diff] [blame] | 428 | @property |
| 429 | def HYPERVISOR_DTB_ADDRESS(self): |
| 430 | return "0x82000000" |
| 431 | |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 432 | def gen_dts(self, dt, test_args): |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 433 | """Create a DeviceTree source which will be compiled into a DTB and |
| 434 | passed to FVP for a test run.""" |
| 435 | |
| 436 | vm_args = join_if_not_None(self.args.vm_args, test_args) |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 437 | write_file(dt.dts, read_file(FVP_PREBUILT_DTS)) |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 438 | |
| 439 | # Write the vm arguments to the partition manifest |
| 440 | to_append = f""" |
| 441 | / {{ |
| 442 | chosen {{ |
| 443 | bootargs = "{vm_args}"; |
| 444 | stdout-path = "serial0:115200n8"; |
| 445 | linux,initrd-start = <{self.INITRD_START if self.args.initrd else 0}>; |
| 446 | linux,initrd-end = <{self.INITRD_END if self.args.initrd else 0}>; |
| 447 | }}; |
| 448 | }};""" |
| 449 | if self.vms_in_partitions_json: |
| 450 | to_append += self.get_manifests_from_json(self.args.partitions["VMs"]) |
| 451 | |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 452 | append_file(dt.dts, to_append) |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 453 | |
| 454 | def gen_fvp_args( |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 455 | 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] | 456 | """Generate command line arguments for FVP.""" |
J-Alves | 38223dd | 2021-04-20 17:31:48 +0100 | [diff] [blame] | 457 | common_args = (self, is_long_running, uart0_log_path, uart1_log_path, dt) |
| 458 | fvp_args = FvpDriver.gen_fvp_args(*common_args) if call_super else [] |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 459 | |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 460 | fvp_args += [ |
J-Alves | 38223dd | 2021-04-20 17:31:48 +0100 | [diff] [blame] | 461 | "--data", f"cluster0.cpu0={dt.dtb}@{self.HYPERVISOR_DTB_ADDRESS}", |
| 462 | "--data", f"cluster0.cpu0={self.args.hypervisor}@{self.HYPERVISOR_ADDRESS}", |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 463 | ] |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 464 | |
| 465 | if self.vms_in_partitions_json: |
| 466 | img_ldadd = self.get_img_and_ldadd(self.args.partitions["VMs"]) |
| 467 | for img, ldadd in img_ldadd: |
| 468 | fvp_args += ["--data", f"cluster0.cpu0={img}@{hex(ldadd)}"] |
| 469 | |
| 470 | if self.args.initrd: |
| 471 | fvp_args += [ |
| 472 | "--data", |
| 473 | f"cluster0.cpu0={self.args.initrd}@{self.INITRD_START}" |
| 474 | ] |
| 475 | return fvp_args |
| 476 | |
| 477 | class FvpDriverSPMC(FvpDriver): |
| 478 | """ |
| 479 | Driver which runs tests in Arm FVP emulator, with hafnium as SPMC |
| 480 | """ |
| 481 | FVP_PREBUILT_SECURE_DTS = os.path.join( |
| 482 | HF_ROOT, "test", "vmapi", "fvp-base-spmc.dts") |
| 483 | HFTEST_CMD_FILE = os.path.join("/tmp/", "hftest_cmds") |
| 484 | |
| 485 | def __init__(self, args): |
| 486 | if args.partitions is None or args.partitions["SPs"] is None: |
J-Alves | 10446d8 | 2021-04-26 11:52:57 +0100 | [diff] [blame] | 487 | raise Exception("Need to provide SPs in partitions_json") |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 488 | super().__init__(args) |
| 489 | |
| 490 | @property |
| 491 | def CPU_START_ADDRESS(self): |
| 492 | return "0x04010000" |
| 493 | |
| 494 | @property |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 495 | def FVP_PREBUILT_BL31(self): |
| 496 | return os.path.join(FVP_PREBUILT_TFA_ROOT, "bl31_spmd.bin") |
| 497 | |
| 498 | @property |
J-Alves | 38223dd | 2021-04-20 17:31:48 +0100 | [diff] [blame] | 499 | def SPMC_ADDRESS(self): |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 500 | return "0x6000000" |
| 501 | |
J-Alves | 38223dd | 2021-04-20 17:31:48 +0100 | [diff] [blame] | 502 | @property |
| 503 | def SPMC_DTB_ADDRESS(self): |
| 504 | return "0x0403f000" |
| 505 | |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 506 | def gen_dts(self, dt, test_args): |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 507 | """Create a DeviceTree source which will be compiled into a DTB and |
| 508 | passed to FVP for a test run.""" |
| 509 | to_append = self.get_manifests_from_json(self.args.partitions["SPs"]) |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 510 | write_file(dt.dts, read_file(FvpDriverSPMC.FVP_PREBUILT_SECURE_DTS)) |
| 511 | append_file(dt.dts, to_append) |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 512 | |
| 513 | def gen_fvp_args( |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 514 | self, is_long_running, uart0_log_path, uart1_log_path, dt, |
J-Alves | 18a25f9 | 2021-05-04 17:47:41 +0100 | [diff] [blame] | 515 | call_super = True, secure_ctrl = True): |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 516 | """Generate command line arguments for FVP.""" |
J-Alves | 38223dd | 2021-04-20 17:31:48 +0100 | [diff] [blame] | 517 | common_args = (self, is_long_running, uart0_log_path, uart1_log_path, dt.dtb) |
| 518 | fvp_args = FvpDriver.gen_fvp_args(*common_args) if call_super else [] |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 519 | |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 520 | fvp_args += [ |
J-Alves | 38223dd | 2021-04-20 17:31:48 +0100 | [diff] [blame] | 521 | "--data", f"cluster0.cpu0={dt.dtb}@{self.SPMC_DTB_ADDRESS}", |
| 522 | "--data", f"cluster0.cpu0={self.args.spmc}@{self.SPMC_ADDRESS}", |
| 523 | "-C", "cluster0.has_arm_v8-5=1", |
| 524 | "-C", "cluster1.has_arm_v8-5=1", |
| 525 | "-C", "cluster0.has_branch_target_exception=1", |
| 526 | "-C", "cluster1.has_branch_target_exception=1", |
| 527 | "-C", "cluster0.restriction_on_speculative_execution=2", |
| 528 | "-C", "cluster1.restriction_on_speculative_execution=2", |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 529 | ] |
| 530 | |
J-Alves | 18a25f9 | 2021-05-04 17:47:41 +0100 | [diff] [blame] | 531 | if secure_ctrl: |
| 532 | fvp_args += [ |
| 533 | "-C", f"bp.pl011_uart0.in_file={FvpDriverSPMC.HFTEST_CMD_FILE}", |
| 534 | "-C", f"bp.pl011_uart0.shutdown_tag=\"{HFTEST_CTRL_FINISHED}\"", |
| 535 | ] |
| 536 | |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 537 | img_ldadd = self.get_img_and_ldadd(self.args.partitions["SPs"]) |
| 538 | for img, ldadd in img_ldadd: |
| 539 | fvp_args += ["--data", f"cluster0.cpu0={img}@{hex(ldadd)}"] |
| 540 | |
| 541 | return fvp_args |
| 542 | |
| 543 | def run(self, run_name, test_args, is_long_running): |
| 544 | with open(FvpDriverSPMC.HFTEST_CMD_FILE, "w+") as f: |
| 545 | vm_args = join_if_not_None(self.args.vm_args, test_args) |
| 546 | f.write(f"{vm_args}\n") |
| 547 | return super().run(run_name, test_args, is_long_running) |
| 548 | |
| 549 | def finish(self): |
| 550 | """Clean up after running tests.""" |
| 551 | os.remove(FvpDriverSPMC.HFTEST_CMD_FILE) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 552 | |
J-Alves | 38223dd | 2021-04-20 17:31:48 +0100 | [diff] [blame] | 553 | class FvpDriverBothWorlds(FvpDriverHypervisor, FvpDriverSPMC): |
| 554 | def __init__(self, args): |
| 555 | FvpDriverHypervisor.__init__(self, args) |
| 556 | FvpDriverSPMC.__init__(self, args) |
| 557 | |
J-Alves | 18a25f9 | 2021-05-04 17:47:41 +0100 | [diff] [blame] | 558 | # Create and build dt |
| 559 | dt = self.create_dt(args.global_run_name) |
| 560 | self.gen_dts(dt, "") |
| 561 | run_state = self.start_run(args.global_run_name + "_dt_compile") |
| 562 | self.compile_dt(run_state, dt) |
| 563 | |
| 564 | # Create file to capture model stdout and stderr |
Federico Recanati | 266bde4 | 2021-11-12 17:16:43 +0100 | [diff] [blame] | 565 | self.fvp_out_filename = args.artifacts.create_file( |
| 566 | args.global_run_name, ".model.log") |
| 567 | self.fvp_out_f = open(self.fvp_out_filename, "a") |
J-Alves | 18a25f9 | 2021-05-04 17:47:41 +0100 | [diff] [blame] | 568 | |
| 569 | # Generate the FVP model arguments |
| 570 | self.fvp_args = self.gen_fvp_args(None, None, dt) |
| 571 | |
| 572 | self.process = None |
| 573 | |
| 574 | |
J-Alves | 38223dd | 2021-04-20 17:31:48 +0100 | [diff] [blame] | 575 | @property |
| 576 | def CPU_START_ADDRESS(self): |
| 577 | return str(0x04010000) |
| 578 | |
| 579 | @property |
| 580 | def FVP_PREBUILT_BL31(self): |
| 581 | return str(os.path.join(FVP_PREBUILT_TFA_ROOT, "bl31_spmd.bin")) |
| 582 | |
| 583 | def create_dt(self, run_name): |
| 584 | dt = dict() |
| 585 | dt["hypervisor"] = FvpDriver.create_dt(self, run_name + "_hypervisor") |
| 586 | dt["spmc"] = FvpDriver.create_dt(self, run_name + "_spmc") |
| 587 | return dt |
| 588 | |
| 589 | @property |
| 590 | def HYPERVISOR_ADDRESS(self): |
| 591 | return "0x88000000" |
| 592 | |
| 593 | @property |
| 594 | def HYPERVISOR_DTB_ADDRESS(self): |
| 595 | return "0x80000000" |
| 596 | |
| 597 | def compile_dt(self, run_state, dt): |
| 598 | FvpDriver.compile_dt(self, run_state, dt["hypervisor"]) |
| 599 | FvpDriver.compile_dt(self, run_state, dt["spmc"]) |
| 600 | |
| 601 | def gen_dts(self, dt, test_args): |
| 602 | FvpDriverHypervisor.gen_dts(self, dt["hypervisor"], test_args) |
| 603 | FvpDriverSPMC.gen_dts(self, dt["spmc"], test_args) |
| 604 | |
J-Alves | 18a25f9 | 2021-05-04 17:47:41 +0100 | [diff] [blame] | 605 | def gen_fvp_args(self, uart0_log_path, uart1_log_path, dt): |
J-Alves | 38223dd | 2021-04-20 17:31:48 +0100 | [diff] [blame] | 606 | """Generate command line arguments for FVP.""" |
J-Alves | 18a25f9 | 2021-05-04 17:47:41 +0100 | [diff] [blame] | 607 | common_args = (self, None, uart0_log_path, uart1_log_path) |
| 608 | fvp_args = FvpDriverHypervisor.gen_fvp_args(*common_args, dt["hypervisor"]) |
| 609 | fvp_args += FvpDriverSPMC.gen_fvp_args(*common_args, dt["spmc"], False, |
| 610 | False) |
| 611 | # Timeout arguments are expected to be at the beggining of fvp args. |
| 612 | # With this driver, the timeouts are going to be managed via the telnet |
| 613 | # APIs. Therefore, removing from list of command arguments: |
| 614 | return fvp_args[3:] |
| 615 | |
Federico Recanati | 266bde4 | 2021-11-12 17:16:43 +0100 | [diff] [blame] | 616 | def get_telnet_port(self): |
| 617 | # Get telnet port by parsing log file, default to 5000 |
| 618 | self.fvp_out_f.flush() |
| 619 | log = read_file(self.fvp_out_filename) |
| 620 | port = re.match( |
| 621 | 'terminal_0: Listening for serial connection on port ([0-9]+)', log) |
| 622 | return port.group(1) if port else 5000 |
| 623 | |
J-Alves | 18a25f9 | 2021-05-04 17:47:41 +0100 | [diff] [blame] | 624 | def process_start(self): |
| 625 | self.process = subprocess.Popen(self.fvp_args, |
| 626 | stdout = self.fvp_out_f, |
| 627 | stderr = self.fvp_out_f) |
| 628 | # Sleep 1 sec so connect to model via telnet doesn't fail |
| 629 | time.sleep(1.0) |
Federico Recanati | d4a6a18 | 2021-11-12 15:51:12 +0100 | [diff] [blame] | 630 | # Start telnet session |
| 631 | try: |
Federico Recanati | 266bde4 | 2021-11-12 17:16:43 +0100 | [diff] [blame] | 632 | self.comm = Telnet("localhost", self.get_telnet_port()) |
Federico Recanati | d4a6a18 | 2021-11-12 15:51:12 +0100 | [diff] [blame] | 633 | except ConnectionError as e: |
| 634 | self.finish() |
| 635 | raise e |
| 636 | |
J-Alves | 18a25f9 | 2021-05-04 17:47:41 +0100 | [diff] [blame] | 637 | |
| 638 | def process_terminate(self): |
| 639 | """ Terminate fvp model's process, and reset internal field """ |
Federico Recanati | d4a6a18 | 2021-11-12 15:51:12 +0100 | [diff] [blame] | 640 | self.comm.close() # Close telnet connection |
J-Alves | 18a25f9 | 2021-05-04 17:47:41 +0100 | [diff] [blame] | 641 | self.process.terminate() |
| 642 | # To give the system time to terminate the process |
| 643 | time.sleep(1.0) |
| 644 | self.process = None |
| 645 | |
| 646 | def run(self, run_name, test_args, is_long_running): |
| 647 | """ Run test """ |
| 648 | run_state = self.start_run(run_name) |
| 649 | assert(run_state.ret_code == 0) |
| 650 | |
| 651 | if self.process is None: |
| 652 | self.process_start() |
| 653 | |
| 654 | test_log = f"{' '.join(self.fvp_args)}\n" |
| 655 | |
Federico Recanati | d4a6a18 | 2021-11-12 15:51:12 +0100 | [diff] [blame] | 656 | # Obtaining HFTEST_CTRL_GET_COMMAND_LINE in logs should be quick |
| 657 | test_log += self.comm.read_until( |
| 658 | HFTEST_CTRL_GET_COMMAND_LINE.encode("ascii"), |
| 659 | timeout=5.0).decode("ascii") |
J-Alves | 18a25f9 | 2021-05-04 17:47:41 +0100 | [diff] [blame] | 660 | |
Federico Recanati | d4a6a18 | 2021-11-12 15:51:12 +0100 | [diff] [blame] | 661 | if HFTEST_CTRL_GET_COMMAND_LINE in test_log: |
| 662 | # Send self.command to instruct partition to execute test |
| 663 | self.comm.write(f"{test_args}\n".encode("ascii")) |
J-Alves | 18a25f9 | 2021-05-04 17:47:41 +0100 | [diff] [blame] | 664 | |
Federico Recanati | d4a6a18 | 2021-11-12 15:51:12 +0100 | [diff] [blame] | 665 | timeout = 80.0 if is_long_running else 10.0 |
| 666 | test_log += self.comm.read_until(HFTEST_CTRL_FINISHED.encode("ascii"), |
| 667 | timeout=timeout).decode("ascii") |
| 668 | else: |
| 669 | print("VM not ready to fetch test command") |
J-Alves | 18a25f9 | 2021-05-04 17:47:41 +0100 | [diff] [blame] | 670 | |
| 671 | # Check wether test went well: |
| 672 | if HFTEST_CTRL_FINISHED not in test_log: |
| 673 | # Terminate process, so it is restarted on the next call to this |
| 674 | # function. In this case, the test binaries didn't reset/reboot the |
| 675 | # system for the execution of the next test. |
| 676 | self.process_terminate() |
| 677 | run_state.set_ret_code(124) |
| 678 | |
| 679 | with open(run_state.log_path, "a") as f: |
| 680 | f.write(test_log) |
| 681 | |
| 682 | return self.finish_run(run_state) |
J-Alves | 38223dd | 2021-04-20 17:31:48 +0100 | [diff] [blame] | 683 | |
| 684 | def finish(self): |
| 685 | """Clean up after running tests.""" |
J-Alves | 18a25f9 | 2021-05-04 17:47:41 +0100 | [diff] [blame] | 686 | if self.process is not None: |
| 687 | self.process_terminate() |
| 688 | self.fvp_out_f.close() |
J-Alves | 38223dd | 2021-04-20 17:31:48 +0100 | [diff] [blame] | 689 | |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame] | 690 | class SerialDriver(Driver): |
| 691 | """Driver which communicates with a device over the serial port.""" |
| 692 | |
David Brazdil | 9d4ed96 | 2020-02-06 17:23:48 +0000 | [diff] [blame] | 693 | def __init__(self, args, tty_file, baudrate, init_wait): |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame] | 694 | Driver.__init__(self, args) |
David Brazdil | 9d4ed96 | 2020-02-06 17:23:48 +0000 | [diff] [blame] | 695 | self.tty_file = tty_file |
| 696 | self.baudrate = baudrate |
David Brazdil | 4f9cf9a | 2020-02-06 17:34:44 +0000 | [diff] [blame] | 697 | self.pyserial = importlib.import_module("serial") |
David Brazdil | d8013f9 | 2020-02-03 16:40:25 +0000 | [diff] [blame] | 698 | |
David Brazdil | 9d4ed96 | 2020-02-06 17:23:48 +0000 | [diff] [blame] | 699 | if init_wait: |
David Brazdil | d8013f9 | 2020-02-03 16:40:25 +0000 | [diff] [blame] | 700 | input("Press ENTER and then reset the device...") |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame] | 701 | |
David Brazdil | 9d4ed96 | 2020-02-06 17:23:48 +0000 | [diff] [blame] | 702 | def connect(self): |
David Brazdil | 4f9cf9a | 2020-02-06 17:34:44 +0000 | [diff] [blame] | 703 | return self.pyserial.Serial(self.tty_file, self.baudrate, timeout=10) |
David Brazdil | 9d4ed96 | 2020-02-06 17:23:48 +0000 | [diff] [blame] | 704 | |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame] | 705 | def run(self, run_name, test_args, is_long_running): |
| 706 | """Communicate `test_args` to the device over the serial port.""" |
| 707 | run_state = self.start_run(run_name) |
| 708 | |
David Brazdil | 9d4ed96 | 2020-02-06 17:23:48 +0000 | [diff] [blame] | 709 | with self.connect() as ser: |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame] | 710 | with open(run_state.log_path, "a") as f: |
| 711 | while True: |
| 712 | # Read one line from the serial port. |
| 713 | line = ser.readline().decode('utf-8') |
| 714 | if len(line) == 0: |
| 715 | # Timeout |
| 716 | run_state.set_ret_code(124) |
| 717 | input("Timeout. " + |
| 718 | "Press ENTER and then reset the device...") |
| 719 | break |
| 720 | # Write the line to the log file. |
| 721 | f.write(line) |
| 722 | if HFTEST_CTRL_GET_COMMAND_LINE in line: |
| 723 | # Device is waiting for `test_args`. |
| 724 | ser.write(test_args.encode('ascii')) |
| 725 | ser.write(b'\r') |
| 726 | elif HFTEST_CTRL_FINISHED in line: |
| 727 | # Device has finished running this test and will reboot. |
| 728 | break |
J-Alves | 18a25f9 | 2021-05-04 17:47:41 +0100 | [diff] [blame] | 729 | |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame] | 730 | return self.finish_run(run_state) |
| 731 | |
David Brazdil | 94fd1e9 | 2020-02-03 16:45:20 +0000 | [diff] [blame] | 732 | def finish(self): |
| 733 | """Clean up after running tests.""" |
David Brazdil | 9d4ed96 | 2020-02-06 17:23:48 +0000 | [diff] [blame] | 734 | with self.connect() as ser: |
David Brazdil | 94fd1e9 | 2020-02-03 16:45:20 +0000 | [diff] [blame] | 735 | while True: |
| 736 | line = ser.readline().decode('utf-8') |
| 737 | if len(line) == 0: |
| 738 | input("Timeout. Press ENTER and then reset the device...") |
| 739 | elif HFTEST_CTRL_GET_COMMAND_LINE in line: |
| 740 | # Device is waiting for a command. Instruct it to exit |
| 741 | # the test environment. |
| 742 | ser.write("exit".encode('ascii')) |
| 743 | ser.write(b'\r') |
| 744 | break |
| 745 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 746 | # Tuple used to return information about the results of running a set of tests. |
| 747 | TestRunnerResult = collections.namedtuple("TestRunnerResult", [ |
| 748 | "tests_run", |
| 749 | "tests_failed", |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 750 | "tests_skipped", |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 751 | ]) |
| 752 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 753 | class TestRunner: |
| 754 | """Class which communicates with a test platform to obtain a list of |
| 755 | available tests and driving their execution.""" |
| 756 | |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 757 | def __init__(self, artifacts, driver, test_set_up, suite_regex, test_regex, |
Andrew Walbran | e1fa70b | 2020-05-28 11:30:11 +0100 | [diff] [blame] | 758 | skip_long_running_tests, force_long_running): |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 759 | self.artifacts = artifacts |
| 760 | self.driver = driver |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 761 | self.test_set_up = test_set_up |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 762 | self.skip_long_running_tests = skip_long_running_tests |
Andrew Walbran | e1fa70b | 2020-05-28 11:30:11 +0100 | [diff] [blame] | 763 | self.force_long_running = force_long_running |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 764 | |
| 765 | self.suite_re = re.compile(suite_regex or ".*") |
| 766 | self.test_re = re.compile(test_regex or ".*") |
| 767 | |
| 768 | def extract_hftest_lines(self, raw): |
| 769 | """Extract hftest-specific lines from a raw output from an invocation |
| 770 | of the test platform.""" |
| 771 | lines = [] |
J-Alves | 07be7bb | 2021-04-13 11:09:12 +0100 | [diff] [blame] | 772 | lines_to_process = raw.splitlines() |
| 773 | |
| 774 | try: |
| 775 | # If logs have logs of more than one VM, the loop below to extract |
| 776 | # lines won't work. Thus, extracting between starting and ending |
| 777 | # logs: HFTEST_CTRL_GET_COMMAND_LINE and HFTEST_CTRL_FINISHED. |
| 778 | hftest_start = lines_to_process.index(HFTEST_CTRL_GET_COMMAND_LINE) + 1 |
| 779 | hftest_end = lines_to_process.index(HFTEST_CTRL_FINISHED) |
| 780 | except ValueError: |
| 781 | hftest_start = 0 |
| 782 | hftest_end = len(lines_to_process) |
| 783 | |
| 784 | lines_to_process = lines_to_process[hftest_start : hftest_end] |
| 785 | |
| 786 | for line in lines_to_process: |
J-Alves | 3dbb856 | 2020-12-01 10:45:37 +0000 | [diff] [blame] | 787 | match = re.search(f"^VM \d+: ", line) |
| 788 | if match is not None: |
| 789 | line = line[match.end():] |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 790 | if line.startswith(HFTEST_LOG_PREFIX): |
| 791 | lines.append(line[len(HFTEST_LOG_PREFIX):]) |
| 792 | return lines |
| 793 | |
| 794 | def get_test_json(self): |
| 795 | """Invoke the test platform and request a JSON of available test and |
| 796 | test suites.""" |
Andrew Walbran | e1fa70b | 2020-05-28 11:30:11 +0100 | [diff] [blame] | 797 | out = self.driver.run("json", "json", self.force_long_running) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 798 | hf_out = "\n".join(self.extract_hftest_lines(out)) |
| 799 | try: |
| 800 | return json.loads(hf_out) |
| 801 | except ValueError as e: |
| 802 | print(out) |
| 803 | raise e |
| 804 | |
| 805 | def collect_results(self, fn, it, xml_node): |
| 806 | """Run `fn` on every entry in `it` and collect their TestRunnerResults. |
| 807 | Insert "tests" and "failures" nodes to `xml_node`.""" |
| 808 | tests_run = 0 |
| 809 | tests_failed = 0 |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 810 | tests_skipped = 0 |
Andrew Walbran | 42bf284 | 2020-06-05 18:50:19 +0100 | [diff] [blame] | 811 | start_time = time.perf_counter() |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 812 | for i in it: |
| 813 | sub_result = fn(i) |
| 814 | assert(sub_result.tests_run >= sub_result.tests_failed) |
| 815 | tests_run += sub_result.tests_run |
| 816 | tests_failed += sub_result.tests_failed |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 817 | tests_skipped += sub_result.tests_skipped |
Andrew Walbran | 42bf284 | 2020-06-05 18:50:19 +0100 | [diff] [blame] | 818 | elapsed_time = time.perf_counter() - start_time |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 819 | |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 820 | xml_node.set("tests", str(tests_run + tests_skipped)) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 821 | xml_node.set("failures", str(tests_failed)) |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 822 | xml_node.set("skipped", str(tests_skipped)) |
Andrew Walbran | 42bf284 | 2020-06-05 18:50:19 +0100 | [diff] [blame] | 823 | xml_node.set("time", str(elapsed_time)) |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 824 | return TestRunnerResult(tests_run, tests_failed, tests_skipped) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 825 | |
| 826 | def is_passed_test(self, test_out): |
| 827 | """Parse the output of a test and return True if it passed.""" |
| 828 | return \ |
| 829 | len(test_out) > 0 and \ |
| 830 | test_out[-1] == HFTEST_LOG_FINISHED and \ |
| 831 | not any(l.startswith(HFTEST_LOG_FAILURE_PREFIX) for l in test_out) |
| 832 | |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 833 | def get_failure_message(self, test_out): |
| 834 | """Parse the output of a test and return the message of the first |
| 835 | assertion failure.""" |
| 836 | for i, line in enumerate(test_out): |
| 837 | if line.startswith(HFTEST_LOG_FAILURE_PREFIX) and i + 1 < len(test_out): |
| 838 | # The assertion message is on the line after the 'Failure:' |
| 839 | return test_out[i + 1].strip() |
| 840 | |
| 841 | return None |
| 842 | |
Fuad Tabba | 36c8c2b | 2019-11-04 16:55:32 +0000 | [diff] [blame] | 843 | def get_log_name(self, suite, test): |
| 844 | """Returns a string with a generated log name for the test.""" |
| 845 | log_name = "" |
| 846 | |
| 847 | cpu = self.driver.args.cpu |
| 848 | if cpu: |
| 849 | log_name += cpu + "." |
| 850 | |
| 851 | log_name += suite["name"] + "." + test["name"] |
| 852 | |
| 853 | return log_name |
| 854 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 855 | def run_test(self, suite, test, suite_xml): |
| 856 | """Invoke the test platform and request to run a given `test` in given |
| 857 | `suite`. Create a new XML node with results under `suite_xml`. |
| 858 | Test only invoked if it matches the regex given to constructor.""" |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 859 | if not self.test_re.match(test["name"]): |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 860 | return TestRunnerResult(tests_run=0, tests_failed=0, tests_skipped=0) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 861 | |
| 862 | test_xml = ET.SubElement(suite_xml, "testcase") |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 863 | test_xml.set("name", test["name"]) |
| 864 | test_xml.set("classname", suite["name"]) |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 865 | |
| 866 | if self.skip_long_running_tests and test["is_long_running"]: |
| 867 | print(" SKIP", test["name"]) |
| 868 | test_xml.set("status", "notrun") |
| 869 | skipped_xml = ET.SubElement(test_xml, "skipped") |
| 870 | skipped_xml.set("message", "Long running") |
| 871 | return TestRunnerResult(tests_run=0, tests_failed=0, tests_skipped=1) |
| 872 | |
| 873 | print(" RUN", test["name"]) |
| 874 | log_name = self.get_log_name(suite, test) |
| 875 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 876 | test_xml.set("status", "run") |
| 877 | |
Andrew Walbran | 42bf284 | 2020-06-05 18:50:19 +0100 | [diff] [blame] | 878 | start_time = time.perf_counter() |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 879 | out = self.driver.run( |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 880 | log_name, "run {} {}".format(suite["name"], test["name"]), |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 881 | test["is_long_running"] or self.force_long_running) |
| 882 | hftest_out = self.extract_hftest_lines(out) |
Andrew Walbran | 42bf284 | 2020-06-05 18:50:19 +0100 | [diff] [blame] | 883 | elapsed_time = time.perf_counter() - start_time |
| 884 | |
| 885 | test_xml.set("time", str(elapsed_time)) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 886 | |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 887 | system_out_xml = ET.SubElement(test_xml, "system-out") |
| 888 | system_out_xml.text = out |
| 889 | |
| 890 | if self.is_passed_test(hftest_out): |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 891 | print(" PASS") |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 892 | return TestRunnerResult(tests_run=1, tests_failed=0, tests_skipped=0) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 893 | else: |
David Brazdil | 623b681 | 2019-09-09 11:41:08 +0100 | [diff] [blame] | 894 | print("[x] FAIL --", self.driver.get_run_log(log_name)) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 895 | failure_xml = ET.SubElement(test_xml, "failure") |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 896 | failure_message = self.get_failure_message(hftest_out) or "Test failed" |
| 897 | failure_xml.set("message", failure_message) |
| 898 | failure_xml.text = '\n'.join(hftest_out) |
| 899 | return TestRunnerResult(tests_run=1, tests_failed=1, tests_skipped=0) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 900 | |
| 901 | def run_suite(self, suite, xml): |
| 902 | """Invoke the test platform and request to run all matching tests in |
| 903 | `suite`. Create new XML nodes with results under `xml`. |
| 904 | Suite skipped if it does not match the regex given to constructor.""" |
| 905 | if not self.suite_re.match(suite["name"]): |
Andrew Walbran | f946392 | 2020-06-05 16:44:42 +0100 | [diff] [blame] | 906 | return TestRunnerResult(tests_run=0, tests_failed=0, tests_skipped=0) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 907 | |
| 908 | print(" SUITE", suite["name"]) |
| 909 | suite_xml = ET.SubElement(xml, "testsuite") |
| 910 | suite_xml.set("name", suite["name"]) |
Andrew Walbran | 16ae62e | 2020-06-05 18:27:46 +0100 | [diff] [blame] | 911 | properties_xml = ET.SubElement(suite_xml, "properties") |
| 912 | |
| 913 | property_xml = ET.SubElement(properties_xml, "property") |
| 914 | property_xml.set("name", "driver") |
| 915 | property_xml.set("value", type(self.driver).__name__) |
| 916 | |
| 917 | if self.driver.args.cpu: |
| 918 | property_xml = ET.SubElement(properties_xml, "property") |
| 919 | property_xml.set("name", "cpu") |
| 920 | property_xml.set("value", self.driver.args.cpu) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 921 | |
| 922 | return self.collect_results( |
| 923 | lambda test: self.run_test(suite, test, suite_xml), |
| 924 | suite["tests"], |
| 925 | suite_xml) |
| 926 | |
| 927 | def run_tests(self): |
| 928 | """Run all suites and tests matching regexes given to constructor. |
| 929 | Write results to sponge log XML. Return the number of run and failed |
| 930 | tests.""" |
| 931 | |
| 932 | test_spec = self.get_test_json() |
| 933 | timestamp = datetime.datetime.now().replace(microsecond=0).isoformat() |
| 934 | |
| 935 | xml = ET.Element("testsuites") |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 936 | xml.set("name", self.test_set_up) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 937 | xml.set("timestamp", timestamp) |
| 938 | |
| 939 | result = self.collect_results( |
| 940 | lambda suite: self.run_suite(suite, xml), |
| 941 | test_spec["suites"], |
| 942 | xml) |
| 943 | |
| 944 | # Write XML to file. |
David Brazdil | ee5e25d | 2020-01-24 14:17:45 +0000 | [diff] [blame] | 945 | ET.ElementTree(xml).write(self.artifacts.sponge_xml_path, |
| 946 | encoding='utf-8', xml_declaration=True) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 947 | |
| 948 | if result.tests_failed > 0: |
| 949 | print("[x] FAIL:", result.tests_failed, "of", result.tests_run, |
| 950 | "tests failed") |
| 951 | elif result.tests_run > 0: |
| 952 | print(" PASS: all", result.tests_run, "tests passed") |
| 953 | |
David Brazdil | 94fd1e9 | 2020-02-03 16:45:20 +0000 | [diff] [blame] | 954 | # Let the driver clean up. |
| 955 | self.driver.finish() |
| 956 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 957 | return result |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 958 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 959 | def Main(): |
| 960 | parser = argparse.ArgumentParser() |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 961 | parser.add_argument("--hypervisor") |
| 962 | parser.add_argument("--spmc") |
Andrew Scull | 23e93a8 | 2018-10-26 14:56:04 +0100 | [diff] [blame] | 963 | parser.add_argument("--log", required=True) |
Andrew Walbran | 7559fcf | 2019-05-09 17:11:20 +0100 | [diff] [blame] | 964 | parser.add_argument("--out_initrd") |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 965 | parser.add_argument("--out_partitions") |
Andrew Scull | 7fd4bb7 | 2018-12-08 23:40:12 +0000 | [diff] [blame] | 966 | parser.add_argument("--initrd") |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 967 | parser.add_argument("--partitions_json") |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 968 | parser.add_argument("--suite") |
| 969 | parser.add_argument("--test") |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 970 | parser.add_argument("--vm_args") |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame] | 971 | parser.add_argument("--driver", default="qemu") |
| 972 | parser.add_argument("--serial-dev", default="/dev/ttyUSB0") |
| 973 | parser.add_argument("--serial-baudrate", type=int, default=115200) |
David Brazdil | d8013f9 | 2020-02-03 16:40:25 +0000 | [diff] [blame] | 974 | parser.add_argument("--serial-no-init-wait", action="store_true") |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 975 | parser.add_argument("--skip-long-running-tests", action="store_true") |
Andrew Walbran | e1fa70b | 2020-05-28 11:30:11 +0100 | [diff] [blame] | 976 | parser.add_argument("--force-long-running", action="store_true") |
Fuad Tabba | 36c8c2b | 2019-11-04 16:55:32 +0000 | [diff] [blame] | 977 | parser.add_argument("--cpu", |
| 978 | help="Selects the CPU configuration for the run environment.") |
Andrew Walbran | f636b84 | 2020-01-10 11:46:12 +0000 | [diff] [blame] | 979 | parser.add_argument("--tfa", action="store_true") |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 980 | args = parser.parse_args() |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 981 | |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 982 | # Create class which will manage all test artifacts. |
| 983 | if args.hypervisor and args.spmc: |
| 984 | test_set_up = "hypervisor_and_spmc" |
| 985 | elif args.hypervisor: |
| 986 | test_set_up = "hypervisor" |
| 987 | elif args.spmc: |
| 988 | test_set_up = "spmc" |
| 989 | else: |
| 990 | raise Exception("No Hafnium image provided!\n") |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 991 | |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 992 | initrd = None |
| 993 | if args.hypervisor and args.initrd: |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 994 | initrd_dir = os.path.join(args.out_initrd, "obj", args.initrd) |
| 995 | initrd = os.path.join(initrd_dir, "initrd.img") |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 996 | test_set_up += "_" + args.initrd |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 997 | vm_args = args.vm_args or "" |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 998 | |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 999 | partitions = None |
J-Alves | 18a25f9 | 2021-05-04 17:47:41 +0100 | [diff] [blame] | 1000 | global_run_name = None |
| 1001 | if args.driver == "fvp": |
| 1002 | if args.partitions_json is not None: |
| 1003 | partitions_dir = os.path.join( |
| 1004 | args.out_partitions, "obj", args.partitions_json) |
| 1005 | partitions = json.load(open(partitions_dir, "r")) |
| 1006 | global_run_name = os.path.basename(args.partitions_json).split(".")[0] |
| 1007 | elif args.hypervisor: |
| 1008 | if args.initrd: |
| 1009 | global_run_name = os.path.basename(args.initrd) |
| 1010 | else: |
| 1011 | global_run_name = os.path.basename(args.hypervisor).split(".")[0] |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 1012 | |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 1013 | # Create class which will manage all test artifacts. |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 1014 | log_dir = os.path.join(args.log, test_set_up) |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 1015 | artifacts = ArtifactsManager(log_dir) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 1016 | |
| 1017 | # Create a driver for the platform we want to test on. |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 1018 | driver_args = DriverArgs(artifacts, args.hypervisor, args.spmc, initrd, |
J-Alves | 18a25f9 | 2021-05-04 17:47:41 +0100 | [diff] [blame] | 1019 | vm_args, args.cpu, partitions, global_run_name) |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame] | 1020 | |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 1021 | if args.spmc: |
J-Alves | 38223dd | 2021-04-20 17:31:48 +0100 | [diff] [blame] | 1022 | # So far only FVP supports tests for SPMC. |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 1023 | if args.driver != "fvp": |
| 1024 | raise Exception("Secure tests can only run with fvp driver") |
J-Alves | 38223dd | 2021-04-20 17:31:48 +0100 | [diff] [blame] | 1025 | |
| 1026 | if args.hypervisor: |
| 1027 | driver = FvpDriverBothWorlds(driver_args) |
| 1028 | else: |
| 1029 | driver = FvpDriverSPMC(driver_args) |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 1030 | elif args.hypervisor: |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 1031 | if args.driver == "qemu": |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 1032 | out = os.path.dirname(args.hypervisor) |
| 1033 | driver = QemuDriver(driver_args, out, args.tfa) |
Olivier Deprez | a6d2e6d | 2020-11-06 18:09:50 +0100 | [diff] [blame] | 1034 | elif args.driver == "fvp": |
| 1035 | driver = FvpDriverHypervisor(driver_args) |
| 1036 | elif args.driver == "serial": |
| 1037 | driver = SerialDriver(driver_args, args.serial_dev, |
| 1038 | args.serial_baudrate, not args.serial_no_init_wait) |
| 1039 | else: |
| 1040 | raise Exception("Unknown driver name: {}".format(args.driver)) |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 1041 | else: |
| 1042 | raise Exception("No Hafnium image provided!\n") |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 1043 | |
| 1044 | # Create class which will drive test execution. |
J-Alves | 8cc7dbb | 2021-04-16 10:38:48 +0100 | [diff] [blame] | 1045 | runner = TestRunner(artifacts, driver, test_set_up, args.suite, args.test, |
Andrew Walbran | e1fa70b | 2020-05-28 11:30:11 +0100 | [diff] [blame] | 1046 | args.skip_long_running_tests, args.force_long_running) |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 1047 | |
| 1048 | # Run tests. |
| 1049 | runner_result = runner.run_tests() |
| 1050 | |
| 1051 | # Print error message if no tests were run as this is probably unexpected. |
| 1052 | # Return suitable error code. |
| 1053 | if runner_result.tests_run == 0: |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 1054 | print("Error: no tests match") |
| 1055 | return 10 |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 1056 | elif runner_result.tests_failed > 0: |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 1057 | return 1 |
| 1058 | else: |
David Brazdil | 2df2408 | 2019-09-05 11:55:08 +0100 | [diff] [blame] | 1059 | return 0 |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 1060 | |
| 1061 | if __name__ == "__main__": |
| 1062 | sys.exit(Main()) |