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