diff mbox series

Run user level code in guest in a new KVM selftest

Message ID a65e6d23-791b-4866-8cb8-543d8f1942a6@zytor.com (mailing list archive)
State New, archived
Headers show
Series Run user level code in guest in a new KVM selftest | expand

Commit Message

Xin Li Oct. 26, 2023, 5:59 p.m. UTC
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:

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

Comments

Sean Christopherson Oct. 27, 2023, 12:54 a.m. UTC | #1
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.
diff mbox series

Patch

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 &