Page 1 of 2

RDMSR always returns 0?

Posted: Sun Jan 18, 2009 4:48 pm
by kllrnohj
I have this function to call rdmsr from C++

Code: Select all

static void read_msr(int msr, int *a, int* d) {
	asm volatile ("rdmsr": "=a" (*a), "=d" (*d): "c" (msr) : );
}

... snip ...

// somewhere in kmain
	int eax = 0, edx = 0;
	read_msr(0x277, &eax, &edx);
The problem is that eax and edx are always 0 after the call (even if I set them to something else - they always get set back to 0). Also, when I pass it an msr that is invalid it doesn't throw a general protection exception like it is supposed to (per Intel documenation). At this point, EAX and EDX *should* be 0x00070406 (the default values for the PAT)

It is being compiled with
g++ -O0 -Wall -c -Wextra -nostdlib -nostartfiles -nodefaultlibs -fno-rtti -fno-builtin -fno-exceptions -fomit-frame-pointer -MMD -MP -MF"src/kernel.d" -MT"src/kernel.d" -o"src/kernel.o" "../src/kernel.cpp"

I am testing this on VMware workstation, and according to CPUID it *does* support MSR.

This is from the Intel Documenation
Reads the contents of a 64-bit model specific register (MSR) specified in the ECX
register into registers EDX:EAX. (On processors that support the Intel 64 architec-
ture, the high-order 32 bits of RCX are ignored.) The EDX register is loaded with the
high-order 32 bits of the MSR and the EAX register is loaded with the low-order 32
bits. (On processors that support the Intel 64 architecture, the high-order 32 bits of
each of RAX and RDX are cleared.) If fewer than 64 bits are implemented in the MSR
being read, the values returned to EDX:EAX in unimplemented bit locations are
undefined.
This instruction must be executed at privilege level 0 or in real-address mode; other-
wise, a general protection exception #GP(0) will be generated. Specifying a reserved
or unimplemented MSR address in ECX will also cause a general protection excep-
tion.

Re: RDMSR always returns 0?

Posted: Sun Jan 18, 2009 5:11 pm
by 01000101
your pointer arithmetic might be messed up, but I don't have enough time right now to check it out further.

try

Code: Select all

uint32_t eax, ecx, edx;
ecx = 0x277;

asm volatile("RDMSR;":"=a"(eax),"=d"(edx):"c"(ecx));
and see if eax/edx are still 0. If that's still the case, try using MSR (ecx = 0x1B), or another well-known MSR.

Re: RDMSR always returns 0?

Posted: Sun Jan 18, 2009 5:25 pm
by kllrnohj
01000101 wrote:your pointer arithmetic might be messed up, but I don't have enough time right now to check it out further.

try

Code: Select all

uint32_t eax, ecx, edx;
ecx = 0x277;

asm volatile("RDMSR;":"=a"(eax),"=d"(edx):"c"(ecx));
and see if eax/edx are still 0. If that's still the case, try using MSR (ecx = 0x1B), or another well-known MSR.
ECX = 0x227 and using your code still returns 0. However, ecx = 0x1B returns edx = 0 and eax = 0xfee00900 (both using your code and mine - which should rule out pointer troubles)

Oh, and I should also mention that the PAT flag *IS* set in the CPUID features, so both MSR and PAT are listed as being supported.

Re: RDMSR always returns 0?

Posted: Sun Jan 18, 2009 5:37 pm
by 01000101
I don't know (haven't checked) what that MSR you are trying to access contains, but is 0 an invalid return value?

Re: RDMSR always returns 0?

Posted: Sun Jan 18, 2009 6:10 pm
by Brendan
Hi,
01000101 wrote:I don't know (haven't checked) what that MSR you are trying to access contains, but is 0 an invalid return value?
In this case 0 would indicate that all pages are "uncacheable" regardless of what you put in the page table entries. The default setting is designed to mimic the behavior of the PCD and PWT flags, which isn't "all pages uncacheable regardless".

However, most emulators don't implement any caching at all (it slows things down), and I wouldn't be surprised if the PAT MSR is ignored anyway - better to see what happens on real hardware.


Cheers,

Brendan

Re: RDMSR always returns 0?

Posted: Sun Jan 18, 2009 6:33 pm
by kllrnohj
Brendan wrote:In this case 0 would indicate that all pages are "uncacheable" regardless of what you put in the page table entries. The default setting is designed to mimic the behavior of the PCD and PWT flags, which isn't "all pages uncacheable regardless".

However, most emulators don't implement any caching at all (it slows things down), and I wouldn't be surprised if the PAT MSR is ignored anyway - better to see what happens on real hardware.
Interesting. Does using MTRRs have any affect in emulators? I was trying to set write combining in the PAT for framebuffer access. I never really planned on using my OS on a real machine, so if there isn't any effect in an emulator then I'll just skip it (for now)

Re: RDMSR always returns 0?

Posted: Sun Jan 18, 2009 6:53 pm
by Brendan
Hi,
kllrnohj wrote:
Brendan wrote:However, most emulators don't implement any caching at all (it slows things down), and I wouldn't be surprised if the PAT MSR is ignored anyway - better to see what happens on real hardware.
Interesting. Does using MTRRs have any affect in emulators? I was trying to set write combining in the PAT for framebuffer access. I never really planned on using my OS on a real machine, so if there isn't any effect in an emulator then I'll just skip it (for now)
For most emulators I doubt MTRRs (and the CD and NW flags in CR0) do anything either (except for validity checks). However different virtual machines are different, and it's not impossible for something like Xen to allow guest caching setup (PAT, MTRRs and CR0 settings) to effect the host's caching setup.

