1. Feb 04, 2023
    • Michal Luczaj's avatar
      KVM: x86/emulator: Fix comment in __load_segment_descriptor() · 096691e0
      Michal Luczaj authored
      
      
      The comment refers to the same condition twice. Make it reflect what the
      code actually does.
      
      No functional change intended.
      
      Signed-off-by: default avatarMichal Luczaj <mhal@rbox.co>
      Reviewed-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      Link: https://lore.kernel.org/r/20230126013405.2967156-3-mhal@rbox.co
      
      
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      096691e0
    • Michal Luczaj's avatar
      KVM: x86/emulator: Fix segment load privilege level validation · 0735d1c3
      Michal Luczaj authored
      Intel SDM describes what steps are taken by the CPU to verify if a
      memory segment can actually be used at a given privilege level. Loading
      DS/ES/FS/GS involves checking segment's type as well as making sure that
      neither selector's RPL nor caller's CPL are greater than segment's DPL.
      
      Emulator implements Intel's pseudocode in __load_segment_descriptor(),
      even quoting the pseudocode in the comments. Although the pseudocode is
      correctly translated, the implementation is incorrect. This is most
      likely due to SDM, at the time, being wrong.
      
      Patch fixes emulator's logic and updates the pseudocode in the comment.
      Below are historical notes.
      
      Emulator code for handling segment descriptors appears to have been
      introduced in March 2010 in commit 38ba30ba
      
       ("KVM: x86 emulator:
      Emulate task switch in emulator.c"). Intel SDM Vol 2A: Instruction Set
      Reference, A-M (Order Number: 253666-034US, _March 2010_) lists the
      steps for loading segment registers in section related to MOV
      instruction:
      
        IF DS, ES, FS, or GS is loaded with non-NULL selector
        THEN
          IF segment selector index is outside descriptor table limits
          or segment is not a data or readable code segment
          or ((segment is a data or nonconforming code segment)
          and (both RPL and CPL > DPL))   <---
            THEN #GP(selector); FI;
      
      This is precisely what __load_segment_descriptor() quotes and
      implements. But there's a twist; a few SDM revisions later
      (253667-044US), in August 2012, the snippet above becomes:
      
        IF DS, ES, FS, or GS is loaded with non-NULL selector
        THEN
          IF segment selector index is outside descriptor table limits
          or segment is not a data or readable code segment
          or ((segment is a data or nonconforming code segment)
            [note: missing or superfluous parenthesis?]
          or ((RPL > DPL) and (CPL > DPL))   <---
            THEN #GP(selector); FI;
      
      Many SDMs later (253667-065US), in December 2017, pseudocode reaches
      what seems to be its final form:
      
        IF DS, ES, FS, or GS is loaded with non-NULL selector
        THEN
          IF segment selector index is outside descriptor table limits
          OR segment is not a data or readable code segment
          OR ((segment is a data or nonconforming code segment)
              AND ((RPL > DPL) or (CPL > DPL)))   <---
            THEN #GP(selector); FI;
      
      which also matches the behavior described in AMD's APM, which states that
      a #GP occurs if:
      
        The DS, ES, FS, or GS register was loaded and the segment pointed to
        was a data or non-conforming code segment, but the RPL or CPL was
        greater than the DPL.
      
      Signed-off-by: default avatarMichal Luczaj <mhal@rbox.co>
      Reviewed-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      Link: https://lore.kernel.org/r/20230126013405.2967156-2-mhal@rbox.co
      
      
      [sean: add blurb to changelog calling out AMD agrees]
      Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
      0735d1c3
  2. Feb 02, 2023
  3. Jan 25, 2023
  4. Jan 24, 2023
    • Paolo Bonzini's avatar
      Merge branch 'kvm-lapic-fix-and-cleanup' into HEAD · f15a87c0
      Paolo Bonzini authored
      
      
      The first half or so patches fix semi-urgent, real-world relevant APICv
      and AVIC bugs.
      
      The second half fixes a variety of AVIC and optimized APIC map bugs
      where KVM doesn't play nice with various edge cases that are
      architecturally legal(ish), but are unlikely to occur in most real world
      scenarios
      
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      f15a87c0
    • Paolo Bonzini's avatar
      Merge branch 'kvm-v6.2-rc4-fixes' into HEAD · dc7c31e9
      Paolo Bonzini authored
      ARM:
      
      * Fix the PMCR_EL0 reset value after the PMU rework
      
      * Correctly handle S2 fault triggered by a S1 page table walk
        by not always classifying it as a write, as this breaks on
        R/O memslots
      
      * Document why we cannot exit with KVM_EXIT_MMIO when taking
        a write fault from a S1 PTW on a R/O memslot
      
      * Put the Apple M2 on the naughty list for not being able to
        correctly implement the vgic SEIS feature, just like the M1
        before it
      
      * Reviewer updates: Alex is stepping down, replaced by Zenghui
      
      x86:
      
      * Fix various rare locking issues in Xen emulation and teach lockdep
        to detect them
      
      * Documentation improvements
      
      * Do not return host topology information from KVM_GET_SUPPORTED_CPUID
      dc7c31e9
    • Paolo Bonzini's avatar
      Merge branch 'kvm-hw-enable-refactor' into HEAD · edd731d7
      Paolo Bonzini authored
      
      
      The main theme of this series is to kill off kvm_arch_init(),
      kvm_arch_hardware_(un)setup(), and kvm_arch_check_processor_compat(), which
      all originated in x86 code from way back when, and needlessly complicate
      both common KVM code and architecture code.  E.g. many architectures don't
      mark functions/data as __init/__ro_after_init purely because kvm_init()
      isn't marked __init to support x86's separate vendor modules.
      
      The idea/hope is that with those hooks gone (moved to arch code), it will
      be easier for x86 (and other architectures) to modify their module init
      sequences as needed without having to fight common KVM code.  E.g. I'm
      hoping that ARM can build on this to simplify its hardware enabling logic,
      especially the pKVM side of things.
      
      There are bug fixes throughout this series.  They are more scattered than
      I would usually prefer, but getting the sequencing correct was a gigantic
      pain for many of the x86 fixes due to needing to fix common code in order
      for the x86 fix to have any meaning.  And while the bugs are often fatal,
      they aren't all that interesting for most users as they either require a
      malicious admin or broken hardware, i.e. aren't likely to be encountered
      by the vast majority of KVM users.  So unless someone _really_ wants a
      particular fix isolated for backporting, I'm not planning on shuffling
      patches.
      
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      edd731d7
  5. Jan 13, 2023