From patchwork Fri Feb 3 14:28:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 650050 Delivered-To: patch@linaro.org Received: by 2002:a17:522:d8c:b0:4be:c3dc:14d8 with SMTP id d12csp850551pva; Fri, 3 Feb 2023 06:32:33 -0800 (PST) X-Google-Smtp-Source: AK7set+MLdEQBMcUGKrHLcPn9WOjc9nZvackZVYJbwY9nX0yQWteTTRYZVCgLADfP266So2UryDk X-Received: by 2002:ac8:5704:0:b0:3a9:86dd:3c60 with SMTP id 4-20020ac85704000000b003a986dd3c60mr18843226qtw.47.1675434753829; Fri, 03 Feb 2023 06:32:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675434753; cv=none; d=google.com; s=arc-20160816; b=0qn385UeCvwA5LcAebejMukCcvUdVeIopO/K2kBHGNN5AlKieAKvJ/qCmgviR12XiQ KIWWvwKw7Wldg79/rYxM9GspsxP6Eva1anQnQlNPDqOQMtprqfGkxeug83zesZ+NcSue ZIXW97fUy7X4D8Vcyx+pLbgcdlC4dHUuoIfAm0TQC9abog7bJ7COpXbgTv497+ci+AaT +1bWD3UOdAh/qLDotaTOUG4tDPzGbhE7kircynKbM3ZWOKjnmVxRE1M3t/YAiYI782T2 OSOmwaJGzBF7o37KhVJj6LdTn9GA75QMIDXbcaIdSXoR7WZhWkOxMICpM+45U2lZjkjf 1EDw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=yxaajrT8hw/p9R5XVhSvXLClGevo4qVUStBee60tFvg=; b=RNrBYgBKR9HzguxpeAYaNn++OYxO+XQQFkRRWxyIOQ5FT33dmmEzEkh7KpLviNLofF xn3gZH3LmoETnQziAsKctsJU06UUmf2hNFpRGBfZVDr+1KAHPBCVHAaPcJVfoV7Hat6S Lce03F368kugrd1bAQMU5w/xHt+fn/y0Tqv2ONogRGKeF+vfXB/HeJaa7VPp0dRte2HX spHHaQMiOHceHT2YWhiaOoiYJ5ai2z5/3Xk+SHCyaALvXC3W4iwLvYtkpUdIg2DAfd3c ZU7RUYWdDCJVFqhcQI5y+WbjajNwMCffCzSJ2laX/VV6AFkB5dD+A24UlKBRZv2T2XYO 5E7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=J8I6OgG4; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id k13-20020ac85fcd000000b003b6472c386asi1931535qta.39.2023.02.03.06.32.33 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 03 Feb 2023 06:32:33 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=J8I6OgG4; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pNx4V-0006g1-DO; Fri, 03 Feb 2023 09:29:47 -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 1pNx4L-0006dI-1x for qemu-devel@nongnu.org; Fri, 03 Feb 2023 09:29:37 -0500 Received: from mail-wm1-x330.google.com ([2a00:1450:4864:20::330]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pNx4I-00054B-0a for qemu-devel@nongnu.org; Fri, 03 Feb 2023 09:29:35 -0500 Received: by mail-wm1-x330.google.com with SMTP id d4-20020a05600c3ac400b003db1de2aef0so4002293wms.2 for ; Fri, 03 Feb 2023 06:29:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=yxaajrT8hw/p9R5XVhSvXLClGevo4qVUStBee60tFvg=; b=J8I6OgG4yK64MYUVV3p0iaHxs/3GpP91jZfikVKcQBZn3XTtvbyiGpGPIF/GuHqozn bxzoz00vjXlc2g/yTmEcfwnCt3Wk1wu8qYevABdr+o3ryKyoU/7P/rqO0Krgc4081hdK L5/noIUY97aaduO/u39v0BQjpG0KbSqE5dGdUPIgR3eOQocTz7j/GFYV6GLmc5BtKeZw Gu6RhOND6QJeC703/+MXq9nGFi7TP9Lz8YYtMHlm5+jPTwnHSaMl/so0HtQ1MrByBMrE 1q6xKalw5yb9bC7OUhJZIqN/es/qm9t0HUtDdUX6FZ3ttMHwN2INT7ZOte9O9NnPNsJp YErQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=yxaajrT8hw/p9R5XVhSvXLClGevo4qVUStBee60tFvg=; b=Mj7PEMPzgh8VcFgJzNv9LFGFfCQ1/5F3VmwwDnjTeF7pX5lcAuK37hJN4ssxLq8ove Bsn0pzqgzn2d2/8h+BsAD5oEnu5D9ebLcf/0zGMgJQSkpLfRnh8Hf0hnM4nt177bD49d TCKRHPJFPGskE8J0QJrlxqhLLNfNHVEwbdQEgP7WybtcoQT5jbvLPqwdbZ9Et04kNy5p jD/+RfvAwXr+5Sj0OeerMMjYvrEoTwpRhmW3kwmqen+fWiVPv6yRGfPoFbw/yu6UDmSX kQl/qZk69DRlDp+Hkefdu47FXIRhYEr2gc/KWS8hCHiojKam79RmDZb34J8tyFJimOnV nnmw== X-Gm-Message-State: AO0yUKWa8Ln12ZgMxzJjwdbU8Qm6JhjPVaVMO4ZugmPiRzJ3K2mvT8qD Ya+zXmEarWynwUqcS+QOpnXUj+qhjbrk8/3U X-Received: by 2002:a7b:c85a:0:b0:3d2:392e:905f with SMTP id c26-20020a7bc85a000000b003d2392e905fmr9589877wml.24.1675434572659; Fri, 03 Feb 2023 06:29:32 -0800 (PST) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by smtp.gmail.com with ESMTPSA id j14-20020a05600c130e00b003df241f52e8sm2578492wmf.42.2023.02.03.06.29.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Feb 2023 06:29:32 -0800 (PST) From: Peter Maydell To: qemu-devel@nongnu.org Subject: [PULL 04/33] hw/char/pl011: add post_load hook for backwards-compatibility Date: Fri, 3 Feb 2023 14:28:58 +0000 Message-Id: <20230203142927.834793-5-peter.maydell@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230203142927.834793-1-peter.maydell@linaro.org> References: <20230203142927.834793-1-peter.maydell@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::330; envelope-from=peter.maydell@linaro.org; helo=mail-wm1-x330.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+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org From: Evgeny Iakovlev Previous change slightly modified the way we handle data writes when FIFO is disabled. Previously we kept incrementing read_pos and were storing data at that position, although we only have a single-register-deep FIFO now. Then we changed it to always store data at pos 0. If guest disables FIFO and the proceeds to read data, it will work out fine, because we still read from current read_pos before setting it to 0. However, to make code less fragile, introduce a post_load hook for PL011State and move fixup read FIFO state when FIFO is disabled. Since we are introducing a post_load hook, also do some sanity checking on untrusted incoming input state. Signed-off-by: Evgeny Iakovlev Message-id: 20230123162304.26254-3-eiakovlev@linux.microsoft.com Signed-off-by: Peter Maydell --- hw/char/pl011.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/hw/char/pl011.c b/hw/char/pl011.c index 3fa3b75d042..05e8bdc050e 100644 --- a/hw/char/pl011.c +++ b/hw/char/pl011.c @@ -352,10 +352,35 @@ static const VMStateDescription vmstate_pl011_clock = { } }; +static int pl011_post_load(void *opaque, int version_id) +{ + PL011State* s = opaque; + + /* Sanity-check input state */ + if (s->read_pos >= ARRAY_SIZE(s->read_fifo) || + s->read_count > ARRAY_SIZE(s->read_fifo)) { + return -1; + } + + if (!pl011_is_fifo_enabled(s) && s->read_count > 0 && s->read_pos > 0) { + /* + * Older versions of PL011 didn't ensure that the single + * character in the FIFO in FIFO-disabled mode is in + * element 0 of the array; convert to follow the current + * code's assumptions. + */ + s->read_fifo[0] = s->read_fifo[s->read_pos]; + s->read_pos = 0; + } + + return 0; +} + static const VMStateDescription vmstate_pl011 = { .name = "pl011", .version_id = 2, .minimum_version_id = 2, + .post_load = pl011_post_load, .fields = (VMStateField[]) { VMSTATE_UINT32(readbuff, PL011State), VMSTATE_UINT32(flags, PL011State),