Getting Memory Map of Every Device in Linux

How to find the memory map of a device?

If it's a PCI device, lspci -vvv will show you.

You can also check /proc/iomem and /proc/ioport for memory and port mappings, respectively.

How to access physical addresses from user space in Linux?

You can map a device file to a user process memory using mmap(2) system call. Usually, device files are mappings of physical memory to the file system.
Otherwise, you have to write a kernel module which creates such a file or provides a way to map the needed memory to a user process.

Another way is remapping parts of /dev/mem to a user memory.

Edit:
Example of mmaping /dev/mem (this program must have access to /dev/mem, e.g. have root rights):

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>

int main(int argc, char *argv[]) {
if (argc < 3) {
printf("Usage: %s <phys_addr> <offset>\n", argv[0]);
return 0;
}

off_t offset = strtoul(argv[1], NULL, 0);
size_t len = strtoul(argv[2], NULL, 0);

// Truncate offset to a multiple of the page size, or mmap will fail.
size_t pagesize = sysconf(_SC_PAGE_SIZE);
off_t page_base = (offset / pagesize) * pagesize;
off_t page_offset = offset - page_base;

int fd = open("/dev/mem", O_SYNC);
unsigned char *mem = mmap(NULL, page_offset + len, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, page_base);
if (mem == MAP_FAILED) {
perror("Can't map memory");
return -1;
}

size_t i;
for (i = 0; i < len; ++i)
printf("%02x ", (int)mem[page_offset + i]);

return 0;
}

mapping a memory region from kernel

You need to remap the memory region with something like ioremap() after you have requested it.

Then, as Tsyvarev and others mentioned, create and export a function in your "parent" driver that returns the mapped memory.

Here is some rough code:

void * mapped_mem;

void * map_addr(unsigned int phy_addr, char * name) {

struct resource * resource;
void * mapped_mem;

resource = request_mem_region(phy_addr, page_size * 4, name);
// check for errors

mapped_mem= ioremap_nocache(phy_addr, page_size * 4);
// check for errors
return mappedMem;

//handle errors
}

void * get_mapped_addr(void) {
return mapped_mem
}

EXPORT_SYMBOL( get_mapped_addr);

Now, mapped_mem should actually be tracked as part of your devices private info, but I figure thats beyond the scope of the question. Also, make sure to check for all possible errors. Make sure that request_mem_region() returns something >0 and not Null.



Related Topics



Leave a reply



Submit