PVOID WINAPI RtlpAddVectoredHandler(ULONG FirstHandler, PVECTORED_EXCEPTION_HANDLER VectorHandler, ULONG Type) { PVOID Peb = NtCurrentPeb(); PVEH_NODE VehNode = NULL; VehNode = (PVEH_NODE)fnRtlAllocateHeap(*(PVOID*)((PBYTE)Peb + 0x18), // Peb.ProcessHeap 0, // No flags sizeof(VEH_NODE)); // 0x10 bytes if (VehNode == NULL) { return NULL; } VehNode->RefCount = 1; VehNode->Handler = (PVECTORED_EXCEPTION_HANDLER)fnRtlEncodePointer(VectorHandler); fnRtlAcquireSRWLockExclusive(&LdrpVectorHandlerList[Type].Lock); if (IsListEmpty(&LdrpVectorHandlerList[Type].Head)) { InterlockedBitTestAndSet((LONG*)((PBYTE)Peb+0x28), // Peb.EnvironmentUpdateCount, seems not a count... Type + 2); } if (FirstHandler == 0) { InsertHeadList(&LdrpVectorHandlerList[Type].Head, &VehNode->Entry); } else { InsertTailList(&LdrpVectorHandlerList[Type].Head, &VehNode->Entry); } fnRtlReleaseSRWLockExclusive(&LdrpVectorHandlerList[Type].Lock); return VehNode; }
bool VInterlocked::TestAndSet( sLONG *inValue, sLONG inBitNumber) { xbox_assert( inBitNumber >= 0 && inBitNumber <= 31); #if VERSIONMAC // works on byte ((char*)theAddress + (n>>3)) // and operate on bit (0x80>>(n&7)) #if SMALLENDIAN return ::OSAtomicTestAndSetBarrier( inBitNumber ^ 7, inValue); #else return ::OSAtomicTestAndSetBarrier( 31 - inBitNumber, inValue); #endif #elif VERSION_LINUX uLONG mask=0x1<<inBitNumber; sLONG val=__sync_fetch_and_or(inValue, mask); return (val & mask ? true : false); #elif VERSIONWIN // always work on a long return InterlockedBitTestAndSet( (LONG*) inValue, inBitNumber) != 0; #endif }
NTSTATUS NTAPI x86BiosAllocateBuffer( ULONG *Size, USHORT *Segment, USHORT *Offset) { /* Check if the system is initialized and the buffer is large enough */ if (!x86BiosIsInitialized || *Size > PAGE_SIZE) { /* Something was wrong, fail! */ return STATUS_INSUFFICIENT_RESOURCES; } /* Check if the buffer is already allocated */ if (InterlockedBitTestAndSet(&x86BiosBufferIsAllocated, 0)) { /* Buffer was already allocated, fail */ return STATUS_INSUFFICIENT_RESOURCES; } /* The buffer is sufficient, return hardcoded address and size */ *Size = PAGE_SIZE; *Segment = 0x2000; *Offset = 0; return STATUS_SUCCESS;; }
VOID AhciPortBusChangeDpcRoutine( _In_ PSTOR_DPC Dpc, _In_ PVOID AdapterExtension, _In_opt_ PVOID SystemArgument1, _In_opt_ PVOID SystemArgument2 ) /*++ Asks port driver to request a QDR on behalf of the miniport It assumes: nothing Called by: Indirectly by AhciHwInterrupt It performs: 1 Kicks off the Start Channel state machine 2 Requests QDR Affected Variables/Registers: none --*/ { PAHCI_CHANNEL_EXTENSION channelExtension = (PAHCI_CHANNEL_EXTENSION)SystemArgument1; ULONG busChangeInProcess; BOOLEAN portIdle = FALSE; UNREFERENCED_PARAMETER(Dpc); UNREFERENCED_PARAMETER(AdapterExtension); UNREFERENCED_PARAMETER(SystemArgument2); if (channelExtension == NULL) { NT_ASSERT(channelExtension != NULL); return; } busChangeInProcess = InterlockedBitTestAndSet((LONG*)&channelExtension->PoFxPendingWork, 1); //BusChange is at bit 1 if (busChangeInProcess == 1) { // bus change is pending in another process. return; } RecordExecutionHistory(channelExtension, 0x00000030);//AhciPortBusChangeDpcRoutine PortAcquireActiveReference(channelExtension, NULL, &portIdle); // if port is in Active state, continue to process bus change notification. // otherwise, it will be processed when port gets into Active state if (!portIdle) { ULONG busChangePending; busChangePending = InterlockedBitTestAndReset((LONG*)&channelExtension->PoFxPendingWork, 1); //BusChange is at bit 1 if (busChangePending == 1) { PortBusChangeProcess(channelExtension); } } return; }
void GSRasterizerMT::Draw(const GSRasterizerData* data) { if(m_id == 0) { __super::Draw(data); } else { m_data = data; InterlockedBitTestAndSet(m_sync, m_id); } }
VOID BtrAcquireSpinLock( IN PBTR_SPINLOCK Lock ) { ULONG Acquired; ULONG ThreadId; ULONG OwnerId; ULONG Count; ULONG SpinCount; ThreadId = GetCurrentThreadId(); OwnerId = ReadForWriteAccess(&Lock->ThreadId); if (OwnerId == ThreadId) { return; } SpinCount = Lock->SpinCount; while (TRUE) { Acquired = InterlockedBitTestAndSet((volatile LONG *)&Lock->Acquired, 0); if (Acquired != 1) { Lock->ThreadId = ThreadId; break; } Count = 0; do { YieldProcessor(); Acquired = ReadForWriteAccess(&Lock->Acquired); Count += 1; if (Count >= SpinCount) { SwitchToThread(); break; } } while (Acquired != 0); } }
VOID NTAPI INIT_FUNCTION KiSystemStartup(IN PLOADER_PARAMETER_BLOCK LoaderBlock) { ULONG Cpu; PKTHREAD InitialThread; ULONG InitialStack; PKGDTENTRY Gdt; PKIDTENTRY Idt; KIDTENTRY NmiEntry, DoubleFaultEntry; PKTSS Tss; PKIPCR Pcr; /* Boot cycles timestamp */ BootCycles = __rdtsc(); /* Save the loader block and get the current CPU */ KeLoaderBlock = LoaderBlock; Cpu = KeNumberProcessors; if (!Cpu) { /* If this is the boot CPU, set FS and the CPU Number*/ Ke386SetFs(KGDT_R0_PCR); __writefsdword(KPCR_PROCESSOR_NUMBER, Cpu); /* Set the initial stack and idle thread as well */ LoaderBlock->KernelStack = (ULONG_PTR)P0BootStack; LoaderBlock->Thread = (ULONG_PTR)&KiInitialThread; } /* Save the initial thread and stack */ InitialStack = LoaderBlock->KernelStack; InitialThread = (PKTHREAD)LoaderBlock->Thread; /* Clean the APC List Head */ InitializeListHead(&InitialThread->ApcState.ApcListHead[KernelMode]); /* Initialize the machine type */ KiInitializeMachineType(); /* Skip initial setup if this isn't the Boot CPU */ if (Cpu) goto AppCpuInit; /* Get GDT, IDT, PCR and TSS pointers */ KiGetMachineBootPointers(&Gdt, &Idt, &Pcr, &Tss); /* Setup the TSS descriptors and entries */ Ki386InitializeTss(Tss, Idt, Gdt); /* Initialize the PCR */ RtlZeroMemory(Pcr, PAGE_SIZE); KiInitializePcr(Cpu, Pcr, Idt, Gdt, Tss, InitialThread, (PVOID)KiDoubleFaultStack); /* Set us as the current process */ InitialThread->ApcState.Process = &KiInitialProcess.Pcb; /* Clear DR6/7 to cleanup bootloader debugging */ __writefsdword(KPCR_TEB, 0); __writefsdword(KPCR_DR6, 0); __writefsdword(KPCR_DR7, 0); /* Setup the IDT */ KeInitExceptions(); /* Load Ring 3 selectors for DS/ES */ Ke386SetDs(KGDT_R3_DATA | RPL_MASK); Ke386SetEs(KGDT_R3_DATA | RPL_MASK); /* Save NMI and double fault traps */ RtlCopyMemory(&NmiEntry, &Idt[2], sizeof(KIDTENTRY)); RtlCopyMemory(&DoubleFaultEntry, &Idt[8], sizeof(KIDTENTRY)); /* Copy kernel's trap handlers */ RtlCopyMemory(Idt, (PVOID)KiIdtDescriptor.Base, KiIdtDescriptor.Limit + 1); /* Restore NMI and double fault */ RtlCopyMemory(&Idt[2], &NmiEntry, sizeof(KIDTENTRY)); RtlCopyMemory(&Idt[8], &DoubleFaultEntry, sizeof(KIDTENTRY)); AppCpuInit: /* Loop until we can release the freeze lock */ do { /* Loop until execution can continue */ while (*(volatile PKSPIN_LOCK*)&KiFreezeExecutionLock == (PVOID)1); } while(InterlockedBitTestAndSet((PLONG)&KiFreezeExecutionLock, 0)); /* Setup CPU-related fields */ __writefsdword(KPCR_NUMBER, Cpu); __writefsdword(KPCR_SET_MEMBER, 1 << Cpu); __writefsdword(KPCR_SET_MEMBER_COPY, 1 << Cpu); __writefsdword(KPCR_PRCB_SET_MEMBER, 1 << Cpu); /* Initialize the Processor with HAL */ HalInitializeProcessor(Cpu, KeLoaderBlock); /* Set active processors */ KeActiveProcessors |= __readfsdword(KPCR_SET_MEMBER); KeNumberProcessors++; /* Check if this is the boot CPU */ if (!Cpu) { /* Initialize debugging system */ KdInitSystem(0, KeLoaderBlock); /* Check for break-in */ if (KdPollBreakIn()) DbgBreakPointWithStatus(DBG_STATUS_CONTROL_C); } /* Raise to HIGH_LEVEL */ KfRaiseIrql(HIGH_LEVEL); /* Switch to new kernel stack and start kernel bootstrapping */ KiSwitchToBootStack(InitialStack & ~3); }