2 * Copyright (c) 2011 NetApp, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
36 #include <x86/specialreg.h>
37 #include <x86/apicreg.h>
39 #include <machine/vmm.h>
41 #include "vmm_lapic.h"
45 lapic_pending_intr(struct vm *vm, int cpu)
47 struct vlapic *vlapic;
49 vlapic = vm_lapic(vm, cpu);
51 return (vlapic_pending_intr(vlapic));
55 lapic_intr_accepted(struct vm *vm, int cpu, int vector)
57 struct vlapic *vlapic;
59 vlapic = vm_lapic(vm, cpu);
61 vlapic_intr_accepted(vlapic, vector);
65 lapic_set_intr(struct vm *vm, int cpu, int vector)
67 struct vlapic *vlapic;
69 if (cpu < 0 || cpu >= VM_MAXCPU)
72 if (vector < 32 || vector > 255)
75 vlapic = vm_lapic(vm, cpu);
76 vlapic_set_intr_ready(vlapic, vector);
78 vm_interrupt_hostcpu(vm, cpu);
84 lapic_timer_tick(struct vm *vm, int cpu)
86 struct vlapic *vlapic;
88 vlapic = vm_lapic(vm, cpu);
90 return (vlapic_timer_tick(vlapic));
96 if (msr >= 0x800 && msr <= 0xBFF)
103 x2apic_msr_to_regoff(u_int msr)
106 return ((msr - 0x800) << 4);
113 if (x2apic_msr(msr) || (msr == MSR_APICBASE))
120 lapic_rdmsr(struct vm *vm, int cpu, u_int msr, uint64_t *rval)
124 struct vlapic *vlapic;
126 vlapic = vm_lapic(vm, cpu);
128 if (msr == MSR_APICBASE) {
129 *rval = vlapic_get_apicbase(vlapic);
132 offset = x2apic_msr_to_regoff(msr);
133 error = vlapic_op_mem_read(vlapic, offset, DWORD, rval);
140 lapic_wrmsr(struct vm *vm, int cpu, u_int msr, uint64_t val)
144 struct vlapic *vlapic;
146 vlapic = vm_lapic(vm, cpu);
148 if (msr == MSR_APICBASE) {
149 vlapic_set_apicbase(vlapic, val);
152 offset = x2apic_msr_to_regoff(msr);
153 error = vlapic_op_mem_write(vlapic, offset, DWORD, val);
160 lapic_mmio_write(void *vm, int cpu, uint64_t gpa, uint64_t wval, int size,
165 struct vlapic *vlapic;
167 off = gpa - DEFAULT_APIC_BASE;
170 * Memory mapped local apic accesses must be 4 bytes wide and
171 * aligned on a 16-byte boundary.
173 if (size != 4 || off & 0xf)
176 vlapic = vm_lapic(vm, cpu);
177 error = vlapic_op_mem_write(vlapic, off, DWORD, wval);
182 lapic_mmio_read(void *vm, int cpu, uint64_t gpa, uint64_t *rval, int size,
187 struct vlapic *vlapic;
189 off = gpa - DEFAULT_APIC_BASE;
192 * Memory mapped local apic accesses must be 4 bytes wide and
193 * aligned on a 16-byte boundary.
195 if (size != 4 || off & 0xf)
198 vlapic = vm_lapic(vm, cpu);
199 error = vlapic_op_mem_read(vlapic, off, DWORD, rval);