All of lore.kernel.org
 help / color / mirror / Atom feed
From: Peter Maydell <peter.maydell@linaro.org>
To: QEMU Developers <qemu-devel@nongnu.org>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Subject: finding all the places in an AddressSpace that alias a specific address
Date: Fri, 12 Mar 2021 16:05:58 +0000	[thread overview]
Message-ID: <CAFEAcA8wAi6gYOLQG1u8wHS8--9+OENGyKs9mLiufJd9vHYQkg@mail.gmail.com> (raw)

I'm trying to fix an issue in Arm M-profile, where on reset the
CPU is supposed to load its initial PC and SP from a vector table.
This goes wrong if the vector table is in a guest image file (loaded
by the rom-blob loader) at address X which is not the same as the vector
table address Y but which is an aliased view of the same underlying RAM
(ie accesses to both X and Y go to the same real guest RAM).
Really the problem here is a reset-ordering one: the CPU reset code
runs before the hw/core/loader.c code has written the guest image
files to RAM. We currently try to work around that with:

        rom = rom_ptr(vecbase, 8);
        if (rom) {
            /* Address zero is covered by ROM which hasn't yet been
             * copied into physical memory.
             */
            initial_msp = ldl_p(rom);
            initial_pc = ldl_p(rom + 4);
        } else {
            /* Address zero not covered by a ROM blob, or the ROM blob
             * is in non-modifiable memory and this is a second reset after
             * it got copied into memory. In the latter case, rom_ptr
             * will return a NULL pointer and we should use ldl_phys instead.
             */
            initial_msp = ldl_phys(s->as, vecbase);
            initial_pc = ldl_phys(s->as, vecbase + 4);
        }

But the rom_ptr() check only matches if the ROM blob
is at the actual same guest address as the vector table base.

Arguably what we should try to sort out is the reset-ordering problem,
but that's a big job. I thought maybe we could for the moment at least
make the workaround we have today handle aliases.

For that I would need to find all the addresses in an AddressSpace
that alias vecbase, so that we can call rom_ptr() on all of them.
I think something like this would work:

    hwaddr xlat, len;
    RCU_READ_LOCK_GUARD();
    FlatView *fv = address_space_to_flatview(s->as);
    MemoryRegion *main_mr = flatview_translate(fv, vecbase, &xlat, &len,
                                               false, MEMTXATTRS_UNSPECIFIED);
    flatview_for_each_range(fv, callback_fn, ...);

where callback_fn() does:
    if (mr != main_mr) {
        return;
    }
    do arithmetic with flatview range start and len, etc to figure
    out the overall address in the AS corresponding to vecbase
    rom = rom_ptr(that_addr);
    ...

Does this seem like it would work ? Is there a nicer way ?

thanks
-- PMM


             reply	other threads:[~2021-03-12 16:11 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-12 16:05 Peter Maydell [this message]
2021-03-22 18:51 ` finding all the places in an AddressSpace that alias a specific address Paolo Bonzini

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=CAFEAcA8wAi6gYOLQG1u8wHS8--9+OENGyKs9mLiufJd9vHYQkg@mail.gmail.com \
    --to=peter.maydell@linaro.org \
    --cc=pbonzini@redhat.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.