From patchwork Fri May 13 00:13:05 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 9087011 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 9133BBF29F for ; Fri, 13 May 2016 00:19:09 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id DCF0320204 for ; Fri, 13 May 2016 00:19:08 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id EBFA7200D0 for ; Fri, 13 May 2016 00:19:07 +0000 (UTC) Received: from localhost ([::1]:60336 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b10ot-00081q-1v for patchwork-qemu-devel@patchwork.kernel.org; Thu, 12 May 2016 20:19:07 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38334) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b10kQ-0008NQ-Kj for qemu-devel@nongnu.org; Thu, 12 May 2016 20:14:35 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b10kO-0002fJ-9Y for qemu-devel@nongnu.org; Thu, 12 May 2016 20:14:29 -0400 Received: from mail-qg0-x243.google.com ([2607:f8b0:400d:c04::243]:33277) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b10kO-0002fD-5a for qemu-devel@nongnu.org; Thu, 12 May 2016 20:14:28 -0400 Received: by mail-qg0-x243.google.com with SMTP id 90so6865319qgz.0 for ; Thu, 12 May 2016 17:14: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:in-reply-to:references; bh=QTzWWCNV3orua38JW7ID8nySwAcLSXmA0TC8mvwAJDU=; b=GV9IyMojoGv2zysTTakjRDGtubPQSH6VCCYAslnEoO8tO/W+QGx2VDlQYCsRNXdlD1 58K9eqil2G3f9nzVyLnegzxdLd79aEFthFO1nwMB+EcZy3qIjmnCll/iXWQZLOy/cy3P V7aA53D3Di7sDSw/AmX+nKoiCFsOWI+JnpxO/RVPtGnXDOPcNNtMOkF4pKCs7CMMA2Fp v/tVnKpE59p1PKI3BkXB1z811XGvBAz8WLdjcT2sh1qGEoJh2EBSetfxKYI2I0QyUyYw 7iQYupRmzjJCTV93ZEXJqBDA2Ef99vLkw5GMJ1hTM14UhFao3txTH2tOTC+1n/DQiEtL B/7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=QTzWWCNV3orua38JW7ID8nySwAcLSXmA0TC8mvwAJDU=; b=mwKgp+H5NJ9b2St4KhO+QrnJOuHErknGO8coYKXTq5YBrlCPQaLvoHhP88xBqFdg7+ /J6/cOaWVH6oyokWvIYUsCd0VGz3wAsGfTZgSkoi3c8Z7pZnvxZf0CIEP+70ib7vzuT8 Ogq9o5Quv42FvW+x85nNZpOBUUlN8UufyPxACTR6Eli+w+LyGmm5hnilKPbpTCaATnTM H6CfzDo+pWbXQ5Ro1uM0HyAypBXeL6aj2rDCxDBqPXu2fBIQcCofJnZsWOcfVlINC1wm I+YsFU3AndPxynCX2dG3Z2ik8+BWXj+tMGfG9WKv9Ku5uzq/teKi3V0D6LlugUwRbChl rV3A== X-Gm-Message-State: AOPr4FUk+xVbC1WTMqXeo2PHH95jEr6EhbZKrc/WwU12JQ+XtHpHeihoeKZZWyOdjpwO8g== X-Received: by 10.140.220.137 with SMTP id q131mr13126936qhb.60.1463098467818; Thu, 12 May 2016 17:14:27 -0700 (PDT) Received: from bigtime.com ([172.56.44.137]) by smtp.gmail.com with ESMTPSA id w16sm7171321qka.35.2016.05.12.17.14.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 12 May 2016 17:14:27 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Thu, 12 May 2016 14:13:05 -1000 Message-Id: <1463098420-29113-5-git-send-email-rth@twiddle.net> X-Mailer: git-send-email 2.5.5 In-Reply-To: <1463098420-29113-1-git-send-email-rth@twiddle.net> References: <1463098420-29113-1-git-send-email-rth@twiddle.net> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:400d:c04::243 Subject: [Qemu-devel] [PULL 04/39] tci: Make direct jump patching thread-safe X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org, Sergey Fedorov , Sergey Fedorov Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Sergey Fedorov Ensure direct jump patching in TCI is atomic by: * naturally aligning a location of direct jump address; * using atomic_read()/atomic_set() to load/store the address. Signed-off-by: Sergey Fedorov Signed-off-by: Sergey Fedorov Message-Id: <1461341333-19646-4-git-send-email-sergey.fedorov@linaro.org> Signed-off-by: Richard Henderson --- include/exec/exec-all.h | 2 +- tcg/tci/tcg-target.inc.c | 2 ++ tci.c | 5 ++++- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/include/exec/exec-all.h b/include/exec/exec-all.h index c75fb3a..d49befd 100644 --- a/include/exec/exec-all.h +++ b/include/exec/exec-all.h @@ -303,7 +303,7 @@ void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr); static inline void tb_set_jmp_target1(uintptr_t jmp_addr, uintptr_t addr) { /* patch the branch destination */ - *(uint32_t *)jmp_addr = addr - (jmp_addr + 4); + atomic_set((int32_t *)jmp_addr, addr - (jmp_addr + 4)); /* no need to flush icache explicitly */ } #elif defined(_ARCH_PPC) diff --git a/tcg/tci/tcg-target.inc.c b/tcg/tci/tcg-target.inc.c index e2fc52a..85eeb5d 100644 --- a/tcg/tci/tcg-target.inc.c +++ b/tcg/tci/tcg-target.inc.c @@ -556,6 +556,8 @@ static void tcg_out_op(TCGContext *s, TCGOpcode opc, const TCGArg *args, if (s->tb_jmp_offset) { /* Direct jump method. */ tcg_debug_assert(args[0] < ARRAY_SIZE(s->tb_jmp_offset)); + /* Align for atomic patching and thread safety */ + s->code_ptr = QEMU_ALIGN_PTR_UP(s->code_ptr, 4); s->tb_jmp_offset[args[0]] = tcg_current_code_size(s); tcg_out32(s, 0); } else { diff --git a/tci.c b/tci.c index 82705fe..a8939e6 100644 --- a/tci.c +++ b/tci.c @@ -1089,7 +1089,10 @@ uintptr_t tcg_qemu_tb_exec(CPUArchState *env, uint8_t *tb_ptr) goto exit; break; case INDEX_op_goto_tb: - t0 = tci_read_i32(&tb_ptr); + /* Jump address is aligned */ + tb_ptr = QEMU_ALIGN_PTR_UP(tb_ptr, 4); + t0 = atomic_read((int32_t *)tb_ptr); + tb_ptr += sizeof(int32_t); tci_assert(tb_ptr == old_code_ptr + op_size); tb_ptr += (int32_t)t0; continue;