grub/grub-core/efiemu/mm.c

634 lines
18 KiB
C
Raw Normal View History

/* Memory management for efiemu */
/*
* 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 <http://www.gnu.org/licenses/>.
*/
/*
2009-06-10 21:04:23 +00:00
To keep efiemu runtime contiguous this mm is special.
It uses deferred allocation.
In the first stage you may request memory with grub_efiemu_request_memalign
2009-06-10 21:04:23 +00:00
It will give you a handle with which in the second phase you can access your
memory with grub_efiemu_mm_obtain_request (handle). It's guaranteed that
subsequent calls with the same handle return the same result. You can't request any additional memory once you're in the second phase
*/
#include <grub/err.h>
#include <grub/normal.h>
#include <grub/mm.h>
#include <grub/misc.h>
#include <grub/machine/memory.h>
#include <grub/efiemu/efiemu.h>
2009-06-10 21:04:23 +00:00
struct grub_efiemu_memrequest
{
struct grub_efiemu_memrequest *next;
grub_efi_memory_type_t type;
grub_size_t size;
grub_size_t align_overhead;
int handle;
void *val;
};
/* Linked list of requested memory. */
static struct grub_efiemu_memrequest *memrequests = 0;
/* Memory map. */
static grub_efi_memory_descriptor_t *efiemu_mmap = 0;
/* Pointer to allocated memory */
static void *resident_memory = 0;
/* Size of requested memory per type */
static grub_size_t requested_memory[GRUB_EFI_MAX_MEMORY_TYPE];
/* How many slots is allocated for memory_map and how many are already used */
static int mmap_reserved_size = 0, mmap_num = 0;
/* Add a memory region to map*/
static grub_err_t
2009-06-10 21:04:23 +00:00
grub_efiemu_add_to_mmap (grub_uint64_t start, grub_uint64_t size,
grub_efi_memory_type_t type)
{
grub_uint64_t page_start, npages;
/* Extend map if necessary*/
if (mmap_num >= mmap_reserved_size)
{
efiemu_mmap = (grub_efi_memory_descriptor_t *)
2009-06-10 21:04:23 +00:00
grub_realloc (efiemu_mmap, (++mmap_reserved_size)
* sizeof (grub_efi_memory_descriptor_t));
if (!efiemu_mmap)
2009-06-10 21:04:23 +00:00
return grub_error (GRUB_ERR_OUT_OF_MEMORY,
2009-12-24 Carles Pina i Estany <carles@pina.cat> * bus/usb/usbhub.c: Fix capitalization, fullstop and newlines in grub_errno calls. * commands/acpi.c: Likewise. * commands/blocklist.c: Likewise. * commands/efi/loadbios.c: Likewise. * commands/i386/pc/drivemap.c: Likewise. * commands/loadenv.c: Likewise. * commands/memrw.c: Likewise. * commands/password.c: Likewise. * commands/videotest.c: Likewise. * disk/ata.c: Likewise. * disk/ata_pthru.c: Likewise. * disk/dmraid_nvidia.c: Likewise. * disk/ieee1275/nand.c: Likewise. * disk/ieee1275/ofdisk.c: Likewise. * disk/loopback.c: Likewise. * disk/lvm.c: Likewise. * disk/mdraid_linux.c: Likewise. * disk/raid.c: Likewise. * disk/raid6_recover.c: Likewise. * disk/scsi.c: Likewise. * efiemu/main.c: Likewise. * efiemu/mm.c: Likewise. * efiemu/pnvram.c: Likewise. * efiemu/symbols.c: Likewise. * font/font.c: Likewise. * fs/cpio.c: Likewise. * fs/hfsplus.c: Likewise. * fs/iso9660.c: Likewise. * fs/jfs.c: Likewise. * fs/minix.c: Likewise. * fs/ntfs.c: Likewise. * fs/ntfscomp.c: Likewise. * fs/reiserfs.c: Likewise. * fs/ufs.c: Likewise. * fs/xfs.c: Likewise. * gettext/gettext.c: Likewise. * include/grub/auth.h: Likewise. * kern/elf.c: Likewise. * kern/file.c: Likewise. * kern/ieee1275/init.c: Likewise. * kern/ieee1275/mmap.c: Likewise. * kern/ieee1275/openfw.c: Likewise. * kern/powerpc/dl.c: Likewise. * kern/sparc64/dl.c: Likewise. * lib/arg.c: Likewise. * loader/i386/bsd.c: Likewise. * loader/i386/bsdXX.c: Likewise. * loader/i386/efi/linux.c: Likewise. * loader/i386/efi/xnu.c: Likewise. * loader/i386/ieee1275/linux.c: Likewise. * loader/i386/linux.c: Likewise. * loader/i386/multiboot.c: Likewise. * loader/i386/pc/linux.c: Likewise. * loader/i386/pc/multiboot2.c: Likewise. * loader/i386/xnu.c: Likewise. * loader/ieee1275/multiboot2.c: Likewise. * loader/macho.c: Likewise. * loader/machoXX.c: Likewise. * loader/multiboot2.c: Likewise. * loader/multiboot_loader.c: Likewise. * loader/powerpc/ieee1275/linux.c: Likewise. * loader/sparc64/ieee1275/linux.c: Likewise. * loader/xnu.c: Likewise. * loader/xnu_resume.c: Likewise. * mmap/i386/pc/mmap.c: Likewise. * normal/menu_viewer.c: Likewise. * partmap/acorn.c: Likewise. * partmap/amiga.c: Likewise. * partmap/apple.c: Likewise. * script/lexer.c: Likewise. * term/gfxterm.c: Likewise. * term/i386/pc/serial.c: Likewise. * term/i386/pc/vga.c: Likewise. * term/ieee1275/ofconsole.c: Likewise. * term/terminfo.c: Likewise. * video/bitmap.c: Likewise. * video/efi_gop.c: Likewise. * video/efi_uga.c: Likewise. * video/fb/video_fb.c: Likewise. * video/i386/pc/vbe.c: Likewise. * video/readers/tga.c: Likewise. * video/video.c: Likewise.
2009-12-24 22:53:05 +00:00
"not enough space for memory map");
}
/* Fill slot*/
page_start = start - (start % GRUB_EFIEMU_PAGESIZE);
npages = (size + (start % GRUB_EFIEMU_PAGESIZE) + GRUB_EFIEMU_PAGESIZE - 1)
/ GRUB_EFIEMU_PAGESIZE;
2009-06-10 21:04:23 +00:00
efiemu_mmap[mmap_num].physical_start = page_start;
efiemu_mmap[mmap_num].virtual_start = page_start;
efiemu_mmap[mmap_num].num_pages = npages;
efiemu_mmap[mmap_num].type = type;
mmap_num++;
return GRUB_ERR_NONE;
}
/* Request a resident memory of type TYPE of size SIZE aligned at ALIGN
2009-06-10 21:04:23 +00:00
ALIGN must be a divisor of page size (if it's a divisor of 4096
it should be ok on all platforms)
*/
int
2009-06-10 21:04:23 +00:00
grub_efiemu_request_memalign (grub_size_t align, grub_size_t size,
grub_efi_memory_type_t type)
{
grub_size_t align_overhead;
struct grub_efiemu_memrequest *ret, *cur, *prev;
/* Check that the request is correct */
if (type >= GRUB_EFI_MAX_MEMORY_TYPE || type <= GRUB_EFI_LOADER_CODE)
return -2;
/* Add new size to requested size */
align_overhead = align - (requested_memory[type]%align);
if (align_overhead == align)
align_overhead = 0;
requested_memory[type] += align_overhead + size;
/* Remember the request */
2009-07-16 Pavel Roskin <proski@gnu.org> * kern/mm.c (grub_zalloc): New function. (grub_debug_zalloc): Likewise. * include/grub/mm.h: Declare grub_zalloc() and grub_debug_zalloc(). * util/misc.c (grub_zalloc): New function. * bus/usb/uhci.c (grub_uhci_pci_iter): Use grub_zalloc() instead of grub_malloc(), remove unneeded initializations. * bus/usb/usbhub.c (grub_usb_hub_add_dev): Likewise. * commands/extcmd.c (grub_extcmd_dispatcher): Likewise. * commands/parttool.c (grub_cmd_parttool): Likewise. * disk/i386/pc/biosdisk.c (grub_biosdisk_open): Likewise. * disk/raid5_recover.c (grub_raid5_recover): Likewise. * disk/raid6_recover.c (grub_raid6_recover): Likewise. * disk/usbms.c (grub_usbms_finddevs): Likewise. * efiemu/mm.c (grub_efiemu_request_memalign): Likewise. * efiemu/pnvram.c (grub_efiemu_pnvram): Likewise. (grub_cmd_efiemu_pnvram): Likewise. * fs/i386/pc/pxe.c (grub_pxefs_open): Likewise. * fs/iso9660.c (grub_iso9660_mount): Likewise. (grub_iso9660_iterate_dir): Likewise. * fs/jfs.c (grub_jfs_opendir): Likewise. * fs/ntfs.c (list_file): Likewise. (grub_ntfs_mount): Likewise. * kern/disk.c (grub_disk_open): Likewise. * kern/dl.c (grub_dl_load_core): Likewise. * kern/elf.c (grub_elf_file): Likewise. * kern/env.c (grub_env_context_open): Likewise. (grub_env_set): Likewise. (grub_env_set_data_slot): Likewise. * kern/file.c (grub_file_open): Likewise. * kern/fs.c (grub_fs_blocklist_open): Likewise. * loader/i386/multiboot.c (grub_module): Likewise. * loader/xnu.c (grub_xnu_create_key): Likewise. (grub_xnu_create_value): Likewise. * normal/main.c (grub_normal_add_menu_entry): Likewise. (read_config_file): Likewise. * normal/menu_entry.c (make_screen): Likewise. * partmap/sun.c (sun_partition_map_iterate): Likewise. * script/sh/lexer.c (grub_script_lexer_init): Likewise. * script/sh/script.c (grub_script_parse): Likewise. * video/bitmap.c (grub_video_bitmap_create): Likewise. * video/readers/jpeg.c (grub_video_reader_jpeg): Likewise. * video/readers/png.c (grub_png_output_byte): Likewise. (grub_video_reader_png): Likewise.
2009-07-16 22:14:09 +00:00
ret = grub_zalloc (sizeof (*ret));
if (!ret)
return -1;
ret->type = type;
ret->size = size;
ret->align_overhead = align_overhead;
prev = 0;
2009-06-10 21:04:23 +00:00
/* Add request to the end of the chain.
It should be at the end because otherwise alignment isn't guaranteed */
for (cur = memrequests; cur; prev = cur, cur = cur->next);
if (prev)
{
ret->handle = prev->handle + 1;
prev->next = ret;
}
else
{
ret->handle = 1; /* Avoid 0 handle*/
memrequests = ret;
}
return ret->handle;
}
/* Really allocate the memory */
static grub_err_t
efiemu_alloc_requests (void)
{
grub_size_t align_overhead = 0;
grub_uint8_t *curptr, *typestart;
struct grub_efiemu_memrequest *cur;
grub_size_t total_alloc = 0;
unsigned i;
/* Order of memory regions */
grub_efi_memory_type_t reqorder[] =
{
/* First come regions usable by OS*/
GRUB_EFI_LOADER_CODE,
GRUB_EFI_LOADER_DATA,
GRUB_EFI_BOOT_SERVICES_CODE,
GRUB_EFI_BOOT_SERVICES_DATA,
GRUB_EFI_CONVENTIONAL_MEMORY,
GRUB_EFI_ACPI_RECLAIM_MEMORY,
/* Then memory used by runtime */
/* This way all our regions are in a single block */
GRUB_EFI_RUNTIME_SERVICES_CODE,
GRUB_EFI_RUNTIME_SERVICES_DATA,
GRUB_EFI_ACPI_MEMORY_NVS,
2009-06-10 21:04:23 +00:00
/* And then unavailable memory types. This is more for a completeness.
You should double think before allocating memory of any of these types
*/
GRUB_EFI_UNUSABLE_MEMORY,
GRUB_EFI_MEMORY_MAPPED_IO,
GRUB_EFI_MEMORY_MAPPED_IO_PORT_SPACE,
2009-06-10 21:04:23 +00:00
GRUB_EFI_PAL_CODE
};
/* Compute total memory needed */
for (i = 0; i < sizeof (reqorder) / sizeof (reqorder[0]); i++)
{
2009-06-10 21:04:23 +00:00
align_overhead = GRUB_EFIEMU_PAGESIZE
- (requested_memory[reqorder[i]] % GRUB_EFIEMU_PAGESIZE);
if (align_overhead == GRUB_EFIEMU_PAGESIZE)
align_overhead = 0;
total_alloc += requested_memory[reqorder[i]] + align_overhead;
}
/* Allocate the whole memory in one block */
resident_memory = grub_memalign (GRUB_EFIEMU_PAGESIZE, total_alloc);
if (!resident_memory)
2009-06-10 21:04:23 +00:00
return grub_error (GRUB_ERR_OUT_OF_MEMORY,
"couldn't allocate resident memory");
/* Split the memory into blocks by type */
curptr = resident_memory;
for (i = 0; i < sizeof (reqorder) / sizeof (reqorder[0]); i++)
{
if (!requested_memory[reqorder[i]])
continue;
typestart = curptr;
/* Write pointers to requests */
for (cur = memrequests; cur; cur = cur->next)
if (cur->type == reqorder[i])
{
curptr = ((grub_uint8_t *)curptr) + cur->align_overhead;
cur->val = curptr;
curptr = ((grub_uint8_t *)curptr) + cur->size;
}
/* Ensure that the regions are page-aligned */
2009-06-10 21:04:23 +00:00
align_overhead = GRUB_EFIEMU_PAGESIZE
- (requested_memory[reqorder[i]] % GRUB_EFIEMU_PAGESIZE);
if (align_overhead == GRUB_EFIEMU_PAGESIZE)
align_overhead = 0;
curptr = ((grub_uint8_t *)curptr) + align_overhead;
2009-06-10 21:04:23 +00:00
/* Add the region to memory map */
2009-06-10 21:04:23 +00:00
grub_efiemu_add_to_mmap (PTR_TO_UINT64 (typestart),
curptr - typestart, reqorder[i]);
}
return GRUB_ERR_NONE;
}
/* Get a pointer to requested memory from handle */
void *
grub_efiemu_mm_obtain_request (int handle)
{
struct grub_efiemu_memrequest *cur;
for (cur = memrequests; cur; cur = cur->next)
if (cur->handle == handle)
return cur->val;
return 0;
}
/* Get type of requested memory by handle */
grub_efi_memory_type_t
grub_efiemu_mm_get_type (int handle)
{
struct grub_efiemu_memrequest *cur;
for (cur = memrequests; cur; cur = cur->next)
if (cur->handle == handle)
return cur->type;
return 0;
}
/* Free a request */
void
grub_efiemu_mm_return_request (int handle)
{
struct grub_efiemu_memrequest *cur, *prev;
/* Remove head if necessary */
while (memrequests && memrequests->handle == handle)
{
cur = memrequests->next;
grub_free (memrequests);
memrequests = cur;
}
if (!memrequests)
return;
/* Remove request from a middle of chain*/
for (prev = memrequests, cur = prev->next; cur;)
if (cur->handle == handle)
{
prev->next = cur->next;
grub_free (cur);
cur = prev->next;
}
else
{
prev = cur;
cur = prev->next;
}
}
/* Reserve space for memory map */
static grub_err_t
grub_efiemu_mmap_init (void)
{
2009-06-10 21:04:23 +00:00
auto int NESTED_FUNC_ATTR bounds_hook (grub_uint64_t, grub_uint64_t,
grub_uint32_t);
int NESTED_FUNC_ATTR bounds_hook (grub_uint64_t addr __attribute__ ((unused)),
grub_uint64_t size __attribute__ ((unused)),
grub_uint32_t type __attribute__ ((unused)))
{
mmap_reserved_size++;
return 0;
}
// the place for memory used by efiemu itself
mmap_reserved_size = GRUB_EFI_MAX_MEMORY_TYPE + 1;
#ifndef GRUB_MACHINE_EMU
grub_machine_mmap_iterate (bounds_hook);
#endif
return GRUB_ERR_NONE;
}
/* This is a drop-in replacement of grub_efi_get_memory_map */
/* Get the memory map as defined in the EFI spec. Return 1 if successful,
return 0 if partial, or return -1 if an error occurs. */
int
grub_efiemu_get_memory_map (grub_efi_uintn_t *memory_map_size,
grub_efi_memory_descriptor_t *memory_map,
grub_efi_uintn_t *map_key,
grub_efi_uintn_t *descriptor_size,
grub_efi_uint32_t *descriptor_version)
{
if (!efiemu_mmap)
{
2009-06-10 21:04:23 +00:00
grub_error (GRUB_ERR_INVALID_COMMAND,
"you need to first launch efiemu_prepare");
return -1;
}
if (*memory_map_size < mmap_num * sizeof (grub_efi_memory_descriptor_t))
{
*memory_map_size = mmap_num * sizeof (grub_efi_memory_descriptor_t);
return 0;
}
*memory_map_size = mmap_num * sizeof (grub_efi_memory_descriptor_t);
grub_memcpy (memory_map, efiemu_mmap, *memory_map_size);
if (descriptor_size)
*descriptor_size = sizeof (grub_efi_memory_descriptor_t);
if (descriptor_version)
*descriptor_version = 1;
if (map_key)
*map_key = 0;
return 1;
}
/* Free everything */
grub_err_t
grub_efiemu_mm_unload (void)
{
struct grub_efiemu_memrequest *cur, *d;
for (cur = memrequests; cur;)
{
d = cur->next;
grub_free (cur);
cur = d;
}
memrequests = 0;
grub_memset (&requested_memory, 0, sizeof (requested_memory));
grub_free (resident_memory);
resident_memory = 0;
grub_free (efiemu_mmap);
efiemu_mmap = 0;
mmap_reserved_size = mmap_num = 0;
return GRUB_ERR_NONE;
}
/* This function should be called before doing any requests */
grub_err_t
grub_efiemu_mm_init (void)
{
grub_err_t err;
err = grub_efiemu_mm_unload ();
if (err)
return err;
grub_efiemu_mmap_init ();
return GRUB_ERR_NONE;
}
/* Copy host memory map */
static grub_err_t
grub_efiemu_mmap_fill (void)
{
auto int NESTED_FUNC_ATTR fill_hook (grub_uint64_t, grub_uint64_t, grub_uint32_t);
int NESTED_FUNC_ATTR fill_hook (grub_uint64_t addr,
grub_uint64_t size,
grub_uint32_t type)
{
switch (type)
{
case GRUB_MACHINE_MEMORY_AVAILABLE:
2009-06-10 21:04:23 +00:00
return grub_efiemu_add_to_mmap (addr, size,
GRUB_EFI_CONVENTIONAL_MEMORY);
#ifdef GRUB_MACHINE_MEMORY_ACPI
case GRUB_MACHINE_MEMORY_ACPI:
2009-06-10 21:04:23 +00:00
return grub_efiemu_add_to_mmap (addr, size,
GRUB_EFI_ACPI_RECLAIM_MEMORY);
#endif
#ifdef GRUB_MACHINE_MEMORY_NVS
case GRUB_MACHINE_MEMORY_NVS:
2009-06-10 21:04:23 +00:00
return grub_efiemu_add_to_mmap (addr, size,
GRUB_EFI_ACPI_MEMORY_NVS);
#endif
default:
grub_printf ("Unknown memory type %d. Marking as unusable\n", type);
case GRUB_MACHINE_MEMORY_RESERVED:
2009-06-10 21:04:23 +00:00
return grub_efiemu_add_to_mmap (addr, size,
GRUB_EFI_UNUSABLE_MEMORY);
}
}
#ifndef GRUB_MACHINE_EMU
grub_machine_mmap_iterate (fill_hook);
#endif
return GRUB_ERR_NONE;
}
grub_err_t
2009-06-10 21:04:23 +00:00
grub_efiemu_mmap_iterate (int NESTED_FUNC_ATTR (*hook) (grub_uint64_t,
grub_uint64_t,
grub_uint32_t))
{
unsigned i;
for (i = 0; i < (unsigned) mmap_num; i++)
switch (efiemu_mmap[i].type)
{
case GRUB_EFI_RUNTIME_SERVICES_CODE:
2009-06-10 21:04:23 +00:00
hook (efiemu_mmap[i].physical_start, efiemu_mmap[i].num_pages * 4096,
GRUB_EFIEMU_MEMORY_CODE);
break;
2009-06-10 21:04:23 +00:00
case GRUB_EFI_RESERVED_MEMORY_TYPE:
case GRUB_EFI_RUNTIME_SERVICES_DATA:
case GRUB_EFI_UNUSABLE_MEMORY:
case GRUB_EFI_MEMORY_MAPPED_IO:
case GRUB_EFI_MEMORY_MAPPED_IO_PORT_SPACE:
case GRUB_EFI_PAL_CODE:
case GRUB_EFI_MAX_MEMORY_TYPE:
2009-06-10 21:04:23 +00:00
hook (efiemu_mmap[i].physical_start, efiemu_mmap[i].num_pages * 4096,
GRUB_EFIEMU_MEMORY_RESERVED);
break;
2009-06-10 21:04:23 +00:00
case GRUB_EFI_LOADER_CODE:
case GRUB_EFI_LOADER_DATA:
case GRUB_EFI_BOOT_SERVICES_CODE:
case GRUB_EFI_BOOT_SERVICES_DATA:
case GRUB_EFI_CONVENTIONAL_MEMORY:
2009-06-10 21:04:23 +00:00
hook (efiemu_mmap[i].physical_start, efiemu_mmap[i].num_pages * 4096,
GRUB_EFIEMU_MEMORY_AVAILABLE);
break;
2009-06-10 21:04:23 +00:00
case GRUB_EFI_ACPI_RECLAIM_MEMORY:
2009-06-10 21:04:23 +00:00
hook (efiemu_mmap[i].physical_start, efiemu_mmap[i].num_pages * 4096,
GRUB_EFIEMU_MEMORY_ACPI);
break;
2009-06-10 21:04:23 +00:00
case GRUB_EFI_ACPI_MEMORY_NVS:
2009-06-10 21:04:23 +00:00
hook (efiemu_mmap[i].physical_start, efiemu_mmap[i].num_pages * 4096,
GRUB_EFIEMU_MEMORY_NVS);
break;
}
2009-06-10 21:04:23 +00:00
return 0;
}
/* This function resolves overlapping regions and sorts the memory map
It uses scanline (sweeping) algorithm
*/
static grub_err_t
grub_efiemu_mmap_sort_and_uniq (void)
{
2009-06-10 21:04:23 +00:00
/* If same page is used by multiple types it's resolved
according to priority
0 - free memory
1 - memory immediately usable after ExitBootServices
2 - memory usable after loading ACPI tables
3 - efiemu memory
4 - unusable memory
*/
int priority[GRUB_EFI_MAX_MEMORY_TYPE] =
{
[GRUB_EFI_RESERVED_MEMORY_TYPE] = 4,
[GRUB_EFI_LOADER_CODE] = 1,
[GRUB_EFI_LOADER_DATA] = 1,
[GRUB_EFI_BOOT_SERVICES_CODE] = 1,
[GRUB_EFI_BOOT_SERVICES_DATA] = 1,
[GRUB_EFI_RUNTIME_SERVICES_CODE] = 3,
[GRUB_EFI_RUNTIME_SERVICES_DATA] = 3,
[GRUB_EFI_CONVENTIONAL_MEMORY] = 0,
[GRUB_EFI_UNUSABLE_MEMORY] = 4,
[GRUB_EFI_ACPI_RECLAIM_MEMORY] = 2,
[GRUB_EFI_ACPI_MEMORY_NVS] = 3,
[GRUB_EFI_MEMORY_MAPPED_IO] = 4,
[GRUB_EFI_MEMORY_MAPPED_IO_PORT_SPACE] = 4,
[GRUB_EFI_PAL_CODE] = 4
};
int i, j, k, done;
/* Scanline events */
struct grub_efiemu_mmap_scan
{
/* At which memory address*/
grub_uint64_t pos;
/* 0 = region starts, 1 = region ends */
int type;
/* Which type of memory region */
grub_efi_memory_type_t memtype;
};
struct grub_efiemu_mmap_scan *scanline_events;
struct grub_efiemu_mmap_scan t;
/* Previous scanline event */
grub_uint64_t lastaddr;
int lasttype;
/* Current scanline event */
int curtype;
/* how many regions of given type overlap at current location */
int present[GRUB_EFI_MAX_MEMORY_TYPE];
/* Here is stored the resulting memory map*/
grub_efi_memory_descriptor_t *result;
/* Initialize variables*/
grub_memset (present, 0, sizeof (int) * GRUB_EFI_MAX_MEMORY_TYPE);
2009-06-10 21:04:23 +00:00
scanline_events = (struct grub_efiemu_mmap_scan *)
grub_malloc (sizeof (struct grub_efiemu_mmap_scan) * 2 * mmap_num);
/* Number of chunks can't increase more than by factor of 2 */
2009-06-10 21:04:23 +00:00
result = (grub_efi_memory_descriptor_t *)
grub_malloc (sizeof (grub_efi_memory_descriptor_t) * 2 * mmap_num);
if (!result || !scanline_events)
{
grub_free (result);
grub_free (scanline_events);
2009-06-10 21:04:23 +00:00
return grub_error (GRUB_ERR_OUT_OF_MEMORY,
"couldn't allocate space for new memory map");
}
2009-06-10 21:04:23 +00:00
/* Register scanline events */
for (i = 0; i < mmap_num; i++)
{
scanline_events[2 * i].pos = efiemu_mmap[i].physical_start;
scanline_events[2 * i].type = 0;
scanline_events[2 * i].memtype = efiemu_mmap[i].type;
scanline_events[2 * i + 1].pos = efiemu_mmap[i].physical_start
+ efiemu_mmap[i].num_pages * GRUB_EFIEMU_PAGESIZE;
scanline_events[2 * i + 1].type = 1;
scanline_events[2 * i + 1].memtype = efiemu_mmap[i].type;
}
2009-06-10 21:04:23 +00:00
/* Primitive bubble sort. It has complexity O(n^2) but since we're
unlikely to have more than 100 chunks it's probably one of the
fastest for one purpose */
done = 1;
while (done)
{
done = 0;
for (i = 0; i < 2 * mmap_num - 1; i++)
if (scanline_events[i + 1].pos < scanline_events[i].pos)
{
t = scanline_events[i + 1];
scanline_events[i + 1] = scanline_events[i];
scanline_events[i] = t;
done = 1;
}
}
/* Pointer in resulting memory map */
j = 0;
lastaddr = scanline_events[0].pos;
lasttype = scanline_events[0].memtype;
for (i = 0; i < 2 * mmap_num; i++)
{
/* Process event */
if (scanline_events[i].type)
present[scanline_events[i].memtype]--;
else
present[scanline_events[i].memtype]++;
/* Determine current region type */
curtype = -1;
for (k = 0; k < GRUB_EFI_MAX_MEMORY_TYPE; k++)
if (present[k] && (curtype == -1 || priority[k] > priority[curtype]))
curtype = k;
/* Add memory region to resulting map if necessary */
2009-06-10 21:04:23 +00:00
if ((curtype == -1 || curtype != lasttype)
&& lastaddr != scanline_events[i].pos
&& lasttype != -1)
{
result[j].virtual_start = result[j].physical_start = lastaddr;
2009-06-10 21:04:23 +00:00
result[j].num_pages = (scanline_events[i].pos - lastaddr)
/ GRUB_EFIEMU_PAGESIZE;
result[j].type = lasttype;
/* We set runtime attribute on pages we need to be mapped */
2009-06-10 21:04:23 +00:00
result[j].attribute
= (lasttype == GRUB_EFI_RUNTIME_SERVICES_CODE
|| lasttype == GRUB_EFI_RUNTIME_SERVICES_DATA)
? GRUB_EFI_MEMORY_RUNTIME : 0;
2009-06-10 21:04:23 +00:00
grub_dprintf ("efiemu",
"mmap entry: type %d start 0x%llx 0x%llx pages\n",
result[j].type,
result[j].physical_start, result[j].num_pages);
j++;
}
/* Update last values if necessary */
2009-06-10 21:04:23 +00:00
if (curtype == -1 || curtype != lasttype)
{
lasttype = curtype;
lastaddr = scanline_events[i].pos;
}
}
grub_free (scanline_events);
2009-06-10 21:04:23 +00:00
/* Shrink resulting memory map to really used size and replace efiemu_mmap
by new value */
grub_free (efiemu_mmap);
efiemu_mmap = grub_realloc (result, j * sizeof (*result));
return GRUB_ERR_NONE;
}
/* This function is called to switch from first to second phase */
grub_err_t
grub_efiemu_mm_do_alloc (void)
{
grub_err_t err;
/* Preallocate mmap */
2009-06-10 21:04:23 +00:00
efiemu_mmap = (grub_efi_memory_descriptor_t *)
grub_malloc (mmap_reserved_size * sizeof (grub_efi_memory_descriptor_t));
if (!efiemu_mmap)
{
grub_efiemu_unload ();
2009-12-24 Carles Pina i Estany <carles@pina.cat> * bus/usb/usbhub.c: Fix capitalization, fullstop and newlines in grub_errno calls. * commands/acpi.c: Likewise. * commands/blocklist.c: Likewise. * commands/efi/loadbios.c: Likewise. * commands/i386/pc/drivemap.c: Likewise. * commands/loadenv.c: Likewise. * commands/memrw.c: Likewise. * commands/password.c: Likewise. * commands/videotest.c: Likewise. * disk/ata.c: Likewise. * disk/ata_pthru.c: Likewise. * disk/dmraid_nvidia.c: Likewise. * disk/ieee1275/nand.c: Likewise. * disk/ieee1275/ofdisk.c: Likewise. * disk/loopback.c: Likewise. * disk/lvm.c: Likewise. * disk/mdraid_linux.c: Likewise. * disk/raid.c: Likewise. * disk/raid6_recover.c: Likewise. * disk/scsi.c: Likewise. * efiemu/main.c: Likewise. * efiemu/mm.c: Likewise. * efiemu/pnvram.c: Likewise. * efiemu/symbols.c: Likewise. * font/font.c: Likewise. * fs/cpio.c: Likewise. * fs/hfsplus.c: Likewise. * fs/iso9660.c: Likewise. * fs/jfs.c: Likewise. * fs/minix.c: Likewise. * fs/ntfs.c: Likewise. * fs/ntfscomp.c: Likewise. * fs/reiserfs.c: Likewise. * fs/ufs.c: Likewise. * fs/xfs.c: Likewise. * gettext/gettext.c: Likewise. * include/grub/auth.h: Likewise. * kern/elf.c: Likewise. * kern/file.c: Likewise. * kern/ieee1275/init.c: Likewise. * kern/ieee1275/mmap.c: Likewise. * kern/ieee1275/openfw.c: Likewise. * kern/powerpc/dl.c: Likewise. * kern/sparc64/dl.c: Likewise. * lib/arg.c: Likewise. * loader/i386/bsd.c: Likewise. * loader/i386/bsdXX.c: Likewise. * loader/i386/efi/linux.c: Likewise. * loader/i386/efi/xnu.c: Likewise. * loader/i386/ieee1275/linux.c: Likewise. * loader/i386/linux.c: Likewise. * loader/i386/multiboot.c: Likewise. * loader/i386/pc/linux.c: Likewise. * loader/i386/pc/multiboot2.c: Likewise. * loader/i386/xnu.c: Likewise. * loader/ieee1275/multiboot2.c: Likewise. * loader/macho.c: Likewise. * loader/machoXX.c: Likewise. * loader/multiboot2.c: Likewise. * loader/multiboot_loader.c: Likewise. * loader/powerpc/ieee1275/linux.c: Likewise. * loader/sparc64/ieee1275/linux.c: Likewise. * loader/xnu.c: Likewise. * loader/xnu_resume.c: Likewise. * mmap/i386/pc/mmap.c: Likewise. * normal/menu_viewer.c: Likewise. * partmap/acorn.c: Likewise. * partmap/amiga.c: Likewise. * partmap/apple.c: Likewise. * script/lexer.c: Likewise. * term/gfxterm.c: Likewise. * term/i386/pc/serial.c: Likewise. * term/i386/pc/vga.c: Likewise. * term/ieee1275/ofconsole.c: Likewise. * term/terminfo.c: Likewise. * video/bitmap.c: Likewise. * video/efi_gop.c: Likewise. * video/efi_uga.c: Likewise. * video/fb/video_fb.c: Likewise. * video/i386/pc/vbe.c: Likewise. * video/readers/tga.c: Likewise. * video/video.c: Likewise.
2009-12-24 22:53:05 +00:00
return grub_error (GRUB_ERR_OUT_OF_MEMORY, "couldn't initialize mmap");
}
if ((err = efiemu_alloc_requests ()))
return err;
if ((err = grub_efiemu_mmap_fill ()))
return err;
return grub_efiemu_mmap_sort_and_uniq ();
2009-06-10 21:04:23 +00:00
}