Scenario 1), I want to modify the CR3 of my thread and do something like this:
my thread:
1. modify processor's CR3
2. do something (i.e. impersonating another process context) using the new CR3
3. restore the processor's CR3
I am not sure what Windows will do when the thread context switches, say, during step 2 above (some advice welcomed).
a. if it saves up the current value of CR3 into KTHREAD, then, in theory, my modification will be visible to any other anti-whatever threads running in another processor since they can scan my KTHREAD, and with some luck in timing, can retrieve the saved modified CR3 value (_KTHREAD.ApcState->Process.DirectoryTableBase, IIRC). This is something undesirable even the chance is low. So I want to enclose the above 3 steps by CLI/STI to prevent context switching.
b. if Windows does not save up the current value of CR3, that means it copy into the CR3 the value from _KTHREAD.ApcState->Process.DirectoryTableBase directly, then any context switching between (1) - (3) will overwrite my modification and must be prevented
In this scenario, raising the IRQL to DISPATCH_LEVEL before the block may achieve the same effect but CLI/STI sounds cleaner
===========================
Scenario 2), I want to modify some struct in memory:
my thread:
1. modify some structures in memory
2. do something using the modified memory
3. restore the modified structures
Also, any anti-whatever threads running in another processor can scan the memory, and, with some luck having this happens during step 2), can detect my modifications. Hence I have to make sure the block finishes as quickly as possible. In particular it should not be pre-empted by some unknown badly written 3rd party device ISR that hangs around for too long during step 2). So the block should be enclosed by CLI/STI. (raising it to DISPATCH_LEVEL here won't work as device interrupt can interrupt it)