OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 8:41 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: inline assembly: write MSR
PostPosted: Tue Jan 19, 2016 10:32 am 
Offline
Member
Member

Joined: Fri Jun 28, 2013 1:48 am
Posts: 62
Hi, I use this code as a helper function to write MSR:

Code:
static inline void write_msr(uint32_t msr_id, uint64_t msr_val) {
    __asm__ __volatile__("wrmsr" :: "c"(msr_id), "A"(msr_val));
}


The key is "A" notation, which stands for "edx:eax" according to GCC manual. However, "write_msr" works well under QEMU, but always fail under Bochs.

So I changed the function to this:

Code:
static inline void write_msr(uint32_t msr_id, uint64_t msr_val) {
    uint32_t edx = msr_val >> 32;
    uint32_t eax = msr_val & 0xffffffff;
    __asm__ __volatile__("wrmsr" :: "c"(msr_id), "d"(edx), "a"(eax));
}


And both QEMU and Bochs runs well. But I can't understand the reason.

PS. My OS is 64 bit, does it matters?

_________________
Reinventing the Wheel, code: https://github.com/songziming/wheel


Top
 Profile  
 
 Post subject: Re: inline assembly: write MSR
PostPosted: Tue Jan 19, 2016 10:48 am 
Offline
Member
Member
User avatar

Joined: Thu Aug 11, 2005 11:00 pm
Posts: 1110
Location: Tartu, Estonia
Did you check and compare the generated assembly output?

_________________
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS


Top
 Profile  
 
 Post subject: Re: inline assembly: write MSR
PostPosted: Tue Jan 19, 2016 4:32 pm 
Offline
Member
Member

Joined: Mon Apr 09, 2007 12:10 pm
Posts: 775
Location: London, UK
songziming wrote:
PS. My OS is 64 bit, does it matters?
Yes. On x86_64 gcc will only split a 128 bit value between rax and rdx using the "A" constraint. For the 64-bit value you are using, it is free to pick either rax or rdx. If you compile as 32 bit is should split a 64 bit value to eax:edx.

In other words, doing it with the manual splitting as you are doing is the only way.

I cannot explain why it worked on qemu the first way.

Regards,
John.

_________________
Tysos | rpi-boot


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 66 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group