Skip to contentSkip to conversation

mmap services

PATCH phcoder 2 participants 4 messages Apr 11, 2009 – Apr 30, 2009

lore
Share & tools
lore

phcoder

author
#0 lore raw

Hello. Here is the patch to allow different mmap filtering. It naturally incorporates badram and also intercepts int12/15 so it works also with OS recieving mmap from BIOS. It's useful for intercepting any bios interrupts

Signature · 2 lines
Regards
Vladimir 'phcoder' Serbinenko
ChangeLog +49 -0
index a6dd8f7..ce0eb8d 100644
@@ -1,3 +1,52 @@
+2009-04-11  Vladimir Serbinenko <[email protected]>
+
+	Mmap services
+
+	* commands/lsmmap.c (grub_cmd_lsmmap): use grub_mmap_iterate
+	* loader/i386/linux.c (find_mmap_size): likewise
+	(allocate_pages): likewise
+	* loader/i386/multiboot.c (grub_get_multiboot_mmap_len): likewise
+	(grub_fill_multiboot_mmap): likewise
+	(grub_multiboot): use grub_mmap_get_lower and grub_mmap_get_upper
+	* loader/i386/pc/linux.c (grub_cmd_linux): use grub_mmap_get_lower
+	* include/grub/i386/bsd.h (OPENBSD_MMAP_AVAILABLE): new definition
+	(OPENBSD_MMAP_RESERVED): likewise
+	* include/grub/i386/pc/memory.h: include grub/memory.h
+	(grub_lower_mem): removed
+	(grub_upper_mem): likewise
+	(GRUB_MACHINE_MEMORY_ACPI): new definition
+	(GRUB_MACHINE_MEMORY_NVS): likewise
+	(GRUB_MACHINE_MEMORY_MAX_TYPE): likewise
+	(GRUB_MACHINE_MEMORY_HOLE): likewise
+	(grub_machine_mmap_register): likewise
+	(grub_machine_mmap_unregister): likewise
+	(grub_machine_get_upper): likewise
+	(grub_machine_get_lower): likewise
+	(grub_machine_get_post64): likewise
+	* conf/i386-pc.rmk (pkglib_MODULES): added mmap.mod
+	(mmap_mod_SOURCES): new variable
+	(mmap_mod_LDFLAGS): likewise
+	(mmap_mod_ASFLAGS): likewise
+	* conf/i386-coreboot.rmk: likewise
+	* conf/i386-ieee1275.rmk: likewise
+	* include/grub/types.h (UINT_TO_PTR): new macro
+	(PTR_TO_UINT32): likewise
+	(PTR_TO_UINT64): likewise
+	* include/grub/memory.h: new file
+	* mmap/i386/pc/mmap.c: likewise
+	* mmap/i386/pc/mmap_helper.S: likewise
+	* mmap/i386/uppermem.c: likewise
+	* mmap/mmap.c: likewise
+	* kern/i386/coreboot/init.c (grub_machine_init): don't use 
+	grub_upper_mem
+	* kern/i386/pc/init.c (grub_lower_mem): removed variable
+	(grub_upper_mem): likewise
+	(grub_machine_init): don't use grub_upper_mem,
+	make grub_lower_mem local
+	* loader/i386/bsd.c (grub_openbsd_boot): use grub_mmap_get_lower,
+	grub_mmap_iterate and grub_mmap_get_upper
+	(grub_netbsd_boot): use grub_mmap_get_lower and grub_mmap_get_upper
+
 2009-04-06  Vladimir Serbinenko <[email protected]>
 
 	Preboot hooks support
conf/i386-coreboot.rmk +7 -1
index bc500d9..554463f 100644
@@ -102,7 +102,13 @@ pkglib_MODULES = linux.mod normal.mod multiboot.mod 	\
 	aout.mod play.mod serial.mod ata.mod		\
 	memdisk.mod pci.mod lspci.mod reboot.mod	\
 	halt.mod datetime.mod date.mod datehook.mod	\
-	lsmmap.mod
+	lsmmap.mod mmap.mod
+
+# For mmap.mod.
+mmap_mod_SOURCES = mmap/mmap.c mmap/i386/uppermem.c
+mmap_mod_CFLAGS = $(COMMON_CFLAGS)
+mmap_mod_LDFLAGS = $(COMMON_LDFLAGS)
+mmap_mod_ASFLAGS = $(COMMON_ASFLAGS)
 
 # For linux.mod.
 linux_mod_SOURCES = loader/i386/linux.c
conf/i386-ieee1275.rmk +7 -1
index bbd6c09..9c0e4bc 100644
@@ -103,7 +103,13 @@ grub_install_SOURCES = util/ieee1275/grub-install.in
 pkglib_MODULES = normal.mod halt.mod reboot.mod suspend.mod		\
 	multiboot.mod aout.mod serial.mod linux.mod	\
 	nand.mod memdisk.mod pci.mod lspci.mod datetime.mod	\
-	date.mod datehook.mod lsmmap.mod
+	date.mod datehook.mod lsmmap.mod mmap.mod
+
+# For mmap.mod.
+mmap_mod_SOURCES = mmap/mmap.c mmap/i386/uppermem.c
+mmap_mod_CFLAGS = $(COMMON_CFLAGS)
+mmap_mod_LDFLAGS = $(COMMON_LDFLAGS)
+mmap_mod_ASFLAGS = $(COMMON_ASFLAGS)
 
 #
 # Only arch dependant part of normal.mod will be here. Common part for
conf/i386-pc.rmk +9 -1
index fdb43df..178974c 100644
@@ -175,7 +175,15 @@ pkglib_MODULES = biosdisk.mod chain.mod normal.mod \
 	ata.mod vga.mod memdisk.mod pci.mod lspci.mod	\
 	aout.mod bsd.mod pxe.mod pxecmd.mod datetime.mod date.mod \
 	datehook.mod lsmmap.mod ata_pthru.mod hdparm.mod \
-	usb.mod uhci.mod ohci.mod usbtest.mod usbms.mod usb_keyboard.mod
+	usb.mod uhci.mod ohci.mod usbtest.mod usbms.mod usb_keyboard.mod \
+	mmap.mod
+
+# For mmap.mod.
+mmap_mod_SOURCES = mmap/mmap.c mmap/i386/uppermem.c mmap/i386/pc/mmap.c \
+		   mmap/i386/pc/mmap_helper.S
+mmap_mod_CFLAGS = $(COMMON_CFLAGS)
+mmap_mod_LDFLAGS = $(COMMON_LDFLAGS)
+mmap_mod_ASFLAGS = $(COMMON_ASFLAGS)
 
 # For biosdisk.mod.
 biosdisk_mod_SOURCES = disk/i386/pc/biosdisk.c
include/grub/i386/bsd.h +2 -0
index f50f18e..78fae79 100644
@@ -148,6 +148,8 @@ struct grub_openbsd_bios_mmap
 {
   grub_uint64_t addr;
   grub_uint64_t len;
+#define	OPENBSD_MMAP_AVAILABLE	1
+#define	OPENBSD_MMAP_RESERVED 2
   grub_uint32_t type;
 };
 
include/grub/i386/pc/memory.h +29 -6
index 08e92a9..610c65a 100644
@@ -25,6 +25,7 @@
 #ifndef ASM_FILE
 #include <grub/types.h>
 #include <grub/err.h>
+#include <grub/memory.h>
 #endif
 
 /* The scratch buffer used in real mode code.  */
@@ -79,12 +80,6 @@
 
 #ifndef ASM_FILE
 
