diff --git a/conf/i386-efi.rmk b/conf/i386-efi.rmk index 80c2978da..f90ee5dc5 100644 --- a/conf/i386-efi.rmk +++ b/conf/i386-efi.rmk @@ -155,7 +155,7 @@ efi_fb_mod_LDFLAGS = $(COMMON_LDFLAGS) pkglib_MODULES += xnu.mod xnu_mod_SOURCES = loader/xnu_resume.c loader/i386/xnu.c loader/i386/efi/xnu.c\ - loader/macho.c loader/xnu.c loader/i386/xnu_helper.S + loader/macho.c loader/xnu.c xnu_mod_CFLAGS = $(COMMON_CFLAGS) xnu_mod_LDFLAGS = $(COMMON_LDFLAGS) xnu_mod_ASFLAGS = $(COMMON_ASFLAGS) diff --git a/conf/i386-pc.rmk b/conf/i386-pc.rmk index 2b0894eae..672204725 100644 --- a/conf/i386-pc.rmk +++ b/conf/i386-pc.rmk @@ -188,7 +188,7 @@ linux_mod_LDFLAGS = $(COMMON_LDFLAGS) pkglib_MODULES += xnu.mod xnu_mod_SOURCES = loader/xnu_resume.c loader/i386/xnu.c loader/i386/pc/xnu.c\ - loader/macho.c loader/xnu.c loader/i386/xnu_helper.S + loader/macho.c loader/xnu.c xnu_mod_CFLAGS = $(COMMON_CFLAGS) xnu_mod_LDFLAGS = $(COMMON_LDFLAGS) xnu_mod_ASFLAGS = $(COMMON_ASFLAGS) diff --git a/conf/x86_64-efi.rmk b/conf/x86_64-efi.rmk index 3b7ac34fa..726bf23fa 100644 --- a/conf/x86_64-efi.rmk +++ b/conf/x86_64-efi.rmk @@ -161,7 +161,7 @@ efi_fb_mod_LDFLAGS = $(COMMON_LDFLAGS) pkglib_MODULES += xnu.mod xnu_mod_SOURCES = loader/xnu_resume.c loader/i386/xnu.c loader/i386/efi/xnu.c\ - loader/macho.c loader/xnu.c loader/i386/xnu_helper.S + loader/macho.c loader/xnu.c xnu_mod_CFLAGS = $(COMMON_CFLAGS) xnu_mod_LDFLAGS = $(COMMON_LDFLAGS) xnu_mod_ASFLAGS = $(COMMON_ASFLAGS) diff --git a/include/grub/i386/xnu.h b/include/grub/i386/xnu.h index 68674e605..306dc7b65 100644 --- a/include/grub/i386/xnu.h +++ b/include/grub/i386/xnu.h @@ -20,6 +20,9 @@ #define GRUB_CPU_XNU_H 1 #include +#include + +#define XNU_RELOCATOR(x) (grub_relocator32_ ## x) #define GRUB_XNU_PAGESIZE 4096 typedef grub_uint32_t grub_xnu_ptr_t; @@ -75,6 +78,4 @@ grub_err_t grub_xnu_boot (void); grub_err_t grub_cpu_xnu_fill_devicetree (void); grub_err_t grub_xnu_set_video (struct grub_xnu_boot_params *bootparams_relloc); extern grub_uint32_t grub_xnu_heap_will_be_at; -extern grub_uint8_t grub_xnu_launcher_start[]; -extern grub_uint8_t grub_xnu_launcher_end[]; #endif diff --git a/include/grub/xnu.h b/include/grub/xnu.h index c3902e670..cf778cf6b 100644 --- a/include/grub/xnu.h +++ b/include/grub/xnu.h @@ -92,6 +92,7 @@ struct grub_xnu_devtree_key *grub_xnu_create_value (struct grub_xnu_devtree_key void grub_xnu_lock (void); void grub_xnu_unlock (void); grub_err_t grub_xnu_resume (char *imagename); +grub_err_t grub_xnu_boot_resume (void); struct grub_xnu_devtree_key *grub_xnu_find_key (struct grub_xnu_devtree_key *parent, char *name); grub_err_t grub_xnu_align_heap (int align); @@ -100,8 +101,7 @@ grub_err_t grub_xnu_scan_dir_for_kexts (char *dirname, char *osbundlerequired, grub_err_t grub_xnu_load_kext_from_dir (char *dirname, char *osbundlerequired, int maxrecursion); void *grub_xnu_heap_malloc (int size); -extern grub_uint32_t grub_xnu_heap_real_start; extern grub_size_t grub_xnu_heap_size; -extern char *grub_xnu_heap_start; +extern void *grub_xnu_heap_start; extern struct grub_video_bitmap *grub_xnu_bitmap; #endif diff --git a/loader/i386/xnu.c b/loader/i386/xnu.c index 275b50dbc..6a3535950 100644 --- a/loader/i386/xnu.c +++ b/loader/i386/xnu.c @@ -30,6 +30,8 @@ #include char grub_xnu_cmdline[1024]; +grub_uint32_t grub_xnu_heap_will_be_at; +grub_uint32_t grub_xnu_entry_point, grub_xnu_arg1, grub_xnu_stack; /* Aliases set for some tables. */ struct tbl_alias @@ -44,21 +46,6 @@ struct tbl_alias table_aliases[] = {GRUB_EFI_ACPI_TABLE_GUID, "ACPI"}, }; -/* The following function is used to be able to debug xnu loader - with grub-emu. */ -#ifdef GRUB_UTIL -static grub_err_t -grub_xnu_launch (void) -{ - grub_printf ("Fake launch %x:%p:%p", grub_xnu_entry_point, grub_xnu_arg1, - grub_xnu_stack); - grub_getkey (); - return 0; -} -#else -static void (*grub_xnu_launch) (void) = 0; -#endif - static int utf16_strlen (grub_uint16_t *in) { @@ -417,6 +404,19 @@ grub_cpu_xnu_fill_devicetree (void) return GRUB_ERR_NONE; } +grub_err_t +grub_xnu_boot_resume (void) +{ + struct grub_relocator32_state state; + + state.esp = grub_xnu_stack; + state.eip = grub_xnu_entry_point; + state.eax = grub_xnu_arg1; + + return grub_relocator32_boot (grub_xnu_heap_start, grub_xnu_heap_will_be_at, + state); +} + /* Boot xnu. */ grub_err_t grub_xnu_boot (void) @@ -434,6 +434,7 @@ grub_xnu_boot (void) void *devtree; grub_size_t devtreelen; int i; + struct grub_relocator32_state state; /* Page-align to avoid following parts to be inadvertently freed. */ err = grub_xnu_align_heap (GRUB_XNU_PAGESIZE); @@ -501,7 +502,8 @@ grub_xnu_boot (void) grub_memcpy (bootparams_relloc->cmdline, grub_xnu_cmdline, sizeof (bootparams_relloc->cmdline)); - bootparams_relloc->devtree = ((char *) devtree - grub_xnu_heap_start) + bootparams_relloc->devtree + = ((grub_uint8_t *) devtree - (grub_uint8_t *) grub_xnu_heap_start) + grub_xnu_heap_will_be_at; bootparams_relloc->devtreelen = devtreelen; @@ -529,12 +531,7 @@ grub_xnu_boot (void) grub_xnu_stack = bootparams_relloc->heap_start + bootparams_relloc->heap_size + GRUB_XNU_PAGESIZE; grub_xnu_arg1 = bootparams_relloc_off + grub_xnu_heap_will_be_at; -#ifndef GRUB_UTIL - grub_xnu_launch = (void (*) (void)) - (grub_xnu_heap_start + grub_xnu_heap_size); -#endif grub_dprintf ("xnu", "eip=%x\n", grub_xnu_entry_point); - grub_dprintf ("xnu", "launch=%p\n", grub_xnu_launch); const char *debug = grub_env_get ("debug"); @@ -560,16 +557,12 @@ grub_xnu_boot (void) bootparams_relloc->lfb_base = 0; } - grub_memcpy (grub_xnu_heap_start + grub_xnu_heap_size, - grub_xnu_launcher_start, - grub_xnu_launcher_end - grub_xnu_launcher_start); - - if (! grub_autoefi_finish_boot_services ()) return grub_error (GRUB_ERR_IO, "can't exit boot services"); - grub_xnu_launch (); - - /* Never reaches here. */ - return 0; + state.eip = grub_xnu_entry_point; + state.eax = grub_xnu_arg1; + state.esp = grub_xnu_stack; + return grub_relocator32_boot (grub_xnu_heap_start, grub_xnu_heap_will_be_at, + state); } diff --git a/loader/i386/xnu_helper.S b/loader/i386/xnu_helper.S deleted file mode 100644 index 4250c58ad..000000000 --- a/loader/i386/xnu_helper.S +++ /dev/null @@ -1,211 +0,0 @@ -/* - * GRUB -- GRand Unified Bootloader - * Copyright (C) 2009 Free Software Foundation, Inc. - * - * GRUB 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 3 of the License, or - * (at your option) any later version. - * - * GRUB 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 GRUB. If not, see . - */ - -#include - - - .p2align 4 /* force 16-byte alignment */ - -VARIABLE(grub_xnu_launcher_start) -base: - cli - -#ifndef __x86_64__ - /* mov imm32, %eax */ - .byte 0xb8 -VARIABLE(grub_xnu_heap_will_be_at) - .long 0 - mov %eax, %edi - - /* mov imm32, %eax */ - .byte 0xb8 -VARIABLE(grub_xnu_heap_start) - .long 0 - mov %eax, %esi - - /* mov imm32, %ecx */ - .byte 0xb9 -VARIABLE(grub_xnu_heap_size) - .long 0 - mov %edi, %eax - add %ecx, %eax - /* %rax now contains our starting position after relocation. */ - /* One more page to copy: ourselves. */ - add $0x403, %ecx - shr $2, %ecx - - /* Forward copy. */ - cld - rep - movsl - - mov %eax, %esi - add $(cont0-base), %eax - jmp *%eax -cont0: -#else - xorq %rax, %rax - - /* mov imm32, %eax */ - .byte 0xb8 -VARIABLE(grub_xnu_heap_will_be_at) - .long 0 - mov %rax, %rdi - - /* mov imm32, %rax */ - .byte 0x48 - .byte 0xb8 -VARIABLE(grub_xnu_heap_start) - .long 0 - .long 0 - mov %rax, %rsi - - /* mov imm32, %rcx */ - .byte 0x48 - .byte 0xb9 -VARIABLE(grub_xnu_heap_size) - .long 0 - .long 0 - mov %rdi, %rax - add %rcx, %rax - /* %rax now contains our starting position after relocation. */ - /* One more page to copy: ourselves. */ - add $0x403, %rcx - shr $2, %rcx - - /* Forward copy. */ - cld - rep - movsl - - mov %rax, %rsi -#ifdef APPLE_CC - add $(cont0-base), %eax -#else - add $(cont0-base), %rax -#endif - jmp *%rax - -cont0: -#ifdef APPLE_CC - lea (cont1 - base) (%esi, 1), %eax - mov %eax, (jump_vector - base) (%esi, 1) - - lea (gdt - base) (%esi, 1), %eax - mov %eax, (gdt_addr - base) (%esi, 1) - - /* Switch to compatibility mode. */ - - lgdt (gdtdesc - base) (%esi, 1) - - /* Update %cs. Thanks to David Miller for pointing this mistake out. */ - ljmp *(jump_vector - base) (%esi,1) -#else - lea (cont1 - base) (%rsi, 1), %rax - mov %eax, (jump_vector - base) (%rsi, 1) - - lea (gdt - base) (%rsi, 1), %rax - mov %rax, (gdt_addr - base) (%rsi, 1) - - /* Switch to compatibility mode. */ - - lgdt (gdtdesc - base) (%rsi, 1) - - /* Update %cs. Thanks to David Miller for pointing this mistake out. */ - ljmp *(jump_vector - base) (%rsi, 1) -#endif - -cont1: - .code32 - - /* Update other registers. */ - mov $0x18, %eax - mov %eax, %ds - mov %eax, %es - mov %eax, %fs - mov %eax, %gs - mov %eax, %ss - - /* Disable paging. */ - mov %cr0, %eax - and $0x7fffffff, %eax - mov %eax, %cr0 - - /* Disable amd64. */ - mov $0xc0000080, %ecx - rdmsr - and $0xfffffeff, %eax - wrmsr - - /* Turn off PAE. */ - movl %cr4, %eax - and $0xffffffcf, %eax - mov %eax, %cr4 - - jmp cont2 -cont2: -#endif - .code32 - - /* Registers on XNU boot: eip, esp and eax. */ - /* mov imm32, %ecx */ - .byte 0xb9 -VARIABLE (grub_xnu_entry_point) - .long 0 - /* mov imm32, %eax */ - .byte 0xb8 -VARIABLE (grub_xnu_arg1) - .long 0 - /* mov imm32, %ebx */ - .byte 0xbb -VARIABLE (grub_xnu_stack) - .long 0 - - movl %ebx, %esp - - jmp *%ecx - -#ifdef __x86_64__ - /* GDT. Copied from loader/i386/linux.c. */ - .p2align 4 -gdt: - /* NULL. */ - .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 - - /* Reserved. */ - .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 - - /* Code segment. */ - .byte 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x9A, 0xCF, 0x00 - - /* Data segment. */ - .byte 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x92, 0xCF, 0x00 - -gdtdesc: - .word 31 -gdt_addr: - /* Filled by the code. */ - .quad 0 - - .p2align 4 -jump_vector: - /* Jump location. Is filled by the code */ - .long 0 - .long 0x10 -#endif -VARIABLE(grub_xnu_launcher_end) diff --git a/loader/xnu.c b/loader/xnu.c index aac4ae372..cc6d55ae2 100644 --- a/loader/xnu.c +++ b/loader/xnu.c @@ -36,6 +36,9 @@ struct grub_xnu_devtree_key *grub_xnu_devtree_root = 0; static int driverspackagenum = 0; static int driversnum = 0; +void *grub_xnu_heap_start = 0; +grub_size_t grub_xnu_heap_size = 0; + /* Allocate heap by 32MB-blocks. */ #define GRUB_XNU_HEAP_ALLOC_BLOCK 0x2000000 @@ -46,12 +49,6 @@ void * grub_xnu_heap_malloc (int size) { void *val; - -#if 0 - /* This way booting is faster but less reliable. - Once we have advanced mm second way will be as fast as this one. */ - val = grub_xnu_heap_start = (char *) 0x100000; -#else int oldblknum, newblknum; /* The page after the heap is used for stack. Ensure it's usable. */ @@ -63,25 +60,21 @@ grub_xnu_heap_malloc (int size) newblknum = (grub_xnu_heap_size + size + GRUB_XNU_PAGESIZE + GRUB_XNU_HEAP_ALLOC_BLOCK - 1) / GRUB_XNU_HEAP_ALLOC_BLOCK; if (oldblknum != newblknum) - /* FIXME: instruct realloc to allocate at 1MB if possible once - advanced mm is ready. */ - val = grub_realloc (grub_xnu_heap_start, - newblknum * GRUB_XNU_HEAP_ALLOC_BLOCK); - else - val = grub_xnu_heap_start; - if (! val) { - grub_error (GRUB_ERR_OUT_OF_MEMORY, - "not enough space on xnu memory heap"); - return 0; + /* FIXME: instruct realloc to allocate at 1MB if possible once + advanced mm is ready. */ + grub_xnu_heap_start + = XNU_RELOCATOR (realloc) (grub_xnu_heap_start, + newblknum + * GRUB_XNU_HEAP_ALLOC_BLOCK); + if (!grub_xnu_heap_start) + return NULL; } - grub_xnu_heap_start = val; -#endif - val = (char *) grub_xnu_heap_start + grub_xnu_heap_size; + val = (grub_uint8_t *) grub_xnu_heap_start + grub_xnu_heap_size; grub_xnu_heap_size += size; grub_dprintf ("xnu", "val=%p\n", val); - return (char *) val; + return val; } /* Make sure next block of the heap will be aligned. @@ -251,7 +244,7 @@ grub_xnu_writetree_toheap (void **start, grub_size_t *size) - *size % GRUB_XNU_PAGESIZE); /* Put real data in the dummy. */ - extdesc->addr = (char *) *start - grub_xnu_heap_start + extdesc->addr = (grub_uint8_t *) *start - (grub_uint8_t *) grub_xnu_heap_start + grub_xnu_heap_will_be_at; extdesc->size = (grub_uint32_t) *size; @@ -503,7 +496,7 @@ grub_xnu_load_driver (char *infoplistname, grub_file_t binaryfile) grub_file_t infoplist; struct grub_xnu_extheader *exthead; int neededspace = sizeof (*exthead); - char *buf; + grub_uint8_t *buf; grub_size_t infoplistsize = 0, machosize = 0; if (! grub_xnu_heap_size) @@ -552,7 +545,7 @@ grub_xnu_load_driver (char *infoplistname, grub_file_t binaryfile) /* Load the binary. */ if (macho) { - exthead->binaryaddr = (buf - grub_xnu_heap_start) + exthead->binaryaddr = (buf - (grub_uint8_t *) grub_xnu_heap_start) + grub_xnu_heap_will_be_at; exthead->binarysize = machosize; if ((err = grub_macho32_readfile (macho, buf))) @@ -568,7 +561,7 @@ grub_xnu_load_driver (char *infoplistname, grub_file_t binaryfile) /* Load the plist. */ if (infoplist) { - exthead->infoplistaddr = (buf - grub_xnu_heap_start) + exthead->infoplistaddr = (buf - (grub_uint8_t *) grub_xnu_heap_start) + grub_xnu_heap_will_be_at; exthead->infoplistsize = infoplistsize + 1; if (grub_file_read (infoplist, buf, infoplistsize) diff --git a/loader/xnu_resume.c b/loader/xnu_resume.c index 77f688726..bfc1eb1e8 100644 --- a/loader/xnu_resume.c +++ b/loader/xnu_resume.c @@ -45,7 +45,7 @@ grub_xnu_resume (char *imagename) grub_file_t file; grub_size_t total_header_size; struct grub_xnu_hibernate_header hibhead; - char *buf, *codetmp; + grub_uint8_t *buf; grub_uint32_t codedest; grub_uint32_t codesize; @@ -94,12 +94,11 @@ grub_xnu_resume (char *imagename) /* Try to allocate necessary space. FIXME: mm isn't good enough yet to handle huge allocations. */ - grub_xnu_hibernate_image = buf = grub_malloc (hibhead.image_size); + grub_xnu_hibernate_image = buf = XNU_RELOCATOR (alloc) (hibhead.image_size); if (! buf) { grub_file_close (file); - return grub_error (GRUB_ERR_OUT_OF_MEMORY, - "not enough memory to load image"); + return grub_errno; } /* Read image. */ @@ -112,22 +111,16 @@ grub_xnu_resume (char *imagename) } grub_file_close (file); - codetmp = grub_memalign (GRUB_XNU_PAGESIZE, codesize + GRUB_XNU_PAGESIZE); /* Setup variables needed by asm helper. */ grub_xnu_heap_will_be_at = codedest; - grub_xnu_heap_start = codetmp; + grub_xnu_heap_start = buf; grub_xnu_heap_size = codesize; grub_xnu_stack = (codedest + hibhead.stack); grub_xnu_entry_point = (codedest + hibhead.entry_point); grub_xnu_arg1 = (long) buf; - /* Prepare asm helper. */ - grub_memcpy (codetmp, ((grub_uint8_t *) buf) + total_header_size, codesize); - grub_memcpy (codetmp + codesize, grub_xnu_launcher_start, - grub_xnu_launcher_end - grub_xnu_launcher_start); - /* We're ready now. */ - grub_loader_set ((grub_err_t (*) (void)) (codetmp + codesize), + grub_loader_set (grub_xnu_boot_resume, grub_xnu_resume_unload, 0); /* Prevent module from unloading. */