summaryrefslogtreecommitdiffstats
path: root/grub-core/mmap
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 16:29:51 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 16:29:51 +0000
commit6e7a315eb67cb6c113cf37e1d66c4f11a51a2b3e (patch)
tree32451fa3cdd9321fb2591fada9891b2cb70a9cd1 /grub-core/mmap
parentInitial commit. (diff)
downloadgrub2-upstream.tar.xz
grub2-upstream.zip
Adding upstream version 2.06.upstream/2.06upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'grub-core/mmap')
-rw-r--r--grub-core/mmap/efi/mmap.c318
-rw-r--r--grub-core/mmap/i386/mmap.c113
-rw-r--r--grub-core/mmap/i386/pc/mmap.c211
-rw-r--r--grub-core/mmap/i386/pc/mmap_helper.S163
-rw-r--r--grub-core/mmap/i386/uppermem.c98
-rw-r--r--grub-core/mmap/mips/uppermem.c72
-rw-r--r--grub-core/mmap/mmap.c554
7 files changed, 1529 insertions, 0 deletions
diff --git a/grub-core/mmap/efi/mmap.c b/grub-core/mmap/efi/mmap.c
new file mode 100644
index 0000000..bd495a1
--- /dev/null
+++ b/grub-core/mmap/efi/mmap.c
@@ -0,0 +1,318 @@
+/* Mmap management. */
+/*
+ * 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/>.
+ */
+
+#include <grub/machine/memory.h>
+#include <grub/memory.h>
+#include <grub/err.h>
+#include <grub/efi/api.h>
+#include <grub/efi/efi.h>
+#include <grub/mm.h>
+#include <grub/misc.h>
+
+#define NEXT_MEMORY_DESCRIPTOR(desc, size) \
+ ((grub_efi_memory_descriptor_t *) ((char *) (desc) + (size)))
+
+grub_err_t
+grub_efi_mmap_iterate (grub_memory_hook_t hook, void *hook_data,
+ int avoid_efi_boot_services)
+{
+ grub_efi_uintn_t mmap_size = 0;
+ grub_efi_memory_descriptor_t *map_buf = 0;
+ grub_efi_uintn_t map_key = 0;
+ grub_efi_uintn_t desc_size = 0;
+ grub_efi_uint32_t desc_version = 0;
+ grub_efi_memory_descriptor_t *desc;
+
+ if (grub_efi_get_memory_map (&mmap_size, map_buf,
+ &map_key, &desc_size,
+ &desc_version) < 0)
+ return grub_errno;
+
+ map_buf = grub_malloc (mmap_size);
+ if (! map_buf)
+ return grub_errno;
+
+ if (grub_efi_get_memory_map (&mmap_size, map_buf,
+ &map_key, &desc_size,
+ &desc_version) <= 0)
+ {
+ grub_free (map_buf);
+ return grub_errno;
+ }
+
+ for (desc = map_buf;
+ desc < NEXT_MEMORY_DESCRIPTOR (map_buf, mmap_size);
+ desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size))
+ {
+ grub_dprintf ("mmap", "EFI memory region 0x%llx-0x%llx: %d\n",
+ (unsigned long long) desc->physical_start,
+ (unsigned long long) desc->physical_start
+ + desc->num_pages * 4096, desc->type);
+ switch (desc->type)
+ {
+ case GRUB_EFI_BOOT_SERVICES_CODE:
+ if (!avoid_efi_boot_services)
+ {
+ hook (desc->physical_start, desc->num_pages * 4096,
+ GRUB_MEMORY_AVAILABLE, hook_data);
+ break;
+ }
+ /* FALLTHROUGH */
+ case GRUB_EFI_RUNTIME_SERVICES_CODE:
+ hook (desc->physical_start, desc->num_pages * 4096,
+ GRUB_MEMORY_CODE, hook_data);
+ break;
+
+ case GRUB_EFI_UNUSABLE_MEMORY:
+ hook (desc->physical_start, desc->num_pages * 4096,
+ GRUB_MEMORY_BADRAM, hook_data);
+ break;
+
+ case GRUB_EFI_BOOT_SERVICES_DATA:
+ if (!avoid_efi_boot_services)
+ {
+ hook (desc->physical_start, desc->num_pages * 4096,
+ GRUB_MEMORY_AVAILABLE, hook_data);
+ break;
+ }
+ /* FALLTHROUGH */
+ case GRUB_EFI_RESERVED_MEMORY_TYPE:
+ case GRUB_EFI_RUNTIME_SERVICES_DATA:
+ case GRUB_EFI_MEMORY_MAPPED_IO:
+ case GRUB_EFI_MEMORY_MAPPED_IO_PORT_SPACE:
+ case GRUB_EFI_PAL_CODE:
+ hook (desc->physical_start, desc->num_pages * 4096,
+ GRUB_MEMORY_RESERVED, hook_data);
+ break;
+
+ case GRUB_EFI_LOADER_CODE:
+ case GRUB_EFI_LOADER_DATA:
+ case GRUB_EFI_CONVENTIONAL_MEMORY:
+ hook (desc->physical_start, desc->num_pages * 4096,
+ GRUB_MEMORY_AVAILABLE, hook_data);
+ break;
+
+ case GRUB_EFI_ACPI_RECLAIM_MEMORY:
+ hook (desc->physical_start, desc->num_pages * 4096,
+ GRUB_MEMORY_ACPI, hook_data);
+ break;
+
+ case GRUB_EFI_ACPI_MEMORY_NVS:
+ hook (desc->physical_start, desc->num_pages * 4096,
+ GRUB_MEMORY_NVS, hook_data);
+ break;
+
+ case GRUB_EFI_PERSISTENT_MEMORY:
+ hook (desc->physical_start, desc->num_pages * 4096,
+ GRUB_MEMORY_PERSISTENT, hook_data);
+ break;
+
+ default:
+ grub_printf ("Unknown memory type %d, considering reserved\n",
+ desc->type);
+ hook (desc->physical_start, desc->num_pages * 4096,
+ GRUB_MEMORY_RESERVED, hook_data);
+ break;
+ }
+ }
+
+ return GRUB_ERR_NONE;
+}
+
+grub_err_t
+grub_machine_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
+{
+ return grub_efi_mmap_iterate (hook, hook_data, 0);
+}
+
+static inline grub_efi_memory_type_t
+make_efi_memtype (int type)
+{
+ switch (type)
+ {
+ case GRUB_MEMORY_CODE:
+ return GRUB_EFI_RUNTIME_SERVICES_CODE;
+
+ /* No way to remove a chunk of memory from EFI mmap.
+ So mark it as unusable. */
+ case GRUB_MEMORY_HOLE:
+ /*
+ * AllocatePages() does not support GRUB_EFI_PERSISTENT_MEMORY,
+ * so no translation for GRUB_MEMORY_PERSISTENT or
+ * GRUB_MEMORY_PERSISTENT_LEGACY.
+ */
+ case GRUB_MEMORY_PERSISTENT:
+ case GRUB_MEMORY_PERSISTENT_LEGACY:
+ case GRUB_MEMORY_RESERVED:
+ return GRUB_EFI_UNUSABLE_MEMORY;
+
+ case GRUB_MEMORY_AVAILABLE:
+ return GRUB_EFI_CONVENTIONAL_MEMORY;
+
+ case GRUB_MEMORY_ACPI:
+ return GRUB_EFI_ACPI_RECLAIM_MEMORY;
+
+ case GRUB_MEMORY_NVS:
+ return GRUB_EFI_ACPI_MEMORY_NVS;
+ }
+
+ return GRUB_EFI_UNUSABLE_MEMORY;
+}
+
+struct overlay
+{
+ struct overlay *next;
+ grub_efi_physical_address_t address;
+ grub_efi_uintn_t pages;
+ int handle;
+};
+
+static struct overlay *overlays = 0;
+static int curhandle = 1;
+
+int
+grub_mmap_register (grub_uint64_t start, grub_uint64_t size, int type)
+{
+ grub_uint64_t end = start + size;
+ grub_efi_physical_address_t address;
+ grub_efi_boot_services_t *b;
+ grub_efi_uintn_t pages;
+ grub_efi_status_t status;
+ struct overlay *curover;
+
+ curover = (struct overlay *) grub_malloc (sizeof (struct overlay));
+ if (! curover)
+ return 0;
+
+ b = grub_efi_system_table->boot_services;
+ address = start & (~0xfffULL);
+ pages = (end - address + 0xfff) >> 12;
+ status = efi_call_2 (b->free_pages, address, pages);
+ if (status != GRUB_EFI_SUCCESS && status != GRUB_EFI_NOT_FOUND)
+ {
+ grub_free (curover);
+ return 0;
+ }
+ status = efi_call_4 (b->allocate_pages, GRUB_EFI_ALLOCATE_ADDRESS,
+ make_efi_memtype (type), pages, &address);
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ grub_free (curover);
+ return 0;
+ }
+ curover->next = overlays;
+ curover->handle = curhandle++;
+ curover->address = address;
+ curover->pages = pages;
+ overlays = curover;
+
+ return curover->handle;
+}
+
+grub_err_t
+grub_mmap_unregister (int handle)
+{
+ struct overlay *curover, *prevover;
+ grub_efi_boot_services_t *b;
+
+ b = grub_efi_system_table->boot_services;
+
+
+ for (curover = overlays, prevover = 0; curover;
+ prevover = curover, curover = curover->next)
+ {
+ if (curover->handle == handle)
+ {
+ efi_call_2 (b->free_pages, curover->address, curover->pages);
+ if (prevover != 0)
+ prevover->next = curover->next;
+ else
+ overlays = curover->next;
+ grub_free (curover);
+ return GRUB_ERR_NONE;
+ }
+ }
+ return grub_error (GRUB_ERR_BUG, "handle %d not found", handle);
+}
+
+/* Result is always page-aligned. */
+void *
+grub_mmap_malign_and_register (grub_uint64_t align __attribute__ ((unused)),
+ grub_uint64_t size,
+ int *handle, int type,
+ int flags __attribute__ ((unused)))
+{
+ grub_efi_physical_address_t address;
+ grub_efi_boot_services_t *b;
+ grub_efi_uintn_t pages;
+ grub_efi_status_t status;
+ struct overlay *curover;
+ grub_efi_allocate_type_t atype;
+
+ curover = (struct overlay *) grub_malloc (sizeof (struct overlay));
+ if (! curover)
+ return 0;
+
+ b = grub_efi_system_table->boot_services;
+
+ address = 0xffffffff;
+
+#if GRUB_TARGET_SIZEOF_VOID_P < 8
+ /* Limit the memory access to less than 4GB for 32-bit platforms. */
+ atype = GRUB_EFI_ALLOCATE_MAX_ADDRESS;
+#else
+ atype = GRUB_EFI_ALLOCATE_ANY_PAGES;
+#endif
+
+ pages = (size + 0xfff) >> 12;
+ status = efi_call_4 (b->allocate_pages, atype,
+ make_efi_memtype (type), pages, &address);
+ if (status != GRUB_EFI_SUCCESS)
+ {
+ grub_free (curover);
+ return 0;
+ }
+
+ if (address == 0)
+ {
+ /* Uggh, the address 0 was allocated... This is too annoying,
+ so reallocate another one. */
+ address = 0xffffffff;
+ status = efi_call_4 (b->allocate_pages, atype,
+ make_efi_memtype (type), pages, &address);
+ grub_efi_free_pages (0, pages);
+ if (status != GRUB_EFI_SUCCESS)
+ return 0;
+ }
+
+ curover->next = overlays;
+ curover->handle = curhandle++;
+ curover->address = address;
+ curover->pages = pages;
+ overlays = curover;
+ *handle = curover->handle;
+
+ return (void *) (grub_addr_t) curover->address;
+}
+
+void
+grub_mmap_free_and_unregister (int handle)
+{
+ grub_mmap_unregister (handle);
+}
diff --git a/grub-core/mmap/i386/mmap.c b/grub-core/mmap/i386/mmap.c
new file mode 100644
index 0000000..ac45f70
--- /dev/null
+++ b/grub-core/mmap/i386/mmap.c
@@ -0,0 +1,113 @@
+/* Mmap management. */
+/*
+ * 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/>.
+ */
+
+#include <grub/machine/memory.h>
+#include <grub/i386/memory.h>
+#include <grub/memory.h>
+#include <grub/err.h>
+#include <grub/misc.h>
+#include <grub/mm.h>
+
+
+#ifndef GRUB_MMAP_REGISTER_BY_FIRMWARE
+
+/* Context for grub_mmap_malign_and_register. */
+struct grub_mmap_malign_and_register_ctx
+{
+ grub_uint64_t align, size, highestlow;
+};
+
+/* Helper for grub_mmap_malign_and_register. */
+static int
+find_hook (grub_uint64_t start, grub_uint64_t rangesize,
+ grub_memory_type_t memtype, void *data)
+{
+ struct grub_mmap_malign_and_register_ctx *ctx = data;
+ grub_uint64_t end = start + rangesize;
+
+ if (memtype != GRUB_MEMORY_AVAILABLE)
+ return 0;
+ if (end > 0x100000)
+ end = 0x100000;
+ if (end > start + ctx->size
+ && ctx->highestlow < ((end - ctx->size)
+ - ((end - ctx->size) & (ctx->align - 1))))
+ ctx->highestlow = (end - ctx->size)
+ - ((end - ctx->size) & (ctx->align - 1));
+ return 0;
+}
+
+void *
+grub_mmap_malign_and_register (grub_uint64_t align, grub_uint64_t size,
+ int *handle, int type, int flags)
+{
+ struct grub_mmap_malign_and_register_ctx ctx = {
+ .align = align,
+ .size = size,
+ .highestlow = 0
+ };
+
+ void *ret;
+ if (flags & GRUB_MMAP_MALLOC_LOW)
+ {
+ /* FIXME: use low-memory mm allocation once it's available. */
+ grub_mmap_iterate (find_hook, &ctx);
+ ret = (void *) (grub_addr_t) ctx.highestlow;
+ }
+ else
+ ret = grub_memalign (align, size);
+
+ if (! ret)
+ {
+ *handle = 0;
+ return 0;
+ }
+
+ *handle = grub_mmap_register ((grub_addr_t) ret, size, type);
+ if (! *handle)
+ {
+ grub_free (ret);
+ return 0;
+ }
+
+ return ret;
+}
+
+void
+grub_mmap_free_and_unregister (int handle)
+{
+ struct grub_mmap_region *cur;
+ grub_uint64_t addr;
+
+ for (cur = grub_mmap_overlays; cur; cur = cur->next)
+ if (cur->handle == handle)
+ break;
+
+ if (! cur)
+ return;
+
+ addr = cur->start;
+
+ grub_mmap_unregister (handle);
+
+ if (addr >= 0x100000)
+ grub_free ((void *) (grub_addr_t) addr);
+}
+
+#endif
diff --git a/grub-core/mmap/i386/pc/mmap.c b/grub-core/mmap/i386/pc/mmap.c
new file mode 100644
index 0000000..6ab4f67
--- /dev/null
+++ b/grub-core/mmap/i386/pc/mmap.c
@@ -0,0 +1,211 @@
+/* Mmap management. */
+/*
+ * 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/>.
+ */
+
+#include <grub/machine/memory.h>
+#include <grub/memory.h>
+#include <grub/misc.h>
+#include <grub/term.h>
+#include <grub/loader.h>
+
+#define min(a,b) (((a) < (b)) ? (a) : (b))
+
+static void *hooktarget = 0;
+
+extern grub_uint8_t grub_machine_mmaphook_start;
+extern grub_uint8_t grub_machine_mmaphook_end;
+extern grub_uint8_t grub_machine_mmaphook_int12;
+extern grub_uint8_t grub_machine_mmaphook_int15;
+
+static grub_uint16_t grub_machine_mmaphook_int12offset = 0;
+static grub_uint16_t grub_machine_mmaphook_int12segment = 0;
+extern grub_uint16_t grub_machine_mmaphook_int15offset;
+extern grub_uint16_t grub_machine_mmaphook_int15segment;
+
+extern grub_uint16_t grub_machine_mmaphook_mmap_num;
+extern grub_uint16_t grub_machine_mmaphook_kblow;
+extern grub_uint16_t grub_machine_mmaphook_kbin16mb;
+extern grub_uint16_t grub_machine_mmaphook_64kbin4gb;
+
+/* Helper for preboot. */
+static int fill_hook (grub_uint64_t addr, grub_uint64_t size,
+ grub_memory_type_t type, void *data)
+{
+ struct grub_e820_mmap_entry **hookmmapcur = data;
+ grub_dprintf ("mmap", "mmap chunk %llx-%llx:%x\n", addr, addr + size, type);
+ (*hookmmapcur)->addr = addr;
+ (*hookmmapcur)->len = size;
+ (*hookmmapcur)->type = type;
+ (*hookmmapcur)++;
+ return 0;
+}
+
+static grub_err_t
+preboot (int noreturn __attribute__ ((unused)))
+{
+ struct grub_e820_mmap_entry *hookmmap, *hookmmapcur;
+
+ if (! hooktarget)
+ return grub_error (GRUB_ERR_OUT_OF_MEMORY,
+ "no space is allocated for memory hook");
+
+ grub_dprintf ("mmap", "installing preboot handlers\n");
+
+ hookmmapcur = hookmmap = (struct grub_e820_mmap_entry *)
+ ((grub_uint8_t *) hooktarget + (&grub_machine_mmaphook_end
+ - &grub_machine_mmaphook_start));
+
+ grub_mmap_iterate (fill_hook, &hookmmapcur);
+ grub_machine_mmaphook_mmap_num = hookmmapcur - hookmmap;
+
+ grub_machine_mmaphook_kblow = grub_mmap_get_lower () >> 10;
+ grub_machine_mmaphook_kbin16mb
+ = min (grub_mmap_get_upper (),0x3f00000ULL) >> 10;
+ grub_machine_mmaphook_64kbin4gb
+ = min (grub_mmap_get_post64 (), 0xfc000000ULL) >> 16;
+
+ /* Correct BDA. */
+ *((grub_uint16_t *) 0x413) = grub_mmap_get_lower () >> 10;
+
+ /* Save old interrupt handlers. */
+ grub_machine_mmaphook_int12offset = *((grub_uint16_t *) 0x48);
+ grub_machine_mmaphook_int12segment = *((grub_uint16_t *) 0x4a);
+ grub_machine_mmaphook_int15offset = *((grub_uint16_t *) 0x54);
+ grub_machine_mmaphook_int15segment = *((grub_uint16_t *) 0x56);
+
+ grub_dprintf ("mmap", "hooktarget = %p\n", hooktarget);
+
+ /* Install the interrupt handlers. */
+ grub_memcpy (hooktarget, &grub_machine_mmaphook_start,
+ &grub_machine_mmaphook_end - &grub_machine_mmaphook_start);
+
+ *((grub_uint16_t *) 0x4a) = ((grub_addr_t) hooktarget) >> 4;
+ *((grub_uint16_t *) 0x56) = ((grub_addr_t) hooktarget) >> 4;
+ *((grub_uint16_t *) 0x48) = &grub_machine_mmaphook_int12
+ - &grub_machine_mmaphook_start;
+ *((grub_uint16_t *) 0x54) = &grub_machine_mmaphook_int15
+ - &grub_machine_mmaphook_start;
+
+ return GRUB_ERR_NONE;
+}
+
+static grub_err_t
+preboot_rest (void)
+{
+ /* Restore old interrupt handlers. */
+ *((grub_uint16_t *) 0x48) = grub_machine_mmaphook_int12offset;
+ *((grub_uint16_t *) 0x4a) = grub_machine_mmaphook_int12segment;
+ *((grub_uint16_t *) 0x54) = grub_machine_mmaphook_int15offset;
+ *((grub_uint16_t *) 0x56) = grub_machine_mmaphook_int15segment;
+
+ return GRUB_ERR_NONE;
+}
+
+/* Helper for malloc_hook. */
+static int
+count_hook (grub_uint64_t addr __attribute__ ((unused)),
+ grub_uint64_t size __attribute__ ((unused)),
+ grub_memory_type_t type __attribute__ ((unused)), void *data)
+{
+ int *regcount = data;
+ (*regcount)++;
+ return 0;
+}
+
+static grub_err_t
+malloc_hook (void)
+{
+ static int reentry = 0;
+ static int mmapregion = 0;
+ static int slots_available = 0;
+ int hooksize;
+ int regcount = 0;
+
+ if (reentry)
+ return GRUB_ERR_NONE;
+
+ grub_dprintf ("mmap", "registering\n");
+
+ grub_mmap_iterate (count_hook, &regcount);
+
+ /* Mapping hook itself may introduce up to 2 additional regions. */
+ regcount += 2;
+
+ if (regcount <= slots_available)
+ return GRUB_ERR_NONE;
+
+ if (mmapregion)
+ {
+ grub_mmap_free_and_unregister (mmapregion);
+ mmapregion = 0;
+ hooktarget = 0;
+ }
+
+ hooksize = &grub_machine_mmaphook_end - &grub_machine_mmaphook_start
+ + regcount * sizeof (struct grub_e820_mmap_entry);
+ /* Allocate an integer number of KiB. */
+ hooksize = ((hooksize - 1) | 0x3ff) + 1;
+ slots_available = (hooksize - (&grub_machine_mmaphook_end
+ - &grub_machine_mmaphook_start))
+ / sizeof (struct grub_e820_mmap_entry);
+
+ reentry = 1;
+ hooktarget
+ = grub_mmap_malign_and_register (16, ALIGN_UP (hooksize, 16), &mmapregion,
+ GRUB_MEMORY_RESERVED,
+ GRUB_MMAP_MALLOC_LOW);
+ reentry = 0;
+
+ if (! hooktarget)
+ {
+ slots_available = 0;
+ return grub_error (GRUB_ERR_OUT_OF_MEMORY, "no space for mmap hook");
+ }
+ return GRUB_ERR_NONE;
+}
+
+grub_err_t
+grub_machine_mmap_register (grub_uint64_t start __attribute__ ((unused)),
+ grub_uint64_t size __attribute__ ((unused)),
+ int type __attribute__ ((unused)),
+ int handle __attribute__ ((unused)))
+{
+ grub_err_t err;
+ static struct grub_preboot *preb_handle = 0;
+
+ err = malloc_hook ();
+ if (err)
+ return err;
+
+ if (! preb_handle)
+ {
+ grub_dprintf ("mmap", "adding preboot\n");
+ preb_handle
+ = grub_loader_register_preboot_hook (preboot, preboot_rest,
+ GRUB_LOADER_PREBOOT_HOOK_PRIO_MEMORY);
+ if (! preb_handle)
+ return grub_errno;
+ }
+ return GRUB_ERR_NONE;
+}
+
+grub_err_t
+grub_machine_mmap_unregister (int handle __attribute__ ((unused)))
+{
+ return GRUB_ERR_NONE;
+}
diff --git a/grub-core/mmap/i386/pc/mmap_helper.S b/grub-core/mmap/i386/pc/mmap_helper.S
new file mode 100644
index 0000000..8e251a1
--- /dev/null
+++ b/grub-core/mmap/i386/pc/mmap_helper.S
@@ -0,0 +1,163 @@
+/* Mmap management. */
+/*
+ * 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/>.
+ */
+
+#include <grub/symbol.h>
+
+#define DS(x) ((x) - LOCAL (segstart))
+
+LOCAL (segstart):
+VARIABLE(grub_machine_mmaphook_start)
+ .code16
+VARIABLE(grub_machine_mmaphook_int12)
+ push %ds
+ push %cs
+ pop %ds
+#ifdef __APPLE__
+ LOCAL(kblow_offset) = DS (LOCAL (kblow))
+ movw LOCAL(kblow_offset), %ax
+#else
+ movw DS (LOCAL (kblow)), %ax
+#endif
+ pop %ds
+ iret
+
+VARIABLE(grub_machine_mmaphook_int15)
+ pushf
+ cmpw $0xe801, %ax
+ jz LOCAL (e801)
+ cmpw $0xe820, %ax
+ jz LOCAL (e820)
+ cmpb $0x88, %ah
+ jz LOCAL (h88)
+ popf
+ /* ljmp */
+ .byte 0xea
+VARIABLE (grub_machine_mmaphook_int15offset)
+ .word 0
+VARIABLE (grub_machine_mmaphook_int15segment)
+ .word 0
+
+LOCAL (e801):
+ popf
+ push %ds
+ push %cs
+ pop %ds
+#ifdef __APPLE__
+ LOCAL(kbin16mb_offset) = DS (LOCAL (kbin16mb))
+ LOCAL(m64kbin4gb_offset) = DS (LOCAL (m64kbin4gb))
+ movw LOCAL(kbin16mb_offset), %ax
+ movw LOCAL(m64kbin4gb_offset), %bx
+#else
+ movw DS (LOCAL (kbin16mb)), %ax
+ movw DS (LOCAL (m64kbin4gb)), %bx
+#endif
+ movw %ax, %cx
+ movw %bx, %dx
+ pop %ds
+ clc
+ jmp LOCAL (iret_cf)
+
+LOCAL (h88):
+ popf
+ push %ds
+ push %cs
+ pop %ds
+#ifdef __APPLE__
+ movw LOCAL(kbin16mb_offset), %ax
+#else
+ movw DS (LOCAL (kbin16mb)), %ax
+#endif
+ pop %ds
+ clc
+ jmp LOCAL (iret_cf)
+
+LOCAL (e820):
+ popf
+ push %ds
+ push %cs
+ pop %ds
+ cmpw $20, %cx
+ jb LOCAL (errexit)
+#ifdef __APPLE__
+ LOCAL(mmap_num_offset) = DS (LOCAL (mmap_num))
+ cmpw LOCAL(mmap_num_offset), %bx
+#else
+ cmpw DS (LOCAL (mmap_num)), %bx
+#endif
+ jae LOCAL (errexit)
+ cmp $0x534d4150, %edx
+ jne LOCAL (errexit)
+ push %si
+ push %di
+ movw $20, %cx
+#ifdef __APPLE__
+ LOCAL(mmaphook_map_offset) = DS(LOCAL (mmaphook_mmap))
+ movw $LOCAL(mmaphook_map_offset), %si
+#else
+ movw $(DS(LOCAL (mmaphook_mmap))), %si
+#endif
+ mov %bx, %ax
+ imul $20, %ax
+ add %ax, %si
+ rep movsb
+ pop %di
+ pop %si
+ movl $20, %ecx
+ inc %bx
+#ifdef __APPLE__
+ cmpw LOCAL(mmap_num_offset), %bx
+#else
+ cmpw DS(LOCAL (mmap_num)), %bx
+#endif
+ jb LOCAL (noclean)
+ xor %bx, %bx
+LOCAL (noclean):
+ mov $0x534d4150, %eax
+ pop %ds
+ clc
+ jmp LOCAL (iret_cf)
+LOCAL (errexit):
+ mov $0x534d4150, %eax
+ pop %ds
+ xor %bx, %bx
+ stc
+
+LOCAL (iret_cf):
+ push %bp
+ mov %sp, %bp
+ setc 6(%bp)
+ pop %bp
+ iret
+
+VARIABLE(grub_machine_mmaphook_mmap_num)
+LOCAL (mmap_num):
+ .word 0
+VARIABLE(grub_machine_mmaphook_kblow)
+LOCAL (kblow):
+ .word 0
+VARIABLE (grub_machine_mmaphook_kbin16mb)
+LOCAL (kbin16mb):
+ .word 0
+VARIABLE (grub_machine_mmaphook_64kbin4gb)
+LOCAL (m64kbin4gb):
+ .word 0
+LOCAL (mmaphook_mmap):
+ /* Memory map is placed just after the interrupt handlers. */
+VARIABLE(grub_machine_mmaphook_end)
+ .byte 0
diff --git a/grub-core/mmap/i386/uppermem.c b/grub-core/mmap/i386/uppermem.c
new file mode 100644
index 0000000..a6be989
--- /dev/null
+++ b/grub-core/mmap/i386/uppermem.c
@@ -0,0 +1,98 @@
+/* Compute amount of lower and upper memory till the first hole. */
+/*
+ * 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/>.
+ */
+
+#include <grub/memory.h>
+#include <grub/i386/memory.h>
+#include <grub/mm.h>
+#include <grub/misc.h>
+
+/* Helper for grub_mmap_get_lower. */
+static int
+lower_hook (grub_uint64_t addr, grub_uint64_t size, grub_memory_type_t type,
+ void *data)
+{
+ grub_uint64_t *lower = data;
+
+ if (type != GRUB_MEMORY_AVAILABLE)
+ return 0;
+#ifdef GRUB_MACHINE_COREBOOT
+ if (addr <= 0x1000)
+#else
+ if (addr == 0)
+#endif
+ *lower = size + addr;
+ return 0;
+}
+
+grub_uint64_t
+grub_mmap_get_lower (void)
+{
+ grub_uint64_t lower = 0;
+
+ grub_mmap_iterate (lower_hook, &lower);
+ if (lower > 0x100000)
+ lower = 0x100000;
+ return lower;
+}
+
+/* Helper for grub_mmap_get_upper. */
+static int
+upper_hook (grub_uint64_t addr, grub_uint64_t size, grub_memory_type_t type,
+ void *data)
+{
+ grub_uint64_t *upper = data;
+
+ if (type != GRUB_MEMORY_AVAILABLE)
+ return 0;
+ if (addr <= 0x100000 && addr + size > 0x100000)
+ *upper = addr + size - 0x100000;
+ return 0;
+}
+
+grub_uint64_t
+grub_mmap_get_upper (void)
+{
+ grub_uint64_t upper = 0;
+
+ grub_mmap_iterate (upper_hook, &upper);
+ return upper;
+}
+
+/* Helper for grub_mmap_get_post64. */
+static int
+post64_hook (grub_uint64_t addr, grub_uint64_t size, grub_memory_type_t type,
+ void *data)
+{
+ grub_uint64_t *post64 = data;
+ if (type != GRUB_MEMORY_AVAILABLE)
+ return 0;
+ if (addr <= 0x4000000 && addr + size > 0x4000000)
+ *post64 = addr + size - 0x4000000;
+ return 0;
+}
+
+/* Count the continuous bytes after 64 MiB. */
+grub_uint64_t
+grub_mmap_get_post64 (void)
+{
+ grub_uint64_t post64 = 0;
+
+ grub_mmap_iterate (post64_hook, &post64);
+ return post64;
+}
diff --git a/grub-core/mmap/mips/uppermem.c b/grub-core/mmap/mips/uppermem.c
new file mode 100644
index 0000000..a980e07
--- /dev/null
+++ b/grub-core/mmap/mips/uppermem.c
@@ -0,0 +1,72 @@
+/* Compute amount of lower and upper memory till the first hole. */
+/*
+ * 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/>.
+ */
+
+#include <grub/memory.h>
+#include <grub/mm.h>
+#include <grub/misc.h>
+#include <grub/cpu/memory.h>
+
+/* Helper for grub_mmap_get_lower. */
+static int
+lower_hook (grub_uint64_t addr, grub_uint64_t size, grub_memory_type_t type,
+ void *data)
+{
+ grub_uint64_t *lower = data;
+
+ if (type != GRUB_MEMORY_AVAILABLE)
+ return 0;
+ if (addr == 0)
+ *lower = size;
+ return 0;
+}
+
+grub_uint64_t
+grub_mmap_get_lower (void)
+{
+ grub_uint64_t lower = 0;
+
+ grub_mmap_iterate (lower_hook, &lower);
+ if (lower > GRUB_ARCH_LOWMEMMAXSIZE)
+ lower = GRUB_ARCH_LOWMEMMAXSIZE;
+ return lower;
+}
+
+/* Helper for grub_mmap_get_upper. */
+static int
+upper_hook (grub_uint64_t addr, grub_uint64_t size, grub_memory_type_t type,
+ void *data)
+{
+ grub_uint64_t *upper = data;
+
+ if (type != GRUB_MEMORY_AVAILABLE)
+ return 0;
+ if (addr <= GRUB_ARCH_HIGHMEMPSTART && addr + size
+ > GRUB_ARCH_HIGHMEMPSTART)
+ *upper = addr + size - GRUB_ARCH_HIGHMEMPSTART;
+ return 0;
+}
+
+grub_uint64_t
+grub_mmap_get_upper (void)
+{
+ grub_uint64_t upper = 0;
+
+ grub_mmap_iterate (upper_hook, &upper);
+ return upper;
+}
diff --git a/grub-core/mmap/mmap.c b/grub-core/mmap/mmap.c
new file mode 100644
index 0000000..c8c8312
--- /dev/null
+++ b/grub-core/mmap/mmap.c
@@ -0,0 +1,554 @@
+/* Mmap management. */
+/*
+ * 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/>.
+ */
+
+#include <grub/memory.h>
+#include <grub/machine/memory.h>
+#include <grub/err.h>
+#include <grub/lockdown.h>
+#include <grub/misc.h>
+#include <grub/mm.h>
+#include <grub/command.h>
+#include <grub/dl.h>
+#include <grub/i18n.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+#ifndef GRUB_MMAP_REGISTER_BY_FIRMWARE
+
+struct grub_mmap_region *grub_mmap_overlays = 0;
+static int curhandle = 1;
+
+#endif
+
+static int current_priority = 1;
+
+/* Scanline events. */
+struct grub_mmap_scan
+{
+ /* At which memory address. */
+ grub_uint64_t pos;
+ /* 0 = region starts, 1 = region ends. */
+ int type;
+ /* Which type of memory region? */
+ grub_memory_type_t memtype;
+ /* Priority. 0 means coming from firmware. */
+ int priority;
+};
+
+/* Context for grub_mmap_iterate. */
+struct grub_mmap_iterate_ctx
+{
+ struct grub_mmap_scan *scanline_events;
+ int i;
+};
+
+/* Helper for grub_mmap_iterate. */
+static int
+count_hook (grub_uint64_t addr __attribute__ ((unused)),
+ grub_uint64_t size __attribute__ ((unused)),
+ grub_memory_type_t type __attribute__ ((unused)), void *data)
+{
+ int *mmap_num = data;
+
+ (*mmap_num)++;
+ return 0;
+}
+
+/* Helper for grub_mmap_iterate. */
+static int
+fill_hook (grub_uint64_t addr, grub_uint64_t size, grub_memory_type_t type,
+ void *data)
+{
+ struct grub_mmap_iterate_ctx *ctx = data;
+
+ if (type == GRUB_MEMORY_HOLE)
+ {
+ grub_dprintf ("mmap", "Unknown memory type %d. Assuming unusable\n",
+ type);
+ type = GRUB_MEMORY_RESERVED;
+ }
+
+ ctx->scanline_events[ctx->i].pos = addr;
+ ctx->scanline_events[ctx->i].type = 0;
+ ctx->scanline_events[ctx->i].memtype = type;
+ ctx->scanline_events[ctx->i].priority = 0;
+
+ ctx->i++;
+
+ ctx->scanline_events[ctx->i].pos = addr + size;
+ ctx->scanline_events[ctx->i].type = 1;
+ ctx->scanline_events[ctx->i].memtype = type;
+ ctx->scanline_events[ctx->i].priority = 0;
+ ctx->i++;
+
+ return 0;
+}
+
+struct mm_list
+{
+ struct mm_list *next;
+ grub_memory_type_t val;
+ int present;
+};
+
+grub_err_t
+grub_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
+{
+ /* This function resolves overlapping regions and sorts the memory map.
+ It uses scanline (sweeping) algorithm.
+ */
+ struct grub_mmap_iterate_ctx ctx;
+ int i, done;
+
+ struct grub_mmap_scan t;
+
+ /* Previous scanline event. */
+ grub_uint64_t lastaddr;
+ int lasttype;
+ /* Current scanline event. */
+ int curtype;
+ /* How many regions of given type/priority overlap at current location? */
+ /* Normally there shouldn't be more than one region per priority but be robust. */
+ struct mm_list *present;
+ /* Number of mmap chunks. */
+ int mmap_num;
+
+#ifndef GRUB_MMAP_REGISTER_BY_FIRMWARE
+ struct grub_mmap_region *cur;
+#endif
+
+ mmap_num = 0;
+
+#ifndef GRUB_MMAP_REGISTER_BY_FIRMWARE
+ for (cur = grub_mmap_overlays; cur; cur = cur->next)
+ mmap_num++;
+#endif
+
+ grub_machine_mmap_iterate (count_hook, &mmap_num);
+
+ /* Initialize variables. */
+ ctx.scanline_events = (struct grub_mmap_scan *)
+ grub_calloc (mmap_num, sizeof (struct grub_mmap_scan) * 2);
+
+ present = grub_calloc (current_priority, sizeof (present[0]));
+
+ if (! ctx.scanline_events || !present)
+ {
+ grub_free (ctx.scanline_events);
+ grub_free (present);
+ return grub_errno;
+ }
+
+ ctx.i = 0;
+#ifndef GRUB_MMAP_REGISTER_BY_FIRMWARE
+ /* Register scanline events. */
+ for (cur = grub_mmap_overlays; cur; cur = cur->next)
+ {
+ ctx.scanline_events[ctx.i].pos = cur->start;
+ ctx.scanline_events[ctx.i].type = 0;
+ ctx.scanline_events[ctx.i].memtype = cur->type;
+ ctx.scanline_events[ctx.i].priority = cur->priority;
+ ctx.i++;
+
+ ctx.scanline_events[ctx.i].pos = cur->end;
+ ctx.scanline_events[ctx.i].type = 1;
+ ctx.scanline_events[ctx.i].memtype = cur->type;
+ ctx.scanline_events[ctx.i].priority = cur->priority;
+ ctx.i++;
+ }
+#endif /* ! GRUB_MMAP_REGISTER_BY_FIRMWARE */
+
+ grub_machine_mmap_iterate (fill_hook, &ctx);
+
+ /* 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 (ctx.scanline_events[i + 1].pos < ctx.scanline_events[i].pos
+ || (ctx.scanline_events[i + 1].pos == ctx.scanline_events[i].pos
+ && ctx.scanline_events[i + 1].type == 0
+ && ctx.scanline_events[i].type == 1))
+ {
+ t = ctx.scanline_events[i + 1];
+ ctx.scanline_events[i + 1] = ctx.scanline_events[i];
+ ctx.scanline_events[i] = t;
+ done = 1;
+ }
+ }
+
+ lastaddr = ctx.scanline_events[0].pos;
+ lasttype = ctx.scanline_events[0].memtype;
+ for (i = 0; i < 2 * mmap_num; i++)
+ {
+ /* Process event. */
+ if (ctx.scanline_events[i].type)
+ {
+ if (present[ctx.scanline_events[i].priority].present)
+ {
+ if (present[ctx.scanline_events[i].priority].val == ctx.scanline_events[i].memtype)
+ {
+ if (present[ctx.scanline_events[i].priority].next)
+ {
+ struct mm_list *p = present[ctx.scanline_events[i].priority].next;
+ present[ctx.scanline_events[i].priority] = *p;
+ grub_free (p);
+ }
+ else
+ {
+ present[ctx.scanline_events[i].priority].present = 0;
+ }
+ }
+ else
+ {
+ struct mm_list **q = &(present[ctx.scanline_events[i].priority].next), *p;
+ for (; *q; q = &((*q)->next))
+ if ((*q)->val == ctx.scanline_events[i].memtype)
+ {
+ p = *q;
+ *q = p->next;
+ grub_free (p);
+ break;
+ }
+ }
+ }
+ }
+ else
+ {
+ if (!present[ctx.scanline_events[i].priority].present)
+ {
+ present[ctx.scanline_events[i].priority].present = 1;
+ present[ctx.scanline_events[i].priority].val = ctx.scanline_events[i].memtype;
+ }
+ else
+ {
+ struct mm_list *n = grub_malloc (sizeof (*n));
+ n->val = ctx.scanline_events[i].memtype;
+ n->present = 1;
+ n->next = present[ctx.scanline_events[i].priority].next;
+ present[ctx.scanline_events[i].priority].next = n;
+ }
+ }
+
+ /* Determine current region type. */
+ curtype = -1;
+ {
+ int k;
+ for (k = current_priority - 1; k >= 0; k--)
+ if (present[k].present)
+ {
+ curtype = present[k].val;
+ break;
+ }
+ }
+
+ /* Announce region to the hook if necessary. */
+ if ((curtype == -1 || curtype != lasttype)
+ && lastaddr != ctx.scanline_events[i].pos
+ && lasttype != -1
+ && lasttype != GRUB_MEMORY_HOLE
+ && hook (lastaddr, ctx.scanline_events[i].pos - lastaddr, lasttype,
+ hook_data))
+ {
+ grub_free (ctx.scanline_events);
+ grub_free (present);
+ return GRUB_ERR_NONE;
+ }
+
+ /* Update last values if necessary. */
+ if (curtype == -1 || curtype != lasttype)
+ {
+ lasttype = curtype;
+ lastaddr = ctx.scanline_events[i].pos;
+ }
+ }
+
+ grub_free (ctx.scanline_events);
+ grub_free (present);
+ return GRUB_ERR_NONE;
+}
+
+#ifndef GRUB_MMAP_REGISTER_BY_FIRMWARE
+int
+grub_mmap_register (grub_uint64_t start, grub_uint64_t size, int type)
+{
+ struct grub_mmap_region *cur;
+
+ grub_dprintf ("mmap", "registering\n");
+
+ cur = (struct grub_mmap_region *)
+ grub_malloc (sizeof (struct grub_mmap_region));
+ if (! cur)
+ return 0;
+
+ cur->next = grub_mmap_overlays;
+ cur->start = start;
+ cur->end = start + size;
+ cur->type = type;
+ cur->handle = curhandle++;
+ cur->priority = current_priority++;
+ grub_mmap_overlays = cur;
+
+ if (grub_machine_mmap_register (start, size, type, curhandle))
+ {
+ grub_mmap_overlays = cur->next;
+ grub_free (cur);
+ return 0;
+ }
+
+ return cur->handle;
+}
+
+grub_err_t
+grub_mmap_unregister (int handle)
+{
+ struct grub_mmap_region *cur, *prev;
+
+ for (cur = grub_mmap_overlays, prev = 0; cur; prev = cur, cur = cur->next)
+ if (handle == cur->handle)
+ {
+ grub_err_t err;
+ err = grub_machine_mmap_unregister (handle);
+ if (err)
+ return err;
+
+ if (prev)
+ prev->next = cur->next;
+ else
+ grub_mmap_overlays = cur->next;
+ grub_free (cur);
+ return GRUB_ERR_NONE;
+ }
+ return grub_error (GRUB_ERR_BUG, "mmap overlay not found");
+}
+
+#endif /* ! GRUB_MMAP_REGISTER_BY_FIRMWARE */
+
+#define CHUNK_SIZE 0x400
+
+struct badram_entry {
+ grub_uint64_t addr, mask;
+};
+
+static inline grub_uint64_t
+fill_mask (struct badram_entry *entry, grub_uint64_t iterator)
+{
+ int i, j;
+ grub_uint64_t ret = (entry->addr & entry->mask);
+
+ /* Find first fixed bit. */
+ for (i = 0; i < 64; i++)
+ if ((entry->mask & (1ULL << i)) != 0)
+ break;
+ j = 0;
+ for (; i < 64; i++)
+ if ((entry->mask & (1ULL << i)) == 0)
+ {
+ if ((iterator & (1ULL << j)) != 0)
+ ret |= 1ULL << i;
+ j++;
+ }
+ return ret;
+}
+
+/* Helper for grub_cmd_badram. */
+static int
+badram_iter (grub_uint64_t addr, grub_uint64_t size,
+ grub_memory_type_t type __attribute__ ((unused)), void *data)
+{
+ struct badram_entry *entry = data;
+ grub_uint64_t iterator, low, high, cur;
+ int tail, var;
+ int i;
+ grub_dprintf ("badram", "hook %llx+%llx\n", (unsigned long long) addr,
+ (unsigned long long) size);
+
+ /* How many trailing zeros? */
+ for (tail = 0; ! (entry->mask & (1ULL << tail)); tail++);
+
+ /* How many zeros in mask? */
+ var = 0;
+ for (i = 0; i < 64; i++)
+ if (! (entry->mask & (1ULL << i)))
+ var++;
+
+ if (fill_mask (entry, 0) >= addr)
+ iterator = 0;
+ else
+ {
+ low = 0;
+ high = ~0ULL;
+ /* Find starting value. Keep low and high such that
+ fill_mask (low) < addr and fill_mask (high) >= addr;
+ */
+ while (high - low > 1)
+ {
+ cur = (low + high) / 2;
+ if (fill_mask (entry, cur) >= addr)
+ high = cur;
+ else
+ low = cur;
+ }
+ iterator = high;
+ }
+
+ for (; iterator < (1ULL << (var - tail))
+ && (cur = fill_mask (entry, iterator)) < addr + size;
+ iterator++)
+ {
+ grub_dprintf ("badram", "%llx (size %llx) is a badram range\n",
+ (unsigned long long) cur, (1ULL << tail));
+ grub_mmap_register (cur, (1ULL << tail), GRUB_MEMORY_HOLE);
+ }
+ return 0;
+}
+
+static grub_err_t
+grub_cmd_badram (grub_command_t cmd __attribute__ ((unused)),
+ int argc, char **args)
+{
+ const char *str;
+ struct badram_entry entry;
+
+ if (argc != 1)
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));
+
+ grub_dprintf ("badram", "executing badram\n");
+
+ str = args[0];
+
+ while (1)
+ {
+ /* Parse address and mask. */
+ entry.addr = grub_strtoull (str, &str, 16);
+ if (*str == ',')
+ str++;
+ entry.mask = grub_strtoull (str, &str, 16);
+ if (*str == ',')
+ str++;
+
+ if (grub_errno == GRUB_ERR_BAD_NUMBER)
+ {
+ grub_errno = 0;
+ return GRUB_ERR_NONE;
+ }
+
+ /* When part of a page is tainted, we discard the whole of it. There's
+ no point in providing sub-page chunks. */
+ entry.mask &= ~(CHUNK_SIZE - 1);
+
+ grub_dprintf ("badram", "badram %llx:%llx\n",
+ (unsigned long long) entry.addr,
+ (unsigned long long) entry.mask);
+
+ grub_mmap_iterate (badram_iter, &entry);
+ }
+}
+
+static grub_uint64_t
+parsemem (const char *str)
+{
+ grub_uint64_t ret;
+ const char *ptr;
+
+ ret = grub_strtoul (str, &ptr, 0);
+
+ switch (*ptr)
+ {
+ case 'K':
+ return ret << 10;
+ case 'M':
+ return ret << 20;
+ case 'G':
+ return ret << 30;
+ case 'T':
+ return ret << 40;
+ }
+ return ret;
+}
+
+struct cutmem_range {
+ grub_uint64_t from, to;
+};
+
+/* Helper for grub_cmd_cutmem. */
+static int
+cutmem_iter (grub_uint64_t addr, grub_uint64_t size,
+ grub_memory_type_t type __attribute__ ((unused)), void *data)
+{
+ struct cutmem_range *range = data;
+ grub_uint64_t end = addr + size;
+
+ if (addr <= range->from)
+ addr = range->from;
+ if (end >= range->to)
+ end = range->to;
+
+ if (end <= addr)
+ return 0;
+
+ grub_mmap_register (addr, end - addr, GRUB_MEMORY_HOLE);
+ return 0;
+}
+
+static grub_err_t
+grub_cmd_cutmem (grub_command_t cmd __attribute__ ((unused)),
+ int argc, char **args)
+{
+ struct cutmem_range range;
+
+ if (argc != 2)
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("two arguments expected"));
+
+ range.from = parsemem (args[0]);
+ if (grub_errno)
+ return grub_errno;
+
+ range.to = parsemem (args[1]);
+ if (grub_errno)
+ return grub_errno;
+
+ grub_mmap_iterate (cutmem_iter, &range);
+
+ return GRUB_ERR_NONE;
+}
+
+static grub_command_t cmd, cmd_cut;
+
+
+GRUB_MOD_INIT(mmap)
+{
+ cmd = grub_register_command_lockdown ("badram", grub_cmd_badram,
+ N_("ADDR1,MASK1[,ADDR2,MASK2[,...]]"),
+ N_("Declare memory regions as faulty (badram)."));
+ cmd_cut = grub_register_command_lockdown ("cutmem", grub_cmd_cutmem,
+ N_("FROM[K|M|G] TO[K|M|G]"),
+ N_("Remove any memory regions in specified range."));
+
+}
+
+GRUB_MOD_FINI(mmap)
+{
+ grub_unregister_command (cmd);
+ grub_unregister_command (cmd_cut);
+}
+