Skip to contentSkip to conversation

QEMU/xen: simplify cpu_ioreq_pio and cpu_ioreq_move

PATCH In review Stefano Stabellini 5 participants 22 messages Sep 10, 2012 – Feb 20, 2013

lore
Share & tools
lore
#0 lore raw

cover QEMU/xen: simplify cpu_ioreq_pio and cpu_ioreq_move

Hi all,
after reviewing the patch "fix multiply issue for int and uint types" with Ian Jackson, we realized that cpu_ioreq_pio and cpu_ioreq_move are in much need for a simplification as well as removal of a possible integer overflow.

This patch series tries to accomplish both switching to two new helper functions and using a more obvious arithmetic. Doing so it should also fix the original problem that Dongxiao was experiencing. The C language can be a nasty backstabber when signed and unsigned integers are involved.

The current patch series if for qemu-xen-traditional but if the patches are deemed correct I'll submit an equivalent set for QEMU upstream (with the appropriate code style changes).

Stefano Stabellini (2):
i should be uint32_t rather than int
introduce read_physical_offset and write_physical_offset

 i386-dm/helper2.c |   66 +++++++++++++++++++++++++++++++++-------------------
 1 files changed, 42 insertions(+), 24 deletions(-)

Cheers,

Stefano

#1 lore raw

PATCH 1/2 i should be uint32_t rather than int

The current code compare i (int) with req->count (uint32_t) in a for loop, risking an infinite loop if req->count is equal to UINT_MAX.

Also i is only used in comparisons or multiplications with unsigned integers.

---
 i386-dm/helper2.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
i386-dm/helper2.c +4 -2
index c6d049c..8f2a893 100644
@@ -351,7 +351,8 @@ static inline void write_physical(uint64_t addr, unsigned long size, void *val)
 
 static void cpu_ioreq_pio(CPUState *env, ioreq_t *req)
 {
-    int i, sign;
+    uint32_t i;
+    int sign;
 
     sign = req->df ? -1 : 1;
 
@@ -386,7 +387,8 @@ static void cpu_ioreq_pio(CPUState *env, ioreq_t *req)
 
 static void cpu_ioreq_move(CPUState *env, ioreq_t *req)
 {
-    int i, sign;
+    uint32_t i;
+    int sign;
 
     sign = req->df ? -1 : 1;
 
Signature · 1 line
1.7.2.5
#2 lore raw

PATCH 2/2 introduce read_physical_offset and write_physical_offset

In reply to Stefano Stabellini

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.

---
 i386-dm/helper2.c |   60 +++++++++++++++++++++++++++++++++-------------------
 1 files changed, 38 insertions(+), 22 deletions(-)
i386-dm/helper2.c +38 -22
index 8f2a893..5eb1901 100644
@@ -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);
             }
         }
Signature · 1 line
1.7.2.5
#3 lore raw
In reply to Stefano Stabellini

Hi Stefano,

Is these patches merged with Xen 4.2? I didn't see them in the upstream. The uint/int fix is critical to fix the nested guest boot issue.

Thanks,
Dongxiao

Quoted 36 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: Tuesday, September 11, 2012 2:05 AM
To: [email protected]
Cc: Xu, Dongxiao; Ian Jackson; [email protected]; Stefano Stabellini
Subject: [PATCH 0/2] QEMU/xen: simplify cpu_ioreq_pio and cpu_ioreq_move

Hi all,
after reviewing the patch "fix multiply issue for int and uint types" with Ian Jackson, we realized that cpu_ioreq_pio and cpu_ioreq_move are in much need for a simplification as well as removal of a possible integer overflow.

This patch series tries to accomplish both switching to two new helper functions and using a more obvious arithmetic. Doing so it should also fix the original problem that Dongxiao was experiencing. The C language can be a nasty backstabber when signed and unsigned integers are involved.

The current patch series if for qemu-xen-traditional but if the patches are deemed correct I'll submit an equivalent set for QEMU upstream (with the appropriate code style changes).

Stefano Stabellini (2):
i should be uint32_t rather than int
introduce read_physical_offset and write_physical_offset

 i386-dm/helper2.c |   66
+++++++++++++++++++++++++++++++++-------------------
 1 files changed, 42 insertions(+), 24 deletions(-)

Cheers,

Stefano

#4 lore raw
Quoted 4 lines · Dongxiao

Hi Stefano,

Is these patches merged with Xen 4.2? I didn't see them in the upstream. The uint/int fix is critical to fix the nested guest boot issue.

They are not. Ian decided that he wanted to merge a different version of them.

#5 lore raw
Quoted 9 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: Tuesday, September 18, 2012 6:24 PM
To: Xu, Dongxiao
Cc: Stefano Stabellini; [email protected]; Ian Jackson; [email protected]; Keir (Xen.org)
Subject: RE: [PATCH 0/2] QEMU/xen: simplify cpu_ioreq_pio and cpu_ioreq_move

Quoted 7 lines
Quoted 4 lines · Dongxiao

Hi Stefano,

Is these patches merged with Xen 4.2? I didn't see them in the upstream. The uint/int fix is critical to fix the nested guest boot issue.

They are not. Ian decided that he wanted to merge a different version of them.

Hi Stefano and Ian,

What's the status of the two patches? I still didn't see them merged...
Also I think 4.2.1 need these patches to enable the basic Xen on Xen nested virtualization usage scenario.

Thanks,
Dongxiao

#6 lore raw
Quoted 12 lines · Dongxiao
Quoted 12 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: Tuesday, September 18, 2012 6:24 PM
To: Xu, Dongxiao
Cc: Stefano Stabellini; [email protected]; Ian Jackson; [email protected]; Keir (Xen.org)
Subject: RE: [PATCH 0/2] QEMU/xen: simplify cpu_ioreq_pio and cpu_ioreq_move

Quoted 2 lines · Dongxiao

Hi Stefano,

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

Is these patches merged with Xen 4.2? I didn't see them in the upstream. The uint/int fix is critical to fix the nested guest boot issue.

They are not. Ian decided that he wanted to merge a different version of them.

Hi Stefano and Ian,

What's the status of the two patches? I still didn't see them merged...

Ian, do you have any updates?

Quoted 1 line

Also I think 4.2.1 need these patches to enable the basic Xen on Xen nested virtualization usage scenario.

I agree.

#7 lore raw
Quoted 4 lines · Stefano Stabellini
Quoted 2 lines

Also I think 4.2.1 need these patches to enable the basic Xen on Xen nested virtualization usage scenario.

I agree.

Nested virt was a tech preview in 4.2.0, is it really worth backporting ?

Ian.

#8 lore raw
Quoted 9 lines · Ian Campbell
Quoted 4 lines · Stefano Stabellini
Quoted 2 lines

Also I think 4.2.1 need these patches to enable the basic Xen on Xen nested virtualization usage scenario.

I agree.

Nested virt was a tech preview in 4.2.0, is it really worth backporting ?

IIRC AMD Nested Virt works on 4.2.0, so if this backport makes Intel Nested Virt working aswell it'd be nice..

-- Pasi

#9 lore raw
In reply to Stefano Stabellini

Stefano Stabellini writes ("[Xen-devel] [PATCH 0/2] QEMU/xen: simplify cpu_ioreq_pio and cpu_ioreq_move"):

Quoted 10 lines

after reviewing the patch "fix multiply issue for int and uint types" with Ian Jackson, we realized that cpu_ioreq_pio and cpu_ioreq_move are in much need for a simplification as well as removal of a possible integer overflow.

This patch series tries to accomplish both switching to two new helper functions and using a more obvious arithmetic. Doing so it should also fix the original problem that Dongxiao was experiencing. The C language can be a nasty backstabber when signed and unsigned integers are involved.

I think the attached patch is better as it removes some formulaic code. I don't think I have a guest which can repro the bug so I have only compile tested it.

Dongxiao, would you care to take a look ?

PS: I'm pretty sure the original overflows aren't security problems.

Thanks,
Ian.

commit d19731e4e452e3415a5c03771d0406efc803baa9

Author: Ian Jackson <[email protected]>
Date:   Fri Dec 7 16:02:04 2012 +0000

cpu_ioreq_pio, cpu_ioreq_move: introduce read_phys_req_item, write_phys_req_item

The current code compare i (int) with req->count (uint32_t) in a for loop, risking an infinite loop if req->count is >INT_MAX. It also does the multiplication of req->size in a too-small type, leading to integer overflows.

Turn read_physical and write_physical into two different helper functions, read_phys_req_item and write_phys_req_item, that take care of adding or subtracting offset depending on sign.

This removes the formulaic multiplication to a single place where the integer overflows can be dealt with by casting to wide-enough unsigned types.

i386-dm/helper2.c +36 -35
index c6d049c..9b8552c 100644
@@ -339,21 +339,40 @@ static void do_outp(CPUState *env, unsigned long addr,
     }
 }
 
-static inline void read_physical(uint64_t addr, unsigned long size, void *val)
+/*
+ * Helper functions which read/write an object from/to physical guest
+ * memory, as part of the implementation of an ioreq.
+ *
+ * Equivalent to
+ *   cpu_physical_memory_rw(addr + (req->df ? -1 : +1) * req->size * i,
+ *                          val, req->size, 0/1)
+ * except without the integer overflow problems.
+ */
+static void rw_phys_req_item(target_phys_addr_t addr,
+                             ioreq_t *req, uint32_t i, void *val, int rw)
 {
-    return cpu_physical_memory_rw((target_phys_addr_t)addr, val, size, 0);
+    /* Do everything unsigned so overflow just results in a truncated result
+     * and accesses to undesired parts of guest memory, which is up
+     * to the guest */
+    target_phys_addr_t offset = (target_phys_addr_t)req->size * i;
+    if (req->df) addr -= offset;
+    else addr -= offset;
+    cpu_physical_memory_rw(addr, val, req->size, rw);
 }
