Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


modified copyright tags
[palacios.git] / palacios / include / palacios / vmm_util.h
1 /* (c) 2008, Jack Lange <jarusl@cs.northwestern.edu> */
2 /* (c) 2008, The V3VEE Project <http://www.v3vee.org> */
3
4 #ifndef __VMM_UTIL_H
5 #define __VMM_UTIL_H
6
7 #ifdef __V3VEE__
8
9 #include <palacios/vmm_types.h>
10
11
12 #ifndef PAGE_SIZE
13 #define PAGE_SIZE 4096
14 #endif
15
16
17 typedef union reg_ex {
18   ullong_t r_reg;
19   struct {
20     uint_t low;
21     uint_t high;
22   } e_reg;
23
24 } reg_ex_t;
25
26
27
28 // These are the GPRs layed out according to 'pusha'
29 struct VMM_GPRs {
30   uint_t edi;
31   uint_t esi;
32   uint_t ebp;
33   uint_t esp;
34   uint_t ebx;
35   uint_t edx;
36   uint_t ecx;
37   uint_t eax;
38 };
39
40
41 #define GET_LOW_32(x) (*((uint_t*)(&(x))))
42 #define GET_HIGH_32(x) (*((uint_t*)(((char*)(&(x)))+4)))
43
44
45 void PrintTraceHex(unsigned char x);
46 void PrintTraceLL(ullong_t num);
47 void PrintTraceMemDump(unsigned char * start, int n);
48
49
50
51
52 #define rdtsc(low,high)                                         \
53      __asm__ __volatile__("rdtsc" : "=a" (low), "=d" (high))
54
55 #define rdtscl(low)                                             \
56      __asm__ __volatile__("rdtsc" : "=a" (low) : : "edx")
57
58 #if defined(__i386__)
59
60 #define rdtscll(val)                            \
61      __asm__ __volatile__("rdtsc" : "=A" (val))
62
63 #elif defined(__x86_64__)
64
65 #define rdtscll(val) do {                                   \
66     unsigned int a,d;                                       \
67     asm volatile("rdtsc" : "=a" (a), "=d" (d));             \
68     (val) = ((unsigned long)a) | (((unsigned long)d)<<32);  \
69   } while(0)
70
71 #endif
72
73
74
75
76
77
78
79
80
81 #ifdef __V3_64BIT__
82
83 # define do_div(n,base) ({                                      \
84         uint32_t __base = (base);                               \
85         uint32_t __rem;                                         \
86         __rem = ((uint64_t)(n)) % __base;                       \
87         (n) = ((uint64_t)(n)) / __base;                         \
88         __rem;                                                  \
89  })
90
91 #else
92
93 /*
94  * do_div() is NOT a C function. It wants to return
95  * two values (the quotient and the remainder), but
96  * since that doesn't work very well in C, what it
97  * does is:
98  *
99  * - modifies the 64-bit dividend _in_place_
100  * - returns the 32-bit remainder
101  *
102  * This ends up being the most efficient "calling
103  * convention" on x86.
104  */
105 #define do_div(n,base) ({                                    \
106       unsigned long __upper, __low, __high, __mod, __base;   \
107       __base = (base);                                       \
108       asm("":"=a" (__low), "=d" (__high):"A" (n));           \
109       __upper = __high;                                      \
110       if (__high) {                                          \
111         __upper = __high % (__base);                         \
112         __high = __high / (__base);                          \
113       }                                                                 \
114       asm("divl %2":"=a" (__low), "=d" (__mod):"rm" (__base), "0" (__low), "1" (__upper)); \
115       asm("":"=A" (n):"a" (__low),"d" (__high));                        \
116       __mod;                                                            \
117     })
118
119
120
121 /* This divides two 64bit unsigned ints
122  * The previous version only allows 32 bit bases(?)...
123  * 
124  * NOTE: This absolutely sucks... there has to be a better way....
125  */
126 #define do_divll(n, base) ({                            \
127       ullong_t __rem = 0;                               \
128       ullong_t __num = 0;                               \
129       while (n > base) {                                \
130         __num++;                                        \
131         n -= base;                                      \
132       }                                                 \
133       __rem = n;                                        \
134       n = __num;                                        \
135       __rem;                                            \
136     })                                          
137
138 #endif
139
140
141
142
143
144 #endif // ! __V3VEE__
145
146 #endif