-#ifndef GRUB_MACHINE_IEEE1275
-extern grub_size_t EXPORT_VAR(grub_lower_mem);
-#endif
-
-extern grub_size_t EXPORT_VAR(grub_upper_mem);
-
 struct grub_machine_mmap_entry
 {
   grub_uint32_t size;
@@ -92,12 +87,40 @@ struct grub_machine_mmap_entry
   grub_uint64_t len;
 #define GRUB_MACHINE_MEMORY_AVAILABLE	1
 #define GRUB_MACHINE_MEMORY_RESERVED	2
+#define GRUB_MACHINE_MEMORY_ACPI	3
+#define GRUB_MACHINE_MEMORY_NVS 	4
+#define GRUB_MACHINE_MEMORY_MAX_TYPE 	4
+  /* This one is special: it's used internally but is never reported 
+     by firmware. */
+#define GRUB_MACHINE_MEMORY_HOLE 	5
+
   grub_uint32_t type;
 } __attribute__((packed));
 
 grub_err_t EXPORT_FUNC(grub_machine_mmap_iterate)
      (int NESTED_FUNC_ATTR (*hook) (grub_uint64_t, grub_uint64_t, grub_uint32_t));
 
+grub_uint64_t grub_mmap_get_post64 (void);
+grub_uint64_t grub_mmap_get_upper (void);
+grub_uint64_t grub_mmap_get_lower (void);
+
+#ifdef GRUB_MACHINE_PCBIOS
+grub_err_t grub_machine_mmap_register (grub_uint64_t start, grub_uint64_t size, 
+				       int type, int handle);
+grub_err_t grub_machine_mmap_unregister (int handle);
+#else
+static inline grub_err_t grub_machine_mmap_register (grub_uint64_t start, 
+					      grub_uint64_t size, 
+					      int type, int handle)
+{
+  return GRUB_ERR_NONE;
+}
+static inline grub_err_t grub_machine_mmap_unregister (int handle)
+{
+  return GRUB_ERR_NONE;
+}
+#endif
+
 #endif
 
 #endif /* ! GRUB_MEMORY_MACHINE_HEADER */
include/grub/memory.h new file +34 -0
new file mode 100644
index 0000000..03a8516
@@ -0,0 +1,34 @@
+/* memory.h - describe the memory map */
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2002,2007,2008  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/>.
+ */
+
+#ifndef GRUB_MEMORY_HEADER
+#define GRUB_MEMORY_HEADER	1
+
+#include <grub/types.h>
+#include <grub/err.h>
+
+grub_err_t
+grub_mmap_iterate (int NESTED_FUNC_ATTR (*hook) (grub_uint64_t, 
+						 grub_uint64_t, grub_uint32_t));
+int
+grub_mmap_register (grub_uint64_t start, grub_uint64_t size, int type);
+grub_err_t
+grub_mmap_unregister (int handle);
+
+#endif /* ! GRUB_MEMORY_HEADER */
include/grub/types.h +10 -0
index 8d51b66..faf2257 100644
@@ -100,6 +100,16 @@ typedef grub_int32_t	grub_ssize_t;
 # define LONG_MAX 2147483647UL
 #endif
 
+#if GRUB_CPU_SIZEOF_VOID_P == 4
+#define UINT_TO_PTR(x) ((void*)(grub_uint32_t)(x))
+#define PTR_TO_UINT64(x) ((grub_uint64_t)(grub_uint32_t)(x))
+#define PTR_TO_UINT32(x) ((grub_uint32_t)(x))
+#else
+#define UINT_TO_PTR(x) ((void*)(grub_uint64_t)(x))
+#define PTR_TO_UINT64(x) ((grub_uint64_t)(x))
+#define PTR_TO_UINT32(x) ((grub_uint32_t)(grub_uint64_t)(x))
+#endif
+
 /* The type for representing a file offset.  */
 typedef grub_uint64_t	grub_off_t;
 
kern/i386/coreboot/init.c +1 -9
index 1348488..2f7e875 100644
@@ -82,16 +82,11 @@ grub_machine_init (void)
 #if GRUB_CPU_SIZEOF_VOID_P == 4
     /* Restrict ourselves to 32-bit memory space.  */
     if (addr > ULONG_MAX)
-      {
-	grub_upper_mem = ULONG_MAX;
-	return 0;
-      }
+      return 0;
     if (addr + size > ULONG_MAX)
       size = ULONG_MAX - addr;
 #endif
 
-    grub_upper_mem = grub_max (grub_upper_mem, addr + size);
-
     if (type != GRUB_MACHINE_MEMORY_AVAILABLE)
       return 0;
 
@@ -128,9 +123,6 @@ grub_machine_init (void)
   grub_machine_mmap_init ();
   grub_machine_mmap_iterate (heap_init);
 
-  /* This variable indicates size, not offset.  */
-  grub_upper_mem -= GRUB_MEMORY_MACHINE_UPPER_START;
-
   grub_tsc_init ();
 }
 
kern/i386/pc/init.c +1 -2
index 6191412..c64497e 100644
@@ -45,7 +45,6 @@ static int num_regions;
 
 grub_addr_t grub_os_area_addr;
 grub_size_t grub_os_area_size;
-grub_size_t grub_lower_mem, grub_upper_mem;
 
 void 
 grub_arch_sync_caches (void *address __attribute__ ((unused)),
@@ -133,6 +132,7 @@ void
 grub_machine_init (void)
 {
   int i;
+  int grub_lower_mem;
   
   /* Initialize the console as early as possible.  */
   grub_console_init ();
@@ -197,7 +197,6 @@ grub_machine_init (void)
       {
 	grub_size_t quarter = mem_regions[i].size >> 2;
 
-	grub_upper_mem = mem_regions[i].size;
 	grub_os_area_addr = mem_regions[i].addr;
 	grub_os_area_size = mem_regions[i].size - quarter;
 	grub_mm_init_region ((void *) (grub_os_area_addr + grub_os_area_size),
loader/i386/bsd.c +27 -19

71 lines not on this page · raw message

loader/i386/ieee1275/linux.c +1 -1
index 2f52880..a563577 100644
@@ -100,7 +100,7 @@ grub_linux_boot (void)
 
   grub_memset ((char *) params, 0, GRUB_OFW_LINUX_CL_OFFSET);
 
-  params->alt_mem = grub_upper_mem >> 10;
+  params->alt_mem = grub_mmap_get_upper () >> 10;
   params->ext_mem = params->alt_mem;
 
   lh->cmd_line_ptr = (char *)
loader/i386/linux.c +3 -3
index 01d10d5..aac9bf3 100644
@@ -168,7 +168,7 @@ find_mmap_size (void)
       return 0;
     }
   
-  grub_machine_mmap_iterate (hook);
+  grub_mmap_iterate (hook);
   
   mmap_size = count * sizeof (struct grub_e820_mmap);
 
@@ -237,7 +237,7 @@ allocate_pages (grub_size_t prot_size)
 
       return 0;
     }
-  grub_machine_mmap_iterate (hook);
+  grub_mmap_iterate (hook);
   if (! real_mode_mem)
     {
       grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate real mode pages");
@@ -404,7 +404,7 @@ grub_linux_boot (void)
     }
 
   e820_num = 0;
-  grub_machine_mmap_iterate (hook);
+  grub_mmap_iterate (hook);
   params->mmap_size = e820_num;
 
   /* Hardware interrupts are not safe any longer.  */
loader/i386/multiboot.c +4 -4
index 27042a5..d38590f 100644
@@ -100,7 +100,7 @@ grub_get_multiboot_mmap_len (void)
       return 0;
     }
   
-  grub_machine_mmap_iterate (hook);
+  grub_mmap_iterate (hook);
   
   return count * sizeof (struct grub_multiboot_mmap_entry);
 }
@@ -123,7 +123,7 @@ grub_fill_multiboot_mmap (struct grub_multiboot_mmap_entry *first_entry)
       return 0;
     }
 
-  grub_machine_mmap_iterate (hook);
+  grub_mmap_iterate (hook);
 }
 
 #define MULTIBOOT_LOAD_ELF64
@@ -342,8 +342,8 @@ grub_multiboot (int argc, char *argv[])
 		grub_multiboot_payload_entry_offset);
 
   /* Convert from bytes to kilobytes.  */
-  mbi->mem_lower = grub_lower_mem / 1024;
-  mbi->mem_upper = grub_upper_mem / 1024;
+  mbi->mem_lower = grub_mmap_get_lower () / 1024;
+  mbi->mem_upper = grub_mmap_get_upper () / 1024;
   mbi->flags |= MULTIBOOT_INFO_MEMORY;
 
   cmdline = p = cmdline_addr (grub_multiboot_payload_orig);
