Skip to contentSkip to conversation

trampoline for linux on 64-bit systems

PATCH In review Vladimir 'phcoder' Serbinenko 2 participants 3 messages May 9, 2009 – May 13, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 trampoline for linux on 64-bit systems

Hello. On 64-bit systems the code may be loaded above 4GiB. When grub tries to switch to 32-bit mode before launching linux it causes the reboot. Here is a patch to make this part of code into a trampoline

Signature · 2 lines
Regards
Vladimir 'phcoder' Serbinenko
ChangeLog +17 -0
index c307abe..6bc043a 100644
@@ -1,3 +1,20 @@
+2009-05-09  Vladimir Serbinenko  <[email protected]>
+
+	trampoline for linux on 64-bit platform
+
+	* conf/x86_64-efi.rmk (linux_mod_SOURCES): added 
+	loader/i386/efi/linux_trampoline.S 
+	* include/grub/x86_64/efi/loader.h (grub_linux_real_boot): removed 
+	declration
+	* kern/x86_64/efi/startup.S (grub_linux_real_boot): moved from here
+	* loader/i386/efi/linux_trampoline.S: moved here
+	* loader/i386/efi/linux.c (allocate_pages): reserve space for trampoline
+	(jumpvector): removed
+	(grub_linux_trampoline_start): new declaration
+	(grub_linux_trampoline_end): likewise
+	(grub_linux_boot): use trampoline when on 64-bit platform
+	* loader/i386/linux.c: likewise
+
 2009-05-08  Robert Millan  <[email protected]>
 
 	* util/i386/pc/grub-setup.c (setup): Factorize find_usable_region(),
conf/x86_64-efi.rmk +1 -1
index 92ad9f7..45e8527 100644
@@ -137,7 +137,7 @@ appleldr_mod_CFLAGS = $(COMMON_CFLAGS)
 appleldr_mod_LDFLAGS = $(COMMON_LDFLAGS)
 
 # For linux.mod.
-linux_mod_SOURCES = loader/i386/efi/linux.c
+linux_mod_SOURCES = loader/i386/efi/linux.c loader/i386/efi/linux_trampoline.S
 linux_mod_CFLAGS = $(COMMON_CFLAGS)
 linux_mod_LDFLAGS = $(COMMON_LDFLAGS)
 
include/grub/x86_64/efi/loader.h +0 -1
index fac6746..7c302e8 100644
@@ -22,6 +22,5 @@
 #include <grub/types.h>
 #include <grub/symbol.h>
 
-void EXPORT_FUNC(grub_linux_real_boot) (void);
 
 #endif /* ! GRUB_LOADER_MACHINE_HEADER */
kern/x86_64/efi/startup.S +0 -23
index 2cb1fd4..fb4fc7b 100644
@@ -61,26 +61,3 @@ codestart:
 	call	EXT_C(grub_main)
 	ret
 
-	.code32
-
-FUNCTION(grub_linux_real_boot)
-        /* Turn off PG bit in CR0 and set CR3 to zero.  */
-        movl    %cr0, %eax
-        andl    $0x7FFFFFFF, %eax
-        movl    %eax, %cr0
-
-        /* Setup EFER (Extended Feature Enable Register).  */
-        movl    $0xc0000080, %ecx
-        rdmsr
-
-        /* Disable Long Mode.  */
-        andl    $0xFFFFFEFF, %eax
-
-        /* Make changes effective.  */
-        wrmsr
-
-        /* Disable PAE.  */
-        xorl    %eax, %eax
-        movl    %eax, %cr4
-
-        jmp     *%ebx
loader/i386/efi/linux.c +5 -18
index 9be88aa..09b8d82 100644
@@ -241,7 +241,7 @@ allocate_pages (grub_size_t prot_size)
 	      
   /* Next, find free pages for the protected mode code.  */
   /* XXX what happens if anything is using this address?  */
