1 /* The common simulator framework for GDB, the GNU Debugger.
3 Copyright 2002-2022 Free Software Foundation, Inc.
5 Contributed by Andrew Cagney and Red Hat.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 /* This must come before any other includes. */
35 #include "sim-assert.h"
37 /* manipulate/lookup device names */
39 typedef struct _name_specifier
42 /* components in the full length name */
66 /* Given a device specifier, break it up into its main components:
67 path (and if present) property name and property value. */
70 split_device_specifier (struct hw *current,
71 const char *device_specifier,
76 /* expand any leading alias if present */
78 && *device_specifier != '\0'
79 && *device_specifier != '.'
80 && *device_specifier != '/')
82 struct hw *aliases = hw_tree_find_device (current, "/aliases");
85 while (device_specifier[len] != '\0'
86 && device_specifier[len] != '/'
87 && device_specifier[len] != ':'
88 && !isspace (device_specifier[len]))
90 alias[len] = device_specifier[len];
92 if (len >= sizeof (alias))
93 hw_abort (NULL, "split_device_specifier: buffer overflow");
97 && hw_find_property (aliases, alias))
99 strcpy (spec->buf, hw_find_string_property (aliases, alias));
100 strcat (spec->buf, device_specifier + len);
104 strcpy (spec->buf, device_specifier);
109 strcpy (spec->buf, device_specifier);
112 /* check no overflow */
113 if (strlen (spec->buf) >= sizeof (spec->buf))
114 hw_abort (NULL, "split_device_specifier: buffer overflow\n");
116 /* strip leading spaces */
118 while (*chp != '\0' && isspace (*chp))
123 /* find the path and terminate it with null */
125 while (*chp != '\0' && !isspace (*chp))
134 while (*chp != '\0' && isspace (*chp))
138 /* now go back and chop the property off of the path */
139 if (spec->value[0] == '\0')
141 spec->property = NULL; /*not a property*/
144 else if (spec->value[0] == '>'
145 || spec->value[0] == '<')
147 /* an interrupt spec */
148 spec->property = NULL;
152 chp = strrchr (spec->path, '/');
155 spec->property = spec->path;
156 spec->path = strchr (spec->property, '\0');
161 spec->property = chp+1;
165 /* and mark the rest as invalid */
170 spec->last_name = NULL;
171 spec->last_family = NULL;
172 spec->last_unit = NULL;
173 spec->last_args = NULL;
179 /* given a device specifier break it up into its main components -
180 path and property name - assuming that the last `device' is a
184 split_property_specifier (struct hw *current,
185 const char *property_specifier,
186 name_specifier *spec)
188 if (split_device_specifier (current, property_specifier, spec))
190 if (spec->property == NULL)
192 /* force the last name to be a property name */
193 char *chp = strrchr (spec->path, '/');
196 spec->property = spec->path;
197 spec->path = strrchr (spec->property, '\0');;
202 spec->property = chp + 1;
212 /* device the next device name and split it up, return 0 when no more
213 names to struct hw */
216 split_device_name (name_specifier *spec)
219 /* remember what came before */
220 spec->last_name = spec->name;
221 spec->last_family = spec->family;
222 spec->last_unit = spec->unit;
223 spec->last_args = spec->args;
225 if (spec->path[0] == '\0')
233 /* break the current device spec from the path */
234 spec->name = spec->path;
235 chp = strchr (spec->name, '/');
237 spec->path = strchr (spec->name, '\0');
243 /* break out the base */
244 if (spec->name[0] == '(')
246 chp = strchr (spec->name, ')');
249 spec->family = spec->name;
254 spec->family = spec->name + 1;
255 spec->name = chp + 1;
260 spec->family = spec->name;
262 /* now break out the unit */
263 chp = strchr (spec->name, '@');
275 /* finally any args */
276 chp = strchr (chp, ':');
288 /* device the value, returning the next non-space token */
291 split_value (name_specifier *spec)
294 if (spec->value == NULL)
296 /* skip leading white space */
297 while (isspace (spec->value[0]))
299 if (spec->value[0] == '\0')
305 /* find trailing space */
306 while (spec->value[0] != '\0' && !isspace (spec->value[0]))
308 /* chop this value out */
309 if (spec->value[0] != '\0')
311 spec->value[0] = '\0';
319 /* traverse the path specified by spec starting at current */
322 split_find_device (struct hw *current,
323 name_specifier *spec)
325 /* strip off (and process) any leading ., .., ./ and / */
328 if (strncmp (spec->path, "/", strlen ("/")) == 0)
331 while (current != NULL && hw_parent (current) != NULL)
332 current = hw_parent (current);
333 spec->path += strlen ("/");
335 else if (strncmp (spec->path, "./", strlen ("./")) == 0)
338 spec->path += strlen ("./");
340 else if (strncmp (spec->path, "../", strlen ("../")) == 0)
343 if (current != NULL && hw_parent (current) != NULL)
344 current = hw_parent (current);
345 spec->path += strlen ("../");
347 else if (strcmp (spec->path, ".") == 0)
350 spec->path += strlen (".");
352 else if (strcmp (spec->path, "..") == 0)
355 if (current != NULL && hw_parent (current) != NULL)
356 current = hw_parent (current);
357 spec->path += strlen ("..");
363 /* now go through the path proper */
367 split_device_name (spec);
371 while (split_device_name (spec))
374 for (child = hw_child (current);
375 child != NULL; child = hw_sibling (child))
377 if (strcmp (spec->name, hw_name (child)) == 0)
379 if (spec->unit == NULL)
384 hw_unit_decode (current, spec->unit, &phys);
385 if (memcmp (&phys, hw_unit_address (child),
386 sizeof (hw_unit)) == 0)
392 return current; /* search failed */
401 split_fill_path (struct hw *current,
402 const char *device_specifier,
403 name_specifier *spec)
406 if (!split_device_specifier (current, device_specifier, spec))
407 hw_abort (current, "error parsing %s\n", device_specifier);
409 /* fill our tree with its contents */
410 current = split_find_device (current, spec);
412 /* add any additional devices as needed */
413 if (spec->name != NULL)
417 if (current != NULL && !hw_finished_p (current))
419 current = hw_create (NULL,
426 while (split_device_name (spec));
433 /* <non-white-space> */
436 skip_token (const char *chp)
438 while (!isspace (*chp) && *chp != '\0')
440 while (isspace (*chp) && *chp != '\0')
446 /* count the number of entries */
449 count_entries (struct hw *current,
450 const char *property_name,
451 const char *property_value,
454 const char *chp = property_value;
459 chp = skip_token (chp);
461 if ((nr_entries % modulo) != 0)
463 hw_abort (current, "incorrect number of entries for %s property %s, should be multiple of %d",
464 property_name, property_value, modulo);
466 return nr_entries / modulo;
471 /* parse: <address> ::= <token> ; device dependant */
474 parse_address (struct hw *current,
479 if (hw_unit_decode (bus, chp, address) < 0)
480 hw_abort (current, "invalid unit address in %s", chp);
481 return skip_token (chp);
485 /* parse: <size> ::= <number> { "," <number> } ; */
488 parse_size (struct hw *current,
495 const char *curr = chp;
496 memset (size, 0, sizeof (*size));
497 /* parse the numeric list */
498 size->nr_cells = hw_unit_nr_size_cells (bus);
503 size->cells[nr] = strtoul (curr, &next, 0);
505 hw_abort (current, "Problem parsing <size> %s", chp);
509 if (nr == size->nr_cells)
510 hw_abort (current, "Too many values in <size> %s", chp);
513 ASSERT (nr > 0 && nr <= size->nr_cells);
514 /* right align the numbers */
515 for (i = 1; i <= size->nr_cells; i++)
518 size->cells[size->nr_cells - i] = size->cells[nr - i];
520 size->cells[size->nr_cells - i] = 0;
522 return skip_token (chp);
526 /* parse: <reg> ::= { <address> <size> } ; */
529 parse_reg_property (struct hw *current,
530 const char *property_name,
531 const char *property_value)
535 reg_property_spec *regs;
538 /* determine the number of reg entries by counting tokens */
539 nr_regs = count_entries (current, property_name, property_value, 2);
541 /* create working space */
542 regs = zalloc (nr_regs * sizeof (*regs));
545 chp = property_value;
546 for (reg_nr = 0; reg_nr < nr_regs; reg_nr++)
548 chp = parse_address (current, hw_parent (current),
549 chp, ®s[reg_nr].address);
550 chp = parse_size (current, hw_parent (current),
551 chp, ®s[reg_nr].size);
555 hw_add_reg_array_property (current, property_name,
562 /* { <child-address> <parent-address> <child-size> }* */
565 parse_ranges_property (struct hw *current,
566 const char *property_name,
567 const char *property_value)
571 range_property_spec *ranges;
574 /* determine the number of ranges specified */
575 nr_ranges = count_entries (current, property_name, property_value, 3);
577 /* create a property of that size */
578 ranges = zalloc (nr_ranges * sizeof (*ranges));
581 chp = property_value;
582 for (range_nr = 0; range_nr < nr_ranges; range_nr++)
584 chp = parse_address (current, current,
585 chp, &ranges[range_nr].child_address);
586 chp = parse_address (current, hw_parent (current),
587 chp, &ranges[range_nr].parent_address);
588 chp = parse_size (current, current,
589 chp, &ranges[range_nr].size);
593 hw_add_range_array_property (current, property_name, ranges, nr_ranges);
602 parse_integer_property (struct hw *current,
603 const char *property_name,
604 const char *property_value)
607 unsigned_cell words[1024];
608 /* integer or integer array? */
613 words[nr_entries] = strtoul (property_value, &end, 0);
614 if (property_value == end)
617 if (nr_entries * sizeof (words[0]) >= sizeof (words))
618 hw_abort (current, "buffer overflow");
619 property_value = end;
622 hw_abort (current, "error parsing integer property %s (%s)",
623 property_name, property_value);
624 else if (nr_entries == 1)
625 hw_add_integer_property (current, property_name, words[0]);
629 for (i = 0; i < nr_entries; i++)
633 /* perhaps integer array property is better */
634 hw_add_array_property (current, property_name, words,
635 sizeof (words[0]) * nr_entries);
643 parse_string_property (struct hw *current,
644 const char *property_name,
645 const char *property_value)
650 int approx_nr_strings;
652 /* get an estimate as to the number of strings by counting double
654 approx_nr_strings = 2;
655 for (chp = property_value; *chp; chp++)
660 approx_nr_strings = (approx_nr_strings) / 2;
662 /* create a string buffer for that many (plus a null) */
663 strings = (char**) zalloc ((approx_nr_strings + 1) * sizeof (char*));
665 /* now find all the strings */
666 chp = property_value;
671 /* skip leading space */
672 while (*chp != '\0' && isspace (*chp))
680 /* a quoted string - watch for '\' et al. */
681 /* estimate the size and allocate space for it */
685 while (chp[pos] != '\0' && chp[pos] != '"')
687 if (chp[pos] == '\\' && chp[pos+1] != '\0')
692 strings[nr_strings] = zalloc (pos + 1);
693 /* copy the string over */
695 while (*chp != '\0' && *chp != '"')
697 if (*chp == '\\' && *(chp+1) != '\0')
699 strings[nr_strings][pos] = *(chp+1);
705 strings[nr_strings][pos] = *chp;
712 strings[nr_strings][pos] = '\0';
716 /* copy over a single unquoted token */
718 while (chp[len] != '\0' && !isspace (chp[len]))
720 strings[nr_strings] = zalloc (len + 1);
721 strncpy (strings[nr_strings], chp, len);
722 strings[nr_strings][len] = '\0';
726 if (nr_strings > approx_nr_strings)
727 hw_abort (current, "String property %s badly formatted",
730 ASSERT (strings[nr_strings] == NULL); /* from zalloc */
734 hw_add_string_property (current, property_name, "");
735 else if (nr_strings == 1)
736 hw_add_string_property (current, property_name, strings[0]);
739 const char **specs = (const char**) strings; /* stop a bogus error */
740 hw_add_string_array_property (current, property_name,
744 /* flush the created string */
745 while (nr_strings > 0)
748 free (strings[nr_strings]);
754 /* <path-to-ihandle-device> */
758 parse_ihandle_property (struct hw *current,
759 const char *property,
762 ihandle_runtime_property_spec ihandle;
764 /* pass the full path */
765 ihandle.full_path = value;
767 /* save this ready for the ihandle create */
768 hw_add_ihandle_runtime_property (current, property,
775 hw_tree_create (SIM_DESC sd,
778 return hw_create (sd, NULL, family, family, NULL, NULL);
782 hw_tree_delete (struct hw *me)
784 /* Need to allow devices to disapear under our feet */
785 while (hw_child (me) != NULL)
787 hw_tree_delete (hw_child (me));
794 hw_tree_parse (struct hw *current,
800 current = hw_tree_vparse (current, fmt, ap);
806 hw_tree_vparse (struct hw *current,
810 char device_specifier[1024];
813 /* format the path */
814 vsprintf (device_specifier, fmt, ap);
815 if (strlen (device_specifier) >= sizeof (device_specifier))
816 hw_abort (NULL, "device_tree_add_deviced: buffer overflow\n");
818 /* construct the tree down to the final struct hw */
819 current = split_fill_path (current, device_specifier, &spec);
821 /* is there an interrupt spec */
822 if (spec.property == NULL
823 && spec.value != NULL)
825 char *op = split_value (&spec);
830 char *my_port_name = split_value (&spec);
832 char *dest_port_name = split_value (&spec);
834 name_specifier dest_spec;
835 char *dest_hw_name = split_value (&spec);
838 if (!hw_finished_p (current))
840 my_port = hw_port_decode (current, my_port_name, output_port);
841 /* find the dest device and port */
842 dest = split_fill_path (current, dest_hw_name, &dest_spec);
843 if (!hw_finished_p (dest))
845 dest_port = hw_port_decode (dest, dest_port_name,
847 /* connect the two */
848 hw_port_attach (current,
856 hw_abort (current, "unreconised interrupt spec %s\n", spec.value);
861 /* is there a property */
862 if (spec.property != NULL)
864 if (strcmp (spec.value, "true") == 0)
865 hw_add_boolean_property (current, spec.property, 1);
866 else if (strcmp (spec.value, "false") == 0)
867 hw_add_boolean_property (current, spec.property, 0);
870 const struct hw_property *property;
871 switch (spec.value[0])
876 parse_ihandle_property (current, spec.property, spec.value + 1);
883 char *curr = spec.value + 1;
888 words[nr_words] = H2BE_1 (strtoul (curr, &next, 0));
894 hw_add_array_property (current, spec.property,
895 words, sizeof (words[0]) * nr_words);
900 parse_string_property (current, spec.property, spec.value);
906 property = hw_tree_find_property (current, spec.value);
907 if (property == NULL)
908 hw_abort (current, "property %s not found\n", spec.value);
909 hw_add_duplicate_property (current,
916 if (strcmp (spec.property, "reg") == 0
917 || strcmp (spec.property, "assigned-addresses") == 0
918 || strcmp (spec.property, "alternate-reg") == 0)
920 parse_reg_property (current, spec.property, spec.value);
922 else if (strcmp (spec.property, "ranges") == 0)
924 parse_ranges_property (current, spec.property, spec.value);
926 else if (isdigit (spec.value[0])
927 || (spec.value[0] == '-' && isdigit (spec.value[1]))
928 || (spec.value[0] == '+' && isdigit (spec.value[1])))
930 parse_integer_property (current, spec.property, spec.value);
933 parse_string_property (current, spec.property, spec.value);
944 finish_hw_tree (struct hw *me,
947 if (!hw_finished_p (me))
952 hw_tree_finish (struct hw *root)
954 hw_tree_traverse (root, finish_hw_tree, NULL, NULL);
960 hw_tree_traverse (struct hw *root,
961 hw_tree_traverse_function *prefix,
962 hw_tree_traverse_function *postfix,
968 for (child = hw_child (root);
970 child = hw_sibling (child))
972 hw_tree_traverse (child, prefix, postfix, data);
975 postfix (root, data);
982 hw_tree_print_callback *print;
987 print_address (struct hw *bus,
992 hw_unit_encode (bus, phys, unit, sizeof (unit));
993 p->print (p->file, " %s", unit);
997 print_size (struct hw *bus,
1002 for (i = 0; i < size->nr_cells; i++)
1003 if (size->cells[i] != 0)
1005 if (i < size->nr_cells)
1007 p->print (p->file, " 0x%lx", (unsigned long) size->cells[i]);
1009 for (; i < size->nr_cells; i++)
1010 p->print (p->file, ",0x%lx", (unsigned long) size->cells[i]);
1013 p->print (p->file, " 0");
1017 print_reg_property (struct hw *me,
1018 const struct hw_property *property,
1022 reg_property_spec reg;
1024 hw_find_reg_array_property (me, property->name, reg_nr, ®);
1027 print_address (hw_parent (me), ®.address, p);
1028 print_size (me, ®.size, p);
1033 print_ranges_property (struct hw *me,
1034 const struct hw_property *property,
1038 range_property_spec range;
1040 hw_find_range_array_property (me, property->name, range_nr, &range);
1043 print_address (me, &range.child_address, p);
1044 print_address (hw_parent (me), &range.parent_address, p);
1045 print_size (me, &range.size, p);
1050 print_string (struct hw *me,
1054 p->print (p->file, " \"");
1055 while (*string != '\0')
1060 p->print (p->file, "\\\"");
1063 p->print (p->file, "\\\\");
1066 p->print (p->file, "%c", *string);
1071 p->print (p->file, "\"");
1075 print_string_array_property (struct hw *me,
1076 const struct hw_property *property,
1080 string_property_spec string;
1082 hw_find_string_array_property (me, property->name, nr, &string);
1085 print_string (me, string, p);
1090 print_properties (struct hw *me,
1093 const struct hw_property *property;
1094 for (property = hw_find_property (me, NULL);
1096 property = hw_next_property (property))
1098 if (hw_parent (me) == NULL)
1099 p->print (p->file, "/%s", property->name);
1101 p->print (p->file, "%s/%s", hw_path (me), property->name);
1102 if (property->original != NULL)
1104 p->print (p->file, " !");
1105 p->print (p->file, "%s/%s",
1106 hw_path (property->original->owner),
1107 property->original->name);
1111 switch (property->type)
1113 case array_property:
1115 if ((property->sizeof_array % sizeof (signed_cell)) == 0)
1117 unsigned_cell *w = (unsigned_cell*) property->array;
1120 cell_nr < (property->sizeof_array / sizeof (unsigned_cell));
1123 p->print (p->file, " 0x%lx", (unsigned long) BE2H_cell (w[cell_nr]));
1128 uint8_t *w = (uint8_t*)property->array;
1129 p->print (p->file, " [");
1130 while ((char*)w - (char*)property->array < property->sizeof_array)
1132 p->print (p->file, " 0x%2x", BE2H_1 (*w));
1138 case boolean_property:
1140 int b = hw_find_boolean_property (me, property->name);
1141 p->print (p->file, " %s", b ? "true" : "false");
1145 case ihandle_property:
1147 if (property->array != NULL)
1149 device_instance *instance = hw_find_ihandle_property (me, property->name);
1150 p->print (p->file, " *%s", device_instance_path (instance));
1154 /* not yet initialized, ask the device for the path */
1155 ihandle_runtime_property_spec spec;
1156 hw_find_ihandle_runtime_property (me, property->name, &spec);
1157 p->print (p->file, " *%s", spec.full_path);
1162 case integer_property:
1164 unsigned_word w = hw_find_integer_property (me, property->name);
1165 p->print (p->file, " 0x%lx", (unsigned long)w);
1168 case range_array_property:
1170 print_ranges_property (me, property, p);
1173 case reg_array_property:
1175 print_reg_property (me, property, p);
1178 case string_property:
1180 const char *s = hw_find_string_property (me, property->name);
1181 print_string (me, s, p);
1184 case string_array_property:
1186 print_string_array_property (me, property, p);
1191 p->print (p->file, "\n");
1196 print_interrupts (struct hw *me,
1202 struct printer *p = data;
1205 hw_port_encode (me, my_port, src, sizeof (src), output_port);
1206 hw_port_encode (dest, dest_port, dst, sizeof (dst), input_port);
1215 print_device (struct hw *me,
1218 struct printer *p = data;
1219 p->print (p->file, "%s\n", hw_path (me));
1220 print_properties (me, p);
1221 hw_port_traverse (me, print_interrupts, data);
1225 hw_tree_print (struct hw *root,
1226 hw_tree_print_callback *print,
1232 hw_tree_traverse (root,
1241 tree_instance (struct hw *root,
1242 const char *device_specifier)
1244 /* find the device node */
1246 name_specifier spec;
1247 if (!split_device_specifier (root, device_specifier, &spec))
1249 me = split_find_device (root, &spec);
1250 if (spec.name != NULL)
1252 /* create the instance */
1253 return device_create_instance (me, device_specifier, spec.last_args);
1258 hw_tree_find_device (struct hw *root,
1259 const char *path_to_device)
1262 name_specifier spec;
1264 /* parse the path */
1265 split_device_specifier (root, path_to_device, &spec);
1266 if (spec.value != NULL)
1267 return NULL; /* something wierd */
1270 node = split_find_device (root, &spec);
1271 if (spec.name != NULL)
1272 return NULL; /* not a leaf */
1278 const struct hw_property *
1279 hw_tree_find_property (struct hw *root,
1280 const char *path_to_property)
1282 name_specifier spec;
1283 if (!split_property_specifier (root, path_to_property, &spec))
1284 hw_abort (root, "Invalid property path %s", path_to_property);
1285 root = split_find_device (root, &spec);
1286 if (spec.name != NULL)
1287 return NULL; /* not a leaf */
1288 return hw_find_property (root, spec.property);
1292 hw_tree_find_boolean_property (struct hw *root,
1293 const char *path_to_property)
1295 name_specifier spec;
1296 if (!split_property_specifier (root, path_to_property, &spec))
1297 hw_abort (root, "Invalid property path %s", path_to_property);
1298 root = split_find_device (root, &spec);
1299 if (spec.name != NULL)
1300 hw_abort (root, "device \"%s\" not found (property \"%s\")",
1301 spec.name, path_to_property);
1302 return hw_find_boolean_property (root, spec.property);
1306 hw_tree_find_integer_property (struct hw *root,
1307 const char *path_to_property)
1309 name_specifier spec;
1310 if (!split_property_specifier (root, path_to_property, &spec))
1311 hw_abort (root, "Invalid property path %s", path_to_property);
1312 root = split_find_device (root, &spec);
1313 if (spec.name != NULL)
1314 hw_abort (root, "device \"%s\" not found (property \"%s\")",
1315 spec.name, path_to_property);
1316 return hw_find_integer_property (root, spec.property);
1321 hw_tree_find_ihandle_property (struct hw *root,
1322 const char *path_to_property)
1325 name_specifier spec;
1326 if (!split_property_specifier (root, path_to_property, &spec))
1327 hw_abort (root, "Invalid property path %s", path_to_property);
1328 root = split_find_device (root, &spec);
1329 if (spec.name != NULL)
1330 hw_abort (root, "device \"%s\" not found (property \"%s\")",
1331 spec.name, path_to_property);
1332 return hw_find_ihandle_property (root, spec.property);
1337 hw_tree_find_string_property (struct hw *root,
1338 const char *path_to_property)
1340 name_specifier spec;
1341 if (!split_property_specifier (root, path_to_property, &spec))
1342 hw_abort (root, "Invalid property path %s", path_to_property);
1343 root = split_find_device (root, &spec);
1344 if (spec.name != NULL)
1345 hw_abort (root, "device \"%s\" not found (property \"%s\")",
1346 spec.name, path_to_property);
1347 return hw_find_string_property (root, spec.property);