loader/i386/pc/linux.c +6 -5
index 8ff97f4..34a4ebc 100644
@@ -108,8 +108,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
       lh.type_of_loader = GRUB_LINUX_BOOT_LOADER_TYPE;
       
       /* Put the real mode part at as a high location as possible.  */
-      grub_linux_real_addr = (char *) (grub_lower_mem
-				       - GRUB_LINUX_SETUP_MOVE_SIZE);
+      grub_linux_real_addr 
+	= (char *) UINT_TO_PTR (grub_mmap_get_lower ()
+				- GRUB_LINUX_SETUP_MOVE_SIZE);
       /* But it must not exceed the traditional area.  */
       if (grub_linux_real_addr > (char *) GRUB_LINUX_OLD_REAL_MODE_ADDR)
 	grub_linux_real_addr = (char *) GRUB_LINUX_OLD_REAL_MODE_ADDR;
@@ -159,12 +160,12 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
     }
   
   if (grub_linux_real_addr + GRUB_LINUX_SETUP_MOVE_SIZE
-      > (char *) grub_lower_mem)
+      > (char *) UINT_TO_PTR (grub_mmap_get_lower ()))
     {
       grub_error (GRUB_ERR_OUT_OF_RANGE,
 		 "too small lower memory (0x%x > 0x%x)",
-		 grub_linux_real_addr + GRUB_LINUX_SETUP_MOVE_SIZE,
-		 (char *) grub_lower_mem);
+		  grub_linux_real_addr + GRUB_LINUX_SETUP_MOVE_SIZE,
+		  (int) grub_mmap_get_lower ());
       goto fail;
     }
 
mmap/i386/pc/mmap.c new file +240 -0

240 lines not on this page · raw message

mmap/i386/pc/mmap_helper.S new file +122 -0

122 lines not on this page · raw message

mmap/i386/uppermem.c new file +85 -0

85 lines not on this page · raw message

mmap/mmap.c new file +364 -0

364 lines not on this page · raw message

#1 lore raw

Updated. Now works on EFI. Additional function grub_mmap_malign_and_register
Which finds a spot in memory map with required characteristics and registers it.
I successfully booted freedos with this patch and badram

Quoted 8 lines · phcoder

Hello. Here is the patch to allow different mmap filtering. It naturally incorporates badram and also intercepts int12/15 so it works also with OS recieving mmap from BIOS. It's useful for intercepting any bios interrupts --

Regards
Vladimir 'phcoder' Serbinenko

Signature · 2 lines
Regards
Vladimir 'phcoder' Serbinenko
ChangeLog +55 -0
index 2f2afc2..2a800d9 100644
@@ -1,5 +1,60 @@
 2009-04-26  Vladimir Serbinenko <[email protected]>
 
+	Mmap services
+
+	* loader/i386/efi/linux.c (grub_linux_boot): use grub_mmap_iterate
+	* loader/i386/linux.c (find_mmap_size): likewise
+	(allocate_pages): likewise
+	* loader/i386/multiboot.c (grub_get_multiboot_mmap_len): likewise
+	(grub_fill_multiboot_mmap): likewise
+	(grub_multiboot): use grub_mmap_get_lower and grub_mmap_get_upper
+	* loader/i386/pc/linux.c (grub_cmd_linux): use grub_mmap_get_lower
+	* include/grub/i386/bsd.h (OPENBSD_MMAP_AVAILABLE): new definition
+	(OPENBSD_MMAP_RESERVED): likewise
+	* include/grub/i386/pc/memory.h: include grub/memory.h
+	(grub_lower_mem): removed
+	(grub_upper_mem): likewise
+	(GRUB_MACHINE_MEMORY_ACPI): new definition
+	(GRUB_MACHINE_MEMORY_NVS): likewise
+	(GRUB_MACHINE_MEMORY_MAX_TYPE): likewise
+	(GRUB_MACHINE_MEMORY_HOLE): likewise
+	(grub_machine_mmap_register): likewise
+	(grub_machine_mmap_unregister): likewise
+	(grub_machine_get_upper): likewise
+	(grub_machine_get_lower): likewise
+	(grub_machine_get_post64): likewise
+	* include/grub/i386/efi/memory.h: new file
+	* include/grub/x86_64/efi/memory.h: likewise
+	* include/grub/efi/memory.h: likewise
+	* conf/i386-pc.rmk (pkglib_MODULES): added mmap.mod
+	(mmap_mod_SOURCES): new variable
+	(mmap_mod_LDFLAGS): likewise
+	(mmap_mod_ASFLAGS): likewise
+	* conf/i386-coreboot.rmk: likewise
+	* conf/i386-ieee1275.rmk: likewise
+	* conf/i386-efi.rmk: likewise
+	* conf/x86_64-efi.rmk: likewise
+	* include/grub/types.h (UINT_TO_PTR): new macro
+	(PTR_TO_UINT32): likewise
+	(PTR_TO_UINT64): likewise
+	* include/grub/memory.h: new file
+	* mmap/i386/pc/mmap.c: likewise
+	* mmap/i386/pc/mmap_helper.S: likewise
+	* mmap/i386/uppermem.c: likewise
+	* mmap/mmap.c: likewise
+	* mmap/efi/mmap.c: likewise
+	* kern/i386/coreboot/init.c (grub_machine_init): don't use 
+	grub_upper_mem
+	* kern/i386/pc/init.c (grub_lower_mem): removed variable
+	(grub_upper_mem): likewise
+	(grub_machine_init): don't use grub_upper_mem,
+	make grub_lower_mem local
+	* loader/i386/bsd.c (grub_openbsd_boot): use grub_mmap_get_lower,
+	grub_mmap_iterate and grub_mmap_get_upper
+	(grub_netbsd_boot): use grub_mmap_get_lower and grub_mmap_get_upper
+
+2009-04-26  Vladimir Serbinenko <[email protected]>
+
 	Preboot hooks support
 
 	* commands/boot.c (struct grub_preboot_t): new declaration
conf/i386-coreboot.rmk +7 -1
index 1284fe4..89014a6 100644
@@ -104,7 +104,13 @@ pkglib_MODULES = linux.mod normal.mod multiboot.mod 	\
 	aout.mod play.mod serial.mod ata.mod		\
 	memdisk.mod pci.mod lspci.mod reboot.mod	\
 	halt.mod datetime.mod date.mod datehook.mod	\
-	lsmmap.mod
+	lsmmap.mod mmap.mod
+
+# For mmap.mod.
+mmap_mod_SOURCES = mmap/mmap.c mmap/i386/uppermem.c mmap/i386/mmap.c
+mmap_mod_CFLAGS = $(COMMON_CFLAGS)
+mmap_mod_LDFLAGS = $(COMMON_LDFLAGS)
+mmap_mod_ASFLAGS = $(COMMON_ASFLAGS)
 
 # For linux.mod.
 linux_mod_SOURCES = loader/i386/linux.c
conf/i386-efi.rmk +7 -1
index 717683b..3c8c62d 100644
@@ -82,7 +82,7 @@ grub_install_SOURCES = util/i386/efi/grub-install.in
 # Modules.
 pkglib_MODULES = kernel.mod normal.mod chain.mod appleldr.mod \
 	linux.mod halt.mod reboot.mod pci.mod lspci.mod \
-	datetime.mod date.mod datehook.mod loadbios.mod fixvideo.mod
+	datetime.mod date.mod datehook.mod loadbios.mod fixvideo.mod mmap.mod
 
 # For kernel.mod.
 kernel_mod_EXPORTS = no
@@ -135,6 +135,12 @@ normal_mod_CFLAGS = $(COMMON_CFLAGS)
 normal_mod_ASFLAGS = $(COMMON_ASFLAGS)
 normal_mod_LDFLAGS = $(COMMON_LDFLAGS)
 
