Windows System Software -- Consulting, Training, Development -- Unique Expertise, Guaranteed Results


Before Posting...

Please check out the Community Guidelines in the Announcements and Administration Category.

More Info on Driver Writing and Debugging

The free OSR Learning Library has more than 50 articles on a wide variety of topics about writing and debugging device drivers and Minifilters. From introductory level to advanced. All the articles have been recently reviewed and updated, and are written using the clear and definitive style you've come to expect from OSR over the years.

Check out The OSR Learning Library at:

disabling apic Interrupts by modify the tpr in the kernel

daniel02daniel02 Member Posts: 16
edited July 2023 in NTDEV

i know there high_level irql to disable all interrupts i want to know if its possible to do so on apic

// I've discovered that the APIC base address must be mapped to virtual memory to ensure proper access

is this approach correct?

#include <intrin.h>
#include <wdm.h>

#define IA32_APIC_BASE_MSR 0x1B
#define APIC_TPR_OFFSET 0x80

uintptr_t cpu_get_apic_base() {
    uint64_t msrValue = __readmsr(IA32_APIC_BASE_MSR);

    return msrValue & 0xFFFFFFFFFFFFF000ULL;
    return msrValue & 0xFFFFF000;

uintptr_t map_physical_to_virtual(uint64_t phys_addr, SIZE_T size) {
    PHYSICAL_ADDRESS PhysicalAddress;
    PhysicalAddress.QuadPart = phys_addr;
    return (uintptr_t)MmMapIoSpace(PhysicalAddress, size, MmNonCached);

void unmap_virtual_address(void* virt_addr) {
    MmUnmapIoSpace(virt_addr, sizeof(uint32_t)); 

static inline uint32_t read_tpr(uintptr_t apicBaseVirt) {
    return *((volatile uint32_t*)(apicBaseVirt + APIC_TPR_OFFSET));

static inline void write_tpr(uintptr_t apicBaseVirt, uint32_t value) {
    *((volatile uint32_t*)(apicBaseVirt + APIC_TPR_OFFSET)) = value;

void DisableAndEnableApicInterrupts() {
    uint64_t apicBasePhys = cpu_get_apic_base();
    uintptr_t apicBaseVirt = map_physical_to_virtual(apicBasePhys, sizeof(uint32_t));
    if (!apicBaseVirt) {
       DbgPrint("Couldn't map physical memory.\n");

    uint32_t original_tpr = read_tpr(apicBaseVirt);

    // Raise the APIC's task priority to effectively mask off all interrupts
    write_tpr(apicBaseVirt, 0xFF);

    // ... Your code that needs to run without external interrupts ...

    // Restore the original TPR value to re-enable interrupts
    write_tpr(apicBaseVirt, original_tpr);


Post edited by daniel02 on


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. Sign in or register to get started.

Upcoming OSR Seminars
OSR has suspended in-person seminars due to the Covid-19 outbreak. But, don't miss your training! Attend via the internet instead!
Kernel Debugging 13-17 May 2024 Live, Online
Developing Minifilters 1-5 Apr 2024 Live, Online
Internals & Software Drivers 11-15 Mar 2024 Live, Online
Writing WDF Drivers 20-24 May 2024 Live, Online