-
-static inline void write_physical(uint64_t addr, unsigned long size, void *val)
+static inline void read_phys_req_item(target_phys_addr_t addr,
+                                      ioreq_t *req, uint32_t i, void *val)
 {
-    return cpu_physical_memory_rw((target_phys_addr_t)addr, val, size, 1);
+    rw_phys_req_item(addr, req, i, val, 0);
+}
+static inline void write_phys_req_item(target_phys_addr_t addr,
+                                       ioreq_t *req, uint32_t i, void *val)
+{
+    rw_phys_req_item(addr, req, i, val, 1);
 }
 
 static void cpu_ioreq_pio(CPUState *env, ioreq_t *req)
 {
-    int i, sign;
-
-    sign = req->df ? -1 : 1;
+    uint32_t i;
 
     if (req->dir == IOREQ_READ) {
         if (!req->data_is_ptr) {
@@ -363,9 +382,7 @@ 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_phys_req_item((target_phys_addr_t) req->data, req, i, &tmp);
             }
         }
     } else if (req->dir == IOREQ_WRITE) {
@@ -375,9 +392,7 @@ 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_phys_req_item(req->data, req, i, &tmp);
                 do_outp(env, req->addr, req->size, tmp);
             }
         }
@@ -386,22 +401,16 @@ static void cpu_ioreq_pio(CPUState *env, ioreq_t *req)
 
 static void cpu_ioreq_move(CPUState *env, ioreq_t *req)
 {
-    int i, sign;
-
-    sign = req->df ? -1 : 1;
+    uint32_t i;
 
     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),
-                  req->size, &req->data);
+                read_phys_req_item(req->addr, req, i, &req->data);
             }
         } else if (req->dir == IOREQ_WRITE) {
             for (i = 0; i < req->count; i++) {
-                write_physical(req->addr
-                  + (sign * i * req->size),
-                  req->size, &req->data);
+                write_phys_req_item(req->addr, req, i, &req->data);
             }
         }
     } else {
@@ -409,21 +418,13 @@ 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),
-                  req->size, &tmp);
-                write_physical((target_phys_addr_t )req->data
-                  + (sign * i * req->size),
-                  req->size, &tmp);
+                read_phys_req_item(req->addr, req, i, &tmp);
+                write_phys_req_item(req->data, req, i, &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),
-                  req->size, &tmp);
-                write_physical(req->addr
-                  + (sign * i * req->size),
-                  req->size, &tmp);
+                read_phys_req_item(req->data, req, i, &tmp);
+                write_phys_req_item(req->addr, req, i, &tmp);
             }
         }
     }
#10 lore raw

i386-dm/helper2.c static void do_outp(CPUState *env, unsigned long addr, · line 359

Quoted 3 lines · Ian Jackson
i386-dm/helper2.c +3 -0
+    target_phys_addr_t offset = (target_phys_addr_t)req->size * i;
+    if (req->df) addr -= offset;
+    else addr -= offset;

One of these -= should be a += I presume?

[...]

Quoted 1 line

+ write_phys_req_item((target_phys_addr_t) req->data, req, i, &tmp);

This seems to be the only one with this cast, why?

write_phys_req_item takes a target_phys_addr_t so this will happen regardless I think.

Ian.

#11 lore raw

Ian Campbell writes ("Re: [Xen-devel] [PATCH 0/2] QEMU/xen: simplify cpu_ioreq_pio and cpu_ioreq_move"):

Quoted 6 lines · Ian Jackson

Review

Quoted 3 lines
(quoted diff) +3 -0
+    target_phys_addr_t offset = (target_phys_addr_t)req->size * i;
+    if (req->df) addr -= offset;
+    else addr -= offset;

One of these -= should be a += I presume?

Uh, yes.

Quoted 4 lines

[...]

Quoted 1 line

+ write_phys_req_item((target_phys_addr_t) req->data, req, i, &tmp);

This seems to be the only one with this cast, why?

This is a mistake.

Quoted 2 lines

write_phys_req_item takes a target_phys_addr_t so this will happen regardless I think.

Indeed.

Ian.

commit fd3865f8e0d867a203b4ddcb22eefa827cfaca0a

Author: Ian Jackson <[email protected]>
Date:   Fri Dec 7 16:02:04 2012 +0000

cpu_ioreq_pio, cpu_ioreq_move: introduce read_phys_req_item, write_phys_req_item

The current code compare i (int) with req->count (uint32_t) in a for loop, risking an infinite loop if req->count is >INT_MAX. It also does the multiplication of req->size in a too-small type, leading to integer overflows.

Turn read_physical and write_physical into two different helper functions, read_phys_req_item and write_phys_req_item, that take care of adding or subtracting offset depending on sign.

This removes the formulaic multiplication to a single place where the integer overflows can be dealt with by casting to wide-enough unsigned types.

--
v2: Fix sign when !!req->df. Remove a useless cast.

i386-dm/helper2.c +36 -35
index c6d049c..63a938b 100644
@@ -339,21 +339,40 @@ static void do_outp(CPUState *env, unsigned long addr,
     }
 }
 
-static inline void read_physical(uint64_t addr, unsigned long size, void *val)
+/*
+ * Helper functions which read/write an object from/to physical guest
+ * memory, as part of the implementation of an ioreq.
+ *
+ * Equivalent to
+ *   cpu_physical_memory_rw(addr + (req->df ? -1 : +1) * req->size * i,
+ *                          val, req->size, 0/1)
+ * except without the integer overflow problems.
+ */
+static void rw_phys_req_item(target_phys_addr_t addr,
+                             ioreq_t *req, uint32_t i, void *val, int rw)
 {
-    return cpu_physical_memory_rw((target_phys_addr_t)addr, val, size, 0);
+    /* Do everything unsigned so overflow just results in a truncated result
+     * and accesses to undesired parts of guest memory, which is up
+     * to the guest */
+    target_phys_addr_t offset = (target_phys_addr_t)req->size * i;
+    if (req->df) addr -= offset;
+    else addr += offset;
+    cpu_physical_memory_rw(addr, val, req->size, rw);
 }
-
-static inline void write_physical(uint64_t addr, unsigned long size, void *val)
+static inline void read_phys_req_item(target_phys_addr_t addr,
+                                      ioreq_t *req, uint32_t i, void *val)
 {
-    return cpu_physical_memory_rw((target_phys_addr_t)addr, val, size, 1);
+    rw_phys_req_item(addr, req, i, val, 0);
+}
+static inline void write_phys_req_item(target_phys_addr_t addr,
+                                       ioreq_t *req, uint32_t i, void *val)
+{
+    rw_phys_req_item(addr, req, i, val, 1);
 }
 
 static void cpu_ioreq_pio(CPUState *env, ioreq_t *req)
 {
-    int i, sign;
-
-    sign = req->df ? -1 : 1;
+    uint32_t i;
 
     if (req->dir == IOREQ_READ) {
         if (!req->data_is_ptr) {
@@ -363,9 +382,7 @@ 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_phys_req_item(req->data, req, i, &tmp);
             }
         }
     } else if (req->dir == IOREQ_WRITE) {
@@ -375,9 +392,7 @@ 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_phys_req_item(req->data, req, i, &tmp);
                 do_outp(env, req->addr, req->size, tmp);
             }
         }
@@ -386,22 +401,16 @@ static void cpu_ioreq_pio(CPUState *env, ioreq_t *req)
 
 static void cpu_ioreq_move(CPUState *env, ioreq_t *req)
 {
-    int i, sign;
-
-    sign = req->df ? -1 : 1;
+    uint32_t i;
 
     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),
-                  req->size, &req->data);
+                read_phys_req_item(req->addr, req, i, &req->data);
             }
         } else if (req->dir == IOREQ_WRITE) {
             for (i = 0; i < req->count; i++) {
-                write_physical(req->addr
-                  + (sign * i * req->size),
-                  req->size, &req->data);
+                write_phys_req_item(req->addr, req, i, &req->data);
             }
         }
     } else {
@@ -409,21 +418,13 @@ 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),
-                  req->size, &tmp);
-                write_physical((target_phys_addr_t )req->data
-                  + (sign * i * req->size),
-                  req->size, &tmp);
+                read_phys_req_item(req->addr, req, i, &tmp);
+                write_phys_req_item(req->data, req, i, &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),
-                  req->size, &tmp);
-                write_physical(req->addr
-                  + (sign * i * req->size),
-                  req->size, &tmp);
+                read_phys_req_item(req->data, req, i, &tmp);
+                write_phys_req_item(req->addr, req, i, &tmp);
             }
         }
     }
#12 lore raw
Quoted 26 lines

-----Original Message-----
From: Ian Jackson [mailto:[email protected]]
Sent: Saturday, December 08, 2012 12:34 AM
To: Ian Campbell
Cc: Stefano Stabellini; Xu, Dongxiao; [email protected]; [email protected]
Subject: Re: [Xen-devel] [PATCH 0/2] QEMU/xen: simplify cpu_ioreq_pio and cpu_ioreq_move

