1 /* The common simulator framework for GDB, the GNU Debugger.
3 Copyright 2002 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 2 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, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
30 #include "sim-assert.h"
46 /* manipulate/lookup device names */
48 typedef struct _name_specifier {
50 /* components in the full length name */
74 /* Given a device specifier, break it up into its main components:
75 path (and if present) property name and property value. */
78 split_device_specifier (struct hw *current,
79 const char *device_specifier,
84 /* expand any leading alias if present */
86 && *device_specifier != '\0'
87 && *device_specifier != '.'
88 && *device_specifier != '/')
90 struct hw *aliases = hw_tree_find_device (current, "/aliases");
93 while (device_specifier[len] != '\0'
94 && device_specifier[len] != '/'
95 && device_specifier[len] != ':'
96 && !isspace (device_specifier[len]))
98 alias[len] = device_specifier[len];
100 if (len >= sizeof(alias))
101 hw_abort (NULL, "split_device_specifier: buffer overflow");
105 && hw_find_property (aliases, alias))
107 strcpy (spec->buf, hw_find_string_property(aliases, alias));
108 strcat (spec->buf, device_specifier + len);
112 strcpy (spec->buf, device_specifier);
117 strcpy(spec->buf, device_specifier);
120 /* check no overflow */
121 if (strlen(spec->buf) >= sizeof(spec->buf))
122 hw_abort (NULL, "split_device_specifier: buffer overflow\n");
124 /* strip leading spaces */
126 while (*chp != '\0' && isspace(*chp))
131 /* find the path and terminate it with null */
133 while (*chp != '\0' && !isspace(*chp))
142 while (*chp != '\0' && isspace(*chp))
146 /* now go back and chop the property off of the path */
147 if (spec->value[0] == '\0')
149 spec->property = NULL; /*not a property*/
152 else if (spec->value[0] == '>'
153 || spec->value[0] == '<')
155 /* an interrupt spec */
156 spec->property = NULL;
159 chp = strrchr(spec->path, '/');
162 spec->property = spec->path;
163 spec->path = strchr(spec->property, '\0');
167 spec->property = chp+1;
171 /* and mark the rest as invalid */
176 spec->last_name = NULL;
177 spec->last_family = NULL;
178 spec->last_unit = NULL;
179 spec->last_args = NULL;
185 /* given a device specifier break it up into its main components -
186 path and property name - assuming that the last `device' is a
190 split_property_specifier (struct hw *current,
191 const char *property_specifier,
192 name_specifier *spec)
194 if (split_device_specifier (current, property_specifier, spec))
196 if (spec->property == NULL)
198 /* force the last name to be a property name */
199 char *chp = strrchr (spec->path, '/');
202 spec->property = spec->path;
203 spec->path = strrchr (spec->property, '\0');;
208 spec->property = chp + 1;
218 /* device the next device name and split it up, return 0 when no more
219 names to struct hw */
222 split_device_name (name_specifier *spec)
225 /* remember what came before */
226 spec->last_name = spec->name;
227 spec->last_family = spec->family;
228 spec->last_unit = spec->unit;
229 spec->last_args = spec->args;
231 if (spec->path[0] == '\0')
239 /* break the current device spec from the path */
240 spec->name = spec->path;
241 chp = strchr (spec->name, '/');
243 spec->path = strchr (spec->name, '\0');
249 /* break out the base */
250 if (spec->name[0] == '(')
252 chp = strchr(spec->name, ')');
255 spec->family = spec->name;
260 spec->family = spec->name + 1;
261 spec->name = chp + 1;
266 spec->family = spec->name;
268 /* now break out the unit */
269 chp = strchr(spec->name, '@');
281 /* finally any args */
282 chp = strchr(chp, ':');
294 /* device the value, returning the next non-space token */
297 split_value (name_specifier *spec)
300 if (spec->value == NULL)
302 /* skip leading white space */
303 while (isspace (spec->value[0]))
305 if (spec->value[0] == '\0')
311 /* find trailing space */
312 while (spec->value[0] != '\0' && !isspace (spec->value[0]))
314 /* chop this value out */
315 if (spec->value[0] != '\0')
317 spec->value[0] = '\0';
325 /* traverse the path specified by spec starting at current */
328 split_find_device (struct hw *current,
329 name_specifier *spec)
331 /* strip off (and process) any leading ., .., ./ and / */
334 if (strncmp (spec->path, "/", strlen ("/")) == 0)
337 while (current != NULL && hw_parent (current) != NULL)
338 current = hw_parent (current);
339 spec->path += strlen ("/");
341 else if (strncmp (spec->path, "./", strlen ("./")) == 0)
345 spec->path += strlen ("./");
347 else if (strncmp (spec->path, "../", strlen ("../")) == 0)
350 if (current != NULL && hw_parent (current) != NULL)
351 current = hw_parent (current);
352 spec->path += strlen ("../");
354 else if (strcmp (spec->path, ".") == 0)
358 spec->path += strlen (".");
360 else if (strcmp (spec->path, "..") == 0)
363 if (current != NULL && hw_parent (current) != NULL)
364 current = hw_parent (current);
365 spec->path += strlen ("..");
371 /* now go through the path proper */
375 split_device_name (spec);
379 while (split_device_name (spec))
382 for (child = hw_child (current);
383 child != NULL; child = hw_sibling (child))
385 if (strcmp (spec->name, hw_name (child)) == 0)
387 if (spec->unit == NULL)
392 hw_unit_decode (current, spec->unit, &phys);
393 if (memcmp (&phys, hw_unit_address (child),
394 sizeof (hw_unit)) == 0)
400 return current; /* search failed */
409 split_fill_path (struct hw *current,
410 const char *device_specifier,
411 name_specifier *spec)
414 if (!split_device_specifier (current, device_specifier, spec))
415 hw_abort (current, "error parsing %s\n", device_specifier);
417 /* fill our tree with its contents */
418 current = split_find_device (current, spec);
420 /* add any additional devices as needed */
421 if (spec->name != NULL)
425 if (current != NULL && !hw_finished_p (current))
427 current = hw_create (NULL,
434 while (split_device_name (spec));
441 /* <non-white-space> */
444 skip_token(const char *chp)
446 while (!isspace(*chp) && *chp != '\0')
448 while (isspace(*chp) && *chp != '\0')
454 /* count the number of entries */
457 count_entries (struct hw *current,
458 const char *property_name,
459 const char *property_value,
462 const char *chp = property_value;
467 chp = skip_token (chp);
469 if ((nr_entries % modulo) != 0)
471 hw_abort (current, "incorrect number of entries for %s property %s, should be multiple of %d",
472 property_name, property_value, modulo);
474 return nr_entries / modulo;
479 /* parse: <address> ::= <token> ; device dependant */
482 parse_address (struct hw *current,
487 if (hw_unit_decode (bus, chp, address) < 0)
488 hw_abort (current, "invalid unit address in %s", chp);
489 return skip_token (chp);
493 /* parse: <size> ::= <number> { "," <number> } ; */
496 parse_size (struct hw *current,
503 const char *curr = chp;
504 memset(size, 0, sizeof(*size));
505 /* parse the numeric list */
506 size->nr_cells = hw_unit_nr_size_cells (bus);
511 size->cells[nr] = strtoul (curr, &next, 0);
513 hw_abort (current, "Problem parsing <size> %s", chp);
517 if (nr == size->nr_cells)
518 hw_abort (current, "Too many values in <size> %s", chp);
521 ASSERT (nr > 0 && nr <= size->nr_cells);
522 /* right align the numbers */
523 for (i = 1; i <= size->nr_cells; i++)
526 size->cells[size->nr_cells - i] = size->cells[nr - i];
528 size->cells[size->nr_cells - i] = 0;
530 return skip_token (chp);
534 /* parse: <reg> ::= { <address> <size> } ; */
537 parse_reg_property (struct hw *current,
538 const char *property_name,
539 const char *property_value)
543 reg_property_spec *regs;
546 /* determine the number of reg entries by counting tokens */
547 nr_regs = count_entries (current, property_name, property_value, 2);
549 /* create working space */
550 regs = zalloc (nr_regs * sizeof (*regs));
553 chp = property_value;
554 for (reg_nr = 0; reg_nr < nr_regs; reg_nr++)
556 chp = parse_address (current, hw_parent(current),
557 chp, ®s[reg_nr].address);
558 chp = parse_size (current, hw_parent(current),
559 chp, ®s[reg_nr].size);
563 hw_add_reg_array_property (current, property_name,
570 /* { <child-address> <parent-address> <child-size> }* */
573 parse_ranges_property (struct hw *current,
574 const char *property_name,
575 const char *property_value)
579 range_property_spec *ranges;
582 /* determine the number of ranges specified */
583 nr_ranges = count_entries (current, property_name, property_value, 3);
585 /* create a property of that size */
586 ranges = zalloc (nr_ranges * sizeof(*ranges));
589 chp = property_value;
590 for (range_nr = 0; range_nr < nr_ranges; range_nr++)
592 chp = parse_address (current, current,
593 chp, &ranges[range_nr].child_address);
594 chp = parse_address (current, hw_parent(current),
595 chp, &ranges[range_nr].parent_address);
596 chp = parse_size (current, current,
597 chp, &ranges[range_nr].size);
601 hw_add_range_array_property (current, property_name, ranges, nr_ranges);
610 parse_integer_property (struct hw *current,
611 const char *property_name,
612 const char *property_value)
615 unsigned_cell words[1024];
616 /* integer or integer array? */
621 words[nr_entries] = strtoul (property_value, &end, 0);
622 if (property_value == end)
625 if (nr_entries * sizeof (words[0]) >= sizeof (words))
626 hw_abort (current, "buffer overflow");
627 property_value = end;
630 hw_abort (current, "error parsing integer property %s (%s)",
631 property_name, property_value);
632 else if (nr_entries == 1)
633 hw_add_integer_property (current, property_name, words[0]);
637 for (i = 0; i < nr_entries; i++)
641 /* perhaps integer array property is better */
642 hw_add_array_property (current, property_name, words,
643 sizeof(words[0]) * nr_entries);
651 parse_string_property (struct hw *current,
652 const char *property_name,
653 const char *property_value)
658 int approx_nr_strings;
660 /* get an estimate as to the number of strings by counting double
662 approx_nr_strings = 2;
663 for (chp = property_value; *chp; chp++)
668 approx_nr_strings = (approx_nr_strings) / 2;
670 /* create a string buffer for that many (plus a null) */
671 strings = (char**) zalloc ((approx_nr_strings + 1) * sizeof(char*));
673 /* now find all the strings */
674 chp = property_value;
679 /* skip leading space */
680 while (*chp != '\0' && isspace (*chp))
688 /* a quoted string - watch for '\' et al. */
689 /* estimate the size and allocate space for it */
693 while (chp[pos] != '\0' && chp[pos] != '"')
695 if (chp[pos] == '\\' && chp[pos+1] != '\0')
700 strings[nr_strings] = zalloc (pos + 1);
701 /* copy the string over */
703 while (*chp != '\0' && *chp != '"')
705 if (*chp == '\\' && *(chp+1) != '\0') {
706 strings[nr_strings][pos] = *(chp+1);
712 strings[nr_strings][pos] = *chp;
719 strings[nr_strings][pos] = '\0';
723 /* copy over a single unquoted token */
725 while (chp[len] != '\0' && !isspace(chp[len]))
727 strings[nr_strings] = zalloc(len + 1);
728 strncpy(strings[nr_strings], chp, len);
729 strings[nr_strings][len] = '\0';
733 if (nr_strings > approx_nr_strings)
734 hw_abort (current, "String property %s badly formatted",
737 ASSERT (strings[nr_strings] == NULL); /* from zalloc */
741 hw_add_string_property (current, property_name, "");
742 else if (nr_strings == 1)
743 hw_add_string_property (current, property_name, strings[0]);
746 const char **specs = (const char**) strings; /* stop a bogus error */
747 hw_add_string_array_property (current, property_name,
751 /* flush the created string */
752 while (nr_strings > 0)
755 zfree (strings[nr_strings]);
761 /* <path-to-ihandle-device> */
765 parse_ihandle_property (struct hw *current,
766 const char *property,
769 ihandle_runtime_property_spec ihandle;
771 /* pass the full path */
772 ihandle.full_path = value;
774 /* save this ready for the ihandle create */
775 hw_add_ihandle_runtime_property (current, property,
782 hw_tree_create (SIM_DESC sd,
785 return hw_create (sd, NULL, family, family, NULL, NULL);
789 hw_tree_delete (struct hw *me)
791 /* Need to allow devices to disapear under our feet */
792 while (hw_child (me) != NULL)
794 hw_tree_delete (hw_child (me));
801 hw_tree_parse (struct hw *current,
807 current = hw_tree_vparse (current, fmt, ap);
813 hw_tree_vparse (struct hw *current,
817 char device_specifier[1024];
820 /* format the path */
821 vsprintf (device_specifier, fmt, ap);
822 if (strlen (device_specifier) >= sizeof (device_specifier))
823 hw_abort (NULL, "device_tree_add_deviced: buffer overflow\n");
825 /* construct the tree down to the final struct hw */
826 current = split_fill_path (current, device_specifier, &spec);
828 /* is there an interrupt spec */
829 if (spec.property == NULL
830 && spec.value != NULL)
832 char *op = split_value (&spec);
837 char *my_port_name = split_value (&spec);
839 char *dest_port_name = split_value (&spec);
841 name_specifier dest_spec;
842 char *dest_hw_name = split_value (&spec);
845 if (!hw_finished_p (current))
847 my_port = hw_port_decode (current, my_port_name, output_port);
848 /* find the dest device and port */
849 dest = split_fill_path (current, dest_hw_name, &dest_spec);
850 if (!hw_finished_p (dest))
852 dest_port = hw_port_decode (dest, dest_port_name,
854 /* connect the two */
855 hw_port_attach (current,
863 hw_abort (current, "unreconised interrupt spec %s\n", spec.value);
868 /* is there a property */
869 if (spec.property != NULL)
871 if (strcmp (spec.value, "true") == 0)
872 hw_add_boolean_property (current, spec.property, 1);
873 else if (strcmp (spec.value, "false") == 0)
874 hw_add_boolean_property (current, spec.property, 0);
877 const struct hw_property *property;
878 switch (spec.value[0])
883 parse_ihandle_property (current, spec.property, spec.value + 1);
889 unsigned8 words[1024];
890 char *curr = spec.value + 1;
895 words[nr_words] = H2BE_1 (strtoul (curr, &next, 0));
901 hw_add_array_property (current, spec.property,
902 words, sizeof(words[0]) * nr_words);
907 parse_string_property (current, spec.property, spec.value);
913 property = hw_tree_find_property (current, spec.value);
914 if (property == NULL)
915 hw_abort (current, "property %s not found\n", spec.value);
916 hw_add_duplicate_property (current,
923 if (strcmp (spec.property, "reg") == 0
924 || strcmp (spec.property, "assigned-addresses") == 0
925 || strcmp (spec.property, "alternate-reg") == 0)
927 parse_reg_property (current, spec.property, spec.value);
929 else if (strcmp (spec.property, "ranges") == 0)
931 parse_ranges_property (current, spec.property, spec.value);
933 else if (isdigit(spec.value[0])
934 || (spec.value[0] == '-' && isdigit(spec.value[1]))
935 || (spec.value[0] == '+' && isdigit(spec.value[1])))
937 parse_integer_property(current, spec.property, spec.value);
940 parse_string_property(current, spec.property, spec.value);
951 finish_hw_tree (struct hw *me,
954 if (!hw_finished_p (me))
959 hw_tree_finish (struct hw *root)
961 hw_tree_traverse (root, finish_hw_tree, NULL, NULL);
967 hw_tree_traverse (struct hw *root,
968 hw_tree_traverse_function *prefix,
969 hw_tree_traverse_function *postfix,
975 for (child = hw_child (root);
977 child = hw_sibling (child))
979 hw_tree_traverse (child, prefix, postfix, data);
982 postfix (root, data);
988 hw_tree_print_callback *print;
993 print_address (struct hw *bus,
998 hw_unit_encode (bus, phys, unit, sizeof(unit));
999 p->print (p->file, " %s", unit);
1003 print_size (struct hw *bus,
1004 const hw_unit *size,
1008 for (i = 0; i < size->nr_cells; i++)
1009 if (size->cells[i] != 0)
1011 if (i < size->nr_cells) {
1012 p->print (p->file, " 0x%lx", (unsigned long) size->cells[i]);
1014 for (; i < size->nr_cells; i++)
1015 p->print (p->file, ",0x%lx", (unsigned long) size->cells[i]);
1018 p->print (p->file, " 0");
1022 print_reg_property (struct hw *me,
1023 const struct hw_property *property,
1027 reg_property_spec reg;
1029 hw_find_reg_array_property (me, property->name, reg_nr, ®);
1031 print_address (hw_parent (me), ®.address, p);
1032 print_size (me, ®.size, p);
1037 print_ranges_property (struct hw *me,
1038 const struct hw_property *property,
1042 range_property_spec range;
1044 hw_find_range_array_property (me, property->name, range_nr, &range);
1047 print_address (me, &range.child_address, p);
1048 print_address (hw_parent (me), &range.parent_address, p);
1049 print_size (me, &range.size, p);
1054 print_string (struct hw *me,
1058 p->print (p->file, " \"");
1059 while (*string != '\0') {
1062 p->print (p->file, "\\\"");
1065 p->print (p->file, "\\\\");
1068 p->print (p->file, "%c", *string);
1073 p->print (p->file, "\"");
1077 print_string_array_property (struct hw *me,
1078 const struct hw_property *property,
1082 string_property_spec string;
1084 hw_find_string_array_property (me, property->name, nr, &string);
1087 print_string (me, string, p);
1092 print_properties (struct hw *me,
1095 const struct hw_property *property;
1096 for (property = hw_find_property (me, NULL);
1098 property = hw_next_property (property))
1100 if (hw_parent (me) == NULL)
1101 p->print (p->file, "/%s", property->name);
1103 p->print (p->file, "%s/%s", hw_path (me), property->name);
1104 if (property->original != NULL)
1106 p->print (p->file, " !");
1107 p->print (p->file, "%s/%s",
1108 hw_path (property->original->owner),
1109 property->original->name);
1113 switch (property->type)
1115 case array_property:
1117 if ((property->sizeof_array % sizeof (signed_cell)) == 0)
1119 unsigned_cell *w = (unsigned_cell*) property->array;
1122 cell_nr < (property->sizeof_array / sizeof (unsigned_cell));
1125 p->print (p->file, " 0x%lx", (unsigned long) BE2H_cell (w[cell_nr]));
1130 unsigned8 *w = (unsigned8*)property->array;
1131 p->print (p->file, " [");
1132 while ((char*)w - (char*)property->array < property->sizeof_array) {
1133 p->print (p->file, " 0x%2x", BE2H_1 (*w));
1139 case boolean_property:
1141 int b = hw_find_boolean_property(me, property->name);
1142 p->print (p->file, " %s", b ? "true" : "false");
1146 case ihandle_property:
1148 if (property->array != NULL)
1150 device_instance *instance = hw_find_ihandle_property (me, property->name);
1151 p->print (p->file, " *%s", device_instance_path(instance));
1155 /* not yet initialized, ask the device for the path */
1156 ihandle_runtime_property_spec spec;
1157 hw_find_ihandle_runtime_property (me, property->name, &spec);
1158 p->print (p->file, " *%s", spec.full_path);
1163 case integer_property:
1165 unsigned_word w = hw_find_integer_property (me, property->name);
1166 p->print (p->file, " 0x%lx", (unsigned long)w);
1169 case range_array_property:
1171 print_ranges_property (me, property, p);
1174 case reg_array_property:
1176 print_reg_property (me, property, p);
1179 case string_property:
1181 const char *s = hw_find_string_property (me, property->name);
1182 print_string (me, s, p);
1185 case string_array_property:
1187 print_string_array_property (me, property, p);
1192 p->print (p->file, "\n");
1197 print_interrupts (struct hw *me,
1203 struct printer *p = data;
1206 hw_port_encode (me, my_port, src, sizeof(src), output_port);
1207 hw_port_encode (dest, dest_port, dst, sizeof(dst), input_port);
1216 print_device (struct hw *me,
1219 struct printer *p = data;
1220 p->print (p->file, "%s\n", hw_path (me));
1221 print_properties (me, p);
1222 hw_port_traverse (me, print_interrupts, data);
1226 hw_tree_print (struct hw *root,
1227 hw_tree_print_callback *print,
1233 hw_tree_traverse (root,
1242 tree_instance(struct hw *root,
1243 const char *device_specifier)
1245 /* find the device node */
1247 name_specifier spec;
1248 if (!split_device_specifier(root, device_specifier, &spec))
1250 me = split_find_device(root, &spec);
1251 if (spec.name != NULL)
1253 /* create the instance */
1254 return device_create_instance(me, device_specifier, spec.last_args);
1259 hw_tree_find_device (struct hw *root,
1260 const char *path_to_device)
1263 name_specifier spec;
1265 /* parse the path */
1266 split_device_specifier (root, path_to_device, &spec);
1267 if (spec.value != NULL)
1268 return NULL; /* something wierd */
1271 node = split_find_device (root, &spec);
1272 if (spec.name != NULL)
1273 return NULL; /* not a leaf */
1279 const struct hw_property *
1280 hw_tree_find_property (struct hw *root,
1281 const char *path_to_property)
1283 name_specifier spec;
1284 if (!split_property_specifier (root, path_to_property, &spec))
1285 hw_abort (root, "Invalid property path %s", path_to_property);
1286 root = split_find_device (root, &spec);
1287 if (spec.name != NULL)
1288 return NULL; /* not a leaf */
1289 return hw_find_property (root, spec.property);
1293 hw_tree_find_boolean_property (struct hw *root,
1294 const char *path_to_property)
1296 name_specifier spec;
1297 if (!split_property_specifier (root, path_to_property, &spec))
1298 hw_abort (root, "Invalid property path %s", path_to_property);
1299 root = split_find_device (root, &spec);
1300 if (spec.name != NULL)
1301 hw_abort (root, "device \"%s\" not found (property \"%s\")",
1302 spec.name, path_to_property);
1303 return hw_find_boolean_property (root, spec.property);
1307 hw_tree_find_integer_property (struct hw *root,
1308 const char *path_to_property)
1310 name_specifier spec;
1311 if (!split_property_specifier (root, path_to_property, &spec))
1312 hw_abort (root, "Invalid property path %s", path_to_property);
1313 root = split_find_device (root, &spec);
1314 if (spec.name != NULL)
1315 hw_abort (root, "device \"%s\" not found (property \"%s\")",
1316 spec.name, path_to_property);
1317 return hw_find_integer_property (root, spec.property);
1322 hw_tree_find_ihandle_property (struct hw *root,
1323 const char *path_to_property)
1326 name_specifier spec;
1327 if (!split_property_specifier (root, path_to_property, &spec))
1328 hw_abort (root, "Invalid property path %s", path_to_property);
1329 root = split_find_device (root, &spec);
1330 if (spec.name != NULL)
1331 hw_abort (root, "device \"%s\" not found (property \"%s\")",
1332 spec.name, path_to_property);
1333 return hw_find_ihandle_property (root, spec.property);
1338 hw_tree_find_string_property (struct hw *root,
1339 const char *path_to_property)
1341 name_specifier spec;
1342 if (!split_property_specifier (root, path_to_property, &spec))
1343 hw_abort (root, "Invalid property path %s", path_to_property);
1344 root = split_find_device (root, &spec);
1345 if (spec.name != NULL)
1346 hw_abort (root, "device \"%s\" not found (property \"%s\")",
1347 spec.name, path_to_property);
1348 return hw_find_string_property (root, spec.property);