All of lore.kernel.org
 help / color / mirror / Atom feed
From: Magnus Kulke <magnuskulke@linux.microsoft.com>
To: Aastha Rawat <aastharawat@linux.microsoft.com>
Cc: qemu-devel@nongnu.org, "Wei Liu" <wei.liu@kernel.org>,
	"Doru Blânzeanu" <dblanzeanu@linux.microsoft.com>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Marc-André Lureau" <marcandre.lureau@redhat.com>,
	"Daniel P. Berrangé" <berrange@redhat.com>,
	"Pierrick Bouvier" <pierrick.bouvier@oss.qualcomm.com>,
	"Peter Maydell" <peter.maydell@linaro.org>,
	"Anirudh Rayabharam" <anirudh@anirudhrb.com>,
	qemu-arm@nongnu.org, "Alexander Graf" <agraf@csgraf.de>,
	"Pedro Barbuda" <pbarbuda@microsoft.com>,
	"Mohamed Mediouni" <mohamed@unpredictable.fr>,
	"Michael S. Tsirkin" <mst@redhat.com>,
	"Richard Henderson" <richard.henderson@linaro.org>,
	"Philippe Mathieu-Daudé" <philmd@oss.qualcomm.com>
Subject: Re: [PATCH v6 01/14] accel/mshv: extract common CPU register helpers
Date: Thu, 19 Nov 2026 14:41:11 +0100	[thread overview]
Message-ID: <av78d3TLGnB94nZM@example.com> (raw)
In-Reply-To: <20261001-mshv_accel_arm64_supp-v6-1-aefe5e76a0e3@linux.microsoft.com>

