blob: 1dc0cecc9329842da79619d17f7d13fd631167c5 [file] [log] [blame]
David Brazdil6c63a262019-12-23 13:23:46 +00001#!/usr/bin/env python3
David Brazdild623d312019-12-19 16:04:06 +00002#
3# Copyright 2019 The Hafnium Authors.
4#
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
David Brazdild623d312019-12-19 16:04:06 +000017"""Check ELF file for assembly-level regressions.
18
19Objdumps the given ELF file and detects known assembly patterns, checking for
20regressions on bugs such as CPU erratas. Throws an exception if a broken pattern
21is detected.
22"""
23
24import argparse
25import os
26import re
27import subprocess
28import sys
29
30HF_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
31CLANG_ROOT = os.path.join(HF_ROOT, "prebuilts", "linux-x64", "clang")
32OBJDUMP = os.path.join(CLANG_ROOT, "bin", "llvm-objdump")
33
34def check_eret_speculation_barrier(objdump_stdout):
35 """
36 Some ARM64 CPUs speculatively execute instructions after ERET.
37 Check that every ERET is followed by DSB NSH and ISB.
38 """
39 found_eret = False
40
41 STATE_DEFAULT = 1
42 STATE_EXPECT_DSB_NSH = 2
43 STATE_EXPECT_ISB = 3
44
45 REGEX_ERET = re.compile(r"^\s*[0-9a-f]+:\s*e0 03 9f d6\s+eret$")
46 REGEX_DSB_NSH = re.compile(r"^\s*[0-9a-f]+:\s*9f 37 03 d5\s*dsb\s+nsh$")
47 REGEX_ISB = re.compile(r"^\s*[0-9a-f]+:\s*df 3f 03 d5\s+isb$")
48
49 state = STATE_DEFAULT
50 for line in objdump_stdout:
51 if state == STATE_DEFAULT:
52 if re.match(REGEX_ERET, line):
53 found_eret = True
54 state = STATE_EXPECT_DSB_NSH
55 elif state == STATE_EXPECT_DSB_NSH:
56 if re.match(REGEX_DSB_NSH, line):
57 state = STATE_EXPECT_ISB
58 else:
59 raise Exception("ERET not followed by DSB NSH")
60 elif state == STATE_EXPECT_ISB:
61 if re.match(REGEX_ISB, line):
62 state = STATE_DEFAULT
63 else:
64 raise Exception("ERET not followed by ISB")
65
66 # Ensure that at least one instance was found, otherwise the regexes are
67 # probably wrong.
68 if not found_eret:
69 raise Exception("Could not find any ERET instructions")
70
71def Main():
72 parser = argparse.ArgumentParser()
73 parser.add_argument("input_elf",
74 help="ELF file to analyze")
75 parser.add_argument("stamp_file",
76 help="file to be touched if successful")
77 args = parser.parse_args()
78
79 objdump_stdout = subprocess.check_output([
80 OBJDUMP, "-d", args.input_elf ])
David Brazdil6c63a262019-12-23 13:23:46 +000081 objdump_stdout = objdump_stdout.decode("utf-8").splitlines()
David Brazdild623d312019-12-19 16:04:06 +000082
83 check_eret_speculation_barrier(objdump_stdout)
84
85 # Touch `stamp_file`.
86 with open(args.stamp_file, "w"):
87 pass
88
89 return 0
90
91if __name__ == "__main__":
92 sys.exit(Main())