How to match two virtual addresses in one physical memory on Linux?

I ran into a rather complicated problem. I am trying to get 2 areas of virtual memory pointing to the same physical memory. The point is to have different page protection settings in different areas of memory.

The user seems to have a solution on this forum, but it seems like it's a hack, and it's pretty clear that something better can be done in terms of performance: http://www.linuxforums.org/forum/programming-scripting /19491-map-two-virtual-memory-addres-same-physical-page.html

As I came across the same problem, I want to give a chance here to find out if anyone has a better idea. Don't be afraid to mention the dirty details behind the hood, that's what this question is about.

Thanks in advance.

+6
source share
3 answers

I am trying to get two areas of virtual memory pointing to the same physical memory.

mmap the same area in the same file twice, or use system V shared memory (which does not require mapping the file to memory).

+3
source

I suggest that if you don't like Sys V sharing, you can use POSIX shared memory objects . They are not very popular, but are available at least on Linux and BSD.

Once you get fd with shm_open , you can immediately call shm_unlink . Then no other process can connect to the same shared memory, and you can mmap it several times. However, a small racing period is available.

+3
source

If you are a root user, you can mmap("/dev/mem", ...) , but there are caveats in the new kernels, see access to mmaped / dev / mem?

+1
source

Source: https://habr.com/ru/post/896790/


All Articles