apic, i386/tcg: add x2apic transitions

This commit adds support for x2APIC transitions when writing to
MSR_IA32_APICBASE register and finally adds CPUID_EXT_X2APIC to
TCG_EXT_FEATURES.

The set_base in APICCommonClass now returns an integer to indicate error in
execution. apic_set_base return -1 on invalid APIC state transition,
accelerator can use this to raise appropriate exception.

Signed-off-by: Bui Quang Minh <minhquangbui99@gmail.com>
Message-Id: <20240111154404.5333-4-minhquangbui99@gmail.com>
Acked-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
This commit is contained in:
Bui Quang Minh 2024-01-11 22:44:00 +07:00 committed by Michael S. Tsirkin
parent b5ee0468e9
commit 774204cf98
10 changed files with 96 additions and 19 deletions

View File

@ -95,9 +95,10 @@ void kvm_get_apic_state(DeviceState *dev, struct kvm_lapic_state *kapic)
apic_next_timer(s, s->initial_count_load_time); apic_next_timer(s, s->initial_count_load_time);
} }
static void kvm_apic_set_base(APICCommonState *s, uint64_t val) static int kvm_apic_set_base(APICCommonState *s, uint64_t val)
{ {
s->apicbase = val; s->apicbase = val;
return 0;
} }
static void kvm_apic_set_tpr(APICCommonState *s, uint8_t val) static void kvm_apic_set_tpr(APICCommonState *s, uint8_t val)

View File

@ -49,8 +49,9 @@ static void xen_apic_realize(DeviceState *dev, Error **errp)
msi_nonbroken = true; msi_nonbroken = true;
} }
static void xen_apic_set_base(APICCommonState *s, uint64_t val) static int xen_apic_set_base(APICCommonState *s, uint64_t val)
{ {
return 0;
} }
static void xen_apic_set_tpr(APICCommonState *s, uint8_t val) static void xen_apic_set_tpr(APICCommonState *s, uint8_t val)

View File

@ -308,8 +308,49 @@ bool is_x2apic_mode(DeviceState *dev)
return s->apicbase & MSR_IA32_APICBASE_EXTD; return s->apicbase & MSR_IA32_APICBASE_EXTD;
} }
static void apic_set_base(APICCommonState *s, uint64_t val) static int apic_set_base_check(APICCommonState *s, uint64_t val)
{ {
/* Enable x2apic when x2apic is not supported by CPU */
if (!cpu_has_x2apic_feature(&s->cpu->env) &&
val & MSR_IA32_APICBASE_EXTD) {
return -1;
}
/*
* Transition into invalid state
* (s->apicbase & MSR_IA32_APICBASE_ENABLE == 0) &&
* (s->apicbase & MSR_IA32_APICBASE_EXTD) == 1
*/
if (!(val & MSR_IA32_APICBASE_ENABLE) &&
(val & MSR_IA32_APICBASE_EXTD)) {
return -1;
}
/* Invalid transition from disabled mode to x2APIC */
if (!(s->apicbase & MSR_IA32_APICBASE_ENABLE) &&
!(s->apicbase & MSR_IA32_APICBASE_EXTD) &&
(val & MSR_IA32_APICBASE_ENABLE) &&
(val & MSR_IA32_APICBASE_EXTD)) {
return -1;
}
/* Invalid transition from x2APIC to xAPIC */
if ((s->apicbase & MSR_IA32_APICBASE_ENABLE) &&
(s->apicbase & MSR_IA32_APICBASE_EXTD) &&
(val & MSR_IA32_APICBASE_ENABLE) &&
!(val & MSR_IA32_APICBASE_EXTD)) {
return -1;
}
return 0;
}
static int apic_set_base(APICCommonState *s, uint64_t val)
{
if (apic_set_base_check(s, val) < 0) {
return -1;
}
s->apicbase = (val & 0xfffff000) | s->apicbase = (val & 0xfffff000) |
(s->apicbase & (MSR_IA32_APICBASE_BSP | MSR_IA32_APICBASE_ENABLE)); (s->apicbase & (MSR_IA32_APICBASE_BSP | MSR_IA32_APICBASE_ENABLE));
/* if disabled, cannot be enabled again */ /* if disabled, cannot be enabled again */
@ -318,6 +359,25 @@ static void apic_set_base(APICCommonState *s, uint64_t val)
cpu_clear_apic_feature(&s->cpu->env); cpu_clear_apic_feature(&s->cpu->env);
s->spurious_vec &= ~APIC_SV_ENABLE; s->spurious_vec &= ~APIC_SV_ENABLE;
} }
/* Transition from disabled mode to xAPIC */
if (!(s->apicbase & MSR_IA32_APICBASE_ENABLE) &&
(val & MSR_IA32_APICBASE_ENABLE)) {
s->apicbase |= MSR_IA32_APICBASE_ENABLE;
cpu_set_apic_feature(&s->cpu->env);
}
/* Transition from xAPIC to x2APIC */
if (cpu_has_x2apic_feature(&s->cpu->env) &&
!(s->apicbase & MSR_IA32_APICBASE_EXTD) &&
(val & MSR_IA32_APICBASE_EXTD)) {
s->apicbase |= MSR_IA32_APICBASE_EXTD;
s->log_dest = ((s->initial_apic_id & 0xffff0) << 16) |
(1 << (s->initial_apic_id & 0xf));
}
return 0;
} }
static void apic_set_tpr(APICCommonState *s, uint8_t val) static void apic_set_tpr(APICCommonState *s, uint8_t val)