+# For mmap.mod.
+mmap_mod_SOURCES = mmap/mmap.c mmap/i386/uppermem.c mmap/i386/mmap.c \
+		   mmap/efi/mmap.c
+mmap_mod_CFLAGS = $(COMMON_CFLAGS)
+mmap_mod_LDFLAGS = $(COMMON_LDFLAGS)
+
 # For chain.mod.
 chain_mod_SOURCES = loader/efi/chainloader.c
 chain_mod_CFLAGS = $(COMMON_CFLAGS)
conf/i386-ieee1275.rmk +7 -1
index cb32c73..08fdd98 100644
@@ -104,7 +104,13 @@ grub_install_SOURCES = util/ieee1275/grub-install.in
 pkglib_MODULES = normal.mod halt.mod reboot.mod suspend.mod		\
 	multiboot.mod aout.mod serial.mod linux.mod	\
 	nand.mod memdisk.mod pci.mod lspci.mod datetime.mod	\
-	date.mod datehook.mod lsmmap.mod
+	date.mod datehook.mod lsmmap.mod mmap.mod
+
+# For mmap.mod.
+mmap_mod_SOURCES = mmap/mmap.c mmap/i386/uppermem.c mmap/i386/mmap.c
+mmap_mod_CFLAGS = $(COMMON_CFLAGS)
+mmap_mod_LDFLAGS = $(COMMON_LDFLAGS)
+mmap_mod_ASFLAGS = $(COMMON_ASFLAGS)
 
 #
 # Only arch dependant part of normal.mod will be here. Common part for
conf/i386-pc.rmk +9 -1
index d74a5fa..709741a 100644
@@ -182,7 +182,15 @@ pkglib_MODULES = biosdisk.mod chain.mod normal.mod \
 	ata.mod vga.mod memdisk.mod pci.mod lspci.mod	\
 	aout.mod bsd.mod pxe.mod pxecmd.mod datetime.mod date.mod \
 	datehook.mod lsmmap.mod ata_pthru.mod hdparm.mod \
-	usb.mod uhci.mod ohci.mod usbtest.mod usbms.mod usb_keyboard.mod
+	usb.mod uhci.mod ohci.mod usbtest.mod usbms.mod usb_keyboard.mod \
+	mmap.mod
+
+# For mmap.mod.
+mmap_mod_SOURCES = mmap/mmap.c mmap/i386/uppermem.c mmap/i386/mmap.c \
+		   mmap/i386/pc/mmap.c mmap/i386/pc/mmap_helper.S
+mmap_mod_CFLAGS = $(COMMON_CFLAGS)
+mmap_mod_LDFLAGS = $(COMMON_LDFLAGS)
+mmap_mod_ASFLAGS = $(COMMON_ASFLAGS)
 
 # For biosdisk.mod.
 biosdisk_mod_SOURCES = disk/i386/pc/biosdisk.c
conf/x86_64-efi.rmk +7 -1
index d7ad9a4..d19649b 100644
@@ -83,7 +83,7 @@ grub_install_SOURCES = util/i386/efi/grub-install.in
 # Modules.
 pkglib_MODULES = kernel.mod normal.mod chain.mod appleldr.mod \
 	halt.mod reboot.mod linux.mod pci.mod lspci.mod \
-	datetime.mod date.mod datehook.mod loadbios.mod fixvideo.mod
+	datetime.mod date.mod datehook.mod loadbios.mod fixvideo.mod mmap.mod
 
 # For kernel.mod.
 kernel_mod_EXPORTS = no
@@ -137,6 +137,12 @@ normal_mod_CFLAGS = $(COMMON_CFLAGS)
 normal_mod_ASFLAGS = $(COMMON_ASFLAGS)
 normal_mod_LDFLAGS = $(COMMON_LDFLAGS)
 
+# For mmap.mod.
+mmap_mod_SOURCES = mmap/mmap.c mmap/i386/uppermem.c mmap/i386/mmap.c \
+		   mmap/efi/mmap.c
+mmap_mod_CFLAGS = $(COMMON_CFLAGS)
+mmap_mod_LDFLAGS = $(COMMON_LDFLAGS)
+
 # For chain.mod.
 chain_mod_SOURCES = loader/efi/chainloader.c
 chain_mod_CFLAGS = $(COMMON_CFLAGS)
include/grub/efi/memory.h new file +48 -0

48 lines not on this page · raw message

include/grub/i386/bsd.h +2 -0
index f50f18e..78fae79 100644
@@ -148,6 +148,8 @@ struct grub_openbsd_bios_mmap
 {
   grub_uint64_t addr;
   grub_uint64_t len;
+#define	OPENBSD_MMAP_AVAILABLE	1
+#define	OPENBSD_MMAP_RESERVED 2
   grub_uint32_t type;
 };
 
include/grub/i386/efi/memory.h new file +1 -0
new file mode 100644
index 0000000..c9a61bb
@@ -0,0 +1 @@
+#include <grub/efi/memory.h>
include/grub/i386/pc/memory.h +30 -6
index 08e92a9..d8d4342 100644
@@ -25,6 +25,7 @@
 #ifndef ASM_FILE
 #include <grub/types.h>
 #include <grub/err.h>
+#include <grub/memory.h>
 #endif
 
 /* The scratch buffer used in real mode code.  */
@@ -79,12 +80,6 @@
 
 #ifndef ASM_FILE
 
-#ifndef GRUB_MACHINE_IEEE1275
-extern grub_size_t EXPORT_VAR(grub_lower_mem);
-#endif
-
-extern grub_size_t EXPORT_VAR(grub_upper_mem);
-
 struct grub_machine_mmap_entry
 {
   grub_uint32_t size;
@@ -92,12 +87,41 @@ struct grub_machine_mmap_entry
   grub_uint64_t len;
 #define GRUB_MACHINE_MEMORY_AVAILABLE	1
 #define GRUB_MACHINE_MEMORY_RESERVED	2
+#define GRUB_MACHINE_MEMORY_ACPI	3
+#define GRUB_MACHINE_MEMORY_NVS 	4
+#define GRUB_MACHINE_MEMORY_MAX_TYPE 	4
+  /* This one is special: it's used internally but is never reported 
+     by firmware. */
+#define GRUB_MACHINE_MEMORY_HOLE 	5
+
   grub_uint32_t type;
 } __attribute__((packed));
 
 grub_err_t EXPORT_FUNC(grub_machine_mmap_iterate)
      (int NESTED_FUNC_ATTR (*hook) (grub_uint64_t, grub_uint64_t, grub_uint32_t));
 
+grub_uint64_t grub_mmap_get_post64 (void);
+grub_uint64_t grub_mmap_get_upper (void);
+grub_uint64_t grub_mmap_get_lower (void);
+
+#ifdef GRUB_MACHINE_PCBIOS
+grub_err_t grub_machine_mmap_register (grub_uint64_t start, grub_uint64_t size, 
+				       int type, int handle);
+grub_err_t grub_machine_mmap_unregister (int handle);
+#define GRUB_MMAP_MALLOC_LOW 1
+#else
+static inline grub_err_t grub_machine_mmap_register (grub_uint64_t start, 
+					      grub_uint64_t size, 
+					      int type, int handle)
+{
+  return GRUB_ERR_NONE;
+}
+static inline grub_err_t grub_machine_mmap_unregister (int handle)
+{
+  return GRUB_ERR_NONE;
+}
+#endif
+
 #endif
 
 #endif /* ! GRUB_MEMORY_MACHINE_HEADER */
include/grub/memory.h new file +52 -0

52 lines not on this page · raw message

include/grub/types.h +10 -0
index 50f8f58..114b456 100644
@@ -102,6 +102,16 @@ typedef grub_int32_t	grub_ssize_t;
 # define GRUB_LONG_MIN -2147483648UL
 #endif
 
+#if GRUB_CPU_SIZEOF_VOID_P == 4
+#define UINT_TO_PTR(x) ((void*)(grub_uint32_t)(x))
+#define PTR_TO_UINT64(x) ((grub_uint64_t)(grub_uint32_t)(x))
+#define PTR_TO_UINT32(x) ((grub_uint32_t)(x))
+#else
+#define UINT_TO_PTR(x) ((void*)(grub_uint64_t)(x))
+#define PTR_TO_UINT64(x) ((grub_uint64_t)(x))
+#define PTR_TO_UINT32(x) ((grub_uint32_t)(grub_uint64_t)(x))
+#endif
+
 /* The type for representing a file offset.  */
 typedef grub_uint64_t	grub_off_t;
 
