5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync/** @file
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync * VirtualBox - VMCPUSET Operation.
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync */
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync/*
e6e8fc31be238a3370495a3e4fb69dbf41cfffb9vboxsync * Copyright (C) 2006-2015 Oracle Corporation
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync *
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync * available from http://www.virtualbox.org. This file is free software;
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync * you can redistribute it and/or modify it under the terms of the GNU
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync * General Public License (GPL) as published by the Free Software
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync *
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync * The contents of this file may alternatively be used under the terms
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync * of the Common Development and Distribution License Version 1.0
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync * VirtualBox OSE distribution, in which case the provisions of the
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync * CDDL are applicable instead of those of the GPL.
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync *
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync * You may elect to license modified versions of this file under the
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync * terms and conditions of either the GPL or the CDDL or both.
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync */
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync#ifndef ___VBox_vmm_vmcpuset_h
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync#define ___VBox_vmm_vmcpuset_h
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync#include <VBox/types.h>
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync#include <iprt/asm.h>
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync#include <iprt/string.h>
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync/** @defgroup grp_vmcpuset VMCPUSET Operations
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync * @ingroup grp_types_both
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync * @sa VMCPUSET
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync * @{
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync */
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync/** Tests if a valid CPU ID is present in the set.. */
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync#define VMCPUSET_IS_PRESENT(pSet, idCpu) ASMBitTest( &(pSet)->au32Bitmap[0], (idCpu))
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync/** Adds a CPU to the set. */
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync#define VMCPUSET_ADD(pSet, idCpu) ASMBitSet( &(pSet)->au32Bitmap[0], (idCpu))
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync/** Deletes a CPU from the set. */
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync#define VMCPUSET_DEL(pSet, idCpu) ASMBitClear(&(pSet)->au32Bitmap[0], (idCpu))
e6e8fc31be238a3370495a3e4fb69dbf41cfffb9vboxsync/** Adds a CPU to the set, atomically. */
e6e8fc31be238a3370495a3e4fb69dbf41cfffb9vboxsync#define VMCPUSET_ATOMIC_ADD(pSet, idCpu) ASMAtomicBitSet( &(pSet)->au32Bitmap[0], (idCpu))
e6e8fc31be238a3370495a3e4fb69dbf41cfffb9vboxsync/** Deletes a CPU from the set, atomically. */
e6e8fc31be238a3370495a3e4fb69dbf41cfffb9vboxsync#define VMCPUSET_ATOMIC_DEL(pSet, idCpu) ASMAtomicBitClear(&(pSet)->au32Bitmap[0], (idCpu))
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync/** Empties the set. */
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync#define VMCPUSET_EMPTY(pSet) memset(&(pSet)->au32Bitmap[0], '\0', sizeof((pSet)->au32Bitmap))
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync/** Fills the set. */
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync#define VMCPUSET_FILL(pSet) memset(&(pSet)->au32Bitmap[0], 0xff, sizeof((pSet)->au32Bitmap))
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync/** Checks if two sets are equal to one another. */
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync#define VMCPUSET_IS_EQUAL(pSet1, pSet2) (memcmp(&(pSet1)->au32Bitmap[0], &(pSet2)->au32Bitmap[0], sizeof((pSet1)->au32Bitmap)) == 0)
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync/** Checks if the set is empty. */
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync#define VMCPUSET_IS_EMPTY(a_pSet) ( (a_pSet)->au32Bitmap[0] == 0 \
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync && (a_pSet)->au32Bitmap[1] == 0 \
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync && (a_pSet)->au32Bitmap[2] == 0 \
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync && (a_pSet)->au32Bitmap[3] == 0 \
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync && (a_pSet)->au32Bitmap[4] == 0 \
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync && (a_pSet)->au32Bitmap[5] == 0 \
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync && (a_pSet)->au32Bitmap[6] == 0 \
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync && (a_pSet)->au32Bitmap[7] == 0 \
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync )
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync/** Finds the first CPU present in the SET.
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync * @returns CPU index if found, NIL_VMCPUID if not. */
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync#define VMCPUSET_FIND_FIRST_PRESENT(a_pSet) VMCpuSetFindFirstPresentInternal(a_pSet)
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync/** Implements VMCPUSET_FIND_FIRST_PRESENT.
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync *
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync * @returns CPU index of the first CPU present in the set, NIL_VMCPUID if none
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync * are present.
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync * @param pSet The set to scan.
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync */
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsyncDECLINLINE(int32_t) VMCpuSetFindFirstPresentInternal(PCVMCPUSET pSet)
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync{
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync int i = ASMBitFirstSet(&pSet->au32Bitmap[0], RT_ELEMENTS(pSet->au32Bitmap) * 32);
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync return i >= 0 ? (VMCPUID)i : NIL_VMCPUID;
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync}
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync/** Finds the first CPU present in the SET.
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync * @returns CPU index if found, NIL_VMCPUID if not. */
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync#define VMCPUSET_FIND_LAST_PRESENT(a_pSet) VMCpuSetFindLastPresentInternal(a_pSet)
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync/** Implements VMCPUSET_FIND_LAST_PRESENT.
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync *
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync * @returns CPU index of the last CPU present in the set, NIL_VMCPUID if none
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync * are present.
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync * @param pSet The set to scan.
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync */
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsyncDECLINLINE(int32_t) VMCpuSetFindLastPresentInternal(PCVMCPUSET pSet)
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync{
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync uint32_t i = RT_ELEMENTS(pSet->au32Bitmap);
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync while (i-- > 0)
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync {
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync uint32_t u = pSet->au32Bitmap[i];
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync if (u)
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync {
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync u = ASMBitLastSetU32(u);
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync u--;
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync u |= i << 5;
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync return u;
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync }
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync }
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync return NIL_VMCPUID;
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync}
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync
c1d279fc0865b91a40b30eda02ed14f6533fe1a4vboxsync/** @} */
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync#endif
5835c306f653aa74c80f79f84a32451fe0ba37c0vboxsync