From patchwork Thu May 11 12:07:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 13237834 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.xenproject.org (lists.xenproject.org [192.237.175.120]) (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 29EF9C7EE22 for ; Thu, 11 May 2023 12:07:38 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.533330.829901 (Exim 4.92) (envelope-from ) id 1px54o-0001h9-W9; Thu, 11 May 2023 12:07:18 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 533330.829901; Thu, 11 May 2023 12:07:18 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1px54o-0001h2-Sy; Thu, 11 May 2023 12:07:18 +0000 Received: by outflank-mailman (input) for mailman id 533330; Thu, 11 May 2023 12:07:18 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1px54o-0000KI-3M for xen-devel@lists.xenproject.org; Thu, 11 May 2023 12:07:18 +0000 Received: from EUR04-DB3-obe.outbound.protection.outlook.com (mail-db3eur04on0601.outbound.protection.outlook.com [2a01:111:f400:fe0c::601]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 5e8ca344-eff4-11ed-8611-37d641c3527e; Thu, 11 May 2023 14:07:16 +0200 (CEST) Received: from VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) by VI1PR04MB7072.eurprd04.prod.outlook.com (2603:10a6:800:12c::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6363.33; Thu, 11 May 2023 12:07:14 +0000 Received: from VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::8e41:82b6:a27f:2e0c]) by VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::8e41:82b6:a27f:2e0c%4]) with mapi id 15.20.6363.033; Thu, 11 May 2023 12:07:14 +0000 X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 5e8ca344-eff4-11ed-8611-37d641c3527e ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=QLE6lLNvVHuSw/fdKAAzSm3vbMHlxyKjSYP1wn7xnBhFEAgjwWTW63L1poNNwN1UqW4DwHMwPbZhQq3+wz4BalLFpaS73N509zDQpmQr+WG6eeJZutserc20TH0r6XAjrUPqHY//t+DwqMXSS3UWj91LQqQYGLUNuQiGsVFCMoSzO/ywD9zyxg3S692T914FJNgbg8g6WuWhh5cQEunOdW1d/MvOLOIgDQN8QTBMyeJ7m94wHM9D2h4g+g+uqX/buceQMWdR3ohgVyA0GUPYzLGtRQq/G/HfvjcWoOuzByC77uYg+tsZOzfYkSEcYDD+fzJAMhEh3JuHqcu9m9bZHQ== 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=m8K8rfTh8nTzrnw7JpPtZZMmTWFZSgvhcqiMn1zjH5Y=; b=ZssgIDn4uYLKy92R3VrnrvWrVB4GnVJYeKzmqHKi7FO8BBW9bcq3wB7A0EXpzd2o77eN4y6PFhm2RdzAVWOIPpz6tI9Umlwf9musXSnswLoZncxgrZvfPS/akNjmunZ0vAdbLc6oL4oT5Jd32gZ99V18xLMygL38i8Tsu4ru7edz6CzK8A2RScgUP2xfjXrsIgrrXt0/rPcHVqYyrG1EsBIoJUYtQUy+M/zHbTpDmZdOj0uOM3cBjYHlu//iesPkY6pvV9RdgYcYiliG4x19rQ0aEUasl+ZTvMxh4mLpUPh5gc5s/TtG30V9aundapwsOLf0R2oWcyqnfxROifTngw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=m8K8rfTh8nTzrnw7JpPtZZMmTWFZSgvhcqiMn1zjH5Y=; b=JhCfwXzO0baWy9CqDtDbNLF9ZaTgt4sYVeyB4ea1qSpYiZqRYrogTS10Q1ryKf/ViF45BPCHQifQn9gX6vd422yOn2pwGg4vgTK+58Zal310WAE2KRtzO5pSNZXiTxn8ZW+rLAaoIdPUSAxJ5WK01+vfYU23U20Xn8BBrH5vygdPCw6kKglPGY9r1txhbsKpJKpPgfj4MJe4rlb3QL3xFHaLddUG3rs5mlUsWHeipf/hf5XVYBfbMQku83fb2cW/dvHe8dyDzjdvrDNDkJNOfpIopAu02s2gtvtD1WOaTv+gwLo9GY46NkRBT6pGJ658hNP++TkQu1A3LsWa3PB/9A== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Message-ID: <042f76dd-d189-c40a-baec-68ded32aa797@suse.com> Date: Thu, 11 May 2023 14:07:12 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.10.1 Subject: [PATCH 5/7] x86: detect PIT aliasing on ports other than 0x4[0-3] Content-Language: en-US From: Jan Beulich To: "xen-devel@lists.xenproject.org" Cc: Andrew Cooper , Wei Liu , =?utf-8?q?Roger_Pau_Monn=C3=A9?= References: <95129c04-f37c-9e26-e65d-786a1db2f003@suse.com> In-Reply-To: <95129c04-f37c-9e26-e65d-786a1db2f003@suse.com> X-ClientProxiedBy: FR0P281CA0221.DEUP281.PROD.OUTLOOK.COM (2603:10a6:d10:ac::17) To VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: VE1PR04MB6560:EE_|VI1PR04MB7072:EE_ X-MS-Office365-Filtering-Correlation-Id: 88a83702-e00d-411a-3ce5-08db5218420f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Piw6xsXWIlRnmjXfRN6ILvU41a/F2MUffr1rvlRQAgppIDeqYvesIV98II/UysaHxr/WmArp9kARj2198WkAr+2PhkRUlisFg+6qYr+EUHDS3EZRsB8CQRrTaiTUMe/xje18lRQllTIV0FneYeglDwdDijoipoYbsgy5+s5CkR+ARor9ReIesOOfluOK9L7naOHnYo2XreX+Dq2ENkIMlmXJrQG1OcOj6tH1tIqFaCyEGWbqcMn5nJh8KYKWJoJ4ZSXUrKHlLYeS3gzEwzRjVHWXr9M+ruD6/KrnGkaJ42Hkk6WDrOtxEqlR4iKF1fAOnrN0ADOe6ybJY8HALQmU1S4YOGdsMCZy/XB+17LIGD21clyYrIvLaoZplZrmK4l5wkx3KgF2hX7BeKQuBEFMRRh/g3/A1QHc9V9wgaPmZQl0Y0UF7Fw3rxwhgQnMFIXiJ4J6Zla1V9wG+HhFMlBh0pssqOtbLZa+fvz79qHKj4EOy/L82S8SRFHIi4tyPOwYyde8HWS2g/5Fb68y8u9psa63Zpfq+vov06zkWVNrbhZp3QTyRF57LCkGPVZDnoqxwc1BIxkz3Sz3Ea7E2WHxELr5g5qDcDLtFQ5QnWjkK2RXw0CFbIoQvzhuA7+YTsZqGlbJv5Dut8FmrbKZQwO28Q== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:VE1PR04MB6560.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(376002)(366004)(39860400002)(136003)(396003)(346002)(451199021)(31686004)(36756003)(2906002)(41300700001)(86362001)(38100700002)(8676002)(31696002)(5660300002)(8936002)(4326008)(316002)(83380400001)(478600001)(66946007)(6506007)(6916009)(186003)(6486002)(66476007)(66556008)(26005)(54906003)(6512007)(2616005)(43740500002)(45980500001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?eqDL2Vn9rcMObnuHzPLXf5b8sNZe?= =?utf-8?q?FtVsJ6kaotKbKeUAMMtQXi65yVvjy2eJXHBJ0BmuAVI1U0SWHTAsnSrokVtRFxNY7?= =?utf-8?q?C99fAWBLD35OgYb2AqGbnbZn1s6iRzphmXAvkMIzMQW+A89rmh9mAYIFBKItqytYZ?= =?utf-8?q?SU3ULgQIfQ8Lzk8DE0yOASAcJ75+HOkHv/3K5C/MKHIKQvuWLaMrz0AHJ0imVIyZj?= =?utf-8?q?DUOl7NfMDg8O26mLiZGjXKJkksssXhI1x45YsT+9E/8tk5T/ur44Y1bAuBfNxLiNc?= =?utf-8?q?i82VfiYYQk2q4iw+DcSoWt0cHC6o7jwilLYbfE+eNiFO1nGcbGp3cRQweZhNB+ur6?= =?utf-8?q?pknb3jO6+B9vhnbCv/zNPuQFjBUp18x1WyNrfRsWdsemLH8dFLSTE4iWoI4FPWLPq?= =?utf-8?q?5j407XPipgk2aSqLpm5rI6w7cimZ4esrWp4BdSAtdwyE7S21b6wgToxNGoJLDqCSc?= =?utf-8?q?MIvKhvJeZswjVeOU/fvesoHQP2Ejs8uFhCHB+DtYUfIgr9ndLDQXC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4UdNxXLPZeNlwFbjkYCSrL91xnm?= =?utf-8?q?2Sahtn1ZBBd+AwIdyI5D4UJxabwoqTI3s1ME8vBjv5a47ujJwSKNG1iPfPlTO5aAt?= =?utf-8?q?fpIRvRTBp18fuhb5m88KZYGRWlI8hLOjo7/DkqCt38iDZUD5kxeFpzFGROsaQU26Z?= =?utf-8?q?4WUpawlDJppk?= X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: 88a83702-e00d-411a-3ce5-08db5218420f X-MS-Exchange-CrossTenant-AuthSource: VE1PR04MB6560.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 May 2023 12:07:14.5554 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: f7a17af6-1c5c-4a36-aa8b-f5be247aa4ba X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: +YOKnAJl09OcsZ+ZZzuKHL3FKqG08+tHX1y4K+dqtN+HaGc/c0hfJSjxbKqtebzTKJUiW/8vvY+KZkFpDBpCRg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR04MB7072 ... in order to also deny Dom0 access through the alias ports. Without this it is only giving the impression of denying access to PIT. Unlike for CMOS/RTC, do detection pretty early, to avoid disturbing normal operation later on (even if typically we won't use much of the PIT). Like for CMOS/RTC a fundamental assumption of the probing is that reads from the probed alias port won't have side effects (beyond such that PIT reads have anyway) in case it does not alias the PIT's. At to the port 0x61 accesses: Unlike other accesses we do, this masks off the top four bits (in addition to the bottom two ones), following Intel chipset documentation saying that these (read-only) bits should only be written with zero. Signed-off-by: Jan Beulich --- If Xen was running on top of another instance of itself (in HVM mode, not PVH, i.e. not as a shim), I'm afraid our vPIT logic would not allow the "Try to further make sure ..." check to pass in the Xen running on top: We don't respect the gate bit being clear when handling counter reads. (There are more unhandled [and unmentioned as being so] aspects of PIT behavior though, yet it's unclear in how far addressing at least some of them would be useful.) --- a/xen/arch/x86/dom0_build.c +++ b/xen/arch/x86/dom0_build.c @@ -504,7 +504,11 @@ int __init dom0_setup_permissions(struct } /* Interval Timer (PIT). */ - rc |= ioports_deny_access(d, 0x40, 0x43); + for ( offs = 0, i = pit_alias_mask & -pit_alias_mask ?: 4; + offs <= pit_alias_mask; offs += i ) + if ( !(offs & ~pit_alias_mask) ) + rc |= ioports_deny_access(d, 0x40 + offs, 0x43 + offs); + /* PIT Channel 2 / PC Speaker Control. */ rc |= ioports_deny_access(d, 0x61, 0x61); --- a/xen/arch/x86/include/asm/setup.h +++ b/xen/arch/x86/include/asm/setup.h @@ -53,6 +53,7 @@ extern unsigned long highmem_start; #endif extern unsigned int pic_alias_mask; +extern unsigned int pit_alias_mask; extern int8_t opt_smt; --- a/xen/arch/x86/time.c +++ b/xen/arch/x86/time.c @@ -425,6 +425,69 @@ static struct platform_timesource __init .resume = resume_pit, }; +unsigned int __initdata pit_alias_mask; + +static void __init probe_pit_alias(void) +{ + unsigned int mask = 0x1c; + uint8_t val = 0; + + /* + * Use channel 2 in mode 0 for probing. In this mode even a non-initial + * count is loaded independent of counting being / becoming enabled. Thus + * we have a 16-bit value fully under our control, to write and then check + * whether we can also read it back unaltered. + */ + + /* Turn off speaker output and disable channel 2 counting. */ + outb(inb(0x61) & 0x0c, 0x61); + + outb((2 << 6) | (3 << 4) | (0 << 1), PIT_MODE); /* Mode 0, LSB/MSB. */ + + do { + uint8_t val2; + unsigned int offs; + + outb(val, PIT_CH2); + outb(val ^ 0xff, PIT_CH2); + + /* Wait for the Null Count bit to clear. */ + do { + /* Latch status. */ + outb((3 << 6) | (1 << 5) | (1 << 3), PIT_MODE); + + /* Try to make sure we're actually having a PIT here. */ + val2 = inb(PIT_CH2); + if ( (val2 & ~(3 << 6)) != ((3 << 4) | (0 << 1)) ) + return; + } while ( val2 & (1 << 6) ); + + /* + * Try to further make sure we're actually having a PIT here. + * + * NB: Deliberately |, not ||, as we always want both reads. + */ + val2 = inb(PIT_CH2); + if ( (val2 ^ val) | (inb(PIT_CH2) ^ val ^ 0xff) ) + return; + + for ( offs = mask & -mask; offs <= mask; offs <<= 1 ) + { + if ( !(mask & offs) ) + continue; + val2 = inb(PIT_CH2 + offs); + if ( (val2 ^ val) | (inb(PIT_CH2 + offs) ^ val ^ 0xff) ) + mask &= ~offs; + } + } while ( mask && (val += 0x0b) ); /* Arbitrary uneven number. */ + + if ( mask ) + { + dprintk(XENLOG_INFO, "PIT aliasing mask: %02x\n", mask); + pit_alias_mask = mask; + } +} + /************************************************************ * PLATFORM TIMER 2: HIGH PRECISION EVENT TIMER (HPET) */ @@ -2390,6 +2453,8 @@ void __init early_time_init(void) } preinit_pit(); + probe_pit_alias(); + tmp = init_platform_timer(); plt_tsc.frequency = tmp;