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.


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