|
Lines 41-48
Link Here
|
| 41 |
|
41 |
|
| 42 |
#include <dev/pci/pcivar.h> |
42 |
#include <dev/pci/pcivar.h> |
| 43 |
|
43 |
|
|
|
44 |
#include <machine/_inttypes.h> |
| 44 |
#include <machine/cpufunc.h> |
45 |
#include <machine/cpufunc.h> |
| 45 |
#include <machine/cpu.h> |
46 |
#include <machine/cpu.h> |
|
|
47 |
#include <machine/md_var.h> |
| 46 |
#include <machine/smp.h> |
48 |
#include <machine/smp.h> |
| 47 |
|
49 |
|
| 48 |
#include <x86/apicreg.h> |
50 |
#include <x86/apicreg.h> |
|
Lines 95-100
TUNABLE_INT("hw.xen.disable_pv_nics", &xen_disable_pv_nics);
Link Here
|
| 95 |
|
97 |
|
| 96 |
/*---------------------- XEN Hypervisor Probe and Setup ----------------------*/ |
98 |
/*---------------------- XEN Hypervisor Probe and Setup ----------------------*/ |
| 97 |
|
99 |
|
|
|
100 |
void xen_emergency_print(const char *str, size_t size) |
| 101 |
{ |
| 102 |
size_t i; |
| 103 |
|
| 104 |
for (i = 0; i < size; i++) |
| 105 |
outb(XEN_HVM_DEBUGCONS_IOPORT, str[i]); |
| 106 |
} |
| 107 |
|
| 98 |
uint32_t xen_cpuid_base; |
108 |
uint32_t xen_cpuid_base; |
| 99 |
|
109 |
|
| 100 |
static uint32_t |
110 |
static uint32_t |
|
Lines 138-144
hypervisor_version(void)
Link Here
|
| 138 |
uint32_t regs[4]; |
148 |
uint32_t regs[4]; |
| 139 |
int major, minor; |
149 |
int major, minor; |
| 140 |
|
150 |
|
| 141 |
do_cpuid(xen_cpuid_base + 1, regs); |
151 |
do_cpuid(hv_base + 1, regs); |
| 142 |
|
152 |
|
| 143 |
major = regs[0] >> 16; |
153 |
major = regs[0] >> 16; |
| 144 |
minor = regs[0] & 0xffff; |
154 |
minor = regs[0] & 0xffff; |
|
Lines 148-209
hypervisor_version(void)
Link Here
|
| 148 |
} |
158 |
} |
| 149 |
|
159 |
|
| 150 |
/* |
160 |
/* |
| 151 |
* Allocate and fill in the hypcall page. |
161 |
* Translate linear to physical address when still running on the bootloader |
|
|
162 |
* created page-tables. |
| 152 |
*/ |
163 |
*/ |
| 153 |
int |
164 |
static vm_paddr_t __nosanitizeaddress __nosanitizememory |
| 154 |
xen_hvm_init_hypercall_stubs(enum xen_hvm_init_type init_type) |
165 |
early_init_vtop(void *addr) |
| 155 |
{ |
166 |
{ |
| 156 |
uint32_t regs[4]; |
167 |
return ((uintptr_t)addr - KERNBASE |
| 157 |
|
168 |
#ifdef __amd64__ |
| 158 |
if (xen_cpuid_base != 0) |
169 |
+ kernphys - KERNLOAD |
| 159 |
/* Already setup. */ |
170 |
#endif |
| 160 |
goto out; |
171 |
); |
| 161 |
|
172 |
} |
| 162 |
xen_cpuid_base = xen_hvm_cpuid_base(); |
|
|
| 163 |
if (xen_cpuid_base == 0) |
| 164 |
return (ENXIO); |
| 165 |
|
173 |
|
|
|
174 |
static int |
| 175 |
map_shared_info(void) |
| 176 |
{ |
| 166 |
/* |
177 |
/* |
| 167 |
* Find the hypercall pages. |
178 |
* TODO shared info page should be mapped in an unpopulated (IOW: |
|
|
179 |
* non-RAM) address. But finding one at this point in boot is |
| 180 |
* complicated, hence re-use a RAM address for the time being. This |
| 181 |
* sadly causes super-page shattering in the second stage translation |
| 182 |
* page tables. |
| 168 |
*/ |
183 |
*/ |
| 169 |
do_cpuid(xen_cpuid_base + 2, regs); |
184 |
static shared_info_t shared_page __attribute__((aligned(PAGE_SIZE))); |
| 170 |
if (regs[0] != 1) |
185 |
static struct xen_add_to_physmap xatp = { |
| 171 |
return (EINVAL); |
186 |
.domid = DOMID_SELF, |
|
|
187 |
.idx = 0, |
| 188 |
.space = XENMAPSPACE_shared_info, |
| 189 |
}; |
| 190 |
int rc; |
| 172 |
|
191 |
|
| 173 |
wrmsr(regs[1], (init_type == XEN_HVM_INIT_EARLY) |
192 |
if (xatp.gpfn == 0) |
| 174 |
? (vm_paddr_t)((uintptr_t)&hypercall_page - KERNBASE) |
193 |
xatp.gpfn = atop(early_init_vtop(&shared_page)); |
| 175 |
: vtophys(&hypercall_page)); |
|
|
| 176 |
|
194 |
|
| 177 |
out: |
195 |
rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp); |
| 178 |
hypervisor_version(); |
196 |
if (rc != 0) { |
| 179 |
return (0); |
197 |
printf("cannot map shared info page: %d\n", rc); |
|
|
198 |
HYPERVISOR_shared_info = NULL; |
| 199 |
} else if (HYPERVISOR_shared_info == NULL) |
| 200 |
HYPERVISOR_shared_info = &shared_page; |
| 201 |
|
| 202 |
return (rc); |
| 180 |
} |
203 |
} |
| 181 |
|
204 |
|
| 182 |
static void |
205 |
/* |
| 183 |
xen_hvm_init_shared_info_page(void) |
206 |
* Populate the hypecall and shared pages. |
|
|
207 |
*/ |
| 208 |
void |
| 209 |
xen_early_init(void) |
| 184 |
{ |
210 |
{ |
| 185 |
struct xen_add_to_physmap xatp; |
211 |
uint32_t regs[4]; |
|
|
212 |
int rc; |
| 186 |
|
213 |
|
| 187 |
if (xen_pv_domain()) { |
214 |
#ifdef EARLY_PRINTF |
| 188 |
/* |
215 |
early_putc = &xen_early_putc; |
| 189 |
* Already setup in the PV case, shared_info is passed inside |
216 |
#endif |
| 190 |
* of the start_info struct at start of day. |
217 |
|
| 191 |
*/ |
218 |
if (hv_high < 2) { |
|
|
219 |
xc_printf("Invalid maximum leaves for hv_base\n"); |
| 220 |
vm_guest = VM_GUEST_VM; |
| 192 |
return; |
221 |
return; |
| 193 |
} |
222 |
} |
| 194 |
|
223 |
|
| 195 |
if (HYPERVISOR_shared_info == NULL) { |
224 |
/* |
| 196 |
HYPERVISOR_shared_info = malloc(PAGE_SIZE, M_XENHVM, M_NOWAIT); |
225 |
* Find the hypercall pages. |
| 197 |
if (HYPERVISOR_shared_info == NULL) |
226 |
*/ |
| 198 |
panic("Unable to allocate Xen shared info page"); |
227 |
do_cpuid(hv_base + 2, regs); |
|
|
228 |
if (regs[0] != 1) { |
| 229 |
xc_printf("Invalid number of hypercall pages %" PRIu32 "\n", |
| 230 |
regs[0]); |
| 231 |
vm_guest = VM_GUEST_VM; |
| 232 |
return; |
| 199 |
} |
233 |
} |
| 200 |
|
234 |
|
| 201 |
xatp.domid = DOMID_SELF; |
235 |
wrmsr(regs[1], early_init_vtop(&hypercall_page)); |
| 202 |
xatp.idx = 0; |
236 |
|
| 203 |
xatp.space = XENMAPSPACE_shared_info; |
237 |
rc = map_shared_info(); |
| 204 |
xatp.gpfn = vtophys(HYPERVISOR_shared_info) >> PAGE_SHIFT; |
238 |
if (rc != 0) { |
| 205 |
if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp)) |
239 |
xc_printf("cannot map shared info page: %d\n", rc); |
| 206 |
panic("HYPERVISOR_memory_op failed"); |
240 |
vm_guest = VM_GUEST_VM; |
|
|
241 |
return; |
| 242 |
} |
| 207 |
} |
243 |
} |
| 208 |
|
244 |
|
| 209 |
static int |
245 |
static int |
|
Lines 322-353
xen_hvm_disable_emulated_devices(void)
Link Here
|
| 322 |
static void |
358 |
static void |
| 323 |
xen_hvm_init(enum xen_hvm_init_type init_type) |
359 |
xen_hvm_init(enum xen_hvm_init_type init_type) |
| 324 |
{ |
360 |
{ |
| 325 |
int error; |
|
|
| 326 |
int i; |
361 |
int i; |
| 327 |
|
362 |
|
| 328 |
if (!xen_domain() || |
363 |
if (!xen_domain() || |
| 329 |
init_type == XEN_HVM_INIT_CANCELLED_SUSPEND) |
364 |
init_type == XEN_HVM_INIT_CANCELLED_SUSPEND) |
| 330 |
return; |
365 |
return; |
| 331 |
|
366 |
|
| 332 |
error = xen_hvm_init_hypercall_stubs(init_type); |
367 |
hypervisor_version(); |
| 333 |
|
368 |
|
| 334 |
switch (init_type) { |
369 |
switch (init_type) { |
| 335 |
case XEN_HVM_INIT_LATE: |
370 |
case XEN_HVM_INIT_LATE: |
| 336 |
if (error != 0) |
|
|
| 337 |
return; |
| 338 |
|
| 339 |
setup_xen_features(); |
371 |
setup_xen_features(); |
| 340 |
#ifdef SMP |
372 |
#ifdef SMP |
| 341 |
cpu_ops = xen_hvm_cpu_ops; |
373 |
cpu_ops = xen_hvm_cpu_ops; |
| 342 |
#endif |
374 |
#endif |
| 343 |
break; |
375 |
break; |
| 344 |
case XEN_HVM_INIT_RESUME: |
376 |
case XEN_HVM_INIT_RESUME: |
| 345 |
if (error != 0) |
|
|
| 346 |
panic("Unable to init Xen hypercall stubs on resume"); |
| 347 |
|
| 348 |
/* Clear stale vcpu_info. */ |
377 |
/* Clear stale vcpu_info. */ |
| 349 |
CPU_FOREACH(i) |
378 |
CPU_FOREACH(i) |
| 350 |
DPCPU_ID_SET(i, vcpu_info, NULL); |
379 |
DPCPU_ID_SET(i, vcpu_info, NULL); |
|
|
380 |
|
| 381 |
if (map_shared_info() != 0) |
| 382 |
panic("cannot map shared info page\n"); |
| 383 |
|
| 351 |
break; |
384 |
break; |
| 352 |
default: |
385 |
default: |
| 353 |
panic("Unsupported HVM initialization type"); |
386 |
panic("Unsupported HVM initialization type"); |
|
Lines 357-369
xen_hvm_init(enum xen_hvm_init_type init_type)
Link Here
|
| 357 |
xen_evtchn_needs_ack = false; |
390 |
xen_evtchn_needs_ack = false; |
| 358 |
xen_hvm_set_callback(NULL); |
391 |
xen_hvm_set_callback(NULL); |
| 359 |
|
392 |
|
| 360 |
/* |
|
|
| 361 |
* On (PV)HVM domains we need to request the hypervisor to |
| 362 |
* fill the shared info page, for PVH guest the shared_info page |
| 363 |
* is passed inside the start_info struct and is already set, so this |
| 364 |
* functions are no-ops. |
| 365 |
*/ |
| 366 |
xen_hvm_init_shared_info_page(); |
| 367 |
xen_hvm_disable_emulated_devices(); |
393 |
xen_hvm_disable_emulated_devices(); |
| 368 |
} |
394 |
} |
| 369 |
|
395 |
|
|
Lines 412-419
xen_hvm_cpu_init(void)
Link Here
|
| 412 |
* Set vCPU ID. If available fetch the ID from CPUID, if not just use |
438 |
* Set vCPU ID. If available fetch the ID from CPUID, if not just use |
| 413 |
* the ACPI ID. |
439 |
* the ACPI ID. |
| 414 |
*/ |
440 |
*/ |
| 415 |
KASSERT(xen_cpuid_base != 0, ("Invalid base Xen CPUID leaf")); |
441 |
KASSERT(hv_base != 0, ("Invalid base Xen CPUID leaf")); |
| 416 |
cpuid_count(xen_cpuid_base + 4, 0, regs); |
442 |
cpuid_count(hv_base + 4, 0, regs); |
| 417 |
KASSERT((regs[0] & XEN_HVM_CPUID_VCPU_ID_PRESENT) || |
443 |
KASSERT((regs[0] & XEN_HVM_CPUID_VCPU_ID_PRESENT) || |
| 418 |
!xen_pv_domain(), |
444 |
!xen_pv_domain(), |
| 419 |
("Xen PV domain without vcpu_id in cpuid")); |
445 |
("Xen PV domain without vcpu_id in cpuid")); |
|
Lines 443-450
xen_has_iommu_maps(void)
Link Here
|
| 443 |
{ |
469 |
{ |
| 444 |
uint32_t regs[4]; |
470 |
uint32_t regs[4]; |
| 445 |
|
471 |
|
| 446 |
KASSERT(xen_cpuid_base != 0, ("Invalid base Xen CPUID leaf")); |
472 |
KASSERT(hv_base != 0, ("Invalid base Xen CPUID leaf")); |
| 447 |
cpuid_count(xen_cpuid_base + 4, 0, regs); |
473 |
cpuid_count(hv_base + 4, 0, regs); |
| 448 |
|
474 |
|
| 449 |
return (regs[0] & XEN_HVM_CPUID_IOMMU_MAPPINGS); |
475 |
return (regs[0] & XEN_HVM_CPUID_IOMMU_MAPPINGS); |
| 450 |
} |
476 |
} |