9

I want to set a range of memory as uncacheable (Linux, x86-86) from within a user-space process. This question comes close, but only mentions the MTRR registers which work with physical memory. I want to do this using the PAT tables as they offer finer-grained control, they allow virtual memory to be set uncacheable on a page-by-page basis.

The Linux documentation, Documentation/x86/pat.txt, suggests there should be something with mmap and a SYNC flag, but I can't find how to do this in practice. Ideally, I'd like to use a call such as mprotect(address, range, O_UNCACHABLE).

Community
  • 1
  • 1
Wim
  • 11,091
  • 41
  • 58
  • Are `madvise`/`mlock` sufficient? – Brian Cain Sep 23 '12 at 05:37
  • No, it looks like these affect the virtual mapping only (whether pages are in RAM or on disk), but they don't seem to affect caching... – Wim Sep 25 '12 at 13:17
  • Why do you want virtual pages to be uncachable? Under normal circumstances you use uncached memory only for device mapped IO or memory. That's why the interfaces provide access to physical memory only. Switching caching off is very likely a bad idea. x86 has special instructions to hint that reuse is unlikely and caching is not desired. That should be more than enough to tune for performance. If you know what you are doing and really need to turn caching off you could write a kernal module to provide the user level interface yourself. But this is this really what you want to do? – Mackie Messer Oct 17 '12 at 00:45
  • This is for a research project (with the eventual goal of proposing hardware modifications if the idea turns out useful). If the initial validation can be done on real hardware, that's always preferable, since the alternative is to use an architectural simulator which is up to a million times slower than hardware... – Wim Oct 18 '12 at 07:13
  • non x86 superst: https://stackoverflow.com/questions/885658/is-it-possible-to-allocate-in-user-space-a-non-cacheable-block-of-memory-on-li – Ciro Santilli OurBigBook.com Aug 24 '17 at 07:24

2 Answers2

3

I would recommend writing a kernel module to provide the necessary interface for a user–level process. Inside the kernel module you can use set_memory_uc to control the page attributes.

Regarding the simulator: it should be around ten to thousand times slower—not a million times—unless you simulate at the gate level. Don't forget to consider the time it takes you to write the kernel module. If it takes you a few weeks to write and debug the module you may be better off using a simulator for a one time experiment.

Mackie Messer
  • 7,118
  • 3
  • 35
  • 40
0

According to various developers questions on ARM platforms the code would be:

fd = open("/dev/mem", O_RDWR|O_SYNC);
uptr = mmap(addr, length, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_LOCKED, fd, off);

ref:

Steve-o
  • 12,678
  • 2
  • 41
  • 60
  • With this code you can access a physical address range but you must also reserve it or somehow tell the kernel not to use it. If you don't bad things will happen... – Mackie Messer Oct 19 '12 at 21:05