From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from EUR02-VI1-obe.outbound.protection.outlook.com (mail-vi1eur02olkn2046.outbound.protection.outlook.com [40.92.48.46]) by sourceware.org (Postfix) with ESMTPS id C75693846410 for ; Thu, 25 Apr 2024 13:13:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C75693846410 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=hotmail.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=hotmail.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C75693846410 Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=40.92.48.46 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1714050806; cv=pass; b=qT7jBoZS0L8dsjQldk8UsAfI6twiqjXDLYa+1j8V27kMDqjcv0vsDKtkllpkJ3kyijgb71vIJIocL7U9+h8YGEVx9lwPbSFiVmcpATg4betgG/G05a9/rYMQSos5nLHhVBgepB6YnkqyjWPaYjgPafvVt1TzL9V4/WB6BBNSnrk= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1714050806; c=relaxed/simple; bh=xlA799hDZSlZVVrQ/q1Ju86D/DfVQD6j33aih2dP+KM=; h=DKIM-Signature:Message-ID:Date:Subject:From:To:MIME-Version; b=tVwb4bAxO6IflA8bmaesV4XQZ1YNG+gi7qhmw9CLOLtvy0bvLM+At5AlwMyXH4JIceji1yWpvozaFBrJutS+UOoRszrKs3vN3Ai/LjKgieUN5YDR4e/byip26cwmY0MNFRwHpweP5ij+yYTlqe13jqHmLwW0SwoVE++ZHG3xKJs= ARC-Authentication-Results: i=2; server2.sourceware.org ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lQE0kCC23SUMVjX0XXDMSIz4oAqWAUCIdiluqmfJULoABV+ANbLzhUfd/V4GEb3QexfNLW1+WEEfqBx6KKA7fMl6ZZalXAeUt31yElftuGLO+XfNq+AHpSsb4vtvo3IQbPCoWZu5M7mXInilTVo/tlTfuYmc6ejBbLWIQSDbjcxN5OODpxth1OjgAzQofy9UHq7YSSXcKMX9OJptylIjAZwn98j9kryxVqhy43Ds33Y14+KJCoYJyGk/+idEDVhruLSC33TMkGKWgfYIqTyjoz51Mm4hiduFiIMriA1kyyQDYtLqhZ4kDYpZVCdot1e7CohGHE97Zq19ICkW2lwOGw== 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-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=usdincDEK+J3x6ZA2rBtaLRaTtOQUFy8KnnF0yOu5HE=; b=ewSUI1qnPENUVsO6g4KQC2qMZPvm43BFSdQ8AwD8kpvB9cK2NsKi0UrVo0ykb3fc+H0ZWlEpPSVermY9A5Fj0XMa1h6vD6svSwVO7uyxbsHE/PE2+1Epl/PkhkTLNGPwJHdXIbWWz3nNcPm4I70o2nUtatZWQy515lstjk3jgpdvpo+9U+3wTLlT6hQistOZWnryG+9bnU4MUGHI9JqQlui9Z0jcumQtUecYL54DTrC4TujlWb8uRPnhIRCAMxKKeWikJ65g6Nt4plTWmE8Ok/Ko1y9TmxorUNVv+T6hMdEQijq5j3Nzkw6t8C2eyd2U7HT8gWz07v1SYkG6WcKCLA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=HOTMAIL.DE; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=usdincDEK+J3x6ZA2rBtaLRaTtOQUFy8KnnF0yOu5HE=; b=asg0aIdOWmWX2hS/cEWAURpGjfcInOadkT8OdbWWkP6//YC/mzdHYAeH0ekmhtbwnTUJdUpJ1nTdWbaBPhswxIMNz/sBRvVR0QZmtwNF7YfByN+IRWdHKzc4Et0nxewPlXQvoSvgNEgtZGl20foh4kDVTZf0HLpYnMUPDbPMt6QtpzYklpY4pak3/x5JKVKtd1Q07UjY+gUxcBs5atLt7c81NuZsdRCO2WxyDuPrl9khu/jOv2g7xvhIx0t4zz3DD3C9UTPCamGZ0waw68FGD4ghKkAbi4fn4foxInq8Fj9W4PVczGNyBwCR/nBdz1pdIhrp/sdsSKKij/2UPJ5z1A== Received: from AS8P193MB1285.EURP193.PROD.OUTLOOK.COM (2603:10a6:20b:333::21) by AM0P193MB0658.EURP193.PROD.OUTLOOK.COM (2603:10a6:20b:165::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7472.44; Thu, 25 Apr 2024 13:13:18 +0000 Received: from AS8P193MB1285.EURP193.PROD.OUTLOOK.COM ([fe80::e65d:5dd2:5662:c61f]) by AS8P193MB1285.EURP193.PROD.OUTLOOK.COM ([fe80::e65d:5dd2:5662:c61f%4]) with mapi id 15.20.7472.045; Thu, 25 Apr 2024 13:13:18 +0000 Message-ID: Date: Thu, 25 Apr 2024 15:15:16 +0200 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2] sim: riscv: Fix some issues with class-a instructions From: Bernd Edlinger To: "gdb-patches@sourceware.org" , Andrew Burgess References: Content-Language: en-US In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-TMN: [pT1E1vQBv6a0elpI7BZpaNHlP94aJK+6+a6uruncIWX171NE4XlRK8EK3JarsxS0] X-ClientProxiedBy: FR0P281CA0100.DEUP281.PROD.OUTLOOK.COM (2603:10a6:d10:a9::6) To AS8P193MB1285.EURP193.PROD.OUTLOOK.COM (2603:10a6:20b:333::21) X-Microsoft-Original-Message-ID: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AS8P193MB1285:EE_|AM0P193MB0658:EE_ X-MS-Office365-Filtering-Correlation-Id: 3105f8bf-8a9f-4d7c-2f23-08dc6529793c X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: aFFtLl8pS1sscgFu0P62duhJi9avgfD/AzP0h4tiTLHMWJKz/o7HUJzdMWhqZ59rEOUT2bOjI6gmXNDbfFindcvQB89o+Fhop3wrPE2A0oIWEv+72pdKfij3QtocbPb413G7FZP+TnPCbH4S6OZqMW5wlry3npVAryeJM878Uo1m3ZWRO+AsxliczQix+tv8EhR8zlYh2TBH5bxQc7Kp2OWhTNWlf5eZxVleLqb7DsesnFdyH74k4Juzdgz/88WBkdvZCBnoexcz/Cs8J6Ax5DSso6aEwaOkXg9BPV4fDn/a6Om7HceBOQPwwbe5pOaBDVTcPiKGi5kkor1/Mu8vR6b2TfaBuyuzCV+0LwXQOAeZzwSj/q2JDtVQMde+7NvNk9uTJME37vmurbhBpDkCvTlB65G5H9qyBkSK33nc4Pn0Y9mDnzxozqvLc5O8ZO7Fu9wEZsyHonkImrIqdwrBpgrui5QgPyOzC3KmP/3a4qQxSbuAGzQwsiQdr2uH+Yw5b18aILOuS16Q1nEat0xNVl/4m/2jDEYL8eUIKQv8t1d8+hCXPVdYSoQG+dXhy9+T X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?Vko3aFZXVWs3cCt5bWNWSlJWb21HYjRBNTZaT3JPSFM0UUJFU2JGeWZpcFpY?= =?utf-8?B?ei94L3hrU0J0MDkvcWFjQS85S0piWWlTcWNhVytJVUV3ZkhMY3Mzbks4Vjl5?= =?utf-8?B?WnduZDJNWGx5WGs3cll4ODA2SjJhS3N0SmIxYXVGR0JRSDBKNjVYWGJrNjdK?= =?utf-8?B?ZzZYd1ZmU3Z3WDFGQ3ZJR0Y4VXNJU05pdXh6RlBZNFRqcGxNNDNYZ3AvdktJ?= =?utf-8?B?OWpva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oOVA1?= =?utf-8?B?S1kzY0dFeHBMT0xjQnlYS3lYZU82M0JHQmFWSzRsT1dTc1RLTTJoY2dwTVFx?= =?utf-8?B?eFJYWXUwVm9sSzZFbEUySG5abzM0RGU2YU5pUkFlcE1MYUEwaHBPYVRRL1lH?= =?utf-8?B?cXpkWjJLZlJvakVlOEdlNEdxaENYRTVnbW55Rnl0Z0VFYTM3TVl6ZVMyUGhE?= =?utf-8?B?ckVPTU53WlR1Myt3dTlJUzI2SFJZQS9MYTA4MXhzc2xySjdOOHJ2TXNlSjUx?= =?utf-8?B?SGFFOGw3dlVVVUNabnhhSHd4MmxWczdyZjNQNWh5RFRYQTY1cVMzaHJoeXJ2?= =?utf-8?B?aDJTUlZZU0NvbjN1WVNIVXlRbGxBU2lBUVJheXdhZHpmOGNFVElWU2VWa2RJ?= =?utf-8?B?MFQxaEVwZjhHcmtmaGZzSjFDRmZLSWtlRkp2cU41c1dYQk5SYjdUcHdYSDIr?= =?utf-8?B?TWtDZU96S1c5WGtMZlVNYU5xc0pkK3hPZ0VTMGtjeEh1cnh1aEJzZlVoWEhm?= =?utf-8?B?S2RxOWMraDFvaHh2d1pyUDJSRDZDeUVVeFcrZkxCSzhUdGhZUzAvaVJUUnlZ?= =?utf-8?B?aTB1NjliY0dwcjFiTDJuMzdZR1FoZ3JxRUNaR3FjeHh2empzanBobXEySHI3?= =?utf-8?Q?xohGSDX5qRoAcqymO36VA0p8JYrvyCWLuD6/xX3?= X-OriginatorOrg: sct-15-20-4755-11-msonline-outlook-80ceb.templateTenant X-MS-Exchange-CrossTenant-Network-Message-Id: 3105f8bf-8a9f-4d7c-2f23-08dc6529793c X-MS-Exchange-CrossTenant-AuthSource: AS8P193MB1285.EURP193.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Apr 2024 13:13:18.5465 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM0P193MB0658 X-Spam-Status: No, score=-13.2 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 4/23/24 16:30, Bernd Edlinger wrote: > This fixes some issues with atomic instruction handling. First the > instructions may have AQ and/or RL bits set, but the emulator has no > such concept, so we have to ignore those. > > According to the spec the memory must be naturally aligned, otherwise > an exception shall be thrown, so do the sim_core read/write aligned. > In the case of riscv64 target, there were the LR_D and SC_D > 64bit load and store instructions missing, so add those. > > Also the AMOMIN/AMOMAX[U]_W instructions were not correct for riscv64 > because the upper half word of the input registers were not ignored > as they should, so use explicit type-casts to uint32_t and int32_t > for those. > > And finally make the class-a instruction set only executable if a > riscv cpu model with A extension is selected. > --- > sim/riscv/sim-main.c | 65 +++++++++++++++++++++++++++++++++++--------- > 1 file changed, 52 insertions(+), 13 deletions(-) > > v2: use sim_core_read/write_unaligned, but override current_alignment, > to raise an exception when the address is not aligned. > > diff --git a/sim/riscv/sim-main.c b/sim/riscv/sim-main.c > index e4b15b533ba..0c05b79dea4 100644 > --- a/sim/riscv/sim-main.c > +++ b/sim/riscv/sim-main.c > @@ -841,6 +841,9 @@ execute_m (SIM_CPU *cpu, unsigned_word iw, const struct riscv_opcode *op) > static sim_cia > execute_a (SIM_CPU *cpu, unsigned_word iw, const struct riscv_opcode *op) > { > + unsigned_word mask_aq = OP_MASK_AQ << OP_SH_AQ; > + unsigned_word mask_rl = OP_MASK_RL << OP_SH_RL; > + unsigned_word mask_aqrl = mask_aq | mask_rl; > struct riscv_sim_cpu *riscv_cpu = RISCV_SIM_CPU (cpu); > SIM_DESC sd = CPU_STATE (cpu); > struct riscv_sim_state *state = RISCV_SIM_STATE (sd); > @@ -853,15 +856,24 @@ execute_a (SIM_CPU *cpu, unsigned_word iw, const struct riscv_opcode *op) > struct atomic_mem_reserved_list *amo_prev, *amo_curr; > unsigned_word tmp; > sim_cia pc = riscv_cpu->pc + 4; > + int prev_alignment = current_alignment; > + > + if (current_alignment != FORCED_ALIGNMENT) > + current_alignment = STRICT_ALIGNMENT; > > /* Handle these two load/store operations specifically. */ > - switch (op->match) > + switch (op->match & ~mask_aqrl) > { > + case MATCH_LR_D: > case MATCH_LR_W: > TRACE_INSN (cpu, "%s %s, (%s);", op->name, rd_name, rs1_name); > - store_rd (cpu, rd, > - sim_core_read_unaligned_4 (cpu, riscv_cpu->pc, read_map, > - riscv_cpu->regs[rs1])); > + if (op->xlen_requirement == 64) > + tmp = sim_core_read_unaligned_8 (cpu, riscv_cpu->pc, read_map, > + riscv_cpu->regs[rs1]); > + else > + tmp = EXTEND32 (sim_core_read_unaligned_4 (cpu, riscv_cpu->pc, read_map, > + riscv_cpu->regs[rs1])); > + store_rd (cpu, rd, tmp); > > /* Walk the reservation list to find an existing match. */ > amo_curr = state->amo_reserved_list; > @@ -878,6 +890,7 @@ execute_a (SIM_CPU *cpu, unsigned_word iw, const struct riscv_opcode *op) > amo_curr->next = state->amo_reserved_list; > state->amo_reserved_list = amo_curr; > goto done; > + case MATCH_SC_D: > case MATCH_SC_W: > TRACE_INSN (cpu, "%s %s, %s, (%s);", op->name, rd_name, rs2_name, > rs1_name); > @@ -889,9 +902,14 @@ execute_a (SIM_CPU *cpu, unsigned_word iw, const struct riscv_opcode *op) > if (amo_curr->addr == riscv_cpu->regs[rs1]) > { > /* We found a reservation, so operate it. */ > - sim_core_write_unaligned_4 (cpu, riscv_cpu->pc, write_map, > - riscv_cpu->regs[rs1], > - riscv_cpu->regs[rs2]); > + if (op->xlen_requirement == 64) > + sim_core_write_unaligned_8 (cpu, riscv_cpu->pc, write_map, > + riscv_cpu->regs[rs1], > + riscv_cpu->regs[rs2]); > + else > + sim_core_write_unaligned_4 (cpu, riscv_cpu->pc, write_map, > + riscv_cpu->regs[rs1], > + riscv_cpu->regs[rs2]); > store_rd (cpu, rd, 0); > if (amo_curr == state->amo_reserved_list) > state->amo_reserved_list = amo_curr->next; > @@ -920,7 +938,7 @@ execute_a (SIM_CPU *cpu, unsigned_word iw, const struct riscv_opcode *op) > riscv_cpu->regs[rs1])); > store_rd (cpu, rd, tmp); > > - switch (op->match) > + switch (op->match & ~mask_aqrl) > { > case MATCH_AMOADD_D: > case MATCH_AMOADD_W: Oops, here is the ultimate bug... when rd == rs1 or rd == rs2 this does not work right. with this store_rd the rs1 and/or rs2 can be overwrittten. e.g. amoswap.d r0, r0, (r1) does not work correctly because rd is overwritten too early. the old value of r0 should be written to memory at r1 r0 should get the value that was read from memory at r1 but due to this aliasing iossue the wrong value is writtenback. I will have to think of a solution and send a v3 version shortly. Thanks Bernd.