Patchwork [2/2] introduce read_physical_offset and write_physical_offset

login
register
mail settings
Submitter Stefano Stabellini
Date Sept. 10, 2012, 6:06 p.m.
Message ID <1347300383-9089-2-git-send-email-stefano.stabellini@eu.citrix.com>
Download mbox | patch
Permalink /patch/182940/
State New
Headers show

Comments

Stefano Stabellini - Sept. 10, 2012, 6:06 p.m.
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(-)

Patch

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);
             }
         }