All of lore.kernel.org
 help / color / mirror / Atom feed
From: Eduard - Gabriel Munteanu <eduard.munteanu@linux360.ro>
To: joro@8bytes.org
Cc: aliguori@us.ibm.com, avi@redhat.com, qemu-devel@nongnu.org,
	kvm@vger.kernel.org,
	Eduard - Gabriel Munteanu <eduard.munteanu@linux360.ro>
Subject: [RFC PATCH 2/7] acpi: split and rename acpi_table_add()
Date: Tue, 30 Mar 2010 11:20:03 +0300	[thread overview]
Message-ID: <972d931f99c27de91d043b1332eb564e6b4260db.1269936879.git.eduard.munteanu@linux360.ro> (raw)
In-Reply-To: <cover.1269936879.git.eduard.munteanu@linux360.ro>

We'd like to let emulation code build and insert ACPI tables at bootup,
without depending on hacking the BIOS code. This will be used to provide
an IVRS table for emulating the AMD IOMMU, for instance.

This splits acpi_table_add(), retaining the old behavior of inserting
cmdline-supplied tables under the name of acpi_table_cmdline_add(). The
other two resulting functions can be used for the aforementioned
purpose.

Signed-off-by: Eduard - Gabriel Munteanu <eduard.munteanu@linux360.ro>
---
 hw/acpi.c |   64 ++++++++++++++++++++++++++++++++++++++++---------------------
 hw/pc.h   |    4 ++-
 vl.c      |    2 +-
 3 files changed, 46 insertions(+), 24 deletions(-)

diff --git a/hw/acpi.c b/hw/acpi.c
index 7c4e8d3..8eb53da 100644
--- a/hw/acpi.c
+++ b/hw/acpi.c
@@ -840,7 +840,7 @@ struct acpi_table_header
 } __attribute__((packed));
 
 char *acpi_tables;
-size_t acpi_tables_len;
+size_t acpi_tables_len, acpi_tables_prev_len;
 
 static int acpi_checksum(const uint8_t *data, int len)
 {
@@ -851,13 +851,44 @@ static int acpi_checksum(const uint8_t *data, int len)
     return (-sum) & 0xff;
 }
 
-int acpi_table_add(const char *t)
+void *acpi_alloc_table(size_t size)
+{
+    void *ptr;
+
+    if (!acpi_tables) {
+        acpi_tables_len = sizeof(uint16_t);
+        acpi_tables = qemu_mallocz(acpi_tables_len);
+    }
+    acpi_tables_prev_len = acpi_tables_len;
+    acpi_tables_len += sizeof(uint16_t) + size;
+    acpi_tables = qemu_realloc(acpi_tables, acpi_tables_len);
+    ptr = acpi_tables + acpi_tables_prev_len;
+
+    *(uint16_t *) ptr = size;
+
+    return ptr + sizeof(uint16_t);
+}
+
+void acpi_commit_table(void *buf)
+{
+    struct acpi_table_header *acpi_hdr = buf;
+    size_t size = acpi_tables_len - acpi_tables_prev_len - sizeof(uint16_t);
+
+    acpi_hdr->length = cpu_to_le32(size);
+    acpi_hdr->checksum = acpi_checksum(buf, size);
+
+    /* increase number of tables */
+    (*(uint16_t *) acpi_tables) =
+	    cpu_to_le32(le32_to_cpu(*(uint16_t *) acpi_tables) + 1);
+}
+
+int acpi_table_cmdline_add(const char *t)
 {
     static const char *dfl_id = "QEMUQEMU";
     char buf[1024], *p, *f;
     struct acpi_table_header acpi_hdr;
     unsigned long val;
-    size_t newlen, off;
+    size_t size, off;
 
     memset(&acpi_hdr, 0, sizeof(acpi_hdr));
   
@@ -915,7 +946,7 @@ int acpi_table_add(const char *t)
          buf[0] = '\0';
     }
 
-    acpi_hdr.length = sizeof(acpi_hdr);
+    size = sizeof(acpi_hdr);
 
     f = buf;
     while (buf[0]) {
@@ -927,27 +958,17 @@ int acpi_table_add(const char *t)
             fprintf(stderr, "Can't stat file '%s': %s\n", f, strerror(errno));
             goto out;
         }
-        acpi_hdr.length += s.st_size;
+        size += s.st_size;
         if (!n)
             break;
         *n = ':';
         f = n + 1;
     }
 
