How to access device in user space via `mmap`
Tert-Butyllithium

firstly we need to check .config in Linux if:

1
2
CONFIG_STRICT_DEVMEM=y
# CONFIG_IO_STRICT_DEVMEM is not set

or CONFIG_IO_STRICT_DEVMEM=y, which means to disallow to access device memory. So we need to ensure # CONFIG_IO_STRICT_DEVMEM is not set

1
2
int fd = open("/dev/mem", O_RDWR | O_SYNC);
void* base = mmap(NULL, 0x1000, PROT_READ, MAP_SHARED, fd, REG_ADDR);

Here, REG_ADDR means the address of mapped regitsers, which can be found from /proc/iomem. Bascially, the effect is the same as ioremap in kernel space