enabling pmode reboots my system

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
extremecoder
Member
Member
Posts: 59
Joined: Tue May 23, 2006 11:00 pm

enabling pmode reboots my system

Post by extremecoder »

Hi,
I am new to OS development. currently i am into developing my own boot loader. my first set of routines is to switch to pmode. the moment i switch to pmode using CR0, my sys gets rebooted. I am trying this in a VMWare guest with 16MB Ram and a virtual floppy drive. Is it necessary to set up a temp GDT before switching to pmode, because I am planning to set it up in kernel.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: enabling pmode reboots my system

Post by Love4Boobies »

Yes, you need to have a GDT when you jump to protected mode (and not only that). Your system restarts because it triple faults without the appropriate data structures set up. You should really do some research before asking such questions. Take a look at the Intel 64 and IA-32 Architectures Software Developer's Manual for more information.

Also, welcome to OSDev.org and have fun programming your OS.

EDIT: hold on... If you're new, how come you're a member since 2006? :)
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
extremecoder
Member
Member
Posts: 59
Joined: Tue May 23, 2006 11:00 pm

Re: enabling pmode reboots my system

Post by extremecoder »

hey thank you man !! ... I have read the Intel manuals ... and I do understand about the descriptor tables and segment descriptor information ... like segment limit, base address, D/B, G bit, and all those ... I have the hard copy of the Intel manuals and read about GDT ... earlier was I interested only to focus on Kernel, thus by giving the headache to GRUB ... but I am interested in learning nuts and bolts of whatever i code ... so want to develop my own bootloader ... i have started this only 3 weeks back ..
skyking
Member
Member
Posts: 174
Joined: Sun Jan 06, 2008 8:41 am

Re: enabling pmode reboots my system

Post by skyking »

Then it's amazing that you even ask this. If you understand the segment translation mechanism in protected mode, where do you imagine that the processor gets it's descriptor from if no GDT (or LDT) is specified?

Perhaps you imagine that the descriptors have an initial state that's useful? To some extent they do, but you normally reload all segment selectors after entering protected mode so the descriptors are reloaded.

Perhaps you imagine that the GDTR and LDTR are initialized to some useful value, maybe they are, but what about the memory that they point to - is this initialized to a useful state? You won't AFAIK find this stated in the documentation and if the documentation does not state that the initial state to be something particular you should not assume anything particular. You can't assume that GDTR and LDTR are anything particular unless you set them.

Perhaps you imagine that the BIOS sets the GDTR and LDTR to something useful? Well don't. I wouldn't count on the BIOS giving the boot sector control in a very well defined processor state - BIOSes tend to even not putting the specified registers in correct state (cs:ip and dl), why should you rely on other registers then?

Perhaps you imagine that GRUB does some initialization? Yes it does, but the documentation explicitly says that you should set up your own tables - so you better do that.
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Re: enabling pmode reboots my system

Post by jal »

skyking wrote:Perhaps you imagine that GRUB does some initialization? Yes it does, but the documentation explicitly says that you should set up your own tables - so you better do that.
But that's exactly what he did: when Grub boots you, you are in a sane PM environment, and you start setting up your GDT etc. Now the guy writes his own bootloader, and must do what Grub does, but doesn't know what Grub does :).


JAL
User avatar
JAAman
Member
Member
Posts: 879
Joined: Wed Oct 27, 2004 11:00 pm
Location: WA

Re: enabling pmode reboots my system

Post by JAAman »

well, technically you dont need a GDT before entering PMode (just think about it for a moment... the only time the CPU uses the GDT is when the segment registers are changed) as long as you dont change or reload any of the segment registers you technically dont need a GDT... however this really isnt very practical... so really you should have one before entering PMode

however if you are certain that the tripple-fault is happening as you enable PMode (and not later when loading - either explicitly or implicitly - any segment registers) then your problem is probably that you didnt disable interrupts before entering PMode (interrupts must be either disabled or masked otherwise the CPU will receive hard-ints, and try to load the interrupt from the existing IDT (which, unless you changed it, is located at absolute address 0, and has a limit of 256*4-1) -- which will try to jump to the vectors indicated -- and in doing so, implicitly load segment registers (which dont exist if you havent set up the GDT, and if you have, still probably dont match valid segments)

you can set up (and initialize with IDTR) your IDT either before or after you enter PMode, but interrupts must be disabled between those 2 events
User avatar
ehenkes
Member
Member
Posts: 124
Joined: Mon Mar 23, 2009 3:15 am
Location: Germany
Contact:

Re: enabling pmode reboots my system

Post by ehenkes »

Look here:
http://www.henkessoft.de/OS_Dev/OS_Dev1.htm
bootloader -> real mode -> PM -> kernel in C -> video -> keyboard
(tutorial is in German, but source code is commented in English)
source: http://www.henkessoft.de/OS_Dev/Downloads/13%20C.zip
tools: nasm(w), bochs, DJGPP(gcc/ld), partcopy, make/makefile
starts in ASM, switches to C arriving at PM.
extremecoder
Member
Member
Posts: 59
Joined: Tue May 23, 2006 11:00 pm

Re: enabling pmode reboots my system

Post by extremecoder »

hey ehenkes, thank you very much ... the link you have posted was very useful ... but I have another doubt ... which one of the following is recommended:

1) include mode changing in the boot loader
2) include mode changing in the kernel

_ec
Post Reply