On Thu, Oct 01, 2026 at 10:52:46AM +0000, Aastha Rawat wrote:
> Move arch-independent MSHV CPU register handling functions into
> accel/mshv/mshv-cpu-common.c. Update x86 MSHV backends to use these
> shared helpers.
> 
> Prefix get_generic_regs() with mshv_ for proper namespacing since it
> is now public.
> 
> Signed-off-by: Aastha Rawat <aastharawat@linux.microsoft.com>
> ---
>  accel/mshv/meson.build       |   3 +-
>  accel/mshv/mshv-cpu-common.c | 151 +++++++++++++++++++++++++++++++++++++++++++
>  include/system/mshv_int.h    |   1 +
>  target/i386/mshv/mshv-cpu.c  | 125 +----------------------------------
>  4 files changed, 155 insertions(+), 125 deletions(-)
> 
> diff --git a/accel/mshv/meson.build b/accel/mshv/meson.build
> index e433187cde0..ebe32921b3d 100644
> --- a/accel/mshv/meson.build
> +++ b/accel/mshv/meson.build
> @@ -1,5 +1,6 @@
>  system_ss.add(when: 'CONFIG_MSHV', if_true: files(
>    'irq.c',
>    'mem.c',
> -  'mshv-all.c'
> +  'mshv-all.c',
> +  'mshv-cpu-common.c'
>  ))
> diff --git a/accel/mshv/mshv-cpu-common.c b/accel/mshv/mshv-cpu-common.c
> new file mode 100644
> index 00000000000..e8ef34689e3
> --- /dev/null
> +++ b/accel/mshv/mshv-cpu-common.c
> @@ -0,0 +1,151 @@
> +/*
> + * QEMU MSHV common CPU register helpers
> + *
> + * Copyright Microsoft, Corp. 2026
> + *
> + * Authors: Ziqiao Zhou   <ziqiaozhou@microsoft.com>
> + *          Magnus Kulke  <magnuskulke@microsoft.com>
> + *          Jinank Jain   <jinankjain@microsoft.com>
> + *
> + * SPDX-License-Identifier: GPL-2.0-or-later
> + */
> +#include "qemu/osdep.h"
> +#include "qemu/error-report.h"
> +#include "qemu/memalign.h"
> +
> +#include "system/mshv.h"
> +#include "linux/mshv.h"
> +#include "system/mshv_int.h"
> +
> +#include "hw/core/cpu.h"
> +
> +#include <sys/ioctl.h>
> +
> +int mshv_set_generic_regs(const CPUState *cpu, const hv_register_assoc *assocs,
> +                          size_t n_regs)
> +{
> +    int cpu_fd = mshv_vcpufd(cpu);
> +    int vp_index = cpu->cpu_index;
> +    size_t in_sz, assocs_sz;
> +    hv_input_set_vp_registers *in = cpu->accel->hvcall_args.input_page;
> +    struct mshv_root_hvcall args = {0};
> +    int ret;
> +
> +    /* find out the size of the struct w/ a flexible array at the tail */
> +    assocs_sz = n_regs * sizeof(hv_register_assoc);
> +    in_sz = sizeof(hv_input_set_vp_registers) + assocs_sz;
> +
> +    /* fill the input struct */
> +    memset(in, 0, sizeof(hv_input_set_vp_registers));
> +    in->vp_index = vp_index;
> +    memcpy(in->elements, assocs, assocs_sz);
> +
> +    /* create the hvcall envelope */
> +    args.code = HVCALL_SET_VP_REGISTERS;
> +    args.in_sz = in_sz;
> +    args.in_ptr = (uint64_t) in;
> +    args.reps = (uint16_t) n_regs;
> +
> +    /* perform the call */
> +    ret = mshv_hvcall(cpu_fd, &args);
> +    if (ret < 0) {
> +        error_report("Failed to set registers");
> +        return -1;
> +    }
> +
> +    /* assert we set all registers */
> +    if (args.reps != n_regs) {
> +        error_report("Failed to set registers: expected %zu elements"
> +                     ", got %u", n_regs, args.reps);
> +        return -1;
> +    }
> +
> +    return 0;
> +}
> +
> +int mshv_get_generic_regs(CPUState *cpu, hv_register_assoc *assocs,
> +                            size_t n_regs)
> +{
> +    int cpu_fd = mshv_vcpufd(cpu);
> +    int vp_index = cpu->cpu_index;
> +    hv_input_get_vp_registers *in = cpu->accel->hvcall_args.input_page;
> +    hv_register_value *values = cpu->accel->hvcall_args.output_page;
> +    size_t in_sz, names_sz, values_sz;
> +    int i, ret;
> +    struct mshv_root_hvcall args = {0};
> +
> +    /* find out the size of the struct w/ a flexible array at the tail */
> +    names_sz = n_regs * sizeof(hv_register_name);
> +    in_sz = sizeof(hv_input_get_vp_registers) + names_sz;
> +
> +    /* fill the input struct */
> +    memset(in, 0, sizeof(hv_input_get_vp_registers));
> +    in->vp_index = vp_index;
> +    for (i = 0; i < n_regs; i++) {
> +        in->names[i] = assocs[i].name;
> +    }
> +
> +    /* determine size of value output buffer */
> +    values_sz = n_regs * sizeof(union hv_register_value);
> +
> +    /* create the hvcall envelope */
> +    args.code = HVCALL_GET_VP_REGISTERS;
> +    args.in_sz = in_sz;
> +    args.in_ptr = (uint64_t) in;
> +    args.out_sz = values_sz;
> +    args.out_ptr = (uint64_t) values;
> +    args.reps = (uint16_t) n_regs;
> +
> +    /* perform the call */
> +    ret = mshv_hvcall(cpu_fd, &args);
> +    if (ret < 0) {
> +        error_report("Failed to retrieve registers");
> +        return -1;
> +    }
> +
> +    /* assert we got all registers */
> +    if (args.reps != n_regs) {
> +        error_report("Failed to retrieve registers: expected %zu elements"
> +                     ", got %u", n_regs, args.reps);
> +        return -1;
> +    }
> +
> +    /* copy values into assoc */
> +    for (i = 0; i < n_regs; i++) {
> +        assocs[i].value = values[i];
> +    }
> +
> +    return 0;
> +}
> +
> +int mshv_create_vcpu(int vm_fd, uint8_t vp_index, int *cpu_fd)
> +{
> +    int ret;
> +    struct mshv_create_vp vp_arg = {
> +        .vp_index = vp_index,
> +    };
> +
> +    ret = ioctl(vm_fd, MSHV_CREATE_VP, &vp_arg);
> +    if (ret < 0) {
> +        error_report("failed to create mshv vcpu: %s", strerror(errno));
> +        return -1;
> +    }
> +
> +    *cpu_fd = ret;
> +
> +    return 0;
> +}
> +
> +void mshv_remove_vcpu(int vm_fd, int cpu_fd)
> +{
> +    close(cpu_fd);
> +}
> +
> +void mshv_setup_hvcall_args(AccelCPUState *state)
> +{
> +    void *mem = qemu_memalign(HV_HYP_PAGE_SIZE, 2 * HV_HYP_PAGE_SIZE);
> +
> +    state->hvcall_args.base = mem;
> +    state->hvcall_args.input_page = mem;
> +    state->hvcall_args.output_page = (uint8_t *)mem + HV_HYP_PAGE_SIZE;
> +}
> diff --git a/include/system/mshv_int.h b/include/system/mshv_int.h
> index 3dffe3c5fbf..2e8caad8fde 100644
> --- a/include/system/mshv_int.h
> +++ b/include/system/mshv_int.h
> @@ -107,6 +107,7 @@ void mshv_arch_amend_proc_features(
>  void mshv_arch_disable_partition_proc_features(
>       union hv_partition_processor_features *disabled_features);
>  int mshv_arch_post_init_vm(int vm_fd);
> +void mshv_setup_hvcall_args(AccelCPUState *state);
>  int mshv_get_vp_state(int cpu_fd, struct mshv_get_set_vp_state *state);
>  int mshv_set_vp_state(int cpu_fd, const struct mshv_get_set_vp_state *state);
>  typedef struct mshv_root_hvcall mshv_root_hvcall;
> diff --git a/target/i386/mshv/mshv-cpu.c b/target/i386/mshv/mshv-cpu.c
> index f528dd2b9a2..3eee097432a 100644
> --- a/target/i386/mshv/mshv-cpu.c
> +++ b/target/i386/mshv/mshv-cpu.c
> @@ -357,103 +357,6 @@ static int translate_gva(const CPUState *cpu, uint64_t gva, uint64_t *gpa,
>      return 0;
>  }
>  
> -int mshv_set_generic_regs(const CPUState *cpu, const hv_register_assoc *assocs,
> -                          size_t n_regs)
> -{
> -    int cpu_fd = mshv_vcpufd(cpu);
> -    int vp_index = cpu->cpu_index;
> -    size_t in_sz, assocs_sz;
> -    hv_input_set_vp_registers *in = cpu->accel->hvcall_args.input_page;
> -    struct mshv_root_hvcall args = {0};
> -    int ret;
> -
> -    /* find out the size of the struct w/ a flexible array at the tail */
> -    assocs_sz = n_regs * sizeof(hv_register_assoc);
> -    in_sz = sizeof(hv_input_set_vp_registers) + assocs_sz;
> -
> -    /* fill the input struct */
> -    memset(in, 0, sizeof(hv_input_set_vp_registers));
> -    in->vp_index = vp_index;
> -    memcpy(in->elements, assocs, assocs_sz);
> -
> -    /* create the hvcall envelope */
> -    args.code = HVCALL_SET_VP_REGISTERS;
> -    args.in_sz = in_sz;
> -    args.in_ptr = (uint64_t) in;
> -    args.reps = (uint16_t) n_regs;
> -
> -    /* perform the call */
> -    ret = mshv_hvcall(cpu_fd, &args);
> -    if (ret < 0) {
> -        error_report("Failed to set registers");
> -        return -1;
> -    }
> -
> -    /* assert we set all registers */
> -    if (args.reps != n_regs) {
> -        error_report("Failed to set registers: expected %zu elements"
> -                     ", got %u", n_regs, args.reps);
> -        return -1;
> -    }
> -
> -    return 0;
> -}
> -
> -int mshv_get_generic_regs(CPUState *cpu, hv_register_assoc *assocs,
> -                          size_t n_regs)
> -{
> -    int cpu_fd = mshv_vcpufd(cpu);
> -    int vp_index = cpu->cpu_index;
> -    hv_input_get_vp_registers *in = cpu->accel->hvcall_args.input_page;
> -    hv_register_value *values = cpu->accel->hvcall_args.output_page;
> -    size_t in_sz, names_sz, values_sz;
> -    int i, ret;
> -    struct mshv_root_hvcall args = {0};
> -
> -    /* find out the size of the struct w/ a flexible array at the tail */
> -    names_sz = n_regs * sizeof(hv_register_name);
> -    in_sz = sizeof(hv_input_get_vp_registers) + names_sz;
> -
> -    /* fill the input struct */
> -    memset(in, 0, sizeof(hv_input_get_vp_registers));
> -    in->vp_index = vp_index;
> -    for (i = 0; i < n_regs; i++) {
> -        in->names[i] = assocs[i].name;
> -    }
> -
> -    /* determine size of value output buffer */
> -    values_sz = n_regs * sizeof(union hv_register_value);
> -
> -    /* create the hvcall envelope */
> -    args.code = HVCALL_GET_VP_REGISTERS;
> -    args.in_sz = in_sz;
> -    args.in_ptr = (uint64_t) in;
> -    args.out_sz = values_sz;
> -    args.out_ptr = (uint64_t) values;
> -    args.reps = (uint16_t) n_regs;
> -
> -    /* perform the call */
> -    ret = mshv_hvcall(cpu_fd, &args);
> -    if (ret < 0) {
> -        error_report("Failed to retrieve registers");
> -        return -1;
> -    }
> -
> -    /* assert we got all registers */
> -    if (args.reps != n_regs) {
> -        error_report("Failed to retrieve registers: expected %zu elements"
> -                     ", got %u", n_regs, args.reps);
> -        return -1;
> -    }
> -
> -    /* copy values into assoc */
> -    for (i = 0; i < n_regs; i++) {
> -        assocs[i].value = values[i];
> -    }
> -
> -    return 0;
> -}
> -
>  static int set_standard_regs(const CPUState *cpu)
>  {
>      X86CPU *x86cpu = X86_CPU(cpu);
> @@ -2040,29 +1943,6 @@ int mshv_run_vcpu(int vm_fd, CPUState *cpu, hv_message *msg, MshvVmExit *exit)
>      return 0;
>  }
>  
> -void mshv_remove_vcpu(int vm_fd, int cpu_fd)
> -{
> -    close(cpu_fd);
> -}
> -
> -
> -int mshv_create_vcpu(int vm_fd, uint8_t vp_index, int *cpu_fd)
> -{
> -    int ret;
> -    struct mshv_create_vp vp_arg = {
> -        .vp_index = vp_index,
> -    };
> -    ret = ioctl(vm_fd, MSHV_CREATE_VP, &vp_arg);
> -    if (ret < 0) {
> -        error_report("failed to create mshv vcpu: %s", strerror(errno));
> -        return -1;
> -    }
> -
> -    *cpu_fd = ret;
> -
> -    return 0;
> -}
> -
>  static void read_segment_descriptor(CPUState *cpu,
>                                      struct x86_segment_descriptor *desc,
>                                      enum X86Seg seg_idx)
> @@ -2121,7 +2001,6 @@ void mshv_arch_init_vcpu(CPUState *cpu)
>      CPUX86State *env = &x86_cpu->env;
>      AccelCPUState *state = cpu->accel;
>      size_t page = HV_HYP_PAGE_SIZE, xsave_len;
> -    void *mem = qemu_memalign(page, 2 * page);
>      int ret;
>      X86XSaveHeader *header;
>  
> @@ -2143,9 +2022,7 @@ void mshv_arch_init_vcpu(CPUState *cpu)
>          env->regs_page = (struct hv_vp_register_page *) rp;
>      }
>  
> -    state->hvcall_args.base = mem;
> -    state->hvcall_args.input_page = mem;
> -    state->hvcall_args.output_page = (uint8_t *)mem + page;
> +    mshv_setup_hvcall_args(state);
>  
>      env->emu_mmio_buf = g_new(char, 4096);
>  
> 
> -- 
> 2.45.4