-    if (!acpi_tables) {
-        acpi_tables_len = sizeof(uint16_t);
-        acpi_tables = qemu_mallocz(acpi_tables_len);
-    }
-    newlen = acpi_tables_len + sizeof(uint16_t) + acpi_hdr.length;
-    acpi_tables = qemu_realloc(acpi_tables, newlen);
-    p = acpi_tables + acpi_tables_len;
-    acpi_tables_len = newlen;
+    p = acpi_alloc_table(size);
+    off = sizeof(struct acpi_table_header);
 
-    acpi_hdr.length = cpu_to_le32(acpi_hdr.length);
-    *(uint16_t*)p = acpi_hdr.length;
-    p += sizeof(uint16_t);
-    memcpy(p, &acpi_hdr, sizeof(acpi_hdr));
-    off = sizeof(acpi_hdr);
+    memcpy(p, &acpi_hdr, off);
 
     f = buf;
     while (buf[0]) {
@@ -983,10 +1004,8 @@ int acpi_table_add(const char *t)
         f = n + 1;
     }
 
-    ((struct acpi_table_header*)p)->checksum = acpi_checksum((uint8_t*)p, off);
-    /* increase number of tables */
-    (*(uint16_t*)acpi_tables) =
-	    cpu_to_le32(le32_to_cpu(*(uint16_t*)acpi_tables) + 1);
+    acpi_commit_table(p);
+
     return 0;
 out:
     if (acpi_tables) {
@@ -995,3 +1014,4 @@ out:
     }
     return -1;
 }
+
diff --git a/hw/pc.h b/hw/pc.h
index b599564..0cef140 100644
--- a/hw/pc.h
+++ b/hw/pc.h
@@ -108,7 +108,9 @@ extern char *acpi_tables;
 extern size_t acpi_tables_len;
 
 void acpi_bios_init(void);
-int acpi_table_add(const char *table_desc);
+void *acpi_alloc_table(size_t size);
+void acpi_commit_table(void *buf);
+int acpi_table_cmdline_add(const char *table_desc);
 
 /* acpi_piix.c */
 i2c_bus *piix4_pm_init(PCIBus *bus, int devfn, uint32_t smb_io_base,
diff --git a/vl.c b/vl.c
index d959fdb..0efba90 100644
--- a/vl.c
+++ b/vl.c
@@ -5492,7 +5492,7 @@ int main(int argc, char **argv, char **envp)
                 rtc_td_hack = 1;
                 break;
             case QEMU_OPTION_acpitable:
-                if(acpi_table_add(optarg) < 0) {
+                if(acpi_table_cmdline_add(optarg) < 0) {
                     fprintf(stderr, "Wrong acpi table provided\n");
                     exit(1);
                 }
-- 
1.6.4.4


WARNING: multiple messages have this Message-ID (diff)
From: Eduard - Gabriel Munteanu <eduard.munteanu@linux360.ro>
To: joro@8bytes.org
Cc: aliguori@us.ibm.com,
	Eduard - Gabriel Munteanu <eduard.munteanu@linux360.ro>,
	avi@redhat.com, kvm@vger.kernel.org, qemu-devel@nongnu.org
Subject: [Qemu-devel] [RFC PATCH 2/7] acpi: split and rename acpi_table_add()
Date: Tue, 30 Mar 2010 11:20:03 +0300	[thread overview]
Message-ID: <972d931f99c27de91d043b1332eb564e6b4260db.1269936879.git.eduard.munteanu@linux360.ro> (raw)
In-Reply-To: <cover.1269936879.git.eduard.munteanu@linux360.ro>

We'd like to let emulation code build and insert ACPI tables at bootup,
without depending on hacking the BIOS code. This will be used to provide
an IVRS table for emulating the AMD IOMMU, for instance.

This splits acpi_table_add(), retaining the old behavior of inserting
cmdline-supplied tables under the name of acpi_table_cmdline_add(). The
other two resulting functions can be used for the aforementioned
purpose.

Signed-off-by: Eduard - Gabriel Munteanu <eduard.munteanu@linux360.ro>
---
 hw/acpi.c |   64 ++++++++++++++++++++++++++++++++++++++++---------------------
 hw/pc.h   |    4 ++-
 vl.c      |    2 +-
 3 files changed, 46 insertions(+), 24 deletions(-)

diff --git a/hw/acpi.c b/hw/acpi.c
index 7c4e8d3..8eb53da 100644
--- a/hw/acpi.c
+++ b/hw/acpi.c
@@ -840,7 +840,7 @@ struct acpi_table_header
 } __attribute__((packed));
 
 char *acpi_tables;
-size_t acpi_tables_len;
+size_t acpi_tables_len, acpi_tables_prev_len;
 
 static int acpi_checksum(const uint8_t *data, int len)
 {
@@ -851,13 +851,44 @@ static int acpi_checksum(const uint8_t *data, int len)
     return (-sum) & 0xff;
 }
 
-int acpi_table_add(const char *t)
+void *acpi_alloc_table(size_t size)
+{
+    void *ptr;
+
+    if (!acpi_tables) {
+        acpi_tables_len = sizeof(uint16_t);
+        acpi_tables = qemu_mallocz(acpi_tables_len);
+    }
+    acpi_tables_prev_len = acpi_tables_len;
+    acpi_tables_len += sizeof(uint16_t) + size;
+    acpi_tables = qemu_realloc(acpi_tables, acpi_tables_len);
+    ptr = acpi_tables + acpi_tables_prev_len;
+
+    *(uint16_t *) ptr = size;
+
+    return ptr + sizeof(uint16_t);
+}
+
+void acpi_commit_table(void *buf)
+{
+    struct acpi_table_header *acpi_hdr = buf;
+    size_t size = acpi_tables_len - acpi_tables_prev_len - sizeof(uint16_t);
+
+    acpi_hdr->length = cpu_to_le32(size);
+    acpi_hdr->checksum = acpi_checksum(buf, size);
+
+    /* increase number of tables */
+    (*(uint16_t *) acpi_tables) =
+	    cpu_to_le32(le32_to_cpu(*(uint16_t *) acpi_tables) + 1);
+}
+
+int acpi_table_cmdline_add(const char *t)
 {
     static const char *dfl_id = "QEMUQEMU";
     char buf[1024], *p, *f;
     struct acpi_table_header acpi_hdr;
     unsigned long val;
-    size_t newlen, off;
+    size_t size, off;
 
     memset(&acpi_hdr, 0, sizeof(acpi_hdr));
   
@@ -915,7 +946,7 @@ int acpi_table_add(const char *t)
          buf[0] = '\0';
     }
 
-    acpi_hdr.length = sizeof(acpi_hdr);
+    size = sizeof(acpi_hdr);
 
     f = buf;
     while (buf[0]) {
@@ -927,27 +958,17 @@ int acpi_table_add(const char *t)
             fprintf(stderr, "Can't stat file '%s': %s\n", f, strerror(errno));
             goto out;
         }
-        acpi_hdr.length += s.st_size;
+        size += s.st_size;
         if (!n)
             break;
         *n = ':';
         f = n + 1;
     }
 
