From patchwork Mon Sep 10 18:06:23 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Stabellini X-Patchwork-Id: 182940 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 9A3D32C0090 for ; Tue, 11 Sep 2012 04:07:14 +1000 (EST) Received: from localhost ([::1]:34424 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TB8O8-0005oC-JI for incoming@patchwork.ozlabs.org; Mon, 10 Sep 2012 14:07:12 -0400 Received: from eggs.gnu.org ([208.118.235.92]:46649) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TB8Nu-0005nt-Qv for qemu-devel@nongnu.org; Mon, 10 Sep 2012 14:07:02 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TB8Nq-00023N-Pe for qemu-devel@nongnu.org; Mon, 10 Sep 2012 14:06:58 -0400 Received: from smtp02.citrix.com ([66.165.176.63]:48582) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TB8Nq-00023B-J6 for qemu-devel@nongnu.org; Mon, 10 Sep 2012 14:06:54 -0400 X-IronPort-AV: E=Sophos;i="4.80,398,1344211200"; d="scan'208";a="207623410" Received: from ftlpmailmx02.citrite.net ([10.13.107.66]) by FTLPIPO02.CITRIX.COM with ESMTP/TLS/RC4-MD5; 10 Sep 2012 18:06:54 +0000 Received: from ukmail1.uk.xensource.com (10.80.16.128) by smtprelay.citrix.com (10.13.107.66) with Microsoft SMTP Server id 8.3.279.1; Mon, 10 Sep 2012 14:06:53 -0400 Received: from kaball.uk.xensource.com ([10.80.2.59]) by ukmail1.uk.xensource.com with esmtp (Exim 4.69) (envelope-from ) id 1TB8Nk-0005UC-44; Mon, 10 Sep 2012 19:06:48 +0100 From: Stefano Stabellini To: xen-devel@lists.xensource.com Date: Mon, 10 Sep 2012 19:06:23 +0100 Message-ID: <1347300383-9089-2-git-send-email-stefano.stabellini@eu.citrix.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: References: MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 66.165.176.63 Cc: dongxiao.xu@intel.com, Ian.Jackson@eu.citrix.com, qemu-devel@nongnu.org, Stefano Stabellini Subject: [Qemu-devel] [PATCH 2/2] introduce read_physical_offset and write_physical_offset X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 --- 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); } }