Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8151449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:23:33+00:00 2026-06-06T15:23:33+00:00

I am writing a kernel module and it is about reading and writing MSRs.

  • 0

I am writing a kernel module and it is about reading and writing MSRs. I wrote a simple program for testing but it still fails. All it is doing is writing to MSR, then reading it back. Here is the code:

static int __init test3_init(void)
{
    uint32_t hi,lo;
    hi=0; lo=0xb;
    asm volatile("mov %0,%%eax"::"r"(lo));
    asm volatile("mov %0,%%edx"::"r"(hi));
    asm volatile("mov $0x38d,%ecx");
    asm volatile("wrmsr");
    printk("exit_write: hi=%08x lo=%08x\n",hi,lo);
    asm volatile("mov $0x38d,%ecx");
    asm volatile("rdmsr":"=a"(lo),"=d"(hi));
    printk("exit_write2: hi=%08x lo=%08x\n",hi,lo);
    return 0;
}

The output looks like:

exit_write: hi=00000000 lo=0000000b

exit_write2: hi=00000000 lo=00000000

Can someone tell me why the return value is 0 in the second output, instead of the original? Is there something wrong with my code? Thanks a lot.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T15:23:35+00:00Added an answer on June 6, 2026 at 3:23 pm

    The problem has to do with the fact that you don’t fully tell gcc which registers you’re using in inline assembly and how and you also expect that gcc doesn’t do anything funky to the registers between the fragments of your inline assembly code. Related mov and xxmsr instructions should be in the same asm block.

    Look what gcc does with your code (I’ve altered it a tiny bit to make it compilable as a regular program)…

    Source:

    // file: msr.c
    #include <stdio.h>
    
    typedef unsigned uint32_t;
    #define printk printf
    #define __init
    
    static int __init test3_init(void)
    {
        uint32_t hi,lo;
        hi=0; lo=0xb;
        asm volatile("mov %0,%%eax"::"r"(lo));
        asm volatile("mov %0,%%edx"::"r"(hi));
        asm volatile("mov $0x38d,%ecx");
        asm volatile("wrmsr");
        printk("exit_write: hi=%08x lo=%08x\n",hi,lo);
        asm volatile("mov $0x38d,%ecx");
        asm volatile("rdmsr":"=a"(lo),"=d"(hi));
        printk("exit_write2: hi=%08x lo=%08x\n",hi,lo);
        return 0;
    }
    
    int main(void)
    {
      return test3_init();
    }
    

    Compiling (with MinGW gcc 4.6.2):

    gcc msr.c -c -S -o msr.s
    

    Disassembly of test3_init() from msr.s:

    _test3_init:
            pushl   %ebp
            movl    %esp, %ebp
            pushl   %esi
            pushl   %ebx
            subl    $32, %esp
            movl    $0, -12(%ebp)
            movl    $11, -16(%ebp)
            movl    -16(%ebp), %eax
            mov %eax,%eax
            movl    -12(%ebp), %eax
            mov %eax,%edx
            mov $0x38d,%ecx
            wrmsr
            movl    -16(%ebp), %eax
            movl    %eax, 8(%esp)
            movl    -12(%ebp), %eax
            movl    %eax, 4(%esp)
            movl    $LC0, (%esp)
            call    _printf
            mov $0x38d,%ecx
            rdmsr
            movl    %edx, %ebx
            movl    %eax, %esi
            movl    %esi, -16(%ebp)
            movl    %ebx, -12(%ebp)
            movl    -16(%ebp), %eax
            movl    %eax, 8(%esp)
            movl    -12(%ebp), %eax
            movl    %eax, 4(%esp)
            movl    $LC1, (%esp)
            call    _printf
            movl    $0, %eax
            addl    $32, %esp
            popl    %ebx
            popl    %esi
            popl    %ebp
            ret
    

    Note that when the CPU starts executing wrmsr it has ecx=0x38d (OK), edx=0 (OK), eax=0 (not 0xb, oops!). Follow the instructions to see it.

    What you can and should write instead is something like the following, even shorter than it was:

    static int __init test3_init2(void)
    {
        uint32_t hi,lo;
        hi=0; lo=0xb;
        asm volatile("wrmsr"::"c"(0x38d),"a"(lo),"d"(hi));
        printk("exit_write: hi=%08x lo=%08x\n",hi,lo);
        asm volatile("rdmsr":"=a"(lo),"=d"(hi):"c"(0x38d));
        printk("exit_write2: hi=%08x lo=%08x\n",hi,lo);
        return 0;
    }
    

    Now, disassembly of test3_init2():

    _test3_init2:
            pushl   %ebp
            movl    %esp, %ebp
            pushl   %esi
            pushl   %ebx
            subl    $48, %esp
            movl    $0, -12(%ebp)
            movl    $11, -16(%ebp)
            movl    $909, %ecx
            movl    -16(%ebp), %eax
            movl    -12(%ebp), %edx
            wrmsr
            movl    -16(%ebp), %eax
            movl    %eax, 8(%esp)
            movl    -12(%ebp), %eax
            movl    %eax, 4(%esp)
            movl    $LC0, (%esp)
            call    _printf
            movl    $909, -28(%ebp)
            movl    -28(%ebp), %ecx
            rdmsr
            movl    %edx, %ebx
            movl    %eax, %esi
            movl    %esi, -16(%ebp)
            movl    %ebx, -12(%ebp)
            movl    -16(%ebp), %eax
            movl    %eax, 8(%esp)
            movl    -12(%ebp), %eax
            movl    %eax, 4(%esp)
            movl    $LC1, (%esp)
            call    _printf
            movl    $0, %eax
            addl    $48, %esp
            popl    %ebx
            popl    %esi
            popl    %ebp
            ret
    

    Also, remember that every CPU has its own MSR and you may want to set this MSR on all of them. Another important consideration is that the thread in which you’re manipulating an MSR should not be moved between different CPUs until you’re done with the MSR.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a Linux kernel module. It is released with all the source
I'm writing a kernel module that needs information about the local machine's interfaces just
I am writing a kernel module in which i have 3 source files and
I am writing a kernel module which forms its own packet at ip level
I'm writing a Linux kernel module, and I'd like to allocate an executable page.
I am beginning some experimentation in writing a kernel and having fun doing it.
I'm writing new kernel module and I add implement new IOCTL's. Is there any
I'm writing a linux kernel module that emulates a block device. There are various
I am writing a CUDA kernel for Histogram on a picture, but I had
I am writing an academic project about extremely long functions in the Linux kernel

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.