Wednesday, February 23, 2022

[SOLVED] How do you register a region of user space memory in a kernel module?

Issue

I am working on a Linux module to interface with a third-party device. When this device is ready to give my module information, it writes directly to the RAM memory address 0x900000.

When I check /proc/iomem, I get:

00000000-3fffffff: System Ram
   00008000-00700fff: Kernel code
   00742000-007a27b3: Kernel datat

From, my understanding, this means that it is writing to an address that is floating out in the middle of user-space.

I know that this is not an optimal situation and it would be better to be able to use memory-mapped addresses/registers, but I don’t have the option of changing the way it works right now.

How do I have my kernel module safely claim the user space memory space from 0x900000 to 0x901000?

I tried mmap and ioremap but those are really for memory-mapped registers, not accessing memory that already ‘exists’ in userspace. I believe that I can read/write from the address by just using the pointer, but that doesn’t prevent corruption if that region is allocated to another process.


Solution

You can tell the kernel to restrict the address for kernel space by setting the mem parameter in the bootargs :

mem=1M@0x900000 --> instructs to use 1M starting from 0x900000

you can have multiple mem in boot args example: mem=1M@0x900000 mem=1M@0xA00000

Following command should tell you the memory region allocated to the kernel:

cat /proc/iomem | grep System


Answered By - Prabhakar Lad
Answer Checked By - Marie Seifert (WPSolving Admin)