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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 7 - (show annotations)
Sat Oct 6 16:23:47 2007 UTC (16 years, 6 months ago) by dpavlin
File MIME type: text/plain
File size: 9024 byte(s)
dynamips-0.2.7-RC1

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

  ViewVC Help
Powered by ViewVC 1.1.26