Re: [PATCH 14/14] KVM: x86: Add helpers to prepare kvm_run for userspace MMIO exit

From: Sean Christopherson

Date: Mon Mar 02 2026 - 21:24:33 EST


On Wed, Feb 25, 2026, Sean Christopherson wrote:
> On Wed, Feb 25, 2026, Rick P Edgecombe wrote:
> > On Tue, 2026-02-24 at 17:20 -0800, Sean Christopherson wrote:
> > > +static inline void __kvm_prepare_emulated_mmio_exit(struct kvm_vcpu *vcpu,
> > > +     gpa_t gpa, unsigned int len,
> > > +     const void *data,
> > > +     bool is_write)
> > > +{
> > > + struct kvm_run *run = vcpu->run;
> > > +
> > > + run->mmio.len = min(8u, len);
> >
> > I would think to extract this to a local var so it can be used twice.
>
> Ya, either way works for me. The copy+paste is a little gross, but it's also
> unlikely that anyone is going to modify this code (or if they did, that any goofs
> wouldn't be immediately disastrous).

Ooh, better idea. Since TDX is the only direct user of
__kvm_prepare_emulated_mmio_exit() and it only supports lenths of 1, 2, 4, and 8,
kvm_prepare_emulated_mmio_exit() is the only path that actually needs to cap the
length. Then the inner helper can assert a valid length. Doesn't change anything
in practice, but I like the idea of making the caller be aware of the limitation
(even if that caller is itself a helper).

static inline void __kvm_prepare_emulated_mmio_exit(struct kvm_vcpu *vcpu,
gpa_t gpa, unsigned int len,
const void *data,
bool is_write)
{
struct kvm_run *run = vcpu->run;

KVM_BUG_ON(len > 8, vcpu->kvm);

run->mmio.len = len;
run->mmio.is_write = is_write;
run->exit_reason = KVM_EXIT_MMIO;
run->mmio.phys_addr = gpa;
if (is_write)
memcpy(run->mmio.data, data, len);
}

static inline void kvm_prepare_emulated_mmio_exit(struct kvm_vcpu *vcpu,
struct kvm_mmio_fragment *frag)
{
WARN_ON_ONCE(!vcpu->mmio_needed || !vcpu->mmio_nr_fragments);

__kvm_prepare_emulated_mmio_exit(vcpu, frag->gpa, min(8u, frag->len),
frag->data, vcpu->mmio_is_write);
}