Hi Hugo,

On Thu, Jul 01, 2021 at 10:28:10 CEST, Hugo V.C. wrote:
> let me clarify that the strace I provided is from my host machine (a common
> Linux distro) which provides "devmem2" that do not requires "0x" before the
> address.

I'm very aware of the fact that your strace was recorded with an
ordinary Linux distro, but still devmem2 needs the 0x prefix to
translate your command-line argument (speak string) into an integer
value.

> In fact, if you try with "0x" you get this argument error:
> 
> root@dev:/home/hugo# devmem2 0x7ffc8402e000
> /dev/mem opened.
> Error at line 75, file devmem2.c (22) [Invalid argument]

It seems you're lucky because your Linux kernel does not access
arbitrary physical addresses but warns you about 0x7ffc8402e000 beeing
an invalid argument for mmap("/dev/mem") [1].

> Instead, if you pass the argument without "0x" tehn looks like it works:
> 
> root@dev:/home/hugo# devmem2 7ffc8402e000
> /dev/mem opened.
> Memory mapped at address 0x7fcea478e000.
> Value at address 0x7 (0x7fcea478e007): 0x60E0F0

Sorry no, it does not work as you expect it to work. Please consider
devmem2 translates your address argument like follows [2].

  #include <stdio.h>
  #include <stdlib.h>

  int main() { printf("i0=%lx\n", strtoul("7fff", 0, 0)); }

If you try this, you will see that strtoul stops parsing the number on
the occurrence of the first f character because this is no decimal digit.

> I really don't know why the devmem2 says "Value at address 0x7" even if
> later it clarifies that the address is "0x7fcea478e007". In fact, in all
> the strace you can see the address handled is "0x7fcea478e007" and not
> "0x7":

Please see above because your argument 7ffc8402e000 is parsed to 0x7.
And please, consider the difference between virtual-memory addresses
and physical-memory addresses.

> as you can see mmap() here is taking the address I passed by argument
> "mmap(NULL, 102488, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fbca43a7000".
> An later all the mmap() calls are using the address as passed by argument
> (0x7fbca43a7000).
> Finally you can see the write() call taking the right address too:
> write(1, "Value at address 0x7 (0x7fbca43e"..., 48Value at address 0x7
> (0x7fbca43ed007): 0x60E0F0

Would you agree that the following numbers are quite different
7ffc8402e000, 0x7, and 0x7fbca43ed007? Only 0x7 is a physical address
in my opinion.

For information on how to retrieve page-frame numbers from
virtual-memory maps please consider [3] which I discovered on Stack
OVerflow [4].

[1] 
https://github.com/hackndev/tools/blob/7ed212230f8fbb1da3424a15ee88de3279bf96ec/devmem2.c#L75-L76
[2] 
https://github.com/hackndev/tools/blob/7ed212230f8fbb1da3424a15ee88de3279bf96ec/devmem2.c#L63
[3] https://github.com/dwks/pagemap
[4] https://unix.stackexchange.com/q/624746/334627

Greets
-- 
Christian Helmuth
Genode Labs

https://www.genode-labs.com/ · https://genode.org/
https://twitter.com/GenodeLabs · /ˈdʒiː.nəʊd/

Genode Labs GmbH · Amtsgericht Dresden · HRB 28424 · Sitz Dresden
Geschäftsführer: Dr.-Ing. Norman Feske, Christian Helmuth
_______________________________________________
Devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to