GIM.cpp revision 236b2935f217749893b7034e59da3e3568928ace
236b2935f217749893b7034e59da3e3568928acevboxsync/* $Id$ */
236b2935f217749893b7034e59da3e3568928acevboxsync/** @file
236b2935f217749893b7034e59da3e3568928acevboxsync * GIM - Guest Interface Manager.
236b2935f217749893b7034e59da3e3568928acevboxsync */
236b2935f217749893b7034e59da3e3568928acevboxsync
236b2935f217749893b7034e59da3e3568928acevboxsync/*
236b2935f217749893b7034e59da3e3568928acevboxsync * Copyright (C) 2014 Oracle Corporation
236b2935f217749893b7034e59da3e3568928acevboxsync *
236b2935f217749893b7034e59da3e3568928acevboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
236b2935f217749893b7034e59da3e3568928acevboxsync * available from http://www.virtualbox.org. This file is free software;
236b2935f217749893b7034e59da3e3568928acevboxsync * you can redistribute it and/or modify it under the terms of the GNU
236b2935f217749893b7034e59da3e3568928acevboxsync * General Public License (GPL) as published by the Free Software
236b2935f217749893b7034e59da3e3568928acevboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
236b2935f217749893b7034e59da3e3568928acevboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
236b2935f217749893b7034e59da3e3568928acevboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
236b2935f217749893b7034e59da3e3568928acevboxsync */
236b2935f217749893b7034e59da3e3568928acevboxsync
236b2935f217749893b7034e59da3e3568928acevboxsync/** @page pg_gim GIM - The Guest Interface Manager
236b2935f217749893b7034e59da3e3568928acevboxsync *
236b2935f217749893b7034e59da3e3568928acevboxsync * The Guest Interface Manager abstracts an interface provider through which
236b2935f217749893b7034e59da3e3568928acevboxsync * guests may interact with the hypervisor.
236b2935f217749893b7034e59da3e3568928acevboxsync *
236b2935f217749893b7034e59da3e3568928acevboxsync * @see grp_gim
236b2935f217749893b7034e59da3e3568928acevboxsync *
236b2935f217749893b7034e59da3e3568928acevboxsync *
236b2935f217749893b7034e59da3e3568928acevboxsync * @section sec_gim_provider Providers
236b2935f217749893b7034e59da3e3568928acevboxsync *
236b2935f217749893b7034e59da3e3568928acevboxsync * A GIM provider implements a particular hypervisor interface such as Microsoft
236b2935f217749893b7034e59da3e3568928acevboxsync * Hyper-V, Linux KVM and so on. It hooks into various components in the VMM to
236b2935f217749893b7034e59da3e3568928acevboxsync * ease the guest in running under a recognized virtualized environment. This is
236b2935f217749893b7034e59da3e3568928acevboxsync * also referred to as paravirtualization interfaces.
236b2935f217749893b7034e59da3e3568928acevboxsync *
236b2935f217749893b7034e59da3e3568928acevboxsync * The idea behind this is primarily for making guests more accurate and
236b2935f217749893b7034e59da3e3568928acevboxsync * efficient when operating in a virtualized environment.
236b2935f217749893b7034e59da3e3568928acevboxsync *
236b2935f217749893b7034e59da3e3568928acevboxsync * For instance, a guest when interfaced to VirtualBox through a GIM provider
236b2935f217749893b7034e59da3e3568928acevboxsync * may rely on the provider (and VirtualBox ultimately) for providing the
236b2935f217749893b7034e59da3e3568928acevboxsync * correct TSC frequency and may therefore not have to caliberate the TSC
236b2935f217749893b7034e59da3e3568928acevboxsync * itself, resulting in higher accuracy and saving several CPU cycles.
236b2935f217749893b7034e59da3e3568928acevboxsync *
236b2935f217749893b7034e59da3e3568928acevboxsync * Only one provider can be active for a running VM.
236b2935f217749893b7034e59da3e3568928acevboxsync */
236b2935f217749893b7034e59da3e3568928acevboxsync
236b2935f217749893b7034e59da3e3568928acevboxsync/*******************************************************************************
236b2935f217749893b7034e59da3e3568928acevboxsync* Header Files *
236b2935f217749893b7034e59da3e3568928acevboxsync*******************************************************************************/
236b2935f217749893b7034e59da3e3568928acevboxsync#define LOG_GROUP LOG_GROUP_GIM
236b2935f217749893b7034e59da3e3568928acevboxsync#include <VBox/log.h>
236b2935f217749893b7034e59da3e3568928acevboxsync#include "GIMInternal.h"
236b2935f217749893b7034e59da3e3568928acevboxsync#include <VBox/vmm/vm.h>
236b2935f217749893b7034e59da3e3568928acevboxsync#include <VBox/vmm/hm.h>
236b2935f217749893b7034e59da3e3568928acevboxsync#include <VBox/vmm/ssm.h>
236b2935f217749893b7034e59da3e3568928acevboxsync
236b2935f217749893b7034e59da3e3568928acevboxsync#include <iprt/err.h>
236b2935f217749893b7034e59da3e3568928acevboxsync#include <iprt/string.h>
236b2935f217749893b7034e59da3e3568928acevboxsync
236b2935f217749893b7034e59da3e3568928acevboxsync/* Include all GIM providers. */
236b2935f217749893b7034e59da3e3568928acevboxsync#include "GIMHvInternal.h"
236b2935f217749893b7034e59da3e3568928acevboxsync
236b2935f217749893b7034e59da3e3568928acevboxsync/*******************************************************************************
236b2935f217749893b7034e59da3e3568928acevboxsync* Internal Functions *
236b2935f217749893b7034e59da3e3568928acevboxsync*******************************************************************************/
236b2935f217749893b7034e59da3e3568928acevboxsyncstatic DECLCALLBACK(int) gimR3Save(PVM pVM, PSSMHANDLE pSSM);
236b2935f217749893b7034e59da3e3568928acevboxsyncstatic DECLCALLBACK(int) gimR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass);
236b2935f217749893b7034e59da3e3568928acevboxsync
236b2935f217749893b7034e59da3e3568928acevboxsync
236b2935f217749893b7034e59da3e3568928acevboxsync/**
236b2935f217749893b7034e59da3e3568928acevboxsync * Initializes the GIM.
236b2935f217749893b7034e59da3e3568928acevboxsync *
236b2935f217749893b7034e59da3e3568928acevboxsync * @returns VBox status code.
236b2935f217749893b7034e59da3e3568928acevboxsync * @param pVM Pointer to the VM.
236b2935f217749893b7034e59da3e3568928acevboxsync */
236b2935f217749893b7034e59da3e3568928acevboxsyncVMMR3_INT_DECL(int) GIMR3Init(PVM pVM)
236b2935f217749893b7034e59da3e3568928acevboxsync{
236b2935f217749893b7034e59da3e3568928acevboxsync LogFlow(("GIMR3Init\n"));
236b2935f217749893b7034e59da3e3568928acevboxsync
236b2935f217749893b7034e59da3e3568928acevboxsync /*
236b2935f217749893b7034e59da3e3568928acevboxsync * Assert alignment and sizes.
236b2935f217749893b7034e59da3e3568928acevboxsync */
236b2935f217749893b7034e59da3e3568928acevboxsync AssertCompile(sizeof(pVM->gim.s) <= sizeof(pVM->gim.padding));
236b2935f217749893b7034e59da3e3568928acevboxsync
236b2935f217749893b7034e59da3e3568928acevboxsync /*
236b2935f217749893b7034e59da3e3568928acevboxsync * Register the saved state data unit.
236b2935f217749893b7034e59da3e3568928acevboxsync */
236b2935f217749893b7034e59da3e3568928acevboxsync int rc;
236b2935f217749893b7034e59da3e3568928acevboxsync#if 0
236b2935f217749893b7034e59da3e3568928acevboxsync rc = SSMR3RegisterInternal(pVM, "GIM", 0, GIM_SSM_VERSION, sizeof(GIM),
236b2935f217749893b7034e59da3e3568928acevboxsync NULL, NULL, NULL,
236b2935f217749893b7034e59da3e3568928acevboxsync NULL, gimR3Save, NULL,
236b2935f217749893b7034e59da3e3568928acevboxsync NULL, gimR3Load, NULL);
236b2935f217749893b7034e59da3e3568928acevboxsync if (RT_FAILURE(rc))
236b2935f217749893b7034e59da3e3568928acevboxsync return rc;
236b2935f217749893b7034e59da3e3568928acevboxsync#endif
236b2935f217749893b7034e59da3e3568928acevboxsync
236b2935f217749893b7034e59da3e3568928acevboxsync /*
236b2935f217749893b7034e59da3e3568928acevboxsync * Read configuration.
236b2935f217749893b7034e59da3e3568928acevboxsync */
236b2935f217749893b7034e59da3e3568928acevboxsync PCFGMNODE pCfgNode = CFGMR3GetChild(CFGMR3GetRoot(pVM), "GIM/");
236b2935f217749893b7034e59da3e3568928acevboxsync
236b2935f217749893b7034e59da3e3568928acevboxsync /** @cfgm{GIM/Provider, string}
236b2935f217749893b7034e59da3e3568928acevboxsync * The name of the GIM provider. The default is "none". */
236b2935f217749893b7034e59da3e3568928acevboxsync char szProvider[64];
236b2935f217749893b7034e59da3e3568928acevboxsync rc = CFGMR3QueryStringDef(pCfgNode, "Provider", szProvider, sizeof(szProvider), "none");
236b2935f217749893b7034e59da3e3568928acevboxsync AssertLogRelRCReturn(rc, rc);
236b2935f217749893b7034e59da3e3568928acevboxsync
236b2935f217749893b7034e59da3e3568928acevboxsync /*
236b2935f217749893b7034e59da3e3568928acevboxsync * Setup the GIM provider for this VM.
236b2935f217749893b7034e59da3e3568928acevboxsync */
236b2935f217749893b7034e59da3e3568928acevboxsync LogRel(("GIM: Using provider \"%s\"\n", szProvider));
236b2935f217749893b7034e59da3e3568928acevboxsync if (!RTStrCmp(szProvider, "none"))
236b2935f217749893b7034e59da3e3568928acevboxsync {
236b2935f217749893b7034e59da3e3568928acevboxsync Assert(!pVM->gim.s.fEnabled);
236b2935f217749893b7034e59da3e3568928acevboxsync pVM->gim.s.enmProvider = GIMPROVIDER_NONE;
236b2935f217749893b7034e59da3e3568928acevboxsync }
236b2935f217749893b7034e59da3e3568928acevboxsync else
236b2935f217749893b7034e59da3e3568928acevboxsync {
236b2935f217749893b7034e59da3e3568928acevboxsync pVM->gim.s.fEnabled = true;
236b2935f217749893b7034e59da3e3568928acevboxsync if (!RTStrCmp(szProvider, "HyperV"))
236b2935f217749893b7034e59da3e3568928acevboxsync {
236b2935f217749893b7034e59da3e3568928acevboxsync pVM->gim.s.enmProvider = GIMPROVIDER_HYPERV;
236b2935f217749893b7034e59da3e3568928acevboxsync rc = GIMR3HvInit(pVM);
236b2935f217749893b7034e59da3e3568928acevboxsync }
236b2935f217749893b7034e59da3e3568928acevboxsync /** @todo KVM and others. */
236b2935f217749893b7034e59da3e3568928acevboxsync else
236b2935f217749893b7034e59da3e3568928acevboxsync {
236b2935f217749893b7034e59da3e3568928acevboxsync LogRel(("GIM: Provider \"%s\" unknown.\n", szProvider));
236b2935f217749893b7034e59da3e3568928acevboxsync rc = VERR_NOT_SUPPORTED;
236b2935f217749893b7034e59da3e3568928acevboxsync }
236b2935f217749893b7034e59da3e3568928acevboxsync }
236b2935f217749893b7034e59da3e3568928acevboxsync
236b2935f217749893b7034e59da3e3568928acevboxsync return rc;
236b2935f217749893b7034e59da3e3568928acevboxsync}
236b2935f217749893b7034e59da3e3568928acevboxsync
236b2935f217749893b7034e59da3e3568928acevboxsync
236b2935f217749893b7034e59da3e3568928acevboxsync/**
236b2935f217749893b7034e59da3e3568928acevboxsync * Applies relocations to data and code managed by this
236b2935f217749893b7034e59da3e3568928acevboxsync * component. This function will be called at init and
236b2935f217749893b7034e59da3e3568928acevboxsync * whenever the VMM need to relocate it self inside the GC.
236b2935f217749893b7034e59da3e3568928acevboxsync *
236b2935f217749893b7034e59da3e3568928acevboxsync * @param pVM Pointer to the VM.
236b2935f217749893b7034e59da3e3568928acevboxsync * @param offDelta Relocation delta relative to old location.
236b2935f217749893b7034e59da3e3568928acevboxsync */
236b2935f217749893b7034e59da3e3568928acevboxsyncVMM_INT_DECL(void) GIMR3Relocate(PVM pVM, RTGCINTPTR offDelta)
236b2935f217749893b7034e59da3e3568928acevboxsync{
236b2935f217749893b7034e59da3e3568928acevboxsync LogFlow(("GIMR3Relocate\n"));
236b2935f217749893b7034e59da3e3568928acevboxsync
236b2935f217749893b7034e59da3e3568928acevboxsync if ( !pVM->gim.s.fEnabled
236b2935f217749893b7034e59da3e3568928acevboxsync || HMIsEnabled(pVM))
236b2935f217749893b7034e59da3e3568928acevboxsync {
236b2935f217749893b7034e59da3e3568928acevboxsync return;
236b2935f217749893b7034e59da3e3568928acevboxsync }
236b2935f217749893b7034e59da3e3568928acevboxsync
236b2935f217749893b7034e59da3e3568928acevboxsync switch (pVM->gim.s.enmProvider)
236b2935f217749893b7034e59da3e3568928acevboxsync {
236b2935f217749893b7034e59da3e3568928acevboxsync case GIMPROVIDER_HYPERV:
236b2935f217749893b7034e59da3e3568928acevboxsync {
236b2935f217749893b7034e59da3e3568928acevboxsync GIMR3HvRelocate(pVM, offDelta);
236b2935f217749893b7034e59da3e3568928acevboxsync break;
236b2935f217749893b7034e59da3e3568928acevboxsync }
236b2935f217749893b7034e59da3e3568928acevboxsync
236b2935f217749893b7034e59da3e3568928acevboxsync case GIMPROVIDER_KVM: /** @todo KVM. */
236b2935f217749893b7034e59da3e3568928acevboxsync default:
236b2935f217749893b7034e59da3e3568928acevboxsync {
236b2935f217749893b7034e59da3e3568928acevboxsync AssertMsgFailed(("Invalid provider id %#x\n", pVM->gim.s.enmProvider));
236b2935f217749893b7034e59da3e3568928acevboxsync break;
236b2935f217749893b7034e59da3e3568928acevboxsync }
236b2935f217749893b7034e59da3e3568928acevboxsync }
236b2935f217749893b7034e59da3e3568928acevboxsync}
236b2935f217749893b7034e59da3e3568928acevboxsync
236b2935f217749893b7034e59da3e3568928acevboxsync
236b2935f217749893b7034e59da3e3568928acevboxsync/**
236b2935f217749893b7034e59da3e3568928acevboxsync * Execute state save operation.
236b2935f217749893b7034e59da3e3568928acevboxsync *
236b2935f217749893b7034e59da3e3568928acevboxsync * @returns VBox status code.
236b2935f217749893b7034e59da3e3568928acevboxsync * @param pVM Pointer to the VM.
236b2935f217749893b7034e59da3e3568928acevboxsync * @param pSSM SSM operation handle.
236b2935f217749893b7034e59da3e3568928acevboxsync */
236b2935f217749893b7034e59da3e3568928acevboxsyncDECLCALLBACK(int) gimR3Save(PVM pVM, PSSMHANDLE pSSM)
236b2935f217749893b7034e59da3e3568928acevboxsync{
236b2935f217749893b7034e59da3e3568928acevboxsync /** @todo save state. */
236b2935f217749893b7034e59da3e3568928acevboxsync return VINF_SUCCESS;
236b2935f217749893b7034e59da3e3568928acevboxsync}
236b2935f217749893b7034e59da3e3568928acevboxsync
236b2935f217749893b7034e59da3e3568928acevboxsync
236b2935f217749893b7034e59da3e3568928acevboxsync/**
236b2935f217749893b7034e59da3e3568928acevboxsync * Execute state load operation.
236b2935f217749893b7034e59da3e3568928acevboxsync *
236b2935f217749893b7034e59da3e3568928acevboxsync * @returns VBox status code.
236b2935f217749893b7034e59da3e3568928acevboxsync * @param pVM Pointer to the VM.
236b2935f217749893b7034e59da3e3568928acevboxsync * @param pSSM SSM operation handle.
236b2935f217749893b7034e59da3e3568928acevboxsync * @param uVersion Data layout version.
236b2935f217749893b7034e59da3e3568928acevboxsync * @param uPass The data pass.
236b2935f217749893b7034e59da3e3568928acevboxsync */
236b2935f217749893b7034e59da3e3568928acevboxsyncDECLCALLBACK(int) gimR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
236b2935f217749893b7034e59da3e3568928acevboxsync{
236b2935f217749893b7034e59da3e3568928acevboxsync /** @todo load state. */
236b2935f217749893b7034e59da3e3568928acevboxsync return VINF_SUCCESS;
236b2935f217749893b7034e59da3e3568928acevboxsync}
236b2935f217749893b7034e59da3e3568928acevboxsync
236b2935f217749893b7034e59da3e3568928acevboxsync
236b2935f217749893b7034e59da3e3568928acevboxsync/**
236b2935f217749893b7034e59da3e3568928acevboxsync * Terminates the GIM.
236b2935f217749893b7034e59da3e3568928acevboxsync *
236b2935f217749893b7034e59da3e3568928acevboxsync * Termination means cleaning up and freeing all resources,
236b2935f217749893b7034e59da3e3568928acevboxsync * the VM itself is, at this point, powered off or suspended.
236b2935f217749893b7034e59da3e3568928acevboxsync *
236b2935f217749893b7034e59da3e3568928acevboxsync * @returns VBox status code.
236b2935f217749893b7034e59da3e3568928acevboxsync * @param pVM Pointer to the VM.
236b2935f217749893b7034e59da3e3568928acevboxsync */
236b2935f217749893b7034e59da3e3568928acevboxsyncVMMR3_INT_DECL(int) GIMR3Term(PVM pVM)
236b2935f217749893b7034e59da3e3568928acevboxsync{
236b2935f217749893b7034e59da3e3568928acevboxsync return VINF_SUCCESS;
236b2935f217749893b7034e59da3e3568928acevboxsync}
236b2935f217749893b7034e59da3e3568928acevboxsync