From nobody Sat Sep 21 04:44:08 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=linux.ibm.com ARC-Seal: i=1; a=rsa-sha256; t=1705555605; cv=none; d=zohomail.com; s=zohoarc; b=DKQNvtv+a+f/Bw8nxxCB7sGax4NeQmDoPCd46WUdPR8X+TAd0GTid7UUU8U4FJWlFPSoWkxqJJL3NzgY05na/f+iUaUubCvQDewGoN2DxR5ajY/ZAwmgoz3Ld6NrXZZ/XQfFE0xu8hIGDoZN4gOvoxZuSlodRpDjWNUg4WUyHKI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1705555605; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=TDwb0SLPKpDa9AK6zKUsP6plMu6QlitlbEx0UjZkViA=; b=jZ1XPM6Qsk26GC5s9YX8kieU2A99xF/ELq9UdFTmBRyWSootiYtcjg4K3ffaHCjC+M4WFBVJoOhZi6AJhruIgeHFG9yRzIFTKgvMpV7VLt7YOYDohxSgOdBEFj2HBKWrjFoeUXczgh60L1XdHHDIDbqhho3x1peN5Pjt/7y+T3Y= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1705555605852975.3610711428231; Wed, 17 Jan 2024 21:26:45 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rQKuJ-0006ZV-Um; Thu, 18 Jan 2024 00:25:39 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rQKuA-0006Qo-L1; Thu, 18 Jan 2024 00:25:30 -0500 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rQKu7-0006cr-50; Thu, 18 Jan 2024 00:25:30 -0500 Received: from pps.filterd (m0353724.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 40I4agfY031347; Thu, 18 Jan 2024 05:25:22 GMT Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3vpuvu26t0-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 18 Jan 2024 05:25:21 +0000 Received: from m0353724.ppops.net (m0353724.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 40I5PL5P023598; Thu, 18 Jan 2024 05:25:21 GMT Received: from ppma23.wdc07v.mail.ibm.com (5d.69.3da9.ip4.static.sl-reverse.com [169.61.105.93]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3vpuvu26sm-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 18 Jan 2024 05:25:21 +0000 Received: from pps.filterd (ppma23.wdc07v.mail.ibm.com [127.0.0.1]) by ppma23.wdc07v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 40I3VTpE005842; Thu, 18 Jan 2024 05:25:20 GMT Received: from smtprelay04.fra02v.mail.ibm.com ([9.218.2.228]) by ppma23.wdc07v.mail.ibm.com (PPS) with ESMTPS id 3vm6bksd21-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 18 Jan 2024 05:25:20 +0000 Received: from smtpav03.fra02v.mail.ibm.com (smtpav03.fra02v.mail.ibm.com [10.20.54.102]) by smtprelay04.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 40I5PHca43123396 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 18 Jan 2024 05:25:17 GMT Received: from smtpav03.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 6D0C320043; Thu, 18 Jan 2024 05:25:17 +0000 (GMT) Received: from smtpav03.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id CD8A420040; Thu, 18 Jan 2024 05:25:15 +0000 (GMT) Received: from li-1901474c-32f3-11b2-a85c-fc5ff2c001f3.in.ibm.com (unknown [9.109.243.35]) by smtpav03.fra02v.mail.ibm.com (Postfix) with ESMTP; Thu, 18 Jan 2024 05:25:15 +0000 (GMT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=TDwb0SLPKpDa9AK6zKUsP6plMu6QlitlbEx0UjZkViA=; b=HZungadBwC6kK7r3xVX4r9Wv9YZb1pygsQL91OiuRJpMchrIiUYEM0wacGIAlpxSpA94 hthS1bjutmm6q9DDwp5wODNRseZOWqxuRXnCj0ODGiIX/s5yM8E+cXv8JBhg25ZxucDP N/+MeEmLeKEiBXt1OxYP9+AsBRWSv3mEwYnF6zXW2KcZe8jBG5MeiR7Ddu3YZQ23UzmL rW5eIbApf67cYiva3zjpJQA8EmPxYMWoURIGdv7D5fTPZMMbipWI5bOQKCVKzVQytriO lFXncJLZ7r/NKRQN7vWf6HRxshZuRSQRVSPq0ju4K+B5qC3ueo/Ype3fWh1+vB5ElKAB /w== From: Harsh Prateek Bora To: npiggin@gmail.com, qemu-ppc@nongnu.org Cc: qemu-devel@nongnu.org, clegoate@redhat.com, mikey@neuling.org, amachhiw@linux.vnet.ibm.com, vaibhav@linux.ibm.com, sbhat@linux.ibm.com, danielhb413@gmail.com Subject: [PATCH v3 13/15] spapr: nested: Introduce H_GUEST_RUN_VCPU hcall. Date: Thu, 18 Jan 2024 10:54:36 +0530 Message-Id: <20240118052438.1475437-14-harshpb@linux.ibm.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240118052438.1475437-1-harshpb@linux.ibm.com> References: <20240118052438.1475437-1-harshpb@linux.ibm.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 X-Proofpoint-GUID: aU6qkU5CQVbMNe1An33Qh1tSt9AVxk0o X-Proofpoint-ORIG-GUID: YKpGo-9SyYAlDof4n_uJHvthxItG9K4d X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.997,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-01-18_02,2024-01-17_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxlogscore=906 spamscore=0 suspectscore=0 malwarescore=0 phishscore=0 priorityscore=1501 impostorscore=0 adultscore=0 clxscore=1015 bulkscore=0 mlxscore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2311290000 definitions=main-2401180035 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=148.163.158.5; envelope-from=harshpb@linux.ibm.com; helo=mx0b-001b2d01.pphosted.com X-Spam_score_int: -19 X-Spam_score: -2.0 X-Spam_bar: -- X-Spam_report: (-2.0 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @ibm.com) X-ZM-MESSAGEID: 1705555606991100002 Content-Type: text/plain; charset="utf-8" The H_GUEST_RUN_VCPU hcall is used to start execution of a Guest VCPU. The Hypervisor will update the state of the Guest VCPU based on the input buffer, restore the saved Guest VCPU state, and start its execution. The Guest VCPU can stop running for numerous reasons including HCALLs, hypervisor exceptions, or an outstanding Host Partition Interrupt. The reason that the Guest VCPU stopped running is communicated through R4 and the output buffer will be filled in with any relevant state. Signed-off-by: Michael Neuling Signed-off-by: Harsh Prateek Bora --- include/hw/ppc/spapr.h | 1 + target/ppc/cpu.h | 2 + hw/ppc/ppc.c | 10 ++ hw/ppc/spapr_nested.c | 333 +++++++++++++++++++++++++++++++++++++---- 4 files changed, 316 insertions(+), 30 deletions(-) diff --git a/include/hw/ppc/spapr.h b/include/hw/ppc/spapr.h index e83a9272c4..4d3dbd4e7a 100644 --- a/include/hw/ppc/spapr.h +++ b/include/hw/ppc/spapr.h @@ -592,6 +592,7 @@ struct SpaprMachineState { #define H_GUEST_CREATE_VCPU 0x474 #define H_GUEST_GET_STATE 0x478 #define H_GUEST_SET_STATE 0x47C +#define H_GUEST_RUN_VCPU 0x480 #define H_GUEST_DELETE 0x488 =20 #define MAX_HCALL_OPCODE H_GUEST_DELETE diff --git a/target/ppc/cpu.h b/target/ppc/cpu.h index dd680178d8..e92a5307c8 100644 --- a/target/ppc/cpu.h +++ b/target/ppc/cpu.h @@ -1566,6 +1566,8 @@ uint64_t cpu_ppc_load_atbl(CPUPPCState *env); uint32_t cpu_ppc_load_atbu(CPUPPCState *env); void cpu_ppc_store_atbl(CPUPPCState *env, uint32_t value); void cpu_ppc_store_atbu(CPUPPCState *env, uint32_t value); +void cpu_ppc_increase_tb_by_offset (CPUPPCState *env, int64_t offset); +void cpu_ppc_decrease_tb_by_offset (CPUPPCState *env, int64_t offset); uint64_t cpu_ppc_load_vtb(CPUPPCState *env); void cpu_ppc_store_vtb(CPUPPCState *env, uint64_t value); bool ppc_decr_clear_on_delivery(CPUPPCState *env); diff --git a/hw/ppc/ppc.c b/hw/ppc/ppc.c index fadb8f5239..55860b9a83 100644 --- a/hw/ppc/ppc.c +++ b/hw/ppc/ppc.c @@ -633,6 +633,16 @@ void cpu_ppc_store_atbu (CPUPPCState *env, uint32_t va= lue) ((uint64_t)value << 32) | tb); } =20 +void cpu_ppc_increase_tb_by_offset (CPUPPCState *env, int64_t offset) +{ + env->tb_env->tb_offset +=3D offset; +} + +void cpu_ppc_decrease_tb_by_offset (CPUPPCState *env, int64_t offset) +{ + env->tb_env->tb_offset -=3D offset; +} + uint64_t cpu_ppc_load_vtb(CPUPPCState *env) { ppc_tb_t *tb_env =3D env->tb_env; diff --git a/hw/ppc/spapr_nested.c b/hw/ppc/spapr_nested.c index 81c1e78323..e7e5e72006 100644 --- a/hw/ppc/spapr_nested.c +++ b/hw/ppc/spapr_nested.c @@ -193,14 +193,28 @@ static void nested_save_state(struct nested_ppc_state= *save, PowerPCCPU *cpu) save->sier =3D env->spr[SPR_POWER_SIER]; save->vscr =3D ppc_get_vscr(env); save->fpscr =3D env->fpscr; + } else if (spapr_nested_api(spapr) =3D=3D NESTED_API_KVM_HV) { + save->tb_offset =3D env->tb_env->tb_offset; } +} =20 - save->tb_offset =3D env->tb_env->tb_offset; +static void nested_post_load_state(CPUPPCState *env, CPUState *cs) +{ + /* + * compute hflags and possible interrupts. + */ + hreg_compute_hflags(env); + ppc_maybe_interrupt(env); + /* + * Nested HV does not tag TLB entries between L1 and L2, so must + * flush on transition. + */ + tlb_flush(cs); + env->reserve_addr =3D -1; /* Reset the reservation */ } =20 static void nested_load_state(PowerPCCPU *cpu, struct nested_ppc_state *lo= ad) { - CPUState *cs =3D CPU(cpu); CPUPPCState *env =3D &cpu->env; SpaprMachineState *spapr =3D SPAPR_MACHINE(qdev_get_machine()); =20 @@ -279,22 +293,9 @@ static void nested_load_state(PowerPCCPU *cpu, struct = nested_ppc_state *load) env->spr[SPR_POWER_SIER] =3D load->sier; ppc_store_vscr(env, load->vscr); ppc_store_fpscr(env, load->fpscr); + } else if (spapr_nested_api(spapr) =3D=3D NESTED_API_KVM_HV) { + env->tb_env->tb_offset =3D load->tb_offset; } - - env->tb_env->tb_offset =3D load->tb_offset; - - /* - * MSR updated, compute hflags and possible interrupts. - */ - hreg_compute_hflags(env); - ppc_maybe_interrupt(env); - - /* - * Nested HV does not tag TLB entries between L1 and L2, so must - * flush on transition. - */ - tlb_flush(cs); - env->reserve_addr =3D -1; /* Reset the reservation */ } =20 /* @@ -309,6 +310,7 @@ static target_ulong h_enter_nested(PowerPCCPU *cpu, { PowerPCCPUClass *pcc =3D POWERPC_CPU_GET_CLASS(cpu); CPUPPCState *env =3D &cpu->env; + CPUState *cs =3D CPU(cpu); SpaprCpuState *spapr_cpu =3D spapr_cpu_state(cpu); struct nested_ppc_state l2_state; target_ulong hv_ptr =3D args[0]; @@ -407,6 +409,7 @@ static target_ulong h_enter_nested(PowerPCCPU *cpu, * Switch to the nested guest environment and start the "hdec" timer. */ nested_load_state(cpu, &l2_state); + nested_post_load_state(env, cs); =20 hdec =3D hv_state.hdec_expiry - now; cpu_ppc_hdecr_init(env); @@ -438,6 +441,7 @@ static target_ulong h_enter_nested(PowerPCCPU *cpu, static void spapr_exit_nested_hv(PowerPCCPU *cpu, int excp) { CPUPPCState *env =3D &cpu->env; + CPUState *cs =3D CPU(cpu); SpaprCpuState *spapr_cpu =3D spapr_cpu_state(cpu); struct nested_ppc_state l2_state; target_ulong hv_ptr =3D spapr_cpu->nested_host_state->gpr[4]; @@ -459,6 +463,7 @@ static void spapr_exit_nested_hv(PowerPCCPU *cpu, int e= xcp) */ assert(env->spr[SPR_LPIDR] !=3D 0); nested_load_state(cpu, spapr_cpu->nested_host_state); + nested_post_load_state(env, cs); env->gpr[3] =3D env->excp_vectors[excp]; /* hcall return value */ =20 cpu_ppc_hdecr_exit(env); @@ -536,19 +541,6 @@ static void spapr_exit_nested_hv(PowerPCCPU *cpu, int = excp) address_space_unmap(CPU(cpu)->as, regs, len, len, true); } =20 -void spapr_exit_nested(PowerPCCPU *cpu, int excp) -{ - SpaprMachineState *spapr =3D SPAPR_MACHINE(qdev_get_machine()); - SpaprCpuState *spapr_cpu =3D spapr_cpu_state(cpu); - - assert(spapr_cpu->in_nested); - if (spapr_nested_api(spapr) =3D=3D NESTED_API_KVM_HV) { - spapr_exit_nested_hv(cpu, excp); - } else { - g_assert_not_reached(); - } -} - SpaprMachineStateNestedGuest *spapr_get_nested_guest(SpaprMachineState *sp= apr, target_ulong guestid) { @@ -1526,6 +1518,286 @@ static target_ulong h_guest_get_state(PowerPCCPU *c= pu, return h_guest_getset_state(cpu, spapr, args, false); } =20 +static void exit_nested_store_l2(PowerPCCPU *cpu, int excp, + SpaprMachineStateNestedGuestVcpu *vcpu) +{ + CPUPPCState *env =3D &cpu->env; + SpaprCpuState *spapr_cpu =3D spapr_cpu_state(cpu); + target_ulong now, hdar, hdsisr, asdr; + + assert(sizeof(env->gpr) =3D=3D sizeof(vcpu->state.gpr)); /* sanity che= ck */ + + now =3D cpu_ppc_load_tbl(env); /* L2 timebase */ + now -=3D vcpu->tb_offset; /* L1 timebase */ + vcpu->state.dec_expiry_tb =3D now - cpu_ppc_load_decr(env); + cpu_ppc_store_decr(env, spapr_cpu->nested_host_state->dec_expiry_tb - = now); + /* backup hdar, hdsisr, asdr if reqd later below */ + hdar =3D vcpu->state.hdar; + hdsisr =3D vcpu->state.hdsisr; + asdr =3D vcpu->state.asdr; + + nested_save_state(&vcpu->state, cpu); + + if (excp =3D=3D POWERPC_EXCP_MCHECK || + excp =3D=3D POWERPC_EXCP_RESET || + excp =3D=3D POWERPC_EXCP_SYSCALL) { + vcpu->state.nip =3D env->spr[SPR_SRR0]; + vcpu->state.msr =3D env->spr[SPR_SRR1] & env->msr_mask; + } else { + vcpu->state.nip =3D env->spr[SPR_HSRR0]; + vcpu->state.msr =3D env->spr[SPR_HSRR1] & env->msr_mask; + } + + /* hdar, hdsisr, asdr should be retained unless certain exceptions */ + if ((excp !=3D POWERPC_EXCP_HDSI) && (excp !=3D POWERPC_EXCP_HISI)) { + vcpu->state.asdr =3D asdr; + } else if (excp !=3D POWERPC_EXCP_HDSI) { + vcpu->state.hdar =3D hdar; + vcpu->state.hdsisr =3D hdsisr; + } +} + +static int get_exit_ids(uint64_t srr0, uint16_t ids[16]) +{ + int nr; + + switch (srr0) { + case 0xc00: + nr =3D 10; + ids[0] =3D GSB_VCPU_GPR3; + ids[1] =3D GSB_VCPU_GPR4; + ids[2] =3D GSB_VCPU_GPR5; + ids[3] =3D GSB_VCPU_GPR6; + ids[4] =3D GSB_VCPU_GPR7; + ids[5] =3D GSB_VCPU_GPR8; + ids[6] =3D GSB_VCPU_GPR9; + ids[7] =3D GSB_VCPU_GPR10; + ids[8] =3D GSB_VCPU_GPR11; + ids[9] =3D GSB_VCPU_GPR12; + break; + case 0xe00: + nr =3D 5; + ids[0] =3D GSB_VCPU_SPR_HDAR; + ids[1] =3D GSB_VCPU_SPR_HDSISR; + ids[2] =3D GSB_VCPU_SPR_ASDR; + ids[3] =3D GSB_VCPU_SPR_NIA; + ids[4] =3D GSB_VCPU_SPR_MSR; + break; + case 0xe20: + nr =3D 4; + ids[0] =3D GSB_VCPU_SPR_HDAR; + ids[1] =3D GSB_VCPU_SPR_ASDR; + ids[2] =3D GSB_VCPU_SPR_NIA; + ids[3] =3D GSB_VCPU_SPR_MSR; + break; + case 0xe40: + nr =3D 3; + ids[0] =3D GSB_VCPU_SPR_HEIR; + ids[1] =3D GSB_VCPU_SPR_NIA; + ids[2] =3D GSB_VCPU_SPR_MSR; + break; + case 0xf80: + nr =3D 3; + ids[0] =3D GSB_VCPU_SPR_HFSCR; + ids[1] =3D GSB_VCPU_SPR_NIA; + ids[2] =3D GSB_VCPU_SPR_MSR; + break; + default: + nr =3D 0; + break; + } + + return nr; +} + +static void exit_process_output_buffer(PowerPCCPU *cpu, + SpaprMachineStateNestedGuest *guest, + target_ulong vcpuid, + target_ulong *r3) +{ + SpaprMachineStateNestedGuestVcpu *vcpu =3D &guest->vcpu[vcpuid]; + struct guest_state_request gsr; + struct guest_state_buffer *gsb; + struct guest_state_element *element; + struct guest_state_element_type *type; + int exit_id_count =3D 0; + uint16_t exit_cause_ids[16]; + hwaddr len; + + len =3D vcpu->runbufout.size; + gsb =3D address_space_map(CPU(cpu)->as, vcpu->runbufout.addr, &len, tr= ue, + MEMTXATTRS_UNSPECIFIED); + if (!gsb || len !=3D vcpu->runbufout.size) { + address_space_unmap(CPU(cpu)->as, gsb, len, true, len); + *r3 =3D H_P2; + return; + } + + exit_id_count =3D get_exit_ids(*r3, exit_cause_ids); + + /* Create a buffer of elements to send back */ + gsb->num_elements =3D cpu_to_be32(exit_id_count); + element =3D gsb->elements; + for (int i =3D 0; i < exit_id_count; i++) { + type =3D guest_state_element_type_find(exit_cause_ids[i]); + assert(type); + element->id =3D cpu_to_be16(exit_cause_ids[i]); + element->size =3D cpu_to_be16(type->size); + element =3D guest_state_element_next(element, NULL, NULL); + } + gsr.gsb =3D gsb; + gsr.len =3D VCPU_OUT_BUF_MIN_SZ; + gsr.flags =3D 0; /* get + never guest wide */ + getset_state(guest, vcpuid, &gsr); + + address_space_unmap(CPU(cpu)->as, gsb, len, true, len); + return; +} + +static +void spapr_exit_nested_papr(SpaprMachineState *spapr, PowerPCCPU *cpu, int= excp) +{ + CPUPPCState *env =3D &cpu->env; + CPUState *cs =3D CPU(cpu); + SpaprCpuState *spapr_cpu =3D spapr_cpu_state(cpu); + target_ulong r3_return =3D env->excp_vectors[excp]; /* hcall return va= lue */ + target_ulong lpid =3D 0, vcpuid =3D 0; + struct SpaprMachineStateNestedGuestVcpu *vcpu =3D NULL; + struct SpaprMachineStateNestedGuest *guest =3D NULL; + + lpid =3D spapr_cpu->nested_host_state->gpr[5]; + vcpuid =3D spapr_cpu->nested_host_state->gpr[6]; + guest =3D spapr_get_nested_guest(spapr, lpid); + assert(guest); + spapr_nested_vcpu_check(guest, vcpuid, false); + vcpu =3D &guest->vcpu[vcpuid]; + + exit_nested_store_l2(cpu, excp, vcpu); + /* do the output buffer for run_vcpu*/ + exit_process_output_buffer(cpu, guest, vcpuid, &r3_return); + + assert(env->spr[SPR_LPIDR] !=3D 0); + nested_load_state(cpu, spapr_cpu->nested_host_state); + cpu_ppc_decrease_tb_by_offset(env, vcpu->tb_offset); + env->gpr[3] =3D H_SUCCESS; + env->gpr[4] =3D r3_return; + nested_post_load_state(env, cs); + cpu_ppc_hdecr_exit(env); + + spapr_cpu->in_nested =3D false; + g_free(spapr_cpu->nested_host_state); + spapr_cpu->nested_host_state =3D NULL; +} + +void spapr_exit_nested(PowerPCCPU *cpu, int excp) +{ + SpaprMachineState *spapr =3D SPAPR_MACHINE(qdev_get_machine()); + SpaprCpuState *spapr_cpu =3D spapr_cpu_state(cpu); + + assert(spapr_cpu->in_nested); + if (spapr_nested_api(spapr) =3D=3D NESTED_API_KVM_HV) { + spapr_exit_nested_hv(cpu, excp); + } else if (spapr_nested_api(spapr) =3D=3D NESTED_API_PAPR) { + spapr_exit_nested_papr(spapr, cpu, excp); + } else { + g_assert_not_reached(); + } +} + +static void nested_papr_load_l2(PowerPCCPU *cpu, + CPUPPCState *env, + SpaprMachineStateNestedGuestVcpu *vcpu, + target_ulong now) +{ + PowerPCCPUClass *pcc =3D POWERPC_CPU_GET_CLASS(cpu); + target_ulong lpcr, lpcr_mask, hdec; + lpcr_mask =3D LPCR_DPFD | LPCR_ILE | LPCR_AIL | LPCR_LD | LPCR_MER; + + assert(vcpu); + assert(sizeof(env->gpr) =3D=3D sizeof(vcpu->state.gpr)); + nested_load_state(cpu, &vcpu->state); + lpcr =3D (env->spr[SPR_LPCR] & ~lpcr_mask) | + (vcpu->state.lpcr & lpcr_mask); + lpcr |=3D LPCR_HR | LPCR_UPRT | LPCR_GTSE | LPCR_HVICE | LPCR_HDICE; + lpcr &=3D ~LPCR_LPES0; + env->spr[SPR_LPCR] =3D lpcr & pcc->lpcr_mask; + + hdec =3D vcpu->hdecr_expiry_tb - now; + cpu_ppc_store_decr(env, vcpu->state.dec_expiry_tb - now); + cpu_ppc_hdecr_init(env); + cpu_ppc_store_hdecr(env, hdec); + + cpu_ppc_increase_tb_by_offset(env, vcpu->tb_offset); +} + +static void nested_papr_run_vcpu(PowerPCCPU *cpu, + uint64_t lpid, + SpaprMachineStateNestedGuestVcpu *vcpu) +{ + SpaprMachineState *spapr =3D SPAPR_MACHINE(qdev_get_machine()); + CPUPPCState *env =3D &cpu->env; + CPUState *cs =3D CPU(cpu); + SpaprCpuState *spapr_cpu =3D spapr_cpu_state(cpu); + target_ulong now =3D cpu_ppc_load_tbl(env); + + assert(env->spr[SPR_LPIDR] =3D=3D 0); + assert(spapr->nested.api); /* ensure API version is initialized */ + spapr_cpu->nested_host_state =3D g_try_new(struct nested_ppc_state, 1); + assert(spapr_cpu->nested_host_state); + nested_save_state(spapr_cpu->nested_host_state, cpu); + spapr_cpu->nested_host_state->dec_expiry_tb =3D now - cpu_ppc_load_dec= r(env); + nested_papr_load_l2(cpu, env, vcpu, now); + env->spr[SPR_LPIDR] =3D lpid; /* post load l2 */ + + spapr_cpu->in_nested =3D true; + nested_post_load_state(env, cs); +} + +static target_ulong h_guest_run_vcpu(PowerPCCPU *cpu, + SpaprMachineState *spapr, + target_ulong opcode, + target_ulong *args) +{ + CPUPPCState *env =3D &cpu->env; + target_ulong flags =3D args[0]; + target_ulong lpid =3D args[1]; + target_ulong vcpuid =3D args[2]; + struct SpaprMachineStateNestedGuestVcpu *vcpu; + struct guest_state_request gsr; + SpaprMachineStateNestedGuest *guest; + target_ulong rc; + + if (flags) /* don't handle any flags for now */ + return H_PARAMETER; + + guest =3D spapr_get_nested_guest(spapr, lpid); + if (!guest) { + return H_P2; + } + if (!spapr_nested_vcpu_check(guest, vcpuid, true)) { + return H_P3; + } + + if (guest->parttbl[0] =3D=3D 0) { + /* At least need a partition scoped radix tree */ + return H_NOT_AVAILABLE; + } + + vcpu =3D &guest->vcpu[vcpuid]; + + /* Read run_vcpu input buffer to update state */ + gsr.buf =3D vcpu->runbufin.addr; + gsr.len =3D vcpu->runbufin.size; + gsr.flags =3D GUEST_STATE_REQUEST_SET; /* Thread wide + writing */ + rc =3D map_and_getset_state(cpu, guest, vcpuid, &gsr); + if (rc =3D=3D H_SUCCESS) { + nested_papr_run_vcpu(cpu, lpid, vcpu); + } else { + env->gpr[3] =3D rc; + } + return env->gpr[3]; +} + void spapr_register_nested_hv(void) { spapr_register_hypercall(KVMPPC_H_SET_PARTITION_TABLE, h_set_ptbl); @@ -1543,6 +1815,7 @@ void spapr_register_nested_papr(void) spapr_register_hypercall(H_GUEST_CREATE_VCPU , h_guest_create_vcpu= ); spapr_register_hypercall(H_GUEST_SET_STATE , h_guest_set_state); spapr_register_hypercall(H_GUEST_GET_STATE , h_guest_get_state); + spapr_register_hypercall(H_GUEST_RUN_VCPU , h_guest_run_vcpu); } =20 #else --=20 2.39.3