On Sat, Oct 17, 2026 at 11:06:49AM +1000, Gavin Shan wrote:
> On 10/17/26 6:08 AM, Mathieu Poirier wrote:
> > On Fri, Oct 02, 2026 at 01:45:40PM +1000, Gavin Shan wrote:
> > > On 9/4/26 5:35 AM, Mathieu Poirier wrote:
> > > > From: Jean-Philippe Brucker <[email protected]>
> > > >
> > > > Once the Realm descriptor has been created, tell KVM to transfer runtime
> > > > images (kernel, DT, and rootfs) from guest memory to Realm memory.
> > > >
> > > > Signed-off-by: Jean-Philippe Brucker <[email protected]>
> > > > Signed-off-by: Lorenzo Pieralisi <[email protected]>
> > > > Signed-off-by: Mathieu Poirier <[email protected]>
> > > > ---
> > > > target/arm/kvm-rme.c | 76
> > > > ++++++++++++++++++++++++++++++++++++++++++++
> > > > 1 file changed, 76 insertions(+)
> > > >
> > > > diff --git a/target/arm/kvm-rme.c b/target/arm/kvm-rme.c
> > > > index 9985f5daba89..c082a5d8f3d1 100644
> > > > --- a/target/arm/kvm-rme.c
> > > > +++ b/target/arm/kvm-rme.c
> > > > @@ -15,6 +15,7 @@
> > > > #include "migration/blocker.h"
> > > > #include "qapi/error.h"
> > > > #include "qemu/error-report.h"
> > > > +#include "qemu/memalign.h"
> > > > #include "qom/object_interfaces.h"
> > > > #include "system/confidential-guest-support.h"
> > > > #include "system/kvm.h"
> > > > @@ -43,12 +44,87 @@ OBJECT_DEFINE_SIMPLE_TYPE_WITH_INTERFACES(RmeGuest,
> > > > rme_guest, RME_GUEST,
> > > > static RmeGuest *rme_guest;
> > > > +static int rme_populate_range(const RmeRamRegion *region, bool measure,
> > > > + Error **errp)
> > > > +{
> > > > + int ret;
> > > > + void *buffer;
> > > > + hwaddr size = region->size;
> > > > + hwaddr base = region->base;
> > > > + hwaddr start = QEMU_ALIGN_DOWN(base, RME_PAGE_SIZE);
> > > > + hwaddr end = QEMU_ALIGN_UP(base + size, RME_PAGE_SIZE);
> > > > + struct kvm_arm_rmi_populate populate_args;
> > > > + size_t aligned_size = ROUND_UP(region->size,
> > > > qemu_real_host_page_size());
> > >
> > > s/qemu_real_host_page_size()/RME_PAGE_SIZE
> >
> > I refactored this function in V5 [1], along with the above declarations.
> >
> > [1].
> > https://lore.kernel.org/kvm/[email protected]/
> >
> > >
> > > > +
> > > > + if (!region->data) {
> > > > + return -ENODEV;
> > > > + }
> > > > +
> > > > + ret = kvm_set_memory_attributes_private(start, end - start);
> > > > + if (ret) {
> > > > + error_report("RME: failed to configure initial"
> > > > + "private guest memory");
> > > > + return ret;
> > > > + }
> > > > +
> > > > + /* Allocate page-aligned memory */
> > > > + buffer = qemu_memalign(qemu_real_host_page_size(), aligned_size);
> > > > +
> > > > + if (!buffer) {
> > > > + return -ENOMEM;
> > > > + }
> > > > +
> > > > + memset(buffer, 0, aligned_size);
> > > > + memcpy(buffer, region->data, region->size);
> > > > +
> > > > + populate_args = (struct kvm_arm_rmi_populate) {
> > > > + .base = start,
> > > > + .size = end - start,
> > > > + .source_uaddr = (uintptr_t)buffer,
> > > > + .flags = measure ? KVM_ARM_RMI_POPULATE_FLAGS_MEASURE : 0,
> > > > + };
> > > > +
> > > > + while (populate_args.size > 0) {
> > > > + ret = kvm_vm_ioctl(kvm_state, KVM_ARM_RMI_POPULATE,
> > > > &populate_args, 0);
> > > > + if (ret) {
> > > > + error_setg_errno(errp, -ret,
> > > > + "failed to populate realm [0x%"HWADDR_PRIx",
> > > > 0x%"HWADDR_PRIx")",
> > > > + start, end);
> > > > + break;
> > > > + }
> > > > + }
> > > > +
> > > > + qemu_vfree(buffer);
> > > > +
> > > > + return ret;
> > > > +}
> > > > +
> > >
> > > I doubt that the unaligned regions (RmeRamRegion) are allowed. For
> > > example,
> > > the region [4KB 64KB+4KB] is turned to the aligned range [0 128KB] when
> > > RME_PAGE_SIZE
> > > is 64KB. The question is how do we know the added regions due to the
> > > alignment
> > > ([0 4KB] and [64KB+4KB 128KB]) are safe for
> > > kvm_vm_ioctl(KVM_ARM_RMI_POPULATE).
> > > I guess we probably just reject unaligned regions if they're not expected.
> >
> > To make sure I understand your point, please expand on what would be an
> > unaligned regions. In your example, [0 128KB] is aligned on 64KB boundries.
> >
> > >
> > > Even if the unaligned regions are allowed, they seem not well handled.
> > >
> > > - @aligned_size is still 64KB instead of the 128KB ?
> >
> > I'm confused. ROUND_UP(64KB+4KB, 64KB) would be 128KB. What am I missing?
> >
>
> ROUND_UP(64KB+4KB, 64KB) corresponds to ROUND_UP(size+base, 64KB) for local
> variable @end instead of @aligned_size. @aligned_size is calculated as
> ROUND_UP(64KB, 64KB) and the output is still 64KB.
>
> > > - In "memcpy(buffer, region->data, region->size);", the blob data would
> > > be copied to
> > > [4KB, 4KB+64KB]. However, we're copying the data to [0 64KB] now.
> >
> > I agree with you. That part was refactored in V5 [1]:
> >
> > memcpy(buffer + (base - start), region->data, region->size);
I misunderstood Gavin's comment on this, so the issue isn't the memcpy per-se
(that was the impression I had) but rather in relation to using the buffer in
kvm_vm_ioctl(kvm_state, KVM_ARM_RMI_POPULATE, &populate_args, 0);
with populate_args.base = start (that was aligned down).
Thanks,
Lorenzo
> > >
> > > > +static void rme_populate_ram_region(gpointer data, gpointer err)
> > > > +{
> > > > + Error **errp = err;
> > > > + const RmeRamRegion *region = data;
> > > > +
> > > > + if (*errp) {
> > > > + return;
> > > > + }
> > > > +
> > > > + rme_populate_range(region, /* measure */ true, errp);
> > > > +}
> > > > +
> > > > static void rme_vm_state_change(void *opaque, bool running, RunState
> > > > state)
> > > > {
> > > > + Error *errp = NULL;
> > > > +
> > > > if (!running) {
> > > > return;
> > > > }
> > > > + g_slist_foreach(rme_guest->ram_regions, rme_populate_ram_region,
> > > > &errp);
> > > > + g_slist_free_full(g_steal_pointer(&rme_guest->ram_regions),
> > > > g_free);
> > > > + if (errp) {
> > > > + return;
> > > > + }
> > > > +
> > > > kvm_mark_guest_state_protected();
> > > > }
> > >
>
> Thanks,
> Gavin
>