]> 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 DEFINE_PER_CPU Per_cpu<Svm> 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
112   enum
113   {
114     Vmcb_size   = 0x1000,
115     Io_pm_size  = 0x3000,
116     Msr_pm_size = 0x2000,
117     State_save_area_size = 0x1000,
118   };
119
120   /* 16kB IO permission map and Vmcb (16kB are good for the buddy allocator)*/
121   check(_iopm = Kmem_alloc::allocator()->unaligned_alloc(Io_pm_size + Vmcb_size));
122   _iopm_base_pa = Kmem::virt_to_phys(_iopm);
123   _kernel_vmcb = (Vmcb*)((char*)_iopm + Io_pm_size);
124   _kernel_vmcb_pa = Kmem::virt_to_phys(_kernel_vmcb);
125   _svm_enabled = true;
126
127   /* disbale all ports */
128   memset(_iopm, ~0, Io_pm_size);
129
130   /* clean out vmcb */
131   memset(_kernel_vmcb, 0, Vmcb_size);
132
133   /* 8kB MSR permission map */
134   check(_msrpm = Kmem_alloc::allocator()->unaligned_alloc(Msr_pm_size));
135   _msrpm_base_pa = Kmem::virt_to_phys(_msrpm);
136   memset(_msrpm, ~0, Msr_pm_size);
137
138   // allow the sysenter MSRs for the guests
139   set_msr_perm(MSR_SYSENTER_CS, Msr_rw);
140   set_msr_perm(MSR_SYSENTER_EIP, Msr_rw);
141   set_msr_perm(MSR_SYSENTER_ESP, Msr_rw);
142
143   /* 4kB Host state-safe area */
144   check(_vm_hsave_area = Kmem_alloc::allocator()->unaligned_alloc(State_save_area_size));
145   Unsigned64 vm_hsave_pa = Kmem::virt_to_phys(_vm_hsave_area);
146
147   c.wrmsr(vm_hsave_pa, MSR_VM_HSAVE_PA);
148 }
149
150 PUBLIC
151 void
152 Svm::set_msr_perm(Unsigned32 msr, Msr_perms perms)
153 {
154   unsigned offs;
155   if (msr <= 0x1fff)
156     offs = 0;
157   else if (0xc0000000 <= msr && msr <= 0xc0001fff)
158     offs = 0x800;
159   else if (0xc0010000 <= msr && msr <= 0xc0011fff)
160     offs = 0x1000;
161   else
162     {
163       WARN("Illegal MSR %x\n", msr);
164       return;
165     }
166
167   msr &= 0x1fff;
168   offs += msr / 4;
169
170   unsigned char *pm = (unsigned char *)_msrpm;
171
172   unsigned shift = (msr & 3) * 2;
173   pm[offs] = (pm[offs] & ~(3 << shift)) | ((unsigned char)perms << shift);
174 }
175
176 PUBLIC
177 Unsigned64
178 Svm::iopm_base_pa()
179 { return _iopm_base_pa; }
180
181 PUBLIC
182 Unsigned64
183 Svm::msrpm_base_pa()
184 { return _msrpm_base_pa; }
185
186 PUBLIC
187 Vmcb *
188 Svm::kernel_vmcb()
189 { return _kernel_vmcb; }
190
191 PUBLIC
192 Address
193 Svm::kernel_vmcb_pa()
194 { return _kernel_vmcb_pa; }
195
196 PUBLIC
197 bool
198 Svm::svm_enabled()
199 { return _svm_enabled; }
200
201 PUBLIC
202 bool
203 Svm::has_npt()
204 { return _has_npt; }
205
206 PUBLIC
207 bool
208 Svm::asid_valid (Unsigned32 asid, Unsigned32 generation)
209 {
210   return ((asid > 0) &&
211           (asid <= _max_asid) &&
212           (generation <= _global_asid_generation));
213 }
214
215 PUBLIC
216 bool
217 Svm::flush_all_asids()
218 { return _flush_all_asids; }
219
220 PUBLIC
221 void
222 Svm::flush_all_asids(bool val)
223 { _flush_all_asids = val; }
224
225 PUBLIC
226 Unsigned32
227 Svm::global_asid_generation()
228 { return _global_asid_generation; }
229
230 PUBLIC
231 Unsigned32
232 Svm::next_asid ()
233 {
234   assert(cpu_lock.test());
235   _flush_all_asids = false;
236   if (_next_asid > _max_asid)
237     {
238       _global_asid_generation++;
239       _next_asid = 1;
240       assert (_global_asid_generation < ~0U);
241       _flush_all_asids = true;
242     }
243   return _next_asid++;
244 }