summaryrefslogtreecommitdiffstats
path: root/extra/source/grub/grub-0.97-x86_64.patch
blob: 480e38fceb7bd66a18f4ab9adde57b119fa0e985 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
diff -Naur grub-0.97.orig/docs/multiboot.h grub-0.97.patched/docs/multiboot.h
--- grub-0.97.orig/docs/multiboot.h	2003-07-09 05:45:36.000000000 -0600
+++ grub-0.97.patched/docs/multiboot.h	2006-08-26 22:44:02.000000000 -0600
@@ -44,76 +44,81 @@
 /* Do not include here in boot.S.  */
 
 /* Types.  */
+#ifndef __x86_64__
+typedef unsigned long UINT32;
+#else
+typedef unsigned      UINT32;
+#endif
 
 /* The Multiboot header.  */
 typedef struct multiboot_header
 {
-  unsigned long magic;
-  unsigned long flags;
-  unsigned long checksum;
-  unsigned long header_addr;
-  unsigned long load_addr;
-  unsigned long load_end_addr;
-  unsigned long bss_end_addr;
-  unsigned long entry_addr;
+  UINT32 magic;
+  UINT32 flags;
+  UINT32 checksum;
+  UINT32 header_addr;
+  UINT32 load_addr;
+  UINT32 load_end_addr;
+  UINT32 bss_end_addr;
+  UINT32 entry_addr;
 } multiboot_header_t;
 
 /* The symbol table for a.out.  */
 typedef struct aout_symbol_table
 {
-  unsigned long tabsize;
-  unsigned long strsize;
-  unsigned long addr;
-  unsigned long reserved;
+  UINT32 tabsize;
+  UINT32 strsize;
+  UINT32 addr;
+  UINT32 reserved;
 } aout_symbol_table_t;
 
 /* The section header table for ELF.  */
 typedef struct elf_section_header_table
 {
-  unsigned long num;
-  unsigned long size;
-  unsigned long addr;
-  unsigned long shndx;
+  UINT32 num;
+  UINT32 size;
+  UINT32 addr;
+  UINT32 shndx;
 } elf_section_header_table_t;
 
 /* The Multiboot information.  */
 typedef struct multiboot_info
 {
-  unsigned long flags;
-  unsigned long mem_lower;
-  unsigned long mem_upper;
-  unsigned long boot_device;
-  unsigned long cmdline;
-  unsigned long mods_count;
-  unsigned long mods_addr;
+  UINT32 flags;
+  UINT32 mem_lower;
+  UINT32 mem_upper;
+  UINT32 boot_device;
+  UINT32 cmdline;
+  UINT32 mods_count;
+  UINT32 mods_addr;
   union
   {
     aout_symbol_table_t aout_sym;
     elf_section_header_table_t elf_sec;
   } u;
-  unsigned long mmap_length;
-  unsigned long mmap_addr;
+  UINT32 mmap_length;
+  UINT32 mmap_addr;
 } multiboot_info_t;
 
 /* The module structure.  */
 typedef struct module
 {
-  unsigned long mod_start;
-  unsigned long mod_end;
-  unsigned long string;
-  unsigned long reserved;
+  UINT32 mod_start;
+  UINT32 mod_end;
+  UINT32 string;
+  UINT32 reserved;
 } module_t;
 
 /* The memory map. Be careful that the offset 0 is base_addr_low
    but no size.  */
 typedef struct memory_map
 {
-  unsigned long size;
-  unsigned long base_addr_low;
-  unsigned long base_addr_high;
-  unsigned long length_low;
-  unsigned long length_high;
-  unsigned long type;
+  UINT32 size;
+  UINT32 base_addr_low;
+  UINT32 base_addr_high;
+  UINT32 length_low;
+  UINT32 length_high;
+  UINT32 type;
 } memory_map_t;
 
 #endif /* ! ASM */
