3 #define LOAD_ADDRESS 0x100000 // 1MB
4 #define PGTABLE_BASE 0x8000
10 #define MB_MAGIC 0x1BADB002
11 #define MB_FLAGS (1<<0 | 1<<1 | 1<<16) // page align, meminfo, rest of header
12 #define MB_CHECKSUM (-(MB_MAGIC + MB_FLAGS))
16 .long MB_MAGIC // magic
17 .long MB_FLAGS // flags
18 .long MB_CHECKSUM // checksum
19 // address part of the header
20 .long multiboot // address of header
21 .long LOAD_ADDRESS // load address of the binary
22 .long _end // end of all the text + data
23 .long 0 // no bss, linker script combines with data
24 .long _start // entry point
27 // zero out 3 pages of ram where we're going to build some page tables
29 mov $PGTABLE_BASE, %edi
33 // top level page table
34 mov $((PGTABLE_BASE + 0x1000) | 0x3), %eax
35 mov %eax, PGTABLE_BASE
36 mov $((PGTABLE_BASE + 0x2000) | 0x3), %eax
37 mov %eax, (PGTABLE_BASE + 0x1000)
39 // 2MB pages to identity map the first 16MB ram
40 mov $(0 | (1<<7) | 3), %eax
41 mov %eax, (PGTABLE_BASE + 0x2000)
43 mov %eax, (PGTABLE_BASE + 0x2008)
45 mov %eax, (PGTABLE_BASE + 0x2010)
47 mov %eax, (PGTABLE_BASE + 0x2018)
49 mov %eax, (PGTABLE_BASE + 0x2020)
51 mov %eax, (PGTABLE_BASE + 0x2028)
53 mov %eax, (PGTABLE_BASE + 0x2030)
55 mov %eax, (PGTABLE_BASE + 0x2038)
57 // set the page table base
58 mov $PGTABLE_BASE, %eax
76 // enable paging, which will really enable long mode (CR0.PG)
81 ljmp $0x08,$(long_64_mode)
96 // ebx should contain a pointer to the multiboot info structure
103 // first entry, also the gdt descriptor
107 // entry 64bit code segment
108 .long 0x00000000 // base & limit are ignored
109 .long 0x00af9a00 // type: 64 bit, code, <present>, priviledge 0