OSDev.org

The Place to Start for Operating System Developers
It is currently Mon Sep 25, 2017 11:57 pm

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

Joined: Fri Jun 28, 2013 1:48 am
Posts: 22
Location: China
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?

_________________
Reinvent 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: 1041
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: 770
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 | EFI tools


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: Bing [Bot], Google [Bot], Yahoo [Bot] and 17 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