diff -Naur grub-0.97.orig/docs/x86_64.txt grub-0.97.patched/docs/x86_64.txt
--- grub-0.97.orig/docs/x86_64.txt	1969-12-31 17:00:00.000000000 -0700
+++ grub-0.97.patched/docs/x86_64.txt	2006-10-10 10:39:42.000000000 -0600
@@ -0,0 +1,81 @@
+[PATCH] - Make GRUB recognize and boot an x86_64 elf image
+
+
+HOW IT WORKS
+------------
+*  Grub interface is unchanged.
+*  Upon passing an elf-x86_64 file via "kernel=", GRUB will load and parse it
+   correctly.
+*  Upon the "boot" directive, GRUB will switch from protected 32-bit
+   mode to long 64-bit mode and transfer control to the image's entrypoint.
+
+
+INSTALL
+-------
+cd grub-0.97
+patch -p1 < grub-x86_64-elf.patch
+./configure
+make
+su -c "make install"
+
+Note:  You do not need to re-install your bootsectors, but you WILL need
+       to copy the modified stage2 file(s) from /usr/share/grub to
+       your actual boot filesystem (usually /boot/grub for GNU/Linux).
+
+
+WHY?
+----
+Because in our world, all elves and elf images are created equal, and have
+been endowed with the ability to be bootable.  :-)
+
+
+THE NITTY GRITTY - GRUB SIDE
+----------------------------
+*  Since paging is manditory in 64-bit long mode, the first 4GB of physical
+   memory (whether present or not) is identity mapped into the linear
+   address space using 2MB pages.
+*  Paging data structures are written to physical 0x70000-0x75fff in the
+   same area reserved for the raw device buffer.  This happens after the
+   "boot" command is issued so the raw device buffer won't mind.  Paging
+   can be relocated and reconfigured after the kernel is running.
+*  Header files have been added to describe the x86_64 elf image structure.
+
+
+THE NITTY GRITTY - KERNEL SIDE
+------------------------------
+*  Parameters passed to the assembly entrypoint are unchanged including
+   magic number and pointer to multiboot info structure.
+*  The assembly entrypoint should be designated as ".code64".
+*  The Global Descriptor Table (GDT) shared by the GRUB and KERNEL sides
+   has been expanded to include 3 additional 64-bit ('L' bit set) segments:
+   0x28 (code) and 0x30 (data).
+*  An updated "multiboot.h" file is provided to reflect the difference in
+   "unsigned long" sizes between i386 and x86_64 architectures for use by
+   the client kernel.
+
+
+CAVEATS
+-------
+Possible collisions with existing GRUB patch sets are unknown.  Use "darcs"
+and worry not.
+
+   http://abridgegame.org/darcs
+
+(An intelligent alternative to cvs and greying hair in the distributed
+post-mainframe age.)
+
+
+DOCUMENTATION CHANGES
+---------------------
+This is it.  Sorry, stuff to do.
+
+
+APOLOGIES TO GRUB2
+------------------
+We would have liked to use GRUB2 for this, but needed it today for our
+kernel.  Of course there is probably no technical reason why this
+functionality cannot be in included in GRUB2 at a future time.  Thank you
+for a way cool bootloader and may all your boots be bogon (and Vogon) free.
+
+<mcnster@gmail.com>
+October 10, 2006
diff -Naur grub-0.97.orig/grub/asmstub.c grub-0.97.patched/grub/asmstub.c
--- grub-0.97.orig/grub/asmstub.c	2005-02-16 13:45:14.000000000 -0700
+++ grub-0.97.patched/grub/asmstub.c	2006-08-26 22:43:24.000000000 -0600
@@ -313,6 +313,12 @@
   stop ();
 }
 
+void
+multi_boot_64bit_doit (int start, int mb_info)
+{
+  stop ();
+}
+
 /* sets it to linear or wired A20 operation */
 void
 gateA20 (int linear)
diff -Naur grub-0.97.orig/stage2/asm.S grub-0.97.patched/stage2/asm.S
--- grub-0.97.orig/stage2/asm.S	2004-06-19 10:55:22.000000000 -0600
+++ grub-0.97.patched/stage2/asm.S	2006-09-25 14:59:48.000000000 -0600
@@ -1809,6 +1809,54 @@
 	/* error */
 	call	EXT_C(stop)
 
