Friday, June 20, 2008

AMD64 #1 - Long Mode

Backing to old days the first tree of microkernel was targeted to the x86 (32bit), going deeper the parent of all post projects (ilix) was targeted to embedded hardware i.e. arm and one internal architecture that was 24 bit.
Nowadays we're have a public accessible and cheep 64bit long architecture, x86 continuing but with 64bit long extension, but on real - it's looks different.
allows many good extended features compairing with basic x86 32bit long structure. To use AMD64 features on full filled hand we're must operate in long mode (amd64 specific mode).
On truth, long mode is a mixed mode i.e. it operates on 64-bit mode and compatibility mode at one time. It has self minuses - it's a flat memory model. But it has 64-bit addressing and we're don't need for tricks with extension from intel to use more than 4Gb address space addressing, that relatively ugly and looks like an ugly hack.
Like a microkernel developer and low developer at one time - the first problem is initialization.
I've read AMD64 documentation directly from AMD, but there are no anything to really help with it, not at all, but if you want to make a sense quickly it's not usable.
I've designed a trick with it, saying simply it consist from following steps :
32bit code:
  • - initialize stack pointer (regarding your boot method and loader, I'm using grub and multiboot)
  • - init bootstrap GDT
  • - jump to 'meet point' within existing GDT
  • - save parameters from grub (will be need on if you using grub)
  • - we're falled on 32bit protected mode (legacy mode on AMD64)
  • - check for varios CPU features (it must be made if you want to made all like it must be in good kernels)
  • - check for AMD64 long mode support is a really needed (you should do it, otherwise you can except some bug - I don't know why - check it if I correct)
  • - all is ok, - enabling 64bit page translations (regarding to documentation set - cr4.pae=1)
  • - setup pages tables
  • - enable long mode (via EFER reg - setting LME to 1)
  • - enable paging in long mode (it will activate long mode and we're falling to compatibility mode)
  • - just jump to your 64bit code
64bit code:
  • - do your stuff, have a lot of fun ... ;)
I'm sure that there are direct long mode switching, but for me it was more quickly to make it like I've describe.

No comments: