summaryrefslogtreecommitdiffstats
path: root/grub-core/kern/i386/qemu
diff options
context:
space:
mode:
Diffstat (limited to 'grub-core/kern/i386/qemu')
-rw-r--r--grub-core/kern/i386/qemu/init.c276
-rw-r--r--grub-core/kern/i386/qemu/mmap.c107
-rw-r--r--grub-core/kern/i386/qemu/startup.S75
3 files changed, 458 insertions, 0 deletions
diff --git a/grub-core/kern/i386/qemu/init.c b/grub-core/kern/i386/qemu/init.c
new file mode 100644
index 0000000..271b6fb
--- /dev/null
+++ b/grub-core/kern/i386/qemu/init.c
@@ -0,0 +1,276 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2013 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/kernel.h>
+#include <grub/mm.h>
+#include <grub/machine/time.h>
+#include <grub/machine/memory.h>
+#include <grub/machine/console.h>
+#include <grub/offsets.h>
+#include <grub/types.h>
+#include <grub/err.h>
+#include <grub/dl.h>
+#include <grub/misc.h>
+#include <grub/loader.h>
+#include <grub/env.h>
+#include <grub/cache.h>
+#include <grub/time.h>
+#include <grub/symbol.h>
+#include <grub/cpu/io.h>
+#include <grub/cpu/floppy.h>
+#include <grub/cpu/tsc.h>
+#include <grub/machine/kernel.h>
+#include <grub/pci.h>
+
+extern grub_uint8_t _start[];
+extern grub_uint8_t _end[];
+extern grub_uint8_t _edata[];
+
+void __attribute__ ((noreturn))
+grub_exit (void)
+{
+ /* We can't use grub_fatal() in this function. This would create an infinite
+ loop, since grub_fatal() calls grub_abort() which in turn calls grub_exit(). */
+ while (1)
+ grub_cpu_idle ();
+}
+
+grub_addr_t grub_modbase;
+
+/* Helper for grub_machine_init. */
+static int
+heap_init (grub_uint64_t addr, grub_uint64_t size, grub_memory_type_t type,
+ void *data __attribute__ ((unused)))
+{
+ grub_uint64_t begin = addr, end = addr + size;
+
+#if GRUB_CPU_SIZEOF_VOID_P == 4
+ /* Restrict ourselves to 32-bit memory space. */
+ if (begin > GRUB_ULONG_MAX)
+ return 0;
+ if (end > GRUB_ULONG_MAX)
+ end = GRUB_ULONG_MAX;
+#endif
+
+ if (type != GRUB_MEMORY_AVAILABLE)
+ return 0;
+
+ /* Avoid the lower memory. */
+ if (begin < GRUB_MEMORY_MACHINE_LOWER_SIZE)
+ begin = GRUB_MEMORY_MACHINE_LOWER_SIZE;
+
+ if (end <= begin)
+ return 0;
+
+ grub_mm_init_region ((void *) (grub_addr_t) begin, (grub_size_t) (end - begin));
+
+ return 0;
+}
+
+struct resource
+{
+ grub_pci_device_t dev;
+ grub_size_t size;
+ unsigned type:4;
+ unsigned bar:3;
+};
+
+struct iterator_ctx
+{
+ struct resource *resources;
+ grub_size_t nresources;
+};
+
+/* We don't support bridges, so can't have more than 32 devices. */
+#define MAX_DEVICES 32
+
+static int
+find_resources (grub_pci_device_t dev,
+ grub_pci_id_t pciid __attribute__ ((unused)),
+ void *data)
+{
+ struct iterator_ctx *ctx = data;
+ int bar;
+
+ if (ctx->nresources >= MAX_DEVICES * 6)
+ return 1;
+
+ for (bar = 0; bar < 6; bar++)
+ {
+ grub_pci_address_t addr;
+ grub_uint32_t ones, zeros, mask;
+ struct resource *res;
+ addr = grub_pci_make_address (dev, GRUB_PCI_REG_ADDRESS_REG0
+ + 4 * bar);
+ grub_pci_write (addr, 0xffffffff);
+ grub_pci_read (addr);
+ ones = grub_pci_read (addr);
+ grub_pci_write (addr, 0);
+ grub_pci_read (addr);
+ zeros = grub_pci_read (addr);
+ if (ones == zeros)
+ continue;
+ res = &ctx->resources[ctx->nresources++];
+ if ((zeros & GRUB_PCI_ADDR_SPACE_MASK) == GRUB_PCI_ADDR_SPACE_IO)
+ mask = GRUB_PCI_ADDR_SPACE_MASK;
+ else
+ mask = (GRUB_PCI_ADDR_MEM_TYPE_MASK | GRUB_PCI_ADDR_SPACE_MASK | GRUB_PCI_ADDR_MEM_PREFETCH);
+
+ res->type = ones & mask;
+ res->dev = dev;
+ res->bar = bar;
+ res->size = (~((zeros ^ ones)) | mask) + 1;
+ if ((zeros & (GRUB_PCI_ADDR_MEM_TYPE_MASK | GRUB_PCI_ADDR_SPACE_MASK))
+ == (GRUB_PCI_ADDR_SPACE_MEMORY | GRUB_PCI_ADDR_MEM_TYPE_64))
+ bar++;
+ }
+ return 0;
+}
+
+static int
+enable_cards (grub_pci_device_t dev,
+ grub_pci_id_t pciid __attribute__ ((unused)),
+ void *data __attribute__ ((unused)))
+{
+ grub_uint16_t cmd = 0;
+ grub_pci_address_t addr;
+ grub_uint32_t class;
+ int bar;
+
+ for (bar = 0; bar < 6; bar++)
+ {
+ grub_uint32_t val;
+ addr = grub_pci_make_address (dev, GRUB_PCI_REG_ADDRESS_REG0
+ + 4 * bar);
+ val = grub_pci_read (addr);
+ if (!val)
+ continue;
+ if ((val & GRUB_PCI_ADDR_SPACE_MASK) == GRUB_PCI_ADDR_SPACE_IO)
+ cmd |= GRUB_PCI_COMMAND_IO_ENABLED;
+ else
+ cmd |= GRUB_PCI_COMMAND_MEM_ENABLED;
+ }
+
+ class = (grub_pci_read (addr) >> 16) & 0xffff;
+
+ if (class == GRUB_PCI_CLASS_SUBCLASS_VGA)
+ cmd |= GRUB_PCI_COMMAND_IO_ENABLED
+ | GRUB_PCI_COMMAND_MEM_ENABLED;
+
+ if (class == GRUB_PCI_CLASS_SUBCLASS_USB)
+ return 0;
+
+ addr = grub_pci_make_address (dev, GRUB_PCI_REG_COMMAND);
+ grub_pci_write (addr, cmd);
+
+ return 0;
+}
+
+static void
+grub_pci_assign_addresses (void)
+{
+ struct iterator_ctx ctx;
+
+ {
+ struct resource resources[MAX_DEVICES * 6];
+ int done;
+ unsigned i;
+ ctx.nresources = 0;
+ ctx.resources = resources;
+ grub_uint32_t memptr = 0xf0000000;
+ grub_uint16_t ioptr = 0x1000;
+
+ grub_pci_iterate (find_resources, &ctx);
+ /* FIXME: do we need a better sort here? */
+ do
+ {
+ done = 0;
+ for (i = 0; i + 1 < ctx.nresources; i++)
+ if (resources[i].size < resources[i+1].size)
+ {
+ struct resource t;
+ t = resources[i];
+ resources[i] = resources[i+1];
+ resources[i+1] = t;
+ done = 1;
+ }
+ }
+ while (done);
+
+ for (i = 0; i < ctx.nresources; i++)
+ {
+ grub_pci_address_t addr;
+ addr = grub_pci_make_address (resources[i].dev,
+ GRUB_PCI_REG_ADDRESS_REG0
+ + 4 * resources[i].bar);
+ if ((resources[i].type & GRUB_PCI_ADDR_SPACE_MASK)
+ == GRUB_PCI_ADDR_SPACE_IO)
+ {
+ grub_pci_write (addr, ioptr | resources[i].type);
+ ioptr += resources[i].size;
+ }
+ else
+ {
+ grub_pci_write (addr, memptr | resources[i].type);
+ memptr += resources[i].size;
+ if ((resources[i].type & (GRUB_PCI_ADDR_MEM_TYPE_MASK
+ | GRUB_PCI_ADDR_SPACE_MASK))
+ == (GRUB_PCI_ADDR_SPACE_MEMORY | GRUB_PCI_ADDR_MEM_TYPE_64))
+ {
+ addr = grub_pci_make_address (resources[i].dev,
+ GRUB_PCI_REG_ADDRESS_REG0
+ + 4 * resources[i].bar + 4);
+ grub_pci_write (addr, 0);
+ }
+ }
+ }
+ grub_pci_iterate (enable_cards, NULL);
+ }
+}
+
+void
+grub_machine_init (void)
+{
+ grub_modbase = grub_core_entry_addr + (_edata - _start);
+
+ grub_pci_assign_addresses ();
+
+ grub_qemu_init_cirrus ();
+
+ grub_vga_text_init ();
+
+ grub_machine_mmap_init ();
+ grub_machine_mmap_iterate (heap_init, NULL);
+
+
+ grub_tsc_init ();
+}
+
+void
+grub_machine_get_bootlocation (char **device __attribute__ ((unused)),
+ char **path __attribute__ ((unused)))
+{
+}
+
+void
+grub_machine_fini (int flags)
+{
+ if (flags & GRUB_LOADER_FLAG_NORETURN)
+ grub_vga_text_fini ();
+ grub_stop_floppy ();
+}
diff --git a/grub-core/kern/i386/qemu/mmap.c b/grub-core/kern/i386/qemu/mmap.c
new file mode 100644
index 0000000..f449637
--- /dev/null
+++ b/grub-core/kern/i386/qemu/mmap.c
@@ -0,0 +1,107 @@
+/*
+ * 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/i386/memory.h>
+#include <grub/machine/memory.h>
+#include <grub/machine/boot.h>
+#include <grub/types.h>
+#include <grub/err.h>
+#include <grub/misc.h>
+#include <grub/cmos.h>
+#include <grub/offsets.h>
+
+#define QEMU_CMOS_MEMSIZE_HIGH 0x35
+#define QEMU_CMOS_MEMSIZE_LOW 0x34
+
+#define QEMU_CMOS_MEMSIZE2_HIGH 0x31
+#define QEMU_CMOS_MEMSIZE2_LOW 0x30
+
+#define min(a,b) ((a) > (b) ? (b) : (a))
+
+extern char _start[];
+extern char _end[];
+
+static grub_uint64_t mem_size, above_4g;
+
+void
+grub_machine_mmap_init (void)
+{
+ grub_uint8_t high, low, b, c, d;
+ grub_cmos_read (QEMU_CMOS_MEMSIZE_HIGH, &high);
+ grub_cmos_read (QEMU_CMOS_MEMSIZE_LOW, &low);
+ mem_size = ((grub_uint64_t) high) << 24
+ | ((grub_uint64_t) low) << 16;
+ if (mem_size > 0)
+ {
+ /* Don't ask... */
+ mem_size += (16 * 1024 * 1024);
+ }
+ else
+ {
+ grub_cmos_read (QEMU_CMOS_MEMSIZE2_HIGH, &high);
+ grub_cmos_read (QEMU_CMOS_MEMSIZE2_LOW, &low);
+ mem_size
+ = ((((grub_uint64_t) high) << 18) | (((grub_uint64_t) low) << 10))
+ + 1024 * 1024;
+ }
+
+ grub_cmos_read (0x5b, &b);
+ grub_cmos_read (0x5c, &c);
+ grub_cmos_read (0x5d, &d);
+ above_4g = (((grub_uint64_t) b) << 16)
+ | (((grub_uint64_t) c) << 24)
+ | (((grub_uint64_t) d) << 32);
+}
+
+grub_err_t
+grub_machine_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
+{
+ if (hook (0x0,
+ (grub_addr_t) _start,
+ GRUB_MEMORY_AVAILABLE, hook_data))
+ return 1;
+
+ if (hook ((grub_addr_t) _end,
+ 0xa0000 - (grub_addr_t) _end,
+ GRUB_MEMORY_AVAILABLE, hook_data))
+ return 1;
+
+ if (hook (GRUB_MEMORY_MACHINE_UPPER,
+ 0x100000 - GRUB_MEMORY_MACHINE_UPPER,
+ GRUB_MEMORY_RESERVED, hook_data))
+ return 1;
+
+ /* Everything else is free. */
+ if (hook (0x100000,
+ min (mem_size, (grub_uint32_t) -GRUB_BOOT_MACHINE_SIZE) - 0x100000,
+ GRUB_MEMORY_AVAILABLE, hook_data))
+ return 1;
+
+ /* Protect boot.img, which contains the gdt. It is mapped at the top of memory
+ (it is also mapped below 0x100000, but we already reserved that area). */
+ if (hook ((grub_uint32_t) -GRUB_BOOT_MACHINE_SIZE,
+ GRUB_BOOT_MACHINE_SIZE,
+ GRUB_MEMORY_RESERVED, hook_data))
+ return 1;
+
+ if (above_4g != 0 && hook (0x100000000ULL, above_4g,
+ GRUB_MEMORY_AVAILABLE, hook_data))
+ return 1;
+
+ return 0;
+}
diff --git a/grub-core/kern/i386/qemu/startup.S b/grub-core/kern/i386/qemu/startup.S
new file mode 100644
index 0000000..0d89858
--- /dev/null
+++ b/grub-core/kern/i386/qemu/startup.S
@@ -0,0 +1,75 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 1999,2000,2001,2002,2003,2005,2006,2007,2008,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 <config.h>
+#include <grub/symbol.h>
+
+#include <grub/machine/memory.h>
+#include <grub/machine/kernel.h>
+
+ .text
+ .code32
+ .globl _start
+_start:
+ jmp codestart
+
+ .org GRUB_KERNEL_I386_QEMU_CORE_ENTRY_ADDR
+VARIABLE(grub_core_entry_addr)
+ .long 0
+
+codestart:
+ /* Relocate to low memory. First we figure out our location.
+ We will derive the rom start address from it. */
+ call 1f
+1: popl %esi
+
+ /* Rom size is a multiple of 64 kiB. With this we get the
+ value of `grub_core_entry_addr' in %esi. */
+ xorw %si, %si
+
+ movl $(_edata - _start), %ecx
+ movl $_start, %edi
+ cld
+ rep
+ movsb
+ ljmp $GRUB_MEMORY_MACHINE_PROT_MODE_CSEG, $1f
+1:
+
+ /* clean out the bss */
+ movl $BSS_START_SYMBOL, %edi
+
+ /* compute the bss length */
+ movl $END_SYMBOL, %ecx
+ subl %edi, %ecx
+
+ /* clean out */
+ xorl %eax, %eax
+ cld
+ rep
+ stosb
+
+ /*
+ * Call the start of main body of C code.
+ */
+ call EXT_C(grub_main)
+
+ /* This should never happen. */
+ cli
+1:
+ hlt
+ jmp 1b