+ENTRY(multi_boot_64bit_doit)
+	call	EXT_C(stop_floppy)
+
+	/* dont squash these! */
+	movl	0x8(%esp), %esi
+	movl	0x4(%esp), %edi
+
+	cli
+
+	mov	%cr0, %eax
+        and 	$0x7fffffff, %eax
+        mov 	%eax, %cr0
+
+	/* enable pae */
+	mov 	%cr4, %eax
+	or 	$0x20, %eax
+	mov	%eax, %cr4
+
+	/* load cr3 with pml4 */
+	mov	$PML4, %eax
+	mov	%eax, %cr3
+
+	/* trigger long mode */
+	mov	$0xc0000080, %ecx
+	rdmsr
+	or 	$0x100, %eax
+	wrmsr
+
+	/* enable paging to actually switch modes */
+	mov 	%cr0, %eax
+        or 	$0x80000000, %eax
+	mov	%eax, %cr0
+
+	/* jump to relocation, flush prefetch queue, and reload %cs */
+	ljmp	$0x28, $longmode
+.code64
+longmode:
+	mov	$0x2BADB002, %eax
+
+	mov	%rsi, %rbx
+	and	$0xffffffff, %rsi
+
+	and	$0xffffffff, %rdi
+	call	*%rdi
+	/* NOTREACHED */
+	call	EXT_C(stop)
+
+.code32
 #endif /* ! STAGE1_5 */
 	
 /*
@@ -2341,27 +2389,35 @@
 
 	.p2align	2	/* force 4-byte alignment */
 gdt:
-	.word	0, 0
+	.word	0, 0				/* 0x0000 */
 	.byte	0, 0, 0, 0
 
-	/* code segment */
+	/* code segment */			/* 0x0008 */
 	.word	0xFFFF, 0
 	.byte	0, 0x9A, 0xCF, 0
 
-	/* data segment */
+	/* data segment */			/* 0x0010 */
 	.word	0xFFFF, 0
 	.byte	0, 0x92, 0xCF, 0
 
-	/* 16 bit real mode CS */
+	/* 16 bit real mode CS */		/* 0x0018 */
 	.word	0xFFFF, 0
 	.byte	0, 0x9E, 0, 0
 
-	/* 16 bit real mode DS */
+	/* 16 bit real mode DS/SS */		/* 0x0020 */
 	.word	0xFFFF, 0
 	.byte	0, 0x92, 0, 0
 
+	/* 64 bit long mode CS */		/* 0x0028 */
+	.word	0xFFFF, 0
+	.byte	0, 0x9A, 0xAF, 0
+
+	/* 64-bit long mode SS */		/* 0x0030 */
+	.word	0xFFFF, 0
+	.byte	0, 0x92, 0xAF, 0
 
 /* this is the GDT descriptor */
 gdtdesc:
-	.word	0x27			/* limit */
+	.word	0x33			/* limit */
 	.long	gdt			/* addr */
+	.long	0			/* in case we go to 64-bit mode */
diff -Naur grub-0.97.orig/stage2/boot.c grub-0.97.patched/stage2/boot.c
--- grub-0.97.orig/stage2/boot.c	2004-03-30 04:44:08.000000000 -0700
+++ grub-0.97.patched/stage2/boot.c	2006-08-25 22:20:17.000000000 -0600
@@ -23,10 +23,11 @@
 
 #include "freebsd.h"
 #include "imgact_aout.h"
-#include "i386-elf.h"
+#include "elf.h"
 
 static int cur_addr;
 entry_func entry_addr;
+unsigned long_64bit_mode = 0;
 static struct mod_list mll[99];
 static int linux_mem_size;
 
@@ -50,7 +51,7 @@
     {
       struct multiboot_header *mb;
       struct exec *aout;
-      Elf32_Ehdr *elf;
+      Elf_Ehdr *elf;
     }
   pu;
   /* presuming that MULTIBOOT_SEARCH is large enough to encompass an
@@ -100,21 +101,25 @@
        || pu.elf->e_ident[EI_OSABI] == ELFOSABI_FREEBSD
        || grub_strcmp (pu.elf->e_ident + EI_BRAND, "FreeBSD") == 0
        || suggested_type == KERNEL_TYPE_NETBSD)
-      && len > sizeof (Elf32_Ehdr)
-      && BOOTABLE_I386_ELF ((*((Elf32_Ehdr *) buffer))))
+      && len > SIZEOF_ELF_EHDR (pu.elf)
+      && (BOOTABLE_I386_ELF ((*((Elf32_Ehdr *) buffer))) 
+       || BOOTABLE_X86_64_ELF ((*((Elf64_Ehdr *) buffer)))))
     {
+      if (BOOTABLE_X86_64_ELF ((*((Elf64_Ehdr *) buffer))))
+        long_64bit_mode = 1;
+
       if (type == KERNEL_TYPE_MULTIBOOT)
-	entry_addr = (entry_func) pu.elf->e_entry;
+	entry_addr = (entry_func) E_ENTRY (pu.elf);
       else
-	entry_addr = (entry_func) (pu.elf->e_entry & 0xFFFFFF);
+	entry_addr = (entry_func) (E_ENTRY (pu.elf) & 0xFFFFFF);
 
       if (entry_addr < (entry_func) 0x100000)
 	errnum = ERR_BELOW_1MB;
 
       /* don't want to deal with ELF program header at some random
          place in the file -- this generally won't happen */
