From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mx0b-002e3701.pphosted.com (mx0b-002e3701.pphosted.com [148.163.143.35]) by mx.groups.io with SMTP id smtpd.web11.13814.1595403868046634193 for ; Wed, 22 Jul 2020 00:44:28 -0700 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: hpe.com, ip: 148.163.143.35, mailfrom: prvs=0472af9021=daniel.schaefer@hpe.com) Received: from pps.filterd (m0134425.ppops.net [127.0.0.1]) by mx0b-002e3701.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 06M7dDbU013962; Wed, 22 Jul 2020 07:44:27 GMT Received: from g4t3427.houston.hpe.com (g4t3427.houston.hpe.com [15.241.140.73]) by mx0b-002e3701.pphosted.com with ESMTP id 32d2g4uv78-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 22 Jul 2020 07:44:26 +0000 Received: from G2W6309.americas.hpqcorp.net (g2w6309.austin.hp.com [16.197.64.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by g4t3427.houston.hpe.com (Postfix) with ESMTPS id 2BB1C7D; Wed, 22 Jul 2020 07:44:26 +0000 (UTC) Received: from G2W6310.americas.hpqcorp.net (2002:10c5:4034::10c5:4034) by G2W6309.americas.hpqcorp.net (2002:10c5:4033::10c5:4033) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Wed, 22 Jul 2020 07:44:25 +0000 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (15.241.52.11) by G2W6310.americas.hpqcorp.net (16.197.64.52) with Microsoft SMTP Server (TLS) id 15.0.1497.2 via Frontend Transport; Wed, 22 Jul 2020 07:44:25 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=d3kthnX53q7uqgCEvxoUlfP26DCMzpSmlP8ymyKC9GUaS4at705zalfQrWXdA5F91Ii9oXl4o2csccgySUKTqpaF4mVfw8omav9V1QP7VI8xrhmj6TiO4zv6g9n26fJqaI+/ZZHM9P9KtydOIdGPzv/ttf4xFUSJ17tSx/MuavmMFq+e7UGANqK/7pox00oRmxUWHKtTTyo+tNLYNxgBOjz61OkwKGh6w6CFrHn41NNqiuf9bDPrc/hwWGx9ymmdNIZevAoxH90xx/0O0ntr16UQC6MaD1bvDg/IShdw65axnZOnXzLdeIH0Af2yyoRq5hVNtG1g3ckuzTxyMCT8Vg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=R+lAznSTo4tMjQ3vswQkvJ6U2c8SCkGBR9s7K3iWOf4=; b=OrkGbi9hfq70EHjTu7lF1Qr6qjfUPxVTGy7EiGLUFaVYvVNg3hHgy2+dj3T3MXlgo6/FSuqTh//IwjMUaHrsuIsltXMUBFKCn0qPPSHXgz3HEB2FhmoDOIBqr3HFJoVH1tlNNP8e0Eh/v5eHpgBjArrzJtHcc4auwKCypXeB7IBcLXmah+dyVKB2hQp1YbAaezQmss0+BQPJEz5GUWKjpBhMcyTtw2ton4iJ2/JJJWzPm4iLrYhrbMDdb2RVb2wkoiUiUUs8iFY4U3PhNzBC5JWPyxq6r9Yj3AatCUCNyp4arJfcyQQBY/s+3jLTNHKthAF61q70cDdmNJ4JU1yLDA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=hpe.com; dmarc=pass action=none header.from=hpe.com; dkim=pass header.d=hpe.com; arc=none Authentication-Results: linaro.org; dkim=none (message not signed) header.d=none;linaro.org; dmarc=none action=none header.from=hpe.com; Received: from DF4PR8401MB0444.NAMPRD84.PROD.OUTLOOK.COM (2a01:111:e400:760d::23) by DF4PR8401MB0570.NAMPRD84.PROD.OUTLOOK.COM (2a01:111:e400:760a::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3195.17; Wed, 22 Jul 2020 07:44:24 +0000 Received: from DF4PR8401MB0444.NAMPRD84.PROD.OUTLOOK.COM ([fe80::4cbe:3dd0:cae4:b093]) by DF4PR8401MB0444.NAMPRD84.PROD.OUTLOOK.COM ([fe80::4cbe:3dd0:cae4:b093%3]) with mapi id 15.20.3195.028; Wed, 22 Jul 2020 07:44:24 +0000 Subject: Re: [PATCH 1/1] BaseLib:Fix RISC-V Supervisor mode (S-Mode) trap handler reentry issue. To: Abner Chang , CC: Michael D Kinney , Liming Gao , Leif Lindholm References: <20200716141040.11806-1-abner.chang@hpe.com> From: "Daniel Schaefer" Message-ID: Date: Wed, 22 Jul 2020 09:44:16 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.9.0 In-Reply-To: <20200716141040.11806-1-abner.chang@hpe.com> X-ClientProxiedBy: AM6P194CA0004.EURP194.PROD.OUTLOOK.COM (2603:10a6:209:90::17) To DF4PR8401MB0444.NAMPRD84.PROD.OUTLOOK.COM (2a01:111:e400:760d::23) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from [IPv6:2001:16b8:571d:f300:c160:5391:13e9:26ab] (2001:16b8:571d:f300:c160:5391:13e9:26ab) by AM6P194CA0004.EURP194.PROD.OUTLOOK.COM (2603:10a6:209:90::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3216.20 via Frontend Transport; Wed, 22 Jul 2020 07:44:22 +0000 X-Originating-IP: [2001:16b8:571d:f300:c160:5391:13e9:26ab] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 2529f93e-6f3f-4cde-40fb-08d82e130db9 X-MS-TrafficTypeDiagnostic: DF4PR8401MB0570: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:5236; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: n7VJUhMjitmaDFEJnXN0Qdme316VLBdSEZqomcwVcW3+gqWGo2VLwRi5naj9NSDG89E05kEwXK4EBxZ5ZoSir8cAoubvYL6SRjx2NcfbFJulysr9sqpp0601Xr+eDPVvDWAJ3iFPJ+7ZIsXg/R9lQIXKLD6sQOOGuMICXoGxoosxX152iyGxgOcGf/fKFf6INyxmqbBVl8/S1XRNP/+hmnXN6sZBqYCIkuWeLTPQMPBU38cPhhc0vQ2NXpspUfAUqfGwAIMSd23x+rp28sF9nBH67BjDICl7a2dmImRCxy1/YQrfsplsY5dHNc4wXo6nipwgy6ZHxQO0ta4zjVXB32Eyt3GJIgRS4LplJafe9jmQDea5PKxWuHBi66STqgh8 X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DF4PR8401MB0444.NAMPRD84.PROD.OUTLOOK.COM;PTR:;CAT:NONE;SFTY:;SFS:(39860400002)(396003)(136003)(346002)(366004)(376002)(66946007)(66476007)(4326008)(44832011)(66556008)(19627235002)(31696002)(478600001)(8676002)(83380400001)(6666004)(36756003)(52116002)(8936002)(2616005)(31686004)(16526019)(186003)(6486002)(5660300002)(53546011)(54906003)(86362001)(316002)(2906002)(43740500002);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData: dqPve3EQTlfbuIvueto6mphjoXpSs0CY5/hltwwY/e6cN1ZXs3gWNeD6+6cPfaeiZ/eR5FWRmUYx0cdO+qmYTHkAsC+2NejnY/+9oGoIevKO6nNaDTkrtZ6Krj/ynI+eUDfcQETskQm1CjzK4fS9wtaBNdCyc+kAqdcIj0OtPYK/aqws7zvPjpEsLp5AYYrez5Ddr2qNSUH3aWCgQiSlErof9Rra3FpVwb0P29Ptvk7XwttZzbOdqP7vrHMVYGIZZrAh8Kbo/FfAuDbkc7y0l8zaTQpwT9L2G5kn/W4qO7bPP8B5cL4ZmP1jGONdkdkK6xyo3fdx5Bu8BkpqcNNku2KhtXinfigDJ5YAihdeU1VFpQm0I2Ma8wGrUA1G7mPS43L1ZIeC/65Em36PQlOqTcx6O1G+uVMHbwaHxFxVhT7tNTc4ELzZrBpzp+xWNwaiEGFoIhiRLyWd7NnuvXBv3rIMpG3tPfg4JOgoz2A8t3oLo28aFdQ5Wdc/acVtq3ryUOx2gFrSWq/lIxTqLP10uSre0UIaTGR02TTOc5vyAkziczan6O51bnU4tY6QMJg3 X-MS-Exchange-CrossTenant-Network-Message-Id: 2529f93e-6f3f-4cde-40fb-08d82e130db9 X-MS-Exchange-CrossTenant-AuthSource: DF4PR8401MB0444.NAMPRD84.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Jul 2020 07:44:24.4715 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 105b2061-b669-4b31-92ac-24d304d195dc X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: b9WyilSidE5+pGJPmJIz6DOs42pmo4tEA/mbXG385FBqHZS3pSKJSDM9qTTbmMSzTejhffzXLCySA72Ai7iYKQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DF4PR8401MB0570 X-OriginatorOrg: hpe.com X-HPE-SCL: -1 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.235,18.0.687 definitions=2020-07-22_03:2020-07-22,2020-07-22 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 bulkscore=0 clxscore=1011 mlxscore=0 phishscore=0 adultscore=0 priorityscore=1501 mlxlogscore=999 impostorscore=0 spamscore=0 lowpriorityscore=0 suspectscore=2 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2006250000 definitions=main-2007220057 Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit On 7/16/20 4:10 PM, Abner Chang wrote: > While RISC-V hart is trapped into S-Mode, the S-Mode interrupt > CSR (SIE) is disabled by RISC-V hart. However the (SIE) is enabled > again by RestoreTPL, this causes the second S-Mode trap is triggered > by the machine mode (M-Mode)timer interrupt redirection. The SRET > instruction clear Supervisor Previous Privilege (SPP) to zero > (User mode) in the second S-Mode interrupt according to the RISC-V > spec. Above brings hart to the user mode (U-Mode) when execute > SRET in the nested S-Mode interrupt handler because SPP is set to > User Mode in the second interrupt. Afterward, system runs in U-Mode > and any accesses to S-Mode CSR causes the invalid instruction exception. > > Signed-off-by: Abner Chang > > Cc: Michael D Kinney > Cc: Liming Gao > Cc: Daniel Schaefer > Cc: Leif Lindholm > Signed-off-by: Abner Chang > --- > .../Library/BaseLib/RiscV64/RiscVInterrupt.S | 45 ++++++++++++++++--- > 1 file changed, 38 insertions(+), 7 deletions(-) > > diff --git a/MdePkg/Library/BaseLib/RiscV64/RiscVInterrupt.S b/MdePkg/Library/BaseLib/RiscV64/RiscVInterrupt.S > index 766fcfb9cb..87b3468fc7 100644 > --- a/MdePkg/Library/BaseLib/RiscV64/RiscVInterrupt.S > +++ b/MdePkg/Library/BaseLib/RiscV64/RiscVInterrupt.S > @@ -12,21 +12,52 @@ ASM_GLOBAL ASM_PFX(RiscVDisableSupervisorModeInterrupts) > ASM_GLOBAL ASM_PFX(RiscVEnableSupervisorModeInterrupt) > ASM_GLOBAL ASM_PFX(RiscVGetSupervisorModeInterrupts) > > -# define MSTATUS_SIE 0x00000002 > -# define CSR_SSTATUS 0x100 > +#define SSTATUS_SIE 0x00000002 > +#define CSR_SSTATUS 0x100 > + #define SSTATUS_SPP_BIT_POSITION 8 Please start this define at the beginning of the line. > > +// > +// This routine disables supervisor mode interrupt > +// > ASM_PFX(RiscVDisableSupervisorModeInterrupts): > - li a1, MSTATUS_SIE > - csrc CSR_SSTATUS, a1 > + add sp, sp, -(__SIZEOF_POINTER__) > + sd a1, (sp) > + li a1, SSTATUS_SIE > + csrc CSR_SSTATUS, a1 > + ld a1, (sp) > + add sp, sp, (__SIZEOF_POINTER__) > ret Good! Let's not lose those register values. > +// > +// This routine enables supervisor mode interrupt > +// > ASM_PFX(RiscVEnableSupervisorModeInterrupt): > - li a1, MSTATUS_SIE > - csrs CSR_SSTATUS, a1 > + add sp, sp, -2*(__SIZEOF_POINTER__) > + sd a0, (0*__SIZEOF_POINTER__)(sp) > + sd a1, (1*__SIZEOF_POINTER__)(sp) > + > + csrr a0, CSR_SSTATUS > + and a0, a0, (1 << SSTATUS_SPP_BIT_POSITION) > + bnez a0, InTrap // We are in supervisor mode (SMode) > + // trap handler. > + // Skip enabling SIE becasue SIE because > + // is set to disabled by RISC-V hart > + // when the trap takes hart to SMode. > + > + li a1, SSTATUS_SIE > + csrs CSR_SSTATUS, a1 > +InTrap: > + ld a0, (0*__SIZEOF_POINTER__)(sp) > + ld a1, (1*__SIZEOF_POINTER__)(sp) > + add sp, sp, 2*(__SIZEOF_POINTER__) > ret > > +// > +// This routine returns supervisor mode interrupt > +// status. > +// > ASM_PFX(RiscVGetSupervisorModeInterrupts): > csrr a0, CSR_SSTATUS > - andi a0, a0, MSTATUS_SIE > + andi a0, a0, SSTATUS_SIE > ret > > Looks good otherwise :) With the the small changes: Reviewed-by: Daniel Schaefer