When OS ejected a vcpu (like: echo 1 > /sys/bus/acpi/devices/LNXCPUXX/eject), it would call acpi EJ0 method, the firmware need to write the new cpumap, QEMU would know which vcpu need to be ejected.
TODO: for now QEMU only supported that cpu was deleted sequentially from the last one in OS, in the further OS should reject vcpu arbitrarily. Signed-off-by: Chen Fan <[email protected]> --- cpus.c | 7 ++++++ hw/acpi/piix4.c | 48 ++++++++++++++++++++++++++++++++++++++- hw/i386/acpi-dsdt-cpu-hotplug.dsl | 6 ++++- include/qom/cpu.h | 10 ++++++++ 4 files changed, 69 insertions(+), 2 deletions(-) diff --git a/cpus.c b/cpus.c index ca4c59f..5829d24 100644 --- a/cpus.c +++ b/cpus.c @@ -1117,6 +1117,13 @@ void resume_all_vcpus(void) } } +void cpu_remove(CPUState *cpu) +{ + cpu->stop = true; + cpu->exit = true; + qemu_cpu_kick(cpu); +} + static void qemu_tcg_init_vcpu(CPUState *cpu) { /* share a single thread for all cpus with TCG */ diff --git a/hw/acpi/piix4.c b/hw/acpi/piix4.c index d2cd4cd..6407f8d 100644 --- a/hw/acpi/piix4.c +++ b/hw/acpi/piix4.c @@ -62,6 +62,7 @@ struct pci_status { typedef struct CPUStatus { uint8_t sts[PIIX4_PROC_LEN]; + uint8_t old_sts[PIIX4_PROC_LEN]; } CPUStatus; typedef struct PIIX4PMState { @@ -651,6 +652,23 @@ static const MemoryRegionOps piix4_pci_ops = { }, }; +static void acpi_piix_eject_vcpu(PIIX4PMState *s, int64_t cpuid) +{ + CPUStatus *g = &s->gpe_cpu; + CPUState *cpu; + + CPU_FOREACH(cpu) { + CPUClass *cc = CPU_GET_CLASS(cpu); + int64_t id = cc->get_arch_id(cpu); + + if (cpuid == id) { + g->old_sts[cpuid / 8] &= ~(1 << (cpuid % 8)); + cpu_remove(cpu); + break; + } + } +} + static uint64_t cpu_status_read(void *opaque, hwaddr addr, unsigned int size) { PIIX4PMState *s = opaque; @@ -663,7 +681,27 @@ static uint64_t cpu_status_read(void *opaque, hwaddr addr, unsigned int size) static void cpu_status_write(void *opaque, hwaddr addr, uint64_t data, unsigned int size) { - /* TODO: implement VCPU removal on guest signal that CPU can be removed */ + PIIX4PMState *s = opaque; + CPUStatus *cpus = &s->gpe_cpu; + uint8_t val; + int i; + int64_t cpuid = -1; + + val = cpus->old_sts[addr] ^ data; + + if (val == 0) { + return; + } + + for (i = 0; i < 8; i++) { + if (val & 1 << i) { + cpuid = 8 * addr + i; + } + } + + if (cpuid != -1) { + acpi_piix_eject_vcpu(s, cpuid); + } } static const MemoryRegionOps cpu_hotplug_ops = { @@ -683,13 +721,20 @@ static void piix4_cpu_hotplug_req(PIIX4PMState *s, CPUState *cpu, ACPIGPE *gpe = &s->ar.gpe; CPUClass *k = CPU_GET_CLASS(cpu); int64_t cpu_id; + int i; assert(s != NULL); *gpe->sts = *gpe->sts | PIIX4_CPU_HOTPLUG_STATUS; cpu_id = k->get_arch_id(CPU(cpu)); + + for (i = 0; i < PIIX4_PROC_LEN; i++) { + g->old_sts[i] = g->sts[i]; + } + if (action == PLUG) { g->sts[cpu_id / 8] |= (1 << (cpu_id % 8)); + g->old_sts[cpu_id / 8] |= (1 << (cpu_id % 8)); } else { g->sts[cpu_id / 8] &= ~(1 << (cpu_id % 8)); } @@ -728,6 +773,7 @@ static void piix4_acpi_system_hot_add_init(MemoryRegion *parent, g_assert((id / 8) < PIIX4_PROC_LEN); s->gpe_cpu.sts[id / 8] |= (1 << (id % 8)); + s->gpe_cpu.old_sts[id / 8] |= (1 << (id % 8)); } memory_region_init_io(&s->io_cpu, OBJECT(s), &cpu_hotplug_ops, s, "acpi-cpu-hotplug", PIIX4_PROC_LEN); diff --git a/hw/i386/acpi-dsdt-cpu-hotplug.dsl b/hw/i386/acpi-dsdt-cpu-hotplug.dsl index c96ac42..8327b0d 100644 --- a/hw/i386/acpi-dsdt-cpu-hotplug.dsl +++ b/hw/i386/acpi-dsdt-cpu-hotplug.dsl @@ -49,7 +49,11 @@ Scope(\_SB) { } Method(CPEJ, 2, NotSerialized) { // _EJ0 method - eject callback - Sleep(200) + Store(Zero, Index(CPON, ToInteger(Arg0))) + Store(One, Local0) + ShiftLeft(Local0, Arg0, Local0) + Not(Local0, Local0) + And(PRS, Local0, PRS) } /* CPU hotplug notify method */ diff --git a/include/qom/cpu.h b/include/qom/cpu.h index 0238532..4412fb0 100644 --- a/include/qom/cpu.h +++ b/include/qom/cpu.h @@ -146,6 +146,7 @@ struct kvm_run; * @halted: Nonzero if the CPU is in suspended state. * @stop: Indicates a pending stop request. * @stopped: Indicates the CPU has been artificially stopped. + * @exit: Indicates the CPU is in impending exit state. * @tcg_exit_req: Set to force TCG to stop executing linked TBs for this * CPU and return to its top level loop. * @singlestep_enabled: Flags for single-stepping. @@ -181,6 +182,7 @@ struct CPUState { bool created; bool stop; bool stopped; + bool exit; volatile sig_atomic_t exit_request; volatile sig_atomic_t tcg_exit_req; uint32_t interrupt_request; @@ -487,6 +489,14 @@ void cpu_exit(CPUState *cpu); void cpu_resume(CPUState *cpu); /** + * qemu_remove_vcpu: + * @cpu: The vCPU to remove. + * + * Requests the CPU @cpu to be removed. + */ +void cpu_remove(CPUState *cpu); + +/** * qemu_init_vcpu: * @cpu: The vCPU to initialize. * -- 1.8.1.4
