[SOLVED]Reboot when initializing GDT

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
rdtsc
Posts: 7
Joined: Fri Oct 29, 2010 2:04 pm
Location: France

[SOLVED]Reboot when initializing GDT

Post by rdtsc »

Hi everyone! :D

First of all, my apologize if my english is not perfect.

I started coding a tiny operating system but when loading the GDT, the computer reboots.

Here is the code where the problem is :

http://x86.pastebin.com/abcgtzqi

I can see that the computer reboots when executing the last assembly instructions but I don't understand why.

Here is a piece of what bochs say me. Could someone help me? :)

Thank you :)
00086610098i[CPU0 ] CPU is in protected mode (active)
00086610098i[CPU0 ] CS.d_b = 32 bit
00086610098i[CPU0 ] SS.d_b = 32 bit
00086610098i[CPU0 ] EFER = 0x00000000
00086610098i[CPU0 ] | RAX=000000000010041c RBX=000000000002bd20
00086610098i[CPU0 ] | RCX=0000000000101490 RDX=0000000000000000
00086610098i[CPU0 ] | RSP=0000000000067e34 RBP=0000000000067e44
00086610098i[CPU0 ] | RSI=000000000002be92 RDI=000000000002be97
00086610098i[CPU0 ] | R8=0000000000000000 R9=0000000000000000
00086610098i[CPU0 ] | R10=0000000000000000 R11=0000000000000000
00086610098i[CPU0 ] | R12=0000000000000000 R13=0000000000000000
00086610098i[CPU0 ] | R14=0000000000000000 R15=0000000000000000
00086610098i[CPU0 ] | IOPL=0 id vip vif ac vm RF nt of df if tf sf zf af pf cf
00086610098i[CPU0 ] | SEG selector base limit G D
00086610098i[CPU0 ] | SEG sltr(index|ti|rpl) base limit G D
00086610098i[CPU0 ] | CS:0008( 0001| 0| 0) 00000000 000fffff 1 1
00086610098i[CPU0 ] | DS:0010( 0002| 0| 0) 00000000 000fffff 1 1
00086610098i[CPU0 ] | SS:0010( 0002| 0| 0) 00000000 000fffff 1 1
00086610098i[CPU0 ] | ES:0010( 0002| 0| 0) 00000000 000fffff 1 1
00086610098i[CPU0 ] | FS:0010( 0002| 0| 0) 00000000 000fffff 1 1
00086610098i[CPU0 ] | GS:0010( 0002| 0| 0) 00000000 000fffff 1 1
00086610098i[CPU0 ] | MSR_FS_BASE:0000000000000000
00086610098i[CPU0 ] | MSR_GS_BASE:0000000000000000
00086610098i[CPU0 ] | RIP=0000000000100260 (0000000000100260)
00086610098i[CPU0 ] | CR0=0x60000011 CR1=0x0 CR2=0x0000000000000000
00086610098i[CPU0 ] | CR3=0x00000000 CR4=0x00000000
00086610098i[CPU0 ] >> movzx eax, byte ptr ds:[eax] : 0FB600
00086610098e[CPU0 ] exception(): 3rd (13) exception with no resolution, shutdown status is 00h, resetting
Last edited by rdtsc on Sat Oct 30, 2010 5:29 am, edited 1 time in total.
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: Reboot when initializing GDT

Post by Combuster »

First thing I notice:
RSP=0000000000067e34 RBP=0000000000067e44
Which looks like you forgot to set up a stack.

Also, you leave room for GCC to optimize away your assembly inlines since they do not include the volatile keyword. Then, last but no least, you set the direction bit for data segments (growing down from 0 instead of up? the segment type should read 1 0010 altogether). The last one is responsible for the crash. Nevertheless, you should fix the other ones too before getting errors that seem to come from nowhere.

Also, based on the formatting of the dump, do you have the latest version of bochs? It looks like you have a 2.3.x or earlier.
"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 ]
rdtsc
Posts: 7
Joined: Fri Oct 29, 2010 2:04 pm
Location: France

