I've read on this topic for many many hours and tried a plethora of different strategies but can't get this to work in a stable manner.
I am operating in the Windows kernel. I have allocated user-space memory for a process for their use. This memory needs to be executable. The solution I have got the farthest with is traversing the page table to find the matching PTE for the allocated user space memory and clearing the NX bit. This "works", but the system eventually freezes. WinDbg kernel debugger sometimes reports deadlocks.
I assume this is because there is some internal structure the Windows kernel uses to operate on these tables, but of course I do not have access. Does anybody know how to safely find and operate on PTEs? I stress that this issue does not happen at all when I do not run this code. I am 100% certain that this PTE modification, and not another part of my code is causing this.