-      if (pu.elf->e_phoff == 0 || pu.elf->e_phnum == 0
-	  || ((pu.elf->e_phoff + (pu.elf->e_phentsize * pu.elf->e_phnum))
+      if (E_PHOFF (pu.elf) == 0 || E_PHNUM (pu.elf) == 0
+	  || ((E_PHOFF (pu.elf) + (E_PHENTSIZE (pu.elf) * E_PHNUM (pu.elf)))
 	      >= len))
 	errnum = ERR_EXEC_FORMAT;
       str = "elf";
@@ -590,39 +595,38 @@
     /* ELF executable */
     {
       unsigned loaded = 0, memaddr, memsiz, filesiz;
-      Elf32_Phdr *phdr;
+      Elf_Phdr *phdr;
 
       /* reset this to zero for now */
       cur_addr = 0;
 
       /* scan for program segments */
-      for (i = 0; i < pu.elf->e_phnum; i++)
+      for (i = 0; i < E_PHNUM (pu.elf); i++)
 	{
-	  phdr = (Elf32_Phdr *)
-	    (pu.elf->e_phoff + ((int) buffer)
-	     + (pu.elf->e_phentsize * i));
-	  if (phdr->p_type == PT_LOAD)
+	  phdr = (Elf_Phdr *) (E_PHOFF (pu.elf) + ((int) buffer)
+		+ (E_PHENTSIZE (pu.elf) * i));
+	  if (P_TYPE (pu.elf, phdr) == PT_LOAD)
 	    {
 	      /* offset into file */
-	      grub_seek (phdr->p_offset);
-	      filesiz = phdr->p_filesz;
+	      grub_seek (P_OFFSET (pu.elf, phdr));
+	      filesiz = P_FILESZ (pu.elf, phdr);
 	      
 	      if (type == KERNEL_TYPE_FREEBSD || type == KERNEL_TYPE_NETBSD)
-		memaddr = RAW_ADDR (phdr->p_paddr & 0xFFFFFF);
+		memaddr = RAW_ADDR (P_PADDR (pu.elf, phdr) & 0xFFFFFF);
 	      else
-		memaddr = RAW_ADDR (phdr->p_paddr);
+		memaddr = RAW_ADDR (P_PADDR (pu.elf, phdr));
 	      
-	      memsiz = phdr->p_memsz;
+	      memsiz = P_MEMSZ (pu.elf, phdr);
 	      if (memaddr < RAW_ADDR (0x100000))
 		errnum = ERR_BELOW_1MB;
 
 	      /* If the memory range contains the entry address, get the
 		 physical address here.  */
 	      if (type == KERNEL_TYPE_MULTIBOOT
-		  && (unsigned) entry_addr >= phdr->p_vaddr
-		  && (unsigned) entry_addr < phdr->p_vaddr + memsiz)
+		  && (unsigned) entry_addr >= P_VADDR (pu.elf, phdr)
+		  && (unsigned) entry_addr < P_VADDR (pu.elf, phdr) + memsiz)
 		real_entry_addr = (entry_func) ((unsigned) entry_addr
-						+ memaddr - phdr->p_vaddr);
+						+ memaddr - P_VADDR (pu.elf, phdr));
 		
 	      /* make sure we only load what we're supposed to! */
 	      if (filesiz > memsiz)
@@ -654,26 +658,26 @@
 	  else
 	    {
 	      /* Load ELF symbols.  */
-	      Elf32_Shdr *shdr = NULL;
+	      Elf_Shdr *shdr = NULL;
 	      int tab_size, sec_size;
 	      int symtab_err = 0;
 
-	      mbi.syms.e.num = pu.elf->e_shnum;
-	      mbi.syms.e.size = pu.elf->e_shentsize;
-	      mbi.syms.e.shndx = pu.elf->e_shstrndx;
+	      mbi.syms.e.num = E_SHNUM (pu.elf);
+	      mbi.syms.e.size = E_SHENTSIZE (pu.elf);
+	      mbi.syms.e.shndx = E_SHSTRNDX (pu.elf);
 	      
 	      /* We should align to a 4K boundary here for good measure.  */
 	      if (align_4k)
 		cur_addr = (cur_addr + 0xFFF) & 0xFFFFF000;
 	      
-	      tab_size = pu.elf->e_shentsize * pu.elf->e_shnum;
+	      tab_size = E_SHENTSIZE (pu.elf) * E_SHNUM (pu.elf);
 	      
-	      grub_seek (pu.elf->e_shoff);
+	      grub_seek (E_SHOFF (pu.elf));
 	      if (grub_read ((char *) RAW_ADDR (cur_addr), tab_size)
 		  == tab_size)
 		{
 		  mbi.syms.e.addr = cur_addr;
-		  shdr = (Elf32_Shdr *) mbi.syms.e.addr;
+		  shdr = (Elf_Shdr *) mbi.syms.e.addr;
 		  cur_addr += tab_size;
 		  
 		  printf (", shtab=0x%x", cur_addr);
@@ -682,20 +686,20 @@
 		    {
 		      /* This section is a loaded section,
 			 so we don't care.  */
-		      if (shdr[i].sh_addr != 0)
+		      if (SH_ADDR_AT (pu.elf, shdr, i) != 0)
 			continue;
 		      
 		      /* This section is empty, so we don't care.  */
-		      if (shdr[i].sh_size == 0)
+		      if (SH_SIZE_AT (pu.elf, shdr, i) == 0)
 			continue;
 		      
 		      /* Align the section to a sh_addralign bits boundary.  */
-		      cur_addr = ((cur_addr + shdr[i].sh_addralign) & 
-				  - (int) shdr[i].sh_addralign);
+		      cur_addr = ((cur_addr + SH_ADDRALIGN_AT (pu.elf, shdr, i)) & 
+				  - (int) SH_ADDRALIGN_AT (pu.elf, shdr, i));
 		      
-		      grub_seek (shdr[i].sh_offset);
+		      grub_seek (SH_OFFSET_AT (pu.elf, shdr, i));
 		      
-		      sec_size = shdr[i].sh_size;
+		      sec_size = SH_SIZE_AT (pu.elf, shdr, i);
 
 		      if (! (memcheck (cur_addr, sec_size)
 			     && (grub_read ((char *) RAW_ADDR (cur_addr),
@@ -706,7 +710,7 @@
 			  break;
 			}
 		      
-		      shdr[i].sh_addr = cur_addr;
+		      SET_SH_ADDR_AT (pu.elf, shdr, i, cur_addr);
 		      cur_addr += sec_size;
 		    }
 		}
diff -Naur grub-0.97.orig/stage2/builtins.c grub-0.97.patched/stage2/builtins.c
--- grub-0.97.orig/stage2/builtins.c	2005-02-15 14:58:23.000000000 -0700
+++ grub-0.97.patched/stage2/builtins.c	2006-09-25 14:59:16.000000000 -0600
@@ -81,6 +81,7 @@
 /* Prototypes for allowing straightfoward calling of builtins functions
    inside other functions.  */
 static int configfile_func (char *arg, int flags);
+static void multi_boot_64bit (int entry_addr, int mbi);
 
 /* Initialize the data for builtins.  */
 void
@@ -297,8 +298,11 @@
       break;
 
     case KERNEL_TYPE_MULTIBOOT:
-      /* Multiboot */
-      multi_boot ((int) entry_addr, (int) &mbi);
+
+      if (long_64bit_mode)
+         multi_boot_64bit((int) entry_addr, (int) &mbi);
+      else
+         multi_boot ((int) entry_addr, (int) &mbi);
       break;
 
     default:
@@ -309,6 +313,73 @@
   return 0;
 }
 
+#define TABLE_BITS  0x7	  /* user, r/w, present */
+#define PAGE_BITS   0x87  /* not pat, not global, not dirty, not accessed, user, r/w, present */
+
+#define NOT_PRESENT_TABLE 0x6
+#define NOT_PRESENT_PAGE  0x86
+
+
+static void
+multi_boot_64bit (int entry_addr, int mbi)
+{
+   unsigned *pml4        = PML4;
+   unsigned *pdpt0x000   = PDPT0x000;
+   unsigned *pd0x000_000 = PD0x000_000;
+   unsigned *pd0x000_001 = PD0x000_001;
+   unsigned *pd0x000_002 = PD0x000_002;
+   unsigned *pd0x000_003 = PD0x000_003;
+   int i;
+   unsigned long base;
+
+   /* identity map 1st 4 GB */
+
+   for (i = 0; i < 512; i++)
+   {
+      pml4[i * 2 + 0] = NOT_PRESENT_TABLE;
+      pml4[i * 2 + 1] = 0;
+      
+      pdpt0x000[i * 2 + 0] = NOT_PRESENT_TABLE;
+      pdpt0x000[i * 2 + 1] = 0;
+
+      pd0x000_000[i * 2 + 0] = NOT_PRESENT_PAGE;
+      pd0x000_000[i * 2 + 1] = 0;
+      pd0x000_001[i * 2 + 0] = NOT_PRESENT_PAGE;
+      pd0x000_001[i * 2 + 1] = 0;
+      pd0x000_002[i * 2 + 0] = NOT_PRESENT_PAGE;
+      pd0x000_002[i * 2 + 1] = 0;
+      pd0x000_003[i * 2 + 0] = NOT_PRESENT_PAGE;
+      pd0x000_003[i * 2 + 1] = 0;
+   }
+
+   pml4[0x000 * 2 + 0] = PDPT0x000 + TABLE_BITS;
+   pml4[0x000 * 2 + 1] = 0;
+
+   pdpt0x000[0x000 * 2 + 0] = PD0x000_000 + TABLE_BITS;
+   pdpt0x000[0x000 * 2 + 1] = 0;
+   pdpt0x000[0x001 * 2 + 0] = PD0x000_001 + TABLE_BITS;
+   pdpt0x000[0x001 * 2 + 1] = 0;
+   pdpt0x000[0x002 * 2 + 0] = PD0x000_002 + TABLE_BITS;
+   pdpt0x000[0x002 * 2 + 1] = 0;
+   pdpt0x000[0x003 * 2 + 0] = PD0x000_003 + TABLE_BITS;
+   pdpt0x000[0x003 * 2 + 1] = 0;
+
+   for (i = 0, base = 0; i < 1005; i++, base += 0x200000)
+   {
+      pd0x000_000[i * 2 + 0] = base + PAGE_BITS;
+      pd0x000_000[i * 2 + 1] = 0;
+      pd0x000_001[i * 2 + 0] = base + (1 * 0x200 * 0x20000) + PAGE_BITS;
+      pd0x000_001[i * 2 + 1] = 0;
+      pd0x000_002[i * 2 + 0] = base + (2 * 0x200 * 0x20000) + PAGE_BITS;
+      pd0x000_002[i * 2 + 1] = 0;
+      pd0x000_003[i * 2 + 0] = base + (3 * 0x200 * 0x20000) + PAGE_BITS;
+      pd0x000_003[i * 2 + 1] = 0;
+   }
+
+   multi_boot_64bit_doit (entry_addr, mbi);
+   /* NOTREACHED */
+}
+
 static struct builtin builtin_boot =
 {
   "boot",
diff -Naur grub-0.97.orig/stage2/elf.h grub-0.97.patched/stage2/elf.h
--- grub-0.97.orig/stage2/elf.h	1969-12-31 17:00:00.000000000 -0700
+++ grub-0.97.patched/stage2/elf.h	2006-08-25 21:58:37.000000000 -0600
@@ -0,0 +1,79 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2001,2002,2006  Free Software Foundation, Inc.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+
+#include "i386-elf.h"
+#include "x86-64-elf.h"
+
+
+typedef union
+{
+  unsigned char  e_ident[EI_NIDENT];
+  Elf32_Ehdr     elf32;
+  Elf64_Ehdr     elf64;
+}
+Elf_Ehdr;
+
+
+typedef union
+{
+  Elf32_Phdr elf32;
+  Elf64_Phdr elf64;
+}
+Elf_Phdr;
+
+
+typedef union
+{
+  Elf32_Shdr elf32;
+  Elf64_Shdr elf64;
+}
+Elf_Shdr;
+
+
+#define SIZEOF_ELF_EHDR(h) (h->e_ident[EI_CLASS] == ELFCLASS32 ? sizeof (Elf32_Ehdr) : sizeof (Elf64_Ehdr))
+
+#define E_ENTRY(h)     ((unsigned) (h->e_ident[EI_CLASS] == ELFCLASS32 ? h->elf32.e_entry : h->elf64.e_entry))
+#define E_PHOFF(h)     ((unsigned) (h->e_ident[EI_CLASS] == ELFCLASS32 ? h->elf32.e_phoff : h->elf64.e_phoff))
+#define E_PHNUM(h)     ((unsigned) (h->e_ident[EI_CLASS] == ELFCLASS32 ? h->elf32.e_phnum : h->elf64.e_phnum))
+#define E_PHENTSIZE(h) ((unsigned) (h->e_ident[EI_CLASS] == ELFCLASS32 ? h->elf32.e_phentsize : h->elf64.e_phentsize))
+#define E_SHNUM(h)     ((unsigned) (h->e_ident[EI_CLASS] == ELFCLASS32 ? h->elf32.e_shnum : h->elf64.e_shnum))
+#define E_SHENTSIZE(h) ((unsigned) (h->e_ident[EI_CLASS] == ELFCLASS32 ? h->elf32.e_shentsize : h->elf64.e_shentsize))
+#define E_SHSTRNDX(h)  ((unsigned) (h->e_ident[EI_CLASS] == ELFCLASS32 ? h->elf32.e_shstrndx : h->elf64.e_shstrndx))
+#define E_SHOFF(h)     ((unsigned) (h->e_ident[EI_CLASS] == ELFCLASS32 ? h->elf32.e_shoff : h->elf64.e_shoff))
+
+#define P_TYPE(h, p)   ((unsigned) (h->e_ident[EI_CLASS] == ELFCLASS32 ? p->elf32.p_type : p->elf64.p_type))
+#define P_OFFSET(h, p) ((unsigned) (h->e_ident[EI_CLASS] == ELFCLASS32 ? p->elf32.p_offset : p->elf64.p_offset))
+#define P_PADDR(h, p)  ((unsigned) (h->e_ident[EI_CLASS] == ELFCLASS32 ? p->elf32.p_paddr : p->elf64.p_paddr))
+#define P_MEMSZ(h, p)  ((unsigned) (h->e_ident[EI_CLASS] == ELFCLASS32 ? p->elf32.p_memsz : p->elf64.p_memsz))
+#define P_VADDR(h, p)  ((unsigned) (h->e_ident[EI_CLASS] == ELFCLASS32 ? p->elf32.p_vaddr : p->elf64.p_vaddr))
+#define P_FILESZ(h, p) ((unsigned) (h->e_ident[EI_CLASS] == ELFCLASS32 ? p->elf32.p_filesz : p->elf64.p_filesz))
+
+#define SH_ADDR_AT(h, sh, i)      ((unsigned) (h->e_ident[EI_CLASS] == ELFCLASS32 ? ((&(sh->elf32))[i]).sh_addr : ((&(sh->elf64))[i]).sh_addr))
+#define SH_SIZE_AT(h, sh, i)      ((unsigned) (h->e_ident[EI_CLASS] == ELFCLASS32 ? ((&(sh->elf32))[i]).sh_size : ((&(sh->elf64))[i]).sh_size))
+#define SH_ADDRALIGN_AT(h, sh, i) ((unsigned) (h->e_ident[EI_CLASS] == ELFCLASS32 ? ((&(sh->elf32))[i]).sh_addralign : ((&(sh->elf64))[i]).sh_addralign))
+#define SH_OFFSET_AT(h, sh, i)    ((unsigned) (h->e_ident[EI_CLASS] == ELFCLASS32 ? ((&(sh->elf32))[i]).sh_offset : ((&(sh->elf64))[i]).sh_offset))
+
+#define SET_SH_ADDR_AT(h, sh, i, v)         \
+  {                                         \
+    if (h->e_ident[EI_CLASS] == ELFCLASS32) \
+      ((&(sh->elf32))[i]).sh_addr = v;      \
+    else                                    \
+      ((&(sh->elf64))[i]).sh_addr = v;      \
+  }
diff -Naur grub-0.97.orig/stage2/shared.h grub-0.97.patched/stage2/shared.h
--- grub-0.97.orig/stage2/shared.h	2004-06-19 10:40:09.000000000 -0600
+++ grub-0.97.patched/stage2/shared.h	2006-09-25 14:54:53.000000000 -0600
@@ -70,6 +70,13 @@
 
 #define BOOT_PART_TABLE	RAW_ADDR (0x07be)
 
+#define PML4		0x70000
+#define PDPT0x000	0x71000
+#define PD0x000_000	0x72000
+#define PD0x000_001	0x73000
+#define PD0x000_002	0x74000
+#define PD0x000_003	0x75000
+
 /*
  *  BIOS disk defines
  */
@@ -692,6 +699,8 @@
 
 extern entry_func entry_addr;
 
+extern unsigned long_64bit_mode;
+
 /* Enter the stage1.5/stage2 C code after the stack is set up. */
 void cmain (void);
 
@@ -739,6 +748,9 @@
 /* booting a multiboot executable */
 void multi_boot (int start, int mb_info) __attribute__ ((noreturn));
 
+/* boot into 64-bit long mode */
+void multi_boot_64bit_doit (int start, int mb_info) __attribute__ ((noreturn));
+
 /* If LINEAR is nonzero, then set the Intel processor to linear mode.
    Otherwise, bit 20 of all memory accesses is always forced to zero,
    causing a wraparound effect for bugwards compatibility with the
diff -Naur grub-0.97.orig/stage2/x86-64-elf.h grub-0.97.patched/stage2/x86-64-elf.h
--- grub-0.97.orig/stage2/x86-64-elf.h	1969-12-31 17:00:00.000000000 -0700
+++ grub-0.97.patched/stage2/x86-64-elf.h	2006-08-26 21:45:22.000000000 -0600
@@ -0,0 +1,86 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2001,2002,2006  Free Software Foundation, Inc.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+
+typedef unsigned short		Elf64_Half;
+typedef unsigned long		Elf64_Word;
+typedef unsigned long long	Elf64_Off;
+typedef unsigned long long	Elf64_Addr;
+typedef unsigned long long      Elf64_Xword;
+
+
+typedef struct
+{
+  unsigned char e_ident[EI_NIDENT];	/* basic identification block */
+  Elf64_Half 	e_type;			/* file types */
+  Elf64_Half 	e_machine;		/* machine types */
+  Elf64_Word 	e_version;		/* use same as "EI_VERSION" above */
+  Elf64_Addr 	e_entry;		/* entry point of the program */
+  Elf64_Off 	e_phoff;		/* program header table file offset */
+  Elf64_Off 	e_shoff;		/* section header table file offset */
+  Elf64_Word 	e_flags;		/* flags */
+  Elf64_Half 	e_ehsize;		/* elf header size in bytes */
+  Elf64_Half 	e_phentsize;		/* program header entry size */
+  Elf64_Half 	e_phnum;		/* number of entries in program header */
+  Elf64_Half 	e_shentsize;		/* section header entry size */
+  Elf64_Half 	e_shnum;		/* number of entries in section header */
+  Elf64_Half 	e_shstrndx;		/* section header table index */
+}
+Elf64_Ehdr;
+
+
+typedef struct
+{
+  Elf64_Word 	p_type;
+  Elf64_Word	p_flags;
+  Elf64_Off 	p_offset;
+  Elf64_Addr 	p_vaddr;
+  Elf64_Addr 	p_paddr;
+  Elf64_Xword 	p_filesz;
+  Elf64_Xword 	p_memsz;
+  Elf64_Xword 	p_align;
+}
+Elf64_Phdr;
+
+
+typedef struct
+{
+  Elf64_Word	sh_name;		/* Section name (string tbl index) */
+  Elf64_Word	sh_type;		/* Section type */
+  Elf64_Xword	sh_flags;		/* Section flags */
+  Elf64_Addr	sh_addr;		/* Section virtual addr at execution */
+  Elf64_Off	sh_offset;		/* Section file offset */
+  Elf64_Xword	sh_size;		/* Section size in bytes */
+  Elf64_Word	sh_link;		/* Link to another section */
+  Elf64_Word	sh_info;		/* Additional section information */
+  Elf64_Xword	sh_addralign;		/* Section alignment */
+  Elf64_Xword	sh_entsize;		/* Entry size if section holds table */
+}
+Elf64_Shdr;
+
+#define ELFCLASS64 2
+
+#define EM_X86_64  0x3e
+
+#define BOOTABLE_X86_64_ELF(h) \
+ ((h.e_ident[EI_MAG0] == ELFMAG0) & (h.e_ident[EI_MAG1] == ELFMAG1) \
+  & (h.e_ident[EI_MAG2] == ELFMAG2) & (h.e_ident[EI_MAG3] == ELFMAG3) \
+  & (h.e_ident[EI_CLASS] == ELFCLASS64) & (h.e_ident[EI_DATA] == ELFDATA2LSB) \
+  & (h.e_ident[EI_VERSION] == EV_CURRENT) & (h.e_type == ET_EXEC) \
+  & (h.e_machine == EM_X86_64) & (h.e_version == EV_CURRENT))