qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Alexander Graf <agraf@suse.de>
To: qemu-devel qemu-devel <qemu-devel@nongnu.org>
Cc: "Blue Swirl" <blauwirbel@gmail.com>,
	"qemu-ppc@nongnu.org list:PowerPC" <qemu-ppc@nongnu.org>,
	"Fabien Chouteau" <chouteau@adacore.com>,
	"Aurélien Jarno" <aurelien@aurel32.net>
Subject: [Qemu-devel] [PATCH 56/66] Save memory allocation in the elf loader
Date: Fri,  8 Mar 2013 21:06:58 +0100	[thread overview]
Message-ID: <1362773228-1747-57-git-send-email-agraf@suse.de> (raw)
In-Reply-To: <1362773228-1747-1-git-send-email-agraf@suse.de>

From: Fabien Chouteau <chouteau@adacore.com>

The current elf loader uses too much memory. For example, I have a
executable with a bss section of 400 MB and I set the ram size to 512
MB. Qemu uses about 780MB of RAM (which is fine), but there's a peak at
1.6 GB during initialization (this is not fine).

This patch fixes two things:
 1) do not allocate each elf program twice.
 2) do not allocate memory for areas that are only zeros.

For this we need a new field in Rom: "datasize" which is the size of the
allocated data. If datasize is less than romsize, it means that the area
from datasize to romsize is filled with zeros.

Signed-off-by: Fabien Chouteau <chouteau@adacore.com>
Signed-off-by: Alexander Graf <agraf@suse.de>
---
 hw/elf_ops.h |   19 ++++++++------
 hw/loader.c  |   75 +++++++++++++++++++++++++++++++++++++++++++++++----------
 hw/loader.h  |    2 +
 3 files changed, 75 insertions(+), 21 deletions(-)