Ian Campbell writes ("Re: [Xen-devel] [PATCH 0/2] QEMU/xen: simplify
cpu_ioreq_pio and cpu_ioreq_move"):

Quoted 6 lines · Ian Jackson

i386-dm/helper2.c static void do_outp(CPUState *env, unsigned long addr,

Quoted 3 lines
i386-dm/helper2.c +3 -0
+    target_phys_addr_t offset = (target_phys_addr_t)req->size * i;
+    if (req->df) addr -= offset;
+    else addr -= offset;

One of these -= should be a += I presume?

Uh, yes.

Quoted 2 lines

[...]

Quoted 1 line

+ write_phys_req_item((target_phys_addr_t) req->data,

req, i, &tmp);

Quoted 2 lines

This seems to be the only one with this cast, why?

Quoted 8 lines

This is a mistake.

Quoted 2 lines

write_phys_req_item takes a target_phys_addr_t so this will happen regardless I think.

Indeed.

Ian.

Tested this v2 patch on my system, and it works.

Thanks,
Dongxiao

Quoted 160 lines

commit fd3865f8e0d867a203b4ddcb22eefa827cfaca0a

Author: Ian Jackson <[email protected]>
Date:   Fri Dec 7 16:02:04 2012 +0000

cpu_ioreq_pio, cpu_ioreq_move: introduce read_phys_req_item,
write_phys_req_item

The current code compare i (int) with req->count (uint32_t) in a for loop, risking an infinite loop if req->count is >INT_MAX. It also does the multiplication of req->size in a too-small type, leading to integer overflows.

Turn read_physical and write_physical into two different helper functions, read_phys_req_item and write_phys_req_item, that take care of adding or subtracting offset depending on sign.

This removes the formulaic multiplication to a single place where the integer overflows can be dealt with by casting to wide-enough unsigned types.

--
v2: Fix sign when !!req->df. Remove a useless cast.

i386-dm/helper2.c +27 -8
index c6d049c..63a938b 100644
@@ -339,21 +339,40 @@ static void do_outp(CPUState *env, unsigned long
 addr,
     }
 }
 
-static inline void read_physical(uint64_t addr, unsigned long size, void *val)
+/*
+ * Helper functions which read/write an object from/to physical guest
+ * memory, as part of the implementation of an ioreq.
+ *
+ * Equivalent to
+ *   cpu_physical_memory_rw(addr + (req->df ? -1 : +1) * req->size * i,
+ *                          val, req->size, 0/1)
+ * except without the integer overflow problems.
+ */
+static void rw_phys_req_item(target_phys_addr_t addr,
+                             ioreq_t *req, uint32_t i, void *val, int rw)
 {
-    return cpu_physical_memory_rw((target_phys_addr_t)addr, val, size, 0);
+    /* Do everything unsigned so overflow just results in a truncated result
+     * and accesses to undesired parts of guest memory, which is up
+     * to the guest */
+    target_phys_addr_t offset = (target_phys_addr_t)req->size * i;
+    if (req->df) addr -= offset;
+    else addr += offset;
+    cpu_physical_memory_rw(addr, val, req->size, rw);
 }
-
-static inline void write_physical(uint64_t addr, unsigned long size, void *val)
+static inline void read_phys_req_item(target_phys_addr_t addr,
+                                      ioreq_t *req, uint32_t i, void
 *val)
 {
-    return cpu_physical_memory_rw((target_phys_addr_t)addr, val, size, 1);
+    rw_phys_req_item(addr, req, i, val, 0);
+}
+static inline void write_phys_req_item(target_phys_addr_t addr,
+                                       ioreq_t *req, uint32_t i, void
 *val)
+{
+    rw_phys_req_item(addr, req, i, val, 1);
 }
 
 static void cpu_ioreq_pio(CPUState *env, ioreq_t *req)
 {
-    int i, sign;
-
-    sign = req->df ? -1 : 1;
+    uint32_t i;
     if (req->dir == IOREQ_READ) {
         if (!req->data_is_ptr) {
i386-dm/helper2.c +5 -15
@@ -363,9 +382,7 @@ 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_phys_req_item(req->data, req, i, &tmp);
             }
         }
     } else if (req->dir == IOREQ_WRITE) {
@@ -375,9 +392,7 @@ 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_phys_req_item(req->data, req, i, &tmp);
                 do_outp(env, req->addr, req->size, tmp);
             }
         }
@@ -386,22 +401,16 @@ static void cpu_ioreq_pio(CPUState *env, ioreq_t
 *req)
 
 static void cpu_ioreq_move(CPUState *env, ioreq_t *req)
 {
-    int i, sign;
-
-    sign = req->df ? -1 : 1;
+    uint32_t i;
 
     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),
-                  req->size, &req->data);
+                read_phys_req_item(req->addr, req, i, &req->data);
             }
         } else if (req->dir == IOREQ_WRITE) {
             for (i = 0; i < req->count; i++) {
-                write_physical(req->addr
-                  + (sign * i * req->size),
-                  req->size, &req->data);
+                write_phys_req_item(req->addr, req, i, &req->data);
             }
         }
     } else {
i386-dm/helper2.c +4 -12
@@ -409,21 +418,13 @@ 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),
-                  req->size, &tmp);
-                write_physical((target_phys_addr_t )req->data
-                  + (sign * i * req->size),
-                  req->size, &tmp);
+                read_phys_req_item(req->addr, req, i, &tmp);
+                write_phys_req_item(req->data, req, i, &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),
-                  req->size, &tmp);
-                write_physical(req->addr
-                  + (sign * i * req->size),
-                  req->size, &tmp);
+                read_phys_req_item(req->data, req, i, &tmp);
+                write_phys_req_item(req->addr, req, i, &tmp);
             }
         }
     }
#13 lore raw
In reply to Ian Jackson
Quoted 38 lines

-----Original Message-----
From: Xu, Dongxiao
Sent: Tuesday, December 11, 2012 1:50 PM
To: Ian Jackson; Ian Campbell
Cc: Stefano Stabellini; [email protected]; [email protected]
Subject: RE: [Xen-devel] [PATCH 0/2] QEMU/xen: simplify cpu_ioreq_pio and cpu_ioreq_move

Quoted 11 lines

-----Original Message-----
From: Ian Jackson [mailto:[email protected]]
Sent: Saturday, December 08, 2012 12:34 AM
To: Ian Campbell
Cc: Stefano Stabellini; Xu, Dongxiao; [email protected]; [email protected]
Subject: Re: [Xen-devel] [PATCH 0/2] QEMU/xen: simplify cpu_ioreq_pio and cpu_ioreq_move

