copy.c revision 316273
1/*- 2 * Copyright (c) 2013 The FreeBSD Foundation 3 * All rights reserved. 4 * 5 * This software was developed by Benno Rice under sponsorship from 6 * the FreeBSD Foundation. 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29#include <sys/cdefs.h> 30__FBSDID("$FreeBSD: stable/10/sys/boot/efi/loader/copy.c 316273 2017-03-30 12:51:44Z dexuan $"); 31 32#include <sys/param.h> 33 34#include <stand.h> 35#include <bootstrap.h> 36 37#include <efi.h> 38#include <efilib.h> 39 40#include "loader_efi.h" 41 42#if defined(__i386__) || defined(__amd64__) 43#include <machine/cpufunc.h> 44#include <machine/specialreg.h> 45 46/* 47 * The code is excerpted from sys/x86/x86/identcpu.c: identify_cpu(), 48 * identify_hypervisor(), and dev/hyperv/vmbus/hyperv.c: hyperv_identify(). 49 */ 50#define CPUID_LEAF_HV_MAXLEAF 0x40000000 51#define CPUID_LEAF_HV_INTERFACE 0x40000001 52#define CPUID_LEAF_HV_FEATURES 0x40000003 53#define CPUID_LEAF_HV_LIMITS 0x40000005 54#define CPUID_HV_IFACE_HYPERV 0x31237648 /* HV#1 */ 55#define CPUID_HV_MSR_TIME_REFCNT 0x0002 /* MSR_HV_TIME_REF_COUNT */ 56#define CPUID_HV_MSR_HYPERCALL 0x0020 57 58static int 59running_on_hyperv(void) 60{ 61 char hv_vendor[16]; 62 uint32_t regs[4]; 63 64 do_cpuid(1, regs); 65 if ((regs[2] & CPUID2_HV) == 0) 66 return (0); 67 68 do_cpuid(CPUID_LEAF_HV_MAXLEAF, regs); 69 if (regs[0] < CPUID_LEAF_HV_LIMITS) 70 return (0); 71 72 ((uint32_t *)&hv_vendor)[0] = regs[1]; 73 ((uint32_t *)&hv_vendor)[1] = regs[2]; 74 ((uint32_t *)&hv_vendor)[2] = regs[3]; 75 hv_vendor[12] = '\0'; 76 if (strcmp(hv_vendor, "Microsoft Hv") != 0) 77 return (0); 78 79 do_cpuid(CPUID_LEAF_HV_INTERFACE, regs); 80 if (regs[0] != CPUID_HV_IFACE_HYPERV) 81 return (0); 82 83 do_cpuid(CPUID_LEAF_HV_FEATURES, regs); 84 if ((regs[0] & CPUID_HV_MSR_HYPERCALL) == 0) 85 return (0); 86 if ((regs[0] & CPUID_HV_MSR_TIME_REFCNT) == 0) 87 return (0); 88 89 return (1); 90} 91 92#define KERNEL_PHYSICAL_BASE (2*1024*1024) 93 94static void 95efi_verify_staging_size(unsigned long *nr_pages) 96{ 97 UINTN sz; 98 EFI_MEMORY_DESCRIPTOR *map, *p; 99 EFI_PHYSICAL_ADDRESS start, end; 100 UINTN key, dsz; 101 UINT32 dver; 102 EFI_STATUS status; 103 int i, ndesc; 104 unsigned long available_pages = 0; 105 106 sz = 0; 107 status = BS->GetMemoryMap(&sz, 0, &key, &dsz, &dver); 108 if (status != EFI_BUFFER_TOO_SMALL) { 109 printf("Can't determine memory map size\n"); 110 return; 111 } 112 113 map = malloc(sz); 114 status = BS->GetMemoryMap(&sz, map, &key, &dsz, &dver); 115 if (EFI_ERROR(status)) { 116 printf("Can't read memory map\n"); 117 goto out; 118 } 119 120 ndesc = sz / dsz; 121 for (i = 0, p = map; i < ndesc; 122 i++, p = NextMemoryDescriptor(p, dsz)) { 123 start = p->PhysicalStart; 124 end = start + p->NumberOfPages * EFI_PAGE_SIZE; 125 126 if (KERNEL_PHYSICAL_BASE < start || 127 KERNEL_PHYSICAL_BASE >= end) 128 continue; 129 130 available_pages = p->NumberOfPages - 131 ((KERNEL_PHYSICAL_BASE - start) >> EFI_PAGE_SHIFT); 132 break; 133 } 134 135 if (available_pages == 0) { 136 printf("Can't find valid memory map for staging area!\n"); 137 goto out; 138 } 139 140 i++; 141 p = NextMemoryDescriptor(p, dsz); 142 143 for ( ; i < ndesc; 144 i++, p = NextMemoryDescriptor(p, dsz)) { 145 if (p->Type != EfiConventionalMemory && 146 p->Type != EfiLoaderData) 147 break; 148 149 if (p->PhysicalStart != end) 150 break; 151 152 end = p->PhysicalStart + p->NumberOfPages * EFI_PAGE_SIZE; 153 154 available_pages += p->NumberOfPages; 155 } 156 157 if (*nr_pages > available_pages) { 158 printf("Staging area's size is reduced: %ld -> %ld!\n", 159 *nr_pages, available_pages); 160 *nr_pages = available_pages; 161 } 162out: 163 free(map); 164} 165#endif /* __i386__ || __amd64__ */ 166 167#ifndef EFI_STAGING_SIZE 168#define EFI_STAGING_SIZE 48 169#endif 170 171EFI_PHYSICAL_ADDRESS staging, staging_end; 172int stage_offset_set = 0; 173ssize_t stage_offset; 174 175int 176efi_copy_init(void) 177{ 178 EFI_STATUS status; 179 180 unsigned long nr_pages; 181 182 nr_pages = EFI_SIZE_TO_PAGES((EFI_STAGING_SIZE) * 1024 * 1024); 183 184#if defined(__i386__) || defined(__amd64__) 185 /* 186 * We'll decrease nr_pages, if it's too big. Currently we only 187 * apply this to FreeBSD VM running on Hyper-V. Why? Please see 188 * https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=211746#c28 189 */ 190 if (running_on_hyperv()) 191 efi_verify_staging_size(&nr_pages); 192 193 /* 194 * The staging area must reside in the the first 1GB physical 195 * memory: see elf64_exec() in 196 * boot/efi/loader/arch/amd64/elf64_freebsd.c. 197 */ 198 staging = 1024*1024*1024; 199 status = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData, 200 nr_pages, &staging); 201#else 202 status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData, 203 nr_pages, &staging); 204#endif 205 if (EFI_ERROR(status)) { 206 printf("failed to allocate staging area: %lu\n", 207 EFI_ERROR_CODE(status)); 208 return (status); 209 } 210 staging_end = staging + nr_pages * EFI_PAGE_SIZE; 211 212 return (0); 213} 214 215ssize_t 216efi_copyin(const void *src, vm_offset_t dest, const size_t len) 217{ 218 219 if (!stage_offset_set) { 220 stage_offset = (vm_offset_t)staging - dest; 221 stage_offset_set = 1; 222 } 223 224 /* XXX: Callers do not check for failure. */ 225 if (dest + stage_offset + len > staging_end) { 226 errno = ENOMEM; 227 return (-1); 228 } 229 bcopy(src, (void *)(dest + stage_offset), len); 230 return (len); 231} 232 233ssize_t 234efi_copyout(const vm_offset_t src, void *dest, const size_t len) 235{ 236 237 /* XXX: Callers do not check for failure. */ 238 if (src + stage_offset + len > staging_end) { 239 errno = ENOMEM; 240 return (-1); 241 } 242 bcopy((void *)(src + stage_offset), dest, len); 243 return (len); 244} 245 246 247ssize_t 248efi_readin(const int fd, vm_offset_t dest, const size_t len) 249{ 250 251 if (dest + stage_offset + len > staging_end) { 252 errno = ENOMEM; 253 return (-1); 254 } 255 return (read(fd, (void *)(dest + stage_offset), len)); 256} 257 258void 259efi_copy_finish(void) 260{ 261 uint64_t *src, *dst, *last; 262 263 src = (uint64_t *)staging; 264 dst = (uint64_t *)(staging - stage_offset); 265 last = (uint64_t *)staging_end; 266 267 while (src < last) 268 *dst++ = *src++; 269} 270