-  prot_mode_mem = grub_efi_allocate_pages (0x100000, prot_mode_pages);
+  prot_mode_mem = grub_efi_allocate_pages (0x100000, prot_mode_pages + 1);
   if (! prot_mode_mem)
     {
       grub_error (GRUB_ERR_OUT_OF_MEMORY,
@@ -286,11 +286,8 @@ grub_e820_add_region (struct grub_e820_mmap *e820_map, int *e820_num,
 }
 
 #ifdef __x86_64__
-struct
-{
-  grub_uint32_t kernel_entry;
-  grub_uint32_t kernel_cs;
-} jumpvector;
+extern grub_uint8_t grub_linux_trampoline_start[];
+extern grub_uint8_t grub_linux_trampoline_end[];
 #endif
 
 static grub_err_t
@@ -384,6 +381,8 @@ grub_linux_boot (void)
       params->v0204.efi_mmap_size = mmap_size;
     }
 
+#else
+
   /* Hardware interrupts are not safe any longer.  */
   asm volatile ("cli" : : );
   
@@ -391,18 +390,6 @@ grub_linux_boot (void)
   asm volatile ("lidt %0" : : "m" (idt_desc));
   asm volatile ("lgdt %0" : : "m" (gdt_desc));
 
-#ifdef __x86_64__
-
-  jumpvector.kernel_entry = (grub_uint64_t) grub_linux_real_boot;
-  jumpvector.kernel_cs = 0x10;
-
-  asm volatile ( "mov %0, %%rbx" : : "m" (params->code32_start));
-  asm volatile ( "mov %0, %%rsi" : : "m" (real_mode_mem));
-
-  asm volatile ( "ljmp *%0" : : "m" (jumpvector));
-
-#else
-
   /* Pass parameters.  */
   asm volatile ("movl %0, %%ecx" : : "m" (params->code32_start));
   asm volatile ("movl %0, %%esi" : : "m" (real_mode_mem));
loader/i386/efi/linux_trampoline.S new file +112 -0

112 lines not on this page · raw message

loader/i386/linux.c +12 -17
index d67a5f8..07fdb99 100644
@@ -432,11 +432,8 @@ grub_linux_setup_video (struct linux_kernel_params *params)
 }
 
 #ifdef __x86_64__
-struct
-{
-  grub_uint32_t kernel_entry;
-  grub_uint32_t kernel_cs;
-} jumpvector;
+extern grub_uint8_t grub_linux_trampoline_start[];
+extern grub_uint8_t grub_linux_trampoline_end[];
 #endif
 
 static grub_err_t
@@ -548,6 +545,16 @@ grub_linux_boot (void)
   grub_mmap_iterate (hook);
   params->mmap_size = e820_num;
 
+#ifdef __x86_64__
+
+  grub_memcpy ((char *) prot_mode_mem + (prot_mode_pages << 12), 
+	       grub_linux_trampoline_start, 
+	       grub_linux_trampoline_end - grub_linux_trampoline_start);
+  
+  ((void (*) (void *, unsigned long)) ((char *) prot_mode_mem 
+				       + (prot_mode_pages << 12)))
+    (params->code32_start, real_mode_mem);
+#else
 
   /* Hardware interrupts are not safe any longer.  */
   asm volatile ("cli" : : );
@@ -556,18 +563,6 @@ grub_linux_boot (void)
   asm volatile ("lidt %0" : : "m" (idt_desc));
   asm volatile ("lgdt %0" : : "m" (gdt_desc));
 
-#ifdef __x86_64__
-
-  jumpvector.kernel_entry = (grub_uint64_t) grub_linux_real_boot;
-  jumpvector.kernel_cs = 0x10;
-
-  asm volatile ( "mov %0, %%rbx" : : "m" (params->code32_start));
-  asm volatile ( "mov %0, %%rsi" : : "m" (real_mode_mem));
-
-  asm volatile ( "ljmp *%0" : : "m" (jumpvector));
-
-#else
-
   /* Pass parameters.  */
   asm volatile ("movl %0, %%ecx" : : "m" (params->code32_start));
   asm volatile ("movl %0, %%esi" : : "m" (real_mode_mem));
#1 lore raw
Quoted 1 line · Vladimir 'phcoder' Serbinenko

+++ b/loader/i386/efi/linux_trampoline.S

Since this file is also used on loader/i386/linux.c, shouldn't it be moved above from efi/ ?

Signature · 5 lines
Robert Millan

  The DRM opt-in fallacy: "Your data belongs to us. We will decide when (and
  how) you may access your data; but nobody's threatening your freedom: we
  still allow you to remove your data and not access it at all."
#2 lore raw
In reply to Vladimir 'phcoder' Serbinenko
Quoted 2 lines · Vladimir 'phcoder' Serbinenko

Hello. On 64-bit systems the code may be loaded above 4GiB. When grub tries to switch to 32-bit mode before launching linux it causes the reboot.

Btw, I assume the problem happens with more than 4 GiB of physical memory?

Does it affect the legacy loader as well?

Signature · 5 lines
Robert Millan

  The DRM opt-in fallacy: "Your data belongs to us. We will decide when (and
  how) you may access your data; but nobody's threatening your freedom: we
  still allow you to remove your data and not access it at all."