OSDev.org

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

All times are UTC - 6 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: A problem when enabling xapic and x2apic
PostPosted: Fri Nov 12, 2021 1:27 am 
Offline

Joined: Fri Nov 12, 2021 1:09 am
Posts: 12
When I use these codes to enable xapic and x2apic:
Code:
__asm__ __volatile__(
      "movq $0x1b,%%rcx   \n\t"
      "rdmsr            \n\t"
      "or $0xc00,%%rax   \n\t"
      "wrmsr         \n\t"
      //"wrmsr   \n\t"
      "movq $0x1b,%%rcx   \n\t"
      "rdmsr            \n\t"
      :"=a"(x),"=d"(y)
      :
      :"memory");

everything goes well at first. But after I remove the annotation mark before the second "wrmsr", it turns me a general protection fault with error code 0xe0.
I'm sure my simulator(Bochs x86 Emulator 2.6) support apic, xapic, and x2apic, and I have no idea why it happens.


Top
 Profile  
 
 Post subject: Re: A problem when enabling xapic and x2apic
PostPosted: Mon Nov 15, 2021 10:38 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
Your inline assembly clobbers RCX without telling the compiler.

But why are you doing all of that in inline assembly? Do you really write a bunch of inline assembly every time you need to read or write a MSR? You could be doing something like this instead:

Code:
inline uint64_t rdmsr( uint32_t msr )
{
    uint32_t low;
    uint32_t high;
    asm volatile ( "rdmsr" : "=a"(low), "=d"(high) : "c"(msr) : "memory" );
    return ((uint64_t)high << 32) | low;
}

inline void wrmsr( uint32_t msr, uint64_t val )
{
    asm volatile ( "wrmsr" :: "a"((uint32_t)val), "d"((uint32_t)(val >> 32)), "c"(msr) : "memory" );
}

uint64_t enable_x2apic( void )
{
    wrmsr( 0x1b, rdmsr( 0x1b ) | 0xc00 );
    return rdmsr( 0x1b );
}


Top
 Profile  
 
 Post subject: Re: A problem when enabling xapic and x2apic
PostPosted: Mon Nov 15, 2021 1:53 pm 
Offline
Member
Member

Joined: Mon Feb 02, 2015 7:11 pm
Posts: 898
Octocontrabass wrote:
Code:
inline uint64_t rdmsr( uint32_t msr )
{
    uint32_t low;
    uint32_t high;
    asm volatile ( "rdmsr" : "=a"(low), "=d"(high) : "c"(msr) : "memory" );
    return ((uint64_t)high << 32) | low;
}


I don't think you need the "memory" clobber on this one... Or are there cases where reading a MSR will change memory?

_________________
https://github.com/kiznit/rainbow-os


Top
 Profile  
 
 Post subject: Re: A problem when enabling xapic and x2apic
PostPosted: Mon Nov 15, 2021 3:01 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
I don't know of any cases where reading a MSR could change memory, but I can think of cases where you might want a compiler memory barrier.

If you're sure you don't need the memory clobber, you can remove it.


Top
 Profile  
 
 Post subject: Re: A problem when enabling xapic and x2apic
PostPosted: Mon Nov 15, 2021 8:09 pm 
Offline
Member
Member

Joined: Mon Feb 02, 2015 7:11 pm
Posts: 898
Octocontrabass wrote:
but I can think of cases where you might want a compiler memory barrier.

Do you have any examples?

_________________
https://github.com/kiznit/rainbow-os


Top
 Profile  
 
 Post subject: Re: A problem when enabling xapic and x2apic
PostPosted: Mon Nov 15, 2021 10:39 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
Reading counters using RDMSR. I don't know why you would want to do that instead of RDTSC/RDPMC, but it's an option.


Top
 Profile  
 
 Post subject: Re: A problem when enabling xapic and x2apic
PostPosted: Mon Nov 15, 2021 11:49 pm 
Offline
Member
Member

Joined: Mon Feb 02, 2015 7:11 pm
Posts: 898
So then it sounds like there is no need for the memory barriers either.

_________________
https://github.com/kiznit/rainbow-os


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 62 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