1 /* The common simulator framework for GDB, the GNU Debugger.
3 Copyright 2002-2023 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. */
29 #include "sim-assert.h"
31 struct hw_instance_data
33 hw_finish_instance_method
*to_finish
;
34 struct hw_instance
*instances
;
37 static hw_finish_instance_method abort_hw_finish_instance
;
40 create_hw_instance_data (struct hw
*me
)
42 me
->instances_of_hw
= HW_ZALLOC (me
, struct hw_instance_data
);
43 set_hw_finish_instance (me
, abort_hw_finish_instance
);
47 delete_hw_instance_data (struct hw
*me
)
54 abort_hw_finish_instance (struct hw
*hw
,
55 struct hw_instance
*instance
)
57 hw_abort (hw
, "no instance finish method");
61 set_hw_finish_instance (struct hw
*me
,
62 hw_finish_instance_method
*finish
)
64 me
->instances_of_hw
->to_finish
= finish
;
70 clean_hw_instances (struct hw
*me
)
72 struct hw_instance
**instance
= &me
->instances
;
73 while (*instance
!= NULL
)
75 struct hw_instance
*old_instance
= *instance
;
76 hw_instance_delete (old_instance
);
77 instance
= &me
->instances
;
84 hw_instance_delete (struct hw_instance
*instance
)
87 hw_abort (hw_instance_hw (instance
), "not implemented");
89 struct hw
*me
= hw_instance_hw (instance
);
90 if (instance
->to_instance_delete
== NULL
)
91 hw_abort (me
, "no delete method");
92 instance
->method
->delete (instance
);
93 if (instance
->args
!= NULL
)
94 free (instance
->args
);
95 if (instance
->path
!= NULL
)
96 free (instance
->path
);
97 if (instance
->child
== NULL
)
99 /* only remove leaf nodes */
100 struct hw_instance
**curr
= &me
->instances
;
101 while (*curr
!= instance
)
103 ASSERT (*curr
!= NULL
);
104 curr
= &(*curr
)->next
;
106 *curr
= instance
->next
;
110 /* check it isn't in the instance list */
111 struct hw_instance
*curr
= me
->instances
;
114 ASSERT (curr
!= instance
);
117 /* unlink the child */
118 ASSERT (instance
->child
->parent
== instance
);
119 instance
->child
->parent
= NULL
;
121 cap_remove (me
->ihandles
, instance
);
128 panic_hw_instance_read (struct hw_instance
*instance
,
132 hw_abort (hw_instance_hw (instance
), "no read method");
139 panic_hw_instance_write (struct hw_instance
*instance
,
143 hw_abort (hw_instance_hw (instance
), "no write method");
149 panic_hw_instance_seek (struct hw_instance
*instance
,
150 unsigned_word pos_hi
,
151 unsigned_word pos_lo
)
153 hw_abort (hw_instance_hw (instance
), "no seek method");
159 hw_instance_call_method (struct hw_instance
*instance
,
160 const char *method_name
,
162 unsigned_cell stack_args
[/*n_stack_args*/],
164 unsigned_cell stack_returns
[/*n_stack_args*/])
167 hw_abort (hw_instance_hw (instance
), "not implemented");
170 struct hw
*me
= instance
->owner
;
171 const hw_instance_methods
*method
= instance
->method
->methods
;
174 hw_abort (me
, "no methods (want %s)", method_name
);
176 while (method
->name
!= NULL
)
178 if (strcmp (method
->name
, method_name
) == 0)
180 return method
->method (instance
,
181 n_stack_args
, stack_args
,
182 n_stack_returns
, stack_returns
);
186 hw_abort (me
, "no %s method", method_name
);
192 #define set_hw_instance_read(instance, method)\
193 ((instance)->to_instance_read = (method))
195 #define set_hw_instance_write(instance, method)\
196 ((instance)->to_instance_write = (method))
198 #define set_hw_instance_seek(instance, method)\
199 ((instance)->to_instance_seek = (method))
204 set_hw_instance_finish (struct hw
*me
,
205 hw_instance_finish_method
*method
)
207 if (me
->instances_of_hw
== NULL
)
208 me
->instances_of_hw
= HW_ZALLOC (me
, struct hw_instance_data
);
209 me
->instances_of_hw
->to_finish
= method
;
215 hw_instance_create (struct hw
*me
,
216 struct hw_instance
*parent
,
220 struct hw_instance
*instance
= ZALLOC (struct hw_instance
);
222 /* link this instance into the devices list */
223 instance
->hw_of_instance
= me
;
224 instance
->parent_of_instance
= NULL
;
225 /* link this instance into the front of the devices instance list */
226 instance
->sibling_of_instance
= me
->instances_of_hw
->instances
;
227 me
->instances_of_hw
->instances
= instance
;
230 ASSERT (parent
->child_of_instance
== NULL
);
231 parent
->child_of_instance
= instance
;
232 instance
->parent_of_instance
= parent
;
234 instance
->args_of_instance
= hw_strdup (me
, args
);
235 instance
->path_of_instance
= hw_strdup (me
, path
);
236 set_hw_instance_read (instance
, panic_hw_instance_read
);
237 set_hw_instance_write (instance
, panic_hw_instance_write
);
238 set_hw_instance_seek (instance
, panic_hw_instance_seek
);
239 hw_handle_add_ihandle (me
, instance
);
240 me
->instances_of_hw
->to_finish (me
, instance
);
246 hw_instance_interceed (struct hw_instance
*parent
,
253 struct hw_instance
*instance
= ZALLOC (struct hw_instance
);
255 /* link this instance into the devices list */
258 ASSERT (parent
== NULL
);
259 instance
->hw_of_instance
= me
;
260 instance
->parent_of_instance
= NULL
;
261 /* link this instance into the front of the devices instance list */
262 instance
->sibling_of_instance
= me
->instances_of_hw
->instances
;
263 me
->instances_of_hw
->instances
= instance
;
267 struct hw_instance
**previous
;
268 ASSERT (parent
->child_of_instance
== NULL
);
269 parent
->child_of_instance
= instance
;
270 instance
->owner
= parent
->owner
;
271 instance
->parent_of_instance
= parent
;
272 /* in the devices instance list replace the parent instance with
274 instance
->next
= parent
->next
;
275 /* replace parent with this new node */
276 previous
= &instance
->owner
->instances
;
277 while (*previous
!= parent
)
279 ASSERT (*previous
!= NULL
);
280 previous
= &(*previous
)->next
;
282 *previous
= instance
;
284 instance
->data
= data
;
285 instance
->args
= (args
== NULL
? NULL
: (char *) strdup (args
));
286 instance
->path
= (path
== NULL
? NULL
: (char *) strdup (path
));
287 cap_add (instance
->owner
->ihandles
, instance
);