include/grub/x86_64/efi/memory.h new file +1 -0
new file mode 100644
index 0000000..c9a61bb
@@ -0,0 +1 @@
+#include <grub/efi/memory.h>
kern/i386/coreboot/init.c +2 -10
index fdaf6fd..7bc4291 100644
@@ -82,16 +82,11 @@ grub_machine_init (void)
 #if GRUB_CPU_SIZEOF_VOID_P == 4
     /* Restrict ourselves to 32-bit memory space.  */
     if (addr > GRUB_ULONG_MAX)
-      {
-	grub_upper_mem = GRUB_ULONG_MAX;
-	return 0;
-      }
-    if (addr + size > GRUB_ULONG_MAX)
+      return 0;
+    if (addr + size > ULONG_MAX)
       size = GRUB_ULONG_MAX - addr;
 #endif
 
-    grub_upper_mem = grub_max (grub_upper_mem, addr + size);
-
     if (type != GRUB_MACHINE_MEMORY_AVAILABLE)
       return 0;
 
@@ -128,9 +123,6 @@ grub_machine_init (void)
   grub_machine_mmap_init ();
   grub_machine_mmap_iterate (heap_init);
 
-  /* This variable indicates size, not offset.  */
-  grub_upper_mem -= GRUB_MEMORY_MACHINE_UPPER_START;
-
   grub_tsc_init ();
 }
 
kern/i386/pc/init.c +1 -2
index 6191412..c64497e 100644
@@ -45,7 +45,6 @@ static int num_regions;
 
 grub_addr_t grub_os_area_addr;
 grub_size_t grub_os_area_size;
-grub_size_t grub_lower_mem, grub_upper_mem;
 
 void 
 grub_arch_sync_caches (void *address __attribute__ ((unused)),
@@ -133,6 +132,7 @@ void
 grub_machine_init (void)
 {
   int i;
+  int grub_lower_mem;
   
   /* Initialize the console as early as possible.  */
   grub_console_init ();
@@ -197,7 +197,6 @@ grub_machine_init (void)
       {
 	grub_size_t quarter = mem_regions[i].size >> 2;
 
-	grub_upper_mem = mem_regions[i].size;
 	grub_os_area_addr = mem_regions[i].addr;
 	grub_os_area_size = mem_regions[i].size - quarter;
 	grub_mm_init_region ((void *) (grub_os_area_addr + grub_os_area_size),
loader/i386/bsd.c +27 -19

71 lines not on this page · raw message

loader/i386/efi/linux.c +33 -64

130 lines not on this page · raw message

loader/i386/ieee1275/linux.c +1 -1
index 2f52880..a563577 100644
@@ -100,7 +100,7 @@ grub_linux_boot (void)
 
   grub_memset ((char *) params, 0, GRUB_OFW_LINUX_CL_OFFSET);
 
-  params->alt_mem = grub_upper_mem >> 10;
+  params->alt_mem = grub_mmap_get_upper () >> 10;
   params->ext_mem = params->alt_mem;
 
   lh->cmd_line_ptr = (char *)
loader/i386/linux.c +25 -3
index 19444f4..a72871b 100644
@@ -168,7 +168,7 @@ find_mmap_size (void)
       return 0;
     }
   
-  grub_machine_mmap_iterate (hook);
+  grub_mmap_iterate (hook);
   
   mmap_size = count * sizeof (struct grub_e820_mmap);
 
@@ -237,7 +237,7 @@ allocate_pages (grub_size_t prot_size)
 
       return 0;
     }
-  grub_machine_mmap_iterate (hook);
+  grub_mmap_iterate (hook);
   if (! real_mode_mem)
     {
       grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate real mode pages");
@@ -396,6 +396,27 @@ grub_linux_boot (void)
 				addr, size, GRUB_E820_RAM);
 	  break;
 
+#ifdef GRUB_MACHINE_MEMORY_ACPI
+        case GRUB_MACHINE_MEMORY_ACPI:
+	  grub_e820_add_region (params->e820_map, &e820_num,
+				addr, size, GRUB_E820_ACPI);
+	  break;
+#endif
+
+#ifdef GRUB_MACHINE_MEMORY_NVS
+        case GRUB_MACHINE_MEMORY_NVS:
+	  grub_e820_add_region (params->e820_map, &e820_num,
+				addr, size, GRUB_E820_NVS);
+	  break;
+#endif
+
+#ifdef GRUB_MACHINE_MEMORY_CODE
+        case GRUB_MACHINE_MEMORY_CODE:
+	  grub_e820_add_region (params->e820_map, &e820_num,
+				addr, size, GRUB_E820_EXEC_CODE);
+	  break;
+#endif
+
         default:
           grub_e820_add_region (params->e820_map, &e820_num,
                                 addr, size, GRUB_E820_RESERVED);
@@ -404,9 +425,10 @@ grub_linux_boot (void)
     }
 
   e820_num = 0;
-  grub_machine_mmap_iterate (hook);
+  grub_mmap_iterate (hook);
   params->mmap_size = e820_num;
 
+
   /* Hardware interrupts are not safe any longer.  */
   asm volatile ("cli" : : );
   
loader/i386/multiboot.c +4 -4
index 27042a5..d38590f 100644
@@ -100,7 +100,7 @@ grub_get_multiboot_mmap_len (void)
       return 0;
     }
   
-  grub_machine_mmap_iterate (hook);
+  grub_mmap_iterate (hook);
   
   return count * sizeof (struct grub_multiboot_mmap_entry);
 }
@@ -123,7 +123,7 @@ grub_fill_multiboot_mmap (struct grub_multiboot_mmap_entry *first_entry)
       return 0;
     }
 
-  grub_machine_mmap_iterate (hook);
+  grub_mmap_iterate (hook);
 }
 
 #define MULTIBOOT_LOAD_ELF64
@@ -342,8 +342,8 @@ grub_multiboot (int argc, char *argv[])
 		grub_multiboot_payload_entry_offset);
 
   /* Convert from bytes to kilobytes.  */
-  mbi->mem_lower = grub_lower_mem / 1024;
-  mbi->mem_upper = grub_upper_mem / 1024;
+  mbi->mem_lower = grub_mmap_get_lower () / 1024;
+  mbi->mem_upper = grub_mmap_get_upper () / 1024;
   mbi->flags |= MULTIBOOT_INFO_MEMORY;
 
   cmdline = p = cmdline_addr (grub_multiboot_payload_orig);
loader/i386/pc/linux.c +6 -5
index 8ff97f4..34a4ebc 100644
@@ -108,8 +108,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
       lh.type_of_loader = GRUB_LINUX_BOOT_LOADER_TYPE;
       
       /* Put the real mode part at as a high location as possible.  */
-      grub_linux_real_addr = (char *) (grub_lower_mem
-				       - GRUB_LINUX_SETUP_MOVE_SIZE);
+      grub_linux_real_addr 
+	= (char *) UINT_TO_PTR (grub_mmap_get_lower ()
+				- GRUB_LINUX_SETUP_MOVE_SIZE);
       /* But it must not exceed the traditional area.  */
       if (grub_linux_real_addr > (char *) GRUB_LINUX_OLD_REAL_MODE_ADDR)
 	grub_linux_real_addr = (char *) GRUB_LINUX_OLD_REAL_MODE_ADDR;
@@ -159,12 +160,12 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
     }
   
   if (grub_linux_real_addr + GRUB_LINUX_SETUP_MOVE_SIZE
-      > (char *) grub_lower_mem)
+      > (char *) UINT_TO_PTR (grub_mmap_get_lower ()))
     {
       grub_error (GRUB_ERR_OUT_OF_RANGE,
 		 "too small lower memory (0x%x > 0x%x)",
-		 grub_linux_real_addr + GRUB_LINUX_SETUP_MOVE_SIZE,
-		 (char *) grub_lower_mem);
+		  grub_linux_real_addr + GRUB_LINUX_SETUP_MOVE_SIZE,
+		  (int) grub_mmap_get_lower ());
       goto fail;
     }
 
