/* * There is an ELF kernel and one or more ELF modules loaded. * We wish to start executing the kernel image, so make such * preparations as are required, and do so. */ static int elf64_exec(struct preloaded_file *fp) { struct file_metadata *md; Elf_Ehdr *ehdr; vm_offset_t modulep, kernend; int err; int i; uint32_t stack[1024]; p4_entry_t PT4[512]; p3_entry_t PT3[512]; p2_entry_t PT2[512]; uint64_t gdtr[3]; if ((md = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL) return(EFTYPE); ehdr = (Elf_Ehdr *)&(md->md_data); err = bi_load64(fp->f_args, &modulep, &kernend); if (err != 0) return(err); bzero(PT4, PAGE_SIZE); bzero(PT3, PAGE_SIZE); bzero(PT2, PAGE_SIZE); /* * Build a scratch stack at physical 0x1000, page tables: * PT4 at 0x2000, * PT3 at 0x3000, * PT2 at 0x4000, * gdtr at 0x5000, */ /* * This is kinda brutal, but every single 1GB VM memory segment * points to the same first 1GB of physical memory. But it is * more than adequate. */ for (i = 0; i < 512; i++) { /* Each slot of the level 4 pages points to the same level 3 page */ PT4[i] = (p4_entry_t) 0x3000; PT4[i] |= PG_V | PG_RW | PG_U; /* Each slot of the level 3 pages points to the same level 2 page */ PT3[i] = (p3_entry_t) 0x4000; PT3[i] |= PG_V | PG_RW | PG_U; /* The level 2 page slots are mapped with 2MB pages for 1GB. */ PT2[i] = i * (2 * 1024 * 1024); PT2[i] |= PG_V | PG_RW | PG_PS | PG_U; } #ifdef DEBUG printf("Start @ %#llx ...\n", ehdr->e_entry); #endif dev_cleanup(); stack[0] = 0; /* return address */ stack[1] = modulep; stack[2] = kernend; CALLBACK(copyin, stack, 0x1000, sizeof(stack)); CALLBACK(copyin, PT4, 0x2000, sizeof(PT4)); CALLBACK(copyin, PT3, 0x3000, sizeof(PT3)); CALLBACK(copyin, PT2, 0x4000, sizeof(PT2)); CALLBACK(setreg, 4, 0x1000); CALLBACK(setmsr, MSR_EFER, EFER_LMA | EFER_LME); CALLBACK(setcr, 4, CR4_PAE | CR4_VMXE); CALLBACK(setcr, 3, 0x2000); CALLBACK(setcr, 0, CR0_PG | CR0_PE | CR0_NE); setup_freebsd_gdt(gdtr); CALLBACK(copyin, gdtr, 0x5000, sizeof(gdtr)); CALLBACK(setgdt, 0x5000, sizeof(gdtr)); CALLBACK(exec, ehdr->e_entry); panic("exec returned"); }
static int multiboot_exec(struct preloaded_file *fp) { vm_offset_t module_start, last_addr, metadata_size; vm_offset_t modulep, kernend, entry; struct file_metadata *md; Elf_Ehdr *ehdr; struct multiboot_info *mb_info = NULL; struct multiboot_mod_list *mb_mod = NULL; char *cmdline = NULL; size_t len; int error, mod_num; /* * Don't pass the memory size found by the bootloader, the memory * available to Dom0 will be lower than that. */ unsetenv("smbios.memory.enabled"); /* Allocate the multiboot struct and fill the basic details. */ mb_info = malloc(sizeof(struct multiboot_info)); if (mb_info == NULL) { error = ENOMEM; goto error; } bzero(mb_info, sizeof(struct multiboot_info)); mb_info->flags = MULTIBOOT_INFO_MEMORY|MULTIBOOT_INFO_BOOT_LOADER_NAME; mb_info->mem_lower = bios_basemem / 1024; mb_info->mem_upper = bios_extmem / 1024; mb_info->boot_loader_name = VTOP(mbl_name); /* Set the Xen command line. */ if (fp->f_args == NULL) { /* Add the Xen command line if it is set. */ cmdline = getenv("xen_cmdline"); if (cmdline != NULL) { fp->f_args = strdup(cmdline); if (fp->f_args == NULL) { error = ENOMEM; goto error; } } } if (fp->f_args != NULL) { len = strlen(fp->f_name) + 1 + strlen(fp->f_args) + 1; cmdline = malloc(len); if (cmdline == NULL) { error = ENOMEM; goto error; } snprintf(cmdline, len, "%s %s", fp->f_name, fp->f_args); mb_info->cmdline = VTOP(cmdline); mb_info->flags |= MULTIBOOT_INFO_CMDLINE; } /* Find the entry point of the Xen kernel and save it for later */ if ((md = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL) { printf("Unable to find %s entry point\n", fp->f_name); error = EINVAL; goto error; } ehdr = (Elf_Ehdr *)&(md->md_data); entry = ehdr->e_entry & 0xffffff; /* * Prepare the multiboot module list, Xen assumes the first * module is the Dom0 kernel, and the second one is the initramfs. * This is not optimal for FreeBSD, that doesn't have a initramfs * but instead loads modules dynamically and creates the metadata * info on-the-fly. * * As expected, the first multiboot module is going to be the * FreeBSD kernel loaded as a raw file. The second module is going * to contain the metadata info and the loaded modules. * * On native FreeBSD loads all the modules and then places the * metadata info at the end, but this is painful when running on Xen, * because it relocates the second multiboot module wherever it * likes. In order to workaround this limitation the metadata * information is placed at the start of the second module and * the original modulep value is saved together with the other * metadata, so we can relocate everything. * * Native layout: * fp->f_addr + fp->f_size * +---------+----------------+------------+ * | | | | * | Kernel | Modules | Metadata | * | | | | * +---------+----------------+------------+ * fp->f_addr modulep kernend * * Xen layout: * * Initial: * fp->f_addr + fp->f_size * +---------+----------+----------------+------------+ * | | | | | * | Kernel | Reserved | Modules | Metadata | * | | | | dry run | * +---------+----------+----------------+------------+ * fp->f_addr * * After metadata polacement (ie: final): * fp->f_addr + fp->f_size * +-----------+---------+----------+----------------+ * | | | | | * | Kernel | Free | Metadata | Modules | * | | | | | * +-----------+---------+----------+----------------+ * fp->f_addr modulep kernend * \__________/ \__________________________/ * Multiboot module 0 Multiboot module 1 */ fp = file_findfile(NULL, "elf kernel"); if (fp == NULL) { printf("No FreeBSD kernel provided, aborting\n"); error = EINVAL; goto error; } if (fp->f_metadata != NULL) { printf("FreeBSD kernel already contains metadata, aborting\n"); error = EINVAL; goto error; } mb_mod = malloc(sizeof(struct multiboot_mod_list) * NUM_MODULES); if (mb_mod == NULL) { error = ENOMEM; goto error; } bzero(mb_mod, sizeof(struct multiboot_mod_list) * NUM_MODULES); /* * Calculate how much memory is needed for the metatdata. We did * an approximation of the maximum size when loading the kernel, * but now we know the exact size, so we can release some of this * preallocated memory if not needed. */ last_addr = roundup(max_addr(), PAGE_SIZE); mod_num = num_modules(fp); /* * Place the metadata after the last used address in order to * calculate it's size, this will not be used. */ error = bi_load64(fp->f_args, last_addr, &modulep, &kernend, 0); if (error != 0) { printf("bi_load64 failed: %d\n", error); goto error; } metadata_size = roundup(kernend - last_addr, PAGE_SIZE); /* Check that the size is not greater than what we have reserved */ if (metadata_size > METADATA_RESV_SIZE(mod_num)) { printf("Required memory for metadata is greater than reserved " "space, please increase METADATA_FIXED_SIZE and " "METADATA_MODULE_SIZE and rebuild the loader\n"); error = ENOMEM; goto error; } /* Clean the metadata added to the kernel in the bi_load64 dry run */ file_removemetadata(fp); /* * This is the position where the second multiboot module * will be placed. */ module_start = fp->f_addr + fp->f_size - metadata_size; error = bi_load64(fp->f_args, module_start, &modulep, &kernend, 0); if (error != 0) { printf("bi_load64 failed: %d\n", error); goto error; } mb_mod[0].mod_start = fp->f_addr; mb_mod[0].mod_end = fp->f_addr + fp->f_size; mb_mod[0].mod_end -= METADATA_RESV_SIZE(mod_num); mb_mod[1].mod_start = module_start; mb_mod[1].mod_end = last_addr; mb_info->mods_count = NUM_MODULES; mb_info->mods_addr = VTOP(mb_mod); mb_info->flags |= MULTIBOOT_INFO_MODS; dev_cleanup(); __exec((void *)VTOP(multiboot_tramp), (void *)entry, (void *)VTOP(mb_info)); panic("exec returned"); error: if (mb_mod) free(mb_mod); if (mb_info) free(mb_info); if (cmdline) free(cmdline); return (error); }