From nobody Thu Sep 19 01:44:57 2024 Delivered-To: importer2@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+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1683270803; cv=none; d=zohomail.com; s=zohoarc; b=hrXfVn2OAqbKg46OdpCRzd2pV63X7LbHju5WAMozQU9a1iXi2RZyDZMV08r3e2YjbubYwTYWDqQhoitl6fI4p21z7uH6hM9rbf0sBsmRNzdJwNDTwgxl/OsYH5xJfe32hBWHuanN9IVzZnXJibMrAfDFlYRUN/r0lkVICq17SqY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1683270803; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=CXPCS+kJxlRKApXWmifHvPqleJ4x39uVxri/m9e/MXc=; b=loaU4aDJfitVHlZCF35lI0SIAAor7jMov3nFnLWgrwCkIUwf/+rNi+tc0KsjEOGFXYTc8j27cVc3euQVn86rnJz72et+W0BNcSGepuLoTH/rZuTtrwAq6lDY3lNuJFk3Qd89+r5/SgKjrUqIMV8bDbnqGvSbQsIF9Rny8lVvYzE= 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+importer2=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 1683270803440134.93774090490012; Fri, 5 May 2023 00:13:23 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pupbH-0004mX-Fa; Fri, 05 May 2023 03:11:31 -0400 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 1pupbD-0004lQ-MA for qemu-devel@nongnu.org; Fri, 05 May 2023 03:11:29 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pupbA-0000f8-6U for qemu-devel@nongnu.org; Fri, 05 May 2023 03:11:27 -0400 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-349-jEEDYcpxNWutIkrtl8jEWw-1; Fri, 05 May 2023 03:11:21 -0400 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9655E3806062; Fri, 5 May 2023 07:11:20 +0000 (UTC) Received: from sirius.home.kraxel.org (unknown [10.39.192.60]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 5B12E492C13; Fri, 5 May 2023 07:11:20 +0000 (UTC) Received: by sirius.home.kraxel.org (Postfix, from userid 1000) id C066C1800986; Fri, 5 May 2023 09:11:17 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1683270682; 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=CXPCS+kJxlRKApXWmifHvPqleJ4x39uVxri/m9e/MXc=; b=CWSVdE1ckorRQP7ShFShC3DvuyHgV/1lRsxr3z894Ccp2H7B4T+QF3RAu6bxw2reL4/vNS zQsNO2bL0uybwsLEfFChPKrQ3jyUroP02bKSTRVqN7PdI+5atlTpmcfGBcLQ2sAohU+3oZ x0gdmj5VMzAqoQbnI+GlaBbB9fcBWfA= X-MC-Unique: jEEDYcpxNWutIkrtl8jEWw-1 From: Gerd Hoffmann To: seabios@seabios.org Cc: qemu-devel@nongnu.org, Gerd Hoffmann Subject: [PATCH v3 2/6] detect physical address space size Date: Fri, 5 May 2023 09:11:13 +0200 Message-Id: <20230505071117.369471-3-kraxel@redhat.com> In-Reply-To: <20230505071117.369471-1-kraxel@redhat.com> References: <20230505071117.369471-1-kraxel@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 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+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=kraxel@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -22 X-Spam_score: -2.3 X-Spam_bar: -- X-Spam_report: (-2.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.161, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1683270804599100001 Content-Type: text/plain; charset="utf-8" Check for pae and long mode using cpuid. If present also read the physical address bits. Apply some qemu sanity checks (see below). Record results in PhysBits and LongMode variables. In case we are not sure what the address space size is leave the PhysBits variable unset. On qemu we have the problem that for historical reasons x86_64 processors advertise 40 physical address space bits by default, even in case the host supports less than that so actually using the whole address space will not work. Because of that the code applies some extra sanity checks in case we find 40 (or less) physical address space bits advertised. Only known-good values (which is 40 for amd processors and 36+39 for intel processors) will be accepted as valid. Recommendation is to use 'qemu -cpu ${name},host-phys-bits=3Don' to advertise valid physical address space bits to the guest. Some distro builds enable this by default, and most likely the qemu default will change in near future too. Signed-off-by: Gerd Hoffmann --- src/fw/paravirt.h | 2 ++ src/fw/paravirt.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/src/fw/paravirt.h b/src/fw/paravirt.h index 4e2e993ba9d3..62a2cd075d2b 100644 --- a/src/fw/paravirt.h +++ b/src/fw/paravirt.h @@ -31,6 +31,8 @@ typedef struct QemuCfgDmaAccess { extern u32 RamSize; extern u64 RamSizeOver4G; extern int PlatformRunningOn; +extern u8 CPUPhysBits; +extern u8 CPULongMode; =20 static inline int runningOnQEMU(void) { return CONFIG_QEMU || ( diff --git a/src/fw/paravirt.c b/src/fw/paravirt.c index c880cb10a1bc..fc308bf1ef1d 100644 --- a/src/fw/paravirt.c +++ b/src/fw/paravirt.c @@ -32,6 +32,10 @@ u32 RamSize; // Amount of continuous ram >4Gig u64 RamSizeOver4G; +// physical address space bits +u8 CPUPhysBits; +// 64bit processor +u8 CPULongMode; // Type of emulator platform. int PlatformRunningOn VARFSEG; // cfg enabled @@ -129,6 +133,58 @@ static void kvmclock_init(void) tsctimer_setfreq(MHz * 1000, "kvmclock"); } =20 +static void physbits(int qemu_quirk) +{ + unsigned int max, eax, ebx, ecx, edx; + unsigned int physbits; + char signature[13]; + int pae =3D 0, valid =3D 0; + + cpuid(0, &eax, &ebx, &ecx, &edx); + memcpy(signature + 0, &ebx, 4); + memcpy(signature + 4, &edx, 4); + memcpy(signature + 8, &ecx, 4); + signature[12] =3D 0; + if (eax >=3D 1) { + cpuid(1, &eax, &ebx, &ecx, &edx); + pae =3D (edx & (1 << 6)); + } + + cpuid(0x80000000, &eax, &ebx, &ecx, &edx); + max =3D eax; + + if (max >=3D 0x80000001) { + cpuid(0x80000001, &eax, &ebx, &ecx, &edx); + CPULongMode =3D !!(edx & (1 << 29)); + } + + if (pae && CPULongMode && max >=3D 0x80000008) { + cpuid(0x80000008, &eax, &ebx, &ecx, &edx); + physbits =3D (u8)eax; + if (!qemu_quirk) { + valid =3D 1; + } else if (physbits >=3D 41) { + valid =3D 1; + } else if (strcmp(signature, "GenuineIntel") =3D=3D 0) { + if ((physbits =3D=3D 36) || (physbits =3D=3D 39)) + valid =3D 1; + } else if (strcmp(signature, "AuthenticAMD") =3D=3D 0) { + if (physbits =3D=3D 40) + valid =3D 1; + } + } else { + physbits =3D pae ? 36 : 32; + valid =3D 1; + } + + dprintf(1, "%s: signature=3D\"%s\", pae=3D%s, lm=3D%s, phys-bits=3D%d,= valid=3D%s\n", + __func__, signature, pae ? "yes" : "no", CPULongMode ? "yes" := "no", + physbits, valid ? "yes" : "no"); + + if (valid) + CPUPhysBits =3D physbits; +} + static void qemu_detect(void) { if (!CONFIG_QEMU_HARDWARE) @@ -161,6 +217,7 @@ static void qemu_detect(void) dprintf(1, "Running on QEMU (unknown nb: %04x:%04x)\n", v, d); break; } + physbits(1); } =20 static int qemu_early_e820(void); --=20 2.40.1