Commit | Line | Data |
---|---|---|
c906108c SS |
1 | This is a loose collection of notes for people hacking on simulators. |
2 | If this document gets big enough it can be prettied up then. | |
3 | ||
4 | Contents | |
5 | ||
6 | - The "common" directory | |
7 | - Common Makefile Support | |
8 | - TAGS support | |
9 | - Generating "configure" files | |
c906108c SS |
10 | - C Language Assumptions |
11 | - "dump" commands under gdb | |
12 | \f | |
13 | The "common" directory | |
14 | ====================== | |
15 | ||
16 | The common directory contains: | |
17 | ||
18 | - common documentation files (e.g. run.1, and maybe in time .texi files) | |
19 | - common source files (e.g. run.c) | |
20 | - common Makefile fragment and configury (e.g. Make-common.in, aclocal.m4). | |
21 | ||
22 | In addition "common" contains portions of the system call support | |
23 | (e.g. callback.c, nltvals.def). | |
24 | ||
25 | Even though no files are built in this directory, it is still configured | |
26 | so support for regenerating nltvals.def is present. | |
27 | \f | |
28 | Common Makefile Support | |
29 | ======================= | |
30 | ||
31 | A common configuration framework is available for simulators that want | |
32 | to use it. The common framework exists to remove a lot of duplication | |
306f4178 | 33 | in configure.ac and Makefile.in, and it also provides a foundation for |
c906108c SS |
34 | enhancing the simulators uniformly (e.g. the more they share in common |
35 | the easier a feature added to one is added to all). | |
36 | ||
306f4178 | 37 | The configure.ac of a simulator using the common framework should look like: |
c906108c SS |
38 | |
39 | --- snip --- | |
40 | dnl Process this file with autoconf to produce a configure script. | |
c906108c | 41 | AC_INIT(Makefile.in) |
136da8cd | 42 | AC_CONFIG_MACRO_DIRS([../common ../.. ../../config]) |
c906108c | 43 | |
c906108c SS |
44 | ... target specific additions ... |
45 | ||
46 | SIM_AC_OUTPUT | |
47 | --- snip --- | |
48 | ||
c906108c SS |
49 | SIM_AC_OUTPUT: |
50 | ||
51 | - creates the symbolic links defined in sim_link_{files,links} | |
52 | - creates config.h | |
53 | - creates the Makefile | |
54 | ||
55 | The Makefile.in of a simulator using the common framework should look like: | |
56 | ||
57 | --- snip --- | |
58 | # Makefile for blah ... | |
59 | # Copyright blah ... | |
60 | ||
61 | ## COMMON_PRE_CONFIG_FRAG | |
62 | ||
63 | # These variables are given default values in COMMON_PRE_CONFIG_FRAG. | |
64 | # We override the ones we need to here. | |
65 | # Not all of these need to be mentioned, only the necessary ones. | |
66 | # In fact it is better to *not* mention ones if the value is the default. | |
67 | ||
68 | # List of object files, less common parts. | |
69 | SIM_OBJS = | |
70 | # List of extra dependencies. | |
71 | # Generally this consists of simulator specific files included by sim-main.h. | |
72 | SIM_EXTRA_DEPS = | |
73 | # List of flags to always pass to $(CC). | |
74 | SIM_EXTRA_CFLAGS = | |
75 | # List of extra libraries to link with. | |
76 | SIM_EXTRA_LIBS = | |
c906108c SS |
77 | # Dependency of `install' to install any extra files. |
78 | SIM_EXTRA_INSTALL = | |
79 | # Dependency of `clean' to clean any extra files. | |
80 | SIM_EXTRA_CLEAN = | |
81 | ||
82 | ## COMMON_POST_CONFIG_FRAG | |
83 | ||
84 | # Rules need to build $(SIM_OBJS), plus whatever else the target wants. | |
85 | ||
86 | ... target specific rules ... | |
87 | --- snip --- | |
88 | ||
89 | COMMON_{PRE,POST}_CONFIG_FRAG are markers for SIM_AC_OUTPUT to tell it | |
90 | where to insert the two pieces of common/Make-common.in. | |
91 | The resulting Makefile is created by doing autoconf substitions on | |
92 | both the target's Makefile.in and Make-common.in, and inserting | |
93 | the two pieces of Make-common.in into the target's Makefile.in at | |
94 | COMMON_{PRE,POST}_CONFIG_FRAG. | |
95 | ||
96 | Note that SIM_EXTRA_{INSTALL,CLEAN} could be removed and "::" targets | |
97 | could be used instead. However, it's not clear yet whether "::" targets | |
98 | are portable enough. | |
99 | \f | |
100 | TAGS support | |
101 | ============ | |
102 | ||
103 | Many files generate program symbols at compile time. | |
104 | Such symbols can't be found with grep nor do they normally appear in | |
105 | the TAGS file. To get around this, source files can add the comment | |
106 | ||
107 | /* TAGS: foo1 foo2 */ | |
108 | ||
109 | where foo1, foo2 are program symbols. Symbols found in such comments | |
110 | are greppable and appear in the TAGS file. | |
111 | \f | |
112 | Generating "configure" files | |
113 | ============================ | |
114 | ||
115 | For targets using the common framework, "configure" can be generated | |
116 | by running `autoconf'. | |
117 | ||
118 | To regenerate the configure files for all targets using the common framework: | |
119 | ||
120 | $ cd devo/sim | |
121 | $ make -f Makefile.in SHELL=/bin/sh autoconf-common | |
122 | ||
123 | To add a change-log entry to the ChangeLog file for each updated | |
124 | directory (WARNING - check the modified new-ChangeLog files before | |
125 | renaming): | |
126 | ||
127 | $ make -f Makefile.in SHELL=/bin/sh autoconf-changelog | |
128 | $ more */new-ChangeLog | |
129 | $ make -f Makefile.in SHELL=/bin/sh autoconf-install | |
130 | ||
131 | In a similar vein, both the configure and config.in files can be | |
132 | updated using the sequence: | |
133 | ||
134 | $ cd devo/sim | |
135 | $ make -f Makefile.in SHELL=/bin/sh autoheader-common | |
136 | $ make -f Makefile.in SHELL=/bin/sh autoheader-changelog | |
137 | $ more */new-ChangeLog | |
138 | $ make -f Makefile.in SHELL=/bin/sh autoheader-install | |
c93abbcc AC |
139 | |
140 | To add the entries to an alternative ChangeLog file, use: | |
141 | ||
142 | $ make ChangeLog=MyChangeLog .... | |
143 | ||
c906108c SS |
144 | \f |
145 | C Language Assumptions | |
146 | ====================== | |
147 | ||
46f900c0 | 148 | An ISO C11 compiler is required, as is an ISO C standard library. |
c906108c SS |
149 | \f |
150 | "dump" commands under gdb | |
151 | ========================= | |
152 | ||
153 | gdbinit.in contains the following | |
154 | ||
155 | define dump | |
156 | set sim_debug_dump () | |
157 | end | |
158 | ||
159 | Simulators that define the sim_debug_dump function can then have their | |
160 | internal state pretty printed from gdb. | |
161 | ||
162 | FIXME: This can obviously be made more elaborate. As needed it will be. | |
163 | \f | |
164 | Rebuilding nltvals.def | |
165 | ====================== | |
166 | ||
167 | Checkout a copy of the SIM and LIBGLOSS modules (Unless you've already | |
168 | got one to hand): | |
169 | ||
170 | $ mkdir /tmp/$$ | |
171 | $ cd /tmp/$$ | |
172 | $ cvs checkout sim-no-testsuite libgloss-no-testsuite newlib-no-testsuite | |
173 | ||
bd0918c9 | 174 | Configure things for an arbitrary simulator target (d10v is used here for |
c906108c SS |
175 | convenience): |
176 | ||
177 | $ mkdir /tmp/$$/build | |
178 | $ cd /tmp/$$/build | |
179 | $ /tmp/$$/devo/configure --target=d10v-elf | |
180 | ||
5e25901f | 181 | In the sim/ directory rebuild the headers: |
c906108c | 182 | |
5e25901f MF |
183 | $ cd sim/ |
184 | $ make nltvals | |
c906108c | 185 | |
bd0918c9 MF |
186 | If the target uses the common syscall table (libgloss/syscall.h), then you're |
187 | all set! If the target has a custom syscall table, you need to declare it: | |
c906108c | 188 | |
bd0918c9 | 189 | devo/sim/common/gennltvals.py |
c906108c SS |
190 | |
191 | Add your new processor target (you'll need to grub | |
192 | around to find where your syscall.h lives). | |
193 | ||
194 | devo/sim/<processor>/Makefile.in | |
195 | ||
196 | Add the definition: | |
197 | ||
198 | ``NL_TARGET = -DNL_TARGET_d10v'' | |
199 | ||
200 | just before the line COMMON_POST_CONFIG_FRAG. | |
201 | ||
202 | devo/sim/<processor>/*.[ch] | |
203 | ||
204 | Include targ-vals.h instead of syscall.h. | |
aba193a5 MF |
205 | \f |
206 | Tracing | |
207 | ======= | |
208 | ||
209 | For ports based on CGEN, tracing instrumentation should largely be for free, | |
210 | so we will cover the basic non-CGEN setup here. The assumption is that your | |
211 | target is using the common autoconf macros and so the build system already | |
212 | includes the sim-trace configure flag. | |
213 | ||
214 | The full tracing API is covered in sim-trace.h, so this section is an overview. | |
215 | ||
216 | Before calling any trace function, you should make a call to the trace_prefix() | |
217 | function. This is usually done in the main sim_engine_run() loop before | |
218 | simulating the next instruction. You should make this call before every | |
219 | simulated insn. You can probably copy & paste this: | |
220 | if (TRACE_ANY_P (cpu)) | |
221 | trace_prefix (sd, cpu, NULL_CIA, oldpc, TRACE_LINENUM_P (cpu), NULL, 0, ""); | |
222 | ||
223 | You will then need to instrument your simulator code with calls to the | |
224 | trace_generic() function with the appropriate trace index. Typically, this | |
225 | will take a form similar to the above snippet. So to trace instructions, you | |
226 | would use something like: | |
227 | if (TRACE_INSN_P (cpu)) | |
228 | trace_generic (sd, cpu, TRACE_INSN_IDX, "NOP;"); | |
229 | ||
230 | The exact output format is up to you. See the trace index enum in sim-trace.h | |
231 | to see the different tracing info available. | |
232 | ||
233 | To utilize the tracing features at runtime, simply use the --trace-xxx flags. | |
234 | run --trace-insn ./some-program | |
235 | \f | |
236 | Profiling | |
237 | ========= | |
238 | ||
239 | Similar to the tracing section, this is merely an overview for non-CGEN based | |
240 | ports. The full API may be found in sim-profile.h. Its API is also similar | |
241 | to the tracing API. | |
242 | ||
243 | Note that unlike the tracing command line options, in addition to the profile | |
244 | flags, you have to use the --verbose option to view the summary report after | |
245 | execution. Tracing output is displayed on the fly, but the profile output is | |
246 | only summarized. | |
247 | ||
248 | To profile core accesses (such as data reads/writes and insn fetches), add | |
249 | calls to PROFILE_COUNT_CORE() to your read/write functions. So in your data | |
250 | fetch function, you'd use something like: | |
251 | PROFILE_COUNT_CORE (cpu, target_addr, size_in_bytes, map_read); | |
252 | Then in your data write function: | |
253 | PROFILE_COUNT_CORE (cpu, target_addr, size_in_bytes, map_write); | |
254 | And in your insn fetcher: | |
255 | PROFILE_COUNT_CORE (cpu, target_addr, size_in_bytes, map_exec); | |
256 | ||
257 | To use the PC profiling code, you simply have to tell the system where to find | |
4c0d76b9 MF |
258 | your simulator's PC. So in your model initialization function: |
259 | CPU_PC_FETCH (cpu) = function_that_fetches_the_pc; | |
aba193a5 MF |
260 | |
261 | To profile branches, in every location where a branch insn is executed, call | |
262 | one of the related helpers: | |
263 | PROFILE_BRANCH_TAKEN (cpu); | |
264 | PROFILE_BRANCH_UNTAKEN (cpu); | |
265 | If you have stall information, you can utilize the other helpers too. | |
266 | \f | |
267 | Environment Simulation | |
268 | ====================== | |
269 | ||
270 | The simplest simulator doesn't include environment support -- it merely | |
271 | simulates the Instruction Set Architecture (ISA). Once you're ready to move | |
425b0b1a MF |
272 | on to the next level, it's time to start handling the --env option. It's |
273 | enabled by default for all ports already. | |
aba193a5 MF |
274 | |
275 | This will support for the user, virtual, and operating environments. See the | |
276 | sim-config.h header for a more detailed description of them. The former are | |
277 | pretty straight forward as things like exceptions (making system calls) are | |
278 | handled in the simulator. Which is to say, an exception does not trigger an | |
279 | exception handler in the simulator target -- that is what the operating env | |
280 | is about. See the following userspace section for more information. | |
281 | \f | |
282 | Userspace System Calls | |
283 | ====================== | |
284 | ||
285 | By default, the libgloss userspace is simulated. That means the system call | |
286 | numbers and calling convention matches that of libgloss. Simulating other | |
287 | userspaces (such as Linux) is pretty straightforward, but let's first focus | |
df68e12b | 288 | on the basics. The basic API is covered in include/sim/callback.h. |
aba193a5 MF |
289 | |
290 | When an instruction is simulated that invokes the system call method (such as | |
291 | forcing a hardware trap or exception), your simulator code should set up the | |
292 | CB_SYSCALL data structure before calling the common cb_syscall() function. | |
293 | For example: | |
294 | static int | |
295 | syscall_read_mem (host_callback *cb, struct cb_syscall *sc, | |
296 | unsigned long taddr, char *buf, int bytes) | |
297 | { | |
298 | SIM_DESC sd = (SIM_DESC) sc->p1; | |
299 | SIM_CPU *cpu = (SIM_CPU *) sc->p2; | |
300 | return sim_core_read_buffer (sd, cpu, read_map, buf, taddr, bytes); | |
301 | } | |
302 | static int | |
303 | syscall_write_mem (host_callback *cb, struct cb_syscall *sc, | |
304 | unsigned long taddr, const char *buf, int bytes) | |
305 | { | |
306 | SIM_DESC sd = (SIM_DESC) sc->p1; | |
307 | SIM_CPU *cpu = (SIM_CPU *) sc->p2; | |
308 | return sim_core_write_buffer (sd, cpu, write_map, buf, taddr, bytes); | |
309 | } | |
310 | void target_sim_syscall (SIM_CPU *cpu) | |
311 | { | |
312 | SIM_DESC sd = CPU_STATE (cpu); | |
313 | host_callback *cb = STATE_CALLBACK (sd); | |
314 | CB_SYSCALL sc; | |
315 | ||
316 | CB_SYSCALL_INIT (&sc); | |
317 | ||
318 | sc.func = <fetch system call number>; | |
319 | sc.arg1 = <fetch first system call argument>; | |
320 | sc.arg2 = <fetch second system call argument>; | |
321 | sc.arg3 = <fetch third system call argument>; | |
322 | sc.arg4 = <fetch fourth system call argument>; | |
323 | sc.p1 = (PTR) sd; | |
324 | sc.p2 = (PTR) cpu; | |
325 | sc.read_mem = syscall_read_mem; | |
326 | sc.write_mem = syscall_write_mem; | |
327 | ||
328 | cb_syscall (cb, &sc); | |
329 | ||
330 | <store system call result from sc.result>; | |
331 | <store system call error from sc.errcode>; | |
332 | } | |
333 | Some targets store the result and error code in different places, while others | |
334 | only store the error code when the result is an error. | |
335 | ||
336 | Keep in mind that the CB_SYS_xxx defines are normalized values with no real | |
337 | meaning with respect to the target. They provide a unique map on the host so | |
338 | that it can parse things sanely. For libgloss, the common/nltvals.def file | |
339 | creates the target's system call numbers to the CB_SYS_xxx values. | |
340 | ||
341 | To simulate other userspace targets, you really only need to update the maps | |
342 | pointers that are part of the callback interface. So create CB_TARGET_DEFS_MAP | |
343 | arrays for each set (system calls, errnos, open bits, etc...) and in a place | |
344 | you find useful, do something like: | |
345 | ||
346 | ... | |
347 | static CB_TARGET_DEFS_MAP cb_linux_syscall_map[] = { | |
348 | # define TARGET_LINUX_SYS_open 5 | |
349 | { CB_SYS_open, TARGET_LINUX_SYS_open }, | |
350 | ... | |
351 | { -1, -1 }, | |
352 | }; | |
353 | ... | |
354 | host_callback *cb = STATE_CALLBACK (sd); | |
355 | cb->syscall_map = cb_linux_syscall_map; | |
356 | cb->errno_map = cb_linux_errno_map; | |
357 | cb->open_map = cb_linux_open_map; | |
358 | cb->signal_map = cb_linux_signal_map; | |
359 | cb->stat_map = cb_linux_stat_map; | |
360 | ... | |
361 | ||
362 | Each of these cb_linux_*_map's are manually declared by the arch target. | |
363 | ||
364 | The target_sim_syscall() example above will then work unchanged (ignoring the | |
365 | system call convention) because all of the callback functions go through these | |
366 | mapping arrays. | |
367 | \f | |
368 | Events | |
369 | ====== | |
370 | ||
371 | Events are scheduled and executed on behalf of either a cpu or hardware devices. | |
372 | The API is pretty much the same and can be found in common/sim-events.h and | |
373 | common/hw-events.h. | |
374 | ||
375 | For simulator targets, you really just have to worry about the schedule and | |
376 | deschedule functions. | |
377 | \f | |
378 | Device Trees | |
379 | ============ | |
380 | ||
381 | The device tree model is based on the OpenBoot specification. Since this is | |
382 | largely inherited from the psim code, consult the existing psim documentation | |
383 | for some in-depth details. | |
384 | http://sourceware.org/psim/manual/ | |
385 | \f | |
386 | Hardware Devices | |
387 | ================ | |
388 | ||
389 | The simplest simulator doesn't include hardware device support. Once you're | |
be0387ee MF |
390 | ready to move on to the next level, declare in your Makefile.in: |
391 | SIM_EXTRA_HW_DEVICES = devone devtwo devthree | |
aba193a5 MF |
392 | |
393 | The basic hardware API is documented in common/hw-device.h. | |
394 | ||
395 | Each device has to have a matching file name with a "dv-" prefix. So there has | |
396 | to be a dv-devone.c, dv-devtwo.c, and dv-devthree.c files. Further, each file | |
397 | has to have a matching hw_descriptor structure. So the dv-devone.c file has to | |
398 | have something like: | |
399 | const struct hw_descriptor dv_devone_descriptor[] = { | |
400 | {"devone", devone_finish,}, | |
401 | {NULL, NULL}, | |
402 | }; | |
403 | ||
404 | The "devone" string as well as the "devone_finish" function are not hard | |
405 | requirements, just common conventions. The structure name is a hard | |
406 | requirement. | |
407 | ||
408 | The devone_finish() callback function is used to instantiate this device by | |
409 | parsing the corresponding properties in the device tree. | |
410 | ||
411 | Hardware devices typically attach address ranges to themselves. Then when | |
412 | accesses to those addresses are made, the hardware will have its callback | |
413 | invoked. The exact callback could be a normal I/O read/write access, as | |
414 | well as a DMA access. This makes it easy to simulate memory mapped registers. | |
415 | ||
416 | Keep in mind that like a proper device driver, it may be instantiated many | |
417 | times over. So any device state it needs to be maintained should be allocated | |
418 | during the finish callback and attached to the hardware device via set_hw_data. | |
419 | Any hardware functions can access this private data via the hw_data function. | |
420 | \f | |
421 | Ports (Interrupts / IRQs) | |
422 | ========================= | |
c906108c | 423 | |
aba193a5 MF |
424 | First, a note on terminology. A "port" is an aspect of a hardware device that |
425 | accepts or generates interrupts. So devices with input ports may be the target | |
426 | of an interrupt (accept it), and/or they have output ports so that they may be | |
427 | the source of an interrupt (generate it). | |
428 | ||
429 | Each port has a symbolic name and a unique number. These are used to identify | |
430 | the port in different contexts. The output port name has no hard relationship | |
431 | to the input port name (same for the unique number). The callback that accepts | |
432 | the interrupt uses the name/id of its input port, while the generator function | |
433 | uses the name/id of its output port. | |
434 | ||
435 | The device tree is used to connect the output port of a device to the input | |
436 | port of another device. There are no limits on the number of inputs connected | |
437 | to an output, or outputs to an input, or the devices attached to the ports. | |
438 | In other words, the input port and output port could be the same device. | |
439 | ||
440 | The basics are: | |
441 | - each hardware device declares an array of ports (hw_port_descriptor). | |
442 | any mix of input and output ports is allowed. | |
443 | - when setting up the device, attach the array (set_hw_ports). | |
444 | - if the device accepts interrupts, it will have to attach a port callback | |
445 | function (set_hw_port_event) | |
446 | - connect ports with the device tree | |
447 | - handle incoming interrupts with the callback | |
448 | - generate outgoing interrupts with hw_port_event |