From nobody Sat Nov 23 17:37:53 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=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1731859904; cv=none; d=zohomail.com; s=zohoarc; b=F9qiwgdAXifEzvPFd4sRO3sOPKrJoaxB+ySiIJmHqyzvREq31zE7Fnc67Teb4TDPcimLkOYTsHzWPVOTdeK55vfgbVT3nNfuyYll9ahCh2saULoPaPVol7o/+Na9wUktdqLT3SWS/Bv9JVMIWnQu5L4nsyPZ7+wz/EWMfy/s5wY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1731859904; 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=GikP/iJqDN/uA4pIDU3RA3esUPeMcwTG8y/JwFqz7k4=; b=jc58c1xkGd3l6riKSfGQpYqR5zI+dsksrnbFtPcTyT/xfhbUf5T/hduKBnQc29Evqk7MPfO9x+dFoKWxDZFRwVWfhRHPJX8cf180twLapCsbLmxnP8ThzZlK9a9XRJgFZW08cn1gKRgB1LUQxqYFPPJ2NaSAuXe8XF1bm2Ijeow= 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 1731859904014393.84359142634787; Sun, 17 Nov 2024 08:11:44 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tChrO-0000yI-J7; Sun, 17 Nov 2024 11:10:50 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tChrK-0000xm-G9 for qemu-devel@nongnu.org; Sun, 17 Nov 2024 11:10:47 -0500 Received: from mail-wm1-x333.google.com ([2a00:1450:4864:20::333]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tChrH-0001gP-Mi for qemu-devel@nongnu.org; Sun, 17 Nov 2024 11:10:46 -0500 Received: by mail-wm1-x333.google.com with SMTP id 5b1f17b1804b1-43152b79d25so18468405e9.1 for ; Sun, 17 Nov 2024 08:10:43 -0800 (PST) Received: from localhost.localdomain (adsl-123.37.6.2.tellas.gr. [37.6.2.123]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-432dab721d7sm126147905e9.9.2024.11.17.08.10.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 17 Nov 2024 08:10:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1731859841; x=1732464641; darn=nongnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=GikP/iJqDN/uA4pIDU3RA3esUPeMcwTG8y/JwFqz7k4=; b=ILdUjcESzBVIGpV9RcUSSfuHrArIVYfrbUZ3ZrHDLv/mnFaWb6H8s4+KWdJuSKGW3w XjxY9hsgCoUMMA8aO4zdGwXcggOPatWd7rlc98noZEUUQcgyeN1UV91VdfZZvAPj0wte bAZ+oFb1kgn1d1RGdzSNiywIan5ZemazZp+JYTXWcMULDrhrqwfwqQ47vjZI15eNL6iG Asbwr4EGh9ANjfGlgxyU27mt8Q9X/WjDWS+md3TX02cKkW9+XrxGP3gSFMjGWu5Dtqiu c4AMRtrm2YKJOx47KKXjIdQfH/Yr88BiV1ayFCJjt8i/iHhNoFyOREE/PNYVZuVGO0OD Fp+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731859841; x=1732464641; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=GikP/iJqDN/uA4pIDU3RA3esUPeMcwTG8y/JwFqz7k4=; b=BrDEH6qg4dPcrjo7pvk+gj6kxWgSnXZuZLBNzfdzCgHgLw8xZX3oiM5F79k+4RDVeY bNru1OnQqZCPHecwoTJk71lqCFmTqapD1PSe+CdC0D3Dk0WwdSX6cXeZiWuFWIaNNeF7 DScN5tMTIGrovxuB4L6TFfWr9f4csJq7q4PSDk4UOIQtYe9c0HiipSQUv7stvbPMvphD O7etEEiJWBBPTqi3typxSqC584rCigmDnOMPK2RBR26rdO0VS6GkoGUHV5gz5t/xQwVR vIVYUn1DzKl39BGKxFqq2SSto//REVVrf4CvIOSELO1M/stwldCsEVqTjN/n0ZulF55G b74g== X-Gm-Message-State: AOJu0YwJQ1OJ9t2CRlWIuloSOg7ajXE2kvow9L4ia6+Z4PER+IdEkGNE HduDxUF7jfhZgMTp4rk/7xtJC82jMAmeKKz4EkFCzP7n0pf+Ss1Xw7szrtYlO1w9z0n+/L4+g5f nSgw= X-Google-Smtp-Source: AGHT+IFx8mitUN5+hjy8LV0JyN3UBFS9MIJ93hNT1dJ8Jy/uDEaUfuQTXR9XLYpPJ50FIFzxfXOHOA== X-Received: by 2002:a05:600c:1e89:b0:431:5863:4240 with SMTP id 5b1f17b1804b1-432df78e296mr76171445e9.24.1731859841438; Sun, 17 Nov 2024 08:10:41 -0800 (PST) From: Manos Pitsidianakis To: qemu-devel@nongnu.org Cc: qemu-arm@nongnu.org, qemu-rust@nongnu.org, =?UTF-8?q?Alex=20Benn=C3=A9e?= , Gustavo Romero , Peter Maydell , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Pierrick Bouvier , Richard Henderson Subject: [PATCH v3] rust/pl011: Fix DeviceID reads Date: Sun, 17 Nov 2024 18:10:36 +0200 Message-ID: <20241117161039.3758840-1-manos.pitsidianakis@linaro.org> X-Mailer: git-send-email 2.45.2 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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: pass client-ip=2a00:1450:4864:20::333; envelope-from=manos.pitsidianakis@linaro.org; helo=mail-wm1-x333.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action 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 @linaro.org) X-ZM-MESSAGEID: 1731859905464116600 Content-Type: text/plain; charset="utf-8" DeviceId, which maps the peripheral and PCell registers of a PL011 device, was not treating each register value as a 32 bit value. Change DeviceId enum to return register values via constified getter functions instead of leveraging the std::ops::Index<_> trait. While at it, print errors when guest attempts to write to other RO registers as well. Signed-off-by: Manos Pitsidianakis --- Notes: Changes from v2: =20 - Group invalid write case matches (Paolo) - Reduce register getter line count to aid review (Peter Maydell) =20 v1 -> v2: =20 - Print error when guest attempts to write to RO registers (Peter Maydell) =20 Version 1: <20241116181549.3430225-1-manos.pitsidianakis@linaro.org> Version 2: <20241116221757.3501603-1-manos.pitsidianakis@linaro.org> Interdiff against v2: diff --git a/rust/hw/char/pl011/src/device.rs b/rust/hw/char/pl011/src/de= vice.rs index fc6f3f394d..f1d959ca28 100644 --- a/rust/hw/char/pl011/src/device.rs +++ b/rust/hw/char/pl011/src/device.rs @@ -40,54 +40,50 @@ enum DeviceId { Luminary, } =20 +macro_rules! pcell_reg_getter { + ($($(#[$attrs:meta])* fn $getter_fn:ident -> $value:literal),*$(,)?)= =3D> { + $($(#[$attrs])* const fn $getter_fn(self) -> u64 { $value })* + }; +} + +macro_rules! periph_reg_getter { + ($($(#[$attrs:meta])* fn $getter_fn:ident -> { Arm =3D> $arm:literal= , Luminary =3D> $lum:literal$(,)?}),*$(,)?) =3D> { + $( + $(#[$attrs])* + const fn $getter_fn(self) -> u64 { + (match self { + Self::Arm =3D> $arm, + Self::Luminary =3D> $lum, + }) as u64 + } + )* + }; +} + impl DeviceId { /// Value of `UARTPeriphID0` register, which contains the `PartNumbe= r0` value. const fn uart_periph_id0(self) -> u64 { 0x11 } =20 - /// Value of `UARTPeriphID1` register, which contains the `Designer0= ` and `PartNumber1` values. - const fn uart_periph_id1(self) -> u64 { - (match self { - Self::Arm =3D> 0x10, - Self::Luminary =3D> 0x00, - }) as u64 + periph_reg_getter! { + /// Value of `UARTPeriphID1` register, which contains the `Desig= ner0` and `PartNumber1` values. + fn uart_periph_id1 -> { Arm =3D> 0x10, Luminary =3D> 0x00 }, + /// Value of `UARTPeriphID2` register, which contains the `Revis= ion` and `Designer1` values. + fn uart_periph_id2 -> { Arm =3D> 0x14, Luminary =3D> 0x18 }, + /// Value of `UARTPeriphID3` register, which contains the `Confi= guration` value. + fn uart_periph_id3 -> { Arm =3D> 0x0, Luminary =3D> 0x1 } } =20 - /// Value of `UARTPeriphID2` register, which contains the `Revision`= and `Designer1` values. - const fn uart_periph_id2(self) -> u64 { - (match self { - Self::Arm =3D> 0x14, - Self::Luminary =3D> 0x18, - }) as u64 - } - - /// Value of `UARTPeriphID3` register, which contains the `Configura= tion` value. - const fn uart_periph_id3(self) -> u64 { - (match self { - Self::Arm =3D> 0x0, - Self::Luminary =3D> 0x1, - }) as u64 - } - - /// Value of `UARTPCellID0` register. - const fn uart_pcell_id0(self) -> u64 { - 0x0d - } - - /// Value of `UARTPCellID1` register. - const fn uart_pcell_id1(self) -> u64 { - 0xf0 - } - - /// Value of `UARTPCellID2` register. - const fn uart_pcell_id2(self) -> u64 { - 0x05 - } - - /// Value of `UARTPCellID3` register. - const fn uart_pcell_id3(self) -> u64 { - 0xb1 + pcell_reg_getter! { + /// Value of `UARTPCellID0` register. + fn uart_pcell_id0 -> 0x0d, + /// Value of `UARTPCellID1` register. + fn uart_pcell_id1 -> 0xf0, + /// Value of `UARTPCellID2` register. + fn uart_pcell_id2 -> 0x05, + /// Value of `UARTPCellID3` register. + fn uart_pcell_id3 -> 0xb1, } } =20 @@ -282,7 +278,7 @@ pub fn write(&mut self, offset: hwaddr, value: u64) { } Ok( dev_id @ (PeriphID0 | PeriphID1 | PeriphID2 | PeriphID3 = | PCellID0 | PCellID1 - | PCellID2 | PCellID3), + | PCellID2 | PCellID3 | FR | RIS | MIS), ) =3D> { eprintln!("write bad offset {offset} at RO register {dev= _id:?} value {value}"); } @@ -304,9 +300,6 @@ pub fn write(&mut self, offset: hwaddr, value: u64) { Ok(RSR) =3D> { self.receive_status_error_clear =3D 0.into(); } - Ok(FR) =3D> { - eprintln!("write bad offset {offset} at RO register UART= FR value {value}"); - } Ok(ILPR) =3D> { self.ilpr =3D value; } @@ -355,12 +348,6 @@ pub fn write(&mut self, offset: hwaddr, value: u64) { self.int_enabled =3D value; self.update(); } - Ok(RIS) =3D> { - eprintln!("write bad offset {offset} at RO register UART= RIS value {value}"); - } - Ok(MIS) =3D> { - eprintln!("write bad offset {offset} at RO register UART= MIS value {value}"); - } Ok(ICR) =3D> { self.int_level &=3D !value; self.update(); rust/hw/char/pl011/src/device.rs | 78 ++++++++++++++++++++++++-------- rust/hw/char/pl011/src/lib.rs | 22 ++++++++- 2 files changed, 79 insertions(+), 21 deletions(-) diff --git a/rust/hw/char/pl011/src/device.rs b/rust/hw/char/pl011/src/devi= ce.rs index 2a85960b81..f1d959ca28 100644 --- a/rust/hw/char/pl011/src/device.rs +++ b/rust/hw/char/pl011/src/device.rs @@ -5,7 +5,7 @@ use core::ptr::{addr_of, addr_of_mut, NonNull}; use std::{ ffi::CStr, - os::raw::{c_int, c_uchar, c_uint, c_void}, + os::raw::{c_int, c_uint, c_void}, }; =20 use qemu_api::{ @@ -32,6 +32,7 @@ /// QEMU sourced constant. pub const PL011_FIFO_DEPTH: usize =3D 16_usize; =20 +/// State enum that represents the values of the peripheral and PCell regi= sters of a PL011 device. #[derive(Clone, Copy, Debug)] enum DeviceId { #[allow(dead_code)] @@ -39,20 +40,51 @@ enum DeviceId { Luminary, } =20 -impl std::ops::Index for DeviceId { - type Output =3D c_uchar; +macro_rules! pcell_reg_getter { + ($($(#[$attrs:meta])* fn $getter_fn:ident -> $value:literal),*$(,)?) = =3D> { + $($(#[$attrs])* const fn $getter_fn(self) -> u64 { $value })* + }; +} =20 - fn index(&self, idx: hwaddr) -> &Self::Output { - match self { - Self::Arm =3D> &Self::PL011_ID_ARM[idx as usize], - Self::Luminary =3D> &Self::PL011_ID_LUMINARY[idx as usize], - } - } +macro_rules! periph_reg_getter { + ($($(#[$attrs:meta])* fn $getter_fn:ident -> { Arm =3D> $arm:literal, = Luminary =3D> $lum:literal$(,)?}),*$(,)?) =3D> { + $( + $(#[$attrs])* + const fn $getter_fn(self) -> u64 { + (match self { + Self::Arm =3D> $arm, + Self::Luminary =3D> $lum, + }) as u64 + } + )* + }; } =20 impl DeviceId { - const PL011_ID_ARM: [c_uchar; 8] =3D [0x11, 0x10, 0x14, 0x00, 0x0d, 0x= f0, 0x05, 0xb1]; - const PL011_ID_LUMINARY: [c_uchar; 8] =3D [0x11, 0x00, 0x18, 0x01, 0x0= d, 0xf0, 0x05, 0xb1]; + /// Value of `UARTPeriphID0` register, which contains the `PartNumber0= ` value. + const fn uart_periph_id0(self) -> u64 { + 0x11 + } + + periph_reg_getter! { + /// Value of `UARTPeriphID1` register, which contains the `Designe= r0` and `PartNumber1` values. + fn uart_periph_id1 -> { Arm =3D> 0x10, Luminary =3D> 0x00 }, + /// Value of `UARTPeriphID2` register, which contains the `Revisio= n` and `Designer1` values. + fn uart_periph_id2 -> { Arm =3D> 0x14, Luminary =3D> 0x18 }, + /// Value of `UARTPeriphID3` register, which contains the `Configu= ration` value. + fn uart_periph_id3 -> { Arm =3D> 0x0, Luminary =3D> 0x1 } + } + + pcell_reg_getter! { + /// Value of `UARTPCellID0` register. + fn uart_pcell_id0 -> 0x0d, + /// Value of `UARTPCellID1` register. + fn uart_pcell_id1 -> 0xf0, + /// Value of `UARTPCellID2` register. + fn uart_pcell_id2 -> 0x05, + /// Value of `UARTPCellID3` register. + fn uart_pcell_id3 -> 0xb1, + } } =20 #[repr(C)] @@ -182,9 +214,14 @@ pub fn read(&mut self, offset: hwaddr, _size: c_uint) = -> std::ops::ControlFlow { - u64::from(self.device_id[(offset - 0xfe0) >> 2]) - } + Ok(PeriphID0) =3D> self.device_id.uart_periph_id0(), + Ok(PeriphID1) =3D> self.device_id.uart_periph_id1(), + Ok(PeriphID2) =3D> self.device_id.uart_periph_id2(), + Ok(PeriphID3) =3D> self.device_id.uart_periph_id3(), + Ok(PCellID0) =3D> self.device_id.uart_pcell_id0(), + Ok(PCellID1) =3D> self.device_id.uart_pcell_id1(), + Ok(PCellID2) =3D> self.device_id.uart_pcell_id2(), + Ok(PCellID3) =3D> self.device_id.uart_pcell_id3(), Err(_) =3D> { // qemu_log_mask(LOG_GUEST_ERROR, "pl011_read: Bad offset = 0x%x\n", (int)offset); 0 @@ -236,9 +273,15 @@ pub fn write(&mut self, offset: hwaddr, value: u64) { use RegisterOffset::*; let value: u32 =3D value as u32; match RegisterOffset::try_from(offset) { - Err(_bad_offset) =3D> { + Err(_) =3D> { eprintln!("write bad offset {offset} value {value}"); } + Ok( + dev_id @ (PeriphID0 | PeriphID1 | PeriphID2 | PeriphID3 | = PCellID0 | PCellID1 + | PCellID2 | PCellID3 | FR | RIS | MIS), + ) =3D> { + eprintln!("write bad offset {offset} at RO register {dev_i= d:?} value {value}"); + } Ok(DR) =3D> { // ??? Check if transmitter is enabled. let ch: u8 =3D value as u8; @@ -257,9 +300,6 @@ pub fn write(&mut self, offset: hwaddr, value: u64) { Ok(RSR) =3D> { self.receive_status_error_clear =3D 0.into(); } - Ok(FR) =3D> { - // flag writes are ignored - } Ok(ILPR) =3D> { self.ilpr =3D value; } @@ -308,8 +348,6 @@ pub fn write(&mut self, offset: hwaddr, value: u64) { self.int_enabled =3D value; self.update(); } - Ok(RIS) =3D> {} - Ok(MIS) =3D> {} Ok(ICR) =3D> { self.int_level &=3D !value; self.update(); diff --git a/rust/hw/char/pl011/src/lib.rs b/rust/hw/char/pl011/src/lib.rs index cd0a49acb9..1f305aa13f 100644 --- a/rust/hw/char/pl011/src/lib.rs +++ b/rust/hw/char/pl011/src/lib.rs @@ -111,6 +111,22 @@ pub enum RegisterOffset { /// DMA control Register #[doc(alias =3D "UARTDMACR")] DMACR =3D 0x048, + #[doc(alias =3D "UARTPeriphID0")] + PeriphID0 =3D 0xFE0, + #[doc(alias =3D "UARTPeriphID1")] + PeriphID1 =3D 0xFE4, + #[doc(alias =3D "UARTPeriphID2")] + PeriphID2 =3D 0xFE8, + #[doc(alias =3D "UARTPeriphID3")] + PeriphID3 =3D 0xFEC, + #[doc(alias =3D "UARTPCellID0")] + PCellID0 =3D 0xFF0, + #[doc(alias =3D "UARTPCellID1")] + PCellID1 =3D 0xFF4, + #[doc(alias =3D "UARTPCellID2")] + PCellID2 =3D 0xFF8, + #[doc(alias =3D "UARTPCellID3")] + PCellID3 =3D 0xFFC, ///// Reserved, offsets `0x04C` to `0x07C`. //Reserved =3D 0x04C, } @@ -137,7 +153,11 @@ const fn _assert_exhaustive(val: RegisterOffset) { } } } - case! { DR, RSR, FR, FBRD, ILPR, IBRD, LCR_H, CR, FLS, IMSC, RIS, = MIS, ICR, DMACR } + case! { + DR, RSR, FR, FBRD, ILPR, IBRD, LCR_H, CR, FLS, IMSC, RIS, MIS,= ICR, DMACR, + PeriphID0, PeriphID1, PeriphID2, PeriphID3, + PCellID0, PCellID1, PCellID2, PCellID3, + } } } =20 base-commit: 43f2def68476697deb0d119cbae51b20019c6c86 --=20 =CE=B3=CE=B1=E1=BF=96=CE=B1 =CF=80=CF=85=CF=81=CE=AF =CE=BC=CE=B9=CF=87=CE= =B8=CE=AE=CF=84=CF=89