From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56889) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1as1Y1-0002DG-Pg for qemu-devel@nongnu.org; Mon, 18 Apr 2016 01:16:34 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1as1Y0-0002Nr-Oe for qemu-devel@nongnu.org; Mon, 18 Apr 2016 01:16:33 -0400 From: David Gibson Date: Mon, 18 Apr 2016 15:17:45 +1000 Message-Id: <1460956667-9520-2-git-send-email-david@gibson.dropbear.id.au> In-Reply-To: <1460956667-9520-1-git-send-email-david@gibson.dropbear.id.au> References: <1460956667-9520-1-git-send-email-david@gibson.dropbear.id.au> Subject: [Qemu-devel] [PULL 1/3] ppc: Fix the range check in the LSWI instruction List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: peter.maydell@linaro.org Cc: agraf@suse.de, qemu-devel@nongnu.org, qemu-ppc@nongnu.org, Thomas Huth , David Gibson From: Thomas Huth There are two issues: First, the number of registers that are used has to be calculated with "(nb + 3) / 4" (i.e. round always up, not down). Second, the "start <= ra && (start + nr - 32) > ra" condition for the wrap-around case is wrong: It has to be tested with "||" instead of "&&". Since we can reuse this check later for the LSWX instruction, let's place the fixed code into a helper function, too. Signed-off-by: Thomas Huth Signed-off-by: David Gibson --- target-ppc/cpu.h | 10 ++++++++++ target-ppc/translate.c | 6 ++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/target-ppc/cpu.h b/target-ppc/cpu.h index 9d4e43c..5282533 100644 --- a/target-ppc/cpu.h +++ b/target-ppc/cpu.h @@ -2415,6 +2415,16 @@ static inline bool msr_is_64bit(CPUPPCState *env, target_ulong msr) return msr & (1ULL << MSR_SF); } +/** + * Check whether register rx is in the range between start and + * start + nregs (as needed by the LSWX and LSWI instructions) + */ +static inline bool lsw_reg_in_range(int start, int nregs, int rx) +{ + return (start + nregs <= 32 && rx >= start && rx < start + nregs) || + (start + nregs > 32 && (rx >= start || rx < start + nregs - 32)); +} + extern void (*cpu_ppc_hypercall)(PowerPCCPU *); #include "exec/exec-all.h" diff --git a/target-ppc/translate.c b/target-ppc/translate.c index 6f0e7b4..b3860ec 100644 --- a/target-ppc/translate.c +++ b/target-ppc/translate.c @@ -3227,10 +3227,8 @@ static void gen_lswi(DisasContext *ctx) if (nb == 0) nb = 32; - nr = nb / 4; - if (unlikely(((start + nr) > 32 && - start <= ra && (start + nr - 32) > ra) || - ((start + nr) <= 32 && start <= ra && (start + nr) > ra))) { + nr = (nb + 3) / 4; + if (unlikely(lsw_reg_in_range(start, nr, ra))) { gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX); return; } -- 2.5.5