0

If I have an address (pointer) in virtual addressing area of current process to the pinned (page-locked) memory, then how can I get an address (pointer) in physical addressing area, of this memory region, by using POSIX?

  • CPU: x86
  • OS: Linux 2.6 and Windows 7/8(Server 2008R2)
Alex
  • 12,578
  • 15
  • 99
  • 195
  • Such information has basically no meaning in user space, as the physical address could change all the time. – Kerrek SB Nov 05 '13 at 14:48
  • I'm not even sure this is possible. – nic Nov 05 '13 at 14:51
  • Similar question: http://stackoverflow.com/questions/6245850/linuxubuntu-c-language-virtual-to-physical-address-translation – Michael Nov 05 '13 at 14:51
  • 2
    POSIX... no way. Kernel-mode driver, yes. Otherwise it's not useful anyway. – Damon Nov 05 '13 at 14:52
  • But if I allocate pinned (page-locked) memory, and then I know that it can't be swapped, how can I get a physical address of this memory region and does it make sense? – Alex Nov 05 '13 at 15:00

1 Answers1

2

You cannot access physical addresses in user space. Everything you do goes through the MMU and the page tables. Even if you pin a page, the kernel may still move it around in physical memory.

Even if you got it, what would it do for you? A userspace process cannot access memory directly by physical access. Only kernel mode can.

If you really need the functionality (although I still can't image any way of using the information), you have to write a kernel mode driver.

Linuxios
  • 34,849
  • 13
  • 91
  • 116
  • Thanks! An example if I have two mapped memory regions to the CPU's physical addressing of two devices via PCI-Express: FPGA and GPU. And I want that FPGA and GPU can comunicate and data transfer without CPU. FPGA and GPU can use only physical address, and can't use virtual address, isn't it? – Alex Nov 05 '13 at 15:17
  • 1
    @Alex: Yes, but communication with that hardware should be done in kernel space, and necessary results and arguments passed to and from userspace. The biggest reasons that this is enforced are systems stability (random process corrupting GPU memory) and security (malicious process using the GPU's DMA to overwrite other kernel/user memory with malicious code). – Linuxios Nov 05 '13 at 15:20
  • Then for communication between FPGA and GPU without CPU, do I must to write driver and use it's API to get a pointers of CPU's physical addressing area, and then put these pointers to the devices (FPGA, GPU)? – Alex Nov 05 '13 at 15:24
  • 1
    @Alex: Not exactly. You need to communicate with them in the driver, and pass results to the userspace process. However, I'd recommend not doing this yourself. Is there any reason why using OpenGL (graphics) or OpenCL (GPU computing) won't work for your purposes? These are mature, stable libraries that will save you possible many months of work trying to work with your GPU directly, and then months more to support more kinds of GPUs. – Linuxios Nov 05 '13 at 15:27
  • Yes, but even if I use OpenCL, I want to know how does it work. But how can I communicate with them in the driver if I want to communicate between FPGA and GPU directly, without any interrupts on CPU(where this driver works)? – Alex Nov 05 '13 at 15:40
  • 1
    @Alex: Let the FPGA and the GPU communicate, or both of them with the CPU? – Linuxios Nov 05 '13 at 15:41
  • FPGA and GPU connected via PCIe and via CPU( FPGA <-PCIe-> CPU <-PCIe-> GPU), but communicate between FPGA and GPU directly by PCIe and by physical memory mapped region, without CPU (not both of the with the CPU). – Alex Nov 05 '13 at 16:16