/[dynamips]/upstream/dynamips-0.2.6-RC5/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.6-RC5/utils.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6 - (show annotations)
Sat Oct 6 16:09:07 2007 UTC (16 years, 5 months ago) by dpavlin
File MIME type: text/plain
File size: 7666 byte(s)
dynamips-0.2.6-RC5

1 /*
2 * Cisco 7200 (Predator) 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 insn_block insn_block_t;
104 typedef struct insn_exec_page insn_exec_page_t;
105
106 /* MIPS instruction */
107 typedef m_uint32_t mips_insn_t;
108
109 /* Max and min macro */
110 #define m_max(a,b) (((a) > (b)) ? (a) : (b))
111 #define m_min(a,b) (((a) < (b)) ? (a) : (b))
112
113 /* A simple macro for adjusting pointers */
114 #define PTR_ADJUST(type,ptr,size) (type)((char *)(ptr) + (size))
115
116 /* Size of a field in a structure */
117 #define SIZEOF(st,field) (sizeof(((st *)NULL)->field))
118
119 /* Compute offset of a field in a structure */
120 #define OFFSET(st,f) ((long)&((st *)(NULL))->f)
121
122 /* MMAP */
123 #ifndef MAP_ANONYMOUS
124 #define MAP_ANONYMOUS MAP_ANON
125 #endif
126
127 /* List item */
128 typedef struct m_list m_list_t;
129 struct m_list {
130 void *data;
131 m_list_t *next;
132 };
133
134 /* MTS mapping info */
135 typedef struct {
136 m_uint64_t vaddr;
137 m_uint64_t paddr;
138 m_uint64_t len;
139 m_uint32_t cached;
140 m_uint32_t tlb_index;
141 }mts_map_t;
142
143 /* Global logfile */
144 extern FILE *log_file;
145
146 /* Check status of a bit */
147 static inline int check_bit(u_int old,u_int new,u_int bit)
148 {
149 int mask = 1 << bit;
150
151 if ((old & mask) && !(new & mask))
152 return(1); /* bit unset */
153
154 if (!(old & mask) && (new & mask))
155 return(2); /* bit set */
156
157 /* no change */
158 return(0);
159 }
160
161 /* Sign-extension */
162 static forced_inline m_int64_t sign_extend(m_int64_t x,int len)
163 {
164 len = 64 - len;
165 return (x << len) >> len;
166 }
167
168 /* Extract bits from a 32-bit values */
169 static inline int bits(m_uint32_t val,int start,int end)
170 {
171 return((val >> start) & ((1 << (end-start+1)) - 1));
172 }
173
174 /* Normalize a size */
175 static inline u_int normalize_size(u_int val,u_int nb,int shift)
176 {
177 return(((val+nb-1) & ~(nb-1)) >> shift);
178 }
179
180 /* Convert a 16-bit number between little and big endian */
181 static forced_inline m_uint16_t swap16(m_uint16_t value)
182 {
183 return((value >> 8) | ((value & 0xFF) << 8));
184 }
185
186 /* Convert a 32-bit number between little and big endian */
187 static forced_inline m_uint32_t swap32(m_uint32_t value)
188 {
189 m_uint32_t result;
190
191 result = value >> 24;
192 result |= ((value >> 16) & 0xff) << 8;
193 result |= ((value >> 8) & 0xff) << 16;
194 result |= (value & 0xff) << 24;
195 return(result);
196 }
197
198 /* Convert a 64-bit number between little and big endian */
199 static forced_inline m_uint64_t swap64(m_uint64_t value)
200 {
201 m_uint64_t result;
202
203 result = (m_uint64_t)swap32(value & 0xffffffff) << 32;
204 result |= swap32(value >> 32);
205 return(result);
206 }
207
208 /* Get current time in number of msec since epoch */
209 static inline m_tmcnt_t m_gettime(void)
210 {
211 struct timeval tvp;
212
213 gettimeofday(&tvp,NULL);
214 return(((m_tmcnt_t)tvp.tv_sec * 1000) + ((m_tmcnt_t)tvp.tv_usec / 1000));
215 }
216
217 /* Get current time in number of usec since epoch */
218 static inline m_tmcnt_t m_gettime_usec(void)
219 {
220 struct timeval tvp;
221
222 gettimeofday(&tvp,NULL);
223 return(((m_tmcnt_t)tvp.tv_sec * 1000000) + (m_tmcnt_t)tvp.tv_usec);
224 }
225
226 #ifdef __CYGWIN__
227 #define GET_TIMEZONE _timezone
228 #else
229 #define GET_TIMEZONE timezone
230 #endif
231
232 /* Get current time in number of ms (localtime) */
233 static inline m_tmcnt_t m_gettime_adj(void)
234 {
235 struct timeval tvp;
236 struct tm tmx;
237 time_t gmt_adjust;
238 time_t ct;
239
240 gettimeofday(&tvp,NULL);
241 ct = tvp.tv_sec;
242 localtime_r(&ct,&tmx);
243
244 #if defined(__CYGWIN__) || defined(SUNOS)
245 gmt_adjust = -(tmx.tm_isdst ? GET_TIMEZONE - 3600 : GET_TIMEZONE);
246 #else
247 gmt_adjust = tmx.tm_gmtoff;
248 #endif
249
250 tvp.tv_sec += gmt_adjust;
251 return(((m_tmcnt_t)tvp.tv_sec * 1000) + ((m_tmcnt_t)tvp.tv_usec / 1000));
252 }
253
254 /* Add an element to a list */
255 m_list_t *m_list_add(m_list_t **head,void *data);
256
257 /* Dynamic sprintf */
258 char *dyn_sprintf(const char *fmt,...);
259
260 /* Split a string */
261 int m_strsplit(char *str,char delim,char **array,int max_count);
262
263 /* Tokenize a string */
264 int m_strtok(char *str,char delim,char **array,int max_count);
265
266 /* Quote a string */
267 char *m_strquote(char *buffer,size_t buf_len,char *str);
268
269 /* Ugly function that dumps a structure in hexa and ascii. */
270 void mem_dump(FILE *f_output,u_char *pkt,u_int len);
271
272 /* Logging function */
273 void m_flog(FILE *fd,char *module,char *fmt,va_list ap);
274
275 /* Logging function */
276 void m_log(char *module,char *fmt,...);
277
278 /* Returns a line from specified file (remove trailing '\n') */
279 char *m_fgets(char *buffer,int size,FILE *fd);
280
281 /* Read a file and returns it in a buffer */
282 ssize_t m_read_file(char *filename,char **buffer);
283
284 /* Allocate aligned memory */
285 void *m_memalign(size_t boundary,size_t size);
286
287 /* Block specified signal for calling thread */
288 int m_signal_block(int sig);
289
290 /* Unblock specified signal for calling thread */
291 int m_signal_unblock(int sig);
292
293 /* Set non-blocking mode on a file descriptor */
294 int m_fd_set_non_block(int fd);
295
296 /* Map a memory zone from a file */
297 u_char *memzone_map_file(int fd,size_t len);
298
299 /* Map a memory zone from a file, with copy-on-write (COW) */
300 u_char *memzone_map_cow_file(int fd,size_t len);
301
302 /* Create a file to serve as a memory zone */
303 int memzone_create_file(char *filename,size_t len,u_char **ptr);
304
305 /* Open a file to serve as a COW memory zone */
306 int memzone_open_cow_file(char *filename,size_t len,u_char **ptr);
307
308 /* Open a file and map it in memory */
309 int memzone_open_file(char *filename,u_char **ptr,off_t *fsize);
310
311 /* Compute NVRAM checksum */
312 m_uint16_t nvram_cksum(m_uint16_t *ptr,size_t count);
313
314 #endif

  ViewVC Help
Powered by ViewVC 1.1.26