View File

@ -35,20 +35,19 @@
bool apic_report_tpr_access; bool apic_report_tpr_access;
void cpu_set_apic_base(DeviceState *dev, uint64_t val) int cpu_set_apic_base(DeviceState *dev, uint64_t val)
{ {
trace_cpu_set_apic_base(val); trace_cpu_set_apic_base(val);
if (dev) { if (dev) {
APICCommonState *s = APIC_COMMON(dev); APICCommonState *s = APIC_COMMON(dev);
APICCommonClass *info = APIC_COMMON_GET_CLASS(s); APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
/* switching to x2APIC, reset possibly modified xAPIC ID */ /* Reset possibly modified xAPIC ID */
if (!(s->apicbase & MSR_IA32_APICBASE_EXTD) && s->id = s->initial_apic_id;
(val & MSR_IA32_APICBASE_EXTD)) { return info->set_base(s, val);
s->id = s->initial_apic_id;
}
info->set_base(s, val);
} }
return 0;
} }
uint64_t cpu_get_apic_base(DeviceState *dev) uint64_t cpu_get_apic_base(DeviceState *dev)

View File

@ -8,7 +8,7 @@ int apic_accept_pic_intr(DeviceState *s);
void apic_deliver_pic_intr(DeviceState *s, int level); void apic_deliver_pic_intr(DeviceState *s, int level);
void apic_deliver_nmi(DeviceState *d); void apic_deliver_nmi(DeviceState *d);
int apic_get_interrupt(DeviceState *s); int apic_get_interrupt(DeviceState *s);
void cpu_set_apic_base(DeviceState *s, uint64_t val); int cpu_set_apic_base(DeviceState *s, uint64_t val);
uint64_t cpu_get_apic_base(DeviceState *s); uint64_t cpu_get_apic_base(DeviceState *s);
void cpu_set_apic_tpr(DeviceState *s, uint8_t val); void cpu_set_apic_tpr(DeviceState *s, uint8_t val);
uint8_t cpu_get_apic_tpr(DeviceState *s); uint8_t cpu_get_apic_tpr(DeviceState *s);

View File

