/[dynamips]/upstream/dynamips-0.2.7-RC3/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 /upstream/dynamips-0.2.7-RC3/utils.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 9 - (show annotations)
Sat Oct 6 16:26:06 2007 UTC (13 years, 2 months ago) by dpavlin
File MIME type: text/plain
File size: 9666 byte(s)
dynamips-0.2.7-RC3

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

  ViewVC Help
Powered by ViewVC 1.1.26