From nobody Sun Nov 24 14:02:59 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=syntacore.com ARC-Seal: i=1; a=rsa-sha256; t=1722351090; cv=none; d=zohomail.com; s=zohoarc; b=IFLiznh2tgfG8wwgJP8uiMNqCic0mKgG76UQIAaCOxSo8tY7KH+BbqD0KhX9JtPDWXyxwGKBD6Yp8O0WR9GEPjNnNfg9PX9zcszl24LGnZEC5lUDeSCGh0TKvl7Eh3iAg0+bB5KLeAQEgxJOq4xwlLPGQkCFUV+3WQo3uTPQ8uE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1722351090; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=V+l+hMPgpSjeGnJjRXQ9Se+/zBWez49MK5BRCt63NoE=; b=JvJVfuAhr0LVR/N83g+0H0cn8sLLVI/zyUhNHMoz7PaA008nkNebNxg6b5SEbys6OixEwHYeUkn4oBuj/DOR5DLVND7OAiBTlpRzterp5qqnj8CSgpnYcsSwynV7yYb5W/EUtjv3I4rb5N5U1Fu/3oWHdmCop/ZIrDS1geS7yBc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1722351090394727.4459718105218; Tue, 30 Jul 2024 07:51:30 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sYoB7-0007fI-75; Tue, 30 Jul 2024 10:50:18 -0400 Received: from eggs.gnu.org ([209.51.188.92]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sYmG4-0005oc-Ie for qemu-devel@nongnu.org; Tue, 30 Jul 2024 08:47:25 -0400 Received: from [89.207.88.248] (helo=mta-04.yadro.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sYmEv-00089X-6H for qemu-devel@nongnu.org; Tue, 30 Jul 2024 08:47:06 -0400 DKIM-Filter: OpenDKIM Filter v2.11.0 mta-04.yadro.com 7FE2BC0004 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=syntacore.com; s=mta-04; t=1722342981; bh=V+l+hMPgpSjeGnJjRXQ9Se+/zBWez49MK5BRCt63NoE=; h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type:From; b=nh6nwHJgdJp4nDGKt2I2qIzmpw1Tc1nFbwgC4mQzxFU8JP4/o46gBZdmbt26LDBOp cw6S2KvZyqo5NJgMSIY5dpQOuu07Ttpdb59RBVw3i2azs/dPUjOguc7qffeFPcz8pX Lpxkx3yLmheVqH8bxLs2U5n/eQBEhYqeeW/+yaQUuqx3OuTBPcCJVoQtroxtGoNkvn YfT8a6+8sqmA2qlj6ydLn2rUgaOqNLUlGvi2lDoL3+4OLrCznvSgRmrVQSoOhLbNHF 0zBqk4xfpnOK/BWiC+ajXP5v9oPguPZKYAGr0y4mNDtiTmAXDwnOm3YeCQxiemlN+r Ur+qvXSqydtYg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=syntacore.com; s=mta-03; t=1722342981; bh=V+l+hMPgpSjeGnJjRXQ9Se+/zBWez49MK5BRCt63NoE=; h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type:From; b=e71ss90Swa8jC8r2k2hWxQM8duJxzuhS1doTgU7siZG0MUJZocsenz075L0+oub7z MFGuT7iATB71lv1d1+l1B7Yt3ogG7eg2VdfcZCxeo4uWh6SFTaDcOL2RntePyRHKeB W5zh80bJwUmWxjwSROyW2Iife6loj4WzHIOP7sskWfFKzJ706mepGRENxX9j++J8vL MpA8qMKNma7+NpOj2k+Tw67d+tSjQlF3c84gTgZYCQXjyknVoEMUfUH27YibjwLPxm eIyQMPU8PVYYJcvKDTN1Qpx5FtDj8Ip9B84YjkxZelah5I3qEAdC6/rHZR2r8WtvQI 3eGnvRGl9aEUg== From: Nikita Novikov To: CC: , , Nikita Novikov Subject: [PATCH] hw/char/riscv_htif: Fix 64-bit var write order in 32-bit system Date: Tue, 30 Jul 2024 15:35:59 +0300 Message-ID: <20240730123559.35115-1-n.novikov@syntacore.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: T-EXCH-08.corp.yadro.com (172.17.11.58) To T-EXCH-12.corp.yadro.com (172.17.11.143) X-Host-Lookup-Failed: Reverse DNS lookup failed for 89.207.88.248 (deferred) Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: permerror client-ip=89.207.88.248; envelope-from=n.novikov@syntacore.com; helo=mta-04.yadro.com X-Spam_score_int: -8 X-Spam_score: -0.9 X-Spam_bar: / X-Spam_report: (-0.9 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, RDNS_NONE=0.793, T_SPF_HELO_TEMPERROR=0.01, T_SPF_TEMPERROR=0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Tue, 30 Jul 2024 10:50:09 -0400 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: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @syntacore.com) (identity @syntacore.com) X-ZM-MESSAGEID: 1722351090969116600 Content-Type: text/plain; charset="utf-8" When we are trying to write 64-bit value in 32-bit system, the value in HTI= F device divides on 2 separate 32-bit parts. So device expects write to xHOST_OFFSET1 first,= then to xHOST_OFFSET2. But some compilers (ex. CLANG) can change the writing queue (xHOST_OFFSET2 = first, xHOST_OFFSET1 second) because of machine instructions order swapping. That causes wrong behaviour= of HTIF device. This patch is removing dependency of writing order, so the device will work= correctly regardless of compilers. Signed-off-by: Nikita Novikov --- hw/char/riscv_htif.c | 43 +++++++++++++++++++----------------- include/hw/char/riscv_htif.h | 11 +++++++-- 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/hw/char/riscv_htif.c b/hw/char/riscv_htif.c index 9bef60def1..d914f158ea 100644 --- a/hw/char/riscv_htif.c +++ b/hw/char/riscv_htif.c @@ -233,7 +233,8 @@ static void htif_handle_tohost_write(HTIFState *s, uint= 64_t val_written) if (cmd =3D=3D HTIF_CONSOLE_CMD_GETC) { /* this should be a queue, but not yet implemented as such */ s->pending_read =3D val_written; - s->tohost =3D 0; /* clear to indicate we read */ + s->tohost =3D 0; + s->tohost_status =3D 0x0; /* clear to indicate we read */ return; } else if (cmd =3D=3D HTIF_CONSOLE_CMD_PUTC) { uint8_t ch =3D (uint8_t)payload; @@ -258,7 +259,8 @@ static void htif_handle_tohost_write(HTIFState *s, uint= 64_t val_written) * } */ s->fromhost =3D (val_written >> 48 << 48) | (resp << 16 >> 16); - s->tohost =3D 0; /* clear to indicate we read */ + s->tohost =3D 0; + s->tohost_status =3D 0x0; /* clear to indicate we read */ } =20 #define TOHOST_OFFSET1 (s->tohost_offset) @@ -291,32 +293,33 @@ static void htif_mm_write(void *opaque, hwaddr addr, { HTIFState *s =3D opaque; if (addr =3D=3D TOHOST_OFFSET1) { - if (s->tohost =3D=3D 0x0) { - s->allow_tohost =3D 1; - s->tohost =3D value & 0xFFFFFFFF; - } else { - s->allow_tohost =3D 0; - } + s->tohost =3D deposit64(s->tohost, 0, 32, value); + s->tohost_status |=3D 0x1; } else if (addr =3D=3D TOHOST_OFFSET2) { - if (s->allow_tohost) { - s->tohost |=3D value << 32; - htif_handle_tohost_write(s, s->tohost); - } + s->tohost =3D deposit64(s->tohost, 32, 32, value); + s->tohost_status |=3D 0x2; } else if (addr =3D=3D FROMHOST_OFFSET1) { - s->fromhost_inprogress =3D 1; - s->fromhost =3D value & 0xFFFFFFFF; + s->fromhost =3D deposit64(s->fromhost, 0, 32, value); + s->fromhost_status |=3D 0x1; } else if (addr =3D=3D FROMHOST_OFFSET2) { - s->fromhost |=3D value << 32; - s->fromhost_inprogress =3D 0; + s->fromhost =3D deposit64(s->fromhost, 32, 32, value); + s->fromhost_status |=3D 0x2; } else { - qemu_log("Invalid htif write: address %016" PRIx64 "\n", - (uint64_t)addr); + qemu_log("Invalid htif write: address %016" HWADDR_PRIu "\n", addr= ); + } + if (s->tohost_status =3D=3D 0x3) { + htif_handle_tohost_write(s, s->tohost); + s->tohost_status =3D 0x0; } } =20 static const MemoryRegionOps htif_mm_ops =3D { .read =3D htif_mm_read, .write =3D htif_mm_write, + .valid.min_access_size =3D 4, + .valid.max_access_size =3D 8, + .impl.min_access_size =3D 4, + .impl.max_access_size =3D 4 }; =20 HTIFState *htif_mm_init(MemoryRegion *address_space, Chardev *chr, @@ -343,8 +346,8 @@ HTIFState *htif_mm_init(MemoryRegion *address_space, Ch= ardev *chr, s->tohost_offset =3D tohost_offset; s->fromhost_offset =3D fromhost_offset; s->pending_read =3D 0; - s->allow_tohost =3D 0; - s->fromhost_inprogress =3D 0; + s->tohost_status =3D 0; + s->fromhost_status =3D 0; qemu_chr_fe_init(&s->chr, chr, &error_abort); qemu_chr_fe_set_handlers(&s->chr, htif_can_recv, htif_recv, htif_event, htif_be_change, s, NULL, true); diff --git a/include/hw/char/riscv_htif.h b/include/hw/char/riscv_htif.h index df493fdf6b..2b6d50dc60 100644 --- a/include/hw/char/riscv_htif.h +++ b/include/hw/char/riscv_htif.h @@ -27,8 +27,15 @@ #define TYPE_HTIF_UART "riscv.htif.uart" =20 typedef struct HTIFState { - int allow_tohost; - int fromhost_inprogress; + /* + * Vars below can be in 4 states: + * 0x0 - nothing written + * 0x1 - xHOST_OFFEST1 written + * 0x2 - xHOST_OFFSET2 written + * 0x3 - Both written + */ + int tohost_status; + int fromhost_status; =20 uint64_t tohost; uint64_t fromhost; --=20 2.34.1