2019-07-30 12:02:16

by Andrew Jones

[permalink] [raw]
Subject: Re: [PATCH 1/2] KVM: selftests: Implement ucall() for s390x

On Tue, Jul 30, 2019 at 12:01:11PM +0200, Thomas Huth wrote:
> On s390x, we can neither exit via PIO nor MMIO, but have to use
> an instruction like DIAGNOSE. While we're at it, rename UCALL_PIO
> to UCALL_DEFAULT, since PIO only works on x86 anyway, and this
> way we can re-use the "default" type for the DIAGNOSE exit on s390x.
>
> Now that ucall() is implemented, we can use it in the sync_reg_test
> on s390x, too.
>
> Signed-off-by: Thomas Huth <[email protected]>
> ---
> .../testing/selftests/kvm/include/kvm_util.h | 2 +-
> tools/testing/selftests/kvm/lib/ucall.c | 34 +++++++++++++++----
> .../selftests/kvm/s390x/sync_regs_test.c | 6 ++--
> 3 files changed, 32 insertions(+), 10 deletions(-)
>
> diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h
> index e0e66b115ef2..c37aea2e33e5 100644
> --- a/tools/testing/selftests/kvm/include/kvm_util.h
> +++ b/tools/testing/selftests/kvm/include/kvm_util.h
> @@ -167,7 +167,7 @@ int vm_create_device(struct kvm_vm *vm, struct kvm_create_device *cd);
>
> /* ucall implementation types */
> typedef enum {
> - UCALL_PIO,
> + UCALL_DEFAULT,

I'd rather we keep explicit types defined; keep PIO and add DIAG. Then
we can have

/* Set default ucall types */
#if defined(__x86_64__)
ucall_type = UCALL_PIO;
#elif defined(__aarch64__)
ucall_type = UCALL_MMIO;
ucall_requires_init = true;
#elif defined(__s390x__)
ucall_type = UCALL_DIAG;
#endif

And add an assert in get_ucall()

assert(!ucall_requires_init || ucall_initialized);


> UCALL_MMIO,
> } ucall_type_t;
>
> diff --git a/tools/testing/selftests/kvm/lib/ucall.c b/tools/testing/selftests/kvm/lib/ucall.c
> index dd9a66700f96..55534dd014dc 100644
> --- a/tools/testing/selftests/kvm/lib/ucall.c
> +++ b/tools/testing/selftests/kvm/lib/ucall.c
> @@ -30,7 +30,7 @@ void ucall_init(struct kvm_vm *vm, ucall_type_t type, void *arg)
> ucall_type = type;
> sync_global_to_guest(vm, ucall_type);
>
> - if (type == UCALL_PIO)
> + if (type == UCALL_DEFAULT)
> return;
>
> if (type == UCALL_MMIO) {
> @@ -84,11 +84,18 @@ void ucall_uninit(struct kvm_vm *vm)
> sync_global_to_guest(vm, ucall_exit_mmio_addr);
> }
>
> -static void ucall_pio_exit(struct ucall *uc)
> +static void ucall_default_exit(struct ucall *uc)
> {
> -#ifdef __x86_64__
> +#if defined(__x86_64__)
> + /* Exit via PIO */
> asm volatile("in %[port], %%al"
> : : [port] "d" (UCALL_PIO_PORT), "D" (uc) : "rax");
> +#elif defined(__s390x__)
> + /* Exit via DIAGNOSE 0x501 (normally used for breakpoints) */
> + asm volatile ("diag 0,%0,0x501" : : "a"(uc) : "memory");
> +#else
> + fprintf(stderr, "No default ucall available on this architecture.\n");
> + exit(1);
> #endif
> }
>
> @@ -113,8 +120,8 @@ void ucall(uint64_t cmd, int nargs, ...)
> va_end(va);
>
> switch (ucall_type) {
> - case UCALL_PIO:
> - ucall_pio_exit(&uc);
> + case UCALL_DEFAULT:
> + ucall_default_exit(&uc);
> break;
> case UCALL_MMIO:
> ucall_mmio_exit(&uc);
> @@ -128,15 +135,28 @@ uint64_t get_ucall(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc)
> struct ucall ucall = {};
> bool got_ucall = false;
>
> -#ifdef __x86_64__
> - if (ucall_type == UCALL_PIO && run->exit_reason == KVM_EXIT_IO &&
> +#if defined(__x86_64__)
> + if (ucall_type == UCALL_DEFAULT && run->exit_reason == KVM_EXIT_IO &&
> run->io.port == UCALL_PIO_PORT) {
> struct kvm_regs regs;
> vcpu_regs_get(vm, vcpu_id, &regs);
> memcpy(&ucall, addr_gva2hva(vm, (vm_vaddr_t)regs.rdi), sizeof(ucall));
> got_ucall = true;
> }
> +#elif defined(__s390x__)
> + if (ucall_type == UCALL_DEFAULT &&
> + run->exit_reason == KVM_EXIT_S390_SIEIC &&
> + run->s390_sieic.icptcode == 4 &&
> + (run->s390_sieic.ipa >> 8) == 0x83 && /* 0x83 means DIAGNOSE */
> + (run->s390_sieic.ipb >> 16) == 0x501) {
> + int reg = run->s390_sieic.ipa & 0xf;
> +
> + memcpy(&ucall, addr_gva2hva(vm, run->s.regs.gprs[reg]),
> + sizeof(ucall));
> + got_ucall = true;
> + }
> #endif
> +
> if (ucall_type == UCALL_MMIO && run->exit_reason == KVM_EXIT_MMIO &&
> run->mmio.phys_addr == (uint64_t)ucall_exit_mmio_addr) {
> vm_vaddr_t gva;
> diff --git a/tools/testing/selftests/kvm/s390x/sync_regs_test.c b/tools/testing/selftests/kvm/s390x/sync_regs_test.c
> index e85ff0d69548..bbc93094519b 100644
> --- a/tools/testing/selftests/kvm/s390x/sync_regs_test.c
> +++ b/tools/testing/selftests/kvm/s390x/sync_regs_test.c
> @@ -25,9 +25,11 @@
>
> static void guest_code(void)
> {
> + register u64 stage asm("11") = 0;
> +
> for (;;) {
> - asm volatile ("diag 0,0,0x501");
> - asm volatile ("ahi 11,1");
> + GUEST_SYNC(0);
> + asm volatile ("ahi %0,1" : : "r"(stage));
> }
> }
>
> --
> 2.21.0
>

Thanks,
drew


2019-07-31 11:02:30

by Thomas Huth

[permalink] [raw]
Subject: Re: [PATCH 1/2] KVM: selftests: Implement ucall() for s390x

On 30/07/2019 12.48, Andrew Jones wrote:
> On Tue, Jul 30, 2019 at 12:01:11PM +0200, Thomas Huth wrote:
>> On s390x, we can neither exit via PIO nor MMIO, but have to use
>> an instruction like DIAGNOSE. While we're at it, rename UCALL_PIO
>> to UCALL_DEFAULT, since PIO only works on x86 anyway, and this
>> way we can re-use the "default" type for the DIAGNOSE exit on s390x.
>>
>> Now that ucall() is implemented, we can use it in the sync_reg_test
>> on s390x, too.
>>
>> Signed-off-by: Thomas Huth <[email protected]>
>> ---
>> .../testing/selftests/kvm/include/kvm_util.h | 2 +-
>> tools/testing/selftests/kvm/lib/ucall.c | 34 +++++++++++++++----
>> .../selftests/kvm/s390x/sync_regs_test.c | 6 ++--
>> 3 files changed, 32 insertions(+), 10 deletions(-)
>>
>> diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h
>> index e0e66b115ef2..c37aea2e33e5 100644
>> --- a/tools/testing/selftests/kvm/include/kvm_util.h
>> +++ b/tools/testing/selftests/kvm/include/kvm_util.h
>> @@ -167,7 +167,7 @@ int vm_create_device(struct kvm_vm *vm, struct kvm_create_device *cd);
>>
>> /* ucall implementation types */
>> typedef enum {
>> - UCALL_PIO,
>> + UCALL_DEFAULT,
>
> I'd rather we keep explicit types defined; keep PIO and add DIAG. Then
> we can have
>
> /* Set default ucall types */
> #if defined(__x86_64__)
> ucall_type = UCALL_PIO;
> #elif defined(__aarch64__)
> ucall_type = UCALL_MMIO;
> ucall_requires_init = true;
> #elif defined(__s390x__)
> ucall_type = UCALL_DIAG;
> #endif
>
> And add an assert in get_ucall()
>
> assert(!ucall_requires_init || ucall_initialized);

I'm not sure whether I really like that. It's yet another additional
#ifdef block, and yet another variable ...

What do you think about removing the enum completely and simply code it
directly, without the ucall_type indirection, i.e.:

void ucall(uint64_t cmd, int nargs, ...)
{
struct ucall uc = {
.cmd = cmd,
};
va_list va;
int i;

nargs = nargs <= UCALL_MAX_ARGS ? nargs : UCALL_MAX_ARGS;

va_start(va, nargs);
for (i = 0; i < nargs; ++i)
uc.args[i] = va_arg(va, uint64_t);
va_end(va);

#if defined(__x86_64__)

/* Exit via PIO */
asm volatile("in %[port], %%al"
: : [port] "d" (UCALL_PIO_PORT), "D" (&uc) : "rax");

#elif defined(__aarch64__)

*ucall_exit_mmio_addr = (vm_vaddr_t)&uc;

#elif defined(__s390x__)

/* Exit via DIAGNOSE 0x501 (normally used for breakpoints) */
asm volatile ("diag 0,%0,0x501" : : "a"(&uc) : "memory");

#endif
}

I think that's way less confusing than having to understand the meaning
of ucall_type etc. before...?

Thomas

2019-07-31 11:25:53

by Andrew Jones

[permalink] [raw]
Subject: Re: [PATCH 1/2] KVM: selftests: Implement ucall() for s390x

On Wed, Jul 31, 2019 at 11:43:16AM +0200, Thomas Huth wrote:
> On 30/07/2019 12.48, Andrew Jones wrote:
> > On Tue, Jul 30, 2019 at 12:01:11PM +0200, Thomas Huth wrote:
> >> On s390x, we can neither exit via PIO nor MMIO, but have to use
> >> an instruction like DIAGNOSE. While we're at it, rename UCALL_PIO
> >> to UCALL_DEFAULT, since PIO only works on x86 anyway, and this
> >> way we can re-use the "default" type for the DIAGNOSE exit on s390x.
> >>
> >> Now that ucall() is implemented, we can use it in the sync_reg_test
> >> on s390x, too.
> >>
> >> Signed-off-by: Thomas Huth <[email protected]>
> >> ---
> >> .../testing/selftests/kvm/include/kvm_util.h | 2 +-
> >> tools/testing/selftests/kvm/lib/ucall.c | 34 +++++++++++++++----
> >> .../selftests/kvm/s390x/sync_regs_test.c | 6 ++--
> >> 3 files changed, 32 insertions(+), 10 deletions(-)
> >>
> >> diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h
> >> index e0e66b115ef2..c37aea2e33e5 100644
> >> --- a/tools/testing/selftests/kvm/include/kvm_util.h
> >> +++ b/tools/testing/selftests/kvm/include/kvm_util.h
> >> @@ -167,7 +167,7 @@ int vm_create_device(struct kvm_vm *vm, struct kvm_create_device *cd);
> >>
> >> /* ucall implementation types */
> >> typedef enum {
> >> - UCALL_PIO,
> >> + UCALL_DEFAULT,
> >
> > I'd rather we keep explicit types defined; keep PIO and add DIAG. Then
> > we can have
> >
> > /* Set default ucall types */
> > #if defined(__x86_64__)
> > ucall_type = UCALL_PIO;
> > #elif defined(__aarch64__)
> > ucall_type = UCALL_MMIO;
> > ucall_requires_init = true;
> > #elif defined(__s390x__)
> > ucall_type = UCALL_DIAG;
> > #endif
> >
> > And add an assert in get_ucall()
> >
> > assert(!ucall_requires_init || ucall_initialized);
>
> I'm not sure whether I really like that. It's yet another additional
> #ifdef block, and yet another variable ...
>
> What do you think about removing the enum completely and simply code it
> directly, without the ucall_type indirection, i.e.:
>
> void ucall(uint64_t cmd, int nargs, ...)
> {
> struct ucall uc = {
> .cmd = cmd,
> };
> va_list va;
> int i;
>
> nargs = nargs <= UCALL_MAX_ARGS ? nargs : UCALL_MAX_ARGS;
>
> va_start(va, nargs);
> for (i = 0; i < nargs; ++i)
> uc.args[i] = va_arg(va, uint64_t);
> va_end(va);
>
> #if defined(__x86_64__)
>
> /* Exit via PIO */
> asm volatile("in %[port], %%al"
> : : [port] "d" (UCALL_PIO_PORT), "D" (&uc) : "rax");
>
> #elif defined(__aarch64__)
>
> *ucall_exit_mmio_addr = (vm_vaddr_t)&uc;
>
> #elif defined(__s390x__)
>
> /* Exit via DIAGNOSE 0x501 (normally used for breakpoints) */
> asm volatile ("diag 0,%0,0x501" : : "a"(&uc) : "memory");
>
> #endif
> }
>
> I think that's way less confusing than having to understand the meaning
> of ucall_type etc. before...?
>

Sounds good to me.

Thanks,
drew

2019-07-31 11:31:54

by Thomas Huth

[permalink] [raw]
Subject: Re: [PATCH 1/2] KVM: selftests: Implement ucall() for s390x

On 31/07/2019 12.28, Andrew Jones wrote:
> On Wed, Jul 31, 2019 at 11:43:16AM +0200, Thomas Huth wrote:
>> On 30/07/2019 12.48, Andrew Jones wrote:
>>> On Tue, Jul 30, 2019 at 12:01:11PM +0200, Thomas Huth wrote:
>>>> On s390x, we can neither exit via PIO nor MMIO, but have to use
>>>> an instruction like DIAGNOSE. While we're at it, rename UCALL_PIO
>>>> to UCALL_DEFAULT, since PIO only works on x86 anyway, and this
>>>> way we can re-use the "default" type for the DIAGNOSE exit on s390x.
>>>>
>>>> Now that ucall() is implemented, we can use it in the sync_reg_test
>>>> on s390x, too.
>>>>
>>>> Signed-off-by: Thomas Huth <[email protected]>
>>>> ---
>>>> .../testing/selftests/kvm/include/kvm_util.h | 2 +-
>>>> tools/testing/selftests/kvm/lib/ucall.c | 34 +++++++++++++++----
>>>> .../selftests/kvm/s390x/sync_regs_test.c | 6 ++--
>>>> 3 files changed, 32 insertions(+), 10 deletions(-)
>>>>
>>>> diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h
>>>> index e0e66b115ef2..c37aea2e33e5 100644
>>>> --- a/tools/testing/selftests/kvm/include/kvm_util.h
>>>> +++ b/tools/testing/selftests/kvm/include/kvm_util.h
>>>> @@ -167,7 +167,7 @@ int vm_create_device(struct kvm_vm *vm, struct kvm_create_device *cd);
>>>>
>>>> /* ucall implementation types */
>>>> typedef enum {
>>>> - UCALL_PIO,
>>>> + UCALL_DEFAULT,
>>>
>>> I'd rather we keep explicit types defined; keep PIO and add DIAG. Then
>>> we can have
>>>
>>> /* Set default ucall types */
>>> #if defined(__x86_64__)
>>> ucall_type = UCALL_PIO;
>>> #elif defined(__aarch64__)
>>> ucall_type = UCALL_MMIO;
>>> ucall_requires_init = true;
>>> #elif defined(__s390x__)
>>> ucall_type = UCALL_DIAG;
>>> #endif
>>>
>>> And add an assert in get_ucall()
>>>
>>> assert(!ucall_requires_init || ucall_initialized);
>>
>> I'm not sure whether I really like that. It's yet another additional
>> #ifdef block, and yet another variable ...
>>
>> What do you think about removing the enum completely and simply code it
>> directly, without the ucall_type indirection, i.e.:
>>
>> void ucall(uint64_t cmd, int nargs, ...)
>> {
>> struct ucall uc = {
>> .cmd = cmd,
>> };
>> va_list va;
>> int i;
>>
>> nargs = nargs <= UCALL_MAX_ARGS ? nargs : UCALL_MAX_ARGS;
>>
>> va_start(va, nargs);
>> for (i = 0; i < nargs; ++i)
>> uc.args[i] = va_arg(va, uint64_t);
>> va_end(va);
>>
>> #if defined(__x86_64__)
>>
>> /* Exit via PIO */
>> asm volatile("in %[port], %%al"
>> : : [port] "d" (UCALL_PIO_PORT), "D" (&uc) : "rax");
>>
>> #elif defined(__aarch64__)
>>
>> *ucall_exit_mmio_addr = (vm_vaddr_t)&uc;
>>
>> #elif defined(__s390x__)
>>
>> /* Exit via DIAGNOSE 0x501 (normally used for breakpoints) */
>> asm volatile ("diag 0,%0,0x501" : : "a"(&uc) : "memory");
>>
>> #endif
>> }
>>
>> I think that's way less confusing than having to understand the meaning
>> of ucall_type etc. before...?
>>
>
> Sounds good to me.

Or maybe even better: Let's move this file into lib/x86_64/ and
lib/aarch64/ instead, since there is more different code between the
architectures here than common code.

Thomas

2019-07-31 13:01:38

by Paolo Bonzini

[permalink] [raw]
Subject: Re: [PATCH 1/2] KVM: selftests: Implement ucall() for s390x

On 31/07/19 13:16, Thomas Huth wrote:
> Or maybe even better: Let's move this file into lib/x86_64/ and
> lib/aarch64/ instead, since there is more different code between the
> architectures here than common code.

All good solutions, just choose one. :))

Paolo

2019-07-31 13:06:10

by Andrew Jones

[permalink] [raw]
Subject: Re: [PATCH 1/2] KVM: selftests: Implement ucall() for s390x

On Wed, Jul 31, 2019 at 02:57:38PM +0200, Paolo Bonzini wrote:
> On 31/07/19 13:16, Thomas Huth wrote:
> > Or maybe even better: Let's move this file into lib/x86_64/ and
> > lib/aarch64/ instead, since there is more different code between the
> > architectures here than common code.
>
> All good solutions, just choose one. :))
>

Agreed, and I like this last solution (move to arch-code) the best.

Thanks,
drew