All of lore.kernel.org
 help / color / mirror / Atom feed
From: Razvan Cojocaru <rcojocaru@bitdefender.com>
To: Andrew Cooper <andrew.cooper3@citrix.com>, xen-devel@lists.xen.org
Cc: tim@xen.org
Subject: Re: [PATCH RFC 2/9] xen: Optimize introspection access to guest state
Date: Thu, 10 Jul 2014 14:57:57 +0300	[thread overview]
Message-ID: <53BE7FC5.8060907@bitdefender.com> (raw)
In-Reply-To: <53BE4C36.4000805@citrix.com>

On 07/10/2014 11:17 AM, Andrew Cooper wrote:
> On 10/07/2014 09:05, Razvan Cojocaru wrote:
>> On 07/02/2014 06:31 PM, Andrew Cooper wrote:
>>> On 02/07/14 14:33, Razvan Cojocaru wrote:
>>>> diff --git a/xen/arch/x86/hvm/vmx/vmx.c b/xen/arch/x86/hvm/vmx/vmx.c
>>>> index 2caa04a..fed21b6 100644
>>>> --- a/xen/arch/x86/hvm/vmx/vmx.c
>>>> +++ b/xen/arch/x86/hvm/vmx/vmx.c
>>>> @@ -425,6 +425,7 @@ static void vmx_vmcs_save(struct vcpu *v, struct hvm_hw_cpu *c)
>>>>      c->cr4 = v->arch.hvm_vcpu.guest_cr[4];
>>>>  
>>>>      c->msr_efer = v->arch.hvm_vcpu.guest_efer;
>>>> +    c->guest_x86_mode = vmx_guest_x86_mode(v);
>>> guest_x86_mode is a linear function of cr0, eflags and efer.  It can be
>>> calculated by userspace doesn't need to transmitted individually.
>> OK, but 1) I'm not sending eflags into userspace,
> 
> rflags is in the structure between r15 and dr7.
> 
>>  and 2) I thought Xen's
>> vmx_guest_x86_mode() function is more trustworthy
> 
> It is not a matter of trust.  It is a matter of correct or not, and it
> would be easy for userspace to simply copy what vmx_guest_x86_mode()
> already has.

Actually, the point I was trying to make is that I find it safer to use
vmx_guest_x86_mode() in the HV because otherwise I need to duplicate
that code in userspace (which I'm currently trying to do), and if for
some reason the implementation changes, someone needs to change it in
the userspace code as well. Having it only in one place in the HV looked
like a good idea.

As for it being a function of cr0, eflags and efer, it would appear that
it is also a function of cs_arbytes:

static int vmx_guest_x86_mode(struct vcpu *v)
{
    unsigned long cs_ar_bytes;

    if ( unlikely(!(v->arch.hvm_vcpu.guest_cr[0] & X86_CR0_PE)) )
        return 0;
    if ( unlikely(guest_cpu_user_regs()->eflags & X86_EFLAGS_VM) )
        return 1;
    __vmread(GUEST_CS_AR_BYTES, &cs_ar_bytes);
    if ( hvm_long_mode_enabled(v) &&
         likely(cs_ar_bytes & X86_SEG_AR_CS_LM_ACTIVE) )
        return 8;
    return (likely(cs_ar_bytes & X86_SEG_AR_DEF_OP_SIZE) ? 4 : 2);
}

However, in hvm.c, hvm_save_cpu_ctxt():

hvm_get_segment_register(v, x86_seg_cs, &seg);
ctxt.cs_sel = seg.sel;
ctxt.cs_limit = seg.limit;
ctxt.cs_base = seg.base;
ctxt.cs_arbytes = seg.attr.bytes;

Looking further at vmx_get_segment_register() in vmx.c, we get this:

 766     case x86_seg_cs:
 767         __vmread(GUEST_CS_SELECTOR, &sel);
 768         __vmread(GUEST_CS_LIMIT,    &limit);
 769         __vmread(GUEST_CS_BASE,     &reg->base);
 770         __vmread(GUEST_CS_AR_BYTES, &attr);
 771         break;

then:

 832     reg->attr.bytes = (attr & 0xff) | ((attr >> 4) & 0xf00);

This is why my userspace version of vmx_guest_x86_mode() (which uses
hwCpu.cs_arbytes from a struct hvm_hw_cpu hwCpu filled by
xc_domain_hvm_getcontext_partial()) does not work properly (it always
ends up returning 2, for both 32-bit guests - where it should return 4,
and 64-bit guests - where it should return 8).

So this solution would appear to be a bit more involved than the initial
solution. But you're, of course, right that guest_x86_mode should not be
VMX-specific.

Would it be OK if I would replace the call to vmx_guest_x86_mode() to a
call to hvm_funcs.guest_x86_mode(v) (assuming that's possible)?


Thanks,
Razvan Cojocaru

  parent reply	other threads:[~2014-07-10 11:57 UTC|newest]

Thread overview: 64+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-02 13:33 [PATCH RFC 1/9] xen: Emulate with no writes; compute current instruction length Razvan Cojocaru
2014-07-02 13:33 ` [PATCH RFC 2/9] xen: Optimize introspection access to guest state Razvan Cojocaru
2014-07-02 15:31   ` Andrew Cooper
2014-07-07 14:50     ` Razvan Cojocaru
2014-07-10  8:05     ` Razvan Cojocaru
2014-07-10  8:17       ` Andrew Cooper
2014-07-10  8:23         ` Razvan Cojocaru
2014-07-10 11:57         ` Razvan Cojocaru [this message]
2014-07-10 12:16           ` Razvan Cojocaru
2014-07-10 13:01           ` Andrew Cooper
2014-07-02 15:37   ` Jan Beulich
2014-07-03  8:12     ` Razvan Cojocaru
2014-07-03  8:54       ` Jan Beulich
2014-07-02 13:33 ` [PATCH RFC 3/9] xen: Force-enable relevant MSR events; optimize the number of sent MSR events Razvan Cojocaru
2014-07-02 15:35   ` Andrew Cooper
2014-07-02 15:43     ` Jan Beulich
2014-07-09  8:02       ` Razvan Cojocaru
2014-07-23  7:56         ` Jan Beulich
2014-07-23  8:03           ` Razvan Cojocaru
2014-07-02 13:33 ` [PATCH RFC 4/9] xenctrl: Make the headers C++ friendly Razvan Cojocaru
2014-07-02 15:37   ` Andrew Cooper
2014-07-02 13:33 ` [PATCH RFC 5/9] xen: Support for VMCALL mem_events Razvan Cojocaru
2014-07-02 15:47   ` Jan Beulich
2014-07-02 15:54     ` Razvan Cojocaru
2014-07-02 16:11       ` Jan Beulich
2014-07-02 16:23         ` Razvan Cojocaru
2014-07-03  6:28           ` Jan Beulich
2014-07-03  7:29             ` Razvan Cojocaru
2014-07-02 15:54   ` Andrew Cooper
2014-07-02 15:59     ` Razvan Cojocaru
2014-07-02 13:33 ` [PATCH RFC 6/9] xen, libxc: Request page fault injection via libxc Razvan Cojocaru
2014-07-02 15:51   ` Jan Beulich
2014-07-02 16:00     ` Andrew Cooper
2014-07-02 16:58       ` Mihai Donțu
2014-07-02 17:07         ` Andrew Cooper
2014-07-03  8:23           ` Mihai Donțu
2014-07-03  9:32             ` Andrew Cooper
2014-07-03  9:40               ` Razvan Cojocaru
2014-07-02 16:06     ` Razvan Cojocaru
2014-07-02 16:13       ` Jan Beulich
2014-07-02 13:33 ` [PATCH RFC 7/9] xen: Handle resumed instruction based on previous mem_event reply Razvan Cojocaru
2014-07-02 15:56   ` Jan Beulich
2014-07-03  8:55     ` Razvan Cojocaru
2014-07-03  9:02       ` Jan Beulich
2014-07-03  9:12         ` Razvan Cojocaru
2014-07-03  9:18           ` Andrew Cooper
2014-07-03  9:22           ` Jan Beulich
2014-07-03  9:34             ` Razvan Cojocaru
2014-07-03 10:14               ` Jan Beulich
2014-07-02 13:34 ` [PATCH RFC 8/9] xen: Generic instruction re-execution mechanism for execute faults Razvan Cojocaru
2014-07-02 16:04   ` Andrew Cooper
2014-07-02 13:34 ` [PATCH RFC 9/9] mm: mark pages that have their permissions controlled by a domain Razvan Cojocaru
2014-07-03 10:19   ` Jan Beulich
2014-07-03 11:27     ` Razvan Cojocaru
2014-07-03 12:15       ` Jan Beulich
2014-07-02 15:20 ` [PATCH RFC 1/9] xen: Emulate with no writes; compute current instruction length Andrew Cooper
2014-07-03  7:42   ` Razvan Cojocaru
2014-07-02 15:21 ` Jan Beulich
2014-07-02 15:43   ` Razvan Cojocaru
2014-07-02 16:08     ` Jan Beulich
2014-07-02 16:18       ` Razvan Cojocaru
2014-07-03  6:24         ` Jan Beulich
2014-07-03  7:38   ` Razvan Cojocaru
2014-07-03  8:05     ` Jan Beulich

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=53BE7FC5.8060907@bitdefender.com \
    --to=rcojocaru@bitdefender.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=tim@xen.org \
    --cc=xen-devel@lists.xen.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.