Windows Internals: Processor logic - Part 1#

Windows architecture is the foundation of the Windows operating system. It defines the core components of the operating system, the services and features provided by it, and the way in which those components interact. The architecture also determines the hardware requirements for running Windows, and the processor selection logic is a major part of this.

The processor selection logic is the set of rules used by Windows to determine which processor is suitable for running the operating system. This logic evaluates the hardware capabilities of the processor against the requirements of the operating system and determines whether the processor can support the features and services that Windows provides.

When selecting a processor, Windows looks at a number of factors, such as the processor’s clock speed, number of cores, memory capacity, and instruction set. Additionally, the processor must be compatible with the Windows kernel and the device drivers for the hardware components the system uses.

Windows also considers the power management implementation of the processor. This is important for efficient power usage and battery life, both of which are very important in laptops and other portable devices. The processor selection logic also takes into account the processor’s security features, such as hardware-based malware protection and virtualization technologies.

Finally, the processor selection logic also checks for compatibility with other Windows components, such as the Windows Update service, the Windows Store, and the Windows Defender security suite. By evaluating these factors, Windows is able to ensure that the system is running with the most suitable processor and that the user is receiving the best possible experience.

Selecting the right processor for a Windows system is essential for optimal performance and reliability. By evaluating the processor selection logic, users can make sure that the processor they choose is the best option for their system and that they are getting the most out of Windows.

Practical example in C++#

The following code use WinAPI for processor selection.

// C++ winapi processor selection code example
#include <Windows.h>
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main() {
    // Get the number of logical processors 
    SYSTEM_INFO sysinfo;
    GetSystemInfo(&sysinfo);
    int processorCount = sysinfo.dwNumberOfProcessors;

    // Get the list of processor affinity masks
    vector<DWORD_PTR> processorAffinityMasks;
    for (int i = 0; i < processorCount; i++) {
        HANDLE hProcess = GetCurrentProcess();
        DWORD_PTR processMask;
        DWORD_PTR systemMask;
        GetProcessAffinityMask(hProcess, &processMask, &systemMask);

        DWORD_PTR currentMask = 1;
        for (int j = 0; j < i; j++) {
            currentMask <<= 1;
        }

        if (currentMask & systemMask)
            processorAffinityMasks.push_back(currentMask);
    }

    // Ask the user to select a processor
    cout << "Please select a processor:" << endl;
    for (int i = 0; i < processorCount; i++) {
        cout << i << ": Processor " << i << endl;
    }

    int selection;
    cin >> selection;
    if (selection < 0 || selection >= processorCount) {
        cout << "Invalid selection." << endl;
        return 0;
    }

    // Set the processor affinity
    HANDLE hProcess = GetCurrentProcess();
    if (!SetProcessAffinityMask(hProcess, processorAffinityMasks[selection])) {
        cout << "Failed to set processor affinity." << endl;
        return 0;
    }

    cout << "Processor affinity set to Processor " << selection << "." << endl;
    return 0;
}

Also you can use Powershell commands example for work with processor selection:

$Processors = (Get-WmiObject -Class Win32_Processor).DeviceID

# Get processors with a speed greater than 3.0 GHz
$HighSpeedProcessors = Get-WmiObject -Class Win32_Processor | Where-Object { $_.CurrentClockSpeed -gt 3000 }

# Get processors with a speed less than 3.0 GHz
$LowSpeedProcessors = Get-WmiObject -Class Win32_Processor | Where-Object { $_.CurrentClockSpeed -lt 3000 } 

# List all processors
$AllProcessors = Get-WmiObject -Class Win32_Processor | Select-Object -Property DeviceID, CurrentClockSpeed

# List processors with a speed greater than 3.0 GHz
$HighSpeedProcessors | Select-Object -Property DeviceID, CurrentClockSpeed

# List processors with a speed less than 3.0 GHz
$LowSpeedProcessors | Select-Object -Property DeviceID, CurrentClockSpeed

Windows Processor Group Assignments#

As technology advances and more complex tasks are performed on computers, processors have had to become more powerful. This is especially true in the world of Windows, where processors are used to handle a wide variety of tasks ranging from simple web browsing to complex gaming and 3D modeling.

In order to ensure that Windows users get the most out of their computers, Microsoft has created a processor group assignment system. This system places processors into different groups depending on the type of task they are designed to perform.

For example, the Intel Core i7-9700K processor is designed for high-performance tasks such as gaming and video editing. It is placed in the “Performance” group and is considered one of the most powerful processors available for Windows users.