-    if (!acpi_tables) {
-        acpi_tables_len = sizeof(uint16_t);
-        acpi_tables = qemu_mallocz(acpi_tables_len);
-    }
-    newlen = acpi_tables_len + sizeof(uint16_t) + acpi_hdr.length;
-    acpi_tables = qemu_realloc(acpi_tables, newlen);
-    p = acpi_tables + acpi_tables_len;
-    acpi_tables_len = newlen;
+    p = acpi_alloc_table(size);
+    off = sizeof(struct acpi_table_header);
 
-    acpi_hdr.length = cpu_to_le32(acpi_hdr.length);
-    *(uint16_t*)p = acpi_hdr.length;
-    p += sizeof(uint16_t);
-    memcpy(p, &acpi_hdr, sizeof(acpi_hdr));
-    off = sizeof(acpi_hdr);
+    memcpy(p, &acpi_hdr, off);
 
     f = buf;
     while (buf[0]) {
@@ -983,10 +1004,8 @@ int acpi_table_add(const char *t)
         f = n + 1;
     }
 
-    ((struct acpi_table_header*)p)->checksum = acpi_checksum((uint8_t*)p, off);
-    /* increase number of tables */
-    (*(uint16_t*)acpi_tables) =
-	    cpu_to_le32(le32_to_cpu(*(uint16_t*)acpi_tables) + 1);
+    acpi_commit_table(p);
+
     return 0;
 out:
     if (acpi_tables) {
@@ -995,3 +1014,4 @@ out:
     }
     return -1;
 }
+
diff --git a/hw/pc.h b/hw/pc.h
index b599564..0cef140 100644
--- a/hw/pc.h
+++ b/hw/pc.h
@@ -108,7 +108,9 @@ extern char *acpi_tables;
 extern size_t acpi_tables_len;
 
 void acpi_bios_init(void);
