4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync/* $Id$ */
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync/** @file
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync * IPRT - RTMpOnPair, generic implementation using RTMpOnAll.
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync */
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync/*
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync * Copyright (C) 2015 Oracle Corporation
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync *
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync * available from http://www.virtualbox.org. This file is free software;
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync * you can redistribute it and/or modify it under the terms of the GNU
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync * General Public License (GPL) as published by the Free Software
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync *
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync * The contents of this file may alternatively be used under the terms
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync * of the Common Development and Distribution License Version 1.0
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync * VirtualBox OSE distribution, in which case the provisions of the
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync * CDDL are applicable instead of those of the GPL.
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync *
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync * You may elect to license modified versions of this file under the
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync * terms and conditions of either the GPL or the CDDL or both.
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync */
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync/*******************************************************************************
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync* Header Files *
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync*******************************************************************************/
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync#include "internal/iprt.h"
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync#include <iprt/mp.h>
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync#include <iprt/asm.h>
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync#include <iprt/assert.h>
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync#include <iprt/err.h>
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync/*******************************************************************************
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync* Structures and Typedefs *
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync*******************************************************************************/
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync/**
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync * Argument package for the generic RTMpOnPair implemenetation.
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync */
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsynctypedef struct RTMPONPAIRGENERIC
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync{
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync RTCPUID idCpu1;
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync RTCPUID idCpu2;
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync PFNRTMPWORKER pfnWorker;
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync void *pvUser1;
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync void *pvUser2;
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync /** Count of how many CPUs actually showed up. */
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync uint32_t volatile cPresent;
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync} RTMPONPAIRGENERIC;
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync/** Pointer to the an argument package for the generic RTMpOnPair
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync * implemenation. */
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsynctypedef RTMPONPAIRGENERIC *PRTMPONPAIRGENERIC;
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync/**
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync * @callback_method_impl{FNRTMPWORKER,
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync * Used by RTMpOnPair to call the worker on the two specified CPUs.}
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync */
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsyncstatic DECLCALLBACK(void) rtMpOnPairGenericWorker(RTCPUID idCpu, void *pvUser1, void *pvUser2)
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync{
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync PRTMPONPAIRGENERIC pArgs = (PRTMPONPAIRGENERIC)pvUser1;
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync /*
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync * Only the two choosen CPUs should call the worker function, count how
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync * many of them that showed up.
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync */
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync if ( idCpu == pArgs->idCpu1
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync || idCpu == pArgs->idCpu2)
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync {
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync ASMAtomicIncU32(&pArgs->cPresent);
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync pArgs->pfnWorker(idCpu, pArgs->pvUser1, pArgs->pvUser2);
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync }
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync}
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsyncRTDECL(int) RTMpOnPair(RTCPUID idCpu1, RTCPUID idCpu2, uint32_t fFlags, PFNRTMPWORKER pfnWorker, void *pvUser1, void *pvUser2)
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync{
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync int rc;
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync AssertReturn(idCpu1 != idCpu2, VERR_INVALID_PARAMETER);
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync AssertReturn(!(fFlags & RTMPON_F_VALID_MASK), VERR_INVALID_FLAGS);
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync if ((fFlags & RTMPON_F_CONCURRENT_EXEC) && !RTMpOnAllIsConcurrentSafe())
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync return VERR_NOT_SUPPORTED;
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync /*
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync * Check that both CPUs are online before doing the broadcast call.
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync */
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync if ( RTMpIsCpuOnline(idCpu1)
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync && RTMpIsCpuOnline(idCpu2))
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync {
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync RTMPONPAIRGENERIC Args;
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync Args.idCpu1 = idCpu1;
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync Args.idCpu2 = idCpu2;
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync Args.pfnWorker = pfnWorker;
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync Args.pvUser1 = pvUser1;
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync Args.pvUser2 = pvUser2;
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync Args.cPresent = 0;
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync rc = RTMpOnAll(rtMpOnPairGenericWorker, &Args, pvUser2);
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync if (RT_SUCCESS(rc))
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync {
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync /*
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync * Let's see if both of the CPUs showed up.
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync */
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync if (RT_LIKELY(Args.cPresent == 2))
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync { /* likely */ }
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync else if (Args.cPresent == 0)
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync rc = VERR_CPU_OFFLINE;
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync else if (Args.cPresent == 1)
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync rc = VERR_NOT_ALL_CPUS_SHOWED;
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync else
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync {
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync rc = VERR_CPU_IPE_1;
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync AssertMsgFailed(("cPresent=%#x\n", Args.cPresent));
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync }
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync }
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync }
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync /*
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync * A CPU must be present to be considered just offline.
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync */
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync else if ( RTMpIsCpuPresent(idCpu1)
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync && RTMpIsCpuPresent(idCpu2))
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync rc = VERR_CPU_OFFLINE;
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync else
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync rc = VERR_CPU_NOT_FOUND;
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync return rc;
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync}
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsyncRTDECL(bool) RTMpOnPairIsConcurrentExecSupported(void)
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync{
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync return RTMpOnAllIsConcurrentSafe();
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync}
4ba3599814cc63307fe48a5d47394ae7c6f391e6vboxsync