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.


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