blob: 713c8c16a13777ee4a42dee2391d0c6cf05eb8cf [file] [log] [blame]
Andrew Scullbc7189d2018-08-14 09:35:13 +01001#!/usr/bin/env python
Andrew Scull18834872018-10-12 11:48:09 +01002#
Andrew Walbran692b3252019-03-07 15:51:31 +00003# Copyright 2018 The Hafnium Authors.
Andrew Scull18834872018-10-12 11:48:09 +01004#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# https://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
Andrew Scullbc7189d2018-08-14 09:35:13 +010017"""Run tests.
18
19Runs tests on QEMU.
20"""
21
22from __future__ import print_function
23
Andrew Scull3b62f2b2018-08-21 14:26:12 +010024import xml.etree.ElementTree as ET
25
Andrew Scullbc7189d2018-08-14 09:35:13 +010026import argparse
Andrew Scull04502e42018-09-03 14:54:52 +010027import datetime
Andrew Scullbc7189d2018-08-14 09:35:13 +010028import json
29import os
30import re
31import subprocess
32import sys
33
Andrew Scull845fc9b2019-04-03 12:44:26 +010034HFTEST_LOG_PREFIX = "[hftest] "
35HFTEST_LOG_FAILURE_PREFIX = "Failure:"
36HFTEST_LOG_FINISHED = "FINISHED"
37
38
39def log_timeout_returncode(f, returncode):
40 if returncode == 0:
41 return
42 elif returncode == 124:
43 f.write("\r\n{}{} timed out\r\n".format(HFTEST_LOG_PREFIX,
44 HFTEST_LOG_FAILURE_PREFIX))
45 else:
46 f.write("\r\n{}{} process return code {}\r\n".format(
47 HFTEST_LOG_PREFIX, HFTEST_LOG_FAILURE_PREFIX, returncode))
48
Andrew Scullbc7189d2018-08-14 09:35:13 +010049
Andrew Scull7fd4bb72018-12-08 23:40:12 +000050def qemu(image, initrd, args, log):
Andrew Scullbc7189d2018-08-14 09:35:13 +010051 qemu_args = [
Andrew Walbranbc342d42019-02-05 16:56:02 +000052 "timeout", "--foreground", "10s",
Andrew Scullf0551c82018-12-15 20:38:47 +000053 "./prebuilts/linux-x64/qemu/qemu-system-aarch64", "-M", "virt,gic_version=3",
Andrew Walbranbc342d42019-02-05 16:56:02 +000054 "-cpu", "cortex-a57", "-smp", "4", "-m", "64M", "-machine", "virtualization=true",
Andrew Scull7fd4bb72018-12-08 23:40:12 +000055 "-nographic", "-nodefaults", "-serial", "stdio", "-kernel", image,
Andrew Scullbc7189d2018-08-14 09:35:13 +010056 ]
Andrew Scull7fd4bb72018-12-08 23:40:12 +000057 if initrd:
Andrew Scullf0551c82018-12-15 20:38:47 +000058 qemu_args += ["-initrd", initrd]
Andrew Scullbc7189d2018-08-14 09:35:13 +010059 if args:
60 qemu_args += ["-append", args]
61 # Save the log to a file.
62 with open(log, "w") as f:
63 f.write("$ {}\r\n".format(" ".join(qemu_args)))
64 f.flush()
Andrew Scull845fc9b2019-04-03 12:44:26 +010065 try:
66 subprocess.check_call(qemu_args, stdout=f, stderr=f)
67 except subprocess.CalledProcessError as e:
68 log_timeout_returncode(f, e.returncode)
Andrew Scullbc7189d2018-08-14 09:35:13 +010069 # Return that log for processing.
70 with open(log, "r") as f:
71 return f.read()
72
73
Andrew Walbran98656252019-03-14 14:52:29 +000074def fvp(image, initrd, args, log):
75 uart0_log = log + ".uart0"
76 uart1_log = log + ".uart1"
77 fdt = log + ".dtb"
78 dtc_args = [
79 "dtc", "-I", "dts", "-O", "dtb",
80 "-o", fdt,
81 ]
82 fvp_args = [
83 "timeout", "--foreground", "10s",
84 "../fvp/Base_RevC_AEMv8A_pkg/models/Linux64_GCC-4.9/FVP_Base_RevC-2xAEMv8A",
85 "-C", "pctl.startup=0.0.0.0",
86 "-C", "bp.secure_memory=0",
87 "-C", "cluster0.NUM_CORES=4",
88 "-C", "cluster1.NUM_CORES=4",
89 "-C", "cache_state_modelled=0",
90 "-C", "bp.vis.disable_visualisation=true",
91 "-C", "bp.vis.rate_limit-enable=false",
92 "-C", "bp.terminal_0.start_telnet=false",
93 "-C", "bp.terminal_1.start_telnet=false",
94 "-C", "bp.terminal_2.start_telnet=false",
95 "-C", "bp.terminal_3.start_telnet=false",
96 "-C", "bp.pl011_uart0.untimed_fifos=1",
97 "-C", "bp.pl011_uart0.unbuffered_output=1",
98 "-C", "bp.pl011_uart0.out_file=" + uart0_log,
99 "-C", "bp.pl011_uart1.out_file=" + uart1_log,
100 "-C", "cluster0.cpu0.RVBAR=0x04020000",
101 "-C", "cluster0.cpu1.RVBAR=0x04020000",
102 "-C", "cluster0.cpu2.RVBAR=0x04020000",
103 "-C", "cluster0.cpu3.RVBAR=0x04020000",
104 "-C", "cluster1.cpu0.RVBAR=0x04020000",
105 "-C", "cluster1.cpu1.RVBAR=0x04020000",
106 "-C", "cluster1.cpu2.RVBAR=0x04020000",
107 "-C", "cluster1.cpu3.RVBAR=0x04020000",
108 "--data", "cluster0.cpu0=prebuilts/linux-aarch64/arm-trusted-firmware/bl31.bin@0x04020000",
109 "--data", "cluster0.cpu0=" + fdt + "@0x82000000",
110 "--data", "cluster0.cpu0=" + image + "@0x80000000",
111 "-C", "bp.ve_sysregs.mmbSiteDefault=0",
112 "-C", "bp.ve_sysregs.exit_on_shutdown=1",
113 ]
114 if initrd:
115 fvp_args += ["--data", "cluster0.cpu0=" + initrd + "@0x84000000"]
116
117 with open(log, "w") as f:
118 f.write("$ {}\r\n".format(" ".join(dtc_args)))
119 f.flush()
Andrew Scull845fc9b2019-04-03 12:44:26 +0100120 dtc = subprocess.Popen(
121 dtc_args, stdout=f, stderr=f, stdin=subprocess.PIPE)
122 with open(
123 "prebuilts/linux-aarch64/arm-trusted-firmware/fvp-base-gicv3-psci-1t.dts",
124 "r") as base_dts:
Andrew Walbran98656252019-03-14 14:52:29 +0000125 dtc.stdin.write(base_dts.read())
126 dtc.stdin.write("/ {\n")
127 dtc.stdin.write(" chosen {\n")
128 dtc.stdin.write(" bootargs = \"" + args + "\";\n")
129 dtc.stdin.write(" stdout-path = \"serial0:115200n8\";\n")
130 dtc.stdin.write(" linux,initrd-start = <0x84000000>;\n")
131 dtc.stdin.write(" linux,initrd-end = <0x85000000>;\n")
132 dtc.stdin.write(" };\n")
133 dtc.stdin.write("};\n")
134 dtc.stdin.close()
135 dtc.wait()
136
137 f.write("$ {}\r\n".format(" ".join(fvp_args)))
138 f.flush()
Andrew Scull845fc9b2019-04-03 12:44:26 +0100139 returncode = subprocess.call(fvp_args, stdout=f, stderr=f)
Andrew Walbran98656252019-03-14 14:52:29 +0000140 with open(uart0_log, "r") as g:
141 f.write(g.read())
Andrew Scull845fc9b2019-04-03 12:44:26 +0100142 log_timeout_returncode(f, returncode)
Andrew Walbran98656252019-03-14 14:52:29 +0000143
144 with open(log, "r") as f:
145 return f.read()
146
147
148def emulator(use_fvp, image, initrd, args, log):
149 if use_fvp:
150 return fvp(image, initrd, args, log)
151 else:
152 return qemu(image, initrd, args, log)
153
154
Andrew Scullbc7189d2018-08-14 09:35:13 +0100155def ensure_dir(path):
156 try:
157 os.makedirs(path)
158 except OSError:
159 if not os.path.isdir(path):
160 raise
161
162
163def hftest_lines(raw):
Andrew Scullbc7189d2018-08-14 09:35:13 +0100164 return [
Andrew Scull845fc9b2019-04-03 12:44:26 +0100165 line[len(HFTEST_LOG_PREFIX):]
Andrew Scullbc7189d2018-08-14 09:35:13 +0100166 for line in raw.splitlines()
Andrew Scull845fc9b2019-04-03 12:44:26 +0100167 if line.startswith(HFTEST_LOG_PREFIX)
Andrew Scullbc7189d2018-08-14 09:35:13 +0100168 ]
169
170
171def Main():
172 parser = argparse.ArgumentParser()
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000173 parser.add_argument("image")
Andrew Scullbc7189d2018-08-14 09:35:13 +0100174 parser.add_argument("--out", required=True)
Andrew Scull23e93a82018-10-26 14:56:04 +0100175 parser.add_argument("--log", required=True)
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000176 parser.add_argument("--initrd")
Andrew Scullbc7189d2018-08-14 09:35:13 +0100177 parser.add_argument("--suite")
178 parser.add_argument("--test")
Andrew Walbranbc342d42019-02-05 16:56:02 +0000179 parser.add_argument("--vm_args")
Andrew Walbran98656252019-03-14 14:52:29 +0000180 parser.add_argument("--fvp", type=bool)
Andrew Scullbc7189d2018-08-14 09:35:13 +0100181 args = parser.parse_args()
182 # Resolve some paths.
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000183 image = os.path.join(args.out, args.image + ".bin")
184 initrd = None
185 suite = args.image
186 if args.initrd:
Andrew Walbran377bd8b2019-02-04 17:51:04 +0000187 initrd = os.path.join(args.out, "obj", args.initrd, "initrd.img")
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000188 suite += "_" + args.initrd
Andrew Walbranbc342d42019-02-05 16:56:02 +0000189 vm_args = args.vm_args or ""
Andrew Scull7fd4bb72018-12-08 23:40:12 +0000190 log = os.path.join(args.log, suite)
Andrew Scullbc7189d2018-08-14 09:35:13 +0100191 ensure_dir(log)
192 print("Logs saved under", log)
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100193 log_file = os.path.join(log, "sponge_log.log")
Andrew Scull845fc9b2019-04-03 12:44:26 +0100194 with open(log_file, "w") as full_log:
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100195 # Query the tests in the image.
Andrew Scull845fc9b2019-04-03 12:44:26 +0100196 out = emulator(args.fvp, image, initrd, vm_args + " json",
197 os.path.join(log, "json.log"))
198 full_log.write(out)
199 full_log.write("\r\n\r\n")
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100200 hftest_json = "\n".join(hftest_lines(out))
201 tests = json.loads(hftest_json)
202 # Run the selected tests.
203 tests_run = 0
204 failures = 0
205 suite_re = re.compile(args.suite or ".*")
206 test_re = re.compile(args.test or ".*")
Andrew Scull845fc9b2019-04-03 12:44:26 +0100207 suites_xml = ET.Element("testsuites")
208 suites_xml.set("name", suite)
209 suites_xml.set(
Andrew Scull04502e42018-09-03 14:54:52 +0100210 "timestamp",
211 datetime.datetime.now().replace(microsecond=0).isoformat())
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100212 for suite in tests["suites"]:
213 if not suite_re.match(suite["name"]):
Andrew Scullbc7189d2018-08-14 09:35:13 +0100214 continue
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100215 tests_run_from_suite = 0
216 failures_from_suite = 0
Andrew Scull845fc9b2019-04-03 12:44:26 +0100217 suite_xml = ET.SubElement(suites_xml, "testsuite")
218 suite_xml.set("name", suite["name"])
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100219 for test in suite["tests"]:
220 if not test_re.match(test):
221 continue
Andrew Scull845fc9b2019-04-03 12:44:26 +0100222 test_xml = ET.SubElement(suite_xml, "testcase")
223 test_xml.set("name", test)
224 test_xml.set("classname", suite['name'])
225 test_xml.set("status", "run")
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100226 tests_run_from_suite += 1
227 if tests_run_from_suite == 1:
228 print(" SUITE", suite["name"])
229 print(" RUN", test)
230 test_log = os.path.join(log,
231 suite["name"] + "." + test + ".log")
Andrew Scull845fc9b2019-04-03 12:44:26 +0100232 out = emulator(
233 args.fvp, image, initrd,
234 vm_args + " run {} {}".format(suite["name"], test),
235 test_log)
236 full_log.write(out)
237 full_log.write("\r\n\r\n")
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100238 hftest_out = hftest_lines(out)
Andrew Scull845fc9b2019-04-03 12:44:26 +0100239 if len(
240 hftest_out
241 ) > 0 and hftest_out[-1] == HFTEST_LOG_FINISHED and not any(
242 l.startswith(HFTEST_LOG_FAILURE_PREFIX)
243 for l in hftest_out):
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100244 print(" PASS")
245 else:
246 failures_from_suite += 1
Andrew Scull845fc9b2019-04-03 12:44:26 +0100247 failure_xml = ET.SubElement(test_xml, "failure")
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100248 # TODO: set a meaningful message and put log in CDATA
Andrew Scull845fc9b2019-04-03 12:44:26 +0100249 failure_xml.set("message", "Test failed")
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100250 print("[x] FAIL --", test_log)
251 tests_run += tests_run_from_suite
252 failures += failures_from_suite
Andrew Scull845fc9b2019-04-03 12:44:26 +0100253 suite_xml.set("tests", str(tests_run_from_suite))
254 suite_xml.set("failures", str(failures_from_suite))
255 suites_xml.set("tests", str(tests_run))
256 suites_xml.set("failures", str(failures))
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100257 with open(os.path.join(log, "sponge_log.xml"), "w") as f:
Andrew Scull845fc9b2019-04-03 12:44:26 +0100258 ET.ElementTree(suites_xml).write(
259 f, encoding='utf-8', xml_declaration=True)
Andrew Scullbc7189d2018-08-14 09:35:13 +0100260 # If none were run, this is probably a mistake.
261 if tests_run == 0:
262 print("Error: no tests match")
263 return 10
264 # Exit with 0 on success and 1 if any test failed.
265 if failures:
266 print("[x] FAIL:", failures, "of", tests_run, "tests failed")
267 return 1
268 else:
269 print(" PASS: all", tests_run, "tests passed")
270 return 0
271
272
273if __name__ == "__main__":
274 sys.exit(Main())