Ian Campbell writes ("Re: [Xen-devel] [PATCH 0/2] QEMU/xen: simplify
cpu_ioreq_pio and cpu_ioreq_move"):

Quoted 11 lines
Quoted 6 lines · Ian Jackson

i386-dm/helper2.c static void do_outp(CPUState *env, unsigned long addr,

Quoted 3 lines
i386-dm/helper2.c +3 -0
+    target_phys_addr_t offset = (target_phys_addr_t)req->size * i;
+    if (req->df) addr -= offset;
+    else addr -= offset;

One of these -= should be a += I presume?

Uh, yes.

Quoted 2 lines

[...]

Quoted 1 line

+ write_phys_req_item((target_phys_addr_t)

req->data,

Quoted 6 lines

req, i, &tmp);

Quoted 2 lines

This seems to be the only one with this cast, why?

This is a mistake.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

write_phys_req_item takes a target_phys_addr_t so this will happen regardless I think.

Indeed.

Ian.

Tested this v2 patch on my system, and it works.

Are we planning to check this patch into qemu-traditional? Since it is a critical patch to boot Xen on Xen.

Thanks,
Dongxiao

Quoted 173 lines

Thanks,
Dongxiao

Quoted 8 lines

commit fd3865f8e0d867a203b4ddcb22eefa827cfaca0a

Author: Ian Jackson <[email protected]>
Date:   Fri Dec 7 16:02:04 2012 +0000

cpu_ioreq_pio, cpu_ioreq_move: introduce read_phys_req_item,
write_phys_req_item

Quoted 7 lines
    The current code compare i (int) with req->count (uint32_t) in a for
    loop, risking an infinite loop if req->count is >INT_MAX.  It also
    does the multiplication of req->size in a too-small type, leading to
    integer overflows.

    Turn read_physical and write_physical into two different helper
    functions, read_phys_req_item and write_phys_req_item, that take

care

Quoted 24 lines

of adding or subtracting offset depending on sign.

This removes the formulaic multiplication to a single place where the integer overflows can be dealt with by casting to wide-enough unsigned types.

--
v2: Fix sign when !!req->df. Remove a useless cast.

i386-dm/helper2.c +1 -1
index c6d049c..63a938b 100644
@@ -339,21 +339,40 @@ static void do_outp(CPUState *env, unsigned long
 addr,
     }
 }
 
-static inline void read_physical(uint64_t addr, unsigned long size, void *val)
+/*

i386-dm/helper2.c static void do_outp(CPUState *env, unsigned long · line 353

Quoted 10 lines
i386-dm/helper2.c +10 -0
+ * Helper functions which read/write an object from/to physical guest
+ * memory, as part of the implementation of an ioreq.
+ *
+ * Equivalent to
+ *   cpu_physical_memory_rw(addr + (req->df ? -1 : +1) * req->size * i,
+ *                          val, req->size, 0/1)
+ * except without the integer overflow problems.
+ */
+static void rw_phys_req_item(target_phys_addr_t addr,
+                             ioreq_t *req, uint32_t i, void *val, int

rw)

i386-dm/helper2.c static void do_outp(CPUState *env, unsigned long · line 345

Quoted 2 lines
i386-dm/helper2.c +0 -1
 {
-    return cpu_physical_memory_rw((target_phys_addr_t)addr, val, size,

0);

Quoted 5 lines
i386-dm/helper2.c +5 -0
+    /* Do everything unsigned so overflow just results in a truncated result
+     * and accesses to undesired parts of guest memory, which is up
+     * to the guest */
+    target_phys_addr_t offset = (target_phys_addr_t)req->size * i;
+    if (req->df) addr -= offset;

i386-dm/helper2.c static void do_outp(CPUState *env, unsigned long addr,

Quoted 10 lines
i386-dm/helper2.c +4 -3
+    else addr += offset;
+    cpu_physical_memory_rw(addr, val, req->size, rw);
 }
-
-static inline void write_physical(uint64_t addr, unsigned long size, void *val)
+static inline void read_phys_req_item(target_phys_addr_t addr,
+                                      ioreq_t *req, uint32_t i, void
 *val)
 {
-    return cpu_physical_memory_rw((target_phys_addr_t)addr, val, size,

1);

i386-dm/helper2.c static void do_outp(CPUState *env, unsigned long addr,

Quoted 4 lines
i386-dm/helper2.c +4 -0
+    rw_phys_req_item(addr, req, i, val, 0);
+}
+static inline void write_phys_req_item(target_phys_addr_t addr,
+                                       ioreq_t *req, uint32_t i,

void

i386-dm/helper2.c static void cpu_ioreq_pio(CPUState *env, ioreq_t

Quoted 15 lines
i386-dm/helper2.c +3 -3
 *val)
+{
+    rw_phys_req_item(addr, req, i, val, 1);
 }
 
 static void cpu_ioreq_pio(CPUState *env, ioreq_t *req)
 {
-    int i, sign;
-
-    sign = req->df ? -1 : 1;
+    uint32_t i;
     if (req->dir == IOREQ_READ) {
         if (!req->data_is_ptr) {
i386-dm/helper2.c +0 -0
@@ -363,9 +382,7 @@ static void cpu_ioreq_pio(CPUState *env, ioreq_t

*req)

i386-dm/helper2.c static void cpu_ioreq_pio(CPUState *env, ioreq_t

Quoted 11 lines
i386-dm/helper2.c +1 -3
             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_phys_req_item(req->data, req, i, &tmp);
             }
         }
     } else if (req->dir == IOREQ_WRITE) {
@@ -375,9 +392,7 @@ static void cpu_ioreq_pio(CPUState *env, ioreq_t

*req)

i386-dm/helper2.c static void cpu_ioreq_move(CPUState *env,

Quoted 38 lines
i386-dm/helper2.c +4 -12
             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_phys_req_item(req->data, req, i, &tmp);
                 do_outp(env, req->addr, req->size, tmp);
             }
         }
@@ -386,22 +401,16 @@ static void cpu_ioreq_pio(CPUState *env, ioreq_t
 *req)
 
 static void cpu_ioreq_move(CPUState *env, ioreq_t *req)
 {
-    int i, sign;
-
-    sign = req->df ? -1 : 1;
+    uint32_t i;
 
     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),
-                  req->size, &req->data);
+                read_phys_req_item(req->addr, req, i, &req->data);
             }
         } else if (req->dir == IOREQ_WRITE) {
             for (i = 0; i < req->count; i++) {
-                write_physical(req->addr
-                  + (sign * i * req->size),
-                  req->size, &req->data);
+                write_phys_req_item(req->addr, req, i, &req->data);
             }
         }
     } else {
i386-dm/helper2.c +0 -0
@@ -409,21 +418,13 @@ static void cpu_ioreq_move(CPUState *env,

ioreq_t

Quoted 26 lines
i386-dm/helper2.c +4 -12
 *req)
 
         if (req->dir == IOREQ_READ) {
             for (i = 0; i < req->count; i++) {
-                read_physical(req->addr
-                  + (sign * i * req->size),
-                  req->size, &tmp);
-                write_physical((target_phys_addr_t )req->data
-                  + (sign * i * req->size),
-                  req->size, &tmp);
+                read_phys_req_item(req->addr, req, i, &tmp);
+                write_phys_req_item(req->data, req, i, &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),
-                  req->size, &tmp);
-                write_physical(req->addr
-                  + (sign * i * req->size),
-                  req->size, &tmp);
+                read_phys_req_item(req->data, req, i, &tmp);
+                write_phys_req_item(req->addr, req, i, &tmp);
             }
         }
     }
#14 lore raw
Quoted 33 lines · Dongxiao
Quoted 33 lines
Quoted 12 lines

-----Original Message-----
From: Ian Jackson [mailto:[email protected]]
Sent: Saturday, December 08, 2012 12:34 AM
To: Ian Campbell
Cc: Stefano Stabellini; Xu, Dongxiao; [email protected]; [email protected]
Subject: Re: [Xen-devel] [PATCH 0/2] QEMU/xen: simplify cpu_ioreq_pio and cpu_ioreq_move

Ian Campbell writes ("Re: [Xen-devel] [PATCH 0/2] QEMU/xen: simplify
cpu_ioreq_pio and cpu_ioreq_move"):

Quoted 11 lines
Quoted 6 lines · Ian Jackson

i386-dm/helper2.c static void do_outp(CPUState *env, unsigned long addr,

Quoted 3 lines
i386-dm/helper2.c +3 -0
+    target_phys_addr_t offset = (target_phys_addr_t)req->size * i;
+    if (req->df) addr -= offset;
+    else addr -= offset;

One of these -= should be a += I presume?

Uh, yes.

Quoted 2 lines

[...]

Quoted 1 line

+ write_phys_req_item((target_phys_addr_t)

req->data,

Quoted 9 lines

req, i, &tmp);

Quoted 2 lines

This seems to be the only one with this cast, why?

This is a mistake.

Quoted 2 lines

write_phys_req_item takes a target_phys_addr_t so this will happen regardless I think.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Indeed.

Ian.

Tested this v2 patch on my system, and it works.

Are we planning to check this patch into qemu-traditional? Since it is a critical patch to boot Xen on Xen.

We should definitely get Xen-on-Xen working..

-- Pasi

Quoted 181 lines

Thanks,
Dongxiao

Quoted 139 lines

Thanks,
Dongxiao

Quoted 8 lines

commit fd3865f8e0d867a203b4ddcb22eefa827cfaca0a

Author: Ian Jackson <[email protected]>
Date:   Fri Dec 7 16:02:04 2012 +0000

cpu_ioreq_pio, cpu_ioreq_move: introduce read_phys_req_item,
write_phys_req_item

Quoted 7 lines
    The current code compare i (int) with req->count (uint32_t) in a for
    loop, risking an infinite loop if req->count is >INT_MAX.  It also
    does the multiplication of req->size in a too-small type, leading to
    integer overflows.

    Turn read_physical and write_physical into two different helper
    functions, read_phys_req_item and write_phys_req_item, that take

care

Quoted 24 lines

of adding or subtracting offset depending on sign.

This removes the formulaic multiplication to a single place where the integer overflows can be dealt with by casting to wide-enough unsigned types.

--
v2: Fix sign when !!req->df. Remove a useless cast.

i386-dm/helper2.c +1 -1
index c6d049c..63a938b 100644
@@ -339,21 +339,40 @@ static void do_outp(CPUState *env, unsigned long
 addr,
     }
 }
 
-static inline void read_physical(uint64_t addr, unsigned long size, void *val)
+/*

i386-dm/helper2.c static void do_outp(CPUState *env, unsigned long · line 353

Quoted 10 lines
i386-dm/helper2.c +10 -0
+ * Helper functions which read/write an object from/to physical guest
+ * memory, as part of the implementation of an ioreq.
+ *
+ * Equivalent to
+ *   cpu_physical_memory_rw(addr + (req->df ? -1 : +1) * req->size * i,
+ *                          val, req->size, 0/1)
+ * except without the integer overflow problems.
+ */
+static void rw_phys_req_item(target_phys_addr_t addr,
+                             ioreq_t *req, uint32_t i, void *val, int

rw)

i386-dm/helper2.c static void do_outp(CPUState *env, unsigned long · line 345

Quoted 2 lines
i386-dm/helper2.c +0 -1
 {
-    return cpu_physical_memory_rw((target_phys_addr_t)addr, val, size,

0);

Quoted 5 lines
i386-dm/helper2.c +5 -0
+    /* Do everything unsigned so overflow just results in a truncated result
+     * and accesses to undesired parts of guest memory, which is up
+     * to the guest */
+    target_phys_addr_t offset = (target_phys_addr_t)req->size * i;
+    if (req->df) addr -= offset;

i386-dm/helper2.c static void do_outp(CPUState *env, unsigned long addr,

Quoted 10 lines
i386-dm/helper2.c +4 -3
+    else addr += offset;
+    cpu_physical_memory_rw(addr, val, req->size, rw);
 }
-
-static inline void write_physical(uint64_t addr, unsigned long size, void *val)
+static inline void read_phys_req_item(target_phys_addr_t addr,
+                                      ioreq_t *req, uint32_t i, void
 *val)
 {
-    return cpu_physical_memory_rw((target_phys_addr_t)addr, val, size,

1);

i386-dm/helper2.c static void do_outp(CPUState *env, unsigned long addr,

Quoted 4 lines
i386-dm/helper2.c +4 -0
+    rw_phys_req_item(addr, req, i, val, 0);
+}
+static inline void write_phys_req_item(target_phys_addr_t addr,
+                                       ioreq_t *req, uint32_t i,

void

i386-dm/helper2.c static void cpu_ioreq_pio(CPUState *env, ioreq_t

Quoted 15 lines
i386-dm/helper2.c +3 -3
 *val)
+{
+    rw_phys_req_item(addr, req, i, val, 1);
 }
 
 static void cpu_ioreq_pio(CPUState *env, ioreq_t *req)
 {
-    int i, sign;
-
-    sign = req->df ? -1 : 1;
+    uint32_t i;
     if (req->dir == IOREQ_READ) {
         if (!req->data_is_ptr) {
i386-dm/helper2.c +0 -0
@@ -363,9 +382,7 @@ static void cpu_ioreq_pio(CPUState *env, ioreq_t

*req)

i386-dm/helper2.c static void cpu_ioreq_pio(CPUState *env, ioreq_t

Quoted 11 lines
i386-dm/helper2.c +1 -3
             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_phys_req_item(req->data, req, i, &tmp);
             }
         }
     } else if (req->dir == IOREQ_WRITE) {
@@ -375,9 +392,7 @@ static void cpu_ioreq_pio(CPUState *env, ioreq_t

*req)

Quoted 31 lines
i386-dm/helper2.c +3 -10
             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_phys_req_item(req->data, req, i, &tmp);
                 do_outp(env, req->addr, req->size, tmp);
             }
         }
@@ -386,22 +401,16 @@ static void cpu_ioreq_pio(CPUState *env, ioreq_t
 *req)
 
 static void cpu_ioreq_move(CPUState *env, ioreq_t *req)
 {
-    int i, sign;
-
-    sign = req->df ? -1 : 1;
+    uint32_t i;
 
     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),
-                  req->size, &req->data);
+                read_phys_req_item(req->addr, req, i, &req->data);
             }
         } else if (req->dir == IOREQ_WRITE) {
             for (i = 0; i < req->count; i++) {
-                write_physical(req->addr
Quoted 34 lines

i386-dm/helper2.c static void cpu_ioreq_move(CPUState *env,

Quoted 7 lines
i386-dm/helper2.c +1 -2
-                  + (sign * i * req->size),
-                  req->size, &req->data);
+                write_phys_req_item(req->addr, req, i, &req->data);
             }
         }
     } else {
i386-dm/helper2.c +0 -0
@@ -409,21 +418,13 @@ static void cpu_ioreq_move(CPUState *env,

ioreq_t

Quoted 26 lines
i386-dm/helper2.c +4 -12
 *req)
 
         if (req->dir == IOREQ_READ) {
             for (i = 0; i < req->count; i++) {
-                read_physical(req->addr
-                  + (sign * i * req->size),
-                  req->size, &tmp);
-                write_physical((target_phys_addr_t )req->data
-                  + (sign * i * req->size),
-                  req->size, &tmp);
+                read_phys_req_item(req->addr, req, i, &tmp);
+                write_phys_req_item(req->data, req, i, &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),
-                  req->size, &tmp);
-                write_physical(req->addr
-                  + (sign * i * req->size),
-                  req->size, &tmp);
+                read_phys_req_item(req->data, req, i, &tmp);
+                write_phys_req_item(req->addr, req, i, &tmp);
             }
         }
     }

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#15 lore raw

Re: [Xen-devel] [PATCH 0/2] QEMU/xen: simplify cpu_ioreq_pio and cpu_ioreq_move / Xen on Xen nested virt

Hello,

IanJ: I can't see this patch in qemu-xen-unstable ..
Does the the patch still need something to be fixed before it can be applied?

Thanks,

-- Pasi

Quoted 234 lines · Pasi Kärkkäinen
Quoted 33 lines · Dongxiao
Quoted 33 lines
Quoted 12 lines

-----Original Message-----
From: Ian Jackson [mailto:[email protected]]
Sent: Saturday, December 08, 2012 12:34 AM
To: Ian Campbell
Cc: Stefano Stabellini; Xu, Dongxiao; [email protected]; [email protected]
Subject: Re: [Xen-devel] [PATCH 0/2] QEMU/xen: simplify cpu_ioreq_pio and cpu_ioreq_move

Ian Campbell writes ("Re: [Xen-devel] [PATCH 0/2] QEMU/xen: simplify
cpu_ioreq_pio and cpu_ioreq_move"):

Quoted 11 lines
Quoted 6 lines · Ian Jackson

i386-dm/helper2.c static void do_outp(CPUState *env, unsigned long addr,

Quoted 3 lines
i386-dm/helper2.c +3 -0
+    target_phys_addr_t offset = (target_phys_addr_t)req->size * i;
+    if (req->df) addr -= offset;
+    else addr -= offset;

One of these -= should be a += I presume?

Uh, yes.

Quoted 2 lines

[...]

Quoted 1 line

+ write_phys_req_item((target_phys_addr_t)

req->data,

Quoted 9 lines

req, i, &tmp);

Quoted 2 lines

This seems to be the only one with this cast, why?

This is a mistake.

Quoted 2 lines

write_phys_req_item takes a target_phys_addr_t so this will happen regardless I think.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Indeed.

Ian.

Tested this v2 patch on my system, and it works.

Are we planning to check this patch into qemu-traditional? Since it is a critical patch to boot Xen on Xen.

We should definitely get Xen-on-Xen working..

-- Pasi

Quoted 177 lines

Thanks,
Dongxiao

Quoted 139 lines

Thanks,
Dongxiao

Quoted 8 lines

commit fd3865f8e0d867a203b4ddcb22eefa827cfaca0a

Author: Ian Jackson <[email protected]>
Date:   Fri Dec 7 16:02:04 2012 +0000

cpu_ioreq_pio, cpu_ioreq_move: introduce read_phys_req_item,
write_phys_req_item

Quoted 7 lines
    The current code compare i (int) with req->count (uint32_t) in a for
    loop, risking an infinite loop if req->count is >INT_MAX.  It also
    does the multiplication of req->size in a too-small type, leading to
    integer overflows.

    Turn read_physical and write_physical into two different helper
    functions, read_phys_req_item and write_phys_req_item, that take

care

Quoted 24 lines

of adding or subtracting offset depending on sign.

This removes the formulaic multiplication to a single place where the integer overflows can be dealt with by casting to wide-enough unsigned types.

--
v2: Fix sign when !!req->df. Remove a useless cast.

i386-dm/helper2.c +1 -1
index c6d049c..63a938b 100644
@@ -339,21 +339,40 @@ static void do_outp(CPUState *env, unsigned long
 addr,
     }
 }
 
-static inline void read_physical(uint64_t addr, unsigned long size, void *val)
+/*

i386-dm/helper2.c static void do_outp(CPUState *env, unsigned long · line 353

Quoted 10 lines
i386-dm/helper2.c +10 -0
+ * Helper functions which read/write an object from/to physical guest
+ * memory, as part of the implementation of an ioreq.
+ *
+ * Equivalent to
+ *   cpu_physical_memory_rw(addr + (req->df ? -1 : +1) * req->size * i,
+ *                          val, req->size, 0/1)
+ * except without the integer overflow problems.
+ */
+static void rw_phys_req_item(target_phys_addr_t addr,
+                             ioreq_t *req, uint32_t i, void *val, int

rw)

i386-dm/helper2.c static void do_outp(CPUState *env, unsigned long · line 345

Quoted 2 lines
i386-dm/helper2.c +0 -1
 {
-    return cpu_physical_memory_rw((target_phys_addr_t)addr, val, size,

0);

Quoted 5 lines
i386-dm/helper2.c +5 -0
+    /* Do everything unsigned so overflow just results in a truncated result
+     * and accesses to undesired parts of guest memory, which is up
+     * to the guest */
+    target_phys_addr_t offset = (target_phys_addr_t)req->size * i;
+    if (req->df) addr -= offset;

i386-dm/helper2.c static void do_outp(CPUState *env, unsigned long addr,

Quoted 10 lines
i386-dm/helper2.c +4 -3
+    else addr += offset;
+    cpu_physical_memory_rw(addr, val, req->size, rw);
 }
-
-static inline void write_physical(uint64_t addr, unsigned long size, void *val)
+static inline void read_phys_req_item(target_phys_addr_t addr,
+                                      ioreq_t *req, uint32_t i, void
 *val)
 {
-    return cpu_physical_memory_rw((target_phys_addr_t)addr, val, size,

1);

i386-dm/helper2.c static void do_outp(CPUState *env, unsigned long addr,

Quoted 4 lines
i386-dm/helper2.c +4 -0
+    rw_phys_req_item(addr, req, i, val, 0);
+}
+static inline void write_phys_req_item(target_phys_addr_t addr,
+                                       ioreq_t *req, uint32_t i,

void

i386-dm/helper2.c static void cpu_ioreq_pio(CPUState *env, ioreq_t

Quoted 15 lines
i386-dm/helper2.c +3 -3
 *val)
+{
+    rw_phys_req_item(addr, req, i, val, 1);
 }
 
 static void cpu_ioreq_pio(CPUState *env, ioreq_t *req)
 {
-    int i, sign;
-
-    sign = req->df ? -1 : 1;
+    uint32_t i;
     if (req->dir == IOREQ_READ) {
         if (!req->data_is_ptr) {
i386-dm/helper2.c +0 -0
@@ -363,9 +382,7 @@ static void cpu_ioreq_pio(CPUState *env, ioreq_t

*req)

i386-dm/helper2.c static void cpu_ioreq_pio(CPUState *env, ioreq_t

Quoted 11 lines
i386-dm/helper2.c +1 -3
             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_phys_req_item(req->data, req, i, &tmp);
             }
         }
     } else if (req->dir == IOREQ_WRITE) {
@@ -375,9 +392,7 @@ static void cpu_ioreq_pio(CPUState *env, ioreq_t

*req)

Quoted 31 lines
i386-dm/helper2.c +3 -10
             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_phys_req_item(req->data, req, i, &tmp);
                 do_outp(env, req->addr, req->size, tmp);
             }
         }
@@ -386,22 +401,16 @@ static void cpu_ioreq_pio(CPUState *env, ioreq_t
 *req)
 
 static void cpu_ioreq_move(CPUState *env, ioreq_t *req)
 {
-    int i, sign;
-
-    sign = req->df ? -1 : 1;
+    uint32_t i;
 
     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),
-                  req->size, &req->data);
+                read_phys_req_item(req->addr, req, i, &req->data);
             }
         } else if (req->dir == IOREQ_WRITE) {
             for (i = 0; i < req->count; i++) {
-                write_physical(req->addr
Quoted 34 lines

i386-dm/helper2.c static void cpu_ioreq_move(CPUState *env,

Quoted 7 lines
i386-dm/helper2.c +1 -2
-                  + (sign * i * req->size),
-                  req->size, &req->data);
+                write_phys_req_item(req->addr, req, i, &req->data);
             }
         }
     } else {
i386-dm/helper2.c +0 -0
@@ -409,21 +418,13 @@ static void cpu_ioreq_move(CPUState *env,

ioreq_t

Quoted 26 lines
i386-dm/helper2.c +4 -12
 *req)
 
         if (req->dir == IOREQ_READ) {
             for (i = 0; i < req->count; i++) {
-                read_physical(req->addr
-                  + (sign * i * req->size),
-                  req->size, &tmp);
-                write_physical((target_phys_addr_t )req->data
-                  + (sign * i * req->size),
-                  req->size, &tmp);
+                read_phys_req_item(req->addr, req, i, &tmp);
+                write_phys_req_item(req->data, req, i, &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),
-                  req->size, &tmp);
-                write_physical(req->addr
-                  + (sign * i * req->size),
-                  req->size, &tmp);
+                read_phys_req_item(req->data, req, i, &tmp);
+                write_phys_req_item(req->addr, req, i, &tmp);
             }
         }
     }
Quoted 4 lines

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#16 lore raw

Re: [Xen-devel] [PATCH 0/2] QEMU/xen: simplify cpu_ioreq_pio and cpu_ioreq_move / Xen on Xen nested virt

Quoted 9 lines

-----Original Message-----
From: Pasi Kärkkäinen [mailto:[email protected]]
Sent: Thursday, January 24, 2013 9:45 PM
To: Ian Jackson
Cc: [email protected]; Ian Campbell; Stefano Stabellini; [email protected]; Xu, Dongxiao
Subject: Re: [Xen-devel] [PATCH 0/2] QEMU/xen: simplify cpu_ioreq_pio and cpu_ioreq_move / Xen on Xen nested virt

Quoted 4 lines

Hello,

IanJ: I can't see this patch in qemu-xen-unstable ..
Does the the patch still need something to be fixed before it can be applied?

Yes, I am also confused why we still keep this fix out of the qemu-xen tree. :(

Our QA needs to apply additional patch to test nested virtualization cases.

Thanks,
Dongxiao

Quoted 262 lines

Thanks,

-- Pasi

Quoted 9 lines · Pasi Kärkkäinen
Quoted 8 lines · Dongxiao
Quoted 8 lines
Quoted 8 lines

-----Original Message-----
From: Ian Jackson [mailto:[email protected]]
Sent: Saturday, December 08, 2012 12:34 AM
To: Ian Campbell
Cc: Stefano Stabellini; Xu, Dongxiao; [email protected]; [email protected]
Subject: Re: [Xen-devel] [PATCH 0/2] QEMU/xen: simplify cpu_ioreq_pio

and

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

cpu_ioreq_move

Ian Campbell writes ("Re: [Xen-devel] [PATCH 0/2] QEMU/xen: simplify
cpu_ioreq_pio and cpu_ioreq_move"):

Quoted 2 lines · Ian Jackson
Quoted 1 line

+ target_phys_addr_t offset = (target_phys_addr_t)req->size *

i;

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
Quoted 9 lines
Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+    if (req->df) addr -= offset;
+    else addr -= offset;

One of these -= should be a += I presume?

Uh, yes.

Quoted 2 lines

[...]

Quoted 1 line

+ write_phys_req_item((target_phys_addr_t)

req->data,

Quoted 9 lines

req, i, &tmp);

Quoted 2 lines

This seems to be the only one with this cast, why?

This is a mistake.

Quoted 2 lines

write_phys_req_item takes a target_phys_addr_t so this will happen regardless I think.

Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

Indeed.

Ian.

Tested this v2 patch on my system, and it works.

Are we planning to check this patch into qemu-traditional? Since it is a

critical patch to boot Xen on Xen.

Quoted 30 lines
Quoted 1 line

We should definitely get Xen-on-Xen working..

-- Pasi

Quoted 23 lines

Thanks,
Dongxiao

Quoted 20 lines

Thanks,
Dongxiao

Quoted 15 lines

commit fd3865f8e0d867a203b4ddcb22eefa827cfaca0a

Author: Ian Jackson <[email protected]>
Date:   Fri Dec 7 16:02:04 2012 +0000

cpu_ioreq_pio, cpu_ioreq_move: introduce read_phys_req_item,
write_phys_req_item

    The current code compare i (int) with req->count (uint32_t) in a for
    loop, risking an infinite loop if req->count is >INT_MAX.  It also
    does the multiplication of req->size in a too-small type, leading to
    integer overflows.

    Turn read_physical and write_physical into two different helper
    functions, read_phys_req_item and write_phys_req_item, that

take

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

care

Quoted 3 lines

of adding or subtracting offset depending on sign.

This removes the formulaic multiplication to a single place where

the

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

integer overflows can be dealt with by casting to wide-enough

unsigned

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

types.

Signed-off-by Stefano Stabellini

<[email protected]>

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

i386-dm/helper2.c static void do_outp(CPUState *env, unsigned

Quoted 10 lines
    Signed-off-by: Ian Jackson <[email protected]>

    --
    v2: Fix sign when !!req->df.  Remove a useless cast.
i386-dm/helper2.c +0 -0
index c6d049c..63a938b 100644
@@ -339,21 +339,40 @@ static void do_outp(CPUState *env, unsigned

long

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

i386-dm/helper2.c static void do_outp(CPUState *env, unsigned · line 343

Quoted 5 lines
i386-dm/helper2.c +0 -1
 addr,
     }
 }
 
-static inline void read_physical(uint64_t addr, unsigned long size, void

*val)

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

i386-dm/helper2.c static void do_outp(CPUState *env, unsigned long addr,

Quoted 6 lines
i386-dm/helper2.c +6 -0
+/*
+ * Helper functions which read/write an object from/to physical guest
+ * memory, as part of the implementation of an ioreq.
+ *
+ * Equivalent to
+ *   cpu_physical_memory_rw(addr + (req->df ? -1 : +1) * req->size *

i,

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

i386-dm/helper2.c static void do_outp(CPUState *env, unsigned long addr,

Quoted 5 lines
i386-dm/helper2.c +5 -0
+ *                          val, req->size, 0/1)
+ * except without the integer overflow problems.
+ */
+static void rw_phys_req_item(target_phys_addr_t addr,
+                             ioreq_t *req, uint32_t i, void *val,

int

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

rw)

Quoted 2 lines
i386-dm/helper2.c +0 -1
 {
-    return cpu_physical_memory_rw((target_phys_addr_t)addr, val,

size,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

0);

Quoted 1 line
i386-dm/helper2.c +1 -0
+    /* Do everything unsigned so overflow just results in a truncated

result

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

i386-dm/helper2.c static void do_outp(CPUState *env, unsigned long addr,

Quoted 9 lines
i386-dm/helper2.c +6 -2
+     * and accesses to undesired parts of guest memory, which is up
+     * to the guest */
+    target_phys_addr_t offset = (target_phys_addr_t)req->size * i;
+    if (req->df) addr -= offset;
+    else addr += offset;
+    cpu_physical_memory_rw(addr, val, req->size, rw);
 }
-
-static inline void write_physical(uint64_t addr, unsigned long size, void

*val)

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

i386-dm/helper2.c static void do_outp(CPUState *env, unsigned · line 364

Quoted 2 lines
i386-dm/helper2.c +2 -0
+static inline void read_phys_req_item(target_phys_addr_t addr,
+                                      ioreq_t *req, uint32_t i,

void

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

i386-dm/helper2.c static void do_outp(CPUState *env, unsigned · line 351

Quoted 3 lines
i386-dm/helper2.c +0 -1
 *val)
 {
-    return cpu_physical_memory_rw((target_phys_addr_t)addr, val,

size,

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

1);

i386-dm/helper2.c static void do_outp(CPUState *env, unsigned long addr,

Quoted 4 lines
i386-dm/helper2.c +4 -0
+    rw_phys_req_item(addr, req, i, val, 0);
+}
+static inline void write_phys_req_item(target_phys_addr_t addr,
+                                       ioreq_t *req, uint32_t

i,

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

void

Quoted 12 lines
i386-dm/helper2.c +3 -3
 *val)
+{
+    rw_phys_req_item(addr, req, i, val, 1);
 }
 
 static void cpu_ioreq_pio(CPUState *env, ioreq_t *req)
 {
-    int i, sign;
-
-    sign = req->df ? -1 : 1;
+    uint32_t i;
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

i386-dm/helper2.c static void cpu_ioreq_pio(CPUState *env,

Quoted 3 lines
     if (req->dir == IOREQ_READ) {
         if (!req->data_is_ptr) {
i386-dm/helper2.c +0 -0
@@ -363,9 +382,7 @@ static void cpu_ioreq_pio(CPUState *env,

ioreq_t

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

*req)

i386-dm/helper2.c static void cpu_ioreq_pio(CPUState *env,

Quoted 11 lines
i386-dm/helper2.c +1 -3
             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_phys_req_item(req->data, req, i, &tmp);
             }
         }
     } else if (req->dir == IOREQ_WRITE) {
@@ -375,9 +392,7 @@ static void cpu_ioreq_pio(CPUState *env,

ioreq_t

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

*req)

Quoted 3 lines
i386-dm/helper2.c +0 -0
             for (i = 0; i < req->count; i++) {
                 unsigned long tmp = 0;
 
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

i386-dm/helper2.c static void cpu_ioreq_pio(CPUState *env,

Quoted 8 lines
i386-dm/helper2.c +1 -3
-                read_physical((target_phys_addr_t) req->data
-                  + (sign * i * req->size),
-                  req->size, &tmp);
+                read_phys_req_item(req->data, req, i, &tmp);
                 do_outp(env, req->addr, req->size, tmp);
             }
         }
@@ -386,22 +401,16 @@ static void cpu_ioreq_pio(CPUState *env,

ioreq_t

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
i386-dm/helper2.c +1 -3
 *req)
 
 static void cpu_ioreq_move(CPUState *env, ioreq_t *req)
 {
-    int i, sign;
-
-    sign = req->df ? -1 : 1;
+    uint32_t i;
 
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

i386-dm/helper2.c static void cpu_ioreq_pio(CPUState *env, ioreq_t *req)

Quoted 7 lines
i386-dm/helper2.c +1 -3
     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),
-                  req->size, &req->data);
+                read_phys_req_item(req->addr, req, i,

&req->data);

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

i386-dm/helper2.c static void cpu_ioreq_pio(CPUState *env, ioreq_t *req)

Quoted 7 lines
i386-dm/helper2.c +1 -3
             }
         } else if (req->dir == IOREQ_WRITE) {
             for (i = 0; i < req->count; i++) {
-                write_physical(req->addr
-                  + (sign * i * req->size),
-                  req->size, &req->data);
+                write_phys_req_item(req->addr, req, i,

&req->data);

Quoted 41 lines
Quoted 32 lines
Quoted 31 lines

i386-dm/helper2.c static void cpu_ioreq_move(CPUState *env,

Quoted 4 lines
i386-dm/helper2.c +0 -0
             }
         }
     } else {
i386-dm/helper2.c +0 -0
@@ -409,21 +418,13 @@ static void cpu_ioreq_move(CPUState *env,

ioreq_t

Quoted 26 lines
i386-dm/helper2.c +4 -12
 *req)
 
         if (req->dir == IOREQ_READ) {
             for (i = 0; i < req->count; i++) {
-                read_physical(req->addr
-                  + (sign * i * req->size),
-                  req->size, &tmp);
-                write_physical((target_phys_addr_t )req->data
-                  + (sign * i * req->size),
-                  req->size, &tmp);
+                read_phys_req_item(req->addr, req, i, &tmp);
+                write_phys_req_item(req->data, req, i, &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),
-                  req->size, &tmp);
-                write_physical(req->addr
-                  + (sign * i * req->size),
-                  req->size, &tmp);
+                read_phys_req_item(req->data, req, i, &tmp);
+                write_phys_req_item(req->addr, req, i, &tmp);
             }
         }
     }
Quoted 4 lines

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#17 lore raw

Re: [Xen-devel] [PATCH 0/2] QEMU/xen: simplify cpu_ioreq_pio and cpu_ioreq_move / Xen on Xen nested virt

In reply to Pasi Kärkkäinen
Quoted 5 lines · Pasi Kärkkäinen

Hello,

IanJ: I can't see this patch in qemu-xen-unstable ..
Does the the patch still need something to be fixed before it can be applied?

Ping again? I wonder if I've gotten into some blacklist already for nagging about this..

This is a required patch for making Xen-on-Xen Nested virt working on Intel..

thanks,

-- Pasi

Quoted 241 lines · Pasi Kärkkäinen
Quoted 230 lines · Dongxiao
Quoted 33 lines
Quoted 33 lines
Quoted 12 lines

-----Original Message-----
From: Ian Jackson [mailto:[email protected]]
Sent: Saturday, December 08, 2012 12:34 AM
To: Ian Campbell
Cc: Stefano Stabellini; Xu, Dongxiao; [email protected]; [email protected]
Subject: Re: [Xen-devel] [PATCH 0/2] QEMU/xen: simplify cpu_ioreq_pio and cpu_ioreq_move

Ian Campbell writes ("Re: [Xen-devel] [PATCH 0/2] QEMU/xen: simplify
cpu_ioreq_pio and cpu_ioreq_move"):

Quoted 11 lines
Quoted 6 lines · Ian Jackson

i386-dm/helper2.c static void do_outp(CPUState *env, unsigned long addr,

Quoted 3 lines
i386-dm/helper2.c +3 -0
+    target_phys_addr_t offset = (target_phys_addr_t)req->size * i;
+    if (req->df) addr -= offset;
+    else addr -= offset;

One of these -= should be a += I presume?

Uh, yes.

Quoted 2 lines

[...]

Quoted 1 line

+ write_phys_req_item((target_phys_addr_t)

req->data,

Quoted 9 lines

req, i, &tmp);

Quoted 2 lines

This seems to be the only one with this cast, why?

This is a mistake.

Quoted 2 lines

write_phys_req_item takes a target_phys_addr_t so this will happen regardless I think.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Indeed.

Ian.

Tested this v2 patch on my system, and it works.

Are we planning to check this patch into qemu-traditional? Since it is a critical patch to boot Xen on Xen.

We should definitely get Xen-on-Xen working..

-- Pasi

Quoted 177 lines

Thanks,
Dongxiao

Quoted 139 lines

Thanks,
Dongxiao

Quoted 8 lines

commit fd3865f8e0d867a203b4ddcb22eefa827cfaca0a

Author: Ian Jackson <[email protected]>
Date:   Fri Dec 7 16:02:04 2012 +0000

cpu_ioreq_pio, cpu_ioreq_move: introduce read_phys_req_item,
write_phys_req_item

Quoted 7 lines
    The current code compare i (int) with req->count (uint32_t) in a for
    loop, risking an infinite loop if req->count is >INT_MAX.  It also
    does the multiplication of req->size in a too-small type, leading to
    integer overflows.

    Turn read_physical and write_physical into two different helper
    functions, read_phys_req_item and write_phys_req_item, that take

care

Quoted 24 lines

of adding or subtracting offset depending on sign.

This removes the formulaic multiplication to a single place where the integer overflows can be dealt with by casting to wide-enough unsigned types.

--
v2: Fix sign when !!req->df. Remove a useless cast.

i386-dm/helper2.c +1 -1
index c6d049c..63a938b 100644
@@ -339,21 +339,40 @@ static void do_outp(CPUState *env, unsigned long
 addr,
     }
 }
 
-static inline void read_physical(uint64_t addr, unsigned long size, void *val)
+/*

i386-dm/helper2.c static void do_outp(CPUState *env, unsigned long · line 353

Quoted 10 lines
i386-dm/helper2.c +10 -0
+ * Helper functions which read/write an object from/to physical guest
+ * memory, as part of the implementation of an ioreq.
+ *
+ * Equivalent to
+ *   cpu_physical_memory_rw(addr + (req->df ? -1 : +1) * req->size * i,
+ *                          val, req->size, 0/1)
+ * except without the integer overflow problems.
+ */
+static void rw_phys_req_item(target_phys_addr_t addr,
+                             ioreq_t *req, uint32_t i, void *val, int

rw)

i386-dm/helper2.c static void do_outp(CPUState *env, unsigned long · line 345

Quoted 2 lines
i386-dm/helper2.c +0 -1
 {
-    return cpu_physical_memory_rw((target_phys_addr_t)addr, val, size,

0);

Quoted 5 lines
i386-dm/helper2.c +5 -0
+    /* Do everything unsigned so overflow just results in a truncated result
+     * and accesses to undesired parts of guest memory, which is up
+     * to the guest */
+    target_phys_addr_t offset = (target_phys_addr_t)req->size * i;
+    if (req->df) addr -= offset;

i386-dm/helper2.c static void do_outp(CPUState *env, unsigned long addr,

Quoted 10 lines
i386-dm/helper2.c +4 -3
+    else addr += offset;
+    cpu_physical_memory_rw(addr, val, req->size, rw);
 }
-
-static inline void write_physical(uint64_t addr, unsigned long size, void *val)
+static inline void read_phys_req_item(target_phys_addr_t addr,
+                                      ioreq_t *req, uint32_t i, void
 *val)
 {
-    return cpu_physical_memory_rw((target_phys_addr_t)addr, val, size,

1);

i386-dm/helper2.c static void do_outp(CPUState *env, unsigned long addr,

Quoted 4 lines
i386-dm/helper2.c +4 -0
+    rw_phys_req_item(addr, req, i, val, 0);
+}
+static inline void write_phys_req_item(target_phys_addr_t addr,
+                                       ioreq_t *req, uint32_t i,

void

i386-dm/helper2.c static void cpu_ioreq_pio(CPUState *env, ioreq_t

Quoted 15 lines
i386-dm/helper2.c +3 -3
 *val)
+{
+    rw_phys_req_item(addr, req, i, val, 1);
 }
 
 static void cpu_ioreq_pio(CPUState *env, ioreq_t *req)
 {
-    int i, sign;
-
-    sign = req->df ? -1 : 1;
+    uint32_t i;
     if (req->dir == IOREQ_READ) {
         if (!req->data_is_ptr) {
i386-dm/helper2.c +0 -0
@@ -363,9 +382,7 @@ static void cpu_ioreq_pio(CPUState *env, ioreq_t

*req)

i386-dm/helper2.c static void cpu_ioreq_pio(CPUState *env, ioreq_t

Quoted 11 lines
i386-dm/helper2.c +1 -3
             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_phys_req_item(req->data, req, i, &tmp);
             }
         }
     } else if (req->dir == IOREQ_WRITE) {
@@ -375,9 +392,7 @@ static void cpu_ioreq_pio(CPUState *env, ioreq_t

*req)

Quoted 31 lines
i386-dm/helper2.c +3 -10
             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_phys_req_item(req->data, req, i, &tmp);
                 do_outp(env, req->addr, req->size, tmp);
             }
         }
@@ -386,22 +401,16 @@ static void cpu_ioreq_pio(CPUState *env, ioreq_t
 *req)
 
 static void cpu_ioreq_move(CPUState *env, ioreq_t *req)
 {
-    int i, sign;
-
-    sign = req->df ? -1 : 1;
+    uint32_t i;
 
     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),
-                  req->size, &req->data);
+                read_phys_req_item(req->addr, req, i, &req->data);
             }
         } else if (req->dir == IOREQ_WRITE) {
             for (i = 0; i < req->count; i++) {
-                write_physical(req->addr
Quoted 34 lines

i386-dm/helper2.c static void cpu_ioreq_move(CPUState *env,

Quoted 7 lines
i386-dm/helper2.c +1 -2
-                  + (sign * i * req->size),
-                  req->size, &req->data);
+                write_phys_req_item(req->addr, req, i, &req->data);
             }
         }
     } else {
i386-dm/helper2.c +0 -0
@@ -409,21 +418,13 @@ static void cpu_ioreq_move(CPUState *env,

ioreq_t

Quoted 26 lines
i386-dm/helper2.c +4 -12
 *req)
 
         if (req->dir == IOREQ_READ) {
             for (i = 0; i < req->count; i++) {
-                read_physical(req->addr
-                  + (sign * i * req->size),
-                  req->size, &tmp);
-                write_physical((target_phys_addr_t )req->data
-                  + (sign * i * req->size),
-                  req->size, &tmp);
+                read_phys_req_item(req->addr, req, i, &tmp);
+                write_phys_req_item(req->data, req, i, &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),
-                  req->size, &tmp);
-                write_physical(req->addr
-                  + (sign * i * req->size),
-                  req->size, &tmp);
+                read_phys_req_item(req->data, req, i, &tmp);
+                write_phys_req_item(req->addr, req, i, &tmp);
             }
         }
     }
Quoted 4 lines

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

Quoted 4 lines

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#18 lore raw

Re: [Xen-devel] [PATCH 0/2] QEMU/xen: simplify cpu_ioreq_pio and cpu_ioreq_move / Xen on Xen nested virt

Pasi Kärkkäinen writes ("Re: [Xen-devel] [PATCH 0/2] QEMU/xen: simplify cpu_ioreq_pio and cpu_ioreq_move / Xen on Xen nested virt"):

Quoted 3 lines

Ping again? I wonder if I've gotten into some blacklist already for nagging about this..

This is a required patch for making Xen-on-Xen Nested virt working on Intel..

Sorry about that, I have applied it.

Ian.

#19 lore raw

Re: [Xen-devel] [PATCH 0/2] QEMU/xen: simplify cpu_ioreq_pio and cpu_ioreq_move / Xen on Xen nested virt

Quoted 7 lines · Ian Jackson

Pasi Kärkkäinen writes ("Re: [Xen-devel] [PATCH 0/2] QEMU/xen: simplify cpu_ioreq_pio and cpu_ioreq_move / Xen on Xen nested virt"):

Quoted 3 lines

Ping again? I wonder if I've gotten into some blacklist already for nagging about this..

This is a required patch for making Xen-on-Xen Nested virt working on Intel..

Sorry about that, I have applied it.

Thanks!

-- Pasi

#20 lore raw
In reply to Ian Jackson
Quoted 64 lines · Ian Jackson

Stefano Stabellini writes ("[Xen-devel] [PATCH 0/2] QEMU/xen: simplify cpu_ioreq_pio and cpu_ioreq_move"):

Quoted 10 lines

after reviewing the patch "fix multiply issue for int and uint types" with Ian Jackson, we realized that cpu_ioreq_pio and cpu_ioreq_move are in much need for a simplification as well as removal of a possible integer overflow.

This patch series tries to accomplish both switching to two new helper functions and using a more obvious arithmetic. Doing so it should also fix the original problem that Dongxiao was experiencing. The C language can be a nasty backstabber when signed and unsigned integers are involved.

I think the attached patch is better as it removes some formulaic code. I don't think I have a guest which can repro the bug so I have only compile tested it.

Dongxiao, would you care to take a look ?

PS: I'm pretty sure the original overflows aren't security problems.

Thanks,
Ian.

commit d19731e4e452e3415a5c03771d0406efc803baa9

Author: Ian Jackson <[email protected]>
Date:   Fri Dec 7 16:02:04 2012 +0000

cpu_ioreq_pio, cpu_ioreq_move: introduce read_phys_req_item, write_phys_req_item

The current code compare i (int) with req->count (uint32_t) in a for loop, risking an infinite loop if req->count is >INT_MAX. It also does the multiplication of req->size in a too-small type, leading to integer overflows.

Turn read_physical and write_physical into two different helper functions, read_phys_req_item and write_phys_req_item, that take care of adding or subtracting offset depending on sign.

This removes the formulaic multiplication to a single place where the integer overflows can be dealt with by casting to wide-enough unsigned types.

i386-dm/helper2.c +9 -1
index c6d049c..9b8552c 100644
@@ -339,21 +339,40 @@ static void do_outp(CPUState *env, unsigned long addr,
     }
 }
 
-static inline void read_physical(uint64_t addr, unsigned long size, void *val)
+/*
+ * Helper functions which read/write an object from/to physical guest
+ * memory, as part of the implementation of an ioreq.
+ *
+ * Equivalent to
+ *   cpu_physical_memory_rw(addr + (req->df ? -1 : +1) * req->size * i,
+ *                          val, req->size, 0/1)
+ * except without the integer overflow problems.
+ */

i386-dm/helper2.c static void do_outp(CPUState *env, unsigned long addr, · line 359

Quoted 10 lines
i386-dm/helper2.c +8 -1
+static void rw_phys_req_item(target_phys_addr_t addr,
+                             ioreq_t *req, uint32_t i, void *val, int rw)
 {
-    return cpu_physical_memory_rw((target_phys_addr_t)addr, val, size, 0);
+    /* Do everything unsigned so overflow just results in a truncated result
+     * and accesses to undesired parts of guest memory, which is up
+     * to the guest */
+    target_phys_addr_t offset = (target_phys_addr_t)req->size * i;
+    if (req->df) addr -= offset;
+    else addr -= offset;

This can't be right, can it?

The search/replace changes below look correct.

For the sake of consistency, could you please send a patch against upstream QEMU to qemu-devel? The corresponding code is in xen-all.c (cpu_ioreq_pio and cpu_ioreq_move).

Quoted 100 lines
i386-dm/helper2.c +19 -33
+    cpu_physical_memory_rw(addr, val, req->size, rw);
 }
-
-static inline void write_physical(uint64_t addr, unsigned long size, void *val)
+static inline void read_phys_req_item(target_phys_addr_t addr,
+                                      ioreq_t *req, uint32_t i, void *val)
 {
-    return cpu_physical_memory_rw((target_phys_addr_t)addr, val, size, 1);
+    rw_phys_req_item(addr, req, i, val, 0);
+}
+static inline void write_phys_req_item(target_phys_addr_t addr,
+                                       ioreq_t *req, uint32_t i, void *val)
+{
+    rw_phys_req_item(addr, req, i, val, 1);
 }
 
 static void cpu_ioreq_pio(CPUState *env, ioreq_t *req)
 {
-    int i, sign;
-
-    sign = req->df ? -1 : 1;
+    uint32_t i;
 
     if (req->dir == IOREQ_READ) {
         if (!req->data_is_ptr) {
@@ -363,9 +382,7 @@ 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_phys_req_item((target_phys_addr_t) req->data, req, i, &tmp);
             }
         }
     } else if (req->dir == IOREQ_WRITE) {
@@ -375,9 +392,7 @@ 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_phys_req_item(req->data, req, i, &tmp);
                 do_outp(env, req->addr, req->size, tmp);
             }
         }
@@ -386,22 +401,16 @@ static void cpu_ioreq_pio(CPUState *env, ioreq_t *req)
 
 static void cpu_ioreq_move(CPUState *env, ioreq_t *req)
 {
-    int i, sign;
-
-    sign = req->df ? -1 : 1;
+    uint32_t i;
 
     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),
-                  req->size, &req->data);
+                read_phys_req_item(req->addr, req, i, &req->data);
             }
         } else if (req->dir == IOREQ_WRITE) {
             for (i = 0; i < req->count; i++) {
-                write_physical(req->addr
-                  + (sign * i * req->size),
-                  req->size, &req->data);
+                write_phys_req_item(req->addr, req, i, &req->data);
             }
         }
     } else {
@@ -409,21 +418,13 @@ 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),
-                  req->size, &tmp);
-                write_physical((target_phys_addr_t )req->data
-                  + (sign * i * req->size),
-                  req->size, &tmp);
+                read_phys_req_item(req->addr, req, i, &tmp);
+                write_phys_req_item(req->data, req, i, &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),
-                  req->size, &tmp);
-                write_physical(req->addr
-                  + (sign * i * req->size),
-                  req->size, &tmp);
+                read_phys_req_item(req->data, req, i, &tmp);
+                write_phys_req_item(req->addr, req, i, &tmp);
             }
         }
     }
#21 lore raw

Stefano Stabellini writes ("Re: [Xen-devel] [PATCH 0/2] QEMU/xen: simplify cpu_ioreq_pio and cpu_ioreq_move"):

Quoted 1 line · Ian Jackson

...

Quoted 4 lines

i386-dm/helper2.c static void do_outp(CPUState *env, unsigned long addr, · line 359

Quoted 2 lines
i386-dm/helper2.c +2 -0
+    if (req->df) addr -= offset;
+    else addr -= offset;

This can't be right, can it?

Indeed not. v2 has this fixed.

Quoted 1 line

The search/replace changes below look correct.

Thanks.

Quoted 3 lines

For the sake of consistency, could you please send a patch against upstream QEMU to qemu-devel? The corresponding code is in xen-all.c (cpu_ioreq_pio and cpu_ioreq_move).

I will do that.

Thanks,
Ian.