README-HACKING 15 KB

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