From patchwork Wed Aug 13 19:04:38 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Musta X-Patchwork-Id: 379722 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3C4F6140090 for ; Thu, 14 Aug 2014 05:08:07 +1000 (EST) Received: from localhost ([::1]:49679 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XHdu0-0000e4-Vz for incoming@patchwork.ozlabs.org; Wed, 13 Aug 2014 15:08:05 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54809) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XHdrX-0004nY-Pw for qemu-devel@nongnu.org; Wed, 13 Aug 2014 15:05:40 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XHdrO-00043l-9O for qemu-devel@nongnu.org; Wed, 13 Aug 2014 15:05:31 -0400 Received: from mail-ob0-x22f.google.com ([2607:f8b0:4003:c01::22f]:58111) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XHdrN-00043h-Vx; Wed, 13 Aug 2014 15:05:22 -0400 Received: by mail-ob0-f175.google.com with SMTP id wp18so149219obc.20 for ; Wed, 13 Aug 2014 12:05:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Lbl54ClHUnRlcdV3it9ZhrUMYdB9S+lM6V/l2AnceDs=; b=APpeP4qiqCFuAfDx4Kxk8/1dYaKJRxeOgDzIPJKSAjB271kNK1znS7ZN8yFpkAu6p1 pCiXMiUCEUHDs8sl82RKiuY2dsn1g02fZnyhVsZjnuCs0rrRuzrsz0f7LZIWAJ5hazU4 e5qZ7DTbRorUGClmeWgRshOKBVePCgDjrI2X9l8XmDZB+zc3JX05UJvW223HrZ7J+Wjb JSOnmicPw15HfRhZMpRDsqcThOn5X0rJ82KhaFehNlh4MNXqgYL9Hz/2FVyily252Ip8 w9lxM5SxS8L5BV5fQV2TyS7+HOx/l3/Lm50EvsAKtyGmnZoy9vDjYEosr9T25ZU7grJD sHIQ== X-Received: by 10.60.161.49 with SMTP id xp17mr7187729oeb.18.1407956721355; Wed, 13 Aug 2014 12:05:21 -0700 (PDT) Received: from tmusta-sc.rchland.ibm.com (rchp4.rochester.ibm.com. [129.42.161.36]) by mx.google.com with ESMTPSA id ej4sm3032727obb.28.2014.08.13.12.05.16 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 13 Aug 2014 12:05:20 -0700 (PDT) From: Tom Musta To: qemu-devel@nongnu.org, qemu-ppc@nongnu.org Date: Wed, 13 Aug 2014 14:04:38 -0500 Message-Id: <1407956688-16006-4-git-send-email-tommusta@gmail.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1407956688-16006-1-git-send-email-tommusta@gmail.com> References: <1407956688-16006-1-git-send-email-tommusta@gmail.com> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:4003:c01::22f Cc: Tom Musta , riku.voipio@linaro.org, agraf@suse.de Subject: [Qemu-devel] [V3 PATCH 03/13] linux-user: Properly Handle semun Structure In Cross-Endian Situations 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 The semun union used in the semctl system call contains both an int (val) and pointers. In cross-endian situations on 64 bit targets, the value passed to semctl is an 8 byte (abi_long) value and thus does not have the 4-byte val field in the correct location. In order to rectify this, the other half of the union must be accessed. This is achieved in code by performing a byte swap on the entire 8 byte union, followed by a 4-byte swap of the first half. Also, eliminate an extraneous (dead) line of code that sets target_su.val in the IPC_SET/IPC_GET case. Signed-off-by: Tom Musta --- V2: Reworked per comments from Peter. linux-user/syscall.c | 13 +++++++++++-- 1 files changed, 11 insertions(+), 2 deletions(-) diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 229c482..bee1f4e 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -2647,9 +2647,18 @@ static inline abi_long do_semctl(int semid, int semnum, int cmd, switch( cmd ) { case GETVAL: case SETVAL: - arg.val = tswap32(target_su.val); + /* In 64 bit cross-endian situations, we will erroneously pick up + * the wrong half of the union for the "val" element. To rectify + * this, the entire 8-byte structure is byteswapped, followed by + * a swap of the 4 byte val field. In other cases, the data is + * already in proper host byte order. */ + if (sizeof(target_su.val) != (sizeof(target_su.buf))) { + target_su.buf = tswapal(target_su.buf); + arg.val = tswap32(target_su.val); + } else { + arg.val = target_su.val; + } ret = get_errno(semctl(semid, semnum, cmd, arg)); - target_su.val = tswap32(arg.val); break; case GETALL: case SETALL: