From patchwork Fri Jan 15 22:46:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 363789 Delivered-To: patch@linaro.org Received: by 2002:a02:ccad:0:0:0:0:0 with SMTP id t13csp269711jap; Fri, 15 Jan 2021 14:47:49 -0800 (PST) X-Google-Smtp-Source: ABdhPJxlDDV/+U0Wy0Es6KBQa0r7MxrjjrIJigaJIheyZwY0FUZWDHSUnsnhgYk46rw431ToOpFt X-Received: by 2002:a25:296:: with SMTP id 144mr13695405ybc.212.1610750869354; Fri, 15 Jan 2021 14:47:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610750869; cv=none; d=google.com; s=arc-20160816; b=jloqcBmkZA+Dr8i2L2VeTizaj+B4pjwhnfteoShVviyKieaG8WSEfK7DG1lpG5yE0/ sDux17WuUbYbYcdGXhXbscHzOJ6PyEMaUk/Obgigwsl4SQ4Gw3+MPVZ383UhgvD76XtF mfaZF7eQgsAKkrDrngCm8yvxSWTTkzVoy0q0BS12u1Sbh8q1dfZRLrkWG6hXWrqFEC6R LinRnU2Nzb0DxqeE3f9VdArhYM0FinCA8Zx6oJD81CJdt/m9g7/rrZJL1rAB1P9Ff5gv BdIGxWJ9zWlvQErzSXDM2607aYAGkEXpY5DfyYc8Nj3G96J0Wuk4OpZiPhIoWi7gB1Dl WELQ== 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:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=OL7icxJH7P33DCDYfdrnfsZF+7Y7OOSJvgMIHgzmTZ8=; b=Apayo5cxDhEAmHsl5I4ij7OlBzrqjZwCg6ojoIM1MRmn1hRaIPd1z/DUghNyC1uKCG 2HLyOk0PlhTtaHQ5UmpAYDr5/zactpJte4uMoYAm1BCru/UJ0pCir/Vbwlaw36l3VDik w6sv8j2I+XU1TLE9u8uJAHDax8qpodSZn/711Bf5FwScs/E2CuWxA2vYhqL9EGAx5JMM Hv4QG9FuaE54mpnNaM9vZOoZfLU/4+w0nt/dh6kWrTZQJh5DxdOF4LWewPX6GDwVp1hg 9YiExiUq1bvHRvvmhR23ZPv4C9ViMxp241WhOFUJX4lYNYGqv+HrgTAWUnJ1ZUBrSwbR 5qWA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=jfhX8Er1; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 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. [209.51.188.17]) by mx.google.com with ESMTPS id n69si10087356yba.348.2021.01.15.14.47.49 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 15 Jan 2021 14:47:49 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=jfhX8Er1; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 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]:49920 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l0Xsi-0003W7-Jq for patch@linaro.org; Fri, 15 Jan 2021 17:47:48 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:53446) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l0Xrq-0003Uz-44 for qemu-devel@nongnu.org; Fri, 15 Jan 2021 17:46:54 -0500 Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]:46262) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1l0Xrn-0005B7-Ta for qemu-devel@nongnu.org; Fri, 15 Jan 2021 17:46:53 -0500 Received: by mail-pl1-x636.google.com with SMTP id u11so1155145plg.13 for ; Fri, 15 Jan 2021 14:46:51 -0800 (PST) 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 :mime-version:content-transfer-encoding; bh=OL7icxJH7P33DCDYfdrnfsZF+7Y7OOSJvgMIHgzmTZ8=; b=jfhX8Er1j525ffIEGeAZal2XJ2s/DSGXZ3KjBXMu+cR25Y3x/ptmmMeLvvDtT+VKT1 uqIm1oYCV2S0EZh6qcOD402YHPUjhpdTqDua5dk5S/9KP9BBt5OUsqisC9AJD4TeLe2H v+mD/pBfuQljEbfuSHu711Zrmoo7WiVzWw6+4hbk//XciT/3qXBOvmAdCL/zyFtQcE28 Q8hyyEgdQ6JGizj+5zvboJ+GoIO9E/i8vh+l9L8lOl9FVo95k8jD8GM4Ce/SKfOECJWD hNGZi0Id2DNLgWRA7WsdwuoUvAz3rzmaS3eV8JFF8WpYr16PPjTpOXHiJHqgV0K06s4p RvYg== 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:mime-version:content-transfer-encoding; bh=OL7icxJH7P33DCDYfdrnfsZF+7Y7OOSJvgMIHgzmTZ8=; b=hXIilZxXKFtvdF7/yEWL2VUdXhGY8PkpHubGC8N41cUCHu2SIrrDOJmq3oZLKwv9P3 Uwbe0bgsU4qolfX8+4m278VP6kC7kdLzZgWBCNWfFW+7SHSk9oh3jJ3auKtV49g61FFU GThIp8Tw5PvgdAZArW1MwlMe1CRQ8EFXGBQzr4sFA4TKsmaat7kwGhze9sPCVtmTcphF AjJKnZojnFNG90TIQfUC/ygzpV07A+Z0YKPN5RKRP/7kmVtE59UifYfH5Uyngj/pLnZ/ 1h9eonO5zwu/BUKP5yP9ebr8aJD89kZfRwJNIOJcl1BxQLtZIlzLJhGatqt/5QzuyU8n pDTQ== X-Gm-Message-State: AOAM530/MZFEdm6J1Ssdcaq3K4pgiJfMomrB8n99BLpEU9NjkU89cj9K bWX9gKzK3wNfQzPO0RLdgQoSxCYfY/wrZSwC X-Received: by 2002:a17:90a:fb92:: with SMTP id cp18mr12694500pjb.203.1610750810645; Fri, 15 Jan 2021 14:46:50 -0800 (PST) Received: from localhost.localdomain (rrcs-173-197-107-21.west.biz.rr.com. [173.197.107.21]) by smtp.gmail.com with ESMTPSA id s13sm9521700pfd.99.2021.01.15.14.46.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Jan 2021 14:46:50 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH v3 01/21] tcg: Introduce target-specific page data for user-only Date: Fri, 15 Jan 2021 12:46:25 -1000 Message-Id: <20210115224645.1196742-2-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210115224645.1196742-1-richard.henderson@linaro.org> References: <20210115224645.1196742-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::636; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x636.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org, qemu-arm@nongnu.org Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" This data can be allocated by page_alloc_target_data() and released by page_set_flags(start, end, prot | PAGE_RESET). This data will be used to hold tag memory for AArch64 MTE. Signed-off-by: Richard Henderson --- v3: Add doc comments; tweak alloc so that the !PAGE_VALID case is clear. --- include/exec/cpu-all.h | 42 +++++++++++++++++++++++++++++++++------ accel/tcg/translate-all.c | 28 ++++++++++++++++++++++++++ linux-user/mmap.c | 4 +++- linux-user/syscall.c | 4 ++-- 4 files changed, 69 insertions(+), 9 deletions(-) -- 2.25.1 Reviewed-by: Peter Maydell diff --git a/include/exec/cpu-all.h b/include/exec/cpu-all.h index 4b5408c341..99a09ee137 100644 --- a/include/exec/cpu-all.h +++ b/include/exec/cpu-all.h @@ -264,15 +264,21 @@ extern intptr_t qemu_host_page_mask; #define PAGE_EXEC 0x0004 #define PAGE_BITS (PAGE_READ | PAGE_WRITE | PAGE_EXEC) #define PAGE_VALID 0x0008 -/* original state of the write flag (used when tracking self-modifying - code */ +/* + * Original state of the write flag (used when tracking self-modifying code) + */ #define PAGE_WRITE_ORG 0x0010 -/* Invalidate the TLB entry immediately, helpful for s390x - * Low-Address-Protection. Used with PAGE_WRITE in tlb_set_page_with_attrs() */ -#define PAGE_WRITE_INV 0x0040 +/* + * Invalidate the TLB entry immediately, helpful for s390x + * Low-Address-Protection. Used with PAGE_WRITE in tlb_set_page_with_attrs() + */ +#define PAGE_WRITE_INV 0x0020 +/* For use with page_set_flags: page is being replaced; target_data cleared. */ +#define PAGE_RESET 0x0040 + #if defined(CONFIG_BSD) && defined(CONFIG_USER_ONLY) /* FIXME: Code that sets/uses this is broken and needs to go away. */ -#define PAGE_RESERVED 0x0020 +#define PAGE_RESERVED 0x0100 #endif /* Target-specific bits that will be used via page_get_flags(). */ #define PAGE_TARGET_1 0x0080 @@ -287,6 +293,30 @@ int walk_memory_regions(void *, walk_memory_regions_fn); int page_get_flags(target_ulong address); void page_set_flags(target_ulong start, target_ulong end, int flags); int page_check_range(target_ulong start, target_ulong len, int flags); + +/** + * page_alloc_target_data(address, size) + * @address: guest virtual address + * @size: size of data to allocate + * + * Allocate @size bytes of out-of-band data to associate with the + * guest page at @address. If the page is not mapped, NULL will + * be returned. If there is existing data associated with @address, + * no new memory will be allocated. + * + * The memory will be freed when the guest page is deallocated, + * e.g. with the munmap system call. + */ +void *page_alloc_target_data(target_ulong address, size_t size); + +/** + * page_get_target_data(address) + * @address: guest virtual address + * + * Return any out-of-bound memory assocated with the guest page + * at @address, as per page_alloc_target_data. + */ +void *page_get_target_data(target_ulong address); #endif CPUArchState *cpu_copy(CPUArchState *env); diff --git a/accel/tcg/translate-all.c b/accel/tcg/translate-all.c index e9de6ff9dd..7bcb6663f1 100644 --- a/accel/tcg/translate-all.c +++ b/accel/tcg/translate-all.c @@ -113,6 +113,7 @@ typedef struct PageDesc { unsigned int code_write_count; #else unsigned long flags; + void *target_data; #endif #ifndef CONFIG_USER_ONLY QemuSpin lock; @@ -2740,6 +2741,7 @@ int page_get_flags(target_ulong address) void page_set_flags(target_ulong start, target_ulong end, int flags) { target_ulong addr, len; + bool reset_target_data; /* This function should never be called with addresses outside the guest address space. If this assert fires, it probably indicates @@ -2754,6 +2756,8 @@ void page_set_flags(target_ulong start, target_ulong end, int flags) if (flags & PAGE_WRITE) { flags |= PAGE_WRITE_ORG; } + reset_target_data = !(flags & PAGE_VALID) || (flags & PAGE_RESET); + flags &= ~PAGE_RESET; for (addr = start, len = end - start; len != 0; @@ -2767,10 +2771,34 @@ void page_set_flags(target_ulong start, target_ulong end, int flags) p->first_tb) { tb_invalidate_phys_page(addr, 0); } + if (reset_target_data && p->target_data) { + g_free(p->target_data); + p->target_data = NULL; + } p->flags = flags; } } +void *page_get_target_data(target_ulong address) +{ + PageDesc *p = page_find(address >> TARGET_PAGE_BITS); + return p ? p->target_data : NULL; +} + +void *page_alloc_target_data(target_ulong address, size_t size) +{ + PageDesc *p = page_find(address >> TARGET_PAGE_BITS); + void *ret = NULL; + + if (p->flags & PAGE_VALID) { + ret = p->target_data; + if (!ret) { + p->target_data = ret = g_malloc0(size); + } + } + return ret; +} + int page_check_range(target_ulong start, target_ulong len, int flags) { PageDesc *p; diff --git a/linux-user/mmap.c b/linux-user/mmap.c index 810653c503..c693505b60 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -599,6 +599,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot, } } the_end1: + page_flags |= PAGE_RESET; page_set_flags(start, start + len, page_flags); the_end: trace_target_mmap_complete(start); @@ -792,7 +793,8 @@ abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size, new_addr = h2g(host_addr); prot = page_get_flags(old_addr); page_set_flags(old_addr, old_addr + old_size, 0); - page_set_flags(new_addr, new_addr + new_size, prot | PAGE_VALID); + page_set_flags(new_addr, new_addr + new_size, + prot | PAGE_VALID | PAGE_RESET); } tb_invalidate_phys_range(new_addr, new_addr + new_size); mmap_unlock(); diff --git a/linux-user/syscall.c b/linux-user/syscall.c index d182890ff0..bec2ab7769 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -4625,8 +4625,8 @@ static inline abi_ulong do_shmat(CPUArchState *cpu_env, raddr=h2g((unsigned long)host_raddr); page_set_flags(raddr, raddr + shm_info.shm_segsz, - PAGE_VALID | PAGE_READ | - ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE)); + PAGE_VALID | PAGE_RESET | PAGE_READ | + (shmflg & SHM_RDONLY ? 0 : PAGE_WRITE)); for (i = 0; i < N_SHM_REGIONS; i++) { if (!shm_regions[i].in_use) {