Add some infrastructure for multicore tests.
[riscv-tests.git] / debug / targets.py
1 import importlib
2 import os.path
3 import sys
4 import tempfile
5
6 import testlib
7
8 class Hart(object):
9 # XLEN of the hart. May be overridden with --32 or --64 command line
10 # options.
11 xlen = 0
12
13 # Will be autodetected (by running ExamineTarget) if left unset. Set to
14 # save a little time.
15 misa = None
16
17 # Path to linker script relative to the .py file where the target is
18 # defined. Defaults to <name>.lds.
19 link_script_path = None
20
21 # Implements dmode in tdata1 as described in the spec. Harts that need
22 # this value set to False are not compliant with the spec (but still usable
23 # as long as running code doesn't try to mess with triggers set by an
24 # external debugger).
25 honors_tdata1_hmode = True
26
27 # Address where a r/w/x block of RAM starts, together with its size.
28 ram = None
29 ram_size = None
30
31 # Number of instruction triggers the hart supports.
32 instruction_hardware_breakpoint_count = 0
33
34 # Defaults to target-<index>
35 name = None
36
37 def extensionSupported(self, letter):
38 # target.misa is set by testlib.ExamineTarget
39 if self.misa:
40 return self.misa & (1 << (ord(letter.upper()) - ord('A')))
41 else:
42 return False
43
44 class Target(object):
45 # pylint: disable=too-many-instance-attributes
46
47 # List of Hart object instances, one for each hart in the target.
48 harts = []
49
50 # Name of the target. Defaults to the name of the class.
51 name = None
52
53 # GDB remotetimeout setting.
54 timeout_sec = 2
55
56 # Timeout waiting for the server to start up. This is different than the
57 # GDB timeout, which is how long GDB waits for commands to execute.
58 # The server_timeout is how long this script waits for the Server to be
59 # ready for GDB connections.
60 server_timeout_sec = 60
61
62 # Path to OpenOCD configuration file relative to the .py file where the
63 # target is defined. Defaults to <name>.cfg.
64 openocd_config_path = None
65
66 # List of commands that should be executed in gdb after connecting but
67 # before starting the test.
68 gdb_setup = []
69
70 # Internal variables:
71 directory = None
72 temporary_files = []
73
74 def __init__(self, path, parsed):
75 # Path to module.
76 self.path = path
77 self.directory = os.path.dirname(path)
78 self.server_cmd = parsed.server_cmd
79 self.sim_cmd = parsed.sim_cmd
80 self.temporary_binary = None
81 Target.isolate = parsed.isolate
82 if not self.name:
83 self.name = type(self).__name__
84 # Default OpenOCD config file to <name>.cfg
85 if not self.openocd_config_path:
86 self.openocd_config_path = "%s.cfg" % self.name
87 self.openocd_config_path = os.path.join(self.directory,
88 self.openocd_config_path)
89 for i, hart in enumerate(self.harts):
90 hart.index = i
91 if not hart.name:
92 hart.name = "%s-%d" % (self.name, i)
93 # Default link script to <name>.lds
94 if not hart.link_script_path:
95 hart.link_script_path = "%s.lds" % self.name
96 hart.link_script_path = os.path.join(self.directory,
97 hart.link_script_path)
98
99 def create(self):
100 """Create the target out of thin air, eg. start a simulator."""
101 pass
102
103 def server(self):
104 """Start the debug server that gdb connects to, eg. OpenOCD."""
105 return testlib.Openocd(server_cmd=self.server_cmd,
106 config=self.openocd_config_path)
107
108 def compile(self, hart, *sources):
109 binary_name = "%s_%s-%d" % (
110 self.name,
111 os.path.basename(os.path.splitext(sources[0])[0]),
112 hart.xlen)
113 if Target.isolate:
114 self.temporary_binary = tempfile.NamedTemporaryFile(
115 prefix=binary_name + "_")
116 binary_name = self.temporary_binary.name
117 Target.temporary_files.append(self.temporary_binary)
118 march = "rv%dima" % hart.xlen
119 for letter in "fdc":
120 if hart.extensionSupported(letter):
121 march += letter
122 testlib.compile(sources +
123 ("programs/entry.S", "programs/init.c",
124 "-DNHARTS=%d" % len(self.harts),
125 "-I", "../env",
126 "-march=%s" % march,
127 "-T", hart.link_script_path,
128 "-nostartfiles",
129 "-mcmodel=medany",
130 "-DXLEN=%d" % hart.xlen,
131 "-o", binary_name),
132 xlen=hart.xlen)
133 return binary_name
134
135 def add_target_options(parser):
136 parser.add_argument("target", help=".py file that contains definition for "
137 "the target to test with.")
138 parser.add_argument("--sim_cmd",
139 help="The command to use to start the actual target (e.g. "
140 "simulation)", default="spike")
141 parser.add_argument("--server_cmd",
142 help="The command to use to start the debug server (e.g. OpenOCD)")
143
144 xlen_group = parser.add_mutually_exclusive_group()
145 xlen_group.add_argument("--32", action="store_const", const=32, dest="xlen",
146 help="Force the target to be 32-bit.")
147 xlen_group.add_argument("--64", action="store_const", const=64, dest="xlen",
148 help="Force the target to be 64-bit.")
149
150 parser.add_argument("--isolate", action="store_true",
151 help="Try to run in such a way that multiple instances can run at "
152 "the same time. This may make it harder to debug a failure if it "
153 "does occur.")
154
155 def target(parsed):
156 directory = os.path.dirname(parsed.target)
157 filename = os.path.basename(parsed.target)
158 module_name = os.path.splitext(filename)[0]
159
160 sys.path.append(directory)
161 module = importlib.import_module(module_name)
162 found = []
163 for name in dir(module):
164 definition = getattr(module, name)
165 if type(definition) == type and issubclass(definition, Target):
166 found.append(definition)
167 assert len(found) == 1, "%s does not define exactly one subclass of " \
168 "targets.Target" % parsed.target
169
170 t = found[0](parsed.target, parsed)
171 assert t.harts, "%s doesn't have any harts defined!" % t.name
172
173 return t