On the other hand, the Intel Core i3-8100 processor is designed for basic tasks, such as web browsing and office applications. It is placed in the “Basic” group and is considered one of the most affordable processors available for Windows users.

The processor group assignment system helps Windows users make informed decisions when choosing a processor. By knowing the type of tasks they plan to perform, they are able to select the processor that best fits their needs.

Microsoft also provides guidelines for processor group assignment. This helps manufacturers design processors that are optimized for the tasks they are intended to perform. For example, if a processor is intended to be used for gaming, it should be placed in the “Performance” group.

Overall, the Windows processor group assignment system is a helpful tool for Windows users. It helps them make informed decisions about the processors they choose, and it helps manufacturers create processors that are optimized for the tasks they are designed to perform.

The following code example uses the Windows Processor Group Assignment API to assign a processor group to a thread.

using System;
using System.Runtime.InteropServices;

namespace ProcessorGroupExample
{
    class Program
    {
        [DllImport("Kernel32.dll")]
        static extern bool SetThreadGroupAffinity(IntPtr hThread, ref GROUP_AFFINITY GroupAffinity, out GROUP_AFFINITY PreviousGroupAffinity);
        [DllImport("Kernel32.dll")]
        static extern IntPtr GetCurrentThread();
 
        static void Main(string[] args)
        {
            IntPtr handle = GetCurrentThread();
            GROUP_AFFINITY groupAffinity = new GROUP_AFFINITY();
            groupAffinity.Group = 0; // processor group 0
            groupAffinity.Mask = 1; // processor 0 in the group
            GROUP_AFFINITY previousGroupAffinity;
            SetThreadGroupAffinity(handle, ref groupAffinity, out previousGroupAffinity);
        }
    }
 
    [StructLayout(LayoutKind.Sequential)]
    public struct GROUP_AFFINITY
    {
        public ulong Mask;
        public ushort Group;
        public ushort Reserved0;
        public ulong Reserved1;
    }
}

Windows Logical processors per Group#

The number of logical processors per group depends on the type of system. Generally, systems with more than four physical processors will have multiple groups of logical processors. For example, a system with 8 physical processors may have two groups of four logical processors each. Intel processors support up to four logical processors per group, while AMD processors support up to eight. However, this can vary depending on the specific processor model. For example, AMD’s Ryzen Threadripper series processors can support up to 64 logical processors per group.

Windows Logical processor state#

A Logical Processor is a term used to refer to the logical core of a processor. A Logical Processor is the unit of computing that is capable of executing instructions. It is the basic unit of processing power in a computer system. Logical Processors can be either physical cores or threads.

A Logical Processor State is the state of a Logical Processor at any given time. The Logical Processor State can be either running, halted, or halted and waiting. It is important to understand the Logical Processor State in order to properly manage system resources.

When a Logical Processor is running, it is actively executing instructions. When a Logical Processor is halted, it is not executing any instructions. When a Logical Processor is halted and waiting, it is waiting for an event to occur before it can begin executing instructions again.

The Logical Processor State is important to understand because it can affect the performance of a computer system. When a Logical Processor is running, it is using system resources such as memory, power, and CPU cycles. When a Logical Processor is halted, it is not using any system resources and is not contributing to the overall performance of the system.

In Windows, the Logical Processor State is managed through the Windows Task Manager. The Task Manager allows users to view the current Logical Processor State of each Logical Processor in the system. In addition, the Task Manager also allows users to assign tasks to specific Logical Processors, which can be used to improve system performance.

The Logical Processor State is an important concept to understand when managing system resources in Windows. By understanding the Logical Processor State and its effects on system performance, users can optimize their system and ensure that their system runs as efficiently as possible.

Processor Threads#

The birth of a thread marks the beginning of a journey that often results in a successful product. Threads are the foundation of modern computing and are essential for a machine’s ability to multitask.

The process of creating a thread begins with a program, which is essentially a set of instructions that tell a computer what to do. When a program is launched, it is broken up into individual tasks, each with its own thread. The threads are then executed in parallel, allowing multiple tasks to be completed at once.

Threads are essential for multitasking, as they allow the system to run multiple programs at the same time. This allows users to run multiple applications at once and to switch between them quickly and easily.

The creation of a thread is a complex process, involving the coordination of the processor, operating system, and other hardware components. The operating system is responsible for creating the thread, launching it, and scheduling it for execution. The processor then handles the actual execution of the thread, and the hardware components provide the necessary resources.

Threads are also responsible for managing memory, which allows the system to allocate and deallocate memory to threads as needed. This helps keep the system running efficiently and prevents memory leaks.