xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
To: xen-devel@lists.xensource.com
Cc: dongxiao.xu@intel.com, Ian.Jackson@eu.citrix.com,
	qemu-devel@nongnu.org,
	Stefano Stabellini <stefano.stabellini@eu.citrix.com>
Subject: [PATCH 2/2] introduce read_physical_offset and write_physical_offset
Date: Mon, 10 Sep 2012 19:06:23 +0100	[thread overview]
Message-ID: <1347300383-9089-2-git-send-email-stefano.stabellini@eu.citrix.com> (raw)
In-Reply-To: <alpine.DEB.2.02.1209101842150.15568@kaball.uk.xensource.com>

Remove read_physical and write_physical.
Introduce two new helper functions, read_physical_offset and
write_physical_offset, that take care of adding or subtracting offset
depending on sign. This way we avoid the automatic casting of sign to
uint32_t that is clearly not a very good idea and can easily cause
overflows. It also makes the code easier to understand.

Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
---
 i386-dm/helper2.c |   60 +++++++++++++++++++++++++++++++++-------------------
 1 files changed, 38 insertions(+), 22 deletions(-)

diff --git a/i386-dm/helper2.c b/i386-dm/helper2.c
index 8f2a893..5eb1901 100644
--- a/i386-dm/helper2.c
+++ b/i386-dm/helper2.c
@@ -339,14 +339,30 @@ static void do_outp(CPUState *env, unsigned long addr,
     }
 }
 
-static inline void read_physical(uint64_t addr, unsigned long size, void *val)
+static inline void read_physical_offset(target_phys_addr_t addr,
+                                        unsigned long offset,
+                                        int sign,
+                                        unsigned long size,
+                                        void *val)
 {
-    return cpu_physical_memory_rw((target_phys_addr_t)addr, val, size, 0);
+    if (sign >= 0)
+        addr += offset;
+    else
+        addr -= offset;
+    return cpu_physical_memory_rw(addr, val, size, 0);
 }
 
-static inline void write_physical(uint64_t addr, unsigned long size, void *val)
+static inline void write_physical_offset(target_phys_addr_t addr,
+                                         unsigned long offset,
+                                         int sign,
+                                         unsigned long size,
+                                         void *val)
 {
-    return cpu_physical_memory_rw((target_phys_addr_t)addr, val, size, 1);
+    if (sign >= 0)
+        addr += offset;
+    else
+        addr -= offset;
+    return cpu_physical_memory_rw(addr, val, size, 1);
 }
 
 static void cpu_ioreq_pio(CPUState *env, ioreq_t *req)
@@ -364,9 +380,9 @@ static void cpu_ioreq_pio(CPUState *env, ioreq_t *req)
 
             for (i = 0; i < req->count; i++) {
                 tmp = do_inp(env, req->addr, req->size);
-                write_physical((target_phys_addr_t) req->data
-                  + (sign * i * req->size),
-                  req->size, &tmp);
+                write_physical_offset((target_phys_addr_t) req->data,
+                                      i * req->size, sign,
+                                      req->size, &tmp);
             }
         }
     } else if (req->dir == IOREQ_WRITE) {
@@ -376,9 +392,9 @@ static void cpu_ioreq_pio(CPUState *env, ioreq_t *req)
             for (i = 0; i < req->count; i++) {
                 unsigned long tmp = 0;
 
-                read_physical((target_phys_addr_t) req->data
-                  + (sign * i * req->size),
-                  req->size, &tmp);
+                read_physical_offset((target_phys_addr_t) req->data,
+                              i * req->size, sign,
+                              req->size, &tmp);
                 do_outp(env, req->addr, req->size, tmp);
             }
         }