diff --git a/hw/elf_ops.h b/hw/elf_ops.h
index 531a425..acc701e 100644
--- a/hw/elf_ops.h
+++ b/hw/elf_ops.h
@@ -197,7 +197,7 @@ static int glue(load_elf, SZ)(const char *name, int fd,
     struct elfhdr ehdr;
     struct elf_phdr *phdr = NULL, *ph;
     int size, i, total_size;
-    elf_word mem_size;
+    elf_word mem_size, file_size;
     uint64_t addr, low = (uint64_t)-1, high = 0;
     uint8_t *data = NULL;
     char label[128];
@@ -252,14 +252,16 @@ static int glue(load_elf, SZ)(const char *name, int fd,
     for(i = 0; i < ehdr.e_phnum; i++) {
         ph = &phdr[i];
         if (ph->p_type == PT_LOAD) {
-            mem_size = ph->p_memsz;
-            /* XXX: avoid allocating */
-            data = g_malloc0(mem_size);
+            mem_size = ph->p_memsz; /* Size of the ROM */
+            file_size = ph->p_filesz; /* Size of the allocated data */
+            data = g_malloc0(file_size);
             if (ph->p_filesz > 0) {
-                if (lseek(fd, ph->p_offset, SEEK_SET) < 0)
+                if (lseek(fd, ph->p_offset, SEEK_SET) < 0) {
                     goto fail;
-                if (read(fd, data, ph->p_filesz) != ph->p_filesz)
+                }
+                if (read(fd, data, file_size) != file_size) {
                     goto fail;
+                }
             }
             /* address_offset is hack for kernel images that are
                linked at the wrong physical address.  */
@@ -281,7 +283,9 @@ static int glue(load_elf, SZ)(const char *name, int fd,
             }
 
             snprintf(label, sizeof(label), "phdr #%d: %s", i, name);
-            rom_add_blob_fixed(label, data, mem_size, addr);
+
+            /* rom_add_elf_program() seize the ownership of 'data' */
+            rom_add_elf_program(label, data, file_size, mem_size, addr);
 
             total_size += mem_size;
             if (addr < low)
@@ -289,7 +293,6 @@ static int glue(load_elf, SZ)(const char *name, int fd,
             if ((addr + mem_size) > high)
                 high = addr + mem_size;
 
-            g_free(data);
             data = NULL;
         }
     }
diff --git a/hw/loader.c b/hw/loader.c
index 995edc3..bd2b52d 100644
--- a/hw/loader.c
+++ b/hw/loader.c
@@ -533,7 +533,14 @@ typedef struct Rom Rom;
 struct Rom {
     char *name;
     char *path;
+
+    /* datasize is the amount of memory allocated in "data". If datasize is less
+     * than romsize, it means that the area from datasize to romsize is filled
+     * with zeros.
+     */
     size_t romsize;
+    size_t datasize;
+
     uint8_t *data;
     int isrom;
     char *fw_dir;
@@ -589,14 +596,15 @@ int rom_add_file(const char *file, const char *fw_dir,
         rom->fw_dir  = g_strdup(fw_dir);
         rom->fw_file = g_strdup(file);
     }
-    rom->addr    = addr;
-    rom->romsize = lseek(fd, 0, SEEK_END);
-    rom->data    = g_malloc0(rom->romsize);
+    rom->addr     = addr;
+    rom->romsize  = lseek(fd, 0, SEEK_END);
+    rom->datasize = rom->romsize;
+    rom->data     = g_malloc0(rom->datasize);
     lseek(fd, 0, SEEK_SET);
-    rc = read(fd, rom->data, rom->romsize);
-    if (rc != rom->romsize) {
+    rc = read(fd, rom->data, rom->datasize);
+    if (rc != rom->datasize) {
         fprintf(stderr, "rom: file %-20s: read error: rc=%d (expected %zd)\n",
-                rom->name, rc, rom->romsize);
+                rom->name, rc, rom->datasize);
         goto err;
     }
     close(fd);
@@ -637,16 +645,37 @@ int rom_add_blob(const char *name, const void *blob, size_t len,
 {
     Rom *rom;
 
-    rom = g_malloc0(sizeof(*rom));
-    rom->name    = g_strdup(name);
-    rom->addr    = addr;
-    rom->romsize = len;
-    rom->data    = g_malloc0(rom->romsize);
+    rom           = g_malloc0(sizeof(*rom));
+    rom->name     = g_strdup(name);
+    rom->addr     = addr;
+    rom->romsize  = len;
+    rom->datasize = len;
+    rom->data     = g_malloc0(rom->datasize);
     memcpy(rom->data, blob, len);
     rom_insert(rom);
     return 0;
 }
 
+/* This function is specific for elf program because we don't need to allocate
+ * all the rom. We just allocate the first part and the rest is just zeros. This
+ * is why romsize and datasize are different. Also, this function seize the
+ * memory ownership of "data", so we don't have to allocate and copy the buffer.
+ */
+int rom_add_elf_program(const char *name, void *data, size_t datasize,
+                        size_t romsize, hwaddr addr)
+{
+    Rom *rom;
+
+    rom           = g_malloc0(sizeof(*rom));
+    rom->name     = g_strdup(name);
+    rom->addr     = addr;
+    rom->datasize = datasize;
+    rom->romsize  = romsize;
+    rom->data     = data;
+    rom_insert(rom);
+    return 0;
+}
+
 int rom_add_vga(const char *file)
 {
     return rom_add_file(file, "vgaroms", 0, -1);
@@ -668,7 +697,7 @@ static void rom_reset(void *unused)
         if (rom->data == NULL) {
             continue;
         }
-        cpu_physical_memory_write_rom(rom->addr, rom->data, rom->romsize);
+        cpu_physical_memory_write_rom(rom->addr, rom->data, rom->datasize);
         if (rom->isrom) {
             /* rom needs to be written only once */
             g_free(rom->data);
@@ -756,13 +785,33 @@ int rom_copy(uint8_t *dest, hwaddr addr, size_t size)
 
         d = dest + (rom->addr - addr);
         s = rom->data;
-        l = rom->romsize;
+        l = rom->datasize;
 
         if ((d + l) > (dest + size)) {
             l = dest - d;
         }
 
         memcpy(d, s, l);
+
+        if (rom->romsize > rom->datasize) {
+            /* If datasize is less than romsize, it means that we didn't
+             * allocate all the ROM because the trailing data are only zeros.
+             */
+
+            d += l;
+            l = rom->romsize - rom->datasize;
+
+            if ((d + l) > (dest + size)) {
+                /* Rom size doesn't fit in the destination area. Adjust to avoid
+                 * overflow.
+                 */
+                l = dest - d;
+            }
+
+            if (l > 0) {
+                memset(d, 0x0, l);
+            }
+        }
     }
 
     return (d + l) - dest;
diff --git a/hw/loader.h b/hw/loader.h
index 5e61c95..0958f06 100644
--- a/hw/loader.h
+++ b/hw/loader.h
@@ -27,6 +27,8 @@ int rom_add_file(const char *file, const char *fw_dir,
                  hwaddr addr, int32_t bootindex);
 int rom_add_blob(const char *name, const void *blob, size_t len,
                  hwaddr addr);
+int rom_add_elf_program(const char *name, void *data, size_t datasize,
+                        size_t romsize, hwaddr addr);
 int rom_load_all(void);
 void rom_set_fw(void *f);
 int rom_copy(uint8_t *dest, hwaddr addr, size_t size);
-- 
1.6.0.2

  parent reply	other threads:[~2013-03-08 20:08 UTC|newest]

Thread overview: 68+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-03-08 20:06 [Qemu-devel] [PULL 00/66] ppc patch queue 2013-03-08 Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 01/66] pseries: Add cleanup hook for PAPR virtual LAN device Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 02/66] target-ppc: Fix CPU_POWERPC_MPC8547E Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 03/66] target-ppc: Fix "G2leGP3" PVR Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 04/66] target-ppc: Update error handling in ppc_cpu_realize() Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 05/66] target-ppc: Drop nested TARGET_PPC64 guard for POWER7 Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 06/66] target-ppc: Inline comma into POWERPC_DEF_SVR() macro Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 07/66] target-ppc: Extract aliases from definitions list Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 08/66] target-ppc: Make -cpu "ppc" an alias to "ppc32" Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 09/66] target-ppc: Extract MPC5xx aliases Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 10/66] target-ppc: Extract MGT823/MPC8xx as aliases Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 11/66] target-ppc: Extract 40x aliases Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 12/66] target-ppc: Extract 440 aliases Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 13/66] target-ppc: Turn "ppc32" and "ppc64" CPUs into aliases Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 14/66] target-ppc: Extract 74x7[A] aliases Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 15/66] target-ppc: Extract 74x5 as aliases Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 16/66] target-ppc: Extract 74x1 aliases Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 17/66] target-ppc: Extract 7450 alias Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 18/66] target-ppc: Extract 7448 alias Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 19/66] target-ppc: Extract 7410 alias Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 20/66] target-ppc: Extract 7400 alias Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 21/66] target-ppc: Extract 7x5 aliases Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 22/66] target-ppc: Extract 750 aliases Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 23/66] target-ppc: Extract 740/750 aliases Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 24/66] target-ppc: Extract 603e alias Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 25/66] target-ppc: Extract 603r alias Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 26/66] target-ppc: Extract 601/601v aliases Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 27/66] target-ppc: Extract 604e alias Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 28/66] target-ppc: Extract MPC85xx aliases Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 29/66] target-ppc: Extract e500v1/e500v2 aliases Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 30/66] target-ppc: Extract MPC83xx aliases Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 31/66] target-ppc: Extract e300 alias Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 32/66] target-ppc: Extract e200 alias Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 33/66] target-ppc: Extract MPC82xx alias Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 34/66] target-ppc: Extract MPC8247/MPC8248/MPC8270-80 aliases Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 35/66] target-ppc: Extract MPC82xx aliases to *_HiP4 Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 36/66] target-ppc: Extract MPC82xx_HiP{3, 4} aliases Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 37/66] target-ppc: Extract MPC52xx alias Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 38/66] target-ppc: Extract MPC5200/MPC5200B aliases Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 39/66] target-ppc: Extract MPC8240 alias Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 40/66] target-ppc: Extract 405GPe alias Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 41/66] target-ppc: Extract 970 aliases Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 42/66] target-ppc: Extract POWER7 alias Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 43/66] target-ppc: Get model name from type name Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 44/66] target-ppc: Convert CPU definitions Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 45/66] target-ppc: Introduce abstract CPU family types Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 46/66] target-ppc: Set instruction flags on CPU family classes Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 47/66] target-ppc: Register all types for TARGET_PPCEMB Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 48/66] target-ppc: Set remaining fields on CPU family classes Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 49/66] target-ppc: Turn descriptive CPU family comments into device descriptions Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 50/66] target-ppc: Turn descriptive CPU model " Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 51/66] target-ppc: Update Coding Style for CPU models Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 52/66] target-ppc: Split model definitions out of translate_init.c Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 53/66] target-ppc: Fix remaining microcontroller typos among models Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 54/66] target-ppc: Change "POWER7" CPU alias Alexander Graf
2013-03-08 20:06 ` [Qemu-devel] [PATCH 55/66] pseries: Implement h_read hcall Alexander Graf
2013-03-08 20:06 ` Alexander Graf [this message]
2013-03-08 20:06 ` [Qemu-devel] [PATCH 57/66] target-ppc: Add mechanism for synchronizing SPRs with KVM Alexander Graf
2013-03-08 20:07 ` [Qemu-devel] [PATCH 58/66] target-ppc: Synchronize FPU state " Alexander Graf
2013-03-08 20:07 ` [Qemu-devel] [PATCH 59/66] target-ppc: Fix PPC_DUMP_SPR_ACCESS build Alexander Graf
2013-03-08 20:07 ` [Qemu-devel] [PATCH 60/66] PPC: Fix dma interrupt Alexander Graf
2013-03-08 20:07 ` [Qemu-devel] [PATCH 61/66] PPC: xnu kernel expects FLUSH to be cleared on STOP Alexander Graf
2013-03-08 20:07 ` [Qemu-devel] [PATCH 62/66] target-ppc: Make host CPU a subclass of the host's CPU model Alexander Graf
2013-03-08 20:07 ` [Qemu-devel] [PATCH 63/66] target-ppc: List alias names alongside CPU models Alexander Graf
2013-03-08 20:07 ` [Qemu-devel] [PATCH 64/66] target-ppc: Report CPU aliases for QMP Alexander Graf
2013-03-08 20:07 ` [Qemu-devel] [PATCH 65/66] target-ppc: Move CPU aliases out of translate_init.c Alexander Graf
2013-03-08 20:07 ` [Qemu-devel] [PATCH 66/66] pseries: Add compatible property to root of device tree Alexander Graf
2013-03-09 12:09 ` [Qemu-devel] [PULL 00/66] ppc patch queue 2013-03-08 Blue Swirl

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1362773228-1747-57-git-send-email-agraf@suse.de \
    --to=agraf@suse.de \
    --cc=aurelien@aurel32.net \
    --cc=blauwirbel@gmail.com \
    --cc=chouteau@adacore.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-ppc@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).