Debian Codegraph @

unstable/cpu.h

raw
1 /*
2 * Cisco router simulation platform.
3 * Copyright (c) 2005,2006 Christophe Fillot ([email protected])
4 */
5
6 #ifndef __CPU_H__
7 #define __CPU_H__
8
9 #include <pthread.h>
10 #include <setjmp.h>
11 #include "utils.h"
12 #include "jit_op.h"
13
14 #include "mips64.h"
15 #include "mips64_cp0.h"
16 #include "ppc32.h"
17
18 /* Possible CPU types */
19 enum {
20 CPU_TYPE_MIPS64 = 1,
21 CPU_TYPE_PPC32,
22 };
23
24 /* Virtual CPU states */
25 enum {
26 CPU_STATE_RUNNING = 0,
27 CPU_STATE_HALTED,
28 CPU_STATE_SUSPENDED,
29 };
30
31 /* Maximum results for idle pc */
32 #define CPU_IDLE_PC_MAX_RES 10
33
34 /* Idle PC proposed value */
35 struct cpu_idle_pc {
36 m_uint64_t pc;
37 u_int count;
38 };
39
40 /* Number of recorded memory accesses (power of two) */
41 #define MEMLOG_COUNT 16
42
43 typedef struct memlog_access memlog_access_t;
44 struct memlog_access {
45 m_uint64_t iaddr;
46 m_uint64_t vaddr;
47 m_uint64_t data;
48 m_uint32_t data_valid;
49 m_uint32_t op_size;
50 m_uint32_t op_type;
51 };
52
53 /* Undefined memory access handler */
54 typedef int (*cpu_undefined_mem_handler_t)(cpu_gen_t *cpu,m_uint64_t vaddr,
55 u_int op_size,u_int op_type,
56 m_uint64_t *data);
57
58 /* Generic CPU definition */
59 struct cpu_gen {
60 /* CPU type and identifier for MP systems */
61 u_int type,id;
62
63 /* CPU states */
64 volatile u_int state,prev_state;
65 volatile m_uint64_t seq_state;
66
67 /* Thread running this CPU */
68 pthread_t cpu_thread;
69 volatile int cpu_thread_running;
70
71 /* Exception restore point */
72 jmp_buf exec_loop_env;
73
74 /* "Idle" loop management */
75 u_int idle_count,idle_max,idle_sleep_time;
76 pthread_mutex_t idle_mutex;
77 pthread_cond_t idle_cond;
78
79 /* VM instance */
80 vm_instance_t *vm;
81
82 /* Next CPU in group */
83 cpu_gen_t *next;
84
85 /* Idle PC proposal */
86 struct cpu_idle_pc idle_pc_prop[CPU_IDLE_PC_MAX_RES];
87 u_int idle_pc_prop_count;
88
89 /* Specific CPU part */
90 union {
91 cpu_mips_t mips64_cpu;
92 cpu_ppc_t ppc32_cpu;
93 }sp;
94
95 /* Methods */
96 void (*reg_set)(cpu_gen_t *cpu,u_int reg_index,m_uint64_t val);
97 void (*reg_dump)(cpu_gen_t *cpu);
98 void (*mmu_dump)(cpu_gen_t *cpu);
99 void (*mmu_raw_dump)(cpu_gen_t *cpu);
100 void (*add_breakpoint)(cpu_gen_t *cpu,m_uint64_t addr);
101 void (*remove_breakpoint)(cpu_gen_t *cpu,m_uint64_t addr);
102 void (*set_idle_pc)(cpu_gen_t *cpu,m_uint64_t addr);
103 void (*get_idling_pc)(cpu_gen_t *cpu);
104 void (*mts_rebuild)(cpu_gen_t *cpu);
105 void (*mts_show_stats)(cpu_gen_t *cpu);
106
107 cpu_undefined_mem_handler_t undef_mem_handler;
108
109 /* Memory access log for fault debugging */
110 u_int memlog_pos;
111 memlog_access_t memlog_array[MEMLOG_COUNT];
112
113 /* Statistics */
114 m_uint64_t dev_access_counter;
115
116 /* JIT op array for current compiled pages */
117 u_int jit_op_array_size;
118 jit_op_t **jit_op_array;
119 jit_op_t **jit_op_current;
120
121 /* Translation group ID and TCB descriptor local list */
122 int tsg;
123 cpu_tc_t *tc_local_list;
124
125 /* Current and free lists of TBs */
126 cpu_tb_t *tb_list,*tb_free_list;
127
128 /* Virtual and Physical hash tables to retrieve TBs */
129 cpu_tb_t **tb_virt_hash,**tb_phys_hash;
130
131 /* CPU List for a Translation Sharing Group */
132 cpu_gen_t **tsg_pprev,*tsg_next;
133
134 /* JIT op pool */
135 jit_op_t *jit_op_pool[JIT_OP_POOL_NR];
136 };
137
138 /* CPU group definition */
139 typedef struct cpu_group cpu_group_t;
140 struct cpu_group {
141 char *name;
142 cpu_gen_t *cpu_list;
143 void *priv_data;
144 };
145
146 #define CPU_MIPS64(cpu) (&(cpu)->sp.mips64_cpu)
147 #define CPU_PPC32(cpu) (&(cpu)->sp.ppc32_cpu)
148
149 /* Get CPU instruction pointer */
150 static forced_inline m_uint64_t cpu_get_pc(cpu_gen_t *cpu)
151 {
152 switch(cpu->type) {
153 case CPU_TYPE_MIPS64:
154 return(CPU_MIPS64(cpu)->pc);
155 case CPU_TYPE_PPC32:
156 return((m_uint64_t)CPU_PPC32(cpu)->ia);
157 default:
158 return(0);
159 }
160 }
161
162 /* Get CPU performance counter */
163 static forced_inline m_uint32_t cpu_get_perf_counter(cpu_gen_t *cpu)
164 {
165 switch(cpu->type) {
166 case CPU_TYPE_MIPS64:
167 return(CPU_MIPS64(cpu)->perf_counter);
168 case CPU_TYPE_PPC32:
169 return(CPU_PPC32(cpu)->perf_counter);
170 default:
171 return(0);
172 }
173 }
174
175 /* Find a CPU in a group given its ID */
176 cpu_gen_t *cpu_group_find_id(cpu_group_t *group,u_int id);
177
178 /* Find the highest CPU ID in a CPU group */
179 int cpu_group_find_highest_id(cpu_group_t *group,u_int *highest_id);
180
181 /* Add a CPU in a CPU group */
182 int cpu_group_add(cpu_group_t *group,cpu_gen_t *cpu);
183
184 /* Create a new CPU group */
185 cpu_group_t *cpu_group_create(char *name);
186
187 /* Delete a CPU group */
188 void cpu_group_delete(cpu_group_t *group);
189
190 /* Rebuild the MTS subsystem for a CPU group */
191 int cpu_group_rebuild_mts(cpu_group_t *group);
192
193 /* Log a message for a CPU */
194 void cpu_log(cpu_gen_t *cpu,char *module,char *format,...);
195
196 /* Create a new CPU */
197 cpu_gen_t *cpu_create(vm_instance_t *vm,u_int type,u_int id);
198
199 /* Delete a CPU */
200 void cpu_delete(cpu_gen_t *cpu);
201
202 /* Start a CPU */
203 void cpu_start(cpu_gen_t *cpu);
204
205 /* Stop a CPU */
206 void cpu_stop(cpu_gen_t *cpu);
207
208 /* Start all CPUs of a CPU group */
209 void cpu_group_start_all_cpu(cpu_group_t *group);
210
211 /* Stop all CPUs of a CPU group */
212 void cpu_group_stop_all_cpu(cpu_group_t *group);
213
214 /* Set a state of all CPUs of a CPU group */
215 void cpu_group_set_state(cpu_group_t *group,u_int state);
216
217 /* Synchronize on CPUs (all CPUs must be inactive) */
218 int cpu_group_sync_state(cpu_group_t *group);
219
220 /* Save state of all CPUs */
221 int cpu_group_save_state(cpu_group_t *group);
222
223 /* Restore state of all CPUs */
224 int cpu_group_restore_state(cpu_group_t *group);
225
226 /* Virtual idle loop */
227 void cpu_idle_loop(cpu_gen_t *cpu);
228
229 /* Break idle wait state */
230 void cpu_idle_break_wait(cpu_gen_t *cpu);
231
232 /* Returns to the CPU exec loop */
233 static inline void cpu_exec_loop_enter(cpu_gen_t *cpu)
234 {
235 longjmp(cpu->exec_loop_env,1);
236 }
237
238 /* Set the exec loop entry point */
239 #define cpu_exec_loop_set(cpu) setjmp((cpu)->exec_loop_env)
240
241 #endif