@@ -395,14 +411,14 @@ static void cpu_ioreq_move(CPUState *env, ioreq_t *req)
     if (!req->data_is_ptr) {
         if (req->dir == IOREQ_READ) {
             for (i = 0; i < req->count; i++) {
-                read_physical(req->addr
-                  + (sign * i * req->size),
+                read_physical_offset(req->addr,
+                  i * req->size, sign,
                   req->size, &req->data);
             }
         } else if (req->dir == IOREQ_WRITE) {
             for (i = 0; i < req->count; i++) {
-                write_physical(req->addr
-                  + (sign * i * req->size),
+                write_physical_offset(req->addr,
+                  i * req->size, sign,
                   req->size, &req->data);
             }
         }
@@ -411,20 +427,20 @@ static void cpu_ioreq_move(CPUState *env, ioreq_t *req)
 
         if (req->dir == IOREQ_READ) {
             for (i = 0; i < req->count; i++) {
-                read_physical(req->addr
-                  + (sign * i * req->size),
+                read_physical_offset(req->addr,
+                  i * req->size, sign,
                   req->size, &tmp);
-                write_physical((target_phys_addr_t )req->data
-                  + (sign * i * req->size),
+                write_physical_offset((target_phys_addr_t )req->data,
+                  i * req->size, sign,
                   req->size, &tmp);
             }
         } else if (req->dir == IOREQ_WRITE) {
             for (i = 0; i < req->count; i++) {
-                read_physical((target_phys_addr_t) req->data
-                  + (sign * i * req->size),
+                read_physical_offset((target_phys_addr_t) req->data,
+                  i * req->size, sign,
                   req->size, &tmp);
-                write_physical(req->addr
-                  + (sign * i * req->size),
+                write_physical_offset(req->addr,
+                  i * req->size, sign,
                   req->size, &tmp);
             }
         }
-- 
1.7.2.5

  parent reply	other threads:[~2012-09-10 18:06 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-09-10 18:04 [PATCH 0/2] QEMU/xen: simplify cpu_ioreq_pio and cpu_ioreq_move Stefano Stabellini
2012-09-10 18:06 ` [PATCH 1/2] i should be uint32_t rather than int Stefano Stabellini
2012-09-10 18:06 ` Stefano Stabellini [this message]
2012-09-17 23:18 ` [PATCH 0/2] QEMU/xen: simplify cpu_ioreq_pio and cpu_ioreq_move Xu, Dongxiao
2012-09-18 10:24   ` Stefano Stabellini
2012-11-29  3:22     ` Xu, Dongxiao
2012-11-29 13:21       ` Stefano Stabellini
2012-11-29 13:57         ` [Xen-devel] " Ian Campbell
2012-11-29 15:10           ` Pasi Kärkkäinen
2012-12-07 16:14 ` [Xen-devel] " Ian Jackson
2012-12-07 16:30   ` Ian Campbell
2012-12-07 16:33     ` [Xen-devel] " Ian Jackson
2012-12-11  5:50       ` Xu, Dongxiao
2013-01-08  1:49       ` Xu, Dongxiao
2013-01-09  7:09         ` Pasi Kärkkäinen
2013-01-24 13:44           ` [Xen-devel] [PATCH 0/2] QEMU/xen: simplify cpu_ioreq_pio and cpu_ioreq_move / Xen on Xen nested virt Pasi Kärkkäinen
2013-01-31  2:23             ` Xu, Dongxiao
2013-02-19 19:38             ` Pasi Kärkkäinen
2013-02-20 15:42               ` Ian Jackson
2013-02-20 15:53                 ` Pasi Kärkkäinen
2012-12-10 16:08   ` [Xen-devel] [PATCH 0/2] QEMU/xen: simplify cpu_ioreq_pio and cpu_ioreq_move Stefano Stabellini
2012-12-10 17:01     ` Ian Jackson

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=1347300383-9089-2-git-send-email-stefano.stabellini@eu.citrix.com \
    --to=stefano.stabellini@eu.citrix.com \
    --cc=Ian.Jackson@eu.citrix.com \
    --cc=dongxiao.xu@intel.com \
    --cc=qemu-devel@nongnu.org \
    --cc=xen-devel@lists.xensource.com \
    /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).