Skip to content

Commit

Permalink
x86/PV: conditionally avoid raising #GP for early guest MSR reads
Browse files Browse the repository at this point in the history
Prior to 4.15 Linux, when running in PV mode, did not install a #GP
handler early enough to cover for example the rdmsrl_safe() of
MSR_K8_TSEG_ADDR in bsp_init_amd() (not to speak of the unguarded read
of MSR_K7_HWCR later in the same function). The respective change
(42b3a4cb5609 "x86/xen: Support early interrupts in xen pv guests") was
backported to 4.14, but no further - presumably since it wasn't really
easy because of other dependencies.

Therefore, to prevent our change in the handling of guest MSR accesses
to render PV Linux 4.13 and older unusable on at least AMD systems, make
the raising of #GP on this paths conditional upon the guest having
installed a handler, provided of course the MSR can be read in the first
place (we would have raised #GP in that case even before). Producing
zero for reads isn't necessarily correct and may trip code trying to
detect presence of MSRs early, but since such detection logic won't work
without a #GP handler anyway, this ought to be a fair workaround.

Signed-off-by: Jan Beulich <jbeulich@suse.com>
Reviewed-by: Roger Pau Monné <roger.pau@citrix.com>
Release-Acked-by: Ian Jackson <iwj@xenproject.org>
Acked-by: Ian Jackson <iwj@xenproject.org>
  • Loading branch information
jbeulich authored and ijackson-citrix committed Mar 12, 2021
1 parent 9dc4638 commit 6eef0a9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
20 changes: 16 additions & 4 deletions xen/arch/x86/pv/emul-priv-op.c
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ static int read_msr(unsigned int reg, uint64_t *val,
struct vcpu *curr = current;
const struct domain *currd = curr->domain;
const struct cpuid_policy *cp = currd->arch.cpuid;
bool vpmu_msr = false;
bool vpmu_msr = false, warn = false;
uint64_t tmp;
int ret;

Expand All @@ -883,7 +883,7 @@ static int read_msr(unsigned int reg, uint64_t *val,
if ( ret == X86EMUL_EXCEPTION )
x86_emul_hw_exception(TRAP_gp_fault, 0, ctxt);

return ret;
goto done;
}

switch ( reg )
Expand Down Expand Up @@ -993,7 +993,7 @@ static int read_msr(unsigned int reg, uint64_t *val,
return X86EMUL_OKAY;
}

gdprintk(XENLOG_WARNING, "RDMSR 0x%08x unimplemented\n", reg);
warn = true;
break;

normal:
Expand All @@ -1002,7 +1002,19 @@ static int read_msr(unsigned int reg, uint64_t *val,
return X86EMUL_OKAY;
}

return X86EMUL_UNHANDLEABLE;
done:
if ( ret != X86EMUL_OKAY && !curr->arch.pv.trap_ctxt[X86_EXC_GP].address &&
(reg >> 16) != 0x4000 && !rdmsr_safe(reg, tmp) )
{
gprintk(XENLOG_WARNING, "faking RDMSR 0x%08x\n", reg);
*val = 0;
x86_emul_reset_event(ctxt);
ret = X86EMUL_OKAY;
}
else if ( warn )
gdprintk(XENLOG_WARNING, "RDMSR 0x%08x unimplemented\n", reg);

return ret;
}

static int write_msr(unsigned int reg, uint64_t val,
Expand Down
6 changes: 6 additions & 0 deletions xen/include/public/arch-x86/xen.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ typedef unsigned long xen_ulong_t;
* Level == 1: Kernel may enter
* Level == 2: Kernel may enter
* Level == 3: Everyone may enter
*
* Note: For compatibility with kernels not setting up exception handlers
* early enough, Xen will avoid trying to inject #GP (and hence crash
* the domain) when an RDMSR would require this, but no handler was
* set yet. The precise conditions are implementation specific, and
* new code may not rely on such behavior anyway.
*/
#define TI_GET_DPL(_ti) ((_ti)->flags & 3)
#define TI_GET_IF(_ti) ((_ti)->flags & 4)
Expand Down

0 comments on commit 6eef0a9

Please sign in to comment.