From nobody Sat Jul 12 15:15:33 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1486014112259398.1904128039023; Wed, 1 Feb 2017 21:41:52 -0800 (PST) Received: from localhost ([::1]:54342 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cZA9W-0007Kn-6F for importer@patchew.org; Thu, 02 Feb 2017 00:41:50 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51261) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cZ9jj-0007Fa-KX for qemu-devel@nongnu.org; Thu, 02 Feb 2017 00:15:16 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cZ9jg-0003aD-3E for qemu-devel@nongnu.org; Thu, 02 Feb 2017 00:15:11 -0500 Received: from ozlabs.org ([2401:3900:2:1::2]:37117) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cZ9jf-0003VN-7I; Thu, 02 Feb 2017 00:15:07 -0500 Received: by ozlabs.org (Postfix, from userid 1007) id 3vDSqN2X3Rz9s7d; Thu, 2 Feb 2017 16:14:55 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gibson.dropbear.id.au; s=201602; t=1486012496; bh=uoCwJ+WVAqOEBqpTHDZYdXHN1UKOBbf0ldsnVKanNjM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=J5BR2/PjzJOddPnon5kgl5hZSLOQmQcRru+mrxYi29CO69+5n0tuj2cBmogGHKSeP 3d4/D7+/wzhejcLthvu9nBOzIO9v50FiqS7TMHZJij6Hq8xk/E8KcVFl8bjNYBMILZ AQe2U0vaqX8VF0kW35iGhmnwPV2KF4FRmuC4MFe8= From: David Gibson To: peter.maydell@linaro.org Date: Thu, 2 Feb 2017 16:13:15 +1100 Message-Id: <20170202051445.5735-18-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170202051445.5735-1-david@gibson.dropbear.id.au> References: <20170202051445.5735-1-david@gibson.dropbear.id.au> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2401:3900:2:1::2 Subject: [Qemu-devel] [PULL 017/107] ppc: Clean up and QOMify hypercall emulation X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: lvivier@redhat.com, thuth@redhat.com, qemu-devel@nongnu.org, mdroth@linux.vnet.ibm.com, agraf@suse.de, aik@ozlabs.ru, qemu-ppc@nongnu.org, David Gibson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The pseries machine type is a bit unusual in that it runs a paravirtualized guest. The guest expects to interact with a hypervisor, and qemu emulates the functions of that hypervisor directly, rather than executing hypervisor code within the emulated system. To implement this in TCG, we need to intercept hypercall instructions and direct them to the machine's hypercall handlers, rather than attempting to perform a privilege change within TCG. This is controlled by a global hook - cpu_ppc_hypercall. This cleanup makes the handling a little cleaner and more extensible than a single global variable. Instead, each CPU to have hypercalls intercepted has a pointer set to a QOM object implementing a new virtual hypervisor interface. A method in that interface is called by TCG when it sees a hypercall instruction. It's possible we may want to add other methods in future. Signed-off-by: David Gibson Reviewed-by: Alexey Kardashevskiy --- hw/ppc/spapr.c | 8 +++++--- hw/ppc/spapr_cpu_core.c | 1 + target/ppc/cpu.h | 26 ++++++++++++++++++++++++-- target/ppc/excp_helper.c | 11 ++++------- target/ppc/translate_init.c | 12 ++++++++++++ 5 files changed, 46 insertions(+), 12 deletions(-) diff --git a/hw/ppc/spapr.c b/hw/ppc/spapr.c index ca78e31..a4774fb 100644 --- a/hw/ppc/spapr.c +++ b/hw/ppc/spapr.c @@ -1005,7 +1005,8 @@ static uint64_t translate_kernel_address(void *opaque= , uint64_t addr) return (addr & 0x0fffffff) + KERNEL_LOAD_ADDR; } =20 -static void emulate_spapr_hypercall(PowerPCCPU *cpu) +static void emulate_spapr_hypercall(PPCVirtualHypervisor *vhyp, + PowerPCCPU *cpu) { CPUPPCState *env =3D &cpu->env; =20 @@ -1843,8 +1844,6 @@ static void ppc_spapr_init(MachineState *machine) =20 QLIST_INIT(&spapr->phbs); =20 - cpu_ppc_hypercall =3D emulate_spapr_hypercall; - /* Allocate RMA if necessary */ rma_alloc_size =3D kvmppc_alloc_rma(&rma); =20 @@ -2680,6 +2679,7 @@ static void spapr_machine_class_init(ObjectClass *oc,= void *data) FWPathProviderClass *fwc =3D FW_PATH_PROVIDER_CLASS(oc); NMIClass *nc =3D NMI_CLASS(oc); HotplugHandlerClass *hc =3D HOTPLUG_HANDLER_CLASS(oc); + PPCVirtualHypervisorClass *vhc =3D PPC_VIRTUAL_HYPERVISOR_CLASS(oc); =20 mc->desc =3D "pSeries Logical Partition (PAPR compliant)"; =20 @@ -2711,6 +2711,7 @@ static void spapr_machine_class_init(ObjectClass *oc,= void *data) fwc->get_dev_path =3D spapr_get_fw_dev_path; nc->nmi_monitor_handler =3D spapr_nmi; smc->phb_placement =3D spapr_phb_placement; + vhc->hypercall =3D emulate_spapr_hypercall; } =20 static const TypeInfo spapr_machine_info =3D { @@ -2726,6 +2727,7 @@ static const TypeInfo spapr_machine_info =3D { { TYPE_FW_PATH_PROVIDER }, { TYPE_NMI }, { TYPE_HOTPLUG_HANDLER }, + { TYPE_PPC_VIRTUAL_HYPERVISOR }, { } }, }; diff --git a/hw/ppc/spapr_cpu_core.c b/hw/ppc/spapr_cpu_core.c index f532689..9dddaeb 100644 --- a/hw/ppc/spapr_cpu_core.c +++ b/hw/ppc/spapr_cpu_core.c @@ -57,6 +57,7 @@ static void spapr_cpu_init(sPAPRMachineState *spapr, Powe= rPCCPU *cpu, cpu_ppc_tb_init(env, SPAPR_TIMEBASE_FREQ); =20 /* Enable PAPR mode in TCG or KVM */ + cpu_ppc_set_vhyp(cpu, PPC_VIRTUAL_HYPERVISOR(spapr)); cpu_ppc_set_papr(cpu); =20 if (cpu->max_compat) { diff --git a/target/ppc/cpu.h b/target/ppc/cpu.h index 7a6ee3f..4fb4c20 100644 --- a/target/ppc/cpu.h +++ b/target/ppc/cpu.h @@ -1150,6 +1150,9 @@ do { \ env->wdt_period[3] =3D (d_); \ } while (0) =20 +typedef struct PPCVirtualHypervisor PPCVirtualHypervisor; +typedef struct PPCVirtualHypervisorClass PPCVirtualHypervisorClass; + /** * PowerPCCPU: * @env: #CPUPPCState @@ -1168,6 +1171,7 @@ struct PowerPCCPU { int cpu_dt_id; uint32_t max_compat; uint32_t cpu_version; + PPCVirtualHypervisor *vhyp; =20 /* Fields related to migration compatibility hacks */ bool pre_2_8_migration; @@ -1189,6 +1193,25 @@ static inline PowerPCCPU *ppc_env_get_cpu(CPUPPCStat= e *env) PowerPCCPUClass *ppc_cpu_class_by_pvr(uint32_t pvr); PowerPCCPUClass *ppc_cpu_class_by_pvr_mask(uint32_t pvr); =20 +struct PPCVirtualHypervisor { + Object parent; +}; + +struct PPCVirtualHypervisorClass { + InterfaceClass parent; + void (*hypercall)(PPCVirtualHypervisor *vhyp, PowerPCCPU *cpu); +}; + +#define TYPE_PPC_VIRTUAL_HYPERVISOR "ppc-virtual-hypervisor" +#define PPC_VIRTUAL_HYPERVISOR(obj) \ + OBJECT_CHECK(PPCVirtualHypervisor, (obj), TYPE_PPC_VIRTUAL_HYPERVISOR) +#define PPC_VIRTUAL_HYPERVISOR_CLASS(klass) \ + OBJECT_CLASS_CHECK(PPCVirtualHypervisorClass, (klass), \ + TYPE_PPC_VIRTUAL_HYPERVISOR) +#define PPC_VIRTUAL_HYPERVISOR_GET_CLASS(obj) \ + OBJECT_GET_CLASS(PPCVirtualHypervisorClass, (obj), \ + TYPE_PPC_VIRTUAL_HYPERVISOR) + void ppc_cpu_do_interrupt(CPUState *cpu); bool ppc_cpu_exec_interrupt(CPUState *cpu, int int_req); void ppc_cpu_dump_state(CPUState *cpu, FILE *f, fprintf_function cpu_fprin= tf, @@ -1261,6 +1284,7 @@ void store_booke_tcr (CPUPPCState *env, target_ulong = val); void store_booke_tsr (CPUPPCState *env, target_ulong val); void ppc_tlb_invalidate_all (CPUPPCState *env); void ppc_tlb_invalidate_one (CPUPPCState *env, target_ulong addr); +void cpu_ppc_set_vhyp(PowerPCCPU *cpu, PPCVirtualHypervisor *vhyp); void cpu_ppc_set_papr(PowerPCCPU *cpu); #endif #endif @@ -2435,8 +2459,6 @@ static inline bool lsw_reg_in_range(int start, int nr= egs, int rx) (start + nregs > 32 && (rx >=3D start || rx < start + nregs - 3= 2)); } =20 -extern void (*cpu_ppc_hypercall)(PowerPCCPU *); - void dump_mmu(FILE *f, fprintf_function cpu_fprintf, CPUPPCState *env); =20 /** diff --git a/target/ppc/excp_helper.c b/target/ppc/excp_helper.c index 93369d4..f4ee7aa 100644 --- a/target/ppc/excp_helper.c +++ b/target/ppc/excp_helper.c @@ -35,11 +35,6 @@ #endif =20 /*************************************************************************= ****/ -/* PowerPC Hypercall emulation */ - -void (*cpu_ppc_hypercall)(PowerPCCPU *); - -/*************************************************************************= ****/ /* Exception processing */ #if defined(CONFIG_USER_ONLY) void ppc_cpu_do_interrupt(CPUState *cs) @@ -318,8 +313,10 @@ static inline void powerpc_excp(PowerPCCPU *cpu, int e= xcp_model, int excp) env->nip +=3D 4; =20 /* "PAPR mode" built-in hypercall emulation */ - if ((lev =3D=3D 1) && cpu_ppc_hypercall) { - cpu_ppc_hypercall(cpu); + if ((lev =3D=3D 1) && cpu->vhyp) { + PPCVirtualHypervisorClass *vhc =3D + PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp); + vhc->hypercall(cpu->vhyp, cpu); return; } if (lev =3D=3D 1) { diff --git a/target/ppc/translate_init.c b/target/ppc/translate_init.c index 94dfcd7..2e921ca 100644 --- a/target/ppc/translate_init.c +++ b/target/ppc/translate_init.c @@ -8857,6 +8857,11 @@ POWERPC_FAMILY(POWER9)(ObjectClass *oc, void *data) =20 #if !defined(CONFIG_USER_ONLY) =20 +void cpu_ppc_set_vhyp(PowerPCCPU *cpu, PPCVirtualHypervisor *vhyp) +{ + cpu->vhyp =3D vhyp; +} + void cpu_ppc_set_papr(PowerPCCPU *cpu) { CPUPPCState *env =3D &cpu->env; @@ -10591,9 +10596,16 @@ static const TypeInfo ppc_cpu_type_info =3D { .class_init =3D ppc_cpu_class_init, }; =20 +static const TypeInfo ppc_vhyp_type_info =3D { + .name =3D TYPE_PPC_VIRTUAL_HYPERVISOR, + .parent =3D TYPE_INTERFACE, + .class_size =3D sizeof(PPCVirtualHypervisorClass), +}; + static void ppc_cpu_register_types(void) { type_register_static(&ppc_cpu_type_info); + type_register_static(&ppc_vhyp_type_info); } =20 type_init(ppc_cpu_register_types) --=20 2.9.3