Debian Codegraph @

unstable/utils.h

raw
1 /*
2 * Cisco router simulation platform.
3 * Copyright (c) 2005,2006 Christophe Fillot ([email protected])
4 */
5
6 #ifndef __UTILS_H__
7 #define __UTILS_H__
8
9 #include "dynamips_common.h"
10
11 #include <sys/time.h>
12 #include <netinet/in.h>
13 #include <signal.h>
14
15 /* Host CPU Types */
16 #define CPU_x86 0
17 #define CPU_amd64 1
18 #define CPU_nojit 2
19
20 /* Number of host registers available for JIT */
21 #if JIT_CPU == CPU_x86
22 #define JIT_HOST_NREG 8
23 #elif JIT_CPU == CPU_amd64
24 #define JIT_HOST_NREG 16
25 #else
26 #define JIT_HOST_NREG 0
27 #endif
28
29 /* Host to VM (big-endian) conversion functions */
30 #if ARCH_BYTE_ORDER == ARCH_BIG_ENDIAN
31 #define htovm16(x) (x)
32 #define htovm32(x) (x)
33 #define htovm64(x) (x)
34
35 #define vmtoh16(x) (x)
36 #define vmtoh32(x) (x)
37 #define vmtoh64(x) (x)
38 #else
39 #define htovm16(x) (htons(x))
40 #define htovm32(x) (htonl(x))
41 #define htovm64(x) (swap64(x))
42
43 #define vmtoh16(x) (ntohs(x))
44 #define vmtoh32(x) (ntohl(x))
45 #define vmtoh64(x) (swap64(x))
46 #endif
47
48 /* FD pool */
49 #define FD_POOL_MAX 16
50
51 typedef struct fd_pool fd_pool_t;
52 struct fd_pool {
53 int fd[FD_POOL_MAX];
54 struct fd_pool *next;
55 };
56
57 /* Forward declarations */
58 typedef struct cpu_gen cpu_gen_t;
59 typedef struct vm_instance vm_instance_t;
60 typedef struct vm_platform vm_platform_t;
61 typedef struct mips64_jit_tcb mips64_jit_tcb_t;
62 typedef struct ppc32_jit_tcb ppc32_jit_tcb_t;
63 typedef struct jit_op jit_op_t;
64 typedef struct cpu_tb cpu_tb_t;
65 typedef struct cpu_tc cpu_tc_t;
66
67 /* Translated block function pointer */
68 typedef void (*insn_tblock_fptr)(void);
69
70 /* Host executable page */
71 typedef struct insn_exec_page insn_exec_page_t;
72 struct insn_exec_page {
73 u_char *ptr;
74 insn_exec_page_t *next;
75 int flags;
76 };
77
78 /* MIPS instruction */
79 typedef m_uint32_t mips_insn_t;
80
81 /* PowerPC instruction */
82 typedef m_uint32_t ppc_insn_t;
83
84 /* MMAP */
85 #ifndef MAP_ANONYMOUS
86 #define MAP_ANONYMOUS MAP_ANON
87 #endif
88
89 /* Macros for double linked list */
90 #define M_LIST_ADD(item,head,prefix) \
91 do { \
92 (item)->prefix##_next = (head); \
93 (item)->prefix##_pprev = &(head); \
94 \
95 if ((head) != NULL) \
96 (head)->prefix##_pprev = &(item)->prefix##_next; \
97 \
98 (head) = (item); \
99 }while(0)
100
101 #define M_LIST_REMOVE(item,prefix) \
102 do { \
103 if ((item)->prefix##_pprev != NULL) { \
104 if ((item)->prefix##_next != NULL) \
105 (item)->prefix##_next->prefix##_pprev = (item)->prefix##_pprev; \
106 \
107 *((item)->prefix##_pprev) = (item)->prefix##_next; \
108 \
109 (item)->prefix##_pprev = NULL; \
110 (item)->prefix##_next = NULL; \
111 } \
112 }while(0)
113
114
115 /* List item */
116 typedef struct m_list m_list_t;
117 struct m_list {
118 void *data;
119 m_list_t *next;
120 };
121
122 /* MTS mapping info */
123 typedef struct {
124 m_uint64_t vaddr;
125 m_uint64_t paddr;
126 m_uint64_t len;
127 m_uint32_t cached;
128 m_uint32_t offset;
129 m_uint32_t flags;
130 }mts_map_t;
131
132 /* Invalid VTLB entry */
133 #define MTS_INV_ENTRY_MASK 0x00000001
134
135 /* MTS entry flags */
136 #define MTS_FLAG_DEV 0x000000001 /* Virtual device used */
137 #define MTS_FLAG_COW 0x000000002 /* Copy-On-Write */
138 #define MTS_FLAG_EXEC 0x000000004 /* Exec page */
139 #define MTS_FLAG_RO 0x000000008 /* Read-only page */
140
141 #define MTS_FLAG_WRCATCH (MTS_FLAG_RO|MTS_FLAG_COW) /* Catch writes */
142
143 /* Virtual TLB entry (32-bit MMU) */
144 typedef struct mts32_entry mts32_entry_t;
145 struct mts32_entry {
146 m_uint32_t gvpa; /* Guest Virtual Page Address */
147 m_uint32_t gppa; /* Guest Physical Page Address */
148 m_iptr_t hpa; /* Host Page Address */
149 m_uint32_t flags; /* Flags */
150 }__attribute__ ((aligned(16)));
151
152 /* Virtual TLB entry (64-bit MMU) */
153 typedef struct mts64_entry mts64_entry_t;
154 struct mts64_entry {
155 m_uint64_t gvpa; /* Guest Virtual Page Address */
156 m_uint64_t gppa; /* Guest Physical Page Address */
157 m_iptr_t hpa; /* Host Page Address */
158 m_uint32_t flags; /* Flags */
159 }__attribute__ ((aligned(16)));
160
161 /* Host register allocation */
162 #define HREG_FLAG_ALLOC_LOCKED 1
163 #define HREG_FLAG_ALLOC_FORCED 2
164
165 struct hreg_map {
166 int hreg,vreg;
167 int flags;
168 struct hreg_map *prev,*next;
169 };
170
171 /* Global logfile */
172 extern FILE *log_file;
173
174 /* Check status of a bit */
175 static inline int check_bit(u_int old,u_int new,u_int bit)
176 {
177 int mask = 1 << bit;
178
179 if ((old & mask) && !(new & mask))
180 return(1); /* bit unset */
181
182 if (!(old & mask) && (new & mask))
183 return(2); /* bit set */
184
185 /* no change */
186 return(0);
187 }
188
189 /* Sign-extension */
190 static forced_inline m_int64_t sign_extend(m_int64_t x,int len)
191 {
192 len = 64 - len;
193 return (x << len) >> len;
194 }
195
196 /* Sign-extension (32-bit) */
197 static forced_inline m_int32_t sign_extend_32(m_int32_t x,int len)
198 {
199 len = 32 - len;
200 return (x << len) >> len;
201 }
202
203 /* Extract bits from a 32-bit values */
204 static inline int bits(m_uint32_t val,int start,int end)
205 {
206 return((val >> start) & ((1 << (end-start+1)) - 1));
207 }
208
209 /* Normalize a size */
210 static inline u_int normalize_size(u_int val,u_int nb,int shift)
211 {
212 return(((val+nb-1) & ~(nb-1)) >> shift);
213 }
214
215 /* Convert a 16-bit number between little and big endian */
216 static forced_inline m_uint16_t swap16(m_uint16_t value)
217 {
218 return((value >> 8) | ((value & 0xFF) << 8));
219 }
220
221 /* Convert a 32-bit number between little and big endian */
222 static forced_inline m_uint32_t swap32(m_uint32_t value)
223 {
224 m_uint32_t result;
225
226 result = value >> 24;
227 result |= ((value >> 16) & 0xff) << 8;
228 result |= ((value >> 8) & 0xff) << 16;
229 result |= (value & 0xff) << 24;
230 return(result);
231 }
232
233 /* Convert a 64-bit number between little and big endian */
234 static forced_inline m_uint64_t swap64(m_uint64_t value)
235 {
236 m_uint64_t result;
237
238 result = (m_uint64_t)swap32(value & 0xffffffff) << 32;
239 result |= swap32(value >> 32);
240 return(result);
241 }
242
243 /* Get current time in number of msec since epoch */
244 static inline m_tmcnt_t m_gettime(void)
245 {
246 struct timeval tvp;
247
248 gettimeofday(&tvp,NULL);
249 return(((m_tmcnt_t)tvp.tv_sec * 1000) + ((m_tmcnt_t)tvp.tv_usec / 1000));
250 }
251
252 /* Get current time in number of usec since epoch */
253 static inline m_tmcnt_t m_gettime_usec(void)
254 {
255 struct timeval tvp;
256
257 gettimeofday(&tvp,NULL);
258 return(((m_tmcnt_t)tvp.tv_sec * 1000000) + (m_tmcnt_t)tvp.tv_usec);
259 }
260
261 #ifdef __CYGWIN__
262 #define GET_TIMEZONE _timezone
263 #else
264 #define GET_TIMEZONE timezone
265 #endif
266
267 /* Get current time in number of ms (localtime) */
268 static inline m_tmcnt_t m_gettime_adj(void)
269 {
270 struct timeval tvp;
271 struct tm tmx;
272 time_t gmt_adjust;
273 time_t ct;
274
275 gettimeofday(&tvp,NULL);
276 ct = tvp.tv_sec;
277 localtime_r(&ct,&tmx);
278
279 #if defined(__CYGWIN__) || defined(SUNOS)
280 gmt_adjust = -(tmx.tm_isdst ? GET_TIMEZONE - 3600 : GET_TIMEZONE);
281 #else
282 gmt_adjust = tmx.tm_gmtoff;
283 #endif
284
285 tvp.tv_sec += gmt_adjust;
286 return(((m_tmcnt_t)tvp.tv_sec * 1000) + ((m_tmcnt_t)tvp.tv_usec / 1000));
287 }
288
289 /* Get a byte-swapped 16-bit value on a non-aligned area */
290 static inline m_uint16_t m_ntoh16(m_uint8_t *ptr)
291 {
292 m_uint16_t val = (ptr[0] << 8) | ptr[1];
293 return(val);
294 }
295
296 /* Get a byte-swapped 32-bit value on a non-aligned area */
297 static inline m_uint32_t m_ntoh32(m_uint8_t *ptr)
298 {
299 m_uint32_t val = (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
300 return(val);
301 }
302
303 /* Set a byte-swapped 16-bit value on a non-aligned area */
304 static inline void m_hton16(m_uint8_t *ptr,m_uint16_t val)
305 {
306 ptr[0] = val >> 8;
307 ptr[1] = val;
308 }
309
310 /* Set a byte-swapped 32-bit value on a non-aligned area */
311 static inline void m_hton32(m_uint8_t *ptr,m_uint32_t val)
312 {
313 ptr[0] = val >> 24;
314 ptr[1] = val >> 16;
315 ptr[2] = val >> 8;
316 ptr[3] = val;
317 }
318
319 /* Add an element to a list */
320 m_list_t *m_list_add(m_list_t **head,void *data);
321
322 /* Dynamic sprintf */
323 char *dyn_sprintf(const char *fmt,...);
324
325 /* Split a string */
326 int m_strsplit(char *str,char delim,char **array,int max_count);
327
328 /* Tokenize a string */
329 int m_strtok(char *str,char delim,char **array,int max_count);
330
331 /* Quote a string */
332 char *m_strquote(char *buffer,size_t buf_len,char *str);
333
334 /* Decode from hex. */
335 int hex_decode(unsigned char *out,const unsigned char *in,int maxlen);
336
337 /* Ugly function that dumps a structure in hexa and ascii. */
338 void mem_dump(FILE *f_output,u_char *pkt,u_int len);
339
340 /* Logging function */
341 void m_flog(FILE *fd,char *module,char *fmt,va_list ap);
342
343 /* Logging function */
344 void m_log(char *module,char *fmt,...);
345
346 /* Write an array of string to a logfile */
347 void m_flog_str_array(FILE *fd,int count,char *str[]);
348
349 /* Returns a line from specified file (remove trailing '\n') */
350 char *m_fgets(char *buffer,int size,FILE *fd);
351
352 /* Read a file and returns it in a buffer */
353 int m_read_file(const char *filename,u_char **buffer,size_t *length);
354
355 /* Allocate aligned memory */
356 void *m_memalign(size_t boundary,size_t size);
357
358 /* Block specified signal for calling thread */
359 int m_signal_block(int sig);
360
361 /* Unblock specified signal for calling thread */
362 int m_signal_unblock(int sig);
363
364 /* Set non-blocking mode on a file descriptor */
365 int m_fd_set_non_block(int fd);
366
367 /* Sync a memory zone */
368 int memzone_sync(void *addr, size_t len);
369
370 /* Sync all mappings of a memory zone */
371 int memzone_sync_all(void *addr, size_t len);
372
373 /* Unmap a memory zone */
374 int memzone_unmap(void *addr, size_t len);
375
376 /* Map a memory zone as an executable area */
377 u_char *memzone_map_exec_area(size_t len);
378
379 /* Map a memory zone from a file */
380 u_char *memzone_map_file(int fd,size_t len);
381
382 /* Map a memory zone from a file, with copy-on-write (COW) */
383 u_char *memzone_map_cow_file(int fd,size_t len);
384
385 /* Create a file to serve as a memory zone */
386 int memzone_create_file(char *filename,size_t len,u_char **ptr);
387
388 /* Open a file to serve as a COW memory zone */
389 int memzone_open_cow_file(char *filename,size_t len,u_char **ptr);
390
391 /* Open a file and map it in memory */
392 int memzone_open_file(char *filename,u_char **ptr,off_t *fsize);
393 int memzone_open_file_ro(char *filename,u_char **ptr,off_t *fsize);
394
395 /* Compute NVRAM checksum */
396 m_uint16_t nvram_cksum(m_uint16_t *ptr,size_t count);
397
398 /* Byte-swap a memory block */
399 void mem_bswap32(void *ptr,size_t len);
400
401 /* Reverse a byte */
402 m_uint8_t m_reverse_u8(m_uint8_t val);
403
404 /* Generate a pseudo random block of data */
405 void m_randomize_block(m_uint8_t *buf,size_t len);
406
407 /* Free an FD pool */
408 void fd_pool_free(fd_pool_t *pool);
409
410 /* Initialize an empty pool */
411 void fd_pool_init(fd_pool_t *pool);
412
413 /* Get a free slot for a FD in a pool */
414 int fd_pool_get_free_slot(fd_pool_t *pool,int **slot);
415
416 /* Fill a FD set and get the maximum FD in order to use with select */
417 int fd_pool_set_fds(fd_pool_t *pool,fd_set *fds);
418
419 /* Send a buffer to all FDs of a pool */
420 int fd_pool_send(fd_pool_t *pool,void *buffer,size_t len,int flags);
421
422 /* Call a function for each FD having incoming data */
423 int fd_pool_check_input(fd_pool_t *pool,fd_set *fds,
424 void (*cbk)(int *fd_slot,void *opt),void *opt);
425
426 /* Equivalent to fprintf, but for a posix fd */
427 ssize_t fd_printf(int fd,int flags,char *fmt,...);
428
429 #endif