Re: Reboot when initializing GDT

Post by rdtsc »

Thank you for your answer :)

I've set up a stack, changed my types and added volatile keyword so as to obtain this :

(btw yes I had an old version so I picked up another one (2.4.5) and recompiled it so as to have a debugger)
(0) Breakpoint 2, 0x00100220 in ?? ()
Next at t=485760063
(0) [0x00100220] 0008:00100220 (unk. ctxt): call .+123 (0x001002a0) ; e87b000000
<bochs:125> c
(0) Breakpoint 3, 0x001001f4 in ?? ()
Next at t=534327029
(0) [0x001001f4] 0008:001001f4 (unk. ctxt): leave ; c9
<bochs:126> c
00534327029e[CPU0 ] read_virtual_checks(): read beyond limit ED
00534327029e[CPU0 ] fetch_raw_descriptor: GDT: index (f007) 1e00 > limit (1f)
00534327029e[CPU0 ] interrupt(): gate descriptor is not valid sys seg (vector=0x08)
00534327029i[CPU0 ] CPU is in protected mode (active)
00534327029i[CPU0 ] CS.d_b = 32 bit
00534327029i[CPU0 ] SS.d_b = 32 bit
00534327029i[CPU0 ] | EAX=00000018 EBX=0002bd20 ECX=001014b8 EDX=0000002e
00534327029i[CPU0 ] | ESP=00020000 EBP=00067e84 ESI=0002be92 EDI=0002be97
00534327029i[CPU0 ] | IOPL=0 id vip vif ac vm RF nt of df if tf sf zf af PF cf
00534327029i[CPU0 ] | SEG selector base limit G D
00534327029i[CPU0 ] | SEG sltr(index|ti|rpl) base limit G D
00534327029i[CPU0 ] | CS:0008( 0001| 0| 0) 00000000 ffffffff 1 1
00534327029i[CPU0 ] | DS:0010( 0002| 0| 0) 00000000 ffffffff 1 1
00534327029i[CPU0 ] | SS:0018( 0003| 0| 0) 00000000 ffffffff 1 1
00534327029i[CPU0 ] | ES:0010( 0002| 0| 0) 00000000 ffffffff 1 1
00534327029i[CPU0 ] | FS:0010( 0002| 0| 0) 00000000 ffffffff 1 1
00534327029i[CPU0 ] | GS:0010( 0002| 0| 0) 00000000 ffffffff 1 1
00534327029i[CPU0 ] | EIP=001001f4 (001001f4)
00534327029i[CPU0 ] | CR0=0x60000011 CR2=0x00000000
00534327029i[CPU0 ] | CR3=0x00000000 CR4=0x00000000
(0).[534327029] [0x001001f4] 0008:001001f4 (unk. ctxt): leave ; c9
00534327029e[CPU0 ] exception(): 3rd (13) exception with no resolution, shutdown status is 00h, resetting
It seems that segment registers contain right values. So does esp. Hence, I guess I can assume that :
-segment descriptors have been correctly loaded
-segment registers including ss and cs have been correctly modified.

So ... what's the problem?

Here is the code of the file dealing with gdt :

http://x86.pastebin.com/FvphFNmS

and the kernel :

http://x86.pastebin.com/7yRbaiRF
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: Reboot when initializing GDT

Post by Combuster »

I wrote:Then, last but no least, you set the direction bit for data segments (growing down from 0 instead of up? the segment type should read 1 0010 altogether)
It helps if you actually fix that. (hint: 151 != 0111b != 0010) :wink:
"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 ]
rdtsc
Posts: 7
Joined: Fri Oct 29, 2010 2:04 pm
Location: France

Re: Reboot when initializing GDT

Post by rdtsc »

:D :D :D :D :D

Thank you very much Combuste, I changed the type of the stack and It works!! :D

*gives a chocolate to Combuster*
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: [SOLVED]Reboot when initializing GDT

Post by Combuster »

No problemom nom nom :D
"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 ]
Post Reply