You can do this on UNIXish with a combination of __attribute__((aligned (PAGESIZE))) and mprotect . On Windows, I think there is an equivalent to mprotect , but it is also limited to one page of memory.
The reason it is impossible to protect things with a higher degree of granularity is because the memory is accessed using hardware rather than software. This would be very slow if each individual memory access required passing through some data structure to check if the page is protected.
source share