@ -137,7 +137,7 @@ struct APICCommonClass {
DeviceRealize realize; DeviceRealize realize;
DeviceUnrealize unrealize; DeviceUnrealize unrealize;
void (*set_base)(APICCommonState *s, uint64_t val); int (*set_base)(APICCommonState *s, uint64_t val);
void (*set_tpr)(APICCommonState *s, uint8_t val); void (*set_tpr)(APICCommonState *s, uint8_t val);
uint8_t (*get_tpr)(APICCommonState *s); uint8_t (*get_tpr)(APICCommonState *s);
void (*enable_tpr_reporting)(APICCommonState *s, bool enable); void (*enable_tpr_reporting)(APICCommonState *s, bool enable);

View File

@ -631,8 +631,8 @@ void x86_cpu_vendor_words2str(char *dst, uint32_t vendor1,
* in CPL=3; remove them if they are ever implemented for system emulation. * in CPL=3; remove them if they are ever implemented for system emulation.
*/ */
#if defined CONFIG_USER_ONLY #if defined CONFIG_USER_ONLY
#define CPUID_EXT_KERNEL_FEATURES (CPUID_EXT_PCID | CPUID_EXT_TSC_DEADLINE_TIMER | \ #define CPUID_EXT_KERNEL_FEATURES \
CPUID_EXT_X2APIC) (CPUID_EXT_PCID | CPUID_EXT_TSC_DEADLINE_TIMER)
#else #else
#define CPUID_EXT_KERNEL_FEATURES 0 #define CPUID_EXT_KERNEL_FEATURES 0
#endif #endif
@ -642,12 +642,13 @@ void x86_cpu_vendor_words2str(char *dst, uint32_t vendor1,
CPUID_EXT_XSAVE | /* CPUID_EXT_OSXSAVE is dynamic */ \ CPUID_EXT_XSAVE | /* CPUID_EXT_OSXSAVE is dynamic */ \
CPUID_EXT_MOVBE | CPUID_EXT_AES | CPUID_EXT_HYPERVISOR | \ CPUID_EXT_MOVBE | CPUID_EXT_AES | CPUID_EXT_HYPERVISOR | \
CPUID_EXT_RDRAND | CPUID_EXT_AVX | CPUID_EXT_F16C | \ CPUID_EXT_RDRAND | CPUID_EXT_AVX | CPUID_EXT_F16C | \
CPUID_EXT_FMA | CPUID_EXT_KERNEL_FEATURES) CPUID_EXT_FMA | CPUID_EXT_X2APIC | CPUID_EXT_KERNEL_FEATURES)
/* missing: /* missing:
CPUID_EXT_DTES64, CPUID_EXT_DSCPL, CPUID_EXT_VMX, CPUID_EXT_SMX, CPUID_EXT_DTES64, CPUID_EXT_DSCPL, CPUID_EXT_VMX, CPUID_EXT_SMX,
CPUID_EXT_EST, CPUID_EXT_TM2, CPUID_EXT_CID, CPUID_EXT_EST, CPUID_EXT_TM2, CPUID_EXT_CID,
CPUID_EXT_XTPR, CPUID_EXT_PDCM, CPUID_EXT_PCID, CPUID_EXT_DCA, CPUID_EXT_XTPR, CPUID_EXT_PDCM, CPUID_EXT_PCID, CPUID_EXT_DCA,
CPUID_EXT_X2APIC, CPUID_EXT_TSC_DEADLINE_TIMER */ CPUID_EXT_TSC_DEADLINE_TIMER
*/
#ifdef TARGET_X86_64 #ifdef TARGET_X86_64
#define TCG_EXT2_X86_64_FEATURES CPUID_EXT2_LM #define TCG_EXT2_X86_64_FEATURES CPUID_EXT2_LM

View File

@ -379,6 +379,10 @@ typedef enum X86Seg {
#define MSR_IA32_APICBASE_ENABLE (1<<11) #define MSR_IA32_APICBASE_ENABLE (1<<11)
#define MSR_IA32_APICBASE_EXTD (1 << 10) #define MSR_IA32_APICBASE_EXTD (1 << 10)
#define MSR_IA32_APICBASE_BASE (0xfffffU<<12) #define MSR_IA32_APICBASE_BASE (0xfffffU<<12)
#define MSR_IA32_APICBASE_RESERVED \
(~(uint64_t)(MSR_IA32_APICBASE_BSP | MSR_IA32_APICBASE_ENABLE \
| MSR_IA32_APICBASE_EXTD | MSR_IA32_APICBASE_BASE))
#define MSR_IA32_FEATURE_CONTROL 0x0000003a #define MSR_IA32_FEATURE_CONTROL 0x0000003a
#define MSR_TSC_ADJUST 0x0000003b #define MSR_TSC_ADJUST 0x0000003b
#define MSR_IA32_SPEC_CTRL 0x48 #define MSR_IA32_SPEC_CTRL 0x48

View File

@ -158,9 +158,19 @@ void helper_wrmsr(CPUX86State *env)
case MSR_IA32_SYSENTER_EIP: case MSR_IA32_SYSENTER_EIP:
env->sysenter_eip = val; env->sysenter_eip = val;
break; break;
case MSR_IA32_APICBASE: case MSR_IA32_APICBASE: {
cpu_set_apic_base(env_archcpu(env)->apic_state, val); int ret;
if (val & MSR_IA32_APICBASE_RESERVED) {
goto error;
}
ret = cpu_set_apic_base(env_archcpu(env)->apic_state, val);
if (ret < 0) {
goto error;
}
break; break;
}
case MSR_EFER: case MSR_EFER:
{ {
uint64_t update_mask; uint64_t update_mask;

View File

@ -90,9 +90,10 @@ static void whpx_get_apic_state(APICCommonState *s,
apic_next_timer(s, s->initial_count_load_time); apic_next_timer(s, s->initial_count_load_time);
} }
static void whpx_apic_set_base(APICCommonState *s, uint64_t val) static int whpx_apic_set_base(APICCommonState *s, uint64_t val)
{ {
s->apicbase = val; s->apicbase = val;
return 0;
} }
static void whpx_put_apic_base(CPUState *cpu, uint64_t val) static void whpx_put_apic_base(CPUState *cpu, uint64_t val)