From nobody Tue Oct 18 06:03:13 2022 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Ms3FF4Z4Gz4g8FK; Tue, 18 Oct 2022 06:03:13 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Ms3FF3SWdz3c1L; Tue, 18 Oct 2022 06:03:13 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1666072993; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=XcrO+eCOQree03pEeZClMnLPTTkBQspi00p3d3YZnrI=; b=dp1VIh69xrmFOo//jEwkkPH+iJuZjlGFD8wFaUmGydtObTJoUHkEwC+9GPdt1qIOSn63ZH jmuob/yZr09FZN8RmfpKF2N4dv5seb9hCLPMQkTXlNieVWxlMijHou2k9R7/lkPCwGa3+Q YEoLXSndTsHeHpBGqI8NsADspdMVgB3HXsBFESmAuSOyiEAwL6DMpsstTSyQODZfffO8gU CISRaGKsGRkz/e8vK9kDQLsRzyoCqtttpT/uSarbDCaNWhoUJonJ0o/eFVQGcGvLeQ0InL zpEUDKX5nMlZjEqgy/LtgdXokpf0J0jqq+8DOFEv6KwvqJgcfuZ9u4jD/x3uCg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Ms3FF2ZcFz15BN; Tue, 18 Oct 2022 06:03:13 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 29I63D2w097465; Tue, 18 Oct 2022 06:03:13 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 29I63DSP097464; Tue, 18 Oct 2022 06:03:13 GMT (envelope-from git) Date: Tue, 18 Oct 2022 06:03:13 GMT Message-Id: <202210180603.29I63DSP097464@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Colin Percival Subject: git: d1ca8cc638c4 - main - x86: Add MPTABLE_LINUX_BUG_COMPAT option List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: cperciva X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: d1ca8cc638c4abc8a968ebacd862ebb0f7b73b91 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1666072993; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=XcrO+eCOQree03pEeZClMnLPTTkBQspi00p3d3YZnrI=; b=QoxLNfIDIPHz9cnlyARsqNzZftceBm+SkzrB8qNlHEG0CWByQpmar8gT4WLrjVoB26u0xr EuyY/TTgy9XBDpUgvCEb7vD1IoPQMq0MkDJNH26NiJ/9vPl+Ml6zCr+GS+yQ96+LIF6AGI 3f8+MntHQcgl83LWIX4RxYMzoc21LjRdpsqxbo3NITOQD5hSwLB3k1QUfVXixUXyNlquCE p3I+n3Cwlktkkgob1klO3A96Yx31d1oMrMFbFCyfHQWfsx+lN0kQZLrduafYFFTflgZeQG VdNbJwlqPSIlcSpl4XB9oQrw8Mh77QsZQJ2AQmgpnbaExPU7QNEWZZJgrkEWIQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1666072993; a=rsa-sha256; cv=none; b=RcBwu71begMUv5L9ymHXK1W4gpfA6kSzSmnKB9MSns++SYgyjleQ/wgoHi8vyhR+ddlEVT arPQj2SAY6yfyqpYcauAL0u3fvHjqOWw+kljpiiQMXpKXWgMgFrd8gSqthp6dr4jIQon4D 9uROWTfvXnPbNVHZmb9NXrMHMWqD5bfr0Ecz77D0btmSa37w7U2XEst0N+hJsDde3XsKOu yFvRBVM8Cvp9/uacCXCxsJAVOCMupSWLRjWog/NzA4Af24/zbDFMTH4xX/wwcFukOpwqsJ Mb6vfbBHJlwcBu/TZNmrv++YP/F+nVERBRoXn6+4NaTGzb2QmwV2FwGMgrpkpQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by cperciva: URL: https://cgit.FreeBSD.org/src/commit/?id=d1ca8cc638c4abc8a968ebacd862ebb0f7b73b91 commit d1ca8cc638c4abc8a968ebacd862ebb0f7b73b91 Author: Colin Percival AuthorDate: 2022-07-13 00:45:18 +0000 Commit: Colin Percival CommitDate: 2022-10-18 06:02:22 +0000 x86: Add MPTABLE_LINUX_BUG_COMPAT option Linux has two bugs in its handling of the x86 MP table: 1. It assumes that there is always 640 kB of base memory, and looks for the MP table in the top kB of this even if the memory map indicates that memory location does not exist. 2. It ignores that entry_count field and instead iterates through the MP table by scanning until it runs out of bytes in the table. The Firecracker VM (and probably other related VMs) relies on both of these bugs. With the MPTABLE_LINUX_BUG_COMPAT option, we search for the MP table at address 639k even if that isn't in the memory map; and replace a zeroed entry_count with a value computed from scanning the table until we run out of table bytes. Reviewed by: imp Sponsored by: https://www.patreon.com/cperciva Differential Revision: https://reviews.freebsd.org/D35799 --- sys/conf/options.amd64 | 3 +++ sys/conf/options.i386 | 3 +++ sys/x86/x86/mptable.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/sys/conf/options.amd64 b/sys/conf/options.amd64 index 66ccfb7bbddb..a91cddb892fe 100644 --- a/sys/conf/options.amd64 +++ b/sys/conf/options.amd64 @@ -63,3 +63,6 @@ EFIRT opt_efirt.h # Don't create a "legacy" PCI bridge if none is found. NO_LEGACY_PCIB opt_cpu.h + +# Compatibility with Linux MP table bugs. +MPTABLE_LINUX_BUG_COMPAT diff --git a/sys/conf/options.i386 b/sys/conf/options.i386 index 03e88b985107..e9e0fa8bffc2 100644 --- a/sys/conf/options.i386 +++ b/sys/conf/options.i386 @@ -107,3 +107,6 @@ ISCI_LOGGING opt_isci.h # Don't create a "legacy" PCI bridge if none is found. NO_LEGACY_PCIB opt_cpu.h + +# Compatibility with Linux MP table bugs. +MPTABLE_LINUX_BUG_COMPAT diff --git a/sys/x86/x86/mptable.c b/sys/x86/x86/mptable.c index ec746b756b4f..173ffaa09b45 100644 --- a/sys/x86/x86/mptable.c +++ b/sys/x86/x86/mptable.c @@ -30,6 +30,7 @@ __FBSDID("$FreeBSD$"); #include "opt_mptable_force_htt.h" +#include "opt_mptable_linux_bug_compat.h" #include #include #include @@ -245,6 +246,34 @@ lookup_bus_type(char *name) return (UNKNOWN_BUSTYPE); } +#ifdef MPTABLE_LINUX_BUG_COMPAT +/* Compute the correct entry_count value. */ +static void +compute_entry_count(void) +{ + u_char *end = (u_char *)(mpct) + mpct->base_table_length; + u_char *entry = (u_char *)(mpct + 1); + size_t nentries = 0; + + while (entry < end) { + switch (*entry) { + case MPCT_ENTRY_PROCESSOR: + case MPCT_ENTRY_IOAPIC: + case MPCT_ENTRY_BUS: + case MPCT_ENTRY_INT: + case MPCT_ENTRY_LOCAL_INT: + break; + default: + panic("%s: Unknown MP Config Entry %d\n", __func__, + (int)*entry); + } + entry += basetable_entry_types[*entry].length; + nentries++; + } + mpct->entry_count = (uint16_t)(nentries); +} +#endif + /* * Look for an Intel MP spec table (ie, SMP capable hardware). */ @@ -273,6 +302,17 @@ mptable_probe(void) if ((x = search_for_sig(target, BIOS_COUNT)) >= 0) goto found; +#ifdef MPTABLE_LINUX_BUG_COMPAT + /* + * Linux assumes that it always has 640 kB of base memory and + * searches for the MP table at 639k regardless of whether that + * address is present in the system memory map. Some VM systems + * rely on this buggy behaviour. + */ + if ((x = search_for_sig(639 * 1024, 1024 / 4)) >= 0) + goto found; +#endif + /* nothing found */ return (ENXIO); @@ -321,6 +361,16 @@ found: printf( "MP Configuration Table version 1.%d found at %p\n", mpct->spec_rev, mpct); +#ifdef MPTABLE_LINUX_BUG_COMPAT + /* + * Linux ignores entry_count and instead scans the MP table + * until it runs out of bytes of table (as specified by the + * base_table_length field). Some VM systems rely on this + * buggy behaviour and record an entry_count of zero. + */ + if (mpct->entry_count == 0) + compute_entry_count(); +#endif } return (-100);