VBoxAcpi.cpp revision 928e1d6581a40f26932a8b35526773805bc69e47
616c4e73368513b3c835881ff9f2386083afad01vboxsync/* $Id$ */
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync/** @file
616c4e73368513b3c835881ff9f2386083afad01vboxsync * VBoxAcpi - VirtualBox ACPI maniputation functionality.
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync */
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync/*
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync * Copyright (C) 2009 Sun Microsystems, Inc.
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync *
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync * available from http://www.virtualbox.org. This file is free software;
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync * you can redistribute it and/or modify it under the terms of the GNU
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync * General Public License (GPL) as published by the Free Software
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync *
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync * additional information or have any questions.
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync */
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync#if !defined(IN_RING3)
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync#error Pure R3 code
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync#endif
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync
3854599844436d009da0f32760d657fc7690a5a8vboxsync#define LOG_GROUP LOG_GROUP_DEV_ACPI
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync#include <VBox/pdmdev.h>
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync#include <VBox/pgm.h>
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync#include <VBox/log.h>
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync#include <VBox/param.h>
eb90548e8e40e597d65cdcc16ec958a3e09c1d73vboxsync#include <VBox/cfgm.h>
70e70d246c1b592db31d93071c48bb43ea61100evboxsync#include <VBox/mm.h>
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync#include <iprt/assert.h>
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync#include <iprt/alloc.h>
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync#include <iprt/string.h>
70e70d246c1b592db31d93071c48bb43ea61100evboxsync#include <iprt/file.h>
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync#ifdef VBOX_WITH_DYNAMIC_DSDT
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync/* vbox.dsl - input to generate proper DSDT on the fly */
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync# include <vboxdsl.hex>
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync#else
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync/* Statically compiled AML */
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync# include <vboxaml.hex>
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync#endif
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync#ifdef VBOX_WITH_DYNAMIC_DSDT
616c4e73368513b3c835881ff9f2386083afad01vboxsyncstatic int prepareDynamicDsdt(PPDMDEVINS pDevIns,
616c4e73368513b3c835881ff9f2386083afad01vboxsync void* *ppPtr,
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync size_t *puDsdtLen)
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync{
3854599844436d009da0f32760d657fc7690a5a8vboxsync *ppPtr = NULL;
3854599844436d009da0f32760d657fc7690a5a8vboxsync *puDsdtLen = 0;
3854599844436d009da0f32760d657fc7690a5a8vboxsync return 0;
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync}
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync
616c4e73368513b3c835881ff9f2386083afad01vboxsyncstatic int cleanupDynamicDsdt(PPDMDEVINS pDevIns,
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync void* pPtr)
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync{
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync return 0;
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync}
ecb074924ed761fe89e91113482db5b7374441b0vboxsync
ecb074924ed761fe89e91113482db5b7374441b0vboxsync#else
ecb074924ed761fe89e91113482db5b7374441b0vboxsyncstatic int patchAml(PPDMDEVINS pDevIns, uint8_t* pAml, size_t uAmlLen)
ecb074924ed761fe89e91113482db5b7374441b0vboxsync{
ecb074924ed761fe89e91113482db5b7374441b0vboxsync uint16_t cNumCpus;
ecb074924ed761fe89e91113482db5b7374441b0vboxsync int rc;
616c4e73368513b3c835881ff9f2386083afad01vboxsync
928e1d6581a40f26932a8b35526773805bc69e47vboxsync rc = CFGMR3QueryU16Def(pDevIns->pCfg, "NumCPUs", &cNumCpus, 1);
616c4e73368513b3c835881ff9f2386083afad01vboxsync
ecb074924ed761fe89e91113482db5b7374441b0vboxsync if (RT_FAILURE(rc))
ecb074924ed761fe89e91113482db5b7374441b0vboxsync return rc;
616c4e73368513b3c835881ff9f2386083afad01vboxsync
a2779f5bd9cc55216a2911d7714ede6a77cac3b2vboxsync /* Clear CPU objects at all, if needed */
3854599844436d009da0f32760d657fc7690a5a8vboxsync bool fShowCpu;
928e1d6581a40f26932a8b35526773805bc69e47vboxsync rc = CFGMR3QueryBoolDef(pDevIns->pCfg, "ShowCpu", &fShowCpu, false);
3854599844436d009da0f32760d657fc7690a5a8vboxsync if (RT_FAILURE(rc))
24eb79e9b779a21a256c7e15e5ad8ef97f7b6178vboxsync return rc;
3854599844436d009da0f32760d657fc7690a5a8vboxsync
3854599844436d009da0f32760d657fc7690a5a8vboxsync if (!fShowCpu)
24eb79e9b779a21a256c7e15e5ad8ef97f7b6178vboxsync cNumCpus = 0;
24eb79e9b779a21a256c7e15e5ad8ef97f7b6178vboxsync
616c4e73368513b3c835881ff9f2386083afad01vboxsync /**
ecb074924ed761fe89e91113482db5b7374441b0vboxsync * Now search AML for:
ecb074924ed761fe89e91113482db5b7374441b0vboxsync * AML_PROCESSOR_OP (UINT16) 0x5b83
ecb074924ed761fe89e91113482db5b7374441b0vboxsync * and replace whole block with
616c4e73368513b3c835881ff9f2386083afad01vboxsync * AML_NOOP_OP (UINT16) 0xa3
ecb074924ed761fe89e91113482db5b7374441b0vboxsync * for VCPU not configured
ecb074924ed761fe89e91113482db5b7374441b0vboxsync */
3854599844436d009da0f32760d657fc7690a5a8vboxsync for (uint32_t i = 0; i < uAmlLen - 7; i++)
ecb074924ed761fe89e91113482db5b7374441b0vboxsync {
ecb074924ed761fe89e91113482db5b7374441b0vboxsync /*
616c4e73368513b3c835881ff9f2386083afad01vboxsync * AML_PROCESSOR_OP
ecb074924ed761fe89e91113482db5b7374441b0vboxsync *
616c4e73368513b3c835881ff9f2386083afad01vboxsync * DefProcessor := ProcessorOp PkgLength NameString ProcID
ecb074924ed761fe89e91113482db5b7374441b0vboxsync PblkAddr PblkLen ObjectList
ecb074924ed761fe89e91113482db5b7374441b0vboxsync * ProcessorOp := ExtOpPrefix 0x83
ecb074924ed761fe89e91113482db5b7374441b0vboxsync * ProcID := ByteData
ecb074924ed761fe89e91113482db5b7374441b0vboxsync * PblkAddr := DwordData
ecb074924ed761fe89e91113482db5b7374441b0vboxsync * PblkLen := ByteData
ecb074924ed761fe89e91113482db5b7374441b0vboxsync */
ecb074924ed761fe89e91113482db5b7374441b0vboxsync if ((pAml[i] == 0x5b) && (pAml[i+1] == 0x83))
ecb074924ed761fe89e91113482db5b7374441b0vboxsync {
ecb074924ed761fe89e91113482db5b7374441b0vboxsync if ((pAml[i+3] != 'C') || (pAml[i+4] != 'P'))
ecb074924ed761fe89e91113482db5b7374441b0vboxsync /* false alarm, not named starting CP */
ecb074924ed761fe89e91113482db5b7374441b0vboxsync continue;
616c4e73368513b3c835881ff9f2386083afad01vboxsync
3854599844436d009da0f32760d657fc7690a5a8vboxsync /* Processor ID */
3854599844436d009da0f32760d657fc7690a5a8vboxsync if (pAml[i+7] < cNumCpus)
3854599844436d009da0f32760d657fc7690a5a8vboxsync continue;
ecb074924ed761fe89e91113482db5b7374441b0vboxsync
ecb074924ed761fe89e91113482db5b7374441b0vboxsync /* Will fill unwanted CPU block with NOOPs */
616c4e73368513b3c835881ff9f2386083afad01vboxsync /*
616c4e73368513b3c835881ff9f2386083afad01vboxsync * See 18.2.4 Package Length Encoding in ACPI spec
616c4e73368513b3c835881ff9f2386083afad01vboxsync * for full format
ecb074924ed761fe89e91113482db5b7374441b0vboxsync */
ecb074924ed761fe89e91113482db5b7374441b0vboxsync uint32_t cBytes = pAml[i + 2];
616c4e73368513b3c835881ff9f2386083afad01vboxsync AssertReleaseMsg((cBytes >> 6) == 0,
ecb074924ed761fe89e91113482db5b7374441b0vboxsync ("So far, we only understand simple package length"));
616c4e73368513b3c835881ff9f2386083afad01vboxsync
ecb074924ed761fe89e91113482db5b7374441b0vboxsync /* including AML_PROCESSOR_OP itself */
ecb074924ed761fe89e91113482db5b7374441b0vboxsync for (uint32_t j = 0; j < cBytes + 2; j++)
ecb074924ed761fe89e91113482db5b7374441b0vboxsync pAml[i+j] = 0xa3;
ecb074924ed761fe89e91113482db5b7374441b0vboxsync
ecb074924ed761fe89e91113482db5b7374441b0vboxsync /* Can increase i by cBytes + 1, but not really worth it */
ecb074924ed761fe89e91113482db5b7374441b0vboxsync }
ecb074924ed761fe89e91113482db5b7374441b0vboxsync }
ecb074924ed761fe89e91113482db5b7374441b0vboxsync
ecb074924ed761fe89e91113482db5b7374441b0vboxsync /* now recompute checksum, whole file byte sum must be 0 */
ecb074924ed761fe89e91113482db5b7374441b0vboxsync pAml[9] = 0;
ecb074924ed761fe89e91113482db5b7374441b0vboxsync uint8_t aSum = 0;
ecb074924ed761fe89e91113482db5b7374441b0vboxsync for (uint32_t i = 0; i < uAmlLen; i++)
ecb074924ed761fe89e91113482db5b7374441b0vboxsync aSum = aSum + (uint8_t)pAml[i];
ecb074924ed761fe89e91113482db5b7374441b0vboxsync pAml[9] = (uint8_t) (0 - aSum);
616c4e73368513b3c835881ff9f2386083afad01vboxsync
ecb074924ed761fe89e91113482db5b7374441b0vboxsync return 0;
ecb074924ed761fe89e91113482db5b7374441b0vboxsync}
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync#endif
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync
689a05012d7f4988aa5e320b22fd1dc3df6c7ca4vboxsync/* Two only public functions */
689a05012d7f4988aa5e320b22fd1dc3df6c7ca4vboxsyncint acpiPrepareDsdt(PPDMDEVINS pDevIns, void * *ppPtr, size_t *puDsdtLen)
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync{
689a05012d7f4988aa5e320b22fd1dc3df6c7ca4vboxsync#ifdef VBOX_WITH_DYNAMIC_DSDT
689a05012d7f4988aa5e320b22fd1dc3df6c7ca4vboxsync return prepareDynamicDsdt(pDevIns, ppPtr, puDsdtLen);
689a05012d7f4988aa5e320b22fd1dc3df6c7ca4vboxsync#else
70e70d246c1b592db31d93071c48bb43ea61100evboxsync uint8_t *pbAmlCode = NULL;
eb90548e8e40e597d65cdcc16ec958a3e09c1d73vboxsync size_t cbAmlCode = 0;
70e70d246c1b592db31d93071c48bb43ea61100evboxsync char *pszAmlFilePath = NULL;
928e1d6581a40f26932a8b35526773805bc69e47vboxsync int rc = CFGMR3QueryStringAlloc(pDevIns->pCfg, "AmlFilePath", &pszAmlFilePath);
70e70d246c1b592db31d93071c48bb43ea61100evboxsync if (RT_SUCCESS(rc))
eb90548e8e40e597d65cdcc16ec958a3e09c1d73vboxsync {
70e70d246c1b592db31d93071c48bb43ea61100evboxsync /* Load from file. */
70e70d246c1b592db31d93071c48bb43ea61100evboxsync RTFILE FileAml = NIL_RTFILE;
70e70d246c1b592db31d93071c48bb43ea61100evboxsync
70e70d246c1b592db31d93071c48bb43ea61100evboxsync rc = RTFileOpen(&FileAml, pszAmlFilePath, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_NONE);
70e70d246c1b592db31d93071c48bb43ea61100evboxsync if (RT_SUCCESS(rc))
70e70d246c1b592db31d93071c48bb43ea61100evboxsync {
70e70d246c1b592db31d93071c48bb43ea61100evboxsync /*
70e70d246c1b592db31d93071c48bb43ea61100evboxsync * An AML file contains the raw DSDT thus the size of the file
70e70d246c1b592db31d93071c48bb43ea61100evboxsync * is equal to the size of the DSDT.
70e70d246c1b592db31d93071c48bb43ea61100evboxsync */
70e70d246c1b592db31d93071c48bb43ea61100evboxsync uint64_t cbAmlFile = 0;
70e70d246c1b592db31d93071c48bb43ea61100evboxsync rc = RTFileGetSize(FileAml, &cbAmlFile);
70e70d246c1b592db31d93071c48bb43ea61100evboxsync
70e70d246c1b592db31d93071c48bb43ea61100evboxsync cbAmlCode = (size_t)cbAmlFile;
70e70d246c1b592db31d93071c48bb43ea61100evboxsync
70e70d246c1b592db31d93071c48bb43ea61100evboxsync /* Don't use AML files over 4GB ;) */
70e70d246c1b592db31d93071c48bb43ea61100evboxsync if ( RT_SUCCESS(rc)
70e70d246c1b592db31d93071c48bb43ea61100evboxsync && ((uint64_t)cbAmlCode == cbAmlFile))
70e70d246c1b592db31d93071c48bb43ea61100evboxsync {
70e70d246c1b592db31d93071c48bb43ea61100evboxsync pbAmlCode = (uint8_t *)RTMemAllocZ(cbAmlCode);
70e70d246c1b592db31d93071c48bb43ea61100evboxsync if (pbAmlCode)
70e70d246c1b592db31d93071c48bb43ea61100evboxsync {
70e70d246c1b592db31d93071c48bb43ea61100evboxsync rc = RTFileReadAt(FileAml, 0, pbAmlCode, cbAmlCode, NULL);
70e70d246c1b592db31d93071c48bb43ea61100evboxsync
70e70d246c1b592db31d93071c48bb43ea61100evboxsync /*
70e70d246c1b592db31d93071c48bb43ea61100evboxsync * We fail if reading failed or the identifier at the
70e70d246c1b592db31d93071c48bb43ea61100evboxsync * beginning is wrong.
70e70d246c1b592db31d93071c48bb43ea61100evboxsync */
70e70d246c1b592db31d93071c48bb43ea61100evboxsync if ( RT_FAILURE(rc)
689a05012d7f4988aa5e320b22fd1dc3df6c7ca4vboxsync || strncmp((const char *)pbAmlCode, "DSDT", 4))
70e70d246c1b592db31d93071c48bb43ea61100evboxsync {
70e70d246c1b592db31d93071c48bb43ea61100evboxsync RTMemFree(pbAmlCode);
70e70d246c1b592db31d93071c48bb43ea61100evboxsync pbAmlCode = NULL;
70e70d246c1b592db31d93071c48bb43ea61100evboxsync
70e70d246c1b592db31d93071c48bb43ea61100evboxsync /* Return error if file header check failed */
70e70d246c1b592db31d93071c48bb43ea61100evboxsync if (RT_SUCCESS(rc))
70e70d246c1b592db31d93071c48bb43ea61100evboxsync rc = VERR_PARSE_ERROR;
70e70d246c1b592db31d93071c48bb43ea61100evboxsync }
70e70d246c1b592db31d93071c48bb43ea61100evboxsync }
70e70d246c1b592db31d93071c48bb43ea61100evboxsync else
70e70d246c1b592db31d93071c48bb43ea61100evboxsync rc = VERR_NO_MEMORY;
70e70d246c1b592db31d93071c48bb43ea61100evboxsync }
70e70d246c1b592db31d93071c48bb43ea61100evboxsync
70e70d246c1b592db31d93071c48bb43ea61100evboxsync RTFileClose(FileAml);
70e70d246c1b592db31d93071c48bb43ea61100evboxsync }
70e70d246c1b592db31d93071c48bb43ea61100evboxsync MMR3HeapFree(pszAmlFilePath);
eb90548e8e40e597d65cdcc16ec958a3e09c1d73vboxsync }
689a05012d7f4988aa5e320b22fd1dc3df6c7ca4vboxsync else if (rc == VERR_CFGM_VALUE_NOT_FOUND)
eb90548e8e40e597d65cdcc16ec958a3e09c1d73vboxsync {
70e70d246c1b592db31d93071c48bb43ea61100evboxsync rc = VINF_SUCCESS;
70e70d246c1b592db31d93071c48bb43ea61100evboxsync
70e70d246c1b592db31d93071c48bb43ea61100evboxsync /* Use the compiled in AML code */
689a05012d7f4988aa5e320b22fd1dc3df6c7ca4vboxsync cbAmlCode = sizeof(AmlCode);
689a05012d7f4988aa5e320b22fd1dc3df6c7ca4vboxsync pbAmlCode = (uint8_t *)RTMemAllocZ(cbAmlCode);
689a05012d7f4988aa5e320b22fd1dc3df6c7ca4vboxsync if (pbAmlCode)
689a05012d7f4988aa5e320b22fd1dc3df6c7ca4vboxsync memcpy(pbAmlCode, AmlCode, cbAmlCode);
70e70d246c1b592db31d93071c48bb43ea61100evboxsync else
70e70d246c1b592db31d93071c48bb43ea61100evboxsync rc = VERR_NO_MEMORY;
eb90548e8e40e597d65cdcc16ec958a3e09c1d73vboxsync }
70e70d246c1b592db31d93071c48bb43ea61100evboxsync else if (RT_FAILURE(rc))
70e70d246c1b592db31d93071c48bb43ea61100evboxsync return PDMDEV_SET_ERROR(pDevIns, rc,
689a05012d7f4988aa5e320b22fd1dc3df6c7ca4vboxsync N_("Configuration error: Failed to read \"AmlFilePath\""));
eb90548e8e40e597d65cdcc16ec958a3e09c1d73vboxsync
70e70d246c1b592db31d93071c48bb43ea61100evboxsync if (RT_SUCCESS(rc))
70e70d246c1b592db31d93071c48bb43ea61100evboxsync {
689a05012d7f4988aa5e320b22fd1dc3df6c7ca4vboxsync patchAml(pDevIns, pbAmlCode, cbAmlCode);
689a05012d7f4988aa5e320b22fd1dc3df6c7ca4vboxsync *ppPtr = pbAmlCode;
689a05012d7f4988aa5e320b22fd1dc3df6c7ca4vboxsync *puDsdtLen = cbAmlCode;
70e70d246c1b592db31d93071c48bb43ea61100evboxsync }
70e70d246c1b592db31d93071c48bb43ea61100evboxsync return rc;
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync#endif
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync}
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsyncint acpiCleanupDsdt(PPDMDEVINS pDevIns, void * pPtr)
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync{
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync#ifdef VBOX_WITH_DYNAMIC_DSDT
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync return cleanupDynamicDsdt(pDevIns, pPtr);
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync#else
70e70d246c1b592db31d93071c48bb43ea61100evboxsync if (pPtr)
70e70d246c1b592db31d93071c48bb43ea61100evboxsync RTMemFree(pPtr);
70e70d246c1b592db31d93071c48bb43ea61100evboxsync return VINF_SUCCESS;
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync#endif
7f8a04081173dbe3c72bddd0ffeb237e7f9070b0vboxsync}
616c4e73368513b3c835881ff9f2386083afad01vboxsync