1 /* The common simulator framework for GDB, the GNU Debugger.
3 Copyright 2002, 2007 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/>. */
28 #include "sim-assert.h"
44 /* manipulate/lookup device names */
46 typedef struct _name_specifier {
48 /* components in the full length name */
72 /* Given a device specifier, break it up into its main components:
73 path (and if present) property name and property value. */
76 split_device_specifier (struct hw *current,
77 const char *device_specifier,
82 /* expand any leading alias if present */
84 && *device_specifier != '\0'
85 && *device_specifier != '.'
86 && *device_specifier != '/')
88 struct hw *aliases = hw_tree_find_device (current, "/aliases");
91 while (device_specifier[len] != '\0'
92 && device_specifier[len] != '/'
93 && device_specifier[len] != ':'
94 && !isspace (device_specifier[len]))
96 alias[len] = device_specifier[len];
98 if (len >= sizeof(alias))
99 hw_abort (NULL, "split_device_specifier: buffer overflow");
103 && hw_find_property (aliases, alias))
105 strcpy (spec->buf, hw_find_string_property(aliases, alias));
106 strcat (spec->buf, device_specifier + len);
110 strcpy (spec->buf, device_specifier);
115 strcpy(spec->buf, device_specifier);
118 /* check no overflow */
119 if (strlen(spec->buf) >= sizeof(spec->buf))
120 hw_abort (NULL, "split_device_specifier: buffer overflow\n");
122 /* strip leading spaces */
124 while (*chp != '\0' && isspace(*chp))
129 /* find the path and terminate it with null */
131 while (*chp != '\0' && !isspace(*chp))
140 while (*chp != '\0' && isspace(*chp))
144 /* now go back and chop the property off of the path */
145 if (spec->value[0] == '\0')
147 spec->property = NULL; /*not a property*/
150 else if (spec->value[0] == '>'
151 || spec->value[0] == '<')
153 /* an interrupt spec */
154 spec->property = NULL;
157 chp = strrchr(spec->path, '/');
160 spec->property = spec->path;
161 spec->path = strchr(spec->property, '\0');
165 spec->property = chp+1;
169 /* and mark the rest as invalid */
174 spec->last_name = NULL;
175 spec->last_family = NULL;
176 spec->last_unit = NULL;
177 spec->last_args = NULL;
183 /* given a device specifier break it up into its main components -
184 path and property name - assuming that the last `device' is a
188 split_property_specifier (struct hw *current,
189 const char *property_specifier,
190 name_specifier *spec)
192 if (split_device_specifier (current, property_specifier, spec))
194 if (spec->property == NULL)
196 /* force the last name to be a property name */
197 char *chp = strrchr (spec->path, '/');
200 spec->property = spec->path;
201 spec->path = strrchr (spec->property, '\0');;
206 spec->property = chp + 1;
216 /* device the next device name and split it up, return 0 when no more
217 names to struct hw */
220 split_device_name (name_specifier *spec)
223 /* remember what came before */
224 spec->last_name = spec->name;
225 spec->last_family = spec->family;
226 spec->last_unit = spec->unit;
227 spec->last_args = spec->args;
229 if (spec->path[0] == '\0')
237 /* break the current device spec from the path */
238 spec->name = spec->path;
239 chp = strchr (spec->name, '/');
241 spec->path = strchr (spec->name, '\0');
247 /* break out the base */
248 if (spec->name[0] == '(')
250 chp = strchr(spec->name, ')');
253 spec->family = spec->name;
258 spec->family = spec->name + 1;
259 spec->name = chp + 1;
264 spec->family = spec->name;
266 /* now break out the unit */
267 chp = strchr(spec->name, '@');
279 /* finally any args */
280 chp = strchr(chp, ':');
292 /* device the value, returning the next non-space token */
295 split_value (name_specifier *spec)
298 if (spec->value == NULL)
300 /* skip leading white space */
301 while (isspace (spec->value[0]))
303 if (spec->value[0] == '\0')
309 /* find trailing space */
310 while (spec->value[0] != '\0' && !isspace (spec->value[0]))
312 /* chop this value out */
313 if (spec->value[0] != '\0')
315 spec->value[0] = '\0';
323 /* traverse the path specified by spec starting at current */
326 split_find_device (struct hw *current,
327 name_specifier *spec)
329 /* strip off (and process) any leading ., .., ./ and / */
332 if (strncmp (spec->path, "/", strlen ("/")) == 0)
335 while (current != NULL && hw_parent (current) != NULL)
336 current = hw_parent (current);
337 spec->path += strlen ("/");
339 else if (strncmp (spec->path, "./", strlen ("./")) == 0)
343 spec->path += strlen ("./");
345 else if (strncmp (spec->path, "../", strlen ("../")) == 0)
348 if (current != NULL && hw_parent (current) != NULL)
349 current = hw_parent (current);
350 spec->path += strlen ("../");
352 else if (strcmp (spec->path, ".") == 0)
356 spec->path += strlen (".");
358 else if (strcmp (spec->path, "..") == 0)
361 if (current != NULL && hw_parent (current) != NULL)
362 current = hw_parent (current);
363 spec->path += strlen ("..");
369 /* now go through the path proper */
373 split_device_name (spec);
377 while (split_device_name (spec))
380 for (child = hw_child (current);
381 child != NULL; child = hw_sibling (child))
383 if (strcmp (spec->name, hw_name (child)) == 0)
385 if (spec->unit == NULL)
390 hw_unit_decode (current, spec->unit, &phys);
391 if (memcmp (&phys, hw_unit_address (child),
392 sizeof (hw_unit)) == 0)
398 return current; /* search failed */
407 split_fill_path (struct hw *current,
408 const char *device_specifier,
409 name_specifier *spec)
412 if (!split_device_specifier (current, device_specifier, spec))
413 hw_abort (current, "error parsing %s\n", device_specifier);
415 /* fill our tree with its contents */
416 current = split_find_device (current, spec);
418 /* add any additional devices as needed */
419 if (spec->name != NULL)
423 if (current != NULL && !hw_finished_p (current))
425 current = hw_create (NULL,
432 while (split_device_name (spec));
439 /* <non-white-space> */
442 skip_token(const char *chp)
444 while (!isspace(*chp) && *chp != '\0')
446 while (isspace(*chp) && *chp != '\0')
452 /* count the number of entries */
455 count_entries (struct hw *current,
456 const char *property_name,
457 const char *property_value,
460 const char *chp = property_value;
465 chp = skip_token (chp);
467 if ((nr_entries % modulo) != 0)
469 hw_abort (current, "incorrect number of entries for %s property %s, should be multiple of %d",
470 property_name, property_value, modulo);
472 return nr_entries / modulo;
477 /* parse: <address> ::= <token> ; device dependant */
480 parse_address (struct hw *current,
485 if (hw_unit_decode (bus, chp, address) < 0)
486 hw_abort (current, "invalid unit address in %s", chp);
487 return skip_token (chp);
491 /* parse: <size> ::= <number> { "," <number> } ; */
494 parse_size (struct hw *current,
501 const char *curr = chp;
502 memset(size, 0, sizeof(*size));
503 /* parse the numeric list */
504 size->nr_cells = hw_unit_nr_size_cells (bus);
509 size->cells[nr] = strtoul (curr, &next, 0);
511 hw_abort (current, "Problem parsing <size> %s", chp);
515 if (nr == size->nr_cells)
516 hw_abort (current, "Too many values in <size> %s", chp);
519 ASSERT (nr > 0 && nr <= size->nr_cells);
520 /* right align the numbers */
521 for (i = 1; i <= size->nr_cells; i++)
524 size->cells[size->nr_cells - i] = size->cells[nr - i];
526 size->cells[size->nr_cells - i] = 0;
528 return skip_token (chp);
532 /* parse: <reg> ::= { <address> <size> } ; */
535 parse_reg_property (struct hw *current,
536 const char *property_name,
537 const char *property_value)
541 reg_property_spec *regs;
544 /* determine the number of reg entries by counting tokens */
545 nr_regs = count_entries (current, property_name, property_value, 2);
547 /* create working space */
548 regs = zalloc (nr_regs * sizeof (*regs));
551 chp = property_value;
552 for (reg_nr = 0; reg_nr < nr_regs; reg_nr++)
554 chp = parse_address (current, hw_parent(current),
555 chp, ®s[reg_nr].address);
556 chp = parse_size (current, hw_parent(current),
557 chp, ®s[reg_nr].size);
561 hw_add_reg_array_property (current, property_name,
568 /* { <child-address> <parent-address> <child-size> }* */
571 parse_ranges_property (struct hw *current,
572 const char *property_name,
573 const char *property_value)
577 range_property_spec *ranges;
580 /* determine the number of ranges specified */
581 nr_ranges = count_entries (current, property_name, property_value, 3);
583 /* create a property of that size */
584 ranges = zalloc (nr_ranges * sizeof(*ranges));
587 chp = property_value;
588 for (range_nr = 0; range_nr < nr_ranges; range_nr++)
590 chp = parse_address (current, current,
591 chp, &ranges[range_nr].child_address);
592 chp = parse_address (current, hw_parent(current),
593 chp, &ranges[range_nr].parent_address);
594 chp = parse_size (current, current,
595 chp, &ranges[range_nr].size);
599 hw_add_range_array_property (current, property_name, ranges, nr_ranges);
608 parse_integer_property (struct hw *current,
609 const char *property_name,
610 const char *property_value)
613 unsigned_cell words[1024];
614 /* integer or integer array? */
619 words[nr_entries] = strtoul (property_value, &end, 0);
620 if (property_value == end)
623 if (nr_entries * sizeof (words[0]) >= sizeof (words))
624 hw_abort (current, "buffer overflow");
625 property_value = end;
628 hw_abort (current, "error parsing integer property %s (%s)",
629 property_name, property_value);
630 else if (nr_entries == 1)
631 hw_add_integer_property (current, property_name, words[0]);
635 for (i = 0; i < nr_entries; i++)
639 /* perhaps integer array property is better */
640 hw_add_array_property (current, property_name, words,
641 sizeof(words[0]) * nr_entries);
649 parse_string_property (struct hw *current,
650 const char *property_name,
651 const char *property_value)
656 int approx_nr_strings;
658 /* get an estimate as to the number of strings by counting double
660 approx_nr_strings = 2;
661 for (chp = property_value; *chp; chp++)
666 approx_nr_strings = (approx_nr_strings) / 2;
668 /* create a string buffer for that many (plus a null) */
669 strings = (char**) zalloc ((approx_nr_strings + 1) * sizeof(char*));
671 /* now find all the strings */
672 chp = property_value;
677 /* skip leading space */
678 while (*chp != '\0' && isspace (*chp))
686 /* a quoted string - watch for '\' et al. */
687 /* estimate the size and allocate space for it */
691 while (chp[pos] != '\0' && chp[pos] != '"')
693 if (chp[pos] == '\\' && chp[pos+1] != '\0')
698 strings[nr_strings] = zalloc (pos + 1);
699 /* copy the string over */
701 while (*chp != '\0' && *chp != '"')
703 if (*chp == '\\' && *(chp+1) != '\0') {
704 strings[nr_strings][pos] = *(chp+1);
710 strings[nr_strings][pos] = *chp;
717 strings[nr_strings][pos] = '\0';
721 /* copy over a single unquoted token */
723 while (chp[len] != '\0' && !isspace(chp[len]))
725 strings[nr_strings] = zalloc(len + 1);
726 strncpy(strings[nr_strings], chp, len);
727 strings[nr_strings][len] = '\0';
731 if (nr_strings > approx_nr_strings)
732 hw_abort (current, "String property %s badly formatted",
735 ASSERT (strings[nr_strings] == NULL); /* from zalloc */
739 hw_add_string_property (current, property_name, "");
740 else if (nr_strings == 1)
741 hw_add_string_property (current, property_name, strings[0]);
744 const char **specs = (const char**) strings; /* stop a bogus error */
745 hw_add_string_array_property (current, property_name,
749 /* flush the created string */
750 while (nr_strings > 0)
753 zfree (strings[nr_strings]);
759 /* <path-to-ihandle-device> */
763 parse_ihandle_property (struct hw *current,
764 const char *property,
767 ihandle_runtime_property_spec ihandle;
769 /* pass the full path */
770 ihandle.full_path = value;
772 /* save this ready for the ihandle create */
773 hw_add_ihandle_runtime_property (current, property,
780 hw_tree_create (SIM_DESC sd,
783 return hw_create (sd, NULL, family, family, NULL, NULL);
787 hw_tree_delete (struct hw *me)
789 /* Need to allow devices to disapear under our feet */
790 while (hw_child (me) != NULL)
792 hw_tree_delete (hw_child (me));
799 hw_tree_parse (struct hw *current,
805 current = hw_tree_vparse (current, fmt, ap);
811 hw_tree_vparse (struct hw *current,
815 char device_specifier[1024];
818 /* format the path */
819 vsprintf (device_specifier, fmt, ap);
820 if (strlen (device_specifier) >= sizeof (device_specifier))
821 hw_abort (NULL, "device_tree_add_deviced: buffer overflow\n");
823 /* construct the tree down to the final struct hw */
824 current = split_fill_path (current, device_specifier, &spec);
826 /* is there an interrupt spec */
827 if (spec.property == NULL
828 && spec.value != NULL)
830 char *op = split_value (&spec);
835 char *my_port_name = split_value (&spec);
837 char *dest_port_name = split_value (&spec);
839 name_specifier dest_spec;
840 char *dest_hw_name = split_value (&spec);
843 if (!hw_finished_p (current))
845 my_port = hw_port_decode (current, my_port_name, output_port);
846 /* find the dest device and port */
847 dest = split_fill_path (current, dest_hw_name, &dest_spec);
848 if (!hw_finished_p (dest))
850 dest_port = hw_port_decode (dest, dest_port_name,
852 /* connect the two */
853 hw_port_attach (current,
861 hw_abort (current, "unreconised interrupt spec %s\n", spec.value);
866 /* is there a property */
867 if (spec.property != NULL)
869 if (strcmp (spec.value, "true") == 0)
870 hw_add_boolean_property (current, spec.property, 1);
871 else if (strcmp (spec.value, "false") == 0)
872 hw_add_boolean_property (current, spec.property, 0);
875 const struct hw_property *property;
876 switch (spec.value[0])
881 parse_ihandle_property (current, spec.property, spec.value + 1);
887 unsigned8 words[1024];
888 char *curr = spec.value + 1;
893 words[nr_words] = H2BE_1 (strtoul (curr, &next, 0));
899 hw_add_array_property (current, spec.property,
900 words, sizeof(words[0]) * nr_words);
905 parse_string_property (current, spec.property, spec.value);
911 property = hw_tree_find_property (current, spec.value);
912 if (property == NULL)
913 hw_abort (current, "property %s not found\n", spec.value);
914 hw_add_duplicate_property (current,
921 if (strcmp (spec.property, "reg") == 0
922 || strcmp (spec.property, "assigned-addresses") == 0
923 || strcmp (spec.property, "alternate-reg") == 0)
925 parse_reg_property (current, spec.property, spec.value);
927 else if (strcmp (spec.property, "ranges") == 0)
929 parse_ranges_property (current, spec.property, spec.value);
931 else if (isdigit(spec.value[0])
932 || (spec.value[0] == '-' && isdigit(spec.value[1]))
933 || (spec.value[0] == '+' && isdigit(spec.value[1])))
935 parse_integer_property(current, spec.property, spec.value);
938 parse_string_property(current, spec.property, spec.value);
949 finish_hw_tree (struct hw *me,
952 if (!hw_finished_p (me))
957 hw_tree_finish (struct hw *root)
959 hw_tree_traverse (root, finish_hw_tree, NULL, NULL);
965 hw_tree_traverse (struct hw *root,
966 hw_tree_traverse_function *prefix,
967 hw_tree_traverse_function *postfix,
973 for (child = hw_child (root);
975 child = hw_sibling (child))
977 hw_tree_traverse (child, prefix, postfix, data);
980 postfix (root, data);
986 hw_tree_print_callback *print;
991 print_address (struct hw *bus,
996 hw_unit_encode (bus, phys, unit, sizeof(unit));
997 p->print (p->file, " %s", unit);
1001 print_size (struct hw *bus,
1002 const hw_unit *size,
1006 for (i = 0; i < size->nr_cells; i++)
1007 if (size->cells[i] != 0)
1009 if (i < size->nr_cells) {
1010 p->print (p->file, " 0x%lx", (unsigned long) size->cells[i]);
1012 for (; i < size->nr_cells; i++)
1013 p->print (p->file, ",0x%lx", (unsigned long) size->cells[i]);
1016 p->print (p->file, " 0");
1020 print_reg_property (struct hw *me,
1021 const struct hw_property *property,
1025 reg_property_spec reg;
1027 hw_find_reg_array_property (me, property->name, reg_nr, ®);
1029 print_address (hw_parent (me), ®.address, p);
1030 print_size (me, ®.size, p);
1035 print_ranges_property (struct hw *me,
1036 const struct hw_property *property,
1040 range_property_spec range;
1042 hw_find_range_array_property (me, property->name, range_nr, &range);
1045 print_address (me, &range.child_address, p);
1046 print_address (hw_parent (me), &range.parent_address, p);
1047 print_size (me, &range.size, p);
1052 print_string (struct hw *me,
1056 p->print (p->file, " \"");
1057 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 unsigned8 *w = (unsigned8*)property->array;
1129 p->print (p->file, " [");
1130 while ((char*)w - (char*)property->array < property->sizeof_array) {
1131 p->print (p->file, " 0x%2x", BE2H_1 (*w));
1137 case boolean_property:
1139 int b = hw_find_boolean_property(me, property->name);
1140 p->print (p->file, " %s", b ? "true" : "false");
1144 case ihandle_property:
1146 if (property->array != NULL)
1148 device_instance *instance = hw_find_ihandle_property (me, property->name);
1149 p->print (p->file, " *%s", device_instance_path(instance));
1153 /* not yet initialized, ask the device for the path */
1154 ihandle_runtime_property_spec spec;
1155 hw_find_ihandle_runtime_property (me, property->name, &spec);
1156 p->print (p->file, " *%s", spec.full_path);
1161 case integer_property:
1163 unsigned_word w = hw_find_integer_property (me, property->name);
1164 p->print (p->file, " 0x%lx", (unsigned long)w);
1167 case range_array_property:
1169 print_ranges_property (me, property, p);
1172 case reg_array_property:
1174 print_reg_property (me, property, p);
1177 case string_property:
1179 const char *s = hw_find_string_property (me, property->name);
1180 print_string (me, s, p);
1183 case string_array_property:
1185 print_string_array_property (me, property, p);
1190 p->print (p->file, "\n");
1195 print_interrupts (struct hw *me,
1201 struct printer *p = data;
1204 hw_port_encode (me, my_port, src, sizeof(src), output_port);
1205 hw_port_encode (dest, dest_port, dst, sizeof(dst), input_port);
1214 print_device (struct hw *me,
1217 struct printer *p = data;
1218 p->print (p->file, "%s\n", hw_path (me));
1219 print_properties (me, p);
1220 hw_port_traverse (me, print_interrupts, data);
1224 hw_tree_print (struct hw *root,
1225 hw_tree_print_callback *print,
1231 hw_tree_traverse (root,
1240 tree_instance(struct hw *root,
1241 const char *device_specifier)
1243 /* find the device node */
1245 name_specifier spec;
1246 if (!split_device_specifier(root, device_specifier, &spec))
1248 me = split_find_device(root, &spec);
1249 if (spec.name != NULL)
1251 /* create the instance */
1252 return device_create_instance(me, device_specifier, spec.last_args);
1257 hw_tree_find_device (struct hw *root,
1258 const char *path_to_device)
1261 name_specifier spec;
1263 /* parse the path */
1264 split_device_specifier (root, path_to_device, &spec);
1265 if (spec.value != NULL)
1266 return NULL; /* something wierd */
1269 node = split_find_device (root, &spec);
1270 if (spec.name != NULL)
1271 return NULL; /* not a leaf */
1277 const struct hw_property *
1278 hw_tree_find_property (struct hw *root,
1279 const char *path_to_property)
1281 name_specifier spec;
1282 if (!split_property_specifier (root, path_to_property, &spec))
1283 hw_abort (root, "Invalid property path %s", path_to_property);
1284 root = split_find_device (root, &spec);
1285 if (spec.name != NULL)
1286 return NULL; /* not a leaf */
1287 return hw_find_property (root, spec.property);
1291 hw_tree_find_boolean_property (struct hw *root,
1292 const char *path_to_property)
1294 name_specifier spec;
1295 if (!split_property_specifier (root, path_to_property, &spec))
1296 hw_abort (root, "Invalid property path %s", path_to_property);
1297 root = split_find_device (root, &spec);
1298 if (spec.name != NULL)
1299 hw_abort (root, "device \"%s\" not found (property \"%s\")",
1300 spec.name, path_to_property);
1301 return hw_find_boolean_property (root, spec.property);
1305 hw_tree_find_integer_property (struct hw *root,
1306 const char *path_to_property)
1308 name_specifier spec;
1309 if (!split_property_specifier (root, path_to_property, &spec))
1310 hw_abort (root, "Invalid property path %s", path_to_property);
1311 root = split_find_device (root, &spec);
1312 if (spec.name != NULL)
1313 hw_abort (root, "device \"%s\" not found (property \"%s\")",
1314 spec.name, path_to_property);
1315 return hw_find_integer_property (root, spec.property);
1320 hw_tree_find_ihandle_property (struct hw *root,
1321 const char *path_to_property)
1324 name_specifier spec;
1325 if (!split_property_specifier (root, path_to_property, &spec))
1326 hw_abort (root, "Invalid property path %s", path_to_property);
1327 root = split_find_device (root, &spec);
1328 if (spec.name != NULL)
1329 hw_abort (root, "device \"%s\" not found (property \"%s\")",
1330 spec.name, path_to_property);
1331 return hw_find_ihandle_property (root, spec.property);
1336 hw_tree_find_string_property (struct hw *root,
1337 const char *path_to_property)
1339 name_specifier spec;
1340 if (!split_property_specifier (root, path_to_property, &spec))
1341 hw_abort (root, "Invalid property path %s", path_to_property);
1342 root = split_find_device (root, &spec);
1343 if (spec.name != NULL)
1344 hw_abort (root, "device \"%s\" not found (property \"%s\")",
1345 spec.name, path_to_property);
1346 return hw_find_string_property (root, spec.property);