From patchwork Fri Oct 20 23:20:19 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 828893 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=2001:4830:134:3::11; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="JqDfNprn"; dkim-atps=neutral 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 3yJjY05sMHz9t38 for ; Sat, 21 Oct 2017 11:03:00 +1100 (AEDT) Received: from localhost ([::1]:56158 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e5hFj-00085g-11 for incoming@patchwork.ozlabs.org; Fri, 20 Oct 2017 20:02:59 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44903) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e5gbi-0006kS-4r for qemu-devel@nongnu.org; Fri, 20 Oct 2017 19:21:44 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e5gbh-00083I-4D for qemu-devel@nongnu.org; Fri, 20 Oct 2017 19:21:38 -0400 Received: from mail-pf0-x241.google.com ([2607:f8b0:400e:c00::241]:55384) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1e5gbg-000834-UF for qemu-devel@nongnu.org; Fri, 20 Oct 2017 19:21:37 -0400 Received: by mail-pf0-x241.google.com with SMTP id 17so13068272pfn.12 for ; Fri, 20 Oct 2017 16:21:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=QAy5P/DiR0V6uHx0vkD7O4ZvsE5YRZZNjBKdzWG/Qf4=; b=JqDfNprn1hcLMHRHINfnIEUsmtrJ/CoedxR10ANG66q3JqH1inrwRt2k4Ff3RI0yzZ 1Wx/F/fnt85tytda6peTPpfus1xoImJa5UkzwEAMloKaSscX4C3VBNdCiFcrqo2NKbgc QJvkwzIBrfa9xUYYM13asz78qNqqu9VrvAV3g= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=QAy5P/DiR0V6uHx0vkD7O4ZvsE5YRZZNjBKdzWG/Qf4=; b=UBiAQBv+iCcULpZEUbw4a32q3OSVdSt42rn5q0er+2YVBTWt4iCXZ4rzu55P0YwTxv X25+Q2R2Le4DFkOQ3Ms0OMHmYRRecZVHZWgK4HNBcXPoAJEs5Ln+iYXTIgUyo62T5wBh J4xCEBVSFVNcK5+o6DOTLjnnsmOMMpkAUzmgXC4alYsfWSZP86mx1JBhY/pn2+bISEmQ WzjP0G+tkdd+BpDEBSU3EhBh0DtSv2yGYeqFOE/37RKJBq0Ie23nEqvJ3zI11NGHdPoQ g+l8tnUSEaUFHCsOOqDWEJS4jDjvmIWtZrPn+k6DV7Ya6w/Z7YCB50YNWNDeRCFpqrvW zyig== X-Gm-Message-State: AMCzsaWjV5xX2I9F5fs3TbJVmOOb9ecFpq3X9sVAVWEI5UyKwVfiuT1D AdGtJTE8PLXW/gmFvGJAio7XUGUbNFo= X-Google-Smtp-Source: ABhQp+T+KT0w59fgp3EXCx+bpt92qF/2/MZjl+tdoqzlUk6eSzH4V4BYlZnAG2n1unYk21Z9PncZ5A== X-Received: by 10.99.7.133 with SMTP id 127mr5753738pgh.147.1508541695669; Fri, 20 Oct 2017 16:21:35 -0700 (PDT) Received: from cloudburst.twiddle.net (97-113-165-104.tukw.qwest.net. [97.113.165.104]) by smtp.gmail.com with ESMTPSA id a17sm3532594pfk.173.2017.10.20.16.21.34 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 20 Oct 2017 16:21:34 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Fri, 20 Oct 2017 16:20:19 -0700 Message-Id: <20171020232023.15010-49-richard.henderson@linaro.org> X-Mailer: git-send-email 2.13.6 In-Reply-To: <20171020232023.15010-1-richard.henderson@linaro.org> References: <20171020232023.15010-1-richard.henderson@linaro.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c00::241 Subject: [Qemu-devel] [PATCH v7 48/52] translate-all: use qemu_protect_rwx/none helpers 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: pbonzini@redhat.com, cota@braap.org, f4bug@amsat.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" From: "Emilio G. Cota" The helpers require the address and size to be page-aligned, so do that before calling them. Reviewed-by: Richard Henderson Signed-off-by: Emilio G. Cota Signed-off-by: Richard Henderson --- accel/tcg/translate-all.c | 61 ++++++++++------------------------------------- 1 file changed, 13 insertions(+), 48 deletions(-) diff --git a/accel/tcg/translate-all.c b/accel/tcg/translate-all.c index 78c150af3e..9061c0508c 100644 --- a/accel/tcg/translate-all.c +++ b/accel/tcg/translate-all.c @@ -602,63 +602,24 @@ static inline void *split_cross_256mb(void *buf1, size_t size1) static uint8_t static_code_gen_buffer[DEFAULT_CODE_GEN_BUFFER_SIZE] __attribute__((aligned(CODE_GEN_ALIGN))); -# ifdef _WIN32 -static inline void do_protect(void *addr, long size, int prot) -{ - DWORD old_protect; - VirtualProtect(addr, size, prot, &old_protect); -} - -static inline void map_exec(void *addr, long size) -{ - do_protect(addr, size, PAGE_EXECUTE_READWRITE); -} - -static inline void map_none(void *addr, long size) -{ - do_protect(addr, size, PAGE_NOACCESS); -} -# else -static inline void do_protect(void *addr, long size, int prot) -{ - uintptr_t start, end; - - start = (uintptr_t)addr; - start &= qemu_real_host_page_mask; - - end = (uintptr_t)addr + size; - end = ROUND_UP(end, qemu_real_host_page_size); - - mprotect((void *)start, end - start, prot); -} - -static inline void map_exec(void *addr, long size) -{ - do_protect(addr, size, PROT_READ | PROT_WRITE | PROT_EXEC); -} - -static inline void map_none(void *addr, long size) -{ - do_protect(addr, size, PROT_NONE); -} -# endif /* WIN32 */ - static inline void *alloc_code_gen_buffer(void) { void *buf = static_code_gen_buffer; + void *end = static_code_gen_buffer + sizeof(static_code_gen_buffer); size_t full_size, size; - /* The size of the buffer, rounded down to end on a page boundary. */ - full_size = (((uintptr_t)buf + sizeof(static_code_gen_buffer)) - & qemu_real_host_page_mask) - (uintptr_t)buf; + /* page-align the beginning and end of the buffer */ + buf = QEMU_ALIGN_PTR_UP(buf, qemu_real_host_page_size); + end = QEMU_ALIGN_PTR_DOWN(end, qemu_real_host_page_size); /* Reserve a guard page. */ + full_size = end - buf; size = full_size - qemu_real_host_page_size; /* Honor a command-line option limiting the size of the buffer. */ if (size > tcg_ctx->code_gen_buffer_size) { - size = (((uintptr_t)buf + tcg_ctx->code_gen_buffer_size) - & qemu_real_host_page_mask) - (uintptr_t)buf; + size = QEMU_ALIGN_DOWN(tcg_ctx->code_gen_buffer_size, + qemu_real_host_page_size); } tcg_ctx->code_gen_buffer_size = size; @@ -669,8 +630,12 @@ static inline void *alloc_code_gen_buffer(void) } #endif - map_exec(buf, size); - map_none(buf + size, qemu_real_host_page_size); + if (qemu_mprotect_rwx(buf, size)) { + abort(); + } + if (qemu_mprotect_none(buf + size, qemu_real_host_page_size)) { + abort(); + } qemu_madvise(buf, size, QEMU_MADV_HUGEPAGE); return buf;