Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping physical addresses to virtual address linux

I am working on a small embedded system. When my linux boots up into user space, I know where are my devices in the physical memory. I want to map them into user space virtual addresses. Currently, I am doing it through a kernel module. I use vmalloc/kmalloc (depending on the size) and then I use ioremap_page_range on that returned virtual addresses to map my physical addresses. I dont think that is the correct way to go about. First of all I am allocating memory and then I am asking kernel to remap that virtual address space to some different physical address space. (Initially mapped physical->virtual in vmcall/kmalloc is kinda useless as I dont care about those physical pages. This is definitely not good.)

Instead of this is there a better way to map the known physical memory into user space process. (I know other than my user space process, no one gonna touch that memory.)

Thanks

like image 272
agent.smith Avatar asked Jul 23 '12 23:07

agent.smith


People also ask

How do you map a physical address to a virtual address?

MMU(Memory Management Unit) :The run time mapping between Virtual address and Physical Address is done by a hardware device known as MMU. In memory management, the Operating System will handle the processes and move the processes between disk and memory for execution . It keeps track of available and used memory.

Can two different virtual addresses map to the same physical address in Linux?

Hence, it is possible for two virtual addresses VA1 and VA2 to point to the same physical address PA.

How mmap is implemented in Linux?

To assign a mmap() operation to a driver, the mmap field of the device driver's struct file_operations must be implemented. If that is the case, the user space process can then use the mmap() system call on a file descriptor associated with the device.

Which table store the mapping of virtual address and physical address?

The page table is where the operating system stores its mappings of virtual addresses to physical addresses, with each mapping also known as a page table entry (PTE).


1 Answers

What you are trying to do is accessing what is called IO memory. I can only encourage you to read the Linux Device Drivers (LDD) book and more specifically the chapter 9.

To "allocate" such an area, you need to call

struct resource *request_mem_region(unsigned long start, unsigned long len, char *name)

. Before your driver can access it, you have to assign it a virtual address, this is done with a call to

void *ioremap(unsigned long phys_addr, unsigned long size)

To ensure that your driver will then work on different architectures/platforms, be sure to use some accessor function to such areas ( ioread8/16/32 or iowrite8/16/32 and all of their variants).

like image 195
Longfield Avatar answered Sep 24 '22 08:09

Longfield