Mapping Physical Addresses to Virtual Address Linux

Mapping physical addresses to virtual address linux

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).

How are same virtual address for different processes mapped to different physical addresses

Thanks for all answers. The actual point that i dont know is that how same virtual address of different processes does not clash with each other's physical correspondent. I found the answer in the link below, each process has its own page table.

http://tldp.org/LDP/tlk/mm/memory.html

How are virtual addresses corresponding to kernel stack mapped?

Note: This is the OS agnostic answer. Details do vary slightly with OS in question (e.g. Darwin and continuations..), and possibly with architectural (ARMv8, x86, etc) implementations.

When a process performs a system call, the user mode state (registers) is saved, including the user mode stack pointer. At that point, a kernel mode stack pointer is loaded, which is usually maintained somewhere in the thread control block.

You are correct in saying that there is only one kernel space. What follows is, that (in theory) one thread in kernel space could easily see and/or tamper with any others in kernel space (just like same process threads can "see" each other in user space) This, however, is (almost always) in theory only, since the kernel code presumably respects memory boundaries (as is assumed user mode does, with thread local storage, etc). That said, "almost always", because if the kernel code can be exploited, then all of kernel memory will be laid bare to the exploiter, and potentially read and/or compromised.

How does an OS deal in virtual addresses and physical addresses

Most OSes map all of physical memory to a range of virtual addresses, with a kernel-only mapping. e.g. https://www.kernel.org/doc/Documentation/x86/x86_64/mm.txt is Linux's memory map.

Note the up-to-64TB "direct mapping" region. If you know a physical address, you access 0xffff880000000000 + phys_addr in kernel virtual address space.

Linux uses 1G hugepages for the direct mapping, so TLB misses are rare.


Things get complicated when there isn't enough virtual address space to easily map all physical RAM into part of virtual address space, like in 32-bit with more than 2GiB of RAM. Then the kernel has to treat part of physical RAM as "highmem" that can't be used directly for some things (e.g. for page tables).



Related Topics



Leave a reply



Submit