[RFC PATCH v1 04/10] vpmu.c: factor out register conversion

Edwin Török posted 10 patches 6 months, 2 weeks ago
[RFC PATCH v1 04/10] vpmu.c: factor out register conversion
Posted by Edwin Török 6 months, 2 weeks ago
A followup commit will use this to store the guest's regs when domid ==
DOMID_XEN.
To avoid code duplication move the code into a function.

No functional change.

Signed-off-by: Edwin Török <edwin.torok@cloud.com>
---
 xen/arch/x86/cpu/vpmu.c | 49 ++++++++++++++++++++++-------------------
 1 file changed, 26 insertions(+), 23 deletions(-)

diff --git a/xen/arch/x86/cpu/vpmu.c b/xen/arch/x86/cpu/vpmu.c
index 7be79c2d00..713311a1ac 100644
--- a/xen/arch/x86/cpu/vpmu.c
+++ b/xen/arch/x86/cpu/vpmu.c
@@ -160,6 +160,31 @@ static inline struct vcpu *choose_hwdom_vcpu(void)
     return hardware_domain->vcpu[idx];
 }
 
+static inline void vpmu_convert_regs(struct xen_pmu_regs *r, uint64_t *flags,
+                                     struct vcpu *sampled,
+                                     const struct cpu_user_regs *cur_regs) {
+  r->ip = cur_regs->rip;
+  r->sp = cur_regs->rsp;
+  r->flags = cur_regs->rflags;
+
+  if (!is_hvm_vcpu(sampled)) {
+    r->ss = cur_regs->ss;
+    r->cs = cur_regs->cs;
+    if (!(sampled->arch.flags & TF_kernel_mode))
+      *flags |= PMU_SAMPLE_USER;
+  } else {
+    struct segment_register seg;
+
+    hvm_get_segment_register(sampled, x86_seg_cs, &seg);
+    r->cs = seg.sel;
+    hvm_get_segment_register(sampled, x86_seg_ss, &seg);
+    r->ss = seg.sel;
+    r->cpl = seg.dpl;
+    if (!(sampled->arch.hvm.guest_cr[0] & X86_CR0_PE))
+      *flags |= PMU_SAMPLE_REAL;
+  }
+}
+
 void vpmu_do_interrupt(void)
 {
     struct vcpu *sampled = current, *sampling;
@@ -255,29 +280,7 @@ void vpmu_do_interrupt(void)
             else
                 cur_regs = guest_cpu_user_regs();
 
-            r->ip = cur_regs->rip;
-            r->sp = cur_regs->rsp;
-            r->flags = cur_regs->rflags;
-
-            if ( !is_hvm_vcpu(sampled) )
-            {
-                r->ss = cur_regs->ss;
-                r->cs = cur_regs->cs;
-                if ( !(sampled->arch.flags & TF_kernel_mode) )
-                    *flags |= PMU_SAMPLE_USER;
-            }
-            else
-            {
-                struct segment_register seg;
-
-                hvm_get_segment_register(sampled, x86_seg_cs, &seg);
-                r->cs = seg.sel;
-                hvm_get_segment_register(sampled, x86_seg_ss, &seg);
-                r->ss = seg.sel;
-                r->cpl = seg.dpl;
-                if ( !(sampled->arch.hvm.guest_cr[0] & X86_CR0_PE) )
-                    *flags |= PMU_SAMPLE_REAL;
-            }
+            vpmu_convert_regs(r, flags, sampled, cur_regs);
         }
 
         vpmu->xenpmu_data->domain_id = domid;
-- 
2.47.1