Reviewed-by: Magnus Kulke <magnuskulke@linux.microsoft.com>


  reply	other threads:[~2026-11-19 13:41 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-01 10:52 [PATCH v6 00/14] Add ARM64 support for MSHV accelerator Aastha Rawat
2026-10-01 10:52 ` [PATCH v6 01/14] accel/mshv: extract common CPU register helpers Aastha Rawat
2026-11-19 13:41   ` Magnus Kulke [this message]
2026-10-01 10:52 ` [PATCH v6 02/14] accel/mshv: add arch-specific accelerator init hook Aastha Rawat
2026-11-19 14:57   ` Magnus Kulke
2026-11-19 15:06   ` Magnus Kulke
2026-10-01 10:52 ` [PATCH v6 03/14] accel/mshv: make MSHV migration initialization arch-specific Aastha Rawat
2026-11-13 11:37   ` Doru Blânzeanu
2026-11-19 15:21   ` Magnus Kulke
2026-11-19 15:24   ` Magnus Kulke
2026-10-01 10:52 ` [PATCH v6 04/14] meson, target/arm/mshv: Enable arm64 build & add initial MSHV support Aastha Rawat
2026-10-13 16:48   ` Peter Maydell
2026-11-13 12:23   ` Doru Blânzeanu
2026-11-13 12:56   ` Doru Blânzeanu
2026-11-19 15:40   ` Magnus Kulke
2026-10-01 10:52 ` [PATCH v6 05/14] target/arm/mshv: implement vcpu state operations for ARM64 Aastha Rawat
2026-11-13 15:05   ` Doru Blânzeanu
2026-11-19 16:15   ` Magnus Kulke
2026-10-01 10:52 ` [PATCH v6 06/14] target/arm: convert host CPU feature ifdef ladder to runtime checks Aastha Rawat
2026-10-13 16:46   ` Peter Maydell
2026-10-23 14:37     ` Aastha Rawat
2026-11-19 16:26   ` Magnus Kulke
2026-10-01 10:52 ` [PATCH v6 07/14] target/arm/mshv: implement -cpu host for MSHV Aastha Rawat
2026-11-20 13:20   ` Magnus Kulke
2026-10-01 10:52 ` [PATCH v6 08/14] accel/mshv: Add access_vp_regs synthetic proc features Aastha Rawat
2026-11-20 13:35   ` Magnus Kulke
2026-10-01 10:52 ` [PATCH v6 09/14] target/arm: cpu: Mark MSHV supporting PSCI 1.3 Aastha Rawat
2026-11-20 13:48   ` Magnus Kulke
2026-10-01 10:52 ` [PATCH v6 10/14] target/arm: extract MMIO emulation logic for HVF & WHPX Aastha Rawat
2026-10-13 16:40   ` Peter Maydell
2026-10-22 10:27     ` Aastha Rawat
2026-11-20 14:13   ` Magnus Kulke
2026-10-01 10:52 ` [PATCH v6 11/14] target/arm/mshv: add vCPU run loop Aastha Rawat
2026-10-13 16:50   ` Peter Maydell
2026-10-16  9:56     ` Anirudh Rayabharam
2026-11-20 15:37   ` Magnus Kulke
2026-10-01 10:52 ` [PATCH v6 12/14] include/hw/hyperv: adjust hv_interrupt_control structure for arm64 Aastha Rawat
2026-11-20 15:41   ` Magnus Kulke
2026-10-01 10:52 ` [PATCH v6 13/14] hw/intc,target/arm/mshv: add MSHV vGICv3 implementation Aastha Rawat
2026-11-20 16:25   ` Magnus Kulke
2026-10-01 10:52 ` [PATCH v6 14/14] MAINTAINERS: updates for MSHV arm64 code Aastha Rawat
2026-11-20 16:27   ` Magnus Kulke

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=av78d3TLGnB94nZM@example.com \
    --to=magnuskulke@linux.microsoft.com \
    --cc=aastharawat@linux.microsoft.com \
    --cc=agraf@csgraf.de \
    --cc=anirudh@anirudhrb.com \
    --cc=berrange@redhat.com \
    --cc=dblanzeanu@linux.microsoft.com \
    --cc=marcandre.lureau@redhat.com \
    --cc=mohamed@unpredictable.fr \
    --cc=mst@redhat.com \
    --cc=pbarbuda@microsoft.com \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=philmd@oss.qualcomm.com \
    --cc=pierrick.bouvier@oss.qualcomm.com \
    --cc=qemu-arm@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=richard.henderson@linaro.org \
    --cc=wei.liu@kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.