RDMSR: Segmentation fault

This is a discussion on RDMSR: Segmentation fault within the Linux Programming forums, part of the Platform Specific Boards category; Hi, I was writing some code to read the internal temperature sensor of an Intel Core2 Duo processor which requires ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    15

    RDMSR: Segmentation fault

    Hi,

    I was writing some code to read the internal temperature sensor of an Intel Core2 Duo processor which requires to read some MSRs.
    When I tried to read a MSR with inline assembly code I always ended up getting a "Segmentation fault". Here's the code I used:
    Code:
    #define rdmsr(msr,val1,val2) __asm__ __volatile__("rdmsr" : "=a" (val1), "=d" (val2) : "c" (msr))
    Searching google in order to identify the problem I found out that writing and reading MSRs is only possible for the kernel because it needs to executed from within protection ring 0.

    I understand that writing a MSR shouldn't be possible from userspace, but why does reading a MSR require running in protection ring 0?

    BTW: I am using /dev/cpu/*/msr instead now, but I'm curious to know why reading a MSR is such a problem...

    Peter

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by pgzh View Post
    BTW: I am using /dev/cpu/*/msr instead now, but I'm curious to know why reading a MSR is such a problem...
    MSR means "model specific register." That means Intel does not intend to fully specify the behavior of this instruction at any one point in time. In other words, it's a catch-all where they can implement new stuff that doesn't really fit anywhere else.

    So clearly, this instruction must be limited to ring 0. If it were not, then it would be useless when they want to implement some kind of privileged operation. Why would they tie their own hands like that?

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    15
    This is definitely true for writing to a MSR, but is there any point in making reading one a privileged operation here?

    Peter

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by pgzh View Post
    This is definitely true for writing to a MSR, but is there any point in making reading one a privileged operation here?
    I think the same argument still applies, which is that Intel might want to create a RDMSR instruction which returns some privileged piece of information. We can't know what they might want to do in the future.

    I think it's hokey, too. Intel is struggling with shrinking opcode space in x86.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    15
    Fair enough, you really got a point here.
    So I'll simply stick with the /dev/cpu/*/msr interface which is sufficient for my needs.

    Thanks for the info!

    Peter

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There are several things stored in MSR's that may want some form of protecting [depending on your level of paranoia, of course - we could all be running Multics]. The /proc/.../msr interface allows the OS to filter the accesses, so any "secret" information can be hidden by not forwarding the actual (or any) information.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 02:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 02:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21