mmap/efi/mmap.c new file +284 -0

284 lines not on this page · raw message

mmap/i386/mmap.c new file +98 -0

98 lines not on this page · raw message

mmap/i386/pc/mmap.c new file +216 -0

216 lines not on this page · raw message

mmap/i386/pc/mmap_helper.S new file +122 -0

122 lines not on this page · raw message

mmap/i386/uppermem.c new file +85 -0

85 lines not on this page · raw message

mmap/mmap.c new file +426 -0

426 lines not on this page · raw message

#2 lore raw

Rediffed and reposted due to request by David Miller

Quoted 24 lines · Vladimir 'phcoder' Serbinenko

Updated. Now works on EFI. Additional function grub_mmap_malign_and_register
Which finds a spot in memory map with required characteristics and registers it.
I successfully booted freedos with this patch and badram

Quoted 8 lines · phcoder

Hello. Here is the patch to allow different mmap filtering. It naturally incorporates badram and also intercepts int12/15 so it works also with OS recieving mmap from BIOS. It's useful for intercepting any bios interrupts
--

Regards
Vladimir 'phcoder' Serbinenko

--
Regards
Vladimir 'phcoder' Serbinenko

Signature · 2 lines
Regards
Vladimir 'phcoder' Serbinenko
ChangeLog +55 -0
index 9097f25..3dab81c 100644
@@ -1,3 +1,58 @@
+2009-04-26  Vladimir Serbinenko <[email protected]>
+
+	Mmap services
+
+	* loader/i386/efi/linux.c (grub_linux_boot): use grub_mmap_iterate
+	* loader/i386/linux.c (find_mmap_size): likewise
+	(allocate_pages): likewise
+	* loader/i386/multiboot.c (grub_get_multiboot_mmap_len): likewise
+	(grub_fill_multiboot_mmap): likewise
+	(grub_multiboot): use grub_mmap_get_lower and grub_mmap_get_upper
+	* loader/i386/pc/linux.c (grub_cmd_linux): use grub_mmap_get_lower
+	* include/grub/i386/bsd.h (OPENBSD_MMAP_AVAILABLE): new definition
+	(OPENBSD_MMAP_RESERVED): likewise
+	* include/grub/i386/pc/memory.h: include grub/memory.h
+	(grub_lower_mem): removed
+	(grub_upper_mem): likewise
+	(GRUB_MACHINE_MEMORY_ACPI): new definition
+	(GRUB_MACHINE_MEMORY_NVS): likewise
+	(GRUB_MACHINE_MEMORY_MAX_TYPE): likewise
+	(GRUB_MACHINE_MEMORY_HOLE): likewise
+	(grub_machine_mmap_register): likewise
+	(grub_machine_mmap_unregister): likewise
+	(grub_machine_get_upper): likewise
+	(grub_machine_get_lower): likewise
+	(grub_machine_get_post64): likewise
+	* include/grub/i386/efi/memory.h: new file
+	* include/grub/x86_64/efi/memory.h: likewise
+	* include/grub/efi/memory.h: likewise
+	* conf/i386-pc.rmk (pkglib_MODULES): added mmap.mod
+	(mmap_mod_SOURCES): new variable
+	(mmap_mod_LDFLAGS): likewise
+	(mmap_mod_ASFLAGS): likewise
+	* conf/i386-coreboot.rmk: likewise
+	* conf/i386-ieee1275.rmk: likewise
+	* conf/i386-efi.rmk: likewise
+	* conf/x86_64-efi.rmk: likewise
+	* include/grub/types.h (UINT_TO_PTR): new macro
+	(PTR_TO_UINT32): likewise
+	(PTR_TO_UINT64): likewise
+	* include/grub/memory.h: new file
+	* mmap/i386/pc/mmap.c: likewise
+	* mmap/i386/pc/mmap_helper.S: likewise
+	* mmap/i386/uppermem.c: likewise
+	* mmap/mmap.c: likewise
+	* mmap/efi/mmap.c: likewise
+	* kern/i386/coreboot/init.c (grub_machine_init): don't use 
+	grub_upper_mem
+	* kern/i386/pc/init.c (grub_lower_mem): removed variable
+	(grub_upper_mem): likewise
+	(grub_machine_init): don't use grub_upper_mem,
+	make grub_lower_mem local
+	* loader/i386/bsd.c (grub_openbsd_boot): use grub_mmap_get_lower,
+	grub_mmap_iterate and grub_mmap_get_upper
+	(grub_netbsd_boot): use grub_mmap_get_lower and grub_mmap_get_upper
+
 2009-04-30  David S. Miller  <[email protected]>
 
 	* util/hostdisk.c (device_is_wholedisk): New function.
conf/i386-coreboot.rmk +7 -1
index 1284fe4..89014a6 100644
@@ -104,7 +104,13 @@ pkglib_MODULES = linux.mod normal.mod multiboot.mod 	\
 	aout.mod play.mod serial.mod ata.mod		\
 	memdisk.mod pci.mod lspci.mod reboot.mod	\
 	halt.mod datetime.mod date.mod datehook.mod	\
-	lsmmap.mod
+	lsmmap.mod mmap.mod
+
+# For mmap.mod.
+mmap_mod_SOURCES = mmap/mmap.c mmap/i386/uppermem.c mmap/i386/mmap.c
+mmap_mod_CFLAGS = $(COMMON_CFLAGS)
+mmap_mod_LDFLAGS = $(COMMON_LDFLAGS)
+mmap_mod_ASFLAGS = $(COMMON_ASFLAGS)
 
 # For linux.mod.
 linux_mod_SOURCES = loader/i386/linux.c
conf/i386-efi.rmk +7 -1
index 717683b..3c8c62d 100644
@@ -82,7 +82,7 @@ grub_install_SOURCES = util/i386/efi/grub-install.in
 # Modules.
 pkglib_MODULES = kernel.mod normal.mod chain.mod appleldr.mod \
 	linux.mod halt.mod reboot.mod pci.mod lspci.mod \
-	datetime.mod date.mod datehook.mod loadbios.mod fixvideo.mod
+	datetime.mod date.mod datehook.mod loadbios.mod fixvideo.mod mmap.mod
 
 # For kernel.mod.
 kernel_mod_EXPORTS = no
@@ -135,6 +135,12 @@ normal_mod_CFLAGS = $(COMMON_CFLAGS)
 normal_mod_ASFLAGS = $(COMMON_ASFLAGS)
 normal_mod_LDFLAGS = $(COMMON_LDFLAGS)
 
+# For mmap.mod.
+mmap_mod_SOURCES = mmap/mmap.c mmap/i386/uppermem.c mmap/i386/mmap.c \
+		   mmap/efi/mmap.c
+mmap_mod_CFLAGS = $(COMMON_CFLAGS)
+mmap_mod_LDFLAGS = $(COMMON_LDFLAGS)
+
 # For chain.mod.
 chain_mod_SOURCES = loader/efi/chainloader.c
 chain_mod_CFLAGS = $(COMMON_CFLAGS)
conf/i386-ieee1275.rmk +7 -1
index cb32c73..08fdd98 100644
@@ -104,7 +104,13 @@ grub_install_SOURCES = util/ieee1275/grub-install.in
 pkglib_MODULES = normal.mod halt.mod reboot.mod suspend.mod		\
 	multiboot.mod aout.mod serial.mod linux.mod	\
 	nand.mod memdisk.mod pci.mod lspci.mod datetime.mod	\
-	date.mod datehook.mod lsmmap.mod
+	date.mod datehook.mod lsmmap.mod mmap.mod
+
+# For mmap.mod.
+mmap_mod_SOURCES = mmap/mmap.c mmap/i386/uppermem.c mmap/i386/mmap.c
+mmap_mod_CFLAGS = $(COMMON_CFLAGS)
+mmap_mod_LDFLAGS = $(COMMON_LDFLAGS)
+mmap_mod_ASFLAGS = $(COMMON_ASFLAGS)
 
 #
 # Only arch dependant part of normal.mod will be here. Common part for
