From patchwork Mon Oct 10 08:52:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lev Kujawski X-Patchwork-Id: 13002377 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4E4BBC433F5 for ; Mon, 10 Oct 2022 08:59:06 +0000 (UTC) Received: from localhost ([::1]:49804 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ohocr-000059-2c for qemu-devel@archiver.kernel.org; Mon, 10 Oct 2022 04:59:05 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:49174) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ohoWj-00030a-Jr; Mon, 10 Oct 2022 04:52:51 -0400 Received: from mout-p-103.mailbox.org ([80.241.56.161]:38792) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_CHACHA20_POLY1305:256) (Exim 4.90_1) (envelope-from ) id 1ohoWg-0006C9-3Z; Mon, 10 Oct 2022 04:52:44 -0400 Received: from smtp2.mailbox.org (smtp2.mailbox.org [10.196.197.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-103.mailbox.org (Postfix) with ESMTPS id 4MmCNM0YsSz9sSX; Mon, 10 Oct 2022 10:52:35 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mailbox.org; s=mail20150812; t=1665391955; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=IKgzJ8v5oduP+PmJ6RUy+LlwkuDDbmwLqegxY3iL1DY=; b=TJVGFfetRw0VDADsdZPTL3iTZ8T/h3sRrpNMBEMzlCfnMF6ROlkdLmrFj1zx2KlfszBG9Z XOA08QWwSg50VQ9Z1li8TAFkuC13ims8f+NSB5vpz4pxehFc9DmccbgGfE738KOVERfO9z qBuJIS3ojoRdKUSP3QD3gucL2srcnrg2ta89k9V6qWSKNI1dEMhz55Dan3ONuyOxruwjfA Y0hE/+MWXTzRihrXRpOISLMvEbvxNUAb4+OW9dmlEedpDbaGZD3pcCduGSRwxMysOwaCcn gWfwcgRabFo8GibFbrswKYpIIrJa65l0EhScPipv1TWjxWbYB/9zj73dFIDTQA== From: Lev Kujawski DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mailbox.org; s=mail20150812; t=1665391953; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=IKgzJ8v5oduP+PmJ6RUy+LlwkuDDbmwLqegxY3iL1DY=; b=lEgBd9YyfQ8Mjg2EiSwzMzYdxOUR/BjnVaV8HRUXf4KqsxaKjldg7OXDyNf+subugHFP3C B6WELyitvyZjr0TxojSmfM2zvEzBGH9h5aoeJgDnUuDlTgjG1Mocj8wt/OukP42O9BWze2 ewHY9iKSstIbZRdgXjF/oLksO7DGDbb6Wa6dYScAxAcT9z7vniklPNkHjIw6Xe+oit0vB+ 4RJqI2EJ/V/XUPNvAxBXcAJbHsyNbMdcSUIwV7l9ryEsqqyavp7Ba1p4B86PHdlf3fC5f9 bLhxJAs50aHMr9uVbK1jBKb/SxXTartiXNjwPmkMztRzW99ov//KdMNEMy2wUA== To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, Thomas Huth , John Snow , Laurent Vivier , Paolo Bonzini , Lev Kujawski Subject: [PATCH 1/2] hw/ide/core.c (cmd_read_native_max): Avoid limited device parameters Date: Mon, 10 Oct 2022 08:52:28 +0000 Message-Id: <20221010085229.2431276-1-lkujaw@mailbox.org> MIME-Version: 1.0 X-MBO-RS-ID: 9f8794479e1583480e3 X-MBO-RS-META: yaew78cu7mp33wrs9comtwzb58ons7gc Received-SPF: pass client-ip=80.241.56.161; envelope-from=lkujaw@mailbox.org; helo=mout-p-103.mailbox.org X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 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_LOW=-0.7, 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Always use the native CHS device parameters for the ATA commands READ NATIVE MAX ADDRESS and READ NATIVE MAX ADDRESS EXT, not those limited by the ATA command INITIALIZE_DEVICE_PARAMETERS (introduced in patch 176e4961, hw/ide/core.c: Implement ATA INITIALIZE_DEVICE_PARAMETERS command, 2022-07-07.) As stated by the ATA/ATAPI specification, "[t]he native maximum is the highest address accepted by the device in the factory default condition." Therefore this patch substitutes the native values in drive_heads and drive_sectors before calling ide_set_sector(). One consequence of the prior behavior was that setting zero sectors per track could lead to an FPE within ide_set_sector(). Thanks to Alexander Bulekov for reporting this issue. Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1243 Signed-off-by: Lev Kujawski --- hw/ide/core.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/hw/ide/core.c b/hw/ide/core.c index 39afdc0006..ee836401bc 100644 --- a/hw/ide/core.c +++ b/hw/ide/core.c @@ -1608,11 +1608,24 @@ static bool cmd_read_native_max(IDEState *s, uint8_t cmd) /* Refuse if no sectors are addressable (e.g. medium not inserted) */ if (s->nb_sectors == 0) { ide_abort_command(s); - return true; - } + } else { + /* + * Save the active drive parameters, which may have been + * limited from their native counterparts by, e.g., INITIALIZE + * DEVICE PARAMETERS or SET MAX ADDRESS. + */ + const int aheads = s->heads; + const int asectors = s->sectors; - ide_cmd_lba48_transform(s, lba48); - ide_set_sector(s, s->nb_sectors - 1); + s->heads = s->drive_heads; + s->sectors = s->drive_sectors; + + ide_cmd_lba48_transform(s, lba48); + ide_set_sector(s, s->nb_sectors - 1); + + s->heads = aheads; + s->sectors = asectors; + } return true; } From patchwork Mon Oct 10 08:52:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lev Kujawski X-Patchwork-Id: 13002376 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id AB2D3C433F5 for ; Mon, 10 Oct 2022 08:57:46 +0000 (UTC) Received: from localhost ([::1]:38970 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ohobZ-00071l-AJ for qemu-devel@archiver.kernel.org; Mon, 10 Oct 2022 04:57:45 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:53250) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ohoWl-00030c-N4; Mon, 10 Oct 2022 04:52:51 -0400 Received: from mout-p-201.mailbox.org ([80.241.56.171]:36258) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_CHACHA20_POLY1305:256) (Exim 4.90_1) (envelope-from ) id 1ohoWj-0006DX-Hg; Mon, 10 Oct 2022 04:52:47 -0400 Received: from smtp2.mailbox.org (smtp2.mailbox.org [IPv6:2001:67c:2050:b231:465::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-201.mailbox.org (Postfix) with ESMTPS id 4MmCNQ5fBfz9sQr; Mon, 10 Oct 2022 10:52:38 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mailbox.org; s=mail20150812; t=1665391958; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=TndfG5ZLHuG7kaSjvXs28bs8nV6vF1WJXNxtA+qxG5w=; b=GTa1DoTA8D7Ha9iYr8UKh3zG+ZcrCy7Xim+eRW1Q2uCyhPbrsBQfPK62/1f/d9bRwiTqol 9AHUbnCmk2U4IJ3XXdN7HAgfIacdDTAqqXmnjtLi8EX/bRxHZMX+OuZX1C7t1BglLLiK2h C3ZK+iEbkpvChtj3umJ7lzszUliRFwS4RArJeLYOHF11MZTBZH6XeF6Dz1L9JBAyyurNvv gUNFX0FfX4ULfjElI6uLnZ5URuF1fX4TrnVlg6YZLcIfBzjG/zhoU0UmfcVcchU6zZE7k3 GJP2vFY+HzFT8UYi3M88bcDXxyBZA6C9XXmZqsV+TdwJjJQtSrMN6iIHFFu82g== From: Lev Kujawski DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mailbox.org; s=mail20150812; t=1665391956; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=TndfG5ZLHuG7kaSjvXs28bs8nV6vF1WJXNxtA+qxG5w=; b=vgPB3Qt7/xSb1v227VirmFReJf6zkvDHnjGpFUUtaNpHKKgGnML4IWFaEbm05toUmEbytb pGueee0kz7NXdR85CVmjFZrSn4WZ18R1mosXFrU5D6aWqXR3yYswNE9jYlLShFRdLMAfN/ pVRUMJ2kXrSIx6cIo1Ts/zDUj15zZza1f9XKK1cKllQ4LYI9y2zNTwX1i0ZGiEej+MfWNV id4MzV75srfigxaj9Hz9y9gJbjheNCXijboioyJZJy3u6PmuWsk8IF/YF9aAIgLW1jAsMy XqTeeB+1hFiaS1bvdKaG1Uus3HbhWpBZ3VRXWpZH4QyMgITAShlfAkm3ibhuAQ== To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, Thomas Huth , John Snow , Laurent Vivier , Paolo Bonzini , Lev Kujawski Subject: [PATCH 2/2] tests/qtest/ide-test: Verify READ NATIVE MAX ADDRESS is not limited Date: Mon, 10 Oct 2022 08:52:29 +0000 Message-Id: <20221010085229.2431276-2-lkujaw@mailbox.org> In-Reply-To: <20221010085229.2431276-1-lkujaw@mailbox.org> References: <20221010085229.2431276-1-lkujaw@mailbox.org> MIME-Version: 1.0 X-MBO-RS-ID: bbcbf016e9e1ddb75f5 X-MBO-RS-META: m3zzy1nous44qwebfffz4jxrj1p69wgn X-Rspamd-Queue-Id: 4MmCNQ5fBfz9sQr Received-SPF: pass client-ip=80.241.56.171; envelope-from=lkujaw@mailbox.org; helo=mout-p-201.mailbox.org X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 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_LOW=-0.7, 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Verify that the ATA command READ NATIVE MAX ADDRESS returns the last valid CHS tuple for the native device rather than any limit established by INITIALIZE DEVICE PARAMETERS. Signed-off-by: Lev Kujawski Acked-by: Thomas Huth --- tests/qtest/ide-test.c | 47 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/tests/qtest/ide-test.c b/tests/qtest/ide-test.c index dbe1563b23..c406e6752a 100644 --- a/tests/qtest/ide-test.c +++ b/tests/qtest/ide-test.c @@ -37,7 +37,8 @@ /* TODO actually test the results and get rid of this */ #define qmp_discard_response(q, ...) qobject_unref(qtest_qmp(q, __VA_ARGS__)) -#define TEST_IMAGE_SIZE 64 * 1024 * 1024 +/* Specified by ATA (physical) CHS geometry for ~64 MiB device. */ +#define TEST_IMAGE_SIZE ((130 * 16 * 63) * 512) #define IDE_PCI_DEV 1 #define IDE_PCI_FUNC 1 @@ -91,11 +92,13 @@ enum { enum { CMD_DSM = 0x06, CMD_DIAGNOSE = 0x90, + CMD_INIT_DP = 0x91, /* INITIALIZE DEVICE PARAMETERS */ CMD_READ_DMA = 0xc8, CMD_WRITE_DMA = 0xca, CMD_FLUSH_CACHE = 0xe7, CMD_IDENTIFY = 0xec, CMD_PACKET = 0xa0, + CMD_READ_NATIVE = 0xf8, /* READ NATIVE MAX ADDRESS */ CMDF_ABORT = 0x100, CMDF_NO_BM = 0x200, @@ -562,6 +565,46 @@ static void string_cpu_to_be16(uint16_t *s, size_t bytes) } } +static void test_specify(void) +{ + QTestState *qts; + QPCIDevice *dev; + QPCIBar bmdma_bar, ide_bar; + uint16_t cyls; + uint8_t heads, spt; + + qts = ide_test_start( + "-blockdev driver=file,node-name=hda,filename=%s " + "-device ide-hd,drive=hda,bus=ide.0,unit=0 ", + tmp_path[0]); + + dev = get_pci_device(qts, &bmdma_bar, &ide_bar); + + /* Initialize drive with zero sectors per track and one head. */ + qpci_io_writeb(dev, ide_bar, reg_nsectors, 0); + qpci_io_writeb(dev, ide_bar, reg_device, 0); + qpci_io_writeb(dev, ide_bar, reg_command, CMD_INIT_DP); + + /* READ NATIVE MAX ADDRESS (CHS mode). */ + qpci_io_writeb(dev, ide_bar, reg_device, 0xa0); + qpci_io_writeb(dev, ide_bar, reg_command, CMD_READ_NATIVE); + + heads = qpci_io_readb(dev, ide_bar, reg_device) & 0xf; + ++heads; + g_assert_cmpint(heads, ==, 16); + + cyls = qpci_io_readb(dev, ide_bar, reg_lba_high) << 8; + cyls |= qpci_io_readb(dev, ide_bar, reg_lba_middle); + ++cyls; + g_assert_cmpint(cyls, ==, 130); + + spt = qpci_io_readb(dev, ide_bar, reg_lba_low); + g_assert_cmpint(spt, ==, 63); + + ide_test_quit(qts); + free_pci_device(dev); +} + static void test_identify(void) { QTestState *qts; @@ -1079,6 +1122,8 @@ int main(int argc, char **argv) /* Run the tests */ g_test_init(&argc, &argv, NULL); + qtest_add_func("/ide/read_native", test_specify); + qtest_add_func("/ide/identify", test_identify); qtest_add_func("/ide/diagnostic", test_diagnostic);