1 /* The common simulator framework for GDB, the GNU Debugger.
3 Copyright 2002-2017 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
49 /* components in the full length name */
73 /* Given a device specifier, break it up into its main components:
74 path (and if present) property name and property value. */
77 split_device_specifier (struct hw *current,
78 const char *device_specifier,
83 /* expand any leading alias if present */
85 && *device_specifier != '\0'
86 && *device_specifier != '.'
87 && *device_specifier != '/')
89 struct hw *aliases = hw_tree_find_device (current, "/aliases");
92 while (device_specifier[len] != '\0'
93 && device_specifier[len] != '/'
94 && device_specifier[len] != ':'
95 && !isspace (device_specifier[len]))
97 alias[len] = device_specifier[len];
99 if (len >= sizeof (alias))
100 hw_abort (NULL, "split_device_specifier: buffer overflow");
104 && hw_find_property (aliases, alias))
106 strcpy (spec->buf, hw_find_string_property (aliases, alias));
107 strcat (spec->buf, device_specifier + len);
111 strcpy (spec->buf, device_specifier);
116 strcpy (spec->buf, device_specifier);
119 /* check no overflow */
120 if (strlen (spec->buf) >= sizeof (spec->buf))
121 hw_abort (NULL, "split_device_specifier: buffer overflow\n");
123 /* strip leading spaces */
125 while (*chp != '\0' && isspace (*chp))
130 /* find the path and terminate it with null */
132 while (*chp != '\0' && !isspace (*chp))
141 while (*chp != '\0' && isspace (*chp))
145 /* now go back and chop the property off of the path */
146 if (spec->value[0] == '\0')
148 spec->property = NULL; /*not a property*/
151 else if (spec->value[0] == '>'
152 || spec->value[0] == '<')
154 /* an interrupt spec */
155 spec->property = NULL;
159 chp = strrchr (spec->path, '/');
162 spec->property = spec->path;
163 spec->path = strchr (spec->property, '\0');
168 spec->property = chp+1;
172 /* and mark the rest as invalid */
177 spec->last_name = NULL;
178 spec->last_family = NULL;
179 spec->last_unit = NULL;
180 spec->last_args = NULL;
186 /* given a device specifier break it up into its main components -
187 path and property name - assuming that the last `device' is a
191 split_property_specifier (struct hw *current,
192 const char *property_specifier,
193 name_specifier *spec)
195 if (split_device_specifier (current, property_specifier, spec))
197 if (spec->property == NULL)
199 /* force the last name to be a property name */
200 char *chp = strrchr (spec->path, '/');
203 spec->property = spec->path;
204 spec->path = strrchr (spec->property, '\0');;
209 spec->property = chp + 1;
219 /* device the next device name and split it up, return 0 when no more
220 names to struct hw */
223 split_device_name (name_specifier *spec)
226 /* remember what came before */
227 spec->last_name = spec->name;
228 spec->last_family = spec->family;
229 spec->last_unit = spec->unit;
230 spec->last_args = spec->args;
232 if (spec->path[0] == '\0')
240 /* break the current device spec from the path */
241 spec->name = spec->path;
242 chp = strchr (spec->name, '/');
244 spec->path = strchr (spec->name, '\0');
250 /* break out the base */
251 if (spec->name[0] == '(')
253 chp = strchr (spec->name, ')');
256 spec->family = spec->name;
261 spec->family = spec->name + 1;
262 spec->name = chp + 1;
267 spec->family = spec->name;
269 /* now break out the unit */
270 chp = strchr (spec->name, '@');
282 /* finally any args */
283 chp = strchr (chp, ':');
295 /* device the value, returning the next non-space token */
298 split_value (name_specifier *spec)
301 if (spec->value == NULL)
303 /* skip leading white space */
304 while (isspace (spec->value[0]))
306 if (spec->value[0] == '\0')
312 /* find trailing space */
313 while (spec->value[0] != '\0' && !isspace (spec->value[0]))
315 /* chop this value out */
316 if (spec->value[0] != '\0')
318 spec->value[0] = '\0';
326 /* traverse the path specified by spec starting at current */
329 split_find_device (struct hw *current,
330 name_specifier *spec)
332 /* strip off (and process) any leading ., .., ./ and / */
335 if (strncmp (spec->path, "/", strlen ("/")) == 0)
338 while (current != NULL && hw_parent (current) != NULL)
339 current = hw_parent (current);
340 spec->path += strlen ("/");
342 else if (strncmp (spec->path, "./", strlen ("./")) == 0)
346 spec->path += strlen ("./");
348 else if (strncmp (spec->path, "../", strlen ("../")) == 0)
351 if (current != NULL && hw_parent (current) != NULL)
352 current = hw_parent (current);
353 spec->path += strlen ("../");
355 else if (strcmp (spec->path, ".") == 0)
359 spec->path += strlen (".");
361 else if (strcmp (spec->path, "..") == 0)
364 if (current != NULL && hw_parent (current) != NULL)
365 current = hw_parent (current);
366 spec->path += strlen ("..");
372 /* now go through the path proper */
376 split_device_name (spec);
380 while (split_device_name (spec))
383 for (child = hw_child (current);
384 child != NULL; child = hw_sibling (child))
386 if (strcmp (spec->name, hw_name (child)) == 0)
388 if (spec->unit == NULL)
393 hw_unit_decode (current, spec->unit, &phys);
394 if (memcmp (&phys, hw_unit_address (child),
395 sizeof (hw_unit)) == 0)
401 return current; /* search failed */
410 split_fill_path (struct hw *current,
411 const char *device_specifier,
412 name_specifier *spec)
415 if (!split_device_specifier (current, device_specifier, spec))
416 hw_abort (current, "error parsing %s\n", device_specifier);
418 /* fill our tree with its contents */
419 current = split_find_device (current, spec);
421 /* add any additional devices as needed */
422 if (spec->name != NULL)
426 if (current != NULL && !hw_finished_p (current))
428 current = hw_create (NULL,
435 while (split_device_name (spec));
442 /* <non-white-space> */
445 skip_token (const char *chp)
447 while (!isspace (*chp) && *chp != '\0')
449 while (isspace (*chp) && *chp != '\0')
455 /* count the number of entries */
458 count_entries (struct hw *current,
459 const char *property_name,
460 const char *property_value,
463 const char *chp = property_value;
468 chp = skip_token (chp);
470 if ((nr_entries % modulo) != 0)
472 hw_abort (current, "incorrect number of entries for %s property %s, should be multiple of %d",
473 property_name, property_value, modulo);
475 return nr_entries / modulo;
480 /* parse: <address> ::= <token> ; device dependant */
483 parse_address (struct hw *current,
488 if (hw_unit_decode (bus, chp, address) < 0)
489 hw_abort (current, "invalid unit address in %s", chp);
490 return skip_token (chp);
494 /* parse: <size> ::= <number> { "," <number> } ; */
497 parse_size (struct hw *current,
504 const char *curr = chp;
505 memset (size, 0, sizeof (*size));
506 /* parse the numeric list */
507 size->nr_cells = hw_unit_nr_size_cells (bus);
512 size->cells[nr] = strtoul (curr, &next, 0);
514 hw_abort (current, "Problem parsing <size> %s", chp);
518 if (nr == size->nr_cells)
519 hw_abort (current, "Too many values in <size> %s", chp);
522 ASSERT (nr > 0 && nr <= size->nr_cells);
523 /* right align the numbers */
524 for (i = 1; i <= size->nr_cells; i++)
527 size->cells[size->nr_cells - i] = size->cells[nr - i];
529 size->cells[size->nr_cells - i] = 0;
531 return skip_token (chp);
535 /* parse: <reg> ::= { <address> <size> } ; */
538 parse_reg_property (struct hw *current,
539 const char *property_name,
540 const char *property_value)
544 reg_property_spec *regs;
547 /* determine the number of reg entries by counting tokens */
548 nr_regs = count_entries (current, property_name, property_value, 2);
550 /* create working space */
551 regs = zalloc (nr_regs * sizeof (*regs));
554 chp = property_value;
555 for (reg_nr = 0; reg_nr < nr_regs; reg_nr++)
557 chp = parse_address (current, hw_parent (current),
558 chp, ®s[reg_nr].address);
559 chp = parse_size (current, hw_parent (current),
560 chp, ®s[reg_nr].size);
564 hw_add_reg_array_property (current, property_name,
571 /* { <child-address> <parent-address> <child-size> }* */
574 parse_ranges_property (struct hw *current,
575 const char *property_name,
576 const char *property_value)
580 range_property_spec *ranges;
583 /* determine the number of ranges specified */
584 nr_ranges = count_entries (current, property_name, property_value, 3);
586 /* create a property of that size */
587 ranges = zalloc (nr_ranges * sizeof (*ranges));
590 chp = property_value;
591 for (range_nr = 0; range_nr < nr_ranges; range_nr++)
593 chp = parse_address (current, current,
594 chp, &ranges[range_nr].child_address);
595 chp = parse_address (current, hw_parent (current),
596 chp, &ranges[range_nr].parent_address);
597 chp = parse_size (current, current,
598 chp, &ranges[range_nr].size);
602 hw_add_range_array_property (current, property_name, ranges, nr_ranges);
611 parse_integer_property (struct hw *current,
612 const char *property_name,
613 const char *property_value)
616 unsigned_cell words[1024];
617 /* integer or integer array? */
622 words[nr_entries] = strtoul (property_value, &end, 0);
623 if (property_value == end)
626 if (nr_entries * sizeof (words[0]) >= sizeof (words))
627 hw_abort (current, "buffer overflow");
628 property_value = end;
631 hw_abort (current, "error parsing integer property %s (%s)",
632 property_name, property_value);
633 else if (nr_entries == 1)
634 hw_add_integer_property (current, property_name, words[0]);
638 for (i = 0; i < nr_entries; i++)
642 /* perhaps integer array property is better */
643 hw_add_array_property (current, property_name, words,
644 sizeof (words[0]) * nr_entries);
652 parse_string_property (struct hw *current,
653 const char *property_name,
654 const char *property_value)
659 int approx_nr_strings;
661 /* get an estimate as to the number of strings by counting double
663 approx_nr_strings = 2;
664 for (chp = property_value; *chp; chp++)
669 approx_nr_strings = (approx_nr_strings) / 2;
671 /* create a string buffer for that many (plus a null) */
672 strings = (char**) zalloc ((approx_nr_strings + 1) * sizeof (char*));
674 /* now find all the strings */
675 chp = property_value;
680 /* skip leading space */
681 while (*chp != '\0' && isspace (*chp))
689 /* a quoted string - watch for '\' et al. */
690 /* estimate the size and allocate space for it */
694 while (chp[pos] != '\0' && chp[pos] != '"')
696 if (chp[pos] == '\\' && chp[pos+1] != '\0')
701 strings[nr_strings] = zalloc (pos + 1);
702 /* copy the string over */
704 while (*chp != '\0' && *chp != '"')
706 if (*chp == '\\' && *(chp+1) != '\0')
708 strings[nr_strings][pos] = *(chp+1);
714 strings[nr_strings][pos] = *chp;
721 strings[nr_strings][pos] = '\0';
725 /* copy over a single unquoted token */
727 while (chp[len] != '\0' && !isspace (chp[len]))
729 strings[nr_strings] = zalloc (len + 1);
730 strncpy (strings[nr_strings], chp, len);
731 strings[nr_strings][len] = '\0';
735 if (nr_strings > approx_nr_strings)
736 hw_abort (current, "String property %s badly formatted",
739 ASSERT (strings[nr_strings] == NULL); /* from zalloc */
743 hw_add_string_property (current, property_name, "");
744 else if (nr_strings == 1)
745 hw_add_string_property (current, property_name, strings[0]);
748 const char **specs = (const char**) strings; /* stop a bogus error */
749 hw_add_string_array_property (current, property_name,
753 /* flush the created string */
754 while (nr_strings > 0)
757 free (strings[nr_strings]);
763 /* <path-to-ihandle-device> */
767 parse_ihandle_property (struct hw *current,
768 const char *property,
771 ihandle_runtime_property_spec ihandle;
773 /* pass the full path */
774 ihandle.full_path = value;
776 /* save this ready for the ihandle create */
777 hw_add_ihandle_runtime_property (current, property,
784 hw_tree_create (SIM_DESC sd,
787 return hw_create (sd, NULL, family, family, NULL, NULL);
791 hw_tree_delete (struct hw *me)
793 /* Need to allow devices to disapear under our feet */
794 while (hw_child (me) != NULL)
796 hw_tree_delete (hw_child (me));
803 hw_tree_parse (struct hw *current,
809 current = hw_tree_vparse (current, fmt, ap);
815 hw_tree_vparse (struct hw *current,
819 char device_specifier[1024];
822 /* format the path */
823 vsprintf (device_specifier, fmt, ap);
824 if (strlen (device_specifier) >= sizeof (device_specifier))
825 hw_abort (NULL, "device_tree_add_deviced: buffer overflow\n");
827 /* construct the tree down to the final struct hw */
828 current = split_fill_path (current, device_specifier, &spec);
830 /* is there an interrupt spec */
831 if (spec.property == NULL
832 && spec.value != NULL)
834 char *op = split_value (&spec);
839 char *my_port_name = split_value (&spec);
841 char *dest_port_name = split_value (&spec);
843 name_specifier dest_spec;
844 char *dest_hw_name = split_value (&spec);
847 if (!hw_finished_p (current))
849 my_port = hw_port_decode (current, my_port_name, output_port);
850 /* find the dest device and port */
851 dest = split_fill_path (current, dest_hw_name, &dest_spec);
852 if (!hw_finished_p (dest))
854 dest_port = hw_port_decode (dest, dest_port_name,
856 /* connect the two */
857 hw_port_attach (current,
865 hw_abort (current, "unreconised interrupt spec %s\n", spec.value);
870 /* is there a property */
871 if (spec.property != NULL)
873 if (strcmp (spec.value, "true") == 0)
874 hw_add_boolean_property (current, spec.property, 1);
875 else if (strcmp (spec.value, "false") == 0)
876 hw_add_boolean_property (current, spec.property, 0);
879 const struct hw_property *property;
880 switch (spec.value[0])
885 parse_ihandle_property (current, spec.property, spec.value + 1);
891 unsigned8 words[1024];
892 char *curr = spec.value + 1;
897 words[nr_words] = H2BE_1 (strtoul (curr, &next, 0));
903 hw_add_array_property (current, spec.property,
904 words, sizeof (words[0]) * nr_words);
909 parse_string_property (current, spec.property, spec.value);
915 property = hw_tree_find_property (current, spec.value);
916 if (property == NULL)
917 hw_abort (current, "property %s not found\n", spec.value);
918 hw_add_duplicate_property (current,
925 if (strcmp (spec.property, "reg") == 0
926 || strcmp (spec.property, "assigned-addresses") == 0
927 || strcmp (spec.property, "alternate-reg") == 0)
929 parse_reg_property (current, spec.property, spec.value);
931 else if (strcmp (spec.property, "ranges") == 0)
933 parse_ranges_property (current, spec.property, spec.value);
935 else if (isdigit (spec.value[0])
936 || (spec.value[0] == '-' && isdigit (spec.value[1]))
937 || (spec.value[0] == '+' && isdigit (spec.value[1])))
939 parse_integer_property (current, spec.property, spec.value);
942 parse_string_property (current, spec.property, spec.value);
953 finish_hw_tree (struct hw *me,
956 if (!hw_finished_p (me))
961 hw_tree_finish (struct hw *root)
963 hw_tree_traverse (root, finish_hw_tree, NULL, NULL);
969 hw_tree_traverse (struct hw *root,
970 hw_tree_traverse_function *prefix,
971 hw_tree_traverse_function *postfix,
977 for (child = hw_child (root);
979 child = hw_sibling (child))
981 hw_tree_traverse (child, prefix, postfix, data);
984 postfix (root, data);
991 hw_tree_print_callback *print;
996 print_address (struct hw *bus,
1001 hw_unit_encode (bus, phys, unit, sizeof (unit));
1002 p->print (p->file, " %s", unit);
1006 print_size (struct hw *bus,
1007 const hw_unit *size,
1011 for (i = 0; i < size->nr_cells; i++)
1012 if (size->cells[i] != 0)
1014 if (i < size->nr_cells)
1016 p->print (p->file, " 0x%lx", (unsigned long) size->cells[i]);
1018 for (; i < size->nr_cells; i++)
1019 p->print (p->file, ",0x%lx", (unsigned long) size->cells[i]);
1022 p->print (p->file, " 0");
1026 print_reg_property (struct hw *me,
1027 const struct hw_property *property,
1031 reg_property_spec reg;
1033 hw_find_reg_array_property (me, property->name, reg_nr, ®);
1036 print_address (hw_parent (me), ®.address, p);
1037 print_size (me, ®.size, p);
1042 print_ranges_property (struct hw *me,
1043 const struct hw_property *property,
1047 range_property_spec range;
1049 hw_find_range_array_property (me, property->name, range_nr, &range);
1052 print_address (me, &range.child_address, p);
1053 print_address (hw_parent (me), &range.parent_address, p);
1054 print_size (me, &range.size, p);
1059 print_string (struct hw *me,
1063 p->print (p->file, " \"");
1064 while (*string != '\0')
1069 p->print (p->file, "\\\"");
1072 p->print (p->file, "\\\\");
1075 p->print (p->file, "%c", *string);
1080 p->print (p->file, "\"");
1084 print_string_array_property (struct hw *me,
1085 const struct hw_property *property,
1089 string_property_spec string;
1091 hw_find_string_array_property (me, property->name, nr, &string);
1094 print_string (me, string, p);
1099 print_properties (struct hw *me,
1102 const struct hw_property *property;
1103 for (property = hw_find_property (me, NULL);
1105 property = hw_next_property (property))
1107 if (hw_parent (me) == NULL)
1108 p->print (p->file, "/%s", property->name);
1110 p->print (p->file, "%s/%s", hw_path (me), property->name);
1111 if (property->original != NULL)
1113 p->print (p->file, " !");
1114 p->print (p->file, "%s/%s",
1115 hw_path (property->original->owner),
1116 property->original->name);
1120 switch (property->type)
1122 case array_property:
1124 if ((property->sizeof_array % sizeof (signed_cell)) == 0)
1126 unsigned_cell *w = (unsigned_cell*) property->array;
1129 cell_nr < (property->sizeof_array / sizeof (unsigned_cell));
1132 p->print (p->file, " 0x%lx", (unsigned long) BE2H_cell (w[cell_nr]));
1137 unsigned8 *w = (unsigned8*)property->array;
1138 p->print (p->file, " [");
1139 while ((char*)w - (char*)property->array < property->sizeof_array)
1141 p->print (p->file, " 0x%2x", BE2H_1 (*w));
1147 case boolean_property:
1149 int b = hw_find_boolean_property (me, property->name);
1150 p->print (p->file, " %s", b ? "true" : "false");
1154 case ihandle_property:
1156 if (property->array != NULL)
1158 device_instance *instance = hw_find_ihandle_property (me, property->name);
1159 p->print (p->file, " *%s", device_instance_path (instance));
1163 /* not yet initialized, ask the device for the path */
1164 ihandle_runtime_property_spec spec;
1165 hw_find_ihandle_runtime_property (me, property->name, &spec);
1166 p->print (p->file, " *%s", spec.full_path);
1171 case integer_property:
1173 unsigned_word w = hw_find_integer_property (me, property->name);
1174 p->print (p->file, " 0x%lx", (unsigned long)w);
1177 case range_array_property:
1179 print_ranges_property (me, property, p);
1182 case reg_array_property:
1184 print_reg_property (me, property, p);
1187 case string_property:
1189 const char *s = hw_find_string_property (me, property->name);
1190 print_string (me, s, p);
1193 case string_array_property:
1195 print_string_array_property (me, property, p);
1200 p->print (p->file, "\n");
1205 print_interrupts (struct hw *me,
1211 struct printer *p = data;
1214 hw_port_encode (me, my_port, src, sizeof (src), output_port);
1215 hw_port_encode (dest, dest_port, dst, sizeof (dst), input_port);
1224 print_device (struct hw *me,
1227 struct printer *p = data;
1228 p->print (p->file, "%s\n", hw_path (me));
1229 print_properties (me, p);
1230 hw_port_traverse (me, print_interrupts, data);
1234 hw_tree_print (struct hw *root,
1235 hw_tree_print_callback *print,
1241 hw_tree_traverse (root,
1250 tree_instance (struct hw *root,
1251 const char *device_specifier)
1253 /* find the device node */
1255 name_specifier spec;
1256 if (!split_device_specifier (root, device_specifier, &spec))
1258 me = split_find_device (root, &spec);
1259 if (spec.name != NULL)
1261 /* create the instance */
1262 return device_create_instance (me, device_specifier, spec.last_args);
1267 hw_tree_find_device (struct hw *root,
1268 const char *path_to_device)
1271 name_specifier spec;
1273 /* parse the path */
1274 split_device_specifier (root, path_to_device, &spec);
1275 if (spec.value != NULL)
1276 return NULL; /* something wierd */
1279 node = split_find_device (root, &spec);
1280 if (spec.name != NULL)
1281 return NULL; /* not a leaf */
1287 const struct hw_property *
1288 hw_tree_find_property (struct hw *root,
1289 const char *path_to_property)
1291 name_specifier spec;
1292 if (!split_property_specifier (root, path_to_property, &spec))
1293 hw_abort (root, "Invalid property path %s", path_to_property);
1294 root = split_find_device (root, &spec);
1295 if (spec.name != NULL)
1296 return NULL; /* not a leaf */
1297 return hw_find_property (root, spec.property);
1301 hw_tree_find_boolean_property (struct hw *root,
1302 const char *path_to_property)
1304 name_specifier spec;
1305 if (!split_property_specifier (root, path_to_property, &spec))
1306 hw_abort (root, "Invalid property path %s", path_to_property);
1307 root = split_find_device (root, &spec);
1308 if (spec.name != NULL)
1309 hw_abort (root, "device \"%s\" not found (property \"%s\")",
1310 spec.name, path_to_property);
1311 return hw_find_boolean_property (root, spec.property);
1315 hw_tree_find_integer_property (struct hw *root,
1316 const char *path_to_property)
1318 name_specifier spec;
1319 if (!split_property_specifier (root, path_to_property, &spec))
1320 hw_abort (root, "Invalid property path %s", path_to_property);
1321 root = split_find_device (root, &spec);
1322 if (spec.name != NULL)
1323 hw_abort (root, "device \"%s\" not found (property \"%s\")",
1324 spec.name, path_to_property);
1325 return hw_find_integer_property (root, spec.property);
1330 hw_tree_find_ihandle_property (struct hw *root,
1331 const char *path_to_property)
1334 name_specifier spec;
1335 if (!split_property_specifier (root, path_to_property, &spec))
1336 hw_abort (root, "Invalid property path %s", path_to_property);
1337 root = split_find_device (root, &spec);
1338 if (spec.name != NULL)
1339 hw_abort (root, "device \"%s\" not found (property \"%s\")",
1340 spec.name, path_to_property);
1341 return hw_find_ihandle_property (root, spec.property);
1346 hw_tree_find_string_property (struct hw *root,
1347 const char *path_to_property)
1349 name_specifier spec;
1350 if (!split_property_specifier (root, path_to_property, &spec))
1351 hw_abort (root, "Invalid property path %s", path_to_property);
1352 root = split_find_device (root, &spec);
1353 if (spec.name != NULL)
1354 hw_abort (root, "device \"%s\" not found (property \"%s\")",
1355 spec.name, path_to_property);
1356 return hw_find_string_property (root, spec.property);