Re: [RFC PATCH v1 04/10] vpmu.c: factor out register conversion
Posted by Andrew Cooper 6 months, 1 week ago
On 25/07/2025 4:06 pm, Edwin Török wrote:
> diff --git a/xen/arch/x86/cpu/vpmu.c b/xen/arch/x86/cpu/vpmu.c
> index 7be79c2d00..713311a1ac 100644
> --- a/xen/arch/x86/cpu/vpmu.c
> +++ b/xen/arch/x86/cpu/vpmu.c
> @@ -160,6 +160,31 @@ static inline struct vcpu *choose_hwdom_vcpu(void)
>      return hardware_domain->vcpu[idx];
>  }
>  
> +static inline void vpmu_convert_regs(struct xen_pmu_regs *r, uint64_t *flags,
> +                                     struct vcpu *sampled,
> +                                     const struct cpu_user_regs *cur_regs) {
> +  r->ip = cur_regs->rip;
> +  r->sp = cur_regs->rsp;
> +  r->flags = cur_regs->rflags;
> +
> +  if (!is_hvm_vcpu(sampled)) {
> +    r->ss = cur_regs->ss;
> +    r->cs = cur_regs->cs;
> +    if (!(sampled->arch.flags & TF_kernel_mode))
> +      *flags |= PMU_SAMPLE_USER;
> +  } else {
> +    struct segment_register seg;
> +
> +    hvm_get_segment_register(sampled, x86_seg_cs, &seg);
> +    r->cs = seg.sel;
> +    hvm_get_segment_register(sampled, x86_seg_ss, &seg);
> +    r->ss = seg.sel;
> +    r->cpl = seg.dpl;
> +    if (!(sampled->arch.hvm.guest_cr[0] & X86_CR0_PE))
> +      *flags |= PMU_SAMPLE_REAL;
> +  }
> +}
> +

This is fine in principle, except that you're changing the style away
from Xen style.

I can fix it on commit, but it's going to collide massively later in the
series.

~Andrew

Re: [RFC PATCH v1 04/10] vpmu.c: factor out register conversion
Posted by Edwin Torok 6 months, 1 week ago
On Mon, Jul 28, 2025 at 11:25 AM Andrew Cooper
<andrew.cooper3@citrix.com> wrote:
>
> On 25/07/2025 4:06 pm, Edwin Török wrote:
> > diff --git a/xen/arch/x86/cpu/vpmu.c b/xen/arch/x86/cpu/vpmu.c
> > index 7be79c2d00..713311a1ac 100644
> > --- a/xen/arch/x86/cpu/vpmu.c
> > +++ b/xen/arch/x86/cpu/vpmu.c
> > @@ -160,6 +160,31 @@ static inline struct vcpu *choose_hwdom_vcpu(void)
> >      return hardware_domain->vcpu[idx];
> >  }
> >
> > +static inline void vpmu_convert_regs(struct xen_pmu_regs *r, uint64_t *flags,
> > +                                     struct vcpu *sampled,
> > +                                     const struct cpu_user_regs *cur_regs) {
> > +  r->ip = cur_regs->rip;
> > +  r->sp = cur_regs->rsp;
> > +  r->flags = cur_regs->rflags;
> > +
> > +  if (!is_hvm_vcpu(sampled)) {
> > +    r->ss = cur_regs->ss;
> > +    r->cs = cur_regs->cs;
> > +    if (!(sampled->arch.flags & TF_kernel_mode))
> > +      *flags |= PMU_SAMPLE_USER;
> > +  } else {
> > +    struct segment_register seg;
> > +
> > +    hvm_get_segment_register(sampled, x86_seg_cs, &seg);
> > +    r->cs = seg.sel;
> > +    hvm_get_segment_register(sampled, x86_seg_ss, &seg);
> > +    r->ss = seg.sel;
> > +    r->cpl = seg.dpl;
> > +    if (!(sampled->arch.hvm.guest_cr[0] & X86_CR0_PE))
> > +      *flags |= PMU_SAMPLE_REAL;
> > +  }
> > +}
> > +
>
> This is fine in principle, except that you're changing the style away
> from Xen style.

Ah looks like we lack a .clang-format file, so `gq` reformatted the
code with Clang's default settings.
I'll avoid using `gq` for now, and have redone the commit using '<' instead.

>
> I can fix it on commit, but it's going to collide massively later in the
> series.

I've pushed a WiP branch which contains this change, and fixed the
rebase conflicts in the followup patches:
https://gitlab.com/xen-project/people/edwintorok/xen/-/commits/pmustack-next?ref_type=heads

Best regards,
--Edwin
>
> ~Andrew