1 /* Simulator hardware option handling.
2 Copyright (C) 1998-2021 Free Software Foundation, Inc.
3 Contributed by Cygnus Support and Andrew Cagney.
5 This file is part of GDB, the GNU debugger.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "sim-assert.h"
22 #include "sim-options.h"
27 #include "hw-device.h"
41 /* if called from a processor */
48 sim_hw_parse (struct sim_state
*sd
,
55 current
= hw_tree_vparse (STATE_HW (sd
)->tree
, fmt
, ap
);
61 struct sim_state
*file
;
62 void (*print
) (struct sim_state
*, const char *, va_list ap
);
66 do_print (void *file
, const char *fmt
, ...)
68 struct printer
*p
= file
;
71 p
->print (p
->file
, fmt
, ap
);
76 sim_hw_print (struct sim_state
*sd
,
77 void (*print
) (struct sim_state
*, const char *, va_list ap
))
82 hw_tree_print (STATE_HW (sd
)->tree
, do_print
, &p
);
88 /* command line options. */
91 OPTION_HW_INFO
= OPTION_START
,
98 static DECLARE_OPTION_HANDLER (hw_option_handler
);
100 static const OPTION hw_options
[] =
102 { {"hw-info", no_argument
, NULL
, OPTION_HW_INFO
},
103 '\0', NULL
, "List configurable hw regions",
104 hw_option_handler
, NULL
},
105 { {"info-hw", no_argument
, NULL
, OPTION_HW_INFO
},
107 hw_option_handler
, NULL
},
109 { {"hw-trace", optional_argument
, NULL
, OPTION_HW_TRACE
},
110 '\0', "on|off", "Trace all hardware devices",
111 hw_option_handler
, NULL
},
112 { {"trace-hw", optional_argument
, NULL
, OPTION_HW_TRACE
},
114 hw_option_handler
, NULL
},
116 { {"hw-device", required_argument
, NULL
, OPTION_HW_DEVICE
},
117 '\0', "DEVICE", "Add the specified device",
118 hw_option_handler
, NULL
},
120 { {"hw-list", no_argument
, NULL
, OPTION_HW_LIST
},
121 '\0', NULL
, "List the device tree",
122 hw_option_handler
, NULL
},
124 { {"hw-file", required_argument
, NULL
, OPTION_HW_FILE
},
125 '\0', "FILE", "Add the devices listed in the file",
126 hw_option_handler
, NULL
},
128 { {NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
, NULL
}
133 /* Copied from ../ppc/psim.c:psim_merge_device_file() */
136 merge_device_file (struct sim_state
*sd
,
137 const char *file_name
)
140 struct hw
*current
= STATE_HW (sd
)->tree
;
142 char device_path
[1000];
144 /* try opening the file */
145 description
= fopen (file_name
, "r");
146 if (description
== NULL
)
153 while (fgets (device_path
, sizeof (device_path
), description
))
156 /* check that a complete line was read */
157 if (strchr (device_path
, '\n') == NULL
)
159 fclose (description
);
160 sim_io_eprintf (sd
, "%s:%d: line to long", file_name
, line_nr
);
163 *strchr (device_path
, '\n') = '\0';
165 /* skip comments ("#" or ";") and blank lines lines */
166 for (device
= device_path
;
167 *device
!= '\0' && isspace (*device
);
171 || device
[0] == '\0')
173 /* merge any appended lines */
174 while (device_path
[strlen (device_path
) - 1] == '\\')
176 int curlen
= strlen (device_path
) - 1;
177 /* zap the `\' at the end of the line */
178 device_path
[curlen
] = '\0';
179 /* append the next line */
180 if (!fgets (device_path
+ curlen
,
181 sizeof (device_path
) - curlen
,
184 fclose (description
);
185 sim_io_eprintf (sd
, "%s:%d: unexpected eof", file_name
, line_nr
);
188 if (strchr (device_path
, '\n') == NULL
)
190 fclose (description
);
191 sim_io_eprintf (sd
, "%s:%d: line to long", file_name
, line_nr
);
194 *strchr (device_path
, '\n') = '\0';
197 /* parse this line */
198 current
= hw_tree_parse (current
, "%s", device
);
200 fclose (description
);
206 hw_option_handler (struct sim_state
*sd
, sim_cpu
*cpu
, int opt
,
207 char *arg
, int is_command
)
214 /* delay info until after the tree is finished */
215 STATE_HW (sd
)->info_p
= 1;
220 case OPTION_HW_TRACE
:
224 STATE_HW (sd
)->trace_p
= 1;
226 else if (strcmp (arg
, "yes") == 0
227 || strcmp (arg
, "on") == 0)
229 STATE_HW (sd
)->trace_p
= 1;
231 else if (strcmp (arg
, "no") == 0
232 || strcmp (arg
, "off") == 0)
234 STATE_HW (sd
)->trace_p
= 0;
238 sim_io_eprintf (sd
, "Option --hw-trace ignored\n");
239 /* set tracing on all devices */
242 /* FIXME: Not very nice - see also hw-base.c */
243 if (STATE_HW (sd
)->trace_p
)
244 hw_tree_parse (STATE_HW (sd
)->tree
, "/global-trace? true");
249 case OPTION_HW_DEVICE
:
251 hw_tree_parse (STATE_HW (sd
)->tree
, "%s", arg
);
257 sim_hw_print (sd
, sim_io_vprintf
);
263 return merge_device_file (sd
, arg
);
267 sim_io_eprintf (sd
, "Unknown hw option %d\n", opt
);
276 /* "hw" module install handler.
278 This is called via sim_module_install to install the "hw" subsystem
279 into the simulator. */
281 static MODULE_INIT_FN sim_hw_init
;
282 static MODULE_UNINSTALL_FN sim_hw_uninstall
;
285 sim_hw_install (struct sim_state
*sd
)
287 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
288 sim_add_option_table (sd
, NULL
, hw_options
);
289 sim_module_add_uninstall_fn (sd
, sim_hw_uninstall
);
290 sim_module_add_init_fn (sd
, sim_hw_init
);
291 STATE_HW (sd
) = ZALLOC (struct sim_hw
);
292 STATE_HW (sd
)->tree
= hw_tree_create (sd
, "core");
298 sim_hw_init (struct sim_state
*sd
)
300 /* FIXME: anything needed? */
301 hw_tree_finish (STATE_HW (sd
)->tree
);
302 if (STATE_HW (sd
)->info_p
)
303 sim_hw_print (sd
, sim_io_vprintf
);
307 /* Uninstall the "hw" subsystem from the simulator. */
310 sim_hw_uninstall (struct sim_state
*sd
)
312 hw_tree_delete (STATE_HW (sd
)->tree
);
313 free (STATE_HW (sd
));
314 STATE_HW (sd
) = NULL
;
319 /* Data transfers to/from the hardware device tree. There are several
323 /* CPU: The simulation is running and the current CPU/CIA
324 initiates a data transfer. */
327 sim_cpu_hw_io_read_buffer (sim_cpu
*cpu
,
335 SIM_DESC sd
= CPU_STATE (cpu
);
336 STATE_HW (sd
)->cpu
= cpu
;
337 STATE_HW (sd
)->cia
= cia
;
338 if (hw_io_read_buffer (hw
, dest
, space
, addr
, nr_bytes
) != nr_bytes
)
339 sim_engine_abort (sd
, cpu
, cia
, "broken CPU read");
343 sim_cpu_hw_io_write_buffer (sim_cpu
*cpu
,
351 SIM_DESC sd
= CPU_STATE (cpu
);
352 STATE_HW (sd
)->cpu
= cpu
;
353 STATE_HW (sd
)->cia
= cia
;
354 if (hw_io_write_buffer (hw
, source
, space
, addr
, nr_bytes
) != nr_bytes
)
355 sim_engine_abort (sd
, cpu
, cia
, "broken CPU write");
361 /* SYSTEM: A data transfer is being initiated by the system. */
364 sim_hw_io_read_buffer (struct sim_state
*sd
,
371 STATE_HW (sd
)->cpu
= NULL
;
372 return hw_io_read_buffer (hw
, dest
, space
, addr
, nr_bytes
);
376 sim_hw_io_write_buffer (struct sim_state
*sd
,
383 STATE_HW (sd
)->cpu
= NULL
;
384 return hw_io_write_buffer (hw
, source
, space
, addr
, nr_bytes
);
389 /* Abort the simulation specifying HW as the reason */
392 hw_vabort (struct hw
*me
,
398 /* find an identity */
399 if (me
!= NULL
&& hw_path (me
) != NULL
&& hw_path (me
) [0] != '\0')
401 else if (me
!= NULL
&& hw_name (me
) != NULL
&& hw_name (me
)[0] != '\0')
403 else if (me
!= NULL
&& hw_family (me
) != NULL
&& hw_family (me
)[0] != '\0')
404 name
= hw_family (me
);
407 /* construct an updated format string */
408 msg
= alloca (strlen (name
) + strlen (": ") + strlen (fmt
) + 1);
412 /* report the problem */
413 sim_engine_vabort (hw_system (me
),
414 STATE_HW (hw_system (me
))->cpu
,
415 STATE_HW (hw_system (me
))->cia
,
420 hw_abort (struct hw
*me
,
425 /* report the problem */
427 hw_vabort (me
, fmt
, ap
);
432 sim_hw_abort (struct sim_state
*sd
,
440 sim_engine_vabort (sd
, NULL
, NULL_CIA
, fmt
, ap
);
442 hw_vabort (me
, fmt
, ap
);
447 /* MISC routines to tie HW into the rest of the system */
450 hw_halt (struct hw
*me
,
454 struct sim_state
*sd
= hw_system (me
);
455 struct sim_hw
*sim
= STATE_HW (sd
);
456 sim_engine_halt (sd
, sim
->cpu
, NULL
, sim
->cia
, reason
, status
);
460 hw_system_cpu (struct hw
*me
)
462 return STATE_HW (hw_system (me
))->cpu
;
466 hw_trace (struct hw
*me
,
470 if (hw_trace_p (me
)) /* to be sure, to be sure */
474 sim_io_eprintf (hw_system (me
), "%s: ", hw_path (me
));
475 sim_io_evprintf (hw_system (me
), fmt
, ap
);
476 sim_io_eprintf (hw_system (me
), "\n");
482 /* Based on gdb-4.17/sim/ppc/main.c:sim_io_read_stdin() */
485 do_hw_poll_read (struct hw
*me
,
486 do_hw_poll_read_method
*read
,
491 int status
= read (hw_system (me
), sim_io_fd
, buf
, sizeof_buf
);
494 else if (status
== 0 && sizeof_buf
== 0)
496 else if (status
== 0)
498 else /* status < 0 */
501 if (STATE_CALLBACK (hw_system (me
))->last_errno
== EAGAIN
)
502 return HW_IO_NOT_READY
;