From patchwork Wed Mar 23 00:58:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 553738 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:1248:0:0:0:0 with SMTP id z8csp2935328mag; Tue, 22 Mar 2022 18:03:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwExlTHKFOmLpMhD0yjNeXkKfG58ogYNSIdaLUYYkSN+TWxTCuL/U6aU/0OZ+6ogUI5sHlQ X-Received: by 2002:a0c:c404:0:b0:431:31c3:3d15 with SMTP id r4-20020a0cc404000000b0043131c33d15mr21976522qvi.116.1647997415470; Tue, 22 Mar 2022 18:03:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647997415; cv=none; d=google.com; s=arc-20160816; b=G68H9CS4Mp+fB1TLltIovTFwKXChHzFPoT42xk1uVM5XaALk38kMH1U5zcDXcePx29 s7s/uFkVn/L1TpjC1oL5QC4wES+e/24yeiub8PRSrulFPRS7b/x0CPEUsBY9FBmzk4Gf nSs+MkgGMLWpS7sZTRcDJV5xwdhLk1o7eWNZYh6M/gj8AxzJEnsllOSfhf10tNRMQ1wa Iu4tcMCt6mgUzt8+hS1CxCe/C2znb6GsFrrBSLFzD6JZoPvEilNJIELZlOz/S0idq/Rd HBmXLwPDWv9x9x36oTGIu84+qxU9PTm1wVrQ9RGq1L3gFCmLl1REHd2LGnRAMdy3UdNp KYfg== 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=EAopWqbVIHUB/SJc8DJm/9zhocJ0OwWkN+8ZoF29nMQ=; b=PI+wbtjGWi4z3w3NS2J6R2DrUEIRYM+0wlY+MUiqdShO7AenlVa30tLOksa6IR4SP5 K5v5cwoM6KgfBY1JNgU10nCR4APt7wn3bX5K5GGaxrgvlCZNcTpLBKYuPxfozZzAjXj1 6QzqNR44/x3kFkiJcy/Td6zIcrhAxcgMCzbB3GyqxFj0FrUe9dHAan6nW12qfmY64wlu LlbpC/lFv7oHzDjxtn41sLklUxcRtE4GURmG3pC3Hq/j9OFIdAFKLaQI5t6HmFkfVsNC HG6si5ZWC3K8S4lcLS+FisuYqIcFmO0WuwD0gFkUSpExwj+SATXyr8WYJCAKwu7WoSRM GYzA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=S5WsksFh; 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 ca8-20020ad45608000000b00440e46a7d85si3257647qvb.547.2022.03.22.18.03.35 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 22 Mar 2022 18:03:35 -0700 (PDT) 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=S5WsksFh; 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]:47562 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nWpPS-0004HN-VX for patch@linaro.org; Tue, 22 Mar 2022 21:03:35 -0400 Received: from eggs.gnu.org ([209.51.188.92]:56082) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nWpKr-0007s0-1Z for qemu-devel@nongnu.org; Tue, 22 Mar 2022 20:58:49 -0400 Received: from [2607:f8b0:4864:20::529] (port=44749 helo=mail-pg1-x529.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nWpKp-0001Un-Dm for qemu-devel@nongnu.org; Tue, 22 Mar 2022 20:58:48 -0400 Received: by mail-pg1-x529.google.com with SMTP id c11so13815770pgu.11 for ; Tue, 22 Mar 2022 17:58:47 -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 :mime-version:content-transfer-encoding; bh=EAopWqbVIHUB/SJc8DJm/9zhocJ0OwWkN+8ZoF29nMQ=; b=S5WsksFhqDUPx9yznbOTPTUdj7o8RlAGIgVaR0ZVHUPDJDrnGC3e6kJjwBTt4PYmvt AAqeK8cL4n+97CeucCslTMHmgoqWU3whTY77emq+1EOBe9N6NEhQX70Tc/FP7YlmJvFP w7dEHIHXaP6xpqD05ac6k+yZbNzTmnoiiCP42+O8T3wpBfKhXcLtHZ3VKWGj3+oFPWmS oEdFDNn+5FGPhUOFHqwzlUO+PzK7cR35sCflsnNnINFo4Wqn8iITjFv8PI8YFfL9HLcO cFoqft5UwNy4fy18uIoteNPchXvMKSz7jlsZ8c9QirG34S4+0GMXN3xW0gb3t1vurrg1 znsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=EAopWqbVIHUB/SJc8DJm/9zhocJ0OwWkN+8ZoF29nMQ=; b=0/WDtdz0iHVJDUl8S/9nFrF5vPR5OB7fhLfAruf1xRmy194muAp8EzOMMd3keypPwY dcPlh4g8/GI6rE5vgxPucuqt2hRsUQ4WwI9ldO5EGpft95skXA1pGD89nsbJdkbaorHw YQlrAtnjQkBjK/AP4ul4RWQtDBKMtXiL/HVcchc0SB3TG5QFdHuhChsCtmSVylo/s02G YbMU1q46wIlw4tP3XIOMPHLOJDYsTcjbi0Vzf65k2xI8pC7xP4NpE+1lZcNvu8MlIZCk J039eI7Iw0vw+vlBq0XUxHB58JjwL/vTACHssw82LSxL8Pz2+NciM6rJw4nfWnoIVnNb nQcw== X-Gm-Message-State: AOAM533e62FHfOOYe9CLY1ZtLGIbMg6vYO4xDNtb23bOH16enjrK18t/ HLn+BKTKCtgtSr6ZMwRuUo+al5ZM8jd/vQ== X-Received: by 2002:a63:1459:0:b0:381:7672:e79f with SMTP id 25-20020a631459000000b003817672e79fmr24116671pgu.214.1647997125799; Tue, 22 Mar 2022 17:58:45 -0700 (PDT) Received: from localhost.localdomain (174-21-142-130.tukw.qwest.net. [174.21.142.130]) by smtp.gmail.com with ESMTPSA id g70-20020a636b49000000b003823dd39d41sm11569167pgc.64.2022.03.22.17.58.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Mar 2022 17:58:45 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH v2 3/3] linux-user/arm: Implement __kernel_cmpxchg64 with host atomics Date: Tue, 22 Mar 2022 17:58:39 -0700 Message-Id: <20220323005839.94327-4-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220323005839.94327-1-richard.henderson@linaro.org> References: <20220323005839.94327-1-richard.henderson@linaro.org> MIME-Version: 1.0 X-Host-Lookup-Failed: Reverse DNS lookup failed for 2607:f8b0:4864:20::529 (failed) Received-SPF: pass client-ip=2607:f8b0:4864:20::529; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x529.google.com X-Spam_score_int: -6 X-Spam_score: -0.7 X-Spam_bar: / X-Spam_report: (-0.7 / 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, PDS_HP_HELO_NORDNS=0.659, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Maydell , laurent@vivier.eu Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" If CONFIG_ATOMIC64, we can use a host cmpxchg and provide atomicity across processes; otherwise we have no choice but to continue using start/end_exclusive. Reviewed-by: Peter Maydell Signed-off-by: Richard Henderson --- linux-user/arm/cpu_loop.c | 79 +++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 41 deletions(-) diff --git a/linux-user/arm/cpu_loop.c b/linux-user/arm/cpu_loop.c index aa2d777bf4..aae375d617 100644 --- a/linux-user/arm/cpu_loop.c +++ b/linux-user/arm/cpu_loop.c @@ -138,7 +138,7 @@ static void arm_kernel_cmpxchg32_helper(CPUARMState *env) } /* - * See the Linux kernel's Documentation/arm/kernel_user_helpers.txt + * See the Linux kernel's Documentation/arm/kernel_user_helpers.rst * Input: * r0 = pointer to oldval * r1 = pointer to newval @@ -155,57 +155,54 @@ static void arm_kernel_cmpxchg64_helper(CPUARMState *env) { uint64_t oldval, newval, val; uint32_t addr, cpsr; + uint64_t *host_addr; - /* Based on the 32 bit code in do_kernel_trap */ + addr = env->regs[0]; + if (get_user_u64(oldval, addr)) { + goto segv; + } - /* XXX: This only works between threads, not between processes. - It's probably possible to implement this with native host - operations. However things like ldrex/strex are much harder so - there's not much point trying. */ - start_exclusive(); - cpsr = cpsr_read(env); + addr = env->regs[1]; + if (get_user_u64(newval, addr)) { + goto segv; + } + + mmap_lock(); addr = env->regs[2]; - - if (get_user_u64(oldval, env->regs[0])) { - env->exception.vaddress = env->regs[0]; - goto segv; - }; - - if (get_user_u64(newval, env->regs[1])) { - env->exception.vaddress = env->regs[1]; - goto segv; - }; - - if (get_user_u64(val, addr)) { - env->exception.vaddress = addr; - goto segv; + host_addr = atomic_mmu_lookup(env, addr, 8); + if (!host_addr) { + mmap_unlock(); + return; } +#ifdef CONFIG_ATOMIC64 + val = qatomic_cmpxchg__nocheck(host_addr, oldval, newval); + cpsr = (val == oldval) * CPSR_C; +#else + /* + * This only works between threads, not between processes, but since + * the host has no 64-bit cmpxchg, it is the best that we can do. + */ + start_exclusive(); + val = *host_addr; if (val == oldval) { - val = newval; - - if (put_user_u64(val, addr)) { - env->exception.vaddress = addr; - goto segv; - }; - - env->regs[0] = 0; - cpsr |= CPSR_C; + *host_addr = newval; + cpsr = CPSR_C; } else { - env->regs[0] = -1; - cpsr &= ~CPSR_C; + cpsr = 0; } - cpsr_write(env, cpsr, CPSR_C, CPSRWriteByInstr); end_exclusive(); +#endif + mmap_unlock(); + + cpsr_write(env, cpsr, CPSR_C, CPSRWriteByInstr); + env->regs[0] = cpsr ? 0 : -1; return; -segv: - end_exclusive(); - /* We get the PC of the entry address - which is as good as anything, - on a real kernel what you get depends on which mode it uses. */ - /* XXX: check env->error_code */ - force_sig_fault(TARGET_SIGSEGV, TARGET_SEGV_MAPERR, - env->exception.vaddress); + segv: + force_sig_fault(TARGET_SIGSEGV, + page_get_flags(addr) & PAGE_VALID ? + TARGET_SEGV_ACCERR : TARGET_SEGV_MAPERR, addr); } /* Handle a jump to the kernel code page. */