From patchwork Wed Oct 25 09:35:30 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 830217 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="cpb0wgby"; 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 3yMQv85h6Pz9t16 for ; Wed, 25 Oct 2017 21:13:08 +1100 (AEDT) Received: from localhost ([::1]:47464 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e7IgM-0005gc-S8 for incoming@patchwork.ozlabs.org; Wed, 25 Oct 2017 06:13:06 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46231) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e7I76-0000Qf-Mo for qemu-devel@nongnu.org; Wed, 25 Oct 2017 05:36:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e7I72-0000JG-MG for qemu-devel@nongnu.org; Wed, 25 Oct 2017 05:36:40 -0400 Received: from mail-wm0-x243.google.com ([2a00:1450:400c:c09::243]:55775) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1e7I72-0000If-F6 for qemu-devel@nongnu.org; Wed, 25 Oct 2017 05:36:36 -0400 Received: by mail-wm0-x243.google.com with SMTP id u138so626754wmu.4 for ; Wed, 25 Oct 2017 02:36: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=i1HvMWTORlTi4mrRA3t7lTf3wipFQA4Aj9Z7zWl118Y=; b=cpb0wgbyKAFuTGp3Rv6Efewb1vPfS+nm9JIPeIumpINMRMWpP6OtqV2SRPzsKihVHz Vd96Hypjf8YN1+hk5PxUtBFuXSCaVFsFpQW8vQYC2Pl3sYl0NwNr+iQwZTVCtHPVyJn0 q61l6tIImlaWi7LX+6VgWABGn99FvtsKXJvEQ= 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=i1HvMWTORlTi4mrRA3t7lTf3wipFQA4Aj9Z7zWl118Y=; b=QymzJuA/TE/1yUcm6lN35/t186GZ7t6sPKUup17EDKXlecQyEEAFV022qzyk7hKmTX rkQ3A6IFA2/f1l7AgzPBiT/OsmbrUkFymhqMU5NE/gKpmyH8+hyAKq1Jdehx7E2tv1qo Km5TPhccn0hkXGVrbK5cfkVBruyRxPp2Aeg+juqjrR23TDsea8HWFfE3MrOb9Oco/+NV M5ZFez7k4XWS5ZDv0WDXQYGfnUl0h/3jaHhDhQtXhbxnRlGqcsSr2I7P6Pa/0wh8gtbO i1Dl4l/U2FD5FN76X+e774XvC5enRpm3qYF213pugyzN2/KcKmGBnMEQBqcqODwkqLYh VPIA== X-Gm-Message-State: AMCzsaV1uMQJw7n1ZhVG4/tvBN8OUHte0Y93iHdnNNDf19SjOyRUOawj xznUS+HfFzJ6RnVDRuFy2SJUgxazQ1w= X-Google-Smtp-Source: ABhQp+T5uwY1eZwcThYC30WMk8WtGpuXwQPJvch+9x9MSCPOKvKef2mI7VbyLdU+lyeAZ7CEOtLDZw== X-Received: by 10.28.180.2 with SMTP id d2mr1072299wmf.118.1508924195292; Wed, 25 Oct 2017 02:36:35 -0700 (PDT) Received: from cloudburst.twiddle.net ([62.168.35.107]) by smtp.gmail.com with ESMTPSA id v23sm2751025wmh.8.2017.10.25.02.36.34 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 25 Oct 2017 02:36:34 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Wed, 25 Oct 2017 11:35:30 +0200 Message-Id: <20171025093535.10175-47-richard.henderson@linaro.org> X-Mailer: git-send-email 2.13.6 In-Reply-To: <20171025093535.10175-1-richard.henderson@linaro.org> References: <20171025093535.10175-1-richard.henderson@linaro.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c09::243 Subject: [Qemu-devel] [PULL 46/51] osdep: introduce qemu_mprotect_rwx/none 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, "Emilio G. Cota" Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" From: "Emilio G. Cota" Reviewed-by: Richard Henderson Signed-off-by: Emilio G. Cota Signed-off-by: Richard Henderson --- include/qemu/osdep.h | 2 ++ util/osdep.c | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h index 826650c58a..281782d526 100644 --- a/include/qemu/osdep.h +++ b/include/qemu/osdep.h @@ -371,6 +371,8 @@ void sigaction_invoke(struct sigaction *action, #endif int qemu_madvise(void *addr, size_t len, int advice); +int qemu_mprotect_rwx(void *addr, size_t size); +int qemu_mprotect_none(void *addr, size_t size); int qemu_open(const char *name, int flags, ...); int qemu_close(int fd); diff --git a/util/osdep.c b/util/osdep.c index a479fedc4a..1231f9f876 100644 --- a/util/osdep.c +++ b/util/osdep.c @@ -73,6 +73,47 @@ int qemu_madvise(void *addr, size_t len, int advice) #endif } +static int qemu_mprotect__osdep(void *addr, size_t size, int prot) +{ + g_assert(!((uintptr_t)addr & ~qemu_real_host_page_mask)); + g_assert(!(size & ~qemu_real_host_page_mask)); + +#ifdef _WIN32 + DWORD old_protect; + + if (!VirtualProtect(addr, size, prot, &old_protect)) { + error_report("%s: VirtualProtect failed with error code %ld", + __func__, GetLastError()); + return -1; + } + return 0; +#else + if (mprotect(addr, size, prot)) { + error_report("%s: mprotect failed: %s", __func__, strerror(errno)); + return -1; + } + return 0; +#endif +} + +int qemu_mprotect_rwx(void *addr, size_t size) +{ +#ifdef _WIN32 + return qemu_mprotect__osdep(addr, size, PAGE_EXECUTE_READWRITE); +#else + return qemu_mprotect__osdep(addr, size, PROT_READ | PROT_WRITE | PROT_EXEC); +#endif +} + +int qemu_mprotect_none(void *addr, size_t size) +{ +#ifdef _WIN32 + return qemu_mprotect__osdep(addr, size, PAGE_NOACCESS); +#else + return qemu_mprotect__osdep(addr, size, PROT_NONE); +#endif +} + #ifndef _WIN32 static int fcntl_op_setlk = -1;