Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1091103pxb; Thu, 4 Mar 2021 03:04:35 -0800 (PST) X-Google-Smtp-Source: ABdhPJy3P83kB9KcZU6Iar4OIpcS8dMJ1taCoAEOmmkTjP3ZsfjtgDr/DYlP0A9mwT7kC6Nkl9pq X-Received: by 2002:a17:907:a04f:: with SMTP id gz15mr3506364ejc.293.1614855875376; Thu, 04 Mar 2021 03:04:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614855875; cv=none; d=google.com; s=arc-20160816; b=YpqxP+VVjj/vY6PyeIKad/RPKo4SHtCGf6zeBcVNX0QKEVr8anQSyTQioc7D1NgiSE Da5qjagqTDgSpzNe1U5CVRGlS/JBBRwqOiAe8xhpmN5vCSVtfwpw/xI5bKMqBkxBW1hp y9qKlWCUxhSQoPWLaA/ij6uG82bOvYUYr7JfXr93MHsYWop8ga35rjpiEsd3kJhNkPhU r8CIBaJDcBz7zxEh6+fJfZusBBPtGYVK2/64TOSfa3MB2oyZze2MicU6NUtCUZYlzury /sQNl49SYNWslriu/fLREbtnZotK+2nDISLPnH+43c2VvCk1OhStRTkN8JzmobInUEkU GtUg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=32RQFgOqfL4RBrlTxaySuNAgFUJN7NaYjCsxx69Arrw=; b=wJRe6RrUkgZoXN0nEs/oN0OeYY9YBtiUQu8fEYomJj8bWNzq7y3UJDQxxwsx+WG9HV Y3Q6qY3JL7pzqB4fXc7eD0F0++AYE1yVqJIxvMwyMJYnMeTY4u54bKX3MsJlkir3kF5U 5TUfeirmusvjsgclWbznpKAGgGkYZuhLTy6xWhY+q4U7e1Yq/f0H5cNwaluLeGgCF6rV 11fqMMw92V5VZby/9U9CFEoa/z5DTLB4b3L+YqxZtDqaJVmeVjy0xWqd0WD+G4928rA9 m/4Y3w94Rdn6B1NTOOroWrrFFZ/TwAcCJtcIRpn1NG1PX5wYJYBJ2iXFGRIXaL4XP3M+ RfWQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id rh9si10629189ejb.85.2021.03.04.03.04.07; Thu, 04 Mar 2021 03:04:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1575818AbhCCRgD (ORCPT + 99 others); Wed, 3 Mar 2021 12:36:03 -0500 Received: from foss.arm.com ([217.140.110.172]:48782 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1379183AbhCCOUB (ORCPT ); Wed, 3 Mar 2021 09:20:01 -0500 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 99E90D6E; Wed, 3 Mar 2021 06:19:15 -0800 (PST) Received: from [10.57.12.223] (unknown [10.57.12.223]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 4FD333F766; Wed, 3 Mar 2021 06:19:13 -0800 (PST) Subject: Re: [PATCH 1/8] ARM: ARMv7-M: Fix register restore corrupt after svc call To: dillon min Cc: Rob Herring , Maxime Coquelin , Alexandre Torgue , "open list:OPEN FIRMWARE AND FLATTENED DEVICE TREE BINDINGS" , linux-stm32@st-md-mailman.stormreply.com, Linux ARM , Linux Kernel Mailing List , linux@armlinux.org.uk, afzal.mohd.ma@gmail.com References: <1614758717-18223-1-git-send-email-dillon.minfei@gmail.com> <1614758717-18223-2-git-send-email-dillon.minfei@gmail.com> <5284d390-c03a-4035-df5a-10d6cd60e47b@arm.com> From: Vladimir Murzin Message-ID: <5efe3d44-8045-e376-003e-3ccbff54fb23@arm.com> Date: Wed, 3 Mar 2021 14:19:19 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 3/3/21 1:35 PM, dillon min wrote: > Hi Vladimir, > > Thanks for the review. > > On Wed, Mar 3, 2021 at 5:52 PM Vladimir Murzin wrote: >> >> On 3/3/21 8:05 AM, dillon.minfei@gmail.com wrote: >>> From: dillon min >>> >>> For some case, kernel not boot by u-boot(single thread), >>> but by rtos , as most rtos use pendsv to do context switch. >> >> >> Hmm, does it mean that it starts kernel from process context? > Yes, kernel might be started from process context, since u-boot not > switch context, so kernel always startup under msp. >> >> I'd assume that it is not only kernel who expects MSP. So, what >> if RTOS you mentioned want to boot other RTOS (even itself)? What >> if you have no access to the source code for those RTOS(es) to >> patch MSP/PSP switch? > > My case is a little complicated. > stm32h7 only have 128Kbytes internal flash, can't store u-boot.bin (>200K), > so, set a bootloader (rt-thread rtos) to internal flash, load > linux/u-boot from serial port via ymodem > store to qspi flash(8Mbytes), then jump to u-boot. > > qspi flash layout: > 0 - 512K: u-boot > 512K- 8M : kernel(xip) > > load process : rt-thread -> u-boot -> linux > > before add psp/msp check after svc call, register restore corrupt. > add a printhex8 around svc call, found the sp stack is 0x24040000c0ffcff8 > it should be 0xc0ffcdf8c0ffcff8. 0x24040000 is the sp stack address > assigned by u-boot > i've no idea how it's become to u-boot's sp. > > I have the rtos code, and will try to fix it on the rtos side. That would be great! > > Can you give more explanation about why linux relies on MSP ? thanks MSP is what set from boot, thus it is natural assumption that boot code would preserve that illusion. I'd guess that kernel is in line in such assumption across different (RT)OS capable to run on M-class cores (please, note that some variants might not have two stack pointers) Cheers Vladimir > >> >> I'd very much prefer to keep stack switching logic outside kernel, >> say, in some shim which RTOS/bootloader can maintain. >> >> Cheers >> Vladimir >> >>> >>> So, we need add an lr check after svc call, to find out should >>> use psp or msp. else register restore after svc call might be >>> corrupted. >>> >>> Fixes: b70cd406d7fe ("ARM: 8671/1: V7M: Preserve registers across switch from Thread to Handler mode") >>> Signed-off-by: dillon min >>> --- >>> arch/arm/mm/proc-v7m.S | 5 ++++- >>> 1 file changed, 4 insertions(+), 1 deletion(-) >>> >>> diff --git a/arch/arm/mm/proc-v7m.S b/arch/arm/mm/proc-v7m.S >>> index 84459c1d31b8..c93d2757312d 100644 >>> --- a/arch/arm/mm/proc-v7m.S >>> +++ b/arch/arm/mm/proc-v7m.S >>> @@ -137,7 +137,10 @@ __v7m_setup_cont: >>> 1: cpsid i >>> /* Calculate exc_ret */ >>> orr r10, lr, #EXC_RET_THREADMODE_PROCESSSTACK >>> - ldmia sp, {r0-r3, r12} >>> + tst lr, #EXC_RET_STACK_MASK >>> + mrsne r4, psp >>> + moveq r4, sp >>> + ldmia r4!, {r0-r3, r12} >>> str r5, [r12, #11 * 4] @ restore the original SVC vector entry >>> mov lr, r6 @ restore LR >>> >>> >> >