Also note that it's better to use the MTRRs to control caching (where possible). If the page table flags and PAT say that an area is not cacheable then the CPU can't assume that's always the case and still needs to use snoop traffic to ensure cache coherency; but if the MTRRs say an area is not cacheable then no snoop traffic is needed (which is more efficient use of bus bandwidth). Basically, for something like video display memory, if you can use MTRRs then use MTRRs, but if you run out of "variable range MTRRs" then use the PAT (if supported) or nothing (if PAT not supported).


Cheers,

Brendan

Re: RDMSR always returns 0?

Posted: Mon Jan 19, 2009 3:11 am
by djmauretto
Stop with emulator please, there are so many bad OS programmers
because there are emulators.
Emulators are like an inflatable doll,I prefer real women :P

Re: RDMSR always returns 0?

Posted: Mon Jan 19, 2009 3:32 am
by Brendan
Hi,
djmauretto wrote:Stop with emulator please, there are so many bad OS programmers
because there are emulators.
Emulators are like an inflatable doll,I prefer real women :P
Emulators are perfect for fast testing (and much more advanced debugging), and can also be used to increase the variety of "hardware" you can test your code on without costing you extra $$$. Most OS developers test on emulators regularly, and also test on real hardware occasionally.

If you test on real hardware regularly then you should still test on emulators occasionally, because most people that are thinking of trying out an alternative OS will try it on an emulator first, and if it doesn't work on an emulator they'll throw your hard work away and won't bother trying it on any real computer.


Cheers,

Brendan

Re: RDMSR always returns 0?

Posted: Mon Jan 19, 2009 3:35 am
by finarfin
djmauretto wrote:Stop with emulator please, there are so many bad OS programmers
because there are emulators.
Emulators are like an inflatable doll,I prefer real women :P
And also, yeah is true that sometime emulators hidden some bugs.

But these days with bochs i found several problems in my code that REAL pc and Qemu doesn't find.

Re: RDMSR always returns 0?

Posted: Mon Jan 19, 2009 10:43 am
by LoseThos
And also, yeah is true that sometime emulators hidden some bugs.

But these days with bochs i found several problems in my code that REAL pc and Qemu doesn't find.Elen sila lumen omentielvo
finarfin
Member


Posts: 29
Joined: Thu Feb 22, 2007 11:41 pm
Location: Italy
Private messageWebsiteMSNM/WLM
I just got a new computer. More bugs show-up when you try different hardware. #-o Mine only supports very limited hardware -- I tried for least common denominator, but even with that practically every device didn't work! I'm fixing them, but I'm wondering if very few people get my operating system working on their hardware.

Re: RDMSR always returns 0?

Posted: Mon Jan 19, 2009 4:16 pm
by 01000101
LoseThos wrote: I just got a new computer. More bugs show-up when you try different hardware. #-o Mine only supports very limited hardware -- I tried for least common denominator, but even with that practically every device didn't work! I'm fixing them, but I'm wondering if very few people get my operating system working on their hardware.
Oh. my. gawd!
you actually see the error in your logic?

Test on emulators at first, not because you need to be developing *for* the emulator, but because when you run into snags, you can fix them much faster using the built-in debugger and such. Also, emulators do a decent enough job, if it runs well on real hardware it will probably run well in an emulator. It's up to you in the end, if you want to write to floppy and restart a physical machine on every little baby-step, go ahead, but progress with be slow and bug-fixes will take 10 times as long.

Re: RDMSR always returns 0?

Posted: Mon Jan 19, 2009 5:09 pm
by LoseThos
The emulator grabs time and I have load balancing stuff so it works like **** unless it's on real hardware. I compile natively -- I don't have to boot another os. An emulator probably has faster turn-around time, but I can test in like 10-15 seconds or less if it doesn't crash.

Re: RDMSR always returns 0?

Posted: Tue Jan 20, 2009 1:22 am
by Brendan
Hi,
LoseThos wrote:The emulator grabs time and I have load balancing stuff so it works like **** unless it's on real hardware.
On real hardware, SMM grabs CPU time too, and thermal throttling can cause a sudden decrease (or increase) in CPU speed. With the new Core I7 there's also "turbo mode" where the CPU decides it can over-clock a core for an added performance boost. Then there's performance variations caused by hyper-threading (where the same core's resources are shared by multiple logical CPUs and the work done by one logical CPU effects the performance of the other).

If your load balancing stuff doesn't work well on emulators, then chances are it'll only run well on real hardware when you're lucky (which might be most of the time but might not, and definitely won't be all the time; depending on the exact hardware, etc involved). Basically I'd suggest that if your load balancing stuff has problems on emulators, then your load balancing stuff probably has design flaws (or maybe bugs).
LoseThos wrote:I compile natively -- I don't have to boot another os. An emulator probably has faster turn-around time, but I can test in like 10-15 seconds or less if it doesn't crash.
What if it does crash? For example, can you stop your kernel running at a precise spot and single step one instruction at a time while viewing the contents of registers and memory (so you can see exactly what is going on inside the kernel, including inside critical sections)?


Cheers,

Brendan

Re: RDMSR always returns 0?

Posted: Tue Jan 20, 2009 3:17 am
by djmauretto
Brendan you have too much trust in the emulators, even to me
like an emulator that allowed me not to ever restart a PC,
but unfortunately the emulators are all full of bugs,
and you have a virtual hardware, fake, a plastic flower,
unluckily today there is no a decent emulator ,
for now I prefer the real hardware, maybe tomorrow....... :wink:
The time that you occupy to develope un Hobbby Os it's
not important,if you take on 1 week and I 2 week it's not significant,
but if you use real hardware you must have too much fantasy,immagination
you must think more,more,more,and you'll develop more ability.
Think to Albert Einstein, he ever gone in the space,he ever used
a computer, but he imagined the universe ,and developed the theory of relativity :oops: