[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH v4 09/42] xen/riscv: implement virtual APLIC MMIO emulation



> [ ... 242 lines skipped ... ]
> +/*
> + * v->processor can't be used here: during a migration it names the new pCPU
> + * before the interrupt file is moved there. The caller has to hold
> + * vsfile_lock until the result is written to the h/w.
> + */
I think this is a leftover from your reordering, as we don't have any
migration yet. ...
> +uint32_t aplic_msi_target_gen(const struct vcpu *v, uint32_t base_val)
> +{
> +    const struct vimsic_state *vimsic_state = v->arch.vimsic_state;
> +    unsigned int guest_id = vcpu_guest_file_id(v);
> +    unsigned long hart_index;
> +
> +    ASSERT(rw_is_locked(&vimsic_state->vsfile_lock));
... same here.
> +    ASSERT(vimsic_state->vsfile_cpu < NR_CPUS);
What about having an inline helper for that, e.g. in asm/imsic.h next to
vsfile_cpu:

static inline bool imsic_is_hw_vsfile(unsigned int vsfile_cpu)
{
    return vsfile_cpu < NR_CPUS;
}

By the end of the series, the same check is open-coded in several places.
It would best come with "xen/riscv: introduce per-vCPU IMSIC state",
which adds vsfile_cpu.
> [ ... 9 lines skipped ... ]
> +uint32_t aplic_hw_read_reg(unsigned int offset)
> +{
> +    unsigned long flags;
> +    uint32_t val;
> +
> +    ASSERT((offset < aplic.size) && IS_ALIGNED(offset, sizeof(uint32_t)));
Nit(style): the parentheses around offset < aplic.size aren't needed.

> [ ... 7 lines skipped ... ]
> +void aplic_hw_write_reg(unsigned int offset, uint32_t value)
> +{
> +    unsigned long flags;
> +
> +    ASSERT((offset < aplic.size) && IS_ALIGNED(offset, sizeof(uint32_t)));
Nit(style): same here.
> [ ... 148 lines skipped ... ]
> +/*
> + * Only a h/w source of a domain, one a h/w IRQ is routed to (see
> + * vintc.hw_irqs), may be accessed in the h/w. A virtual source has nothing
> + * there: until its state is emulated, it reads as zero and ignores writes,
> + * like a source not assigned to the domain.
> + */
>
Nit: I think this comment could be shorter and clearer. What about:

/*
 * Only sources with a h/w IRQ routed to the domain (see vintc.hw_irqs) are
 * backed by the h/w. Virtual sources aren't emulated yet: they read as zero
 * and ignore writes, like sources not assigned to the domain.
 */

> [ ... 9 lines skipped ... ]
Nit: I think this comment could be clearer. What about:

/*
 * Convert a byte offset within a register group to a 32-bit index, either:
 * - the index of a register in a SETIP/CLRIP/SETIE/CLRIE group, each one
 *   covering 32 interrupt sources;
 * - the source number for SOURCECFG/TARGET.
 */

> +/*
> + * Convert a byte offset (within a SETIP/CLRIP/SETIE/CLRIE register group) to
> + * a 32-bit word index into the hw_irqs bitmap. Each word covers 32
> + * interrupt sources. For SOURCECFG and TARGET groups the same division also
> + * yields the interrupt number directly, because those arrays store one 
> 32-bit
> + * register per source.
> + */
> +#define regoffset_to_word_idx(reg_val) ((reg_val) / sizeof(uint32_t))
Also, the argument is an offset, not a register value: s/reg_val/offset/?
> [ ... 6 lines skipped ... ]
> +static bool vaplic_source_is_active(unsigned int irqn)
> +{
I would add an ASSERT(irqn) here, because with irqn == 0 this would
silently read domaincfg instead. It would also document the
precondition.

> [ ... 13 lines skipped ... ]
To be consistent with vaplic_target_write() and to say why the read uses
only the shadow, I'd add a small docstring. What about:

/*
 * Emulate a read of the shadowed target[@irqn], as the h/w register holds
 * Xen's translated value and the guest shouldn't read it.
 */

...
> +static uint32_t vaplic_target_read(const struct domain *d, unsigned int irqn)
> +{
> +    struct vaplic *vaplic = to_vaplic(d);
vaplic is only used to reach sources, so it can be const:

    const struct vaplic *vaplic = to_vaplic(d);
> [ ... 26 lines skipped ... ]
...
/*
 * Emulate a write of @value to the shadowed target[@irqn] and, for a h/w
 * source, of its translated value to the h/w register.
 */

> +/* Emulate a write of @value to target[@irqn]. */
> +static void vaplic_target_write(const struct domain *d, unsigned int irqn,
> +                                uint32_t value)
> +{
> +    struct vaplic *vaplic = to_vaplic(d);
Same here.
> +    struct vimsic_state *vimsic_state;
> +    const struct vcpu *target_vcpu;
> +    struct vaplic_source *src;
> +    unsigned long flags;
> +
vaplic_is_hw_source() already rejects irqn >= nr_virqs (and irqn == 0,
as long as bit 0 of hw_irqs is never set), so this is fine as is. But
_read() does the bounds check explicitly while _write() relies on the
helper. Could both use the same pattern, so a reader doesn't have to
check the helper to know an invalid irqn is handled?

Moreover, I would add a comment here, e.g.:

    /* Only a h/w source is emulated so far; otherwise, ignore the write. */
> [ ... 48 lines skipped ... ]
> +    if ( vimsic_state->vsfile_cpu != CPU_NONE )
> +        aplic_hw_write_reg(offsetof(struct aplic_regs, target[irqn - 1]),
> +                           aplic_msi_target_gen(target_vcpu, value));
So as long as there is no h/w interrupt file, the h/w target[] is not
updated: it keeps whatever it held. In that case, the guest is free to
setie/setipnum the source and, per AIA, "If source i is changed from
inactive to an active mode, ... the defined subfields of target[i] obtain
UNSPECIFIED values". That's not a value Xen chose; in practice, it's what
the previous owner left: Xen's own hart/EIID before route_irq_to_guest(),
or another domain's hart + guest file.
Couldn't this lead to unattended IRQs? If so, we might want to write the
h/w target[] register to point to something harmless, like EIID 0.

> [ ... 6 lines skipped ... ]
> +static uint32_t generate_auth_mask(const struct domain *currd,
> +                                   unsigned int word_idx)
Nit: nothing here needs the domain to be current, and the other helpers
(vaplic_is_hw_source(), vaplic_target_read/write()) take a plain d. In Xen,
currd is for a local variable caching current->domain, so s/currd/d/ here?
> +{
> +    unsigned int nr_words = DIV_ROUND_UP(currd->arch.vintc->nr_virqs,
> +                                         sizeof(uint32_t) * BITS_PER_BYTE);
Nit: sizeof(uint32_t) * BITS_PER_BYTE is open-coded twice in this function, and
a later patch of the series introduces IRQS_PER_WORD and vaplic_nr_words() for
exactly this. Could they be introduced here instead, so this patch already uses
them?
> [ ... 9 lines skipped ... ]
> +    first_bit = array_index_nospec(word_idx, nr_words) *
> +                sizeof(uint32_t) * BITS_PER_BYTE;
> +
> +    return currd->arch.vintc->hw_irqs[first_bit / BITS_PER_LONG] >>
> +           (first_bit % BITS_PER_LONG);
When none of the 32 sources this register covers is a h/w source of the
domain, generate_auth_mask() returns 0, and the h/w is still accessed. I
know it's harmless, but couldn't the callers return early when the mask
is 0, and skip the h/w access? ...
> [ ... 21 lines skipped ... ]
> +    case APLIC_SOURCECFG_BASE ... APLIC_SOURCECFG_LAST:
> +        /*
> +         * Look at vaplic_emulate_store() for explanation why APLIC_DOMAINCFG
> +         * is subtracted.
> +         */
Nit: vaplic_emulate_store() has the same remarks, "look at
vaplic_emulate_load()". For readability, it would be better to keep all
of these explanations in one place.
> [ ... 71 lines skipped ... ]
> +    case APLIC_GENMSI:
> +    default:
> +        gdprintk(XENLOG_WARNING, "Unhandled APLIC read at offset %#x\n",
> +                 offset);
> +
> +        return false;

The region is APLIC_SIZE(0), and it has holes (e.g. 0x1000-0x1bbf,
0x2008-0x2fff). Those are reserved, i.e. read-only zero per AIA, as your own
comment above says, and on v3 you agreed to do "read-only zeros instead of
default". Here they abort, and with patch 15 that is domain_crash() ->
arch_domain_shutdown() -> BUG_ON("unimplemented"), i.e. a guest takes the host
down. It would be better to read zero / ignore writes in default:, and the same
for the store side.

> +    }
> +
> +    *out = aplic_hw_read_reg(offset) & auth_mask;
... like:
        *out = auth_mask ? (aplic_hw_read_reg(offset) & auth_mask) : 0;

> [ ... 3 lines skipped ... ]
> +static bool vaplic_emulate_store(const struct vcpu *curr, paddr_t addr,
> +                                 uint32_t value)
> +{
> +    const struct domain *currd = curr->domain;
> +    struct vaplic *vaplic = to_vaplic(currd);
> +    unsigned int offset = addr - vaplic->regs_start;
> +
> +    ASSERT(curr == current);
Nit: vaplic_emulate_load() and vaplic_emulate_store() are static and only
called from vaplic_mmio_read() and vaplic_mmio_write(), which already
ASSERT(curr == current). Is the ASSERT() needed again here?

> [ ... 58 lines skipped ... ]
> +        if ( !vaplic_is_hw_source(currd,
> +                                  regoffset_to_word_idx(offset -
> +                                                        APLIC_DOMAINCFG)) )
> +            /* Interrupt not enabled, ignore it */
Nit: the comment is misleading, this checks whether the source is a h/w
source of the domain, not whether it is enabled. E.g.:

    /* Only a h/w source is emulated so far. */

> [ ... 9 lines skipped ... ]
> +        unsigned int word_idx =
> +            regoffset_to_word_idx(offset & APLIC_SETCLR_OFFSET_MASK);
>
Nit: offset & APLIC_SETCLR_OFFSET_MASK isn't a bounds check: it gets the
offset of the register within its group and relies on 128-byte aligned
bases, which isn't obvious at the call sites. As the same expression is
repeated five times in the aplic-imsic series, what about a helper,
something like:

/*
 * SET* and CLR* groups are 32 registers (128 bytes) each and start at 128-byte
 * aligned bases, so the low 7 bits of an offset are the byte offset within its
 * group. Masking them (offset % 128) drops the group base and gives the offset
 * of the register within its group, for any SET* or CLR* group.
 */
static inline unsigned int setclr_regoffset(unsigned int offset)
{
    return offset & APLIC_SETCLR_OFFSET_MASK;
}

Then the callers become:

        i = regoffset_to_word_idx(setclr_regoffset(offset));
> [ ... 57 lines skipped ... ]
> +    aplic_hw_write_reg(offset, value);
> +
> +    return true;
> +}

For sourcecfg[], this activates the h/w source without touching target[],
whose content is then UNSPECIFIED (see above), while src->target still
holds the guest's old value and is what the guest reads back. Rewrite the
h/w target[] (from src->target, or EIID 0 if there's no file) under
src->lock when a h/w source becomes active. Patch 39 keeps the same hole.

> +
>  static int __init cf_check vaplic_make_domu_dt_node(struct kernel_info 
> *kinfo)
>  {
> -    struct domain *d = kinfo->bd.d;
>      int res;
>      void *fdt = kinfo->fdt;
>      unsigned int msi_parent_phandle;
>      char vaplic_name[32];
> -    unsigned int aplic_size = APLIC_SIZE(d->max_vcpus);
>      const __be32 reg[] = {
>          cpu_to_be32(GUEST_APLIC_S_BASE >> 32),
>          cpu_to_be32(GUEST_APLIC_S_BASE),
>          cpu_to_be32(0),
> -        cpu_to_be32(aplic_size),
> +        cpu_to_be32(APLIC_SIZE(0)),
>      };
>  
> -    BUILD_BUG_ON(APLIC_SIZE(MAX_VIRT_CPUS) > UINT_MAX);
> -
This hunk isn't tied to the MMIO emulation: direct delivery mode was
never supported, so the DT has always advertised IDC structures which
don't exist. I would move it to a separate patch, fixing the size of
the region advertised to the guest.

-- 
Baptiste Le Duc <baptiste.le-duc@xxxxxxxxxx>



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.