README-HACKING 17 KB

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