1 /* The common simulator framework for GDB, the GNU Debugger.
2 
3    Copyright 2002, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 
5    Contributed by Andrew Cagney and Red Hat.
6 
7    This file is part of GDB.
8 
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.
13 
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.
18 
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/>.  */
21 
22 
23 #include "hw-main.h"
24 #include "hw-base.h"
25 
26 #include "sim-io.h"
27 #include "sim-assert.h"
28 
29 struct hw_instance_data
30 {
31   hw_finish_instance_method *to_finish;
32   struct hw_instance *instances;
33 };
34 
35 static hw_finish_instance_method abort_hw_finish_instance;
36 
37 void
create_hw_instance_data(struct hw * me)38 create_hw_instance_data (struct hw *me)
39 {
40   me->instances_of_hw = HW_ZALLOC (me, struct hw_instance_data);
41   set_hw_finish_instance (me, abort_hw_finish_instance);
42 }
43 
44 void
delete_hw_instance_data(struct hw * me)45 delete_hw_instance_data (struct hw *me)
46 {
47   /* NOP */
48 }
49 
50 
51 static void
abort_hw_finish_instance(struct hw * hw,struct hw_instance * instance)52 abort_hw_finish_instance (struct hw *hw,
53 			  struct hw_instance *instance)
54 {
55   hw_abort (hw, "no instance finish method");
56 }
57 
58 void
set_hw_finish_instance(struct hw * me,hw_finish_instance_method * finish)59 set_hw_finish_instance (struct hw *me,
60 			hw_finish_instance_method *finish)
61 {
62   me->instances_of_hw->to_finish = finish;
63 }
64 
65 
66 #if 0
67 void
68 clean_hw_instances (struct hw *me)
69 {
70   struct hw_instance **instance = &me->instances;
71   while (*instance != NULL)
72     {
73       struct hw_instance *old_instance = *instance;
74       hw_instance_delete (old_instance);
75       instance = &me->instances;
76     }
77 }
78 #endif
79 
80 
81 void
hw_instance_delete(struct hw_instance * instance)82 hw_instance_delete (struct hw_instance *instance)
83 {
84 #if 1
85   hw_abort (hw_instance_hw (instance), "not implemented");
86 #else
87   struct hw *me = hw_instance_hw (instance);
88   if (instance->to_instance_delete == NULL)
89     hw_abort (me, "no delete method");
90   instance->method->delete(instance);
91   if (instance->args != NULL)
92     free (instance->args);
93   if (instance->path != NULL)
94     free (instance->path);
95   if (instance->child == NULL)
96     {
97       /* only remove leaf nodes */
98       struct hw_instance **curr = &me->instances;
99       while (*curr != instance)
100 	{
101 	  ASSERT (*curr != NULL);
102 	  curr = &(*curr)->next;
103 	}
104       *curr = instance->next;
105     }
106   else
107     {
108       /* check it isn't in the instance list */
109       struct hw_instance *curr = me->instances;
110       while (curr != NULL)
111 	{
112 	  ASSERT(curr != instance);
113 	  curr = curr->next;
114 	}
115       /* unlink the child */
116       ASSERT (instance->child->parent == instance);
117       instance->child->parent = NULL;
118     }
119   cap_remove (me->ihandles, instance);
120   free (instance);
121 #endif
122 }
123 
124 
125 static int
panic_hw_instance_read(struct hw_instance * instance,void * addr,unsigned_word len)126 panic_hw_instance_read (struct hw_instance *instance,
127 			void *addr,
128 			unsigned_word len)
129 {
130   hw_abort (hw_instance_hw (instance), "no read method");
131   return -1;
132 }
133 
134 
135 
136 static int
panic_hw_instance_write(struct hw_instance * instance,const void * addr,unsigned_word len)137 panic_hw_instance_write (struct hw_instance *instance,
138 			 const void *addr,
139 			 unsigned_word len)
140 {
141   hw_abort (hw_instance_hw (instance), "no write method");
142   return -1;
143 }
144 
145 
146 static int
panic_hw_instance_seek(struct hw_instance * instance,unsigned_word pos_hi,unsigned_word pos_lo)147 panic_hw_instance_seek (struct hw_instance *instance,
148 			unsigned_word pos_hi,
149 			unsigned_word pos_lo)
150 {
151   hw_abort (hw_instance_hw (instance), "no seek method");
152   return -1;
153 }
154 
155 
156 int
hw_instance_call_method(struct hw_instance * instance,const char * method_name,int n_stack_args,unsigned_cell stack_args[],int n_stack_returns,unsigned_cell stack_returns[])157 hw_instance_call_method (struct hw_instance *instance,
158 			 const char *method_name,
159 			 int n_stack_args,
160 			 unsigned_cell stack_args[/*n_stack_args*/],
161 			 int n_stack_returns,
162 			 unsigned_cell stack_returns[/*n_stack_args*/])
163 {
164 #if 1
165   hw_abort (hw_instance_hw (instance), "not implemented");
166   return -1;
167 #else
168   struct hw *me = instance->owner;
169   const hw_instance_methods *method = instance->method->methods;
170   if (method == NULL)
171     {
172       hw_abort (me, "no methods (want %s)", method_name);
173     }
174   while (method->name != NULL)
175     {
176       if (strcmp(method->name, method_name) == 0)
177 	{
178 	  return method->method (instance,
179 				 n_stack_args, stack_args,
180 				 n_stack_returns, stack_returns);
181 	}
182       method++;
183     }
184   hw_abort (me, "no %s method", method_name);
185   return 0;
186 #endif
187 }
188 
189 
190 #define set_hw_instance_read(instance, method)\
191 ((instance)->to_instance_read = (method))
192 
193 #define set_hw_instance_write(instance, method)\
194 ((instance)->to_instance_write = (method))
195 
196 #define set_hw_instance_seek(instance, method)\
197 ((instance)->to_instance_seek = (method))
198 
199 
200 #if 0
201 static void
202 set_hw_instance_finish (struct hw *me,
203 			hw_instance_finish_method *method)
204 {
205   if (me->instances_of_hw == NULL)
206     me->instances_of_hw = HW_ZALLOC (me, struct hw_instance_data);
207   me->instances_of_hw->to_finish = method;
208 }
209 #endif
210 
211 
212 struct hw_instance *
hw_instance_create(struct hw * me,struct hw_instance * parent,const char * path,const char * args)213 hw_instance_create (struct hw *me,
214 		    struct hw_instance *parent,
215 		    const char *path,
216 		    const char *args)
217 {
218   struct hw_instance *instance = ZALLOC (struct hw_instance);
219   /*instance->unit*/
220   /* link this instance into the devices list */
221   instance->hw_of_instance = me;
222   instance->parent_of_instance = NULL;
223   /* link this instance into the front of the devices instance list */
224   instance->sibling_of_instance = me->instances_of_hw->instances;
225   me->instances_of_hw->instances = instance;
226   if (parent != NULL)
227     {
228       ASSERT (parent->child_of_instance == NULL);
229       parent->child_of_instance = instance;
230       instance->parent_of_instance = parent;
231     }
232   instance->args_of_instance = hw_strdup (me, args);
233   instance->path_of_instance = hw_strdup (me, path);
234   set_hw_instance_read (instance, panic_hw_instance_read);
235   set_hw_instance_write (instance, panic_hw_instance_write);
236   set_hw_instance_seek (instance, panic_hw_instance_seek);
237   hw_handle_add_ihandle (me, instance);
238   me->instances_of_hw->to_finish (me, instance);
239   return instance;
240 }
241 
242 
243 struct hw_instance *
hw_instance_interceed(struct hw_instance * parent,const char * path,const char * args)244 hw_instance_interceed (struct hw_instance *parent,
245 		       const char *path,
246 		       const char *args)
247 {
248 #if 1
249   return NULL;
250 #else
251   struct hw_instance *instance = ZALLOC (struct hw_instance);
252   /*instance->unit*/
253   /* link this instance into the devices list */
254   if (me != NULL)
255     {
256       ASSERT (parent == NULL);
257       instance->hw_of_instance = me;
258       instance->parent_of_instance = NULL;
259       /* link this instance into the front of the devices instance list */
260       instance->sibling_of_instance = me->instances_of_hw->instances;
261       me->instances_of_hw->instances = instance;
262     }
263   if (parent != NULL)
264     {
265       struct hw_instance **previous;
266       ASSERT (parent->child_of_instance == NULL);
267       parent->child_of_instance = instance;
268       instance->owner = parent->owner;
269       instance->parent_of_instance = parent;
270       /* in the devices instance list replace the parent instance with
271 	 this one */
272       instance->next = parent->next;
273       /* replace parent with this new node */
274       previous = &instance->owner->instances;
275       while (*previous != parent)
276 	{
277 	  ASSERT (*previous != NULL);
278 	  previous = &(*previous)->next;
279 	}
280       *previous = instance;
281     }
282   instance->data = data;
283   instance->args = (args == NULL ? NULL : (char *) strdup(args));
284   instance->path = (path == NULL ? NULL : (char *) strdup(path));
285   cap_add (instance->owner->ihandles, instance);
286   return instance;
287 #endif
288 }
289