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.


updated comments for compiler compatibility
[palacios.git] / palacios / include / palacios / vmm_util.h
1 /*
2  * This file is part of the Palacios Virtual Machine Monitor developed
3  * by the V3VEE Project with funding from the United States National 
4  * Science Foundation and the Department of Energy.  
5  *
6  * The V3VEE Project is a joint project between Northwestern University
7  * and the University of New Mexico.  You can find out more at 
8  * http://www.v3vee.org
9  *
10  * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu> 
11  * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org> 
12  * All rights reserved.
13  *
14  * Author: Jack Lange <jarusl@cs.northwestern.edu>
15  *
16  * This is free software.  You are permitted to use,
17  * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
18  */
19
20 #ifndef __VMM_UTIL_H
21 #define __VMM_UTIL_H
22
23 #ifdef __V3VEE__
24
25 #include <palacios/vmm_types.h>
26
27
28 typedef union reg_ex {
29     ullong_t r_reg;
30     struct {
31         uint_t low;
32         uint_t high;
33     } e_reg;
34
35 } reg_ex_t;
36
37
38
39 #define GET_LOW_32(x) (*((uint_t*)(&(x))))
40 #define GET_HIGH_32(x) (*((uint_t*)(((uchar_t*)(&(x)))+4)))
41
42
43
44 void v3_dump_mem(uint8_t * 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
56
57 #define rdtscll(val)                                    \
58     do {                                                \
59         uint64_t tsc;                                   \
60         uint32_t a, d;                                  \
61         asm volatile("rdtsc" : "=a" (a), "=d" (d));     \
62         *(uint32_t *)&(tsc) = a;                        \
63         *(uint32_t *)(((uchar_t *)&tsc) + 4) = d;       \
64         val = tsc;                                      \
65     } while (0)                                 
66
67
68
69
70
71
72 #ifdef __V3_64BIT__
73
74
75 #define do_divll(n, base) ({                    \
76             uint64_t __rem = 0;                 \
77             uint64_t __num = 0;                 \
78             while (n > base) {                  \
79                 __num++;                        \
80                 n -= base;                      \
81             }                                   \
82             __rem = n;                          \
83             n = __num;                          \
84             __rem;                              \
85         })                                              
86
87 /*#define do_divll do_div*/
88
89
90 /*
91   #define do_div(n,base) ({                                     \
92   uint32_t __base = (base);                                     \
93   uint32_t __rem;                                               \
94   __rem = ((uint64_t)(n)) % __base;                             \
95   (n) = ((uint64_t)(n)) / __base;                               \
96   __rem;                                                        \
97   })
98 */
99
100 #else
101
102 /*
103  * do_div() is NOT a C function. It wants to return
104  * two values (the quotient and the remainder), but
105  * since that doesn't work very well in C, what it
106  * does is:
107  *
108  * - modifies the 64-bit dividend _in_place_
109  * - returns the 32-bit remainder
110  *
111  * This ends up being the most efficient "calling
112  * convention" on x86.
113  */
114 #define do_div(n,base) ({                                               \
115             unsigned long __upper, __low, __high, __mod, __base;        \
116             __base = (base);                                            \
117             asm("":"=a" (__low), "=d" (__high):"A" (n));                \
118             __upper = __high;                                           \
119             if (__high) {                                               \
120                 __upper = __high % (__base);                            \
121                 __high = __high / (__base);                             \
122             }                                                           \
123             asm("divl %2":"=a" (__low), "=d" (__mod):"rm" (__base), "0" (__low), "1" (__upper)); \
124             asm("":"=A" (n):"a" (__low),"d" (__high));                  \
125             __mod;                                                      \
126         })
127
128
129
130 /* This divides two 64bit unsigned ints
131  * The previous version only allows 32 bit bases(?)...
132  * 
133  * NOTE: This absolutely sucks... there has to be a better way....
134  */
135 #define do_divll(n, base) ({                    \
136             ullong_t __rem = 0;                 \
137             ullong_t __num = 0;                 \
138             while (n > base) {                  \
139                 __num++;                        \
140                 n -= base;                      \
141             }                                   \
142             __rem = n;                          \
143             n = __num;                          \
144             __rem;                              \
145         })                                              
146
147 #endif
148
149
150
151
152
153 #endif /* ! __V3VEE__ */
154
155 #endif