block all processors but keep processor 0

daniel02x 26 Reputation points
2023-05-15T18:59:16.5+00:00

based on this code its block all processors

i want modify it so its block all processors but keep processor 0 how i can do it ?


static u32 locked;
static u32 inp_dpcs;
static u32 out_dpcs;
static u32 dpc_refs;

static void global_lock_proc(PKDPC dpc, PKDPC *cpu_dpc, void *arg1, void *arg2)
{
	_disable();
	lock_dec(&inp_dpcs);
	
	while (locked != 0) 
		KeStallExecutionProcessor(1);	
	
	_enable();
	
	if (lock_dec(&dpc_refs) == 0)
		ExFreePool(cpu_dpc);
	
	lock_dec(&out_dpcs);
}


KIRQL global_lock()
{
	KIRQL old_irql;
	PKDPC cpu_dpc;
	CCHAR n_cpu, i;
	
	n_cpu    = KeNumberProcessors;
	cpu_dpc  = ExAllocatePool(NonPagedPoolMustSucceed, sizeof(KDPC) * n_cpu);
	old_irql = KeRaiseIrqlToDpcLevel();
	locked   = 1;
	inp_dpcs = n_cpu - 1;
	out_dpcs = inp_dpcs;
	dpc_refs = inp_dpcs;
	
	for (i = 0; i < KeNumberProcessors; i++) 
		if (i != KeGetCurrentProcessorNumber()) {
			KeInitializeDpc(&cpu_dpc[i], global_lock_proc, cpu_dpc);
			KeSetTargetProcessorDpc(&cpu_dpc[i], i);
			KeSetImportanceDpc(&cpu_dpc[i], HighImportance);
			KeInsertQueueDpc(&cpu_dpc[i], NULL, NULL);
		}
	
	while (inp_dpcs != 0) 
		KeStallExecutionProcessor(1); 

	_disable();
	
	return old_irql;
}


void global_unlock(KIRQL old_irql)
{
	locked = 0;
	
	while (out_dpcs != 0) 
		KeStallExecutionProcessor(1); 

	_enable();
	
	KeLowerIrql(old_irql);
}

will this work ?

{
    KIRQL old_irql;
    PKDPC cpu_dpc;
    CCHAR n_cpu, i;

    // Get the original IRQL
    old_irql = KeGetCurrentIrql();

    // This function should only be run on processor 0
    if (KeGetCurrentProcessorNumber() != 0) {
        // Return original IRQL if not on processor 0
        return old_irql;
    }

    n_cpu = KeNumberProcessors;
    cpu_dpc = (PKDPC)ExAllocatePool(NonPagedPoolMustSucceed, sizeof(KDPC) * n_cpu);
    old_irql = KeRaiseIrqlToDpcLevel();
    locked = 1;
    inp_dpcs = n_cpu - 1;
    out_dpcs = inp_dpcs;
    dpc_refs = inp_dpcs;

    for (i = 0; i < KeNumberProcessors; i++)
        if (i != KeGetCurrentProcessorNumber()) {
            KeInitializeDpc(&cpu_dpc[i], (PKDEFERRED_ROUTINE)global_lock_proc, cpu_dpc);
            KeSetTargetProcessorDpc(&cpu_dpc[i], i);
            KeSetImportanceDpc(&cpu_dpc[i], HighImportance);
            KeInsertQueueDpc(&cpu_dpc[i], NULL, NULL);
        }

    while (inp_dpcs != 0)
        KeStallExecutionProcessor(1);

    _disable();

    return old_irql;
}```
Windows 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
11,057 questions
Windows 10 Hardware Performance
Windows 10 Hardware Performance
Windows 10: A Microsoft operating system that runs on personal computers and tablets.Hardware Performance: Delivering / providing hardware or hardware systems or adjusting / adapting hardware or hardware systems.
101 questions
Windows Hardware Performance
Windows Hardware Performance
Windows: A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.Hardware Performance: Delivering / providing hardware or hardware systems or adjusting / adapting hardware or hardware systems.
1,570 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Limitless Technology 44,091 Reputation points
    2023-05-18T13:42:00.4666667+00:00

    Hello there,

    You could write a Governor class . This class would contain a utility method that should be called on a regular basis (e.g. calling this utility function within a while loop of your function) by your CPU bound function.

    This thread might help you in getting from insights

    https://learn.microsoft.com/en-us/dotnet/api/system.environment.processorcount?view=net-7.0

    Hope this resolves your Query !!

    --If the reply is helpful, please Upvote and Accept it as an answer--

    0 comments No comments