/[dynamips]/upstream/dynamips-0.2.7-RC2/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

Annotation of /upstream/dynamips-0.2.7-RC2/utils.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 8 - (hide annotations)
Sat Oct 6 16:24:54 2007 UTC (16 years, 6 months ago) by dpavlin
File MIME type: text/plain
File size: 9140 byte(s)
dynamips-0.2.7-RC2

1 dpavlin 1 /*
2 dpavlin 7 * Cisco router simulation platform.
3 dpavlin 1 * 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 dpavlin 7 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 dpavlin 1 typedef struct insn_exec_page insn_exec_page_t;
111 dpavlin 7 struct insn_exec_page {
112     u_char *ptr;
113     insn_exec_page_t *next;
114     };
115 dpavlin 1
116     /* MIPS instruction */
117     typedef m_uint32_t mips_insn_t;
118    
119 dpavlin 7 /* PowerPC instruction */
120     typedef m_uint32_t ppc_insn_t;
121    
122 dpavlin 1 /* 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 dpavlin 7 /* Invalid VTLB entry */
157     #define MTS_INV_ENTRY_MASK 0x00000001
158    
159     /* MTS entry flags */
160 dpavlin 8 #define MTS_FLAG_DEV 0x000000001 /* Virtual device used */
161     #define MTS_FLAG_COW 0x000000002 /* Copy-On-Write */
162     #define MTS_FLAG_EXEC 0x000000004 /* Exec page */
163 dpavlin 7
164     /* Virtual TLB entry (32-bit MMU) */
165     typedef struct mts32_entry mts32_entry_t;
166     struct mts32_entry {
167     m_uint32_t gvpa; /* Guest Virtual Page Address */
168     m_uint32_t gppa; /* Guest Physical Page Address */
169     m_iptr_t hpa; /* Host Page Address */
170     m_uint32_t flags; /* Flags */
171     }__attribute__ ((aligned(16)));
172    
173     /* Virtual TLB entry (64-bit MMU) */
174     typedef struct mts64_entry mts64_entry_t;
175     struct mts64_entry {
176     m_uint64_t gvpa; /* Guest Virtual Page Address */
177     m_uint64_t gppa; /* Guest Physical Page Address */
178     m_iptr_t hpa; /* Host Page Address */
179     m_uint32_t flags; /* Flags */
180     }__attribute__ ((aligned(16)));
181    
182 dpavlin 1 /* Global logfile */
183     extern FILE *log_file;
184    
185     /* Check status of a bit */
186     static inline int check_bit(u_int old,u_int new,u_int bit)
187     {
188     int mask = 1 << bit;
189    
190     if ((old & mask) && !(new & mask))
191     return(1); /* bit unset */
192    
193     if (!(old & mask) && (new & mask))
194     return(2); /* bit set */
195    
196     /* no change */
197     return(0);
198     }
199    
200     /* Sign-extension */
201     static forced_inline m_int64_t sign_extend(m_int64_t x,int len)
202     {
203     len = 64 - len;
204     return (x << len) >> len;
205     }
206    
207 dpavlin 7 /* Sign-extension (32-bit) */
208     static forced_inline m_int32_t sign_extend_32(m_int32_t x,int len)
209     {
210     len = 32 - len;
211     return (x << len) >> len;
212     }
213    
214 dpavlin 1 /* Extract bits from a 32-bit values */
215     static inline int bits(m_uint32_t val,int start,int end)
216     {
217     return((val >> start) & ((1 << (end-start+1)) - 1));
218     }
219    
220     /* Normalize a size */
221     static inline u_int normalize_size(u_int val,u_int nb,int shift)
222     {
223     return(((val+nb-1) & ~(nb-1)) >> shift);
224     }
225    
226     /* Convert a 16-bit number between little and big endian */
227     static forced_inline m_uint16_t swap16(m_uint16_t value)
228     {
229     return((value >> 8) | ((value & 0xFF) << 8));
230     }
231    
232     /* Convert a 32-bit number between little and big endian */
233     static forced_inline m_uint32_t swap32(m_uint32_t value)
234     {
235     m_uint32_t result;
236    
237     result = value >> 24;
238     result |= ((value >> 16) & 0xff) << 8;
239     result |= ((value >> 8) & 0xff) << 16;
240     result |= (value & 0xff) << 24;
241     return(result);
242     }
243    
244     /* Convert a 64-bit number between little and big endian */
245     static forced_inline m_uint64_t swap64(m_uint64_t value)
246     {
247     m_uint64_t result;
248    
249     result = (m_uint64_t)swap32(value & 0xffffffff) << 32;
250     result |= swap32(value >> 32);
251     return(result);
252     }
253    
254     /* Get current time in number of msec since epoch */
255     static inline m_tmcnt_t m_gettime(void)
256     {
257     struct timeval tvp;
258    
259     gettimeofday(&tvp,NULL);
260     return(((m_tmcnt_t)tvp.tv_sec * 1000) + ((m_tmcnt_t)tvp.tv_usec / 1000));
261     }
262    
263     /* Get current time in number of usec since epoch */
264     static inline m_tmcnt_t m_gettime_usec(void)
265     {
266     struct timeval tvp;
267    
268     gettimeofday(&tvp,NULL);
269     return(((m_tmcnt_t)tvp.tv_sec * 1000000) + (m_tmcnt_t)tvp.tv_usec);
270     }
271    
272 dpavlin 3 #ifdef __CYGWIN__
273     #define GET_TIMEZONE _timezone
274     #else
275     #define GET_TIMEZONE timezone
276     #endif
277    
278     /* Get current time in number of ms (localtime) */
279     static inline m_tmcnt_t m_gettime_adj(void)
280     {
281     struct timeval tvp;
282     struct tm tmx;
283     time_t gmt_adjust;
284     time_t ct;
285    
286     gettimeofday(&tvp,NULL);
287     ct = tvp.tv_sec;
288     localtime_r(&ct,&tmx);
289    
290     #if defined(__CYGWIN__) || defined(SUNOS)
291     gmt_adjust = -(tmx.tm_isdst ? GET_TIMEZONE - 3600 : GET_TIMEZONE);
292     #else
293     gmt_adjust = tmx.tm_gmtoff;
294     #endif
295    
296     tvp.tv_sec += gmt_adjust;
297     return(((m_tmcnt_t)tvp.tv_sec * 1000) + ((m_tmcnt_t)tvp.tv_usec / 1000));
298     }
299    
300 dpavlin 1 /* Add an element to a list */
301     m_list_t *m_list_add(m_list_t **head,void *data);
302    
303     /* Dynamic sprintf */
304     char *dyn_sprintf(const char *fmt,...);
305    
306     /* Split a string */
307     int m_strsplit(char *str,char delim,char **array,int max_count);
308    
309     /* Tokenize a string */
310     int m_strtok(char *str,char delim,char **array,int max_count);
311    
312     /* Quote a string */
313     char *m_strquote(char *buffer,size_t buf_len,char *str);
314    
315     /* Ugly function that dumps a structure in hexa and ascii. */
316     void mem_dump(FILE *f_output,u_char *pkt,u_int len);
317    
318     /* Logging function */
319     void m_flog(FILE *fd,char *module,char *fmt,va_list ap);
320    
321     /* Logging function */
322     void m_log(char *module,char *fmt,...);
323    
324     /* Returns a line from specified file (remove trailing '\n') */
325     char *m_fgets(char *buffer,int size,FILE *fd);
326    
327     /* Read a file and returns it in a buffer */
328     ssize_t m_read_file(char *filename,char **buffer);
329    
330     /* Allocate aligned memory */
331     void *m_memalign(size_t boundary,size_t size);
332    
333 dpavlin 3 /* Block specified signal for calling thread */
334     int m_signal_block(int sig);
335    
336     /* Unblock specified signal for calling thread */
337     int m_signal_unblock(int sig);
338    
339     /* Set non-blocking mode on a file descriptor */
340     int m_fd_set_non_block(int fd);
341    
342 dpavlin 4 /* Map a memory zone from a file */
343     u_char *memzone_map_file(int fd,size_t len);
344    
345     /* Map a memory zone from a file, with copy-on-write (COW) */
346     u_char *memzone_map_cow_file(int fd,size_t len);
347    
348     /* Create a file to serve as a memory zone */
349     int memzone_create_file(char *filename,size_t len,u_char **ptr);
350    
351     /* Open a file to serve as a COW memory zone */
352     int memzone_open_cow_file(char *filename,size_t len,u_char **ptr);
353    
354     /* Open a file and map it in memory */
355     int memzone_open_file(char *filename,u_char **ptr,off_t *fsize);
356    
357     /* Compute NVRAM checksum */
358     m_uint16_t nvram_cksum(m_uint16_t *ptr,size_t count);
359    
360 dpavlin 7 /* Byte-swap a memory block */
361     void mem_bswap32(void *ptr,size_t len);
362    
363 dpavlin 8 /* Reverse a byte */
364     m_uint8_t m_reverse_u8(m_uint8_t val);
365    
366 dpavlin 1 #endif

  ViewVC Help
Powered by ViewVC 1.1.26