Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp2150989rdh; Sat, 25 Nov 2023 16:06:58 -0800 (PST) X-Google-Smtp-Source: AGHT+IFeu8hi4IAU2s587F/QlUylnL6oVp+h6YxYjgxlCLt+RPVvToogIiw9bpQk0YgBd09ROZzf X-Received: by 2002:a05:6a20:3c8c:b0:18c:14e2:ad8c with SMTP id b12-20020a056a203c8c00b0018c14e2ad8cmr5482185pzj.25.1700957218614; Sat, 25 Nov 2023 16:06:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700957218; cv=none; d=google.com; s=arc-20160816; b=bMBi3KhJalHXlpEWgSPjE4uuklK97Pt1aKuaSAWG8Bei29KoEuJiP/98e+XBUHUkrb O2w7KR8qI/QXSr72Aw+CoRHa8TLeQnYczTt20389rGlU9j2dXl9irKI5y8L4195DXqOz pyqGzCDtOxsBWtqDhRDfnw1PcXR8zZWTEz9WjUOrJxUFwULgZsnV/YHQS3u3kMWT7Ai9 CwZ3YPk02NOqd4QBZ8EPWh07sAVPwQHekmzp7+rfjK7KurHb8Qf7KgTnl0i7iJhdhiYn FJkzBwuupaeNK0bMSFCFWs1szis44PyZPIo3eE7jAgQBAOvu9A2u/xRDGCHR4KOc2uL+ G2bA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=NZyjHH024UtJYC8nd5F0O4jycJThUYyuQjeJaYfroYQ=; fh=HZDsTHC0uAwNtvF960pExCYj71QbG0FbZeLrx0rXL4g=; b=YnQLodHDtXydrR+YuKtHGrorQbV9YqbXJnfETYue8Uqn33trvT6ilgAP0kVzdlMBVq cCPNqtzRpmFmLhn2Vy+Bsr6ZGGHnXgFoyCg2+v1dyp4x4xCxQdE3+L6qj1I+BOurJQPo DRFqIpCO7Dycz13Vy/9CPyY61gZatUfo2OeXMfDOfXidzQnHJOlNCE087muVRPQGathd Pj7osNxww+bwDtV7zTEhfUqKp4cqOWPpSAHhnfRBDMgRG43mPz1gxHezNjd2e3ullDZM sHkWOmJE/dXVGusGsy10CGy/y2NAPL+BXfhlv+4X0zFRMUGOM/NhNY5aXVU8C0JMp4Zg CFkg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=X0RTjRPt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=sifive.com Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id nv7-20020a17090b1b4700b00285b7d57ef0si822457pjb.137.2023.11.25.16.06.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Nov 2023 16:06:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=X0RTjRPt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=sifive.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id E330A8088689; Sat, 25 Nov 2023 16:06:51 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229505AbjKZAGg (ORCPT + 99 others); Sat, 25 Nov 2023 19:06:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45566 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229448AbjKZAGe (ORCPT ); Sat, 25 Nov 2023 19:06:34 -0500 Received: from mail-io1-xd2c.google.com (mail-io1-xd2c.google.com [IPv6:2607:f8b0:4864:20::d2c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DB81A127 for ; Sat, 25 Nov 2023 16:06:40 -0800 (PST) Received: by mail-io1-xd2c.google.com with SMTP id ca18e2360f4ac-7b37dbf896eso60941939f.1 for ; Sat, 25 Nov 2023 16:06:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1700957200; x=1701562000; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=NZyjHH024UtJYC8nd5F0O4jycJThUYyuQjeJaYfroYQ=; b=X0RTjRPthpgEryiNF+PMCTxgwRhMjbAXhrCLMNqh5KF5BmuAGpKseLedeEh7ZNL1Zq 6BUaxoBFRjb6pz0vvr4htmuvpYcOFffMDa9jj6ocr12iIr8STwot5M2XMrrbNukT7Lha p6BwqRtPkb8c6mdZaXwqfRH8K6BqrsIyr3sUvNOQpLD8gd5HueEDUakCV7mvmKQyZJD3 0Za1RYzdQuHi/Lc+yPk5Gha32aFZHQ1peTq5AGrv9PNaDPQnvxl6gi4oZRNsyNOqBzAb fkyiNo5qeT+8lTiOM2grx0FnU1iv1BKOA9G831BcQ/aqO8dZQiZDQAjv+b1+cQ+dt20R xzeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700957200; x=1701562000; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=NZyjHH024UtJYC8nd5F0O4jycJThUYyuQjeJaYfroYQ=; b=KHMYecX7knSxfueGMlyA2AFNQ5j8pCTj7WgET1t6+7kA5WTHEsgyDYrTlNTuLRdc// tnx/i0yYlVMKMwSijbIDNlgHF+NFYvbknU5mAJwGtu/+Yjg0z+TAOSoqpeeKSzV8z/GK wXUUWf30B+NFm22aQi1Jj+YwqkaXlmn8aPB9zqYEHrTnobfUuE5TI5viPhqV+1N8fHEm gLmVGkqiU8P2KghA+6b92jHB2p2StIDjWYVQjRDwb8fs5AgrygEmXnZhKETBwqpui4tq A1VZalhuIwq9jSpxwkfUO0Bo9XYJeP3w7iDtamOkFQOfZunJvwWsSE3Xa3HD83VAxaqj Urhg== X-Gm-Message-State: AOJu0YzPn4gU/pPz6OHz5JFm0db6M7jFHKuEytqVxqKE+mwjaDQoWDpi EQEAaEialgufkJRr/8YDNZCqeg== X-Received: by 2002:a5e:8614:0:b0:7b3:84f4:f003 with SMTP id z20-20020a5e8614000000b007b384f4f003mr3186196ioj.19.1700957200239; Sat, 25 Nov 2023 16:06:40 -0800 (PST) Received: from ?IPV6:2605:a601:adae:4500:169:4685:f118:f646? ([2605:a601:adae:4500:169:4685:f118:f646]) by smtp.gmail.com with ESMTPSA id m7-20020a02cdc7000000b004667006c370sm1564707jap.76.2023.11.25.16.06.39 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 25 Nov 2023 16:06:39 -0800 (PST) Message-ID: <17d0646b-bcd5-48ff-a55c-d5cf88cdd015@sifive.com> Date: Sat, 25 Nov 2023 18:06:38 -0600 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] riscv: Fix SMP when shadow call stacks are enabled Content-Language: en-US To: Conor Dooley Cc: Palmer Dabbelt , linux-riscv@lists.infradead.org, Albert Ou , Andy Chiu , =?UTF-8?B?Q2zDqW1lbnQgTMOpZ2Vy?= , Greentime Hu , Guo Ren , Heiko Stuebner , Masahiro Yamada , Nam Cao , Paul Walmsley , Sami Tolvanen , linux-kernel@vger.kernel.org References: <20231121211958.3158576-1-samuel.holland@sifive.com> <20231123-vision-nuttiness-b75be703f162@wendy> From: Samuel Holland In-Reply-To: <20231123-vision-nuttiness-b75be703f162@wendy> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Sat, 25 Nov 2023 16:06:52 -0800 (PST) Hi Conor, On 2023-11-23 8:06 AM, Conor Dooley wrote: > On Tue, Nov 21, 2023 at 01:19:29PM -0800, Samuel Holland wrote: >> This fixes two bugs in SCS initialization for secondary CPUs. First, >> the SCS was not initialized at all in the spinwait boot path. Second, >> the code for the SBI HSM path attempted to initialize the SCS before >> enabling the MMU. However, that involves dereferencing the thread >> pointer, which requires the MMU to be enabled. >> >> Fix both issues by setting up the SCS in the common secondary entry >> path, after enabling the MMU. > > I'm curious, mostly because I do not know much about the implemtnation > of the shadow call stack, but does it actually work correctly when the > kernel is built without mmu support? I imagine it would work. The SCS implementation is purely software; it stores the return address in a stack at `gp` instead of with the rest of local variables at `sp`. The problem here is that we are passing a pointer between CPUs with different views of the virtual address space (i.e. the boot CPU sees the kernel at 0xffffffff80000000 while the CPU being brought up sees it at its physical address), and then dereferencing it. If there is no MMU support, then the virtual address space is identity mapped on all CPUs, and there is no problem. Regards, Samuel >> Fixes: d1584d791a29 ("riscv: Implement Shadow Call Stack") >> Signed-off-by: Samuel Holland >> --- >> >> arch/riscv/kernel/head.S | 2 +- >> 1 file changed, 1 insertion(+), 1 deletion(-) >> >> diff --git a/arch/riscv/kernel/head.S b/arch/riscv/kernel/head.S >> index b77397432403..76ace1e0b46f 100644 >> --- a/arch/riscv/kernel/head.S >> +++ b/arch/riscv/kernel/head.S >> @@ -154,7 +154,6 @@ secondary_start_sbi: >> XIP_FIXUP_OFFSET a3 >> add a3, a3, a1 >> REG_L sp, (a3) >> - scs_load_current >> >> .Lsecondary_start_common: >> >> @@ -165,6 +164,7 @@ secondary_start_sbi: >> call relocate_enable_mmu >> #endif >> call .Lsetup_trap_vector >> + scs_load_current >> tail smp_callin >> #endif /* CONFIG_SMP */ >> >> -- >> 2.42.0 >>