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