conf/i386-pc.rmk +9 -1
index d74a5fa..709741a 100644
@@ -182,7 +182,15 @@ pkglib_MODULES = biosdisk.mod chain.mod normal.mod \
 	ata.mod vga.mod memdisk.mod pci.mod lspci.mod	\
 	aout.mod bsd.mod pxe.mod pxecmd.mod datetime.mod date.mod \
 	datehook.mod lsmmap.mod ata_pthru.mod hdparm.mod \
-	usb.mod uhci.mod ohci.mod usbtest.mod usbms.mod usb_keyboard.mod
+	usb.mod uhci.mod ohci.mod usbtest.mod usbms.mod usb_keyboard.mod \
+	mmap.mod
+
+# For mmap.mod.
+mmap_mod_SOURCES = mmap/mmap.c mmap/i386/uppermem.c mmap/i386/mmap.c \
+		   mmap/i386/pc/mmap.c mmap/i386/pc/mmap_helper.S
+mmap_mod_CFLAGS = $(COMMON_CFLAGS)
+mmap_mod_LDFLAGS = $(COMMON_LDFLAGS)
+mmap_mod_ASFLAGS = $(COMMON_ASFLAGS)
 
 # For biosdisk.mod.
 biosdisk_mod_SOURCES = disk/i386/pc/biosdisk.c
conf/x86_64-efi.rmk +7 -1
index d7ad9a4..d19649b 100644
@@ -83,7 +83,7 @@ grub_install_SOURCES = util/i386/efi/grub-install.in
 # Modules.
 pkglib_MODULES = kernel.mod normal.mod chain.mod appleldr.mod \
 	halt.mod reboot.mod linux.mod pci.mod lspci.mod \
-	datetime.mod date.mod datehook.mod loadbios.mod fixvideo.mod
+	datetime.mod date.mod datehook.mod loadbios.mod fixvideo.mod mmap.mod
 
 # For kernel.mod.
 kernel_mod_EXPORTS = no
@@ -137,6 +137,12 @@ normal_mod_CFLAGS = $(COMMON_CFLAGS)
 normal_mod_ASFLAGS = $(COMMON_ASFLAGS)
 normal_mod_LDFLAGS = $(COMMON_LDFLAGS)
 
+# For mmap.mod.
+mmap_mod_SOURCES = mmap/mmap.c mmap/i386/uppermem.c mmap/i386/mmap.c \
+		   mmap/efi/mmap.c
+mmap_mod_CFLAGS = $(COMMON_CFLAGS)
+mmap_mod_LDFLAGS = $(COMMON_LDFLAGS)
+
 # For chain.mod.
 chain_mod_SOURCES = loader/efi/chainloader.c
 chain_mod_CFLAGS = $(COMMON_CFLAGS)
include/grub/efi/memory.h new file +48 -0

48 lines not on this page · raw message

include/grub/i386/bsd.h +2 -0
index f50f18e..78fae79 100644
@@ -148,6 +148,8 @@ struct grub_openbsd_bios_mmap
 {
   grub_uint64_t addr;
   grub_uint64_t len;
+#define	OPENBSD_MMAP_AVAILABLE	1
+#define	OPENBSD_MMAP_RESERVED 2
   grub_uint32_t type;
 };
 
include/grub/i386/efi/memory.h new file +1 -0
new file mode 100644
index 0000000..c9a61bb
@@ -0,0 +1 @@
+#include <grub/efi/memory.h>
include/grub/i386/pc/memory.h +30 -6
index 08e92a9..d8d4342 100644
@@ -25,6 +25,7 @@
 #ifndef ASM_FILE
 #include <grub/types.h>
 #include <grub/err.h>
+#include <grub/memory.h>
 #endif
 
 /* The scratch buffer used in real mode code.  */
@@ -79,12 +80,6 @@
 
 #ifndef ASM_FILE
 
-#ifndef GRUB_MACHINE_IEEE1275
-extern grub_size_t EXPORT_VAR(grub_lower_mem);
-#endif
-
-extern grub_size_t EXPORT_VAR(grub_upper_mem);
-
 struct grub_machine_mmap_entry
 {
   grub_uint32_t size;
@@ -92,12 +87,41 @@ struct grub_machine_mmap_entry
   grub_uint64_t len;
 #define GRUB_MACHINE_MEMORY_AVAILABLE	1
 #define GRUB_MACHINE_MEMORY_RESERVED	2
+#define GRUB_MACHINE_MEMORY_ACPI	3
+#define GRUB_MACHINE_MEMORY_NVS 	4
+#define GRUB_MACHINE_MEMORY_MAX_TYPE 	4
+  /* This one is special: it's used internally but is never reported 
+     by firmware. */
+#define GRUB_MACHINE_MEMORY_HOLE 	5
+
   grub_uint32_t type;
 } __attribute__((packed));
 
 grub_err_t EXPORT_FUNC(grub_machine_mmap_iterate)
      (int NESTED_FUNC_ATTR (*hook) (grub_uint64_t, grub_uint64_t, grub_uint32_t));
 
+grub_uint64_t grub_mmap_get_post64 (void);
+grub_uint64_t grub_mmap_get_upper (void);
+grub_uint64_t grub_mmap_get_lower (void);
+
+#ifdef GRUB_MACHINE_PCBIOS
+grub_err_t grub_machine_mmap_register (grub_uint64_t start, grub_uint64_t size, 
+				       int type, int handle);
+grub_err_t grub_machine_mmap_unregister (int handle);
+#define GRUB_MMAP_MALLOC_LOW 1
+#else
+static inline grub_err_t grub_machine_mmap_register (grub_uint64_t start, 
+					      grub_uint64_t size, 
+					      int type, int handle)
+{
+  return GRUB_ERR_NONE;
+}
+static inline grub_err_t grub_machine_mmap_unregister (int handle)
+{
+  return GRUB_ERR_NONE;
+}
+#endif
+
 #endif
 
 #endif /* ! GRUB_MEMORY_MACHINE_HEADER */
include/grub/memory.h new file +52 -0

52 lines not on this page · raw message

include/grub/types.h +10 -0
index 50f8f58..114b456 100644
@@ -102,6 +102,16 @@ typedef grub_int32_t	grub_ssize_t;
 # define GRUB_LONG_MIN -2147483648UL
 #endif
 
+#if GRUB_CPU_SIZEOF_VOID_P == 4
+#define UINT_TO_PTR(x) ((void*)(grub_uint32_t)(x))
+#define PTR_TO_UINT64(x) ((grub_uint64_t)(grub_uint32_t)(x))
+#define PTR_TO_UINT32(x) ((grub_uint32_t)(x))
+#else
+#define UINT_TO_PTR(x) ((void*)(grub_uint64_t)(x))
+#define PTR_TO_UINT64(x) ((grub_uint64_t)(x))
+#define PTR_TO_UINT32(x) ((grub_uint32_t)(grub_uint64_t)(x))
+#endif
+
 /* The type for representing a file offset.  */
 typedef grub_uint64_t	grub_off_t;
 
include/grub/x86_64/efi/memory.h new file +1 -0
new file mode 100644
index 0000000..c9a61bb
@@ -0,0 +1 @@
+#include <grub/efi/memory.h>
kern/i386/coreboot/init.c +2 -10
index fdaf6fd..7bc4291 100644
@@ -82,16 +82,11 @@ grub_machine_init (void)
 #if GRUB_CPU_SIZEOF_VOID_P == 4
     /* Restrict ourselves to 32-bit memory space.  */
     if (addr > GRUB_ULONG_MAX)
-      {
-	grub_upper_mem = GRUB_ULONG_MAX;
-	return 0;
-      }
-    if (addr + size > GRUB_ULONG_MAX)
+      return 0;
+    if (addr + size > ULONG_MAX)
       size = GRUB_ULONG_MAX - addr;
 #endif
 
