Debian Codegraph @

unstable/cpu.c

raw
1 /*
2 * Cisco router simulation platform.
3 * Copyright (c) 2005,2006 Christophe Fillot ([email protected])
4 *
5 * Management of CPU groups (for MP systems).
6 */
7
8 #define _GNU_SOURCE
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <string.h>
13 #include <stdarg.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <fcntl.h>
17 #include <pthread.h>
18
19 #include "cpu.h"
20 #include "vm.h"
21 #include "tcb.h"
22 #include "memory.h"
23 #include "device.h"
24 #include "mips64.h"
25 #include "mips64_cp0.h"
26 #include "mips64_exec.h"
27 #include "mips64_jit.h"
28 #include "ppc32.h"
29 #include "ppc32_exec.h"
30 #include "ppc32_jit.h"
31 #include "dynamips.h"
32
33 /* Find a CPU in a group given its ID */
34 cpu_gen_t *cpu_group_find_id(cpu_group_t *group,u_int id)
35 {
36 cpu_gen_t *cpu;
37
38 if (!group)
39 return NULL;
40
41 for(cpu=group->cpu_list;cpu;cpu=cpu->next)
42 if (cpu->id == id)
43 return cpu;
44
45 return NULL;
46 }
47
48 /* Find the highest CPU ID in a CPU group */
49 int cpu_group_find_highest_id(cpu_group_t *group,u_int *highest_id)
50 {
51 cpu_gen_t *cpu;
52 u_int max_id = 0;
53
54 if (!group || group->cpu_list)
55 return(-1);
56
57 for(cpu=group->cpu_list;cpu;cpu=cpu->next)
58 if (cpu->id >= max_id)
59 max_id = cpu->id;
60
61 *highest_id = max_id;
62 return(0);
63 }
64
65 /* Add a CPU in a CPU group */
66 int cpu_group_add(cpu_group_t *group,cpu_gen_t *cpu)
67 {
68 if (!group)
69 return(-1);
70
71 /* check that we don't already have a CPU with this id */
72 if (cpu_group_find_id(group,cpu->id) != NULL) {
73 fprintf(stderr,"cpu_group_add: CPU%u already present in group.\n",
74 cpu->id);
75 return(-1);
76 }
77
78 cpu->next = group->cpu_list;
79 group->cpu_list = cpu;
80 return(0);
81 }
82
83 /* Create a new CPU group */
84 cpu_group_t *cpu_group_create(char *name)
85 {
86 cpu_group_t *group;
87
88 if (!(group = malloc(sizeof(*group))))
89 return NULL;
90
91 group->name = name;
92 group->cpu_list = NULL;
93 return group;
94 }
95
96 /* Delete a CPU group */
97 void cpu_group_delete(cpu_group_t *group)
98 {
99 cpu_gen_t *cpu,*next;
100
101 if (group != NULL) {
102 for(cpu=group->cpu_list;cpu;cpu=next) {
103 next = cpu->next;
104 cpu_delete(cpu);
105 }
106
107 free(group);
108 }
109 }
110
111 /* Rebuild the MTS subsystem for a CPU group */
112 int cpu_group_rebuild_mts(cpu_group_t *group)
113 {
114 cpu_gen_t *cpu;
115
116 for(cpu=group->cpu_list;cpu;cpu=cpu->next)
117 cpu->mts_rebuild(cpu);
118
119 return(0);
120 }
121
122 /* Log a message for a CPU */
123 void cpu_log(cpu_gen_t *cpu,char *module,char *format,...)
124 {
125 char buffer[256];
126 va_list ap;
127
128 va_start(ap,format);
129 snprintf(buffer,sizeof(buffer),"CPU%u: %s",cpu->id,module);
130 vm_flog(cpu->vm,buffer,format,ap);
131 va_end(ap);
132 }
133
134 /* Create a new CPU */
135 cpu_gen_t *cpu_create(vm_instance_t *vm,u_int type,u_int id)
136 {
137 void *(*cpu_run_fn)(void *);
138 cpu_gen_t *cpu;
139
140 if (!(cpu = malloc(sizeof(*cpu))))
141 return NULL;
142
143 memset(cpu,0,sizeof(*cpu));
144 cpu->vm = vm;
145 cpu->id = id;
146 cpu->type = type;
147 cpu->state = CPU_STATE_SUSPENDED;
148 cpu->tsg = vm->tsg;
149
150 switch(cpu->type) {
151 case CPU_TYPE_MIPS64:
152 cpu->jit_op_array_size = MIPS_INSN_PER_PAGE;
153 CPU_MIPS64(cpu)->vm = vm;
154 CPU_MIPS64(cpu)->gen = cpu;
155 mips64_init(CPU_MIPS64(cpu));
156
157 cpu_run_fn = (void *)mips64_jit_run_cpu;
158
159 if (!cpu->vm->jit_use)
160 cpu_run_fn = (void *)mips64_exec_run_cpu;
161 else
162 mips64_jit_init(CPU_MIPS64(cpu));
163 break;
164
165 case CPU_TYPE_PPC32:
166 cpu->jit_op_array_size = PPC32_INSN_PER_PAGE;
167 CPU_PPC32(cpu)->vm = vm;
168 CPU_PPC32(cpu)->gen = cpu;
169 ppc32_init(CPU_PPC32(cpu));
170
171 cpu_run_fn = (void *)ppc32_jit_run_cpu;
172
173 if (!cpu->vm->jit_use)
174 cpu_run_fn = (void *)ppc32_exec_run_cpu;
175 else
176 ppc32_jit_init(CPU_PPC32(cpu));
177 break;
178
179 default:
180 fprintf(stderr,"CPU type %u is not supported yet\n",cpu->type);
181 abort();
182 break;
183 }
184
185 /* create the CPU thread execution */
186 if (pthread_create(&cpu->cpu_thread,NULL,cpu_run_fn,cpu) != 0) {
187 fprintf(stderr,"cpu_create: unable to create thread for CPU%u\n",id);
188 free(cpu);
189 return NULL;
190 }
191
192 return cpu;
193 }
194
195 /* Delete a CPU */
196 void cpu_delete(cpu_gen_t *cpu)
197 {
198 if (cpu) {
199 /* Stop activity of this CPU */
200 cpu_stop(cpu);
201 pthread_join(cpu->cpu_thread,NULL);
202
203 /* Free resources */
204 switch(cpu->type) {
205 case CPU_TYPE_MIPS64:
206 mips64_delete(CPU_MIPS64(cpu));
207 break;
208
209 case CPU_TYPE_PPC32:
210 ppc32_delete(CPU_PPC32(cpu));
211 break;
212 }
213
214 free(cpu->jit_op_array);
215 free(cpu);
216 }
217 }
218
219 /* Start a CPU */
220 void cpu_start(cpu_gen_t *cpu)
221 {
222 if (cpu) {
223 cpu_log(cpu,"CPU_STATE","Starting CPU (old state=%u)...\n",cpu->state);
224 cpu->state = CPU_STATE_RUNNING;
225 }
226 }
227
228 /* Stop a CPU */
229 void cpu_stop(cpu_gen_t *cpu)
230 {
231 if (cpu) {
232 cpu_log(cpu,"CPU_STATE","Halting CPU (old state=%u)...\n",cpu->state);
233 cpu->state = CPU_STATE_HALTED;
234 }
235 }
236
237 /* Start all CPUs of a CPU group */
238 void cpu_group_start_all_cpu(cpu_group_t *group)
239 {
240 cpu_gen_t *cpu;
241
242 for(cpu=group->cpu_list;cpu;cpu=cpu->next)
243 cpu_start(cpu);
244 }
245
246 /* Stop all CPUs of a CPU group */
247 void cpu_group_stop_all_cpu(cpu_group_t *group)
248 {
249 cpu_gen_t *cpu;
250
251 for(cpu=group->cpu_list;cpu;cpu=cpu->next)
252 cpu_stop(cpu);
253 }
254
255 /* Set a state of all CPUs of a CPU group */
256 void cpu_group_set_state(cpu_group_t *group,u_int state)
257 {
258 cpu_gen_t *cpu;
259
260 for(cpu=group->cpu_list;cpu;cpu=cpu->next)
261 cpu->state = state;
262 }
263
264 /* Returns TRUE if all CPUs in a CPU group are inactive */
265 static int cpu_group_check_activity(cpu_group_t *group)
266 {
267 cpu_gen_t *cpu;
268
269 for(cpu=group->cpu_list;cpu;cpu=cpu->next) {
270 if (!cpu->cpu_thread_running)
271 continue;
272
273 if ((cpu->state == CPU_STATE_RUNNING) || !cpu->seq_state)
274 return(FALSE);
275 }
276
277 return(TRUE);
278 }
279
280 /* Synchronize on CPUs (all CPUs must be inactive) */
281 int cpu_group_sync_state(cpu_group_t *group)
282 {
283 cpu_gen_t *cpu;
284 m_tmcnt_t t1,t2;
285
286 /* Check that CPU activity is really suspended */
287 t1 = m_gettime();
288
289 for(cpu=group->cpu_list;cpu;cpu=cpu->next)
290 cpu->seq_state = 0;
291
292 while(!cpu_group_check_activity(group)) {
293 t2 = m_gettime();
294
295 if (t2 > (t1 + 10000))
296 return(-1);
297
298 usleep(50000);
299 }
300
301 return(0);
302 }
303
304 /* Save state of all CPUs */
305 int cpu_group_save_state(cpu_group_t *group)
306 {
307 cpu_gen_t *cpu;
308
309 for(cpu=group->cpu_list;cpu;cpu=cpu->next)
310 cpu->prev_state = cpu->state;
311
312 return(TRUE);
313 }
314
315 /* Restore state of all CPUs */
316 int cpu_group_restore_state(cpu_group_t *group)
317 {
318 cpu_gen_t *cpu;
319
320 for(cpu=group->cpu_list;cpu;cpu=cpu->next)
321 cpu->state = cpu->prev_state;
322
323 return(TRUE);
324 }
325
326 /* Virtual idle loop */
327 void cpu_idle_loop(cpu_gen_t *cpu)
328 {
329 struct timespec t_spc;
330 m_tmcnt_t expire;
331
332 expire = m_gettime_usec() + cpu->idle_sleep_time;
333
334 pthread_mutex_lock(&cpu->idle_mutex);
335 t_spc.tv_sec = expire / 1000000;
336 t_spc.tv_nsec = (expire % 1000000) * 1000;
337 pthread_cond_timedwait(&cpu->idle_cond,&cpu->idle_mutex,&t_spc);
338 pthread_mutex_unlock(&cpu->idle_mutex);
339 }
340
341 /* Break idle wait state */
342 void cpu_idle_break_wait(cpu_gen_t *cpu)
343 {
344 pthread_cond_signal(&cpu->idle_cond);
345 cpu->idle_count = 0;
346 }