Processors
Intel® Processors, Tools, and Utilities
15880 Discussions

How can I run "invd" instruction?

Alam__Shariful
New Contributor I
2,941 Views

Hello,

I'm trying to invalidate all the modified cache lines after performing an operation. I know I can run "wbinvd" to invalidate the modified cache line, however, as far I know running "wbinvd" will flush all the modified cache line into memory then it will invalidate the cache line. That is not what I'm trying to achieve. My goal is to remove all the changes without flushing them to the memory. I know that running "invd" instruction will achieve that. However, while I try to run the "invd" instruction I'm getting "general protection fault".  I wrote a simple Linux kernel module just to check if I can run the "invd" instruction, Here is the code, 

 

#include <linux/module.h>     /* Needed by all modules */
#include <linux/kernel.h>     /* Needed for KERN_INFO */
#include <linux/init.h>       /* Needed for the macros */

int new_wbinvd(void){
    asm volatile ("wbinvd" : : : "memory");
    return 0;
}


int new_invd(void){
    asm volatile ("invd" : : : "memory");
    return 0;
}

static int __init hello_start(void)
{
    printk(KERN_INFO "Loading hello module...\n");

    //check if invd instruction executes
    printk(KERN_INFO "running wbinvd\n", new_wbinvd());  
    printk(KERN_INFO "running invd\n", new_invd());

    return 0;
}

static void __exit hello_end(void)
{
    printk(KERN_INFO "Goodbye\n");
}

module_init(hello_start);
module_exit(hello_end);

 

The output from dmesg shows,

[25737.058676] Loading hello module...
[25737.059001] running wbinvd
[25737.059015] general protection fault: 0000 [#1] SMP

Here, to maintain memory consistency I run "wbinvd" first and then run "invd". But no luck. Still getting "general protection error". Any help, what I'm doing wrong? Or how can run "invd" to clear the modified caheline without flushing the changes to the memory? 

Labels (1)
0 Kudos
5 Replies
n_scott_pearson
Super User
2,933 Views

These are privileged instructions. You need to be in a Ring0 context to execute them. In other words, you need to be executing them from within kernel code (most typically from a device driver).

...S

0 Kudos
Alam__Shariful
New Contributor I
2,920 Views

Thank you for your reply. Isn't a kernel module is running in ring 0 too? (please correct me if I'm wrong). 

0 Kudos
n_scott_pearson
Super User
2,906 Views

If it's truly in kernel space, yes, it should be. This error would seem to indicate otherwise. Check with the PTB on the Linux forums for better help.

...S

0 Kudos
Alam__Shariful
New Contributor I
2,876 Views

Mr. Pearson,

Yes, this code is running in kernel space. I was wondering if there is any specific processor feature that needs to be turned off before running this instruction. I notice Protected Mode Exceptions says that executing invd will cause GP(0)  if the processor reserved memory protections are activated. I'm guessing, this might be causing the general protection fault.  Can you please tell me a bit more about the "processor reserved memory protections"? What is it and how to disable it? I'm guessing this is not the Protected Mode represent by bit 0 of the CR0 register.     

0 Kudos
n_scott_pearson
Super User
2,864 Views

To be honest, I am not sure you can. This is part of the Enclave support in Intel Software Guard Extensions (SGX). I have no experience using this capability and my Linux Kernel experience is 10 years stale. You could ask in the Intel Developer Zone or the Linux Support Community or perhaps even in the Intel Open Source Organization.

...S

0 Kudos
Reply