Index: hw/pc.c
===================================================================
--- hw/pc.c	(revision 5846)
+++ hw/pc.c	(working copy)
@@ -806,39 +806,29 @@
 
     vmport_init();
 
-    /* allocate RAM */
-    ram_addr = qemu_ram_alloc(0xa0000);
+    /* allocate first MB of RAM */
+    ram_addr = qemu_ram_alloc(0x100000);
     cpu_register_physical_memory(0, 0xa0000, ram_addr);
 
-    /* Allocate, even though we won't register, so we don't break the
-     * phys_ram_base + PA assumption. This range includes vga (0xa0000 - 0xc0000),
-     * and some bios areas, which will be registered later
-     */
-    ram_addr = qemu_ram_alloc(0x100000 - 0xa0000);
-    ram_addr = qemu_ram_alloc(below_4g_mem_size - 0x100000);
-    cpu_register_physical_memory(0x100000,
-                 below_4g_mem_size - 0x100000,
-                 ram_addr);
+    /* allocate all of the remaining RAM and register it with the CPU */
+    cpu_register_physical_memory(0x100000, below_4g_mem_size - 0x100000,
+        qemu_ram_alloc(below_4g_mem_size - 0x100000));
 
-    /* above 4giga memory allocation */
     if (above_4g_mem_size > 0) {
-        ram_addr = qemu_ram_alloc(above_4g_mem_size);
-        cpu_register_physical_memory(0x100000000ULL,
-                                     above_4g_mem_size,
-                                     ram_addr);
+        cpu_register_physical_memory(0x100000000ULL, above_4g_mem_size,
+            qemu_ram_alloc(above_4g_mem_size));
     }
 
-
     /* allocate VGA RAM */
     vga_ram_addr = qemu_ram_alloc(vga_ram_size);
 
-    /* BIOS load */
+    /* BIOS: load it to memory, copy the ISA BIOS into the last 128k
+     * of the first MB, map the whole BIOS at the top of memory */
     if (bios_name == NULL)
         bios_name = BIOS_FILENAME;
     snprintf(buf, sizeof(buf), "%s/%s", bios_dir, bios_name);
     bios_size = get_image_size(buf);
-    if (bios_size <= 0 ||
-        (bios_size % 65536) != 0) {
+    if (bios_size <= 0 || (bios_size % 65536) != 0) {
         goto bios_error;
     }
     bios_offset = qemu_ram_alloc(bios_size);
@@ -849,7 +839,20 @@
         exit(1);
     }
 
-    /* VGA BIOS load */
+    cpu_register_physical_memory((uint32_t)(-bios_size),
+        bios_size, bios_offset | IO_MEM_ROM);
+
+    isa_bios_size = bios_size;
+    if (isa_bios_size > (128 * 1024))
+        isa_bios_size = 128 * 1024;
+
+    memcpy(phys_ram_base + 0x100000 - isa_bios_size,
+        phys_ram_base + ram_addr + 0x100000 - isa_bios_size, isa_bios_size);
+    cpu_register_physical_memory(0x100000 - isa_bios_size, isa_bios_size,
+        ram_addr + 0x100000 - isa_bios_size);
+
+    /* VGA BIOS: load it directly into 0xc0000+0x10000,
+     * register the range with the CPU. */
     if (cirrus_vga_enabled) {
         snprintf(buf, sizeof(buf), "%s/%s", bios_dir, VGABIOS_CIRRUS_FILENAME);
     } else {
@@ -858,27 +861,17 @@
     vga_bios_size = get_image_size(buf);
     if (vga_bios_size <= 0 || vga_bios_size > 65536)
         goto vga_bios_error;
-    vga_bios_offset = qemu_ram_alloc(65536);
 
-    ret = load_image(buf, phys_ram_base + vga_bios_offset);
+    ret = load_image(buf, phys_ram_base + ram_addr + 0xc0000);
     if (ret != vga_bios_size) {
     vga_bios_error:
         fprintf(stderr, "qemu: could not load VGA BIOS '%s'\n", buf);
         exit(1);
     }
 
-    /* setup basic memory access */
     cpu_register_physical_memory(0xc0000, 0x10000,
-                                 vga_bios_offset | IO_MEM_ROM);
+        vga_bios_offset | IO_MEM_ROM);
 
-    /* map the last 128KB of the BIOS in ISA space */
-    isa_bios_size = bios_size;
-    if (isa_bios_size > (128 * 1024))
-        isa_bios_size = 128 * 1024;
-    cpu_register_physical_memory(0x100000 - isa_bios_size,
-                                 isa_bios_size,
-                                 (bios_offset + bios_size - isa_bios_size) | IO_MEM_ROM);
-
     {
         ram_addr_t option_rom_offset;
         int size, offset;
@@ -916,10 +909,6 @@
         }
     }
 
-    /* map all the bios at the top of memory */
-    cpu_register_physical_memory((uint32_t)(-bios_size),
-                                 bios_size, bios_offset | IO_MEM_ROM);
-
     bochs_bios_init();
 
     cpu_irq = qemu_allocate_irqs(pic_irq_request, NULL, 1);
