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: 117094 Delivered-To: patch@linaro.org Received: by 10.140.22.164 with SMTP id 33csp664975qgn; Wed, 25 Oct 2017 03:13:57 -0700 (PDT) X-Google-Smtp-Source: ABhQp+TNP+lLTwhgaJgVUbpvTvxUicgBflJj23Z9r89qKBXMEX3s58ivt9bK4nnw9yul2DPfyFM2 X-Received: by 10.37.111.135 with SMTP id k129mr864666ybc.507.1508926437436; Wed, 25 Oct 2017 03:13:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1508926437; cv=none; d=google.com; s=arc-20160816; b=L7YTHgkYKm55HhqKpoOvg4wHZ3KkGquK/sSAFfFUFSZ0/6+ayIbOqAUqpBt2op3dPs dOS244oxs0Iud9uZNxwugdJP39EYL0JNXiY1SrIjhV1Z8l8YwPZTWXlhBTassDy7i+ZK V/ZT0kBh9O9D5JH+wdErZpb358iVwn0zoF3RAiZzr4f5DNel9YCreLqRiPRt2Tdg9tl/ NevQW9PrJPUvubH4a13JuchelecqvXprsGB4No+sbs25GDYQtKnP/yh1cCHUnaA0YRpK 26VfmW9fOoQ+JUBTHwM/5K9NUxuWUeSkN8A4Z524csxG6XSla9EtcuaF8JOpIZntqcaU 4HYQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject:references:in-reply-to :message-id:date:to:from:dkim-signature:arc-authentication-results; bh=i1HvMWTORlTi4mrRA3t7lTf3wipFQA4Aj9Z7zWl118Y=; b=yGiDEuSAnAxrAkkJXMUCA81Ys10PzmAlkTyIgvqbUmcGGr3KCd/sZ1UPMyTbXOMeDp t2bLwOZSyqLrebmiX8PtQ5n5QNX/t/Ghpk5z2YjkeZikCW0vg2s1qQGfQ9lcqYs3suxn 9idOIvsk9VUuS2GCJ4TPAYTOtmOaXReUwsxx/eanr042BP6eHBx5n6ZHIKrPr2IUM3Eu JGHFrMBSHRlVzNX8nTRotAozAC0XZseg+cEUw90W1QOu//xQtI8Q4rOBMiF2U3+tS6eF bANAZWfQE8pqxX0pnSmFY7qnWBHYorU/afE2MhKwT7swmngsZy7aFDd28cTweK3LQaCJ ijHg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=cpb0wgby; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom=qemu-devel-bounces+patch=linaro.org@nongnu.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id o80si364112yba.510.2017.10.25.03.13.57 for (version=TLS1 cipher=AES128-SHA bits=128/128); Wed, 25 Oct 2017 03:13:57 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=cpb0wgby; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom=qemu-devel-bounces+patch=linaro.org@nongnu.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:47467 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e7IhA-0006I3-Vm for patch@linaro.org; Wed, 25 Oct 2017 06:13:57 -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-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+patch=linaro.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(+) -- 2.13.6 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;