From patchwork Fri Oct 20 23:20:18 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 116589 Delivered-To: patch@linaro.org Received: by 10.140.22.164 with SMTP id 33csp2246080qgn; Fri, 20 Oct 2017 17:01:41 -0700 (PDT) X-Received: by 10.55.164.215 with SMTP id n206mr9174653qke.85.1508544100999; Fri, 20 Oct 2017 17:01:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1508544100; cv=none; d=google.com; s=arc-20160816; b=u3vjP8kbgzqIWAEoqf3ESeCLm/JzPecJbau6q3czN99z3UDMXvKL1xwYr2F3OO+w8D XD1BoXYo/GBasLqmhsUJHpC3rd8dqBfwjpE7NGBThx4G39TgpEe7EkpPvWJhSn+NQ9Uj cppc/njG1iXm0F1v9+VZ5fsKU3p8EX34k/URi7pykkmI6QN990OElmNWyrcr0EE+vRms ctZ8DVFsVHyg+rvkea/ltg+i1BGWWANpRJr3e/9iUuBYDNHnURHzs5+plH2CYpFgMQRp v98TvzxQfcHRLrKWA1vNc8du7qR6RmzfhuXPygWbfjWLszF4poB3a8B32K9hGS+LqmJA jUCQ== 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=nml9Y/s7PH6BnDtIOkX2bZqgao5/RjJckqYmGppXVGCQh1AuBGal1oEiOoIKU/rfA9 Vki0b3/mDl4c3u1XjORS/lfLq7dq/0YojVtUnoZ3J2STWkk2G3PuN4ZOD5R7mdGsd2Oc Y7l8T3bZKc6aIJ7ypkH5E0+Lkcl913pSrCSWHNetrpH5XgbVH+QVB45mC2mxY/+cZClJ M8Kk92U+A2TlLC+uKVrjzg0R/BgIPNubrUH91LrhOylc0TtVUaFqf5IJlZbtRvDFTFHa INWSVTA/Jh/yaosCZeLHprQUhXQ4EELSLqAn6qrPXBG/l9WtBf7g+jwlTsfoHNdJzxUX +gjg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=NN1zIJck; 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 s45si1795533qtk.103.2017.10.20.17.01.40 for (version=TLS1 cipher=AES128-SHA bits=128/128); Fri, 20 Oct 2017 17:01:40 -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=NN1zIJck; 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]:56153 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e5hEQ-0007M9-UU for patch@linaro.org; Fri, 20 Oct 2017 20:01:39 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44874) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e5gbg-0006k1-9n for qemu-devel@nongnu.org; Fri, 20 Oct 2017 19:21:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e5gbf-00082Q-B7 for qemu-devel@nongnu.org; Fri, 20 Oct 2017 19:21:36 -0400 Received: from mail-pf0-x243.google.com ([2607:f8b0:400e:c00::243]:47101) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1e5gbf-000822-52 for qemu-devel@nongnu.org; Fri, 20 Oct 2017 19:21:35 -0400 Received: by mail-pf0-x243.google.com with SMTP id p87so13075443pfj.3 for ; Fri, 20 Oct 2017 16:21:35 -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=NN1zIJckg8H7tn1IAxLujoRBxDOeZRFOUEbUwrgWkMZWd5GnMr22KgmrLFEPSsnEan rRPIGSxELSZooQotskHu2eGJFdw2U7HxkX8Uy2bHEykaAHkEP5kTGiEwKnLK+WG8ICmq bLoURVk3G5kHKwEHW3FFqokxNxnPB9RjFNSt4= 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=csCmlbaWNmS9mb+NoY/Ac62Ic8UumtRZr1CikwXjJeQImOoQNVfPVmZ5VeABR+HIpG NYuFWFnsQp2s6KTUfG2ekdTPUbMbLV7SJ4b81F7qxbr2kz4CrYGr5UcxT4PXdiSVpzMh 0KJo8+cP4I3+5g5VRT+UuGP5X90xgalj4+IzfGsEKAA9kq1J9t35Lkf4gJHE41nEYC8w nJ8FZoOXXDJpLhR5t2X3KvSZJQU4MJQvqb7b+YfCVvYxH3LPhaNq0aFsaw0Z+qRjdQll pww7jUxlUlwhaUe7IlzjVQth8i/ZAAFkO5FmTprg3QVejV5ohlqBDtsbw/duxvCBD2i3 6EOw== X-Gm-Message-State: AMCzsaV6ks+s8cq3J7itoaSoegOkjcAqnWSBAfn7DhX9iDdvDBNGdHfS lSNJ4OUFS1UZYxtvStZ8FkpkoewTRB4= X-Google-Smtp-Source: ABhQp+RcQjKmkiplMkGnbnOW8pbyFr0SQIFvyrc29NTtaG9Yel5yjGBBGVo2OL4pCyerHTtbgrPd5g== X-Received: by 10.99.158.82 with SMTP id r18mr5876743pgo.349.1508541693959; Fri, 20 Oct 2017 16:21:33 -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.32 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 20 Oct 2017 16:21:32 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Fri, 20 Oct 2017 16:20:18 -0700 Message-Id: <20171020232023.15010-48-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::243 Subject: [Qemu-devel] [PATCH v7 47/52] 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: pbonzini@redhat.com, cota@braap.org, f4bug@amsat.org 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;