/[dynamips]/trunk/utils.h
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/utils.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 12 - (show annotations)
Sat Oct 6 16:45:40 2007 UTC (11 years, 11 months ago) by dpavlin
File MIME type: text/plain
File size: 9842 byte(s)
make working copy

1 /*
2 * Cisco router simulation platform.
3 * Copyright (c) 2005,2006 Christophe Fillot (cf@utc.fr)
4 */
5
6 #ifndef __UTILS_H__
7 #define __UTILS_H__
8
9 #include <stdarg.h>
10 #include <sys/types.h>
11 #include <sys/mman.h>
12 #include <sys/time.h>
13 #include <time.h>
14 #include <netinet/in.h>
15 #include <pthread.h>
16 #include <signal.h>
17
18 /* True/False definitions */
19 #ifndef FALSE
20 #define FALSE 0
21 #endif
22
23 #ifndef TRUE
24 #define TRUE 1
25 #endif
26
27 /* Host CPU Types */
28 #define CPU_x86 0
29 #define CPU_amd64 1
30 #define CPU_nojit 2
31
32 /* Number of host registers available for JIT */
33 #if JIT_CPU == CPU_x86
34 #define JIT_HOST_NREG 8
35 #elif JIT_CPU == CPU_amd64
36 #define JIT_HOST_NREG 16
37 #else
38 #define JIT_HOST_NREG 0
39 #endif
40
41 /* Endianness */
42 #define ARCH_BIG_ENDIAN 0x4321
43 #define ARCH_LITTLE_ENDIAN 0x1234
44
45 #if defined(PPC) || defined(__powerpc__) || defined(__ppc__)
46 #define ARCH_BYTE_ORDER ARCH_BIG_ENDIAN
47 #elif defined(__sparc) || defined(__sparc__)
48 #define ARCH_BYTE_ORDER ARCH_BIG_ENDIAN
49 #elif defined(__alpha) || defined(__alpha__)
50 #define ARCH_BYTE_ORDER ARCH_LITTLE_ENDIAN
51 #elif defined(__i386) || defined(__i386__) || defined(i386)
52 #define ARCH_BYTE_ORDER ARCH_LITTLE_ENDIAN
53 #elif defined(__x86_64__)
54 #define ARCH_BYTE_ORDER ARCH_LITTLE_ENDIAN
55 #elif defined(__ia64__)
56 #define ARCH_BYTE_ORDER ARCH_LITTLE_ENDIAN
57 #endif
58
59 #ifndef ARCH_BYTE_ORDER
60 #error Please define your architecture in utils.h!
61 #endif
62
63 /* Host to VM (big-endian) conversion functions */
64 #if ARCH_BYTE_ORDER == ARCH_BIG_ENDIAN
65 #define htovm16(x) (x)
66 #define htovm32(x) (x)
67 #define htovm64(x) (x)
68
69 #define vmtoh16(x) (x)
70 #define vmtoh32(x) (x)
71 #define vmtoh64(x) (x)
72 #else
73 #define htovm16(x) (htons(x))
74 #define htovm32(x) (htonl(x))
75 #define htovm64(x) (swap64(x))
76
77 #define vmtoh16(x) (ntohs(x))
78 #define vmtoh32(x) (ntohl(x))
79 #define vmtoh64(x) (swap64(x))
80 #endif
81
82 /* Useful attributes for functions */
83 #define asmlinkage __attribute__((regparm(0)))
84 #define fastcall __attribute__((regparm(3)))
85
86 #if __GNUC__ > 2
87 #define forced_inline inline __attribute__((always_inline))
88 #define no_inline __attribute__ ((noinline))
89 #else
90 #define forced_inline inline
91 #define no_inline
92 #endif
93
94 #if __GNUC__ > 2
95 /* http://kerneltrap.org/node/4705 */
96 #define likely(x) __builtin_expect(!!(x),1)
97 #define unlikely(x) __builtin_expect((x),0)
98 #else
99 #define likely(x) (x)
100 #define unlikely(x) (x)
101 #endif
102
103 /* Common types */
104 typedef unsigned char m_uint8_t;
105 typedef signed char m_int8_t;
106
107 typedef unsigned short m_uint16_t;
108 typedef signed short m_int16_t;
109
110 typedef unsigned int m_uint32_t;
111 typedef signed int m_int32_t;
112
113 typedef unsigned long long m_uint64_t;
114 typedef signed long long m_int64_t;
115
116 typedef unsigned long m_iptr_t;
117 typedef m_uint64_t m_tmcnt_t;
118
119 /* Forward declarations */
120 typedef struct cpu_gen cpu_gen_t;
121 typedef struct vm_instance vm_instance_t;
122 typedef struct vm_platform vm_platform_t;
123 typedef struct mips64_jit_tcb mips64_jit_tcb_t;
124 typedef struct ppc32_jit_tcb ppc32_jit_tcb_t;
125 typedef struct jit_op jit_op_t;
126
127 /* Translated block function pointer */
128 typedef void (*insn_tblock_fptr)(void);
129
130 /* Host executable page */
131 typedef struct insn_exec_page insn_exec_page_t;
132 struct insn_exec_page {
133 u_char *ptr;
134 insn_exec_page_t *next;
135 };
136
137 /* MIPS instruction */
138 typedef m_uint32_t mips_insn_t;
139
140 /* PowerPC instruction */
141 typedef m_uint32_t ppc_insn_t;
142
143 /* Max and min macro */
144 #define m_max(a,b) (((a) > (b)) ? (a) : (b))
145 #define m_min(a,b) (((a) < (b)) ? (a) : (b))
146
147 /* A simple macro for adjusting pointers */
148 #define PTR_ADJUST(type,ptr,size) (type)((char *)(ptr) + (size))
149
150 /* Size of a field in a structure */
151 #define SIZEOF(st,field) (sizeof(((st *)NULL)->field))
152
153 /* Compute offset of a field in a structure */
154 #define OFFSET(st,f) ((long)&((st *)(NULL))->f)
155
156 /* MMAP */
157 #ifndef MAP_ANONYMOUS
158 #define MAP_ANONYMOUS MAP_ANON
159 #endif
160
161 /* List item */
162 typedef struct m_list m_list_t;
163 struct m_list {
164 void *data;
165 m_list_t *next;
166 };
167
168 /* MTS mapping info */
169 typedef struct {
170 m_uint64_t vaddr;
171 m_uint64_t paddr;
172 m_uint64_t len;
173 m_uint32_t cached;
174 m_uint32_t tlb_index;
175 m_uint32_t offset;
176 }mts_map_t;
177
178 /* Invalid VTLB entry */
179 #define MTS_INV_ENTRY_MASK 0x00000001
180
181 /* MTS entry flags */
182 #define MTS_FLAG_DEV 0x000000001 /* Virtual device used */
183 #define MTS_FLAG_COW 0x000000002 /* Copy-On-Write */
184 #define MTS_FLAG_EXEC 0x000000004 /* Exec page */
185
186 /* Virtual TLB entry (32-bit MMU) */
187 typedef struct mts32_entry mts32_entry_t;
188 struct mts32_entry {
189 m_uint32_t gvpa; /* Guest Virtual Page Address */
190 m_uint32_t gppa; /* Guest Physical Page Address */
191 m_iptr_t hpa; /* Host Page Address */
192 m_uint32_t flags; /* Flags */
193 }__attribute__ ((aligned(16)));
194
195 /* Virtual TLB entry (64-bit MMU) */
196 typedef struct mts64_entry mts64_entry_t;
197 struct mts64_entry {
198 m_uint64_t gvpa; /* Guest Virtual Page Address */
199 m_uint64_t gppa; /* Guest Physical Page Address */
200 m_iptr_t hpa; /* Host Page Address */
201 m_uint32_t flags; /* Flags */
202 }__attribute__ ((aligned(16)));
203
204 /* Host register allocation */
205 #define HREG_FLAG_ALLOC_LOCKED 1
206 #define HREG_FLAG_ALLOC_FORCED 2
207
208 struct hreg_map {
209 int hreg,vreg;
210 int flags;
211 struct hreg_map *prev,*next;
212 };
213
214 /* Global logfile */
215 extern FILE *log_file;
216
217 /* Check status of a bit */
218 static inline int check_bit(u_int old,u_int new,u_int bit)
219 {
220 int mask = 1 << bit;
221
222 if ((old & mask) && !(new & mask))
223 return(1); /* bit unset */
224
225 if (!(old & mask) && (new & mask))
226 return(2); /* bit set */
227
228 /* no change */
229 return(0);
230 }
231
232 /* Sign-extension */
233 static forced_inline m_int64_t sign_extend(m_int64_t x,int len)
234 {
235 len = 64 - len;
236 return (x << len) >> len;
237 }
238
239 /* Sign-extension (32-bit) */
240 static forced_inline m_int32_t sign_extend_32(m_int32_t x,int len)
241 {
242 len = 32 - len;
243 return (x << len) >> len;
244 }
245
246 /* Extract bits from a 32-bit values */
247 static inline int bits(m_uint32_t val,int start,int end)
248 {
249 return((val >> start) & ((1 << (end-start+1)) - 1));
250 }
251
252 /* Normalize a size */
253 static inline u_int normalize_size(u_int val,u_int nb,int shift)
254 {
255 return(((val+nb-1) & ~(nb-1)) >> shift);
256 }
257
258 /* Convert a 16-bit number between little and big endian */
259 static forced_inline m_uint16_t swap16(m_uint16_t value)
260 {
261 return((value >> 8) | ((value & 0xFF) << 8));
262 }
263
264 /* Convert a 32-bit number between little and big endian */
265 static forced_inline m_uint32_t swap32(m_uint32_t value)
266 {
267 m_uint32_t result;
268
269 result = value >> 24;
270 result |= ((value >> 16) & 0xff) << 8;
271 result |= ((value >> 8) & 0xff) << 16;
272 result |= (value & 0xff) << 24;
273 return(result);
274 }
275
276 /* Convert a 64-bit number between little and big endian */
277 static forced_inline m_uint64_t swap64(m_uint64_t value)
278 {
279 m_uint64_t result;
280
281 result = (m_uint64_t)swap32(value & 0xffffffff) << 32;
282 result |= swap32(value >> 32);
283 return(result);
284 }
285
286 /* Get current time in number of msec since epoch */
287 static inline m_tmcnt_t m_gettime(void)
288 {
289 struct timeval tvp;
290
291 gettimeofday(&tvp,NULL);
292 return(((m_tmcnt_t)tvp.tv_sec * 1000) + ((m_tmcnt_t)tvp.tv_usec / 1000));
293 }
294
295 /* Get current time in number of usec since epoch */
296 static inline m_tmcnt_t m_gettime_usec(void)
297 {
298 struct timeval tvp;
299
300 gettimeofday(&tvp,NULL);
301 return(((m_tmcnt_t)tvp.tv_sec * 1000000) + (m_tmcnt_t)tvp.tv_usec);
302 }
303
304 #ifdef __CYGWIN__
305 #define GET_TIMEZONE _timezone
306 #else
307 #define GET_TIMEZONE timezone
308 #endif
309
310 /* Get current time in number of ms (localtime) */
311 static inline m_tmcnt_t m_gettime_adj(void)
312 {
313 struct timeval tvp;
314 struct tm tmx;
315 time_t gmt_adjust;
316 time_t ct;
317
318 gettimeofday(&tvp,NULL);
319 ct = tvp.tv_sec;
320 localtime_r(&ct,&tmx);
321
322 #if defined(__CYGWIN__) || defined(SUNOS)
323 gmt_adjust = -(tmx.tm_isdst ? GET_TIMEZONE - 3600 : GET_TIMEZONE);
324 #else
325 gmt_adjust = tmx.tm_gmtoff;
326 #endif
327
328 tvp.tv_sec += gmt_adjust;
329 return(((m_tmcnt_t)tvp.tv_sec * 1000) + ((m_tmcnt_t)tvp.tv_usec / 1000));
330 }
331
332 /* Add an element to a list */
333 m_list_t *m_list_add(m_list_t **head,void *data);
334
335 /* Dynamic sprintf */
336 char *dyn_sprintf(const char *fmt,...);
337
338 /* Split a string */
339 int m_strsplit(char *str,char delim,char **array,int max_count);
340
341 /* Tokenize a string */
342 int m_strtok(char *str,char delim,char **array,int max_count);
343
344 /* Quote a string */
345 char *m_strquote(char *buffer,size_t buf_len,char *str);
346
347 /* Ugly function that dumps a structure in hexa and ascii. */
348 void mem_dump(FILE *f_output,u_char *pkt,u_int len);
349
350 /* Logging function */
351 void m_flog(FILE *fd,char *module,char *fmt,va_list ap);
352
353 /* Logging function */
354 void m_log(char *module,char *fmt,...);
355
356 /* Returns a line from specified file (remove trailing '\n') */
357 char *m_fgets(char *buffer,int size,FILE *fd);
358
359 /* Read a file and returns it in a buffer */
360 ssize_t m_read_file(char *filename,u_char **buffer);
361
362 /* Allocate aligned memory */
363 void *m_memalign(size_t boundary,size_t size);
364
365 /* Block specified signal for calling thread */
366 int m_signal_block(int sig);
367
368 /* Unblock specified signal for calling thread */
369 int m_signal_unblock(int sig);
370
371 /* Set non-blocking mode on a file descriptor */
372 int m_fd_set_non_block(int fd);
373
374 /* Map a memory zone from a file */
375 u_char *memzone_map_file(int fd,size_t len);
376
377 /* Map a memory zone from a file, with copy-on-write (COW) */
378 u_char *memzone_map_cow_file(int fd,size_t len);
379
380 /* Create a file to serve as a memory zone */
381 int memzone_create_file(char *filename,size_t len,u_char **ptr);
382
383 /* Open a file to serve as a COW memory zone */
384 int memzone_open_cow_file(char *filename,size_t len,u_char **ptr);
385
386 /* Open a file and map it in memory */
387 int memzone_open_file(char *filename,u_char **ptr,off_t *fsize);
388
389 /* Compute NVRAM checksum */
390 m_uint16_t nvram_cksum(m_uint16_t *ptr,size_t count);
391
392 /* Byte-swap a memory block */
393 void mem_bswap32(void *ptr,size_t len);
394
395 /* Reverse a byte */
396 m_uint8_t m_reverse_u8(m_uint8_t val);
397
398 #endif

  ViewVC Help
Powered by ViewVC 1.1.26