Can't write to CR0

	ULONG64 Cr0 = __readcr0();
	__writecr0(Cr0 & ~ (1ULL << 16));

I am getting a STATUS_PRIVILEGED_INSTRUCTION with SYSTEM_THREAD_EXCEPTION bugcheck. I have no idea why I cannot write to it. I can write to it in windbg, but not in my driver's code. I tried raising the IRQL, it didn't help.

Don't do that. End of story.

Do you disable interrupt before you read or write CR0?

It would be a big surprise if you could do this successfully. The much bigger question is why would you want to? What objective do you think you can achieve this way?

I am just going to test a few things in my hypervisor. In addition to original problem, I also seem to cannot enable VMXE on single-core systems too. I get the same problem when I try to modify CR4. (Works fine on multi-core systems.)

writing a hypervisor is a large task and it requires a detailed understanding of both the host OS (in this case Windows) and the machine architecture that the guests will expect to operate in.

That's true even if you don't want to implement more advanced features like para-virtualization for thread sync privatives and scheduling. Or IO virtualization, hardware pass through etc.

Among other things, that means that you should know how the debugger does it, and that you should use the debugger (including your own custom debugger extensions).

Unfortunately, sharing too many details has led to bad consequences in the past. So if you want to go further, this forum is probably too public of a place