-    grub_upper_mem = grub_max (grub_upper_mem, addr + size);
-
     if (type != GRUB_MACHINE_MEMORY_AVAILABLE)
       return 0;
 
@@ -128,9 +123,6 @@ grub_machine_init (void)
   grub_machine_mmap_init ();
   grub_machine_mmap_iterate (heap_init);
 
-  /* This variable indicates size, not offset.  */
-  grub_upper_mem -= GRUB_MEMORY_MACHINE_UPPER_START;
-
   grub_tsc_init ();
 }
 
kern/i386/pc/init.c +1 -2
index 6191412..c64497e 100644
@@ -45,7 +45,6 @@ static int num_regions;
 
 grub_addr_t grub_os_area_addr;
 grub_size_t grub_os_area_size;
-grub_size_t grub_lower_mem, grub_upper_mem;
 
 void 
 grub_arch_sync_caches (void *address __attribute__ ((unused)),
@@ -133,6 +132,7 @@ void
 grub_machine_init (void)
 {
   int i;
+  int grub_lower_mem;
   
   /* Initialize the console as early as possible.  */
   grub_console_init ();
@@ -197,7 +197,6 @@ grub_machine_init (void)
       {
 	grub_size_t quarter = mem_regions[i].size >> 2;
 
-	grub_upper_mem = mem_regions[i].size;
 	grub_os_area_addr = mem_regions[i].addr;
 	grub_os_area_size = mem_regions[i].size - quarter;
 	grub_mm_init_region ((void *) (grub_os_area_addr + grub_os_area_size),
loader/i386/bsd.c +27 -19

71 lines not on this page · raw message

loader/i386/efi/linux.c +33 -64

130 lines not on this page · raw message

loader/i386/ieee1275/linux.c +1 -1
index 2f52880..a563577 100644
@@ -100,7 +100,7 @@ grub_linux_boot (void)
 
   grub_memset ((char *) params, 0, GRUB_OFW_LINUX_CL_OFFSET);
 
-  params->alt_mem = grub_upper_mem >> 10;
+  params->alt_mem = grub_mmap_get_upper () >> 10;
   params->ext_mem = params->alt_mem;
 
   lh->cmd_line_ptr = (char *)
loader/i386/linux.c +25 -3
index 19444f4..a72871b 100644
@@ -168,7 +168,7 @@ find_mmap_size (void)
       return 0;
     }
   
-  grub_machine_mmap_iterate (hook);
+  grub_mmap_iterate (hook);
   
   mmap_size = count * sizeof (struct grub_e820_mmap);
 
@@ -237,7 +237,7 @@ allocate_pages (grub_size_t prot_size)
 
       return 0;
     }
-  grub_machine_mmap_iterate (hook);
+  grub_mmap_iterate (hook);
   if (! real_mode_mem)
     {
       grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate real mode pages");
@@ -396,6 +396,27 @@ grub_linux_boot (void)
 				addr, size, GRUB_E820_RAM);
 	  break;
 
+#ifdef GRUB_MACHINE_MEMORY_ACPI
+        case GRUB_MACHINE_MEMORY_ACPI:
+	  grub_e820_add_region (params->e820_map, &e820_num,
+				addr, size, GRUB_E820_ACPI);
+	  break;
+#endif
+
+#ifdef GRUB_MACHINE_MEMORY_NVS
+        case GRUB_MACHINE_MEMORY_NVS:
+	  grub_e820_add_region (params->e820_map, &e820_num,
+				addr, size, GRUB_E820_NVS);
+	  break;
+#endif
+
+#ifdef GRUB_MACHINE_MEMORY_CODE
+        case GRUB_MACHINE_MEMORY_CODE:
+	  grub_e820_add_region (params->e820_map, &e820_num,
+				addr, size, GRUB_E820_EXEC_CODE);
+	  break;
+#endif
+
         default:
           grub_e820_add_region (params->e820_map, &e820_num,
                                 addr, size, GRUB_E820_RESERVED);
@@ -404,9 +425,10 @@ grub_linux_boot (void)
     }
 
   e820_num = 0;
-  grub_machine_mmap_iterate (hook);
+  grub_mmap_iterate (hook);
   params->mmap_size = e820_num;
 
+
   /* Hardware interrupts are not safe any longer.  */
   asm volatile ("cli" : : );
   
loader/i386/multiboot.c +4 -4
index 27042a5..d38590f 100644
@@ -100,7 +100,7 @@ grub_get_multiboot_mmap_len (void)
       return 0;
     }
   
-  grub_machine_mmap_iterate (hook);
+  grub_mmap_iterate (hook);
   
   return count * sizeof (struct grub_multiboot_mmap_entry);
 }
@@ -123,7 +123,7 @@ grub_fill_multiboot_mmap (struct grub_multiboot_mmap_entry *first_entry)
       return 0;
     }
 
-  grub_machine_mmap_iterate (hook);
+  grub_mmap_iterate (hook);
 }
 
 #define MULTIBOOT_LOAD_ELF64
@@ -342,8 +342,8 @@ grub_multiboot (int argc, char *argv[])
 		grub_multiboot_payload_entry_offset);
 
   /* Convert from bytes to kilobytes.  */
-  mbi->mem_lower = grub_lower_mem / 1024;
-  mbi->mem_upper = grub_upper_mem / 1024;
+  mbi->mem_lower = grub_mmap_get_lower () / 1024;
+  mbi->mem_upper = grub_mmap_get_upper () / 1024;
   mbi->flags |= MULTIBOOT_INFO_MEMORY;
 
   cmdline = p = cmdline_addr (grub_multiboot_payload_orig);
loader/i386/pc/linux.c +6 -5
index 8ff97f4..34a4ebc 100644
@@ -108,8 +108,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
       lh.type_of_loader = GRUB_LINUX_BOOT_LOADER_TYPE;
       
       /* Put the real mode part at as a high location as possible.  */
-      grub_linux_real_addr = (char *) (grub_lower_mem
-				       - GRUB_LINUX_SETUP_MOVE_SIZE);
+      grub_linux_real_addr 
+	= (char *) UINT_TO_PTR (grub_mmap_get_lower ()
+				- GRUB_LINUX_SETUP_MOVE_SIZE);
       /* But it must not exceed the traditional area.  */
       if (grub_linux_real_addr > (char *) GRUB_LINUX_OLD_REAL_MODE_ADDR)
 	grub_linux_real_addr = (char *) GRUB_LINUX_OLD_REAL_MODE_ADDR;
@@ -159,12 +160,12 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
     }
   
   if (grub_linux_real_addr + GRUB_LINUX_SETUP_MOVE_SIZE
-      > (char *) grub_lower_mem)
+      > (char *) UINT_TO_PTR (grub_mmap_get_lower ()))
     {
       grub_error (GRUB_ERR_OUT_OF_RANGE,
 		 "too small lower memory (0x%x > 0x%x)",
-		 grub_linux_real_addr + GRUB_LINUX_SETUP_MOVE_SIZE,
-		 (char *) grub_lower_mem);
+		  grub_linux_real_addr + GRUB_LINUX_SETUP_MOVE_SIZE,
+		  (int) grub_mmap_get_lower ());
       goto fail;
     }
 
mmap/efi/mmap.c new file +284 -0

284 lines not on this page · raw message

mmap/i386/mmap.c new file +98 -0

98 lines not on this page · raw message

mmap/i386/pc/mmap.c new file +216 -0

216 lines not on this page · raw message

mmap/i386/pc/mmap_helper.S new file +122 -0

122 lines not on this page · raw message

mmap/i386/uppermem.c new file +85 -0

85 lines not on this page · raw message

mmap/mmap.c new file +426 -0

426 lines not on this page · raw message

#3 lore raw

From: "Vladimir 'phcoder' Serbinenko" <[email protected]>
Date: Thu, 30 Apr 2009 15:36:36 +0200

Quoted 1 line

Rediffed and reposted due to request by David Miller

I've looked these over and I'm completely OK with these changes as long as at least basic testing occurs for the BIOS, ACPI, and EFI cases that are effected by this change.