* Run user level code in guest in a new KVM selftest
@ 2023-10-26 17:59 Xin Li
2023-10-27 0:54 ` Sean Christopherson
0 siblings, 1 reply; 2+ messages in thread
From: Xin Li @ 2023-10-26 17:59 UTC (permalink / raw)
To: Sean Christopherson; +Cc: kvm
Hi Sean,
I'm adding a nested exception selftest for FRED, which needs to run
user level code in guest. I have to add the following hack for that:
diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c
b/tools/testing/selftests/kvm/lib/x86_64/processor.c
index d8288374078e..72928c07ccbe 100644
--- a/tools/testing/selftests/kvm/lib/x86_64/processor.c
+++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c
@@ -159,6 +159,7 @@ static uint64_t *virt_create_upper_pte(struct kvm_vm
*vm,
if (!(*pte & PTE_PRESENT_MASK)) {
*pte = PTE_PRESENT_MASK | PTE_WRITABLE_MASK;
+ *pte |= PTE_USER_MASK;
if (current_level == target_level)
*pte |= PTE_LARGE_MASK | (paddr &
PHYSICAL_PAGE_MASK);
else
@@ -222,6 +223,7 @@ void __virt_pg_map(struct kvm_vm *vm, uint64_t
vaddr, uint64_t paddr, int level)
TEST_ASSERT(!(*pte & PTE_PRESENT_MASK),
"PTE already present for 4k page at vaddr:
0x%lx\n", vaddr);
*pte = PTE_PRESENT_MASK | PTE_WRITABLE_MASK | (paddr &
PHYSICAL_PAGE_MASK);
+ *pte |= PTE_USER_MASK;
}
void virt_arch_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr)
Is there an exiting selftest running user level code in guest?
It seems there is none as the USER bit in PTEs is never set, what have I
missed?
If such a facility doesn't exist, we probably need to find a
clean solution to add the USER bit in user level page table mappings
(which seems not yet clearly defined yet).
Thanks!
Xin
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: Run user level code in guest in a new KVM selftest
2023-10-26 17:59 Run user level code in guest in a new KVM selftest Xin Li
@ 2023-10-27 0:54 ` Sean Christopherson
0 siblings, 0 replies; 2+ messages in thread
From: Sean Christopherson @ 2023-10-27 0:54 UTC (permalink / raw)
To: Xin Li; +Cc: kvm
On Thu, Oct 26, 2023, Xin Li wrote:
> Hi Sean,
>
> I'm adding a nested exception selftest for FRED, which needs to run
> user level code in guest. I have to add the following hack for that:
>
> diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c
> b/tools/testing/selftests/kvm/lib/x86_64/processor.c
> index d8288374078e..72928c07ccbe 100644
> --- a/tools/testing/selftests/kvm/lib/x86_64/processor.c
> +++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c
> @@ -159,6 +159,7 @@ static uint64_t *virt_create_upper_pte(struct kvm_vm
> *vm,
>
> if (!(*pte & PTE_PRESENT_MASK)) {
> *pte = PTE_PRESENT_MASK | PTE_WRITABLE_MASK;
> + *pte |= PTE_USER_MASK;
> if (current_level == target_level)
> *pte |= PTE_LARGE_MASK | (paddr &
> PHYSICAL_PAGE_MASK);
> else
> @@ -222,6 +223,7 @@ void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr,
> uint64_t paddr, int level)
> TEST_ASSERT(!(*pte & PTE_PRESENT_MASK),
> "PTE already present for 4k page at vaddr: 0x%lx\n",
> vaddr);
> *pte = PTE_PRESENT_MASK | PTE_WRITABLE_MASK | (paddr &
> PHYSICAL_PAGE_MASK);
> + *pte |= PTE_USER_MASK;
> }
>
> void virt_arch_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr)
>
>
> Is there an exiting selftest running user level code in guest?
Nope, not that I know of.
> It seems there is none as the USER bit in PTEs is never set, what have I
> missed?
Nothing.
> If such a facility doesn't exist, we probably need to find a
> clean solution to add the USER bit in user level page table mappings
> (which seems not yet clearly defined yet).
Yes, being able to run usercode would be very nice indeed. It should be a lot
simpler than KUT since we can stuff guest state directly. The big hiccup is SMEP
and SMAP, e.g. we can't just set PTE_USER_MASK blindly :-(
My best off-the-top-of-my-head idea is to play games with "enum vm_guest_mode" so
that we know from time zero that the guest will be run in user mode, e.g. so that
loading the initial guest_code can map it for user.
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2023-10-27 0:55 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-10-26 17:59 Run user level code in guest in a new KVM selftest Xin Li
2023-10-27 0:54 ` Sean Christopherson
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox