From patchwork Thu Oct 11 19:22:14 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 190988 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 24EB52C007C for ; Fri, 12 Oct 2012 07:01:27 +1100 (EST) Received: from localhost ([::1]:43402 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TMOLQ-000499-6Z for incoming@patchwork.ozlabs.org; Thu, 11 Oct 2012 15:22:56 -0400 Received: from eggs.gnu.org ([208.118.235.92]:54814) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TMOL4-0003of-PY for qemu-devel@nongnu.org; Thu, 11 Oct 2012 15:22:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TMOKy-0001Xy-KO for qemu-devel@nongnu.org; Thu, 11 Oct 2012 15:22:34 -0400 Received: from mail-da0-f45.google.com ([209.85.210.45]:44224) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TMOKy-0001X3-Ej for qemu-devel@nongnu.org; Thu, 11 Oct 2012 15:22:28 -0400 Received: by mail-da0-f45.google.com with SMTP id n15so915041dad.4 for ; Thu, 11 Oct 2012 12:22:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references; bh=Gaxf08D912eUh9pTfT/ZybBCyqGh3kcC9a0/Hs+bLKA=; b=HOk3YUuIOWAfFkEAPYtZjtc/WP36LvI3yvqo1vFBTtLyvvlYoSxyusWuTlloEC2E6n nfZjkjR3vfbYsNX3KqMSM0r7pKFJNGZXCIF8Y9rHwQyfUp75CHp3P7BqPZLoWt6QAFdV k280tu6V2kCE4VTttP1c1YnaQ78VdP2UnIl3neObstwmUqcPbPLNLVL3VAlJMWw7Hxk0 JxQKDB0C3IrAMR8jwsiB7hh/nUG8XQBhLblRxXh8fOpGXIygR/VRS1w4Hz4QvOZGVPjU G3KkgIOp8MpPlQkU2gvZRJDfozpUtBJ2oyCXY08MhXN99BSbI2BaAvLF/+oGEEsUbyDc 6Zwg== Received: by 10.68.232.71 with SMTP id tm7mr6501961pbc.118.1349983347968; Thu, 11 Oct 2012 12:22:27 -0700 (PDT) Received: from anchor.twiddle.home.com (50-194-63-110-static.hfc.comcastbusiness.net. [50.194.63.110]) by mx.google.com with ESMTPS id wl4sm3153795pbc.17.2012.10.11.12.22.26 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 11 Oct 2012 12:22:27 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Thu, 11 Oct 2012 12:22:14 -0700 Message-Id: <1349983336-9974-5-git-send-email-rth@twiddle.net> X-Mailer: git-send-email 1.7.11.4 In-Reply-To: <1349983336-9974-1-git-send-email-rth@twiddle.net> References: <1349983336-9974-1-git-send-email-rth@twiddle.net> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.85.210.45 Cc: Riku Voipio Subject: [Qemu-devel] [PATCH 4/6] linux-user: Rewrite __get_user/__put_user with __builtin_choose_expr 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 previous formuation with multiple assignments to __typeof(*hptr) falls down when hptr is qualified const. E.g. with const struct S *p, p->f is also qualified const. With this formulation, there's no assignment to any local variable. Signed-off-by: Richard Henderson --- linux-user/qemu.h | 50 ++++++++++++++++++++------------------------------ 1 file changed, 20 insertions(+), 30 deletions(-) diff --git a/linux-user/qemu.h b/linux-user/qemu.h index fc4cc00..39b2c9c 100644 --- a/linux-user/qemu.h +++ b/linux-user/qemu.h @@ -284,36 +284,26 @@ static inline int access_ok(int type, abi_ulong addr, abi_ulong size) (type == VERIFY_READ) ? PAGE_READ : (PAGE_READ | PAGE_WRITE)) == 0; } -/* NOTE __get_user and __put_user use host pointers and don't check access. */ -/* These are usually used to access struct data members once the - * struct has been locked - usually with lock_user_struct(). - */ -#define __put_user(x, hptr)\ -({ __typeof(*hptr) pu_ = (x);\ - switch(sizeof(*hptr)) {\ - case 1: break;\ - case 2: pu_ = tswap16(pu_); break; \ - case 4: pu_ = tswap32(pu_); break; \ - case 8: pu_ = tswap64(pu_); break; \ - default: abort();\ - }\ - memcpy(hptr, &pu_, sizeof(pu_)); \ - 0;\ -}) - -#define __get_user(x, hptr) \ -({ __typeof(*hptr) gu_; \ - memcpy(&gu_, hptr, sizeof(gu_)); \ - switch(sizeof(*hptr)) {\ - case 1: break; \ - case 2: gu_ = tswap16(gu_); break; \ - case 4: gu_ = tswap32(gu_); break; \ - case 8: gu_ = tswap64(gu_); break; \ - default: abort();\ - }\ - (x) = gu_; \ - 0;\ -}) +/* NOTE __get_user and __put_user use host pointers and don't check access. + These are usually used to access struct data members once the struct has + been locked - usually with lock_user_struct. */ +#define __put_user(x, hptr) \ +(*(hptr) = \ + __builtin_choose_expr(sizeof(*(hptr)) == 1, (uint8_t)(x), \ + __builtin_choose_expr(sizeof(*(hptr)) == 2, tswap16((uint16_t)(x)), \ + __builtin_choose_expr(sizeof(*(hptr)) == 4, tswap32((uint32_t)(x)), \ + __builtin_choose_expr(sizeof(*(hptr)) == 8, tswap64((uint64_t)(x)), \ + abort())))), \ + 0) + +#define __get_user(x, hptr) \ +((x) = \ + __builtin_choose_expr(sizeof(*(hptr)) == 1, *(hptr), \ + __builtin_choose_expr(sizeof(*(hptr)) == 2, tswap16(*(hptr)), \ + __builtin_choose_expr(sizeof(*(hptr)) == 4, tswap32(*(hptr)), \ + __builtin_choose_expr(sizeof(*(hptr)) == 8, tswap64(*(hptr)), \ + abort())))), \ + 0) /* put_user()/get_user() take a guest address and check access */ /* These are usually used to access an atomic data type, such as an int,