qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: Li Qiang <liq3ea@gmail.com>
Cc: Qemu Developers <qemu-devel@nongnu.org>
Subject: Re: Questions about the real mode in kvm/qemu
Date: Thu, 26 Sep 2019 11:53:06 +0200	[thread overview]
Message-ID: <a7687447-5df8-bb08-b78b-bef0321b40a9@redhat.com> (raw)
In-Reply-To: <CAKXe6SJaNju-O8mECU18n-3W3OnYnjnOUCKfAmTK32LAXj0nng@mail.gmail.com>

On 26/09/19 11:35, Li Qiang wrote:
> So without unrestrict guest the mainline is this: KVM set guest's
> rflag bit X86_EFLAGS_VM, so when the guest enter guest mode, it is in
> vm86 mode. In this mode, the CPU will access the address like in
> real mode(seg*4+offset), this address is linear address. And in fact,
> the vm86 is still in protected, so the linear address will be
> translated to gpa by the identity mapping table. Then goes to EPT
> table?

Yes.

>     ... as soon as the guest tries to enter protected mode, it will get into
>     a situation which is not real mode but doesn't have the segment
>     registers properly loaded with selectors.  
> 
>     Therefore, it will either
>     hack things together (enter_pmode) or emulate instructions until the
>     state is accepted even without unrestricted guest support.
> 
> Could you please explain this situation more detailed? Why this happen?

Protected mode entry looks like this:

        mov %cr0, %eax
        or $1, %al
        mov %eax, %cr0
	# [1] now in 16-bit protected mode
        lgdtl gdt32
        ljmpl $8, 2f
	# [2] now in 32-bit protected mode
2:
        .code32
        mov $16, %ax
        mov %ax, %ds
        mov %ax, %es
        mov %ax, %fs
        mov %ax, %gs
        mov %ax, %ss
	# [3] now everything is okay

Between [1] and [3] the vmentry could fail if not in unrestricted mode.
 For example (see checks on guest segment registers in the SDM):

- "CS. Type must be 9, 11, 13, or 15 (accessed code segment)."  CS in
real-mode is a RW data segment, not a code segment.  This applies
between [1] and [2].

- "SS. If the guest will not be virtual-8086 and the “unrestricted
guest” VM-execution control is 0, the RPL (bits 1:0) must equal the RPL
of the selector field for CS."  This may not be the case if the segment
register still holds real-mode values (which are not selectors, just
base >> 4).  This applies between [1] and [3].

- "DS, ES, FS, GS. The DPL cannot be less than the RPL in the selector
field"   Again, the real-mode DPL is zero but the RPL makes no sense if
the segment registers hold a real-mode value.

You can find more about these checks in guest_state_valid(); look at the
"else" branch of that function, the "then" branch is for pmode->rmode
transitions.  When any of the checks fail, KVM emulates instructions
instead of using VMX non-root mode (usually it's just a handful of them,
as in the case above).

Paolo



  reply	other threads:[~2019-09-26  9:54 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-26  7:52 Questions about the real mode in kvm/qemu Li Qiang
2019-09-26  8:31 ` Maxim Levitsky
2019-09-26  8:52   ` Li Qiang
2019-09-26  8:59     ` Maxim Levitsky
2019-09-26  9:18       ` Paolo Bonzini
2019-09-26  9:24         ` Maxim Levitsky
2019-09-26  9:33           ` Paolo Bonzini
2019-09-26  9:41             ` Maxim Levitsky
2019-09-26 10:00               ` Paolo Bonzini
2019-09-26 10:03                 ` Maxim Levitsky
2019-09-28 22:10         ` Avi Kivity
2019-09-29  7:39         ` Li Qiang
2019-09-26  9:15 ` Paolo Bonzini
2019-09-26  9:35   ` Maxim Levitsky
2019-09-26  9:35   ` Li Qiang
2019-09-26  9:53     ` Paolo Bonzini [this message]
2019-09-26 11:47       ` Li Qiang

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=a7687447-5df8-bb08-b78b-bef0321b40a9@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=liq3ea@gmail.com \
    --cc=qemu-devel@nongnu.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).