Tried wiki example to switch to long mode directly from real

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
Roman
Member
Member
Posts: 568
Joined: Thu Mar 27, 2014 3:57 am
Location: Moscow, Russia
Contact:

Tried wiki example to switch to long mode directly from real

Post by Roman »

For me the code seems ok, but it causes reboots.

Code: Select all

   mov ebx, cr0                      ; Activate long mode -
   or ebx,0x80000001                 ; - by enabling paging and protection simultaneously.
   mov cr0, ebx
Last edited by Roman on Fri Apr 04, 2014 2:09 pm, edited 1 time in total.
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
- Alan Kay
CWood
Member
Member
Posts: 127
Joined: Sun Jun 20, 2010 1:21 pm

Re: Tried wiki example to switch to long mode directly from

Post by CWood »

Roman, I really don't want to sound rude or discouraging, and I appreciate you're new to all of this stuff, but you really need to start applying some basic debugging techniques, if nothing more than to narrow down your question when you post on here, to less than your entire source.

An honest tip I can give you, and one that has saved me several times, especially before I've bootstrapped properly into a reasonable environment, is to put the lines

Code: Select all

CLI
HLT
where you think the fault is happening. If the fault no longer happens, the problem is after this point. If it does, the problem is before. Repeat until you find the exact instruction that is faulting, and from there determine possible causes. Check register values, page status, etc.

To reiterate, this is difficult stuff, and I appreciate you're new to it, but many people here don't tolerate newbie questions for very long, and there are some who lose their tempers very quickly, driving people from the community (and I've seen it happen, on more than one occasion). I'd hate to see that happen again.
User avatar
iansjack
Member
Member
Posts: 4711
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Tried wiki example to switch to long mode directly from

Post by iansjack »

I've got to agree with CWood. You desparately need to learn how to debug. I can see some obvious errors just glancing at your code (for example you don't appear to disable interrupts but you don't set up a viable IDT), but it wouldn't really help you to have them pointed out one-by-one. OS development gets a lot harder than this, so take the opportunity now to learn how to track down errors.

As well as the simple debugging techniques already mentioned, consider using a full debugger to step through the code one instruction at a time. If you are using Linux one particularly good tool for doing this is SimNow from AMD. But you could also use gdb in conjunction with qemu or, I believe, Bochs has good debugging facilities. If you are not using Linux, consider doing so. I believe it provides better tools for this purpose than other environments.
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: Tried wiki example to switch to long mode directly from

Post by qw »

To add a little something: posting 274 lines of code and asking what's wrong with it isn't much appreciated either.
User avatar
Roman
Member
Member
Posts: 568
Joined: Thu Mar 27, 2014 3:57 am
Location: Moscow, Russia
Contact:

Re: Tried wiki example to switch to long mode directly from

Post by Roman »

What is it?

Code: Select all

or ebx,0x80000001
Could not find it on google.
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
- Alan Kay
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Tried wiki example to switch to long mode directly from

Post by Combuster »

Oh really? Have you tried looking it up in the intel manuals instead? I'm sure that'll give you an even more correct answer.

Have you also considered what those three truncated lines do as a whole?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
mao
Posts: 6
Joined: Fri Apr 04, 2014 2:35 pm
Location: Sweden

Re: Tried wiki example to switch to long mode directly from

Post by mao »

> For me the code seems ok, but it causes reboots.

Could be that long mode was not entered properly.
The problem in that case is that 64 bit addresses get truncated to 32 bit and execution ends up where you do not expect it after the long jump.

Check that EFER is 0x0000000000000500.

You can do this by printing the reigsters, If you are using Qemu press ALT+CTRL+2.
Then type "print registers".
mao
Posts: 6
Joined: Fri Apr 04, 2014 2:35 pm
Location: Sweden

Re: Tried wiki example to switch to long mode directly from

Post by mao »

What is it?

Code: Select all

or ebx,0x80000001
[/b]
It's bitwise setting 0x80000001 in ebx, but also keeping what ever bits already set in the ebx register.
or meens "this or that" or "keep this and that" in ebx.

Im guessing you are refering to the tutorial lines:

Code: Select all

mov ebx, cr0                   ; Activate long mode -
or ebx,0x80000001              ; - by enabling paging and protection simultaneously.
mov cr0, ebx                    
1. First line copys cr0 to ebx in Intel syntax, in AT&T syntax it would move ebx register content to cr0.
2. Then make shore 0x80000001 bits are set in ebx.
3. Activate the settings by storing ebx in cr0.

cr0 is a CPU control register. 0x80000001 sets enable protected mode (bit 0 to 1) and enable paging (bit 31 to 1).
This adds up to 0x80000001 if you enter it into a hex calculator.

But please do refer to the intel manual and CR0, to understand what these and the other bits do.
CWood
Member
Member
Posts: 127
Joined: Sun Jun 20, 2010 1:21 pm

Re: Tried wiki example to switch to long mode directly from

Post by CWood »

Okay, you've narrowed it down to when you set CR0. That's a good start. Now, you already know that both of these bits set, respectively, paging and protected mode, and it is fairly clear that this code should be correct. So, backtrack a little, and think about what each of these, on their own, rely on. Check each of these bits of code, individually, to check the right values end up in the right place at the right time.
Post Reply