Introduce following APIs:
1) kvm_arch_main : to be called at the startup of each test.
2) kvm_arch_post_vm_load: called after guest elf image is loaded into
memory to populate any global state in guest memory.
Suggested-by: Sean Christopherson <[email protected]>
Signed-off-by: Vishal Annapurve <[email protected]>
---
tools/testing/selftests/kvm/include/kvm_util_base.h | 10 ++++++++++
tools/testing/selftests/kvm/lib/aarch64/processor.c | 8 ++++++++
tools/testing/selftests/kvm/lib/elf.c | 2 ++
tools/testing/selftests/kvm/lib/kvm_util.c | 2 ++
tools/testing/selftests/kvm/lib/riscv/processor.c | 8 ++++++++
tools/testing/selftests/kvm/lib/s390x/processor.c | 8 ++++++++
tools/testing/selftests/kvm/lib/x86_64/processor.c | 8 ++++++++
7 files changed, 46 insertions(+)
diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h
index 9e521d1c8afe..301bef6376a5 100644
--- a/tools/testing/selftests/kvm/include/kvm_util_base.h
+++ b/tools/testing/selftests/kvm/include/kvm_util_base.h
@@ -834,6 +834,16 @@ static inline int __vm_disable_nx_huge_pages(struct kvm_vm *vm)
return __vm_enable_cap(vm, KVM_CAP_VM_DISABLE_NX_HUGE_PAGES, 0);
}
+/*
+ * API to execute architecture specific setup before executing selftest logic.
+ */
+void kvm_arch_main(void);
+
+/*
+ * API to execute architecture specific setup after loading VMs.
+ */
+void kvm_arch_post_vm_load(struct kvm_vm *vm);
+
/*
* API to be implemented by all the selftests.
*/
diff --git a/tools/testing/selftests/kvm/lib/aarch64/processor.c b/tools/testing/selftests/kvm/lib/aarch64/processor.c
index 6f5551368944..a7ca1947d574 100644
--- a/tools/testing/selftests/kvm/lib/aarch64/processor.c
+++ b/tools/testing/selftests/kvm/lib/aarch64/processor.c
@@ -528,3 +528,11 @@ void smccc_hvc(uint32_t function_id, uint64_t arg0, uint64_t arg1,
[arg4] "r"(arg4), [arg5] "r"(arg5), [arg6] "r"(arg6)
: "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7");
}
+
+void kvm_arch_main(void)
+{
+}
+
+void kvm_arch_post_vm_load(struct kvm_vm *vm)
+{
+}
diff --git a/tools/testing/selftests/kvm/lib/elf.c b/tools/testing/selftests/kvm/lib/elf.c
index 9f54c098d9d0..f56f9279e703 100644
--- a/tools/testing/selftests/kvm/lib/elf.c
+++ b/tools/testing/selftests/kvm/lib/elf.c
@@ -189,4 +189,6 @@ void kvm_vm_elf_load(struct kvm_vm *vm, const char *filename)
phdr.p_filesz);
}
}
+
+ kvm_arch_post_vm_load(vm);
}
diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
index 2e611a021c6e..b778dc684e30 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -1985,6 +1985,8 @@ int main(int argc, char *argv[])
/* Tell stdout not to buffer its content */
setbuf(stdout, NULL);
+ kvm_arch_main();
+
__main(argc, argv);
return 0;
diff --git a/tools/testing/selftests/kvm/lib/riscv/processor.c b/tools/testing/selftests/kvm/lib/riscv/processor.c
index 604478151212..d992ad5b5771 100644
--- a/tools/testing/selftests/kvm/lib/riscv/processor.c
+++ b/tools/testing/selftests/kvm/lib/riscv/processor.c
@@ -362,3 +362,11 @@ void vcpu_args_set(struct kvm_vcpu *vcpu, unsigned int num, ...)
void assert_on_unhandled_exception(struct kvm_vcpu *vcpu)
{
}
+
+void kvm_arch_main(void)
+{
+}
+
+void kvm_arch_post_vm_load(struct kvm_vm *vm)
+{
+}
diff --git a/tools/testing/selftests/kvm/lib/s390x/processor.c b/tools/testing/selftests/kvm/lib/s390x/processor.c
index 89d7340d9cbd..3a249783b3fe 100644
--- a/tools/testing/selftests/kvm/lib/s390x/processor.c
+++ b/tools/testing/selftests/kvm/lib/s390x/processor.c
@@ -218,3 +218,11 @@ void vcpu_arch_dump(FILE *stream, struct kvm_vcpu *vcpu, uint8_t indent)
void assert_on_unhandled_exception(struct kvm_vcpu *vcpu)
{
}
+
+void kvm_arch_main(void)
+{
+}
+
+void kvm_arch_post_vm_load(struct kvm_vm *vm)
+{
+}
diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c
index 2e6e61bbe81b..e22cfc4bf284 100644
--- a/tools/testing/selftests/kvm/lib/x86_64/processor.c
+++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c
@@ -1311,3 +1311,11 @@ bool vm_is_unrestricted_guest(struct kvm_vm *vm)
return val == 'Y';
}
+
+void kvm_arch_main(void)
+{
+}
+
+void kvm_arch_post_vm_load(struct kvm_vm *vm)
+{
+}
--
2.37.2.789.g6183377224-goog
On Sat, Sep 03, 2022 at 01:28:46AM +0000, Vishal Annapurve wrote:
> Introduce following APIs:
> 1) kvm_arch_main : to be called at the startup of each test.
With this, AArch64 can move the content of its constructor,
init_guest_modes(), into kvm_arch_main(). Or, instead of the
main()
{
/* common main stuff */
kvm_arch_main();
__main();
}
approach we could have each arch provide a constructor
arch_init()
{
common_pre_main_stuff();
/* arch specific pre-main stuff */
}
I personally prefer the latter.
Thanks,
drew
> 2) kvm_arch_post_vm_load: called after guest elf image is loaded into
> memory to populate any global state in guest memory.
>
> Suggested-by: Sean Christopherson <[email protected]>
> Signed-off-by: Vishal Annapurve <[email protected]>
> ---
> tools/testing/selftests/kvm/include/kvm_util_base.h | 10 ++++++++++
> tools/testing/selftests/kvm/lib/aarch64/processor.c | 8 ++++++++
> tools/testing/selftests/kvm/lib/elf.c | 2 ++
> tools/testing/selftests/kvm/lib/kvm_util.c | 2 ++
> tools/testing/selftests/kvm/lib/riscv/processor.c | 8 ++++++++
> tools/testing/selftests/kvm/lib/s390x/processor.c | 8 ++++++++
> tools/testing/selftests/kvm/lib/x86_64/processor.c | 8 ++++++++
> 7 files changed, 46 insertions(+)
>
> diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h
> index 9e521d1c8afe..301bef6376a5 100644
> --- a/tools/testing/selftests/kvm/include/kvm_util_base.h
> +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h
> @@ -834,6 +834,16 @@ static inline int __vm_disable_nx_huge_pages(struct kvm_vm *vm)
> return __vm_enable_cap(vm, KVM_CAP_VM_DISABLE_NX_HUGE_PAGES, 0);
> }
>
> +/*
> + * API to execute architecture specific setup before executing selftest logic.
> + */
> +void kvm_arch_main(void);
> +
> +/*
> + * API to execute architecture specific setup after loading VMs.
> + */
> +void kvm_arch_post_vm_load(struct kvm_vm *vm);
> +
> /*
> * API to be implemented by all the selftests.
> */
> diff --git a/tools/testing/selftests/kvm/lib/aarch64/processor.c b/tools/testing/selftests/kvm/lib/aarch64/processor.c
> index 6f5551368944..a7ca1947d574 100644
> --- a/tools/testing/selftests/kvm/lib/aarch64/processor.c
> +++ b/tools/testing/selftests/kvm/lib/aarch64/processor.c
> @@ -528,3 +528,11 @@ void smccc_hvc(uint32_t function_id, uint64_t arg0, uint64_t arg1,
> [arg4] "r"(arg4), [arg5] "r"(arg5), [arg6] "r"(arg6)
> : "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7");
> }
> +
> +void kvm_arch_main(void)
> +{
> +}
> +
> +void kvm_arch_post_vm_load(struct kvm_vm *vm)
> +{
> +}
> diff --git a/tools/testing/selftests/kvm/lib/elf.c b/tools/testing/selftests/kvm/lib/elf.c
> index 9f54c098d9d0..f56f9279e703 100644
> --- a/tools/testing/selftests/kvm/lib/elf.c
> +++ b/tools/testing/selftests/kvm/lib/elf.c
> @@ -189,4 +189,6 @@ void kvm_vm_elf_load(struct kvm_vm *vm, const char *filename)
> phdr.p_filesz);
> }
> }
> +
> + kvm_arch_post_vm_load(vm);
> }
> diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
> index 2e611a021c6e..b778dc684e30 100644
> --- a/tools/testing/selftests/kvm/lib/kvm_util.c
> +++ b/tools/testing/selftests/kvm/lib/kvm_util.c
> @@ -1985,6 +1985,8 @@ int main(int argc, char *argv[])
> /* Tell stdout not to buffer its content */
> setbuf(stdout, NULL);
>
> + kvm_arch_main();
> +
> __main(argc, argv);
>
> return 0;
> diff --git a/tools/testing/selftests/kvm/lib/riscv/processor.c b/tools/testing/selftests/kvm/lib/riscv/processor.c
> index 604478151212..d992ad5b5771 100644
> --- a/tools/testing/selftests/kvm/lib/riscv/processor.c
> +++ b/tools/testing/selftests/kvm/lib/riscv/processor.c
> @@ -362,3 +362,11 @@ void vcpu_args_set(struct kvm_vcpu *vcpu, unsigned int num, ...)
> void assert_on_unhandled_exception(struct kvm_vcpu *vcpu)
> {
> }
> +
> +void kvm_arch_main(void)
> +{
> +}
> +
> +void kvm_arch_post_vm_load(struct kvm_vm *vm)
> +{
> +}
> diff --git a/tools/testing/selftests/kvm/lib/s390x/processor.c b/tools/testing/selftests/kvm/lib/s390x/processor.c
> index 89d7340d9cbd..3a249783b3fe 100644
> --- a/tools/testing/selftests/kvm/lib/s390x/processor.c
> +++ b/tools/testing/selftests/kvm/lib/s390x/processor.c
> @@ -218,3 +218,11 @@ void vcpu_arch_dump(FILE *stream, struct kvm_vcpu *vcpu, uint8_t indent)
> void assert_on_unhandled_exception(struct kvm_vcpu *vcpu)
> {
> }
> +
> +void kvm_arch_main(void)
> +{
> +}
> +
> +void kvm_arch_post_vm_load(struct kvm_vm *vm)
> +{
> +}
> diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c
> index 2e6e61bbe81b..e22cfc4bf284 100644
> --- a/tools/testing/selftests/kvm/lib/x86_64/processor.c
> +++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c
> @@ -1311,3 +1311,11 @@ bool vm_is_unrestricted_guest(struct kvm_vm *vm)
>
> return val == 'Y';
> }
> +
> +void kvm_arch_main(void)
> +{
> +}
> +
> +void kvm_arch_post_vm_load(struct kvm_vm *vm)
> +{
> +}
> --
> 2.37.2.789.g6183377224-goog
>
On Mon, Sep 5, 2022 at 12:46 AM Andrew Jones <[email protected]> wrote:
>
> On Sat, Sep 03, 2022 at 01:28:46AM +0000, Vishal Annapurve wrote:
> > Introduce following APIs:
> > 1) kvm_arch_main : to be called at the startup of each test.
>
> With this, AArch64 can move the content of its constructor,
> init_guest_modes(), into kvm_arch_main(). Or, instead of the
>
> main()
> {
> /* common main stuff */
> kvm_arch_main();
> __main();
> }
>
> approach we could have each arch provide a constructor
>
> arch_init()
> {
> common_pre_main_stuff();
> /* arch specific pre-main stuff */
> }
>
> I personally prefer the latter.
>
I agree with your suggestion of using constructors here. This will
help avoid changes in all the selftests.
Maybe I can add a common constructor that can invoke arch specific
init. I will add this change in the next series.
Thanks,
Vishal
> Thanks,
> drew
>
> > 2) kvm_arch_post_vm_load: called after guest elf image is loaded into
> > memory to populate any global state in guest memory.
> >
> > Suggested-by: Sean Christopherson <[email protected]>
> > Signed-off-by: Vishal Annapurve <[email protected]>
> > ---
> > tools/testing/selftests/kvm/include/kvm_util_base.h | 10 ++++++++++
> > tools/testing/selftests/kvm/lib/aarch64/processor.c | 8 ++++++++
> > tools/testing/selftests/kvm/lib/elf.c | 2 ++
> > tools/testing/selftests/kvm/lib/kvm_util.c | 2 ++
> > tools/testing/selftests/kvm/lib/riscv/processor.c | 8 ++++++++
> > tools/testing/selftests/kvm/lib/s390x/processor.c | 8 ++++++++
> > tools/testing/selftests/kvm/lib/x86_64/processor.c | 8 ++++++++
> > 7 files changed, 46 insertions(+)
> >
> > diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h
> > index 9e521d1c8afe..301bef6376a5 100644
> > --- a/tools/testing/selftests/kvm/include/kvm_util_base.h
> > +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h
> > @@ -834,6 +834,16 @@ static inline int __vm_disable_nx_huge_pages(struct kvm_vm *vm)
> > return __vm_enable_cap(vm, KVM_CAP_VM_DISABLE_NX_HUGE_PAGES, 0);
> > }
> >
> > +/*
> > + * API to execute architecture specific setup before executing selftest logic.
> > + */
> > +void kvm_arch_main(void);
> > +
> > +/*
> > + * API to execute architecture specific setup after loading VMs.
> > + */
> > +void kvm_arch_post_vm_load(struct kvm_vm *vm);
> > +
> > /*
> > * API to be implemented by all the selftests.
> > */
> > diff --git a/tools/testing/selftests/kvm/lib/aarch64/processor.c b/tools/testing/selftests/kvm/lib/aarch64/processor.c
> > index 6f5551368944..a7ca1947d574 100644
> > --- a/tools/testing/selftests/kvm/lib/aarch64/processor.c
> > +++ b/tools/testing/selftests/kvm/lib/aarch64/processor.c
> > @@ -528,3 +528,11 @@ void smccc_hvc(uint32_t function_id, uint64_t arg0, uint64_t arg1,
> > [arg4] "r"(arg4), [arg5] "r"(arg5), [arg6] "r"(arg6)
> > : "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7");
> > }
> > +
> > +void kvm_arch_main(void)
> > +{
> > +}
> > +
> > +void kvm_arch_post_vm_load(struct kvm_vm *vm)
> > +{
> > +}
> > diff --git a/tools/testing/selftests/kvm/lib/elf.c b/tools/testing/selftests/kvm/lib/elf.c
> > index 9f54c098d9d0..f56f9279e703 100644
> > --- a/tools/testing/selftests/kvm/lib/elf.c
> > +++ b/tools/testing/selftests/kvm/lib/elf.c
> > @@ -189,4 +189,6 @@ void kvm_vm_elf_load(struct kvm_vm *vm, const char *filename)
> > phdr.p_filesz);
> > }
> > }
> > +
> > + kvm_arch_post_vm_load(vm);
> > }
> > diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
> > index 2e611a021c6e..b778dc684e30 100644
> > --- a/tools/testing/selftests/kvm/lib/kvm_util.c
> > +++ b/tools/testing/selftests/kvm/lib/kvm_util.c
> > @@ -1985,6 +1985,8 @@ int main(int argc, char *argv[])
> > /* Tell stdout not to buffer its content */
> > setbuf(stdout, NULL);
> >
> > + kvm_arch_main();
> > +
> > __main(argc, argv);
> >
> > return 0;
> > diff --git a/tools/testing/selftests/kvm/lib/riscv/processor.c b/tools/testing/selftests/kvm/lib/riscv/processor.c
> > index 604478151212..d992ad5b5771 100644
> > --- a/tools/testing/selftests/kvm/lib/riscv/processor.c
> > +++ b/tools/testing/selftests/kvm/lib/riscv/processor.c
> > @@ -362,3 +362,11 @@ void vcpu_args_set(struct kvm_vcpu *vcpu, unsigned int num, ...)
> > void assert_on_unhandled_exception(struct kvm_vcpu *vcpu)
> > {
> > }
> > +
> > +void kvm_arch_main(void)
> > +{
> > +}
> > +
> > +void kvm_arch_post_vm_load(struct kvm_vm *vm)
> > +{
> > +}
> > diff --git a/tools/testing/selftests/kvm/lib/s390x/processor.c b/tools/testing/selftests/kvm/lib/s390x/processor.c
> > index 89d7340d9cbd..3a249783b3fe 100644
> > --- a/tools/testing/selftests/kvm/lib/s390x/processor.c
> > +++ b/tools/testing/selftests/kvm/lib/s390x/processor.c
> > @@ -218,3 +218,11 @@ void vcpu_arch_dump(FILE *stream, struct kvm_vcpu *vcpu, uint8_t indent)
> > void assert_on_unhandled_exception(struct kvm_vcpu *vcpu)
> > {
> > }
> > +
> > +void kvm_arch_main(void)
> > +{
> > +}
> > +
> > +void kvm_arch_post_vm_load(struct kvm_vm *vm)
> > +{
> > +}
> > diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c
> > index 2e6e61bbe81b..e22cfc4bf284 100644
> > --- a/tools/testing/selftests/kvm/lib/x86_64/processor.c
> > +++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c
> > @@ -1311,3 +1311,11 @@ bool vm_is_unrestricted_guest(struct kvm_vm *vm)
> >
> > return val == 'Y';
> > }
> > +
> > +void kvm_arch_main(void)
> > +{
> > +}
> > +
> > +void kvm_arch_post_vm_load(struct kvm_vm *vm)
> > +{
> > +}
> > --
> > 2.37.2.789.g6183377224-goog
> >
On Tue, Sep 06, 2022 at 03:46:20PM -0700, Vishal Annapurve wrote:
> On Mon, Sep 5, 2022 at 12:46 AM Andrew Jones <[email protected]> wrote:
> >
> > On Sat, Sep 03, 2022 at 01:28:46AM +0000, Vishal Annapurve wrote:
> > > Introduce following APIs:
> > > 1) kvm_arch_main : to be called at the startup of each test.
> >
> > With this, AArch64 can move the content of its constructor,
> > init_guest_modes(), into kvm_arch_main(). Or, instead of the
> >
> > main()
> > {
> > /* common main stuff */
> > kvm_arch_main();
> > __main();
> > }
> >
> > approach we could have each arch provide a constructor
> >
> > arch_init()
> > {
> > common_pre_main_stuff();
> > /* arch specific pre-main stuff */
> > }
> >
> > I personally prefer the latter.
> >
>
> I agree with your suggestion of using constructors here. This will
> help avoid changes in all the selftests.
> Maybe I can add a common constructor that can invoke arch specific
> init. I will add this change in the next series.
In case anyone else is confused like me: "constructor" refers to
__attribute__ ((constructor)), which causes the function to run before
main().
I have a slight preference for having as few constructors as possible,
since they are somewhat subtle. So how about one constructor for all
selftests, e.g.:
void __attribute__ ((constructor)) kvm_selftest_init(void)
{
/* Tell stdout not to buffer its content. */
setbuf(stdout, NULL);
kvm_selftest_arch_init();
}
Per-arch:
void kvm_selftest_arch_init(void)
{
/* arch-specific pre-main stuff */
}
On Sat, Sep 03, 2022 at 01:28:46AM +0000, Vishal Annapurve wrote:
> Introduce following APIs:
> 1) kvm_arch_main : to be called at the startup of each test.
> 2) kvm_arch_post_vm_load: called after guest elf image is loaded into
> memory to populate any global state in guest memory.
nit: Can you throw the post_vm_load in a separate commit? It's a
logically distinct change. Also suggest naming it
kvm_arch_post_vm_elf_load() to make it obvious it runs after
kvm_vm_elf_load().
On Thu, Sep 08, 2022 at 02:21:52PM -0700, David Matlack wrote:
> On Tue, Sep 06, 2022 at 03:46:20PM -0700, Vishal Annapurve wrote:
> > On Mon, Sep 5, 2022 at 12:46 AM Andrew Jones <[email protected]> wrote:
> > >
> > > On Sat, Sep 03, 2022 at 01:28:46AM +0000, Vishal Annapurve wrote:
> > > > Introduce following APIs:
> > > > 1) kvm_arch_main : to be called at the startup of each test.
> > >
> > > With this, AArch64 can move the content of its constructor,
> > > init_guest_modes(), into kvm_arch_main(). Or, instead of the
> > >
> > > main()
> > > {
> > > /* common main stuff */
> > > kvm_arch_main();
> > > __main();
> > > }
> > >
> > > approach we could have each arch provide a constructor
> > >
> > > arch_init()
> > > {
> > > common_pre_main_stuff();
> > > /* arch specific pre-main stuff */
> > > }
> > >
> > > I personally prefer the latter.
> > >
> >
> > I agree with your suggestion of using constructors here. This will
> > help avoid changes in all the selftests.
> > Maybe I can add a common constructor that can invoke arch specific
> > init. I will add this change in the next series.
>
> In case anyone else is confused like me: "constructor" refers to
> __attribute__ ((constructor)), which causes the function to run before
> main().
>
> I have a slight preference for having as few constructors as possible,
> since they are somewhat subtle. So how about one constructor for all
> selftests, e.g.:
>
> void __attribute__ ((constructor)) kvm_selftest_init(void)
> {
> /* Tell stdout not to buffer its content. */
> setbuf(stdout, NULL);
>
> kvm_selftest_arch_init();
> }
>
> Per-arch:
>
> void kvm_selftest_arch_init(void)
> {
> /* arch-specific pre-main stuff */
> }
WFM and I think that's what Vishal was suggesting as well.
Thanks,
drew
On Fri, Sep 9, 2022 at 12:01 AM Andrew Jones <[email protected]> wrote:
...
> >
> > void __attribute__ ((constructor)) kvm_selftest_init(void)
> > {
> > /* Tell stdout not to buffer its content. */
> > setbuf(stdout, NULL);
> >
> > kvm_selftest_arch_init();
> > }
> >
> > Per-arch:
> >
> > void kvm_selftest_arch_init(void)
> > {
> > /* arch-specific pre-main stuff */
> > }
>
> WFM and I think that's what Vishal was suggesting as well.
>
Yes, this matches with what I was suggesting. Planning to post this
update in the next series.
- Vishal
> Thanks,
> drew