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.


Cleanup of linkage issues for non-Linux hosts
[palacios.git] / palacios / include / palacios / vmm_lowlevel.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 #include <palacios/vmm_types.h>
21
22
23 #define CPUID_FEATURE_IDS 0x00000001
24 #define CPUID_EXT_FEATURE_IDS 0x80000001
25
26 struct seg_selector {
27     union {
28         uint16_t value;
29         struct {
30             uint8_t rpl     : 2;
31             uint8_t ti      : 1;
32             uint16_t si     : 13;
33         } __attribute__((packed));
34     } __attribute__((packed));
35 } __attribute__((packed));
36
37 struct gen_segment {
38     uint16_t limit_lo;
39     uint32_t base_lo     : 24;
40     uint8_t type         : 4;
41     uint8_t system       : 1;
42     uint8_t dpl          : 2;
43     uint8_t present      : 1;
44     uint8_t limit_hi     : 4;
45     uint8_t avail        : 1;
46     uint8_t long_mode    : 1;
47     uint8_t db           : 1;
48     uint8_t granularity  : 1;
49     uint8_t base_hi      : 8;
50 } __attribute__((packed));
51
52 struct sys_segment64 {
53     uint16_t limit_lo;
54     uint32_t base_lo     : 24;
55     uint8_t type         : 4;
56     uint8_t rsvd0        : 1;
57     uint8_t dpl          : 2;
58     uint8_t present      : 1;
59     uint8_t limit_hi     : 4;
60     uint8_t avail        : 1;
61     uint8_t rsvd1        : 3;
62     uint8_t granularity  : 1;
63     uint64_t base_hi     : 40;
64     uint32_t rsvd2;
65 } __attribute__((packed));
66
67
68
69 static void __inline__ v3_cpuid(uint32_t target, 
70                                 uint32_t * eax, uint32_t * ebx, 
71                                 uint32_t * ecx, uint32_t * edx) {
72 #ifdef __V3_64BIT__
73     // avoid rbx on -FPIC - gcc likes to own rbx even on 64 bit PIC code
74     __asm__ __volatile__ (
75                           "pushq %%rbx\n\t"
76                           "movq %%rdi, %%rbx\n\t"
77                           "cpuid\n\t"
78                           "movq %%rbx, %%rdi\n\t"
79                           "popq %%rbx\n\t"
80                           : "=a" (*eax), "=D" (*ebx), "=c" (*ecx), "=d" (*edx)
81                           : "0" (target), "2" (*ecx)
82                           );
83 #elif __V3_32BIT__
84     // 32 bit code compiled with -fPIC, cannot use ebx as an ouput reg. Fantastic.
85     __asm__ __volatile__ (
86                           "pushl %%ebx\n\t"
87                           "movl %%edi, %%ebx\n\t"
88                           "cpuid\n\t"
89                           "movl %%ebx, %%edi\n\t"
90                           "popl %%ebx\n\t"
91                           : "=a" (*eax), "=D" (*ebx), "=c" (*ecx), "=d" (*edx)
92                           : "0" (target), "2" (*ecx)
93                           );
94 #endif
95     return;
96 }
97
98
99 static void __inline__ v3_set_msr(uint_t msr, uint_t high_byte, uint_t low_byte) {
100     __asm__ __volatile__ (
101                           "wrmsr"
102                           : 
103                           : "c" (msr), "d" (high_byte), "a" (low_byte)
104                           );
105
106
107 }
108
109
110
111 static void __inline__ v3_get_msr(uint_t msr, uint_t * high_byte, uint_t * low_byte) {
112     __asm__ __volatile__ (
113                           "rdmsr"
114                           : "=d" (*high_byte), "=a" (*low_byte) 
115                           : "c" (msr)
116                           );
117 }
118
119
120
121 static void __inline__ v3_enable_ints() {
122     __asm__ __volatile__ ("sti");
123 }
124
125 static void __inline__ v3_disable_ints() {
126     __asm__ __volatile__ ("cli");
127 }
128
129
130
131
132 #ifdef __V3_32BIT__
133
134 static addr_t __inline__ v3_irq_save() {
135     addr_t state;
136
137     __asm__ __volatile__ ("pushf \n\t"
138                           "popl %0 \n\t"
139                           "cli \n\t"
140                           :"=g" (state)
141                           : 
142                           :"memory"
143                           ); 
144     return state;
145 }
146
147 static void __inline__ v3_irq_restore(addr_t state) {
148     __asm__ __volatile__("pushl %0 \n\t"
149                          "popfl \n\t"
150                          : 
151                          :"g" (state)
152                          :"memory", "cc"
153                          );
154 }
155
156 #elif __V3_64BIT__
157
158 static addr_t __inline__ v3_irq_save() {
159     addr_t state; 
160
161     __asm__ __volatile__ ("pushfq \n\t"
162                           "popq %0 \n\t"
163                           "cli \n\t"
164                           :"=g" (state)
165                           : 
166                           :"memory"
167                           ); 
168
169     return state;
170 }
171
172
173 static void __inline__ v3_irq_restore(addr_t state) {
174     __asm__ __volatile__("pushq %0 \n\t"
175                          "popfq \n\t"
176                          : 
177                          :"g" (state)
178                          :"memory", "cc"
179                          );
180 }
181
182 #endif