Re: Kernel allocated memory not visible in L4Re virtual device despite correct physical RAM mapping

Adam Lackorzynski <[email protected]> Wed, 15 Apr 2026 13:39:23 +0200
Newsgroups gmane.comp.micro-kernel.l4.devel
Message-ID <[email protected]>
Hi Stephen,

the memory in Linux is mapped cached, while in your L4Re side, you
mapped it uncached. Did you try mapping it cached on the L4Re side?
Or you do a cache clean on the str string in Linux.


BR, Adam

On Mon Apr 13, 2026 at 14:53:00 +0800, stephen.yang wrote:
> Hi l4-hackers,
>  I am trying to pass a kernel-allocated memory buffer to a virtual device=
, but I am running into an issue. I would appreciate any help or suggestion=
s. =20
>=20
>=20
> 1.Kernel side(driver)
>  In my platform driver, I allocate memory using devm_kzalloc, write a str=
ing into it, and pass its physical address to the device via an MMIO regist=
er: =20
> =A1=AD=A1=AD
> void* buf =3D devm_kzalloc(&pdev->dev, 0x200, GFP_KERNEL);
> if (!buf)
>     return -ENOMEM;
> phys_addr_t pa =3D virt_to_phys(buf);
> char *str =3D "hi stephen";
> memcpy(buf, str, strlen(str) + 1);
> wmb();
> writeq((u64)pa, hb->base + 0x00);
> =A1=AD=A1=AD
>=20
>=20
>  My understanding is: allocate kernel memory =A1=FA write data =A1=FA pas=
s physical address to the device. =20
>=20
>=20
> 2. Memory layout
>  I observed that the allocated physical address falls within the RAM regi=
ons configured via bootargs : 0x66200000 - 0x6fffffff =20
>  So my assumption was that if the virtual device maps this physical memor=
y region, it should be able to access the data written by the kernel. =20
>=20
>=20
> 3. Virtual device side (L4Re)
> On the virtual device side, I map the physical memory using L4Re RM attac=
h:
> void write(unsigned reg, char /*size*/, l4_uint64_t value, unsigned)
> {
>     if (reg =3D=3D 0x00) {
>         addr =3D value;
>=20
>=20
>         auto d =3D L4Re::Env::env()->get_cap<L4Re::Dataspace>("lram");
>=20
>=20
>         l4_addr_t vaddr;
>         l4_addr_t base =3D 0x66200000;
>         l4_addr_t size =3D 0x09E00000;
>         int r =3D L4Re::Env::env()->rm()->attach( &vaddr, size, L4Re::Rm:=
:F::Search_addr | L4Re::Rm::F::RW |=20
>   L4Re::Rm::F::Cache_uncached,
>             L4::Ipc::make_cap_rw(d),
>             0,
>             L4_PAGESHIFT);
>         if (r) {
>             printf("can't attach hb memory\n");
>             return;
>         }
>         char *dst =3D reinterpret_cast<char *>(vaddr + (addr - base));
>         for (int i =3D 0; i < 20; i++) {
>             printf("%02x ", (unsigned char)dst[i]);
>         }
>         printf("\n");
>         printf("get msg for guest: %s\n", dst);
>     }
> }
>=20
>=20
> 4.Problem
> However, I cannot read the string written by the kernel. The memory conte=
nt I get appears to be uninitialized or unrelated data.
>=20
>=20
> 5.Question
> I can confirm that the lram dataspace capability does correctly correspon=
d to the physical memory region 0x66200000 - 0x6fffffff,=20
> and this region is valid and usable RAM on the system.=20
>=20
>=20
>  I am unsure where the issue lies: =20
> (1) Is this approach (kernel allocation =A1=FA pass physical address =A1=
=FA direct mapping in virtual device) actually valid?
> (2)  Could this be a cache coherence / DMA issue?
> (3)  Or is there something incorrect in my L4Re RM attach usage?
>=20
>=20
>  Any insights would be greatly appreciated. =20

> _______________________________________________
> l4-hackers mailing list -- [email protected]
> To unsubscribe send an email to [email protected]
_______________________________________________
l4-hackers mailing list -- [email protected]
To unsubscribe send an email to [email protected]