Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does mprotect flush the instruction cache on ARM Linux?

I am writing a JIT on ARM Linux that executes an instruction set that contains self-modifying code. The instruction set does not have any cache flush instructions (similar to x86 in that respect).

If I write out some code to a page and then call mprotect on that page, is that sufficient to invalidate the instruction cache? Or do I also need to use the cacheflush syscall on those pages?

like image 651
Adam Goode Avatar asked May 06 '10 00:05

Adam Goode


2 Answers

You'd expect that the mmap/mprotect syscalls would establish mappings that are updated immediately, and need no further interaction to use the memory ranges as specified. I see that the kernel does indeed flush caches on mprotect. In that case, no cache flush would be required.

However, I also see that some versions of libc do call cacheflush after mprotect, which would imply that some environments would need the caches flushed (or have previously). I'd take a guess that this is a workaround to a bug.

You could always add the call to cacheflush; although it's extra code, it shouldn't be to harmful - at worst, the caches will already be flushed. You could always write a quick test and see what happens...

like image 176
Jeremy Kerr Avatar answered Oct 01 '22 01:10

Jeremy Kerr


In Linux specifically, mprotect DOES cacheflush all caches since at least version 2.6.39 (and even before that for sure). You can see that in the code: https://elixir.bootlin.com/linux/v2.6.39.4/source/mm/mprotect.c#L122 .

If you are writing a POSIX portable code, I would call cacheflush as the standard C library is not demanding such behavior from the kernel, nor from the implementation.

Edit: You should also be carefull and check what flush_cache_range does in the specific architecture you are implementing for, as in some architecture (like ARM64) this function does nothing...

like image 26
matan7890 Avatar answered Oct 01 '22 01:10

matan7890