-int acpi_table_add(const char *table_desc);
+void *acpi_alloc_table(size_t size);
+void acpi_commit_table(void *buf);
+int acpi_table_cmdline_add(const char *table_desc);
 
 /* acpi_piix.c */
 i2c_bus *piix4_pm_init(PCIBus *bus, int devfn, uint32_t smb_io_base,
diff --git a/vl.c b/vl.c
index d959fdb..0efba90 100644
--- a/vl.c
+++ b/vl.c
@@ -5492,7 +5492,7 @@ int main(int argc, char **argv, char **envp)
                 rtc_td_hack = 1;
                 break;
             case QEMU_OPTION_acpitable:
-                if(acpi_table_add(optarg) < 0) {
+                if(acpi_table_cmdline_add(optarg) < 0) {
                     fprintf(stderr, "Wrong acpi table provided\n");
                     exit(1);
                 }
-- 
1.6.4.4

  parent reply	other threads:[~2010-03-30  8:20 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-03-30  8:20 [RFC PATCH 0/7] Beginning implementing the AMD IOMMU emulation Eduard - Gabriel Munteanu
2010-03-30  8:20 ` [Qemu-devel] " Eduard - Gabriel Munteanu
2010-03-30  8:20 ` [RFC PATCH 1/7] acpi: qemu_realloc() might return a different pointer Eduard - Gabriel Munteanu
2010-03-30  8:20   ` [Qemu-devel] " Eduard - Gabriel Munteanu
2010-03-30  8:20 ` Eduard - Gabriel Munteanu [this message]
2010-03-30  8:20   ` [Qemu-devel] [RFC PATCH 2/7] acpi: split and rename acpi_table_add() Eduard - Gabriel Munteanu
2010-03-30  8:20 ` [RFC PATCH 3/7] acpi: move table header definition into pc.h Eduard - Gabriel Munteanu
2010-03-30  8:20   ` [Qemu-devel] " Eduard - Gabriel Munteanu
2010-03-30  8:20 ` [RFC PATCH 4/7] sparc: rename hw/iommu.c Eduard - Gabriel Munteanu
2010-03-30  8:20   ` [Qemu-devel] " Eduard - Gabriel Munteanu
2010-03-30 17:06   ` Blue Swirl
2010-03-30 17:06     ` Blue Swirl
2010-03-30 19:28     ` Joerg Roedel
2010-03-30 19:28       ` Joerg Roedel
2010-03-30 20:00       ` Blue Swirl
2010-03-30 20:00         ` Blue Swirl
2010-03-30 20:15         ` Eduard - Gabriel Munteanu
2010-03-30 20:15           ` Eduard - Gabriel Munteanu
2010-03-31  7:27     ` Gerd Hoffmann
2010-03-31  7:27       ` Gerd Hoffmann
2010-03-30  8:20 ` [RFC PATCH 5/7] x86-64: AMD IOMMU stub Eduard - Gabriel Munteanu
2010-03-30  8:20   ` [Qemu-devel] " Eduard - Gabriel Munteanu
2010-03-30 20:37   ` Blue Swirl
2010-03-30 20:37     ` [Qemu-devel] " Blue Swirl
2010-03-30  8:20 ` [RFC PATCH 6/7] acpi: cleanup acpi_checksum() Eduard - Gabriel Munteanu
2010-03-30  8:20   ` [Qemu-devel] " Eduard - Gabriel Munteanu
2010-03-30  8:20 ` [RFC PATCH 7/7] acpi: fix bug in acpi_checksum() caused by garbage in checksum field Eduard - Gabriel Munteanu
2010-03-30  8:20   ` [Qemu-devel] " Eduard - Gabriel Munteanu
2010-03-30 15:10   ` Richard Henderson
2010-03-30 15:10     ` Richard Henderson
2010-03-30 19:40 ` [RFC PATCH 0/7] Beginning implementing the AMD IOMMU emulation Joerg Roedel
2010-03-30 19:40   ` [Qemu-devel] " Joerg Roedel
2010-03-31  5:38   ` Avi Kivity
2010-03-31  5:38     ` [Qemu-devel] " Avi Kivity

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=972d931f99c27de91d043b1332eb564e6b4260db.1269936879.git.eduard.munteanu@linux360.ro \
    --to=eduard.munteanu@linux360.ro \
    --cc=aliguori@us.ibm.com \
    --cc=avi@redhat.com \
    --cc=joro@8bytes.org \
    --cc=kvm@vger.kernel.org \
    --cc=qemu-devel@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.