From: Zeng Guang <guang.zeng@intel.com>
To: Sean Christopherson <seanjc@google.com>,
Paolo Bonzini <pbonzini@redhat.com>,
Shuah Khan <shuah@kernel.org>, Marc Zyngier <maz@kernel.org>,
Oliver Upton <oliver.upton@linux.dev>,
James Morse <james.morse@arm.com>,
Suzuki K Poulose <suzuki.poulose@arm.com>,
Zenghui Yu <yuzenghui@huawei.com>,
Anup Patel <anup@brainfault.org>,
Atish Patra <atishp@atishpatra.org>,
David Hildenbrand <david@redhat.com>
Cc: kvm@vger.kernel.org, linux-kselftest@vger.kernel.org,
linux-kernel@vger.kernel.org,
linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev,
kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org,
Zeng Guang <guang.zeng@intel.com>
Subject: [RFC PATCH v1 7/8] KVM: selftests: x86: Support vcpu run in user mode
Date: Thu, 2 Nov 2023 23:51:10 +0800 [thread overview]
Message-ID: <20231102155111.28821-8-guang.zeng@intel.com> (raw)
In-Reply-To: <20231102155111.28821-1-guang.zeng@intel.com>
Introduce vcpu_setup_user_mode() to support vcpu run in user mode.
Signed-off-by: Zeng Guang <guang.zeng@intel.com>
---
.../selftests/kvm/include/x86_64/processor.h | 1 +
.../selftests/kvm/lib/x86_64/processor.c | 19 +++++++++++++++++++
2 files changed, 20 insertions(+)
diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h
index 9c8224c80664..2534bdf8aa71 100644
--- a/tools/testing/selftests/kvm/include/x86_64/processor.h
+++ b/tools/testing/selftests/kvm/include/x86_64/processor.h
@@ -800,6 +800,7 @@ static inline void cpu_relax(void)
struct kvm_x86_state *vcpu_save_state(struct kvm_vcpu *vcpu);
void vcpu_load_state(struct kvm_vcpu *vcpu, struct kvm_x86_state *state);
void kvm_x86_state_cleanup(struct kvm_x86_state *state);
+void vcpu_setup_user_mode(struct kvm_vcpu *vcpu, void *guest_code);
const struct kvm_msr_list *kvm_get_msr_index_list(void);
const struct kvm_msr_list *kvm_get_feature_msr_index_list(void);
diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c
index 7647c3755ca2..c84292b35f2d 100644
--- a/tools/testing/selftests/kvm/lib/x86_64/processor.c
+++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c
@@ -1071,6 +1071,25 @@ void vcpu_load_state(struct kvm_vcpu *vcpu, struct kvm_x86_state *state)
vcpu_nested_state_set(vcpu, &state->nested);
}
+void vcpu_setup_user_mode(struct kvm_vcpu *vcpu, void *guest_code)
+{
+ struct kvm_sregs sregs;
+ struct kvm_regs regs;
+ struct kvm_vm *vm = vcpu->vm;
+
+ vcpu_sregs_get(vcpu, &sregs);
+ kvm_seg_set_code_64bit(vm, USER_CODE_SELECTOR, &sregs.cs);
+ kvm_seg_set_data_64bit(vm, USER_DATA_SELECTOR, &sregs.ds);
+ kvm_seg_set_data_64bit(vm, USER_DATA_SELECTOR, &sregs.es);
+ kvm_seg_set_data_64bit(vm, USER_DATA_SELECTOR, &sregs.ss);
+ vcpu_sregs_set(vcpu, &sregs);
+
+ vcpu_regs_get(vcpu, ®s);
+ regs.rsp = vcpu->stack_vaddr - (DEFAULT_STACK_PGS >> 1) * getpagesize();
+ regs.rip = (unsigned long) guest_code;
+ vcpu_regs_set(vcpu, ®s);
+}
+
void kvm_x86_state_cleanup(struct kvm_x86_state *state)
{
free(state->xsave);
--
2.21.3
next prev parent reply other threads:[~2023-11-02 16:34 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-02 15:51 [RFC PATCH v1 0/8] KVM: seftests: Support guest user mode execution and running Zeng Guang
2023-11-02 15:51 ` [RFC PATCH v1 1/8] KVM: selftests: x86: Fix bug in addr_arch_gva2gpa() Zeng Guang
2024-01-31 22:45 ` Sean Christopherson
2023-11-02 15:51 ` [RFC PATCH v1 2/8] KVM: selftests: x86: Support guest running on canonical linear-address organization Zeng Guang
2024-01-31 23:03 ` Sean Christopherson
2023-11-02 15:51 ` [RFC PATCH v1 3/8] KVM: selftests: Add virt_arch_ucall_prealloc() arch specific implementation Zeng Guang
2024-01-31 23:20 ` Sean Christopherson
2023-11-02 15:51 ` [RFC PATCH v1 4/8] KVM : selftests : Adapt selftest cases to kernel canonical linear address Zeng Guang
2023-11-02 15:51 ` [RFC PATCH v1 5/8] KVM: selftests: x86: Prepare setup for user mode support Zeng Guang
2023-11-02 15:51 ` [RFC PATCH v1 6/8] KVM: selftests: x86: Allow user to access user-mode address and I/O address space Zeng Guang
2023-11-02 15:51 ` Zeng Guang [this message]
2023-11-02 15:51 ` [RFC PATCH v1 8/8] KVM: selftests: x86: Add KVM forced emulation prefix capability Zeng Guang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231102155111.28821-8-guang.zeng@intel.com \
--to=guang.zeng@intel.com \
--cc=anup@brainfault.org \
--cc=atishp@atishpatra.org \
--cc=david@redhat.com \
--cc=james.morse@arm.com \
--cc=kvm-riscv@lists.infradead.org \
--cc=kvm@vger.kernel.org \
--cc=kvmarm@lists.linux.dev \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=maz@kernel.org \
--cc=oliver.upton@linux.dev \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
--cc=shuah@kernel.org \
--cc=suzuki.poulose@arm.com \
--cc=yuzenghui@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).