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