Merge freeldr from amd64 branch:
[reactos.git] / reactos / boot / freeldr / freeldr / arch / amd64 / arch.S
index 466dabf..33910ff 100644 (file)
@@ -2,10 +2,10 @@
 .text
 .code16
 
-//.org 0x8000
+#define ASM
+#include <arch.h>
 
-#define STACK16ADDR    0x7000  /* The 16-bit stack top will be at 0000:7000 */
-#define STACK64ADDR    0x74000 /* The 64-bit stack top will be at 0x74000 */
+//.org 0x8000
 
 .global RealEntryPoint
 RealEntryPoint:
@@ -92,52 +92,51 @@ x86_16_EnableA20:
 
 
 /*
- * We defines one 2MB page at the start of memory, so we can access the first 
- * 2MBs as if paging was disabled
+ * We define 512 2MB pages at the start of memory, so we can access the first 
+ * 1 GB as if paging was disabled
  */
-
-#define PML4_PAGENUM 60 // Put it high enough so it doesn't interfere with freeldr
-
-#define PAGESIZE 4096
-#define PDP_PAGENUM (PML4_PAGENUM + 1)
-#define PD_PAGENUM (PDP_PAGENUM + 1)
-#define PML4_ADDRESS (PML4_PAGENUM * PAGESIZE)
-#define PDP_ADDRESS (PDP_PAGENUM * PAGESIZE)
-#define PML4_SEG (PML4_ADDRESS / 16)
-
 x86_16_BuildPageTables:
        pusha
        push es
 
-       mov ax, PML4_SEG
+       /* Get segment of pml4 */
+       mov eax, offset _pml4_startup
+       shr eax, 4
        mov es, ax
        cld
        xor di, di
 
        /* One entry in the PML4 pointing to PDP */
-       mov ax, ((PDP_PAGENUM << 12) & 0xffff) | 0x00f
-       stosw
-       mov ax, (PDP_PAGENUM >> 4)
-       stosw
-       xor ax,ax
-       mov cx, 0x07fe
-       rep stosw
+       mov eax, offset _pdp_startup
+       or eax, 0x00f
+       stosd
+       /* clear rest */
+       xor eax, eax
+       mov cx, 0x03ff
+       rep stosd
 
        /* One entry in the PDP pointing to PD */
-       mov ax, ((PD_PAGENUM << 12) & 0xffff) | 0x00f
-       stosw
-       mov ax, (PD_PAGENUM >> 4)
-       stosw
-       xor ax,ax
-       mov cx, 0x07fe
-       rep stosw
-
-       /* One entry in the PD defining a 2MB page */
-       mov ax, 0x018f
-       stosw
-       xor ax,ax
-       mov cx,0x07ff
-       rep stosw
+       mov eax, offset _pd_startup
+       or eax, 0x00f
+       stosd
+       /* clear rest */
+       xor eax, eax
+       mov ecx, 0x03ff
+       rep stosd
+
+       /* 512 entries in the PD defining a 2MB page each */
+       mov ecx, 512
+       mov eax, 0x008f
+
+Bpt2:
+       mov es: [di], eax
+       mov dword ptr es: [di + 4], 0
+       add eax, 512 << 12 // add 512 4k pages
+       add di, 8
+
+       /* Loop it */
+       dec cx
+       jnz Bpt2
 
        /* Return */
        pop es
@@ -145,11 +144,6 @@ x86_16_BuildPageTables:
        ret
 
 
-#define LMODE_CS       0x08
-#define LMODE_DS       0x10
-#define RMODE_CS       0x18    /* RMode code selector, base 0 limit 64k */
-#define RMODE_DS       0x20    /* RMode data selector, base 0 limit 64k */
-
 //.global x86_16_SwitchToLong
 x86_16_SwitchToLong:
        cli
@@ -170,7 +164,7 @@ x86_16_SwitchToLong:
        mov eax, 0x00a0                 // Set PAE and PGE: 10100000b
        mov cr4, eax
 
-       mov edx, PML4_ADDRESS   // Point cr3 at PML4
+       mov edx, offset _pml4_startup // Point cr3 at PML4
        mov cr3, edx
 
        mov ecx, 0xC0000080             // Specify EFER MSR
@@ -258,6 +252,7 @@ BeReal:
        ret
 
 
+
 /** Some data *****************************************************************/
 
 .code64
@@ -272,15 +267,16 @@ code64ret:
        .quad 0
 
 gdt:
-       .quad 0x0000000000000000
-       .quad 0x0020980000000000
-       .quad 0x0000900000000000
+       .quad 0x0000000000000000 /* 00: NULL descriptor */
+       .quad 0x0000000000000000 /* 08:  */
+       .quad 0x0020980000000000 /* 10: long mode cs */
+       .quad 0x0000900000000000 /* 18: long mode ds */
        .word   0xFFFF, 0x0000, 0x9E00, 0x0000 /* 16-bit real mode CS */
        .word   0xFFFF, 0x0000, 0x9200, 0x0000 /* 16-bit real mode DS */
 
 /* GDT table pointer */
 gdtptr:
-       .word   0x27            /* Limit */
+       .word   0x2f            /* Limit */
        .long   gdt                     /* Base Address */