tstRTR0ThreadPreemption.cpp revision d84e3f7154abd56986d6ce98d9156769c7ae6587
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync/* $Id$ */
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync/** @file
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * IPRT R0 Testcase - Thread Preemption.
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync */
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync/*
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * Copyright (C) 2009-2015 Oracle Corporation
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync *
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * available from http://www.virtualbox.org. This file is free software;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * you can redistribute it and/or modify it under the terms of the GNU
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * General Public License (GPL) as published by the Free Software
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync *
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * The contents of this file may alternatively be used under the terms
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * of the Common Development and Distribution License Version 1.0
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * VirtualBox OSE distribution, in which case the provisions of the
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * CDDL are applicable instead of those of the GPL.
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync *
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * You may elect to license modified versions of this file under the
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * terms and conditions of either the GPL or the CDDL or both.
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync */
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync/*******************************************************************************
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync* Header Files *
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync*******************************************************************************/
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync#include <iprt/thread.h>
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync#include <iprt/asm-amd64-x86.h>
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync#include <iprt/err.h>
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync#include <iprt/mem.h>
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync#include <iprt/time.h>
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync#include <iprt/string.h>
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync#include <VBox/sup.h>
bdec40b10acfe9f798021b508a0a7b8c786fc193vboxsync#include "tstRTR0ThreadPreemption.h"
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync
bdec40b10acfe9f798021b508a0a7b8c786fc193vboxsync
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync#define TSTRTR0THREADCTXDATA_MAGIC 0xc01a50da
bdec40b10acfe9f798021b508a0a7b8c786fc193vboxsync
bdec40b10acfe9f798021b508a0a7b8c786fc193vboxsync/**
bdec40b10acfe9f798021b508a0a7b8c786fc193vboxsync * Thread-context hook data.
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync */
8683cfdc9273dd77e1c4e89886a7052de103591avboxsynctypedef struct TSTRTR0THREADCTXDATA
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync{
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync uint32_t volatile u32Magic;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTCPUID uSourceCpuId;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTNATIVETHREAD hSourceThread;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTTHREADCTX hThreadCtx;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync /* For RTTHREADCTXEVENT_PREEMPTING. */
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync bool fPreemptingSuccess;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync volatile bool fPreemptingInvoked;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync /* For RTTHREADCTXEVENT_RESUMED. */
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync bool fResumedSuccess;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync volatile bool fResumedInvoked;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync char achResult[512];
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync} TSTRTR0THREADCTXDATA, *PTSTRTR0THREADCTXDATA;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync/**
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * Thread-context hook function.
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync *
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * @param enmEvent The thread-context event.
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * @param pvUser Pointer to the user argument.
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync */
8683cfdc9273dd77e1c4e89886a7052de103591avboxsyncstatic DECLCALLBACK(void) tstRTR0ThreadCtxHook(RTTHREADCTXEVENT enmEvent, void *pvUser)
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync{
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync PTSTRTR0THREADCTXDATA pData = (PTSTRTR0THREADCTXDATA)pvUser;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync AssertPtrReturnVoid(pData);
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync if (pData->u32Magic != TSTRTR0THREADCTXDATA_MAGIC)
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync {
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pData->achResult, sizeof(pData->achResult), "!tstRTR0ThreadCtxHook: Invalid magic.");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync return;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync }
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync switch (enmEvent)
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync {
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync case RTTHREADCTXEVENT_PREEMPTING:
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync {
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync ASMAtomicWriteBool(&pData->fPreemptingInvoked, true);
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync /* We've already been called once, we now might very well be on another CPU. Nothing to do here. */
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync if (pData->fPreemptingSuccess)
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync return;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync if (RTThreadPreemptIsEnabled(NIL_RTTHREAD))
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync {
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pData->achResult, sizeof(pData->achResult),
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync "!tstRTR0ThreadCtxHook[RTTHREADCTXEVENT_PREEMPTING]: Called with preemption enabled");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync break;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync }
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTNATIVETHREAD hCurrentThread = RTThreadNativeSelf();
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync if (pData->hSourceThread != hCurrentThread)
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync {
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pData->achResult, sizeof(pData->achResult),
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync "!tstRTR0ThreadCtxHook[RTTHREADCTXEVENT_PREEMPTING]: Thread switched! Source=%RTnthrd Current=%RTnthrd.",
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync pData->hSourceThread, hCurrentThread);
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync break;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync }
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTCPUID uCurrentCpuId = RTMpCpuId();
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync if (pData->uSourceCpuId != uCurrentCpuId)
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync {
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pData->achResult, sizeof(pData->achResult),
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync "!tstRTR0ThreadCtxHook[RTTHREADCTXEVENT_PREEMPTING]: migrated uSourceCpuId=%RU32 uCurrentCpuId=%RU32",
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync pData->uSourceCpuId, uCurrentCpuId);
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync break;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync }
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync pData->fPreemptingSuccess = true;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync break;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync }
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync case RTTHREADCTXEVENT_RESUMED:
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync {
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync ASMAtomicWriteBool(&pData->fResumedInvoked, true);
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync /* We've already been called once successfully, nothing more to do. */
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync if (ASMAtomicReadBool(&pData->fResumedSuccess))
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync return;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync if (!pData->fPreemptingSuccess)
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync {
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pData->achResult, sizeof(pData->achResult),
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync "!tstRTR0ThreadCtxHook[RTTHREADCTXEVENT_RESUMED]: Called before preempting callback was invoked.");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync break;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync }
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTNATIVETHREAD hCurrentThread = RTThreadNativeSelf();
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync if (pData->hSourceThread != hCurrentThread)
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync {
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pData->achResult, sizeof(pData->achResult),
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync "!tstRTR0ThreadCtxHook[RTTHREADCTXEVENT_RESUMED]: Thread switched! Source=%RTnthrd Current=%RTnthrd.",
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync pData->hSourceThread, hCurrentThread);
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync break;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync }
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync ASMAtomicWriteBool(&pData->fResumedSuccess, true);
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync break;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync }
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync default:
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync AssertMsgFailed(("Invalid event %#x\n", enmEvent));
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync break;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync }
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync}
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync/**
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * Service request callback function.
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync *
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * @returns VBox status code.
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * @param pSession The caller's session.
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * @param u64Arg 64-bit integer argument.
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * @param pReqHdr The request header. Input / Output. Optional.
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync */
8683cfdc9273dd77e1c4e89886a7052de103591avboxsyncDECLEXPORT(int) TSTRTR0ThreadPreemptionSrvReqHandler(PSUPDRVSESSION pSession, uint32_t uOperation,
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync uint64_t u64Arg, PSUPR0SERVICEREQHDR pReqHdr)
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync{
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync NOREF(pSession);
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync if (u64Arg)
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync return VERR_INVALID_PARAMETER;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync if (!VALID_PTR(pReqHdr))
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync return VERR_INVALID_PARAMETER;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync char *pszErr = (char *)(pReqHdr + 1);
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync size_t cchErr = pReqHdr->cbReq - sizeof(*pReqHdr);
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync if (cchErr < 32 || cchErr >= 0x10000)
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync return VERR_INVALID_PARAMETER;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync *pszErr = '\0';
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync /*
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync * The big switch.
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync */
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync switch (uOperation)
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync {
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync case TSTRTR0THREADPREEMPTION_SANITY_OK:
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync break;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync case TSTRTR0THREADPREEMPTION_SANITY_FAILURE:
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "!42failure42%1024s", "");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync break;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync case TSTRTR0THREADPREEMPTION_BASIC:
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync {
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync if (!ASMIntAreEnabled())
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "!Interrupts disabled");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync else if (!RTThreadPreemptIsEnabled(NIL_RTTHREAD))
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns false by default");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync else
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync {
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTTHREADPREEMPTSTATE State = RTTHREADPREEMPTSTATE_INITIALIZER;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTThreadPreemptDisable(&State);
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync if (RTThreadPreemptIsEnabled(NIL_RTTHREAD))
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after RTThreadPreemptDisable");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync else if (!ASMIntAreEnabled())
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "!Interrupts disabled");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTThreadPreemptRestore(&State);
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync }
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync break;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync }
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync case TSTRTR0THREADPREEMPTION_IS_TRUSTY:
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync if (!RTThreadPreemptIsPendingTrusty())
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "!Untrusty");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync break;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync case TSTRTR0THREADPREEMPTION_IS_PENDING:
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync {
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTTHREADPREEMPTSTATE State = RTTHREADPREEMPTSTATE_INITIALIZER;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTThreadPreemptDisable(&State);
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync if (!RTThreadPreemptIsEnabled(NIL_RTTHREAD))
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync {
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync#ifdef RT_OS_DARWIN
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync uint64_t const cNsMax = UINT64_C(8)*1000U*1000U*1000U;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync#else
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync uint64_t const cNsMax = UINT64_C(2)*1000U*1000U*1000U;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync#endif
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync if (ASMIntAreEnabled())
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync {
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync uint64_t u64StartTS = RTTimeNanoTS();
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync uint64_t u64StartSysTS = RTTimeSystemNanoTS();
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync uint64_t cLoops = 0;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync uint64_t cNanosSysElapsed;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync uint64_t cNanosElapsed;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync bool fPending;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync do
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync {
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync fPending = RTThreadPreemptIsPending(NIL_RTTHREAD);
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync cNanosElapsed = RTTimeNanoTS() - u64StartTS;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync cNanosSysElapsed = RTTimeSystemNanoTS() - u64StartSysTS;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync cLoops++;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync } while ( !fPending
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync && cNanosElapsed < cNsMax
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync && cNanosSysElapsed < cNsMax
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync && cLoops < 100U*_1M);
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync if (!fPending)
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "!Preempt not pending after %'llu loops / %'llu ns / %'llu ns (sys)",
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync cLoops, cNanosElapsed, cNanosSysElapsed);
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync else if (cLoops == 1)
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "!cLoops=1\n");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync else
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "RTThreadPreemptIsPending returned true after %'llu loops / %'llu ns / %'llu ns (sys)",
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync cLoops, cNanosElapsed, cNanosSysElapsed);
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync }
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync else
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "!Interrupts disabled");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync }
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync else
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after RTThreadPreemptDisable");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTThreadPreemptRestore(&State);
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync break;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync }
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync case TSTRTR0THREADPREEMPTION_NESTED:
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync {
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync bool const fDefault = RTThreadPreemptIsEnabled(NIL_RTTHREAD);
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTTHREADPREEMPTSTATE State1 = RTTHREADPREEMPTSTATE_INITIALIZER;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTThreadPreemptDisable(&State1);
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync if (!RTThreadPreemptIsEnabled(NIL_RTTHREAD))
bdec40b10acfe9f798021b508a0a7b8c786fc193vboxsync {
bdec40b10acfe9f798021b508a0a7b8c786fc193vboxsync RTTHREADPREEMPTSTATE State2 = RTTHREADPREEMPTSTATE_INITIALIZER;
bdec40b10acfe9f798021b508a0a7b8c786fc193vboxsync RTThreadPreemptDisable(&State2);
bdec40b10acfe9f798021b508a0a7b8c786fc193vboxsync if (!RTThreadPreemptIsEnabled(NIL_RTTHREAD))
bdec40b10acfe9f798021b508a0a7b8c786fc193vboxsync {
bdec40b10acfe9f798021b508a0a7b8c786fc193vboxsync RTTHREADPREEMPTSTATE State3 = RTTHREADPREEMPTSTATE_INITIALIZER;
bdec40b10acfe9f798021b508a0a7b8c786fc193vboxsync RTThreadPreemptDisable(&State3);
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync if (RTThreadPreemptIsEnabled(NIL_RTTHREAD))
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after 3rd RTThreadPreemptDisable");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTThreadPreemptRestore(&State3);
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync if (RTThreadPreemptIsEnabled(NIL_RTTHREAD) && !*pszErr)
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after 1st RTThreadPreemptRestore");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync }
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync else
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after 2nd RTThreadPreemptDisable");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTThreadPreemptRestore(&State2);
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync if (RTThreadPreemptIsEnabled(NIL_RTTHREAD) && !*pszErr)
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after 2nd RTThreadPreemptRestore");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync }
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync else
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after 1st RTThreadPreemptDisable");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTThreadPreemptRestore(&State1);
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync if (RTThreadPreemptIsEnabled(NIL_RTTHREAD) != fDefault && !*pszErr)
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns false after 3rd RTThreadPreemptRestore");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync break;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync }
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync case TSTRTR0THREADPREEMPTION_CTXHOOKS:
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync {
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync if (!RTThreadPreemptIsEnabled(NIL_RTTHREAD))
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync {
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "!RTThreadCtxHooksCreate must be called with preemption enabled");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync break;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync }
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync bool fRegistered = RTThreadCtxHooksAreRegistered(NIL_RTTHREADCTX);
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync if (fRegistered)
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync {
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "!RTThreadCtxHooksAreRegistered returns true before creating any hooks");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync break;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync }
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTTHREADCTX hThreadCtx;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync int rc = RTThreadCtxHooksCreate(&hThreadCtx);
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync if (RT_FAILURE(rc))
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync {
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync if (rc == VERR_NOT_SUPPORTED)
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "RTThreadCtxHooksCreate returns VERR_NOT_SUPPORTED");
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync else
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync RTStrPrintf(pszErr, cchErr, "!RTThreadCtxHooksCreate returns %Rrc", rc);
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync break;
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync }
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync fRegistered = RTThreadCtxHooksAreRegistered(hThreadCtx);
8683cfdc9273dd77e1c4e89886a7052de103591avboxsync if (fRegistered)
{
RTStrPrintf(pszErr, cchErr, "!RTThreadCtxHooksAreRegistered returns true before registering any hooks");
RTThreadCtxHooksRelease(hThreadCtx);
break;
}
PTSTRTR0THREADCTXDATA pCtxData = (PTSTRTR0THREADCTXDATA)RTMemAllocZ(sizeof(*pCtxData));
AssertReturn(pCtxData, VERR_NO_MEMORY);
pCtxData->u32Magic = TSTRTR0THREADCTXDATA_MAGIC;
pCtxData->hThreadCtx = hThreadCtx;
pCtxData->fPreemptingSuccess = false;
pCtxData->fPreemptingInvoked = false;
pCtxData->fResumedInvoked = false;
pCtxData->fResumedSuccess = false;
pCtxData->hSourceThread = RTThreadNativeSelf();
RT_ZERO(pCtxData->achResult);
RTTHREADPREEMPTSTATE PreemptState = RTTHREADPREEMPTSTATE_INITIALIZER;
RTThreadPreemptDisable(&PreemptState);
Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
pCtxData->uSourceCpuId = RTMpCpuId();
rc = RTThreadCtxHooksRegister(hThreadCtx, &tstRTR0ThreadCtxHook, pCtxData);
if (RT_FAILURE(rc))
{
RTThreadPreemptRestore(&PreemptState);
RTMemFree(pCtxData);
RTStrPrintf(pszErr, cchErr, "!RTThreadCtxHooksRegister returns %Rrc", rc);
break;
}
fRegistered = RTThreadCtxHooksAreRegistered(hThreadCtx);
if (!fRegistered)
{
RTThreadPreemptRestore(&PreemptState);
RTMemFree(pCtxData);
RTStrPrintf(pszErr, cchErr, "!RTThreadCtxHooksAreRegistered return false when hooks are supposedly registered");
break;
}
RTThreadPreemptRestore(&PreemptState);
/* Check if the preempting callback has/will been invoked. */
const uint32_t cMsTimeout = 10000;
const uint32_t cMsSleepGranularity = 50;
uint32_t cMsSlept = 0;
RTCPUID uCurrentCpuId = NIL_RTCPUID;
for (;;)
{
RTThreadYield();
RTThreadPreemptDisable(&PreemptState);
uCurrentCpuId = RTMpCpuId();
RTThreadPreemptRestore(&PreemptState);
if ( pCtxData->uSourceCpuId != uCurrentCpuId
|| cMsSlept >= cMsTimeout)
{
break;
}
RTThreadSleep(cMsSleepGranularity);
cMsSlept += cMsSleepGranularity;
}
if (!ASMAtomicReadBool(&pCtxData->fPreemptingInvoked))
{
if (pCtxData->uSourceCpuId != uCurrentCpuId)
{
RTStrPrintf(pszErr, cchErr,
"!tstRTR0ThreadCtxHooks[RTTHREADCTXEVENT_PREEMPTING] not invoked before migrating from CPU %RU32 to %RU32",
pCtxData->uSourceCpuId, uCurrentCpuId);
}
else
{
RTStrPrintf(pszErr, cchErr, "!tstRTR0ThreadCtxHooks[RTTHREADCTXEVENT_PREEMPTING] not invoked after ca. %u ms",
cMsSlept);
}
}
else if (!pCtxData->fPreemptingSuccess)
RTStrCopy(pszErr, cchErr, pCtxData->achResult);
else
{
/* Preempting callback succeeded, now check if the resumed callback has/will been invoked. */
cMsSlept = 0;
for (;;)
{
if ( ASMAtomicReadBool(&pCtxData->fResumedInvoked)
|| cMsSlept >= cMsTimeout)
{
break;
}
RTThreadSleep(cMsSleepGranularity);
cMsSlept += cMsSleepGranularity;
}
if (!ASMAtomicReadBool(&pCtxData->fResumedInvoked))
{
RTStrPrintf(pszErr, cchErr, "!tstRTR0ThreadCtxHooks[RTTHREADCTXEVENT_RESUMED] not invoked after ca. %u ms",
cMsSlept);
}
else if (!pCtxData->fResumedSuccess)
RTStrCopy(pszErr, cchErr, pCtxData->achResult);
}
RTThreadCtxHooksDeregister(hThreadCtx);
fRegistered = RTThreadCtxHooksAreRegistered(hThreadCtx);
if (fRegistered)
{
RTMemFree(pCtxData);
RTStrPrintf(pszErr, cchErr, "!RTThreadCtxHooksAreRegistered return true when hooks are deregistered");
break;
}
Assert(RTThreadPreemptIsEnabled(NIL_RTTHREAD));
uint32_t cRefs = RTThreadCtxHooksRelease(hThreadCtx);
if (cRefs == UINT32_MAX)
RTStrPrintf(pszErr, cchErr, "!RTThreadCtxHooksRelease returns invalid cRefs!");
RTMemFree(pCtxData);
break;
}
default:
RTStrPrintf(pszErr, cchErr, "!Unknown test #%d", uOperation);
break;
}
/* The error indicator is the '!' in the message buffer. */
return VINF_SUCCESS;
}