tags:

views:

259

answers:

1

So, I'm disassembling a bit of code for fun and no profit (other than the joy of knowledge, that is), and I come across this bit:

mov eax, cr3
mov cr3, eax

Is this doing anything other than just masturbating with cr3? If so, what? This is x86 low-level (bios/firmware/before boot loader) intialization code. We haven't even setup up caches yet.

+12  A: 

It is flushing the TLBs (Translation Lookaside Buffers) by loading cr3 with itself.

Intel even mentions the code in their "Intel 64 and IA-32 Architectures Software Develoment Manual Volume 3A - System Programming Guide".

mov EAX,CR3  ; invalidate the TLB
mov CR3,EAX  ; by copying CR3 to itself

You can find that and many more handy manuals at:

http://www.intel.com/products/processor/manuals/index.htm

George Phillips
What's the difference by doing directly "mov cr3, cr3"?
Eliseo Ocampos
@Eliseo - That's not a legal instruction. There are distinct "Load from register into control register" and "Load control register into register" instructions. There is no control register -> control register mov instruction.
Michael
:) I didn't realize that detail, thanks for the answer!
Eliseo Ocampos