Skip to content

Commit 046bc25

Browse files
committed
virt_to_phys: fix %p vs %px
Move doc to README
1 parent 65fc5b8 commit 046bc25

File tree

3 files changed

+71
-35
lines changed

3 files changed

+71
-35
lines changed

README.adoc

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4127,7 +4127,9 @@ The module also shows which handlers are registered for each IRQ, as we have obs
41274127

41284128
When in text mode, we can also observe interrupt line 4 with handler `ttyS0` increase continuously as IO goes through the UART.
41294129

4130-
=== Linux kernel utility functions
4130+
=== Kernel utility functions
4131+
4132+
https://github.com/torvalds/linux/blob/v4.17/Documentation/core-api/kernel-api.rst
41314133

41324134
==== kstrto
41334135

@@ -4151,6 +4153,54 @@ Sources:
41514153

41524154
Bibliography: https://stackoverflow.com/questions/6139493/how-convert-char-to-int-in-linux-kernel/49811658#49811658
41534155

4156+
==== virt_to_phys
4157+
4158+
Convert a virtual address to physical:
4159+
4160+
....
4161+
insmod /virt_to_phys.ko
4162+
cat /sys/kernel/debug/lkmc_virt_to_phys
4163+
....
4164+
4165+
Source: link:kernel_module/virt_to_phys.c[]
4166+
4167+
Sample output:
4168+
4169+
....
4170+
*kmalloc_ptr = 0x12345678
4171+
kmalloc_ptr = ffff88000e169ae8
4172+
virt_to_phys(kmalloc_ptr) = 0xe169ae8
4173+
static_var = 0x12345678
4174+
&static_var = ffffffffc0002308
4175+
virt_to_phys(&static_var) = 0x40002308
4176+
....
4177+
4178+
We can confirm that the `kmalloc_ptr` translation worked with:
4179+
4180+
....
4181+
./qemumonitor 'xp 0xe169ae8'
4182+
....
4183+
4184+
which reads four bytes from a given physical address, and gives the expected:
4185+
4186+
....
4187+
000000000e169ae8: 0x12345678
4188+
....
4189+
4190+
TODO it only works for kmalloc however, for the static variable:
4191+
4192+
....
4193+
./qemumonitor 'xp 0x40002308'
4194+
....
4195+
4196+
it gave a wrong value of `00000000`.
4197+
4198+
Bibliography:
4199+
4200+
* https://stackoverflow.com/questions/5748492/is-there-any-api-for-determining-the-physical-address-from-virtual-address-in-li/45128487#45128487
4201+
* https://stackoverflow.com/questions/39134990/mmap-of-dev-mem-fails-with-invalid-argument-for-virt-to-phys-address-but-addre/45127582#45127582
4202+
* https://stackoverflow.com/questions/43325205/can-we-use-virt-to-phys-for-user-space-memory-in-kernel-module
4203+
41544204
=== Linux kernel tracing
41554205

41564206
Good overviews:

kernel_module/README.adoc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ Our kernel modules!
1010
.. link:work_from_work.c[]
1111
.. link:workqueue_cheat.c[]
1212
. Misc
13-
.. link:virt_to_phys.c[]
1413
.. link:netlink.c[]
1514
. Hardening
1615
.. link:strlen_overflow.c[]

kernel_module/virt_to_phys.c

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,4 @@
1-
/*
2-
Also try on QEMU monitor:
3-
4-
xp 0x<vaddr>
5-
6-
Only works for kmalloc.
7-
8-
static inline phys_addr_t virt_to_phys(volatile void *address)
9-
10-
- https://stackoverflow.com/questions/5748492/is-there-any-api-for-determining-the-physical-address-from-virtual-address-in-li
11-
- https://stackoverflow.com/questions/43325205/can-we-use-virt-to-phys-for-user-space-memory-in-kernel-module
12-
- https://stackoverflow.com/questions/39134990/mmap-of-dev-mem-fails-with-invalid-argument-but-address-is-page-aligned
13-
*/
1+
/* https://github.com/cirosantilli/linux-kernel-module-cheat#virt_to_phys */
142

153
#include <asm/io.h> /* virt_to_phys */
164
#include <linux/debugfs.h>
@@ -21,26 +9,26 @@ static inline phys_addr_t virt_to_phys(volatile void *address)
219
#include <linux/seq_file.h> /* single_open, single_release */
2210
#include <linux/slab.h> /* kmalloc, kfree */
2311

24-
static volatile u32 *k;
25-
static volatile u32 i;
12+
static volatile u32 *kmalloc_ptr;
13+
static volatile u32 static_var;
2614

2715
static struct dentry *debugfs_file;
2816

2917
static int show(struct seq_file *m, void *v)
3018
{
3119
seq_printf(m,
32-
"k 0x%llx\n"
33-
"addr_k %p\n"
34-
"virt_to_phys_k 0x%llx\n"
35-
"i 0x%llx\n"
36-
"addr_i %p\n"
37-
"virt_to_phys_i 0x%llx\n",
38-
(unsigned long long)*k,
39-
k,
40-
(unsigned long long)virt_to_phys((void *)k),
41-
(unsigned long long)i,
42-
&i,
43-
(unsigned long long)virt_to_phys((void *)&i)
20+
"*kmalloc_ptr = 0x%llx\n"
21+
"kmalloc_ptr = %px\n"
22+
"virt_to_phys(kmalloc_ptr) = 0x%llx\n"
23+
"static_var = 0x%llx\n"
24+
"&static_var = %px\n"
25+
"virt_to_phys(&static_var) = 0x%llx\n",
26+
(unsigned long long)*kmalloc_ptr,
27+
kmalloc_ptr,
28+
(unsigned long long)virt_to_phys((void *)kmalloc_ptr),
29+
(unsigned long long)static_var,
30+
&static_var,
31+
(unsigned long long)virt_to_phys((void *)&static_var)
4432
);
4533
return 0;
4634
}
@@ -60,18 +48,17 @@ static const struct file_operations fops = {
6048

6149
static int myinit(void)
6250
{
63-
k = kmalloc(sizeof(k), GFP_KERNEL);
64-
*k = 0x12345678;
65-
i = 0x12345678;
66-
debugfs_file = debugfs_create_file(
67-
"lkmc_virt_to_phys", S_IRUSR, NULL, NULL, &fops);
51+
kmalloc_ptr = kmalloc(sizeof(kmalloc_ptr), GFP_KERNEL);
52+
*kmalloc_ptr = 0x12345678;
53+
static_var = 0x12345678;
54+
debugfs_file = debugfs_create_file("lkmc_virt_to_phys", S_IRUSR, NULL, NULL, &fops);
6855
return 0;
6956
}
7057

7158
static void myexit(void)
7259
{
7360
debugfs_remove(debugfs_file);
74-
kfree((void *)k);
61+
kfree((void *)kmalloc_ptr);
7562
}
7663

7764
module_init(myinit)

0 commit comments

Comments
 (0)