From: "Kamble, Nitin A" Subject: Re: [PATCH][RFC] Emulating real mode with x86_emulate Date: Wed, 4 Apr 2007 18:24:10 -0700 Message-ID: <1175736250.21558.16.camel@lnitindesktop.sc.intel.com> References: <4607074E.1030807@us.ibm.com> <1175203075.27076.17.camel@lnitindesktop.sc.intel.com> <460C4AAE.5020707@us.ibm.com> <1175212362.27076.32.camel@lnitindesktop.sc.intel.com> <460C55BD.5050202@us.ibm.com> <1175216381.27076.39.camel@lnitindesktop.sc.intel.com> <1175221214.27076.43.camel@lnitindesktop.sc.intel.com> <460C8207.8000604@us.ibm.com> <1175280781.32115.13.camel@lnitindesktop.sc.intel.com> <460D5E34.2080803@us.ibm.com> <1175288913.32115.20.camel@lnitindesktop.sc.intel.com> <1175289886.32115.26.camel@lnitindesktop.sc.intel.com> <460D8B1B.6020308@us.ibm.com> <1175539525.9276.5.camel@lnitindesktop.sc.intel.com> <4611514D.4090404@us.ibm.com> <1175557945.3593.16.camel@lnitindesktop.sc.intel.com> <46125ECC.2030202@us.ibm.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="===============0049329001==" Return-path: Content-class: urn:content-classes:message In-Reply-To: <46125ECC.2030202@us.ibm.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Mime-version: 1.0 Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: Anthony Liguori Cc: "Yu, Wilfred" , xen-devel@lists.xensource.com, Keir Fraser , "Nakajima, Jun" List-Id: xen-devel@lists.xenproject.org --===============0049329001== Content-Type: multipart/signed; boundary="=-SL8K9lvG2OxJVnUIuBkL"; protocol="application/pgp-signature"; micalg=pgp-sha1 Content-class: urn:content-classes:message --=-SL8K9lvG2OxJVnUIuBkL Content-Type: multipart/alternative; boundary="=-hUTR8T8wPHNh4mydbrN+" --=-hUTR8T8wPHNh4mydbrN+ Content-Type: text/plain Content-Transfer-Encoding: quoted-printable Hi Anthony, Keir, As per your suggestion I added the SCHEDULE_SOFTIRQ in the set_cr0 path. And now I see the code path going to vmx_do_emulate() for emulation. I added the emulation code for ljmp and it is able to go forward 1 instruction now. (XEN) HVM2: Loading Cirrus VGABIOS ... =20 (XEN) HVM2: Loading ACPI ... =20 (XEN) HVM2: Loading VMXAssist ... deadbeef =20 (XEN) HVM2: foo =20 (XEN) hvmop_emulate_realmode =20 (XEN) guest requests real mode emulation =20 (XEN) foo 221 =20 (XEN) HVM2: Invoking ROMBIOS ... =20 (XEN) vmx_vmexit_handler called. eip =3D 0x0 =20 (XEN) vmx_cr_access called eip=3D0x0 =20 (XEN) mov_to_cr 0 called eip=3D0x0 =20 (XEN) vmx_set_cr0 called eip=3D0x0 =20 (XEN) Transfering -- control to x86_emulate eip 0x0 =20 (XEN) cr access done: updating eip from eip =3D 0x0 =20 (XEN) updated eip=3D0x3 =20 (XEN) arch_vmx_do_resume: starting emulation =20 (XEN) vmx_do_emulate: hvm_store_cpu_guest_regs =20 (XEN) vmx_do_emulate: hypercall_preempt_check() =3D 0 =20 (XEN) vmx_do_emulate: cs:eip =3D 0008:0003 =20 (XEN) x86_emulate realmode: 0008:0003 =20 (XEN) jmp far seg:offset (Xea) handled =20 (XEN) vmx_do_emulate: cs:eip =3D 0000:0008 =20 (XEN) x86_emulate realmode: 0000:0008 =20 (XEN) x86_emulate.c:2441:d2 Instr: 8e =20 (XEN) failed to emulate instruction at eip =3D 0x8 =20 (XEN) domain_crash_sync called from vmcs.c:625 =20 (XEN) Domain 2 (vcpu#0) crashed on cpu#0: =20 (XEN) ----[ Xen-3.0-unstable x86_32p debug=3Dn Not tainted ]---- =20 (XEN) CPU: 0 =20 (XEN) EIP: 0008:[<00000003>] =20 (XEN) EFLAGS: 00000046 CONTEXT: hvm =20 (XEN) eax: 00000000 ebx: 00000000 ecx: 00000000 edx: 00000000 =20 (XEN) esi: 00000000 edi: 00000000 ebp: 00000000 esp: 00000000 =20 (XEN) cr0: 00000000 cr4: 00000650 cr3: 00000000 cr2: 00000000 =20 (XEN) ds: 0000 es: 0000 fs: 0000 gs: 0000 ss: 0010 cs: 0008 =20 I think getting more instruction emulated will be easy now. I will keep posted about the progress. Thanks & Regards, Nitin=20 Open Source Technology Center, Intel Corporation. ------------------------------------------------------------------------- The mind is like a parachute; it works much better when it's open.=20 On Tue, 2007-04-03 at 07:03 -0700, Anthony Liguori wrote: > Kamble, Nitin A wrote: > > On Mon, 2007-04-02 at 11:54 -0700, Anthony Liguori wrote: > >> Before calling x86_emulate, we use hvm_store_cpu_guest_regs() to > copy > >> the guest state into a regs struct (which happens to be the vcpu's > reg > >> struct). This reads directly from the VMCS via vmread() so it > should be > >> okay. I don't think a vmx_world_save/restore is actually needed > >> although perhaps I'm missing something? > > It may be ok to use hvm_store_cpu_guest_regs() for 1st few > > instructions, but I think it is not complete enough for emulator > use. >=20 > What is missing? x86_emulate() only uses info in the regs (it calls > out > to function pointers for special registers). The GP registers should > be > kept up-to-date on vmexit and hvm_store_cpu_guest_regs() should sync > the > remainder of the register state. >=20 > Is there a specific item you think is missing? >=20 > >> > >> > Also the function arch_vmx_do_resume() is called at the time of > vcpu > >> > schedule, so it is not right to call the vmx_do_emulate() from > there. > >> > >> Right, the idea was to have x86_emulate() be called instead of > >> vmentry(). I thought that being in the set_cr0 path would ensure > that > >> we go through do_resume() again. Is this assumption incorrect? > > Yes, This assumption is not right. arch_vmx_do_resume() is assigned > to > > schedule tail, so that the vcpu context is saved/restored when > another > > vcpu is scheduled on the physical cpu. >=20 > Hrm, okay. Manually invoking the scheduler is probably a reasonable > place to start. It would be nice to clean things up though so that > wasn't necessary. >=20 > >> > >> I didn't want to just stick it in the set_cr0 path because we ought > to > >> be able to pull the emulation loop into common code for SVM/VT and > the > >> do_resume path seems like the only place where there's common place > to > >> hook right now. > > I thought the emulator will be needed only for VMX; why is it needed > > for SVM? >=20 > As Keir mentioned, there are some corner cases where emulation is > needed. Also, there is some opportunity to simplify things by using > the > emulator. For instance, instead of decoding a PIO instruction using > the > info in the VMCS/VMCB (none of which is actually common to VT/SVM), we > may find that it is simpler to just call out to x86_emulate() and let > it > decode and dispatch the PIO operation. >=20 > In fact, a large number of the exits can be handled in this way. I > have > no clue if this would impact performance in a significant way but it > would definitely simplify things. >=20 > > Also calling the x86_emulate() to emulate multiple instructions from > > vmx_do_resume() will block the physical cpu from other vcpus. >=20 > That's what the hypercall_preempt_check() is for. >=20 > > I think we discussed the approach of using the non-root context for > > for emulator within the Xen. Or did I misunderstanding it? >=20 > We discussed quite a few approaches :-) I thought we settled on doing > the emulation within Xen. I'm not sure what would be gained from a > non-root context other than better security assurances. >=20 > Regards, >=20 > Anthony Liguori >=20 > >> Regards, > >> > >> Anthony Liguori > >> --=-hUTR8T8wPHNh4mydbrN+ Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable Re: [PATCH][RFC] Emulating real mode with x86_emulate Hi Anthony, Keir,
  As per your suggestion I added the SCHEDULE_SOFTIRQ in the set_cr0 p= ath. And now I see the code path going to vmx_do_emulate() for emulation. I= added the emulation code for ljmp and it is able to go forward 1 instructi= on now.

(XEN) HVM2: Loading Cirrus VGABIOS ...      &= nbsp;           &nbs= p;            &= nbsp;         
(XEN) HVM2: Loading ACPI ...        = ;            &n= bsp;            = ;            &n= bsp;     
(XEN) HVM2: Loading VMXAssist ... deadbeef     &nb= sp;            =             &nb= sp;      
(XEN) HVM2: foo          =             &nb= sp;            =             &nb= sp;            =     
(XEN) hvmop_emulate_realmode        = ;            &n= bsp;            = ;            &n= bsp;     
(XEN) guest requests real mode emulation      = ;            &n= bsp;            = ;        
(XEN) foo 221          &n= bsp;            = ;            &n= bsp;            = ;            &n= bsp;     
(XEN) HVM2: Invoking ROMBIOS ...       &= nbsp;           &nbs= p;            &= nbsp;           &nbs= p;  
(XEN) vmx_vmexit_handler called. eip =3D 0x0     &= nbsp;           &nbs= p;            &= nbsp;      
(XEN) vmx_cr_access called eip=3D0x0      &nb= sp;            =             &nb= sp;            =  
(XEN) mov_to_cr 0 called eip=3D0x0       = ;            &n= bsp;            = ;            &n= bsp;  
(XEN) vmx_set_cr0 called eip=3D0x0       = ;            &n= bsp;            = ;            &n= bsp;  
(XEN) Transfering -- control to x86_emulate eip 0x0    =             &nb= sp;           
(XEN) cr access done: updating eip from eip =3D 0x0    =             &nb= sp;            =  
(XEN) updated eip=3D0x3        &nbs= p;            &= nbsp;           &nbs= p;            &= nbsp;            (XEN) arch_vmx_do_resume: starting emulation     &= nbsp;           &nbs= p;            &= nbsp;    
(XEN) vmx_do_emulate: hvm_store_cpu_guest_regs     = ;            &n= bsp;            = ;   
(XEN) vmx_do_emulate: hypercall_preempt_check() =3D 0   &nbs= p;            &= nbsp;            (XEN) vmx_do_emulate: cs:eip =3D 0008:0003     &nb= sp;            =             &nb= sp;        
(XEN) x86_emulate realmode: 0008:0003      &n= bsp;            = ;            &n= bsp;          
(XEN) jmp far seg:offset (Xea) handled      &= nbsp;           &nbs= p;            &= nbsp;         
(XEN) vmx_do_emulate: cs:eip =3D 0000:0008     &nb= sp;            =             &nb= sp;        
(XEN) x86_emulate realmode: 0000:0008      &n= bsp;            = ;            &n= bsp;          
(XEN) x86_emulate.c:2441:d2 Instr: 8e      &n= bsp;            = ;            &n= bsp;          
(XEN) failed to emulate instruction at eip =3D 0x8    &= nbsp;           &nbs= p;            &= nbsp; 
(XEN) domain_crash_sync called from vmcs.c:625     = ;            &n= bsp;            = ;   
(XEN) Domain 2 (vcpu#0) crashed on cpu#0:     &nbs= p;            &= nbsp;           &nbs= p;       
(XEN) ----[ Xen-3.0-unstable  x86_32p  debug=3Dn  Not tainte= d ]----           &n= bsp;  
(XEN) CPU:    0       &nb= sp;            =             &nb= sp;            =             &nb= sp;      
(XEN) EIP:    0008:[<00000003>]    = ;            &n= bsp;            = ;            &n= bsp;      
(XEN) EFLAGS: 00000046   CONTEXT: hvm    &nbs= p;            &= nbsp;           &nbs= p;             =
(XEN) eax: 00000000   ebx: 00000000   ecx: 00000000&nbs= p;  edx: 00000000         = ;   
(XEN) esi: 00000000   edi: 00000000   ebp: 00000000&nbs= p;  esp: 00000000         = ;   
(XEN) cr0: 00000000   cr4: 00000650   cr3: 00000000&nbs= p;  cr2: 00000000         = ;   
(XEN) ds: 0000   es: 0000   fs: 0000   gs: 00= 00   ss: 0010   cs: 0008 

I think getting more instruction emulated will be easy now. I will keep pos= ted about the progress.

Thanks & Regards,
Nitin
Open Source Technology Center, Int= el Corporation.
----------------------------------= ---------------------------------------
The mind is like a parachute; it w= orks much better when it's open.=20

On Tue, 2007-04-03 at 07:03 -0700, Anthony Liguori wrote:
Kamble, Nitin A wrote:<= /FONT>
> On Mon, 2007-04-02 at 11:= 54 -0700, Anthony Liguori wrote:
>> Before calling x86_em= ulate, we use hvm_store_cpu_guest_regs() to copy
>> the guest state into = a regs struct (which happens to be the vcpu's reg
>> struct).  This r= eads directly from the VMCS via vmread() so it should be
>> okay.  I don't t= hink a vmx_world_save/restore is actually needed
>> although perhaps I'm = missing something?
> It may be ok to use hvm_s= tore_cpu_guest_regs() for 1st few
> instructions, but I think= it is not complete enough for emulator use.

What is missing?  x86_emu= late() only uses info in the regs (it calls out
to function pointers for speci= al registers).  The GP registers should be
kept up-to-date on vmexit and = hvm_store_cpu_guest_regs() should sync the
remainder of the register stat= e.

Is there a specific item you t= hink is missing?

>>
>> > Also the functio= n  arch_vmx_do_resume() is called at the time of vcpu >> > schedule, so it = is not right to call the  vmx_do_emulate() from there. >>
>> Right, the idea was t= o have x86_emulate() be called instead of
>> vmentry().  I th= ought that being in the set_cr0 path would ensure that
>> we go through do_resu= me() again.  Is this assumption incorrect?
> Yes, This assumption is n= ot right. arch_vmx_do_resume() is assigned to
> schedule tail, so that th= e vcpu context is saved/restored when another
> vcpu is scheduled on the = physical cpu.

Hrm, okay.  Manually invo= king the scheduler is probably a reasonable
place to start.  It would= be nice to clean things up though so that
wasn't necessary.

>>
>> I didn't want to just= stick it in the set_cr0 path because we ought to
>> be able to pull the e= mulation loop into common code for SVM/VT and the
>> do_resume path seems = like the only place where there's common place to
>> hook right now.
> I thought the emulator wi= ll be needed only for VMX; why is it needed
> for SVM?
As Keir mentioned, there are s= ome corner cases where emulation is
needed.  Also, there is s= ome opportunity to simplify things by using the
emulator.  For instance, = instead of decoding a PIO instruction using the
info in the VMCS/VMCB (none of= which is actually common to VT/SVM), we
may find that it is simpler to= just call out to x86_emulate() and let it
decode and dispatch the PIO op= eration.

In fact, a large number of the= exits can be handled in this way.  I have
no clue if this would impact p= erformance in a significant way but it
would definitely simplify thin= gs.

> Also calling the x86_emul= ate() to emulate multiple instructions from
> vmx_do_resume() will bloc= k the physical cpu from other vcpus.

That's what the hypercall_pree= mpt_check() is for.

> I think we discussed the = approach of using the non-root context for
> for emulator within the X= en. Or did I misunderstanding it?

We discussed quite a few appro= aches :-)  I thought we settled on doing
the emulation within Xen. = ; I'm not sure what would be gained from a
non-root context other than be= tter security assurances.

Regards,

Anthony Liguori<= BR>
>> Regards,
>>
>> Anthony Liguori
>>

--=-hUTR8T8wPHNh4mydbrN+-- --=-SL8K9lvG2OxJVnUIuBkL Content-Type: application/pgp-signature; name=signature.asc Content-Description: This is a digitally signed message part -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (GNU/Linux) iD8DBQBGFE+6Z1sfU7kTCR8RArFQAJ9PpksSKyT4jNqiDeWxddT/JeYTEACeN7oo 9vCXWLTnSNpmFOrr4qdyRCY= =S6Bt -----END PGP SIGNATURE----- --=-SL8K9lvG2OxJVnUIuBkL-- --===============0049329001== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel --===============0049329001==--