]> rtime.felk.cvut.cz Git - l4.git/blob - kernel/fiasco/src/kern/ia32/svm.cpp
update
[l4.git] / kernel / fiasco / src / kern / ia32 / svm.cpp
1 INTERFACE:
2
3 class Svm
4 {
5 };
6
7 //-----------------------------------------------------------------------------
8 INTERFACE[svm]:
9
10 #include "per_cpu_data.h"
11 #include "virt.h"
12 #include "cpu_lock.h"
13
14 EXTENSION class Svm
15 {
16 public:
17   static Per_cpu<Svm> cpus;
18
19   enum Msr_perms
20   {
21     Msr_intercept = 3,
22     Msr_ro        = 2,
23     Msr_wo        = 1,
24     Msr_rw        = 0,
25   };
26
27 private:
28   void *_vm_hsave_area;
29   void *_iopm;
30   void *_msrpm;
31   Unsigned32 _next_asid;
32   Unsigned32 _global_asid_generation;
33   Unsigned32 _max_asid;
34   bool _flush_all_asids;
35   bool _svm_enabled;
36   bool _has_npt;
37   Unsigned64 _iopm_base_pa;
38   Unsigned64 _msrpm_base_pa;
39   Vmcb *_kernel_vmcb;
40   Address _kernel_vmcb_pa;
41 };
42
43 //-----------------------------------------------------------------------------
44 INTERFACE [svm && ia32]:
45
46 EXTENSION class Svm
47 {
48 public:
49   enum { Gpregs_words = 10 };
50 };
51
52 //-----------------------------------------------------------------------------
53 INTERFACE [svm && amd64]:
54
55 EXTENSION class Svm
56 {
57 public:
58   enum { Gpregs_words = 18 };
59 };
60
61 // -----------------------------------------------------------------------
62 IMPLEMENTATION[svm]:
63
64 #include "cpu.h"
65 #include "kmem.h"
66 #include "l4_types.h"
67 #include "warn.h"
68 #include <cstring>
69
70 Per_cpu<Svm> DEFINE_PER_CPU Svm::cpus(true);
71
72 PUBLIC
73 Svm::Svm(unsigned cpu)
74 {
75   Cpu &c = Cpu::cpus.cpu(cpu);
76   _svm_enabled = false;
77   _next_asid = 1;
78   _global_asid_generation = 0;
79   _max_asid = 0;
80   _flush_all_asids = true;
81   _has_npt = false;
82
83   if (!c.svm())
84     return;
85
86   Unsigned64 efer, vmcr;
87
88   vmcr = c.rdmsr(MSR_VM_CR);
89   if (vmcr & (1 << 4)) // VM_CR.SVMDIS
90     {
91       printf("SVM supported but locked.\n");
92       return;
93     }
94
95   printf("Enabling SVM support\n");
96
97   efer = c.rdmsr(MSR_EFER);
98   efer |= 1 << 12;
99   c.wrmsr(efer, MSR_EFER);
100
101   Unsigned32 eax, ebx, ecx, edx;
102   c.cpuid (0x8000000a, &eax, &ebx, &ecx, &edx);
103   if (edx & 1)
104     {
105       printf("Nested Paging supported\n");
106       _has_npt = true;
107     }
108   printf("NASID: 0x%x\n", ebx);
109   _max_asid = ebx - 1;
110   assert(_max_asid > 0);
111   // we internally use Unigned8 for asids to save per cpu data
112   // your machine supports more asids
113   assert(_max_asid < (1<<8));
114
115   enum
116   {
117     Vmcb_size   = 0x1000,
118     Io_pm_size  = 0x3000,
119     Msr_pm_size = 0x2000,
120     State_save_area_size = 0x1000,
121   };
122
123   /* 16kB IO permission map and Vmcb (16kB are good for the buddy allocator)*/
124   check(_iopm = Mapped_allocator::allocator()->unaligned_alloc(Io_pm_size + Vmcb_size));
125   _iopm_base_pa = Kmem::virt_to_phys(_iopm);
126   _kernel_vmcb = (Vmcb*)((char*)_iopm + Io_pm_size);
127   _kernel_vmcb_pa = Kmem::virt_to_phys(_kernel_vmcb);
128   _svm_enabled = true;
129
130   /* disbale all ports */
131   memset(_iopm, ~0, Io_pm_size);
132
133   /* clean out vmcb */
134   memset(_kernel_vmcb, 0, Vmcb_size);
135
136   /* 8kB MSR permission map */
137   check(_msrpm = Mapped_allocator::allocator()->unaligned_alloc(Msr_pm_size));
138   _msrpm_base_pa = Kmem::virt_to_phys(_msrpm);
139   memset(_msrpm, ~0, Msr_pm_size);
140
141   // allow the sysenter MSRs for the guests
142   set_msr_perm(MSR_SYSENTER_CS, Msr_rw);
143   set_msr_perm(MSR_SYSENTER_EIP, Msr_rw);
144   set_msr_perm(MSR_SYSENTER_ESP, Msr_rw);
145
146   /* 4kB Host state-safe area */
147   check(_vm_hsave_area = Mapped_allocator::allocator()->unaligned_alloc(State_save_area_size));
148   Unsigned64 vm_hsave_pa = Kmem::virt_to_phys(_vm_hsave_area);
149
150   c.wrmsr(vm_hsave_pa, MSR_VM_HSAVE_PA);
151 }
152
153 PUBLIC
154 void
155 Svm::set_msr_perm(Unsigned32 msr, Msr_perms perms)
156 {
157   unsigned offs;
158   if (msr <= 0x1fff)
159     offs = 0;
160   else if (0xc0000000 <= msr && msr <= 0xc0001fff)
161     offs = 0x800;
162   else if (0xc0010000 <= msr && msr <= 0xc0011fff)
163     offs = 0x1000;
164   else
165     {
166       WARN("Illegal MSR %x\n", msr);
167       return;
168     }
169
170   msr &= 0x1fff;
171   offs += msr / 4;
172
173   unsigned char *pm = (unsigned char *)_msrpm;
174
175   unsigned shift = (msr & 3) * 2;
176   pm[offs] = (pm[offs] & ~(3 << shift)) | ((unsigned char)perms << shift);
177 }
178
179 PUBLIC
180 Unsigned64
181 Svm::iopm_base_pa()
182 { return _iopm_base_pa; }
183
184 PUBLIC
185 Unsigned64
186 Svm::msrpm_base_pa()
187 { return _msrpm_base_pa; }
188
189 PUBLIC
190 Vmcb *
191 Svm::kernel_vmcb()
192 { return _kernel_vmcb; }
193
194 PUBLIC
195 Address
196 Svm::kernel_vmcb_pa()
197 { return _kernel_vmcb_pa; }
198
199 PUBLIC
200 bool
201 Svm::svm_enabled()
202 { return _svm_enabled; }
203
204 PUBLIC
205 bool
206 Svm::has_npt()
207 { return _has_npt; }
208
209 PUBLIC
210 bool
211 Svm::asid_valid (Unsigned32 asid, Unsigned32 generation)
212 {
213   return ((asid > 0) &&
214           (asid <= _max_asid) &&
215           (generation <= _global_asid_generation));
216 }
217
218 PUBLIC
219 bool
220 Svm::flush_all_asids()
221 { return _flush_all_asids; }
222
223 PUBLIC
224 void
225 Svm::flush_all_asids(bool val)
226 { _flush_all_asids = val; }
227
228 PUBLIC
229 Unsigned32
230 Svm::global_asid_generation()
231 { return _global_asid_generation; }
232
233 PUBLIC
234 Unsigned32
235 Svm::next_asid ()
236 {
237   assert (cpu_lock.test());
238   _flush_all_asids = false;
239   if (_next_asid > _max_asid) {
240     _global_asid_generation++;
241     _next_asid = 1;
242     assert (_global_asid_generation < ~0U);
243     _flush_all_asids = true;
244   }
245   return _next_asid++;
246 }