Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp43038pxu; Tue, 24 Nov 2020 18:02:32 -0800 (PST) X-Google-Smtp-Source: ABdhPJxryKn5vff/3nKAzISI9lQCpegRK/W0ZA4c+NjogYG2IqRS4qVMxOYVaT6BLjnxNENiAb1Z X-Received: by 2002:a17:906:9252:: with SMTP id c18mr1132289ejx.489.1606269752648; Tue, 24 Nov 2020 18:02:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606269752; cv=none; d=google.com; s=arc-20160816; b=NL7Ot+DiMEsM+V9XwgSPZfhtxxREobMuaHAIxfLs5s4B9+iDle8T9TZJ4oF3Wk5odp AZYXsCvB9+4ObFHmkRETtRGp2+r3I72rPaSeoby9v1/mxU7kPnhXOwoVPqcaN3UioBNH EhSJjD4Ad0t4NNg9Gd8vAdp0H1AqmJHw/0cWjbeOlixLQ25mj+QiMlhVCzG15qTd0Ka+ DRGm2IJtVCSAO9IbnXLctxLPwtkCP+vx+zrKjGVvCCbzH+oeSp57E59RJ3CytPaLStyq EJ2mb/6+EgXZnjIwBRI+L/bOs3iGeaUMZA9hujD0V2oKwtOP/1xoNNitf38kPWWEufSh 7hMA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=BC5+8LRYhpSa2f8/Hw83GNqklL+HJD3GODekfOgtol4=; b=mtijbL41NgHQ4qvFFrPKo5UHo/dwb88CE8pO4ehloMob5dpxziqBxxomJKzF8nIjNn sv93G0tVcA0HiMsh+PW6KZ1Hi8V9v3gVi88HqGBe6dyQmbipJNk6guiVmqOqQ5ps4LV2 mekYqKeUbN5Wp51v+b5oOqP2gJsO6Klvxh0MlVDfE54e6R0s58oIlc2Ehr9czJ6eMdSp fREcFCqETxNAx7imBPV4YS6vqI7wowgFxRiD48qh8IrglGJGWZ9Wnhcpg/1XEcdFPrJR ssU96ckwghPD7KjsOSUTCqqDR1+RNSTPVVJ0P5jl68gomjwgU/YOdpfKjOPWXW/KNTf+ XZiA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=DaiASXbl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ibm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id cx24si330509edb.419.2020.11.24.18.02.09; Tue, 24 Nov 2020 18:02:32 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=DaiASXbl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732433AbgKXLA7 (ORCPT + 99 others); Tue, 24 Nov 2020 06:00:59 -0500 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:3188 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1732421AbgKXLA7 (ORCPT ); Tue, 24 Nov 2020 06:00:59 -0500 Received: from pps.filterd (m0098420.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 0AOAVulv162750; Tue, 24 Nov 2020 06:00:31 -0500 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=BC5+8LRYhpSa2f8/Hw83GNqklL+HJD3GODekfOgtol4=; b=DaiASXblsWr6t3x9qwIxM3xOjWMT+2lhDBbsvjBPOpUlfSzV2pUh+f7BtIhBWyQOCoV6 aAPpo1LT3lxAr+ONeKP6iw5MKE8qFkwt255wn+6Iy6GJyLu6CjOdxaanwmVkQnly1dRK WN7WEg9dVbVcS4qFZ4GYwTHB3QWnY1tSZr6WLkYdIA1Ez2qwyd7pSzx18tZIxhyuzexJ SRfUVh5t8LOTazRLYDffGcgm2pa5s7/R/onpuavTK/pqg6qjSTeZ/SqZvFyqKDu7H2i9 zv/No3U0VzDKcwc8TKvmU5lBZHugwgtaPxIvQJh+In0++yllvGcW5QJO0yqRF2I5VION xQ== Received: from pps.reinject (localhost [127.0.0.1]) by mx0b-001b2d01.pphosted.com with ESMTP id 350pdq2tae-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 24 Nov 2020 06:00:30 -0500 Received: from m0098420.ppops.net (m0098420.ppops.net [127.0.0.1]) by pps.reinject (8.16.0.36/8.16.0.36) with SMTP id 0AOAWPL7164594; Tue, 24 Nov 2020 06:00:22 -0500 Received: from ppma06ams.nl.ibm.com (66.31.33a9.ip4.static.sl-reverse.com [169.51.49.102]) by mx0b-001b2d01.pphosted.com with ESMTP id 350pdq2t8d-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 24 Nov 2020 06:00:22 -0500 Received: from pps.filterd (ppma06ams.nl.ibm.com [127.0.0.1]) by ppma06ams.nl.ibm.com (8.16.0.42/8.16.0.42) with SMTP id 0AOAqBYC002556; Tue, 24 Nov 2020 11:00:20 GMT Received: from b06cxnps4076.portsmouth.uk.ibm.com (d06relay13.portsmouth.uk.ibm.com [9.149.109.198]) by ppma06ams.nl.ibm.com with ESMTP id 34xt5hbhh7-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 24 Nov 2020 11:00:20 +0000 Received: from d06av24.portsmouth.uk.ibm.com (mk.ibm.com [9.149.105.60]) by b06cxnps4076.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 0AOB0HNC37159332 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 24 Nov 2020 11:00:18 GMT Received: from d06av24.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id C79944206E; Tue, 24 Nov 2020 11:00:14 +0000 (GMT) Received: from d06av24.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 3A78F4203F; Tue, 24 Nov 2020 11:00:12 +0000 (GMT) Received: from bangoria.ibmuc.com (unknown [9.199.32.189]) by d06av24.portsmouth.uk.ibm.com (Postfix) with ESMTP; Tue, 24 Nov 2020 11:00:12 +0000 (GMT) From: Ravi Bangoria To: mpe@ellerman.id.au, paulus@samba.org Cc: ravi.bangoria@linux.ibm.com, mikey@neuling.org, npiggin@gmail.com, leobras.c@gmail.com, pbonzini@redhat.com, christophe.leroy@c-s.fr, jniethe5@gmail.com, kvm@vger.kernel.org, kvm-ppc@vger.kernel.org, linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org Subject: [PATCH v2 1/4] KVM: PPC: Allow nested guest creation when L0 hv_guest_state > L1 Date: Tue, 24 Nov 2020 16:29:50 +0530 Message-Id: <20201124105953.39325-2-ravi.bangoria@linux.ibm.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201124105953.39325-1-ravi.bangoria@linux.ibm.com> References: <20201124105953.39325-1-ravi.bangoria@linux.ibm.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-TM-AS-GCONF: 00 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.312,18.0.737 definitions=2020-11-24_04:2020-11-24,2020-11-23 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 malwarescore=0 suspectscore=0 mlxlogscore=999 spamscore=0 priorityscore=1501 mlxscore=0 phishscore=0 clxscore=1015 impostorscore=0 bulkscore=0 adultscore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2011240061 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On powerpc, L1 hypervisor takes help of L0 using H_ENTER_NESTED hcall to load L2 guest state in cpu. L1 hypervisor prepares the L2 state in struct hv_guest_state and passes a pointer to it via hcall. Using that pointer, L0 reads/writes that state directly from/to L1 memory. Thus L0 must be aware of hv_guest_state layout of L1. Currently it uses version field to achieve this. i.e. If L0 hv_guest_state.version != L1 hv_guest_state.version, L0 won't allow nested kvm guest. This restriction can be loosen up a bit. L0 can be taught to understand older layout of hv_guest_state, if we restrict the new member to be added only at the end. i.e. we can allow nested guest even when L0 hv_guest_state.version > L1 hv_guest_state.version. Though, the other way around is not possible. Signed-off-by: Ravi Bangoria --- arch/powerpc/include/asm/hvcall.h | 17 +++++++-- arch/powerpc/kvm/book3s_hv_nested.c | 53 ++++++++++++++++++++++++----- 2 files changed, 59 insertions(+), 11 deletions(-) diff --git a/arch/powerpc/include/asm/hvcall.h b/arch/powerpc/include/asm/hvcall.h index fbb377055471..a7073fddb657 100644 --- a/arch/powerpc/include/asm/hvcall.h +++ b/arch/powerpc/include/asm/hvcall.h @@ -524,9 +524,12 @@ struct h_cpu_char_result { u64 behaviour; }; -/* Register state for entering a nested guest with H_ENTER_NESTED */ +/* + * Register state for entering a nested guest with H_ENTER_NESTED. + * New member must be added at the end. + */ struct hv_guest_state { - u64 version; /* version of this structure layout */ + u64 version; /* version of this structure layout, must be first */ u32 lpid; u32 vcpu_token; /* These registers are hypervisor privileged (at least for writing) */ @@ -560,6 +563,16 @@ struct hv_guest_state { /* Latest version of hv_guest_state structure */ #define HV_GUEST_STATE_VERSION 1 +static inline int hv_guest_state_size(unsigned int version) +{ + switch (version) { + case 1: + return offsetofend(struct hv_guest_state, ppr); + default: + return -1; + } +} + #endif /* __ASSEMBLY__ */ #endif /* __KERNEL__ */ #endif /* _ASM_POWERPC_HVCALL_H */ diff --git a/arch/powerpc/kvm/book3s_hv_nested.c b/arch/powerpc/kvm/book3s_hv_nested.c index 33b58549a9aa..2b433c3bacea 100644 --- a/arch/powerpc/kvm/book3s_hv_nested.c +++ b/arch/powerpc/kvm/book3s_hv_nested.c @@ -215,6 +215,45 @@ static void kvmhv_nested_mmio_needed(struct kvm_vcpu *vcpu, u64 regs_ptr) } } +static int kvmhv_read_guest_state_and_regs(struct kvm_vcpu *vcpu, + struct hv_guest_state *l2_hv, + struct pt_regs *l2_regs, + u64 hv_ptr, u64 regs_ptr) +{ + int size; + + if (kvm_vcpu_read_guest(vcpu, hv_ptr, &(l2_hv->version), + sizeof(l2_hv->version))) + return -1; + + if (kvmppc_need_byteswap(vcpu)) + l2_hv->version = swab64(l2_hv->version); + + size = hv_guest_state_size(l2_hv->version); + if (size < 0) + return -1; + + return kvm_vcpu_read_guest(vcpu, hv_ptr, l2_hv, size) || + kvm_vcpu_read_guest(vcpu, regs_ptr, l2_regs, + sizeof(struct pt_regs)); +} + +static int kvmhv_write_guest_state_and_regs(struct kvm_vcpu *vcpu, + struct hv_guest_state *l2_hv, + struct pt_regs *l2_regs, + u64 hv_ptr, u64 regs_ptr) +{ + int size; + + size = hv_guest_state_size(l2_hv->version); + if (size < 0) + return -1; + + return kvm_vcpu_write_guest(vcpu, hv_ptr, l2_hv, size) || + kvm_vcpu_write_guest(vcpu, regs_ptr, l2_regs, + sizeof(struct pt_regs)); +} + long kvmhv_enter_nested_guest(struct kvm_vcpu *vcpu) { long int err, r; @@ -235,17 +274,15 @@ long kvmhv_enter_nested_guest(struct kvm_vcpu *vcpu) hv_ptr = kvmppc_get_gpr(vcpu, 4); regs_ptr = kvmppc_get_gpr(vcpu, 5); vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); - err = kvm_vcpu_read_guest(vcpu, hv_ptr, &l2_hv, - sizeof(struct hv_guest_state)) || - kvm_vcpu_read_guest(vcpu, regs_ptr, &l2_regs, - sizeof(struct pt_regs)); + err = kvmhv_read_guest_state_and_regs(vcpu, &l2_hv, &l2_regs, + hv_ptr, regs_ptr); srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); if (err) return H_PARAMETER; if (kvmppc_need_byteswap(vcpu)) byteswap_hv_regs(&l2_hv); - if (l2_hv.version != HV_GUEST_STATE_VERSION) + if (l2_hv.version > HV_GUEST_STATE_VERSION) return H_P2; if (kvmppc_need_byteswap(vcpu)) @@ -325,10 +362,8 @@ long kvmhv_enter_nested_guest(struct kvm_vcpu *vcpu) byteswap_pt_regs(&l2_regs); } vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); - err = kvm_vcpu_write_guest(vcpu, hv_ptr, &l2_hv, - sizeof(struct hv_guest_state)) || - kvm_vcpu_write_guest(vcpu, regs_ptr, &l2_regs, - sizeof(struct pt_regs)); + err = kvmhv_write_guest_state_and_regs(vcpu, &l2_hv, &l2_regs, + hv_ptr, regs_ptr); srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); if (err) return H_AUTHORITY; -- 2.26.2