OSDev.org

The Place to Start for Operating System Developers
It is currently Wed Dec 13, 2017 11:03 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 12 posts ] 
Author Message
 Post subject: qemu keeps rebooting on loading GDT
PostPosted: Wed Mar 08, 2017 1:55 am 
Offline
Member
Member

Joined: Thu Aug 18, 2016 12:54 pm
Posts: 25
I am trying to load GDT but experiencing some problems. The code is from JamesMolly tutorial
Code:
   gdpt.limit = (sizeof(struct gdt_entry)*3)-1;
   gdpt.base = (u32)&gp;

   set_gdt(0, 0, 0, 0, 0);
   set_gdt(1, 0, 0xFFFFFFFF, 0x9A, 0xCF); /* kernel code segment */
   set_gdt(2, 0, 0xFFFFFFFF, 0x92, 0xCF); /* kernel data segment */
   set_gdt(3, 0, 0xFFFFFFFF, 0xFA, 0xCF); /* User mode code segment */
        set_gdt(4, 0, 0xFFFFFFFF, 0xF2, 0xCF); /* User mode data segment */

   gdt_flush((u32)&gdpt);


Attaching GDB to qemu and stepping through the disassembly show that the problem is when ds() is loaded

Code:
0x10021e <gdt_flush>:   mov    eax,DWORD PTR [esp+0x4]
0x100222 <gdt_flush+4>:   lgdtd  [eax]
0x100225 <gdt_flush+7>:   mov    ax,0x10
0x100229 <gdt_flush+11>:   mov    ds,eax
0x10022b <gdt_flush+13>:   mov    es,eax
0x10022d <gdt_flush+15>:   mov    fs,eax
0x10022f <gdt_flush+17>:   mov    gs,eax
0x100231 <gdt_flush+19>:   mov    ss,eax
0x100233 <gdt_flush+21>:   jmp    0x8:0x10023a
0x10023a <flush2>:   retw   


When it steps through that instruction it jumps to weird memory location. Can anybody give me a hint?


Top
 Profile  
 
 Post subject: Re: qemu keeps rebooting on loading GDT
PostPosted: Wed Mar 08, 2017 2:11 am 
Offline
Member
Member

Joined: Mon Feb 02, 2015 7:11 pm
Posts: 228
dream21 wrote:
Code:
0x100225 <gdt_flush+7>:   mov    ax,0x10
0x100229 <gdt_flush+11>:   mov    ds,eax


When it steps through that instruction it jumps to weird memory location. Can anybody give me a hint?


You are setting ax to 0x10, but then using eax to set the segment. Try loading eax with 0x10 instead?

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


Top
 Profile  
 
 Post subject: Re: qemu keeps rebooting on loading GDT
PostPosted: Wed Mar 08, 2017 2:22 am 
Offline
Member
Member

Joined: Tue Mar 04, 2014 5:27 am
Posts: 813
retw looks at best suspicious in the context of 32-bit code.


Top
 Profile  
 
 Post subject: Re: qemu keeps rebooting on loading GDT
PostPosted: Wed Mar 08, 2017 3:15 am 
Offline
Member
Member

Joined: Thu Aug 18, 2016 12:54 pm
Posts: 25
kzinti wrote:
You are setting ax to 0x10, but then using eax to set the segment. Try loading eax with 0x10 instead?


Loading eax would be incorrect for sure.

alexfru wrote:
retw looks at best suspicious in the context of 32-bit code.


I replaced the instruction with ret instruction but no gain.


Top
 Profile  
 
 Post subject: Re: qemu keeps rebooting on loading GDT
PostPosted: Wed Mar 08, 2017 3:40 am 
Offline
Member
Member
User avatar

Joined: Wed Dec 01, 2010 3:41 am
Posts: 1723
Location: Hong Kong
dream21 wrote:
Attaching GDB to qemu and stepping through the disassembly show that the problem is when ds() is loaded...Can anybody give me a hint?


The suspect is set_gdt, which you didn't tell. I suggest to take a dump on the GDT content after lgdt, bochs is handy for this, and there seems some problems in your gdb setup.

As a side note,
Code:
gdpt.limit = (sizeof(struct gdt_entry)*3)-1;

Do you meant 4+1 entries?


Top
 Profile  
 
 Post subject: Re: qemu keeps rebooting on loading GDT
PostPosted: Wed Mar 08, 2017 4:21 am 
Offline
Member
Member

Joined: Thu Aug 18, 2016 12:54 pm
Posts: 25
bluemoon wrote:
Do you meant 4+1 entries?

Ahh sorry! Earlier I was having gdt only for kernel mode, then I added for userspace but forgot to update it. I am attaching the source code here.


Attachments:
kernel.c [279 Bytes]
Downloaded 11 times
boot.s [6.29 KiB]
Downloaded 11 times
gdt.c [1.91 KiB]
Downloaded 17 times
Top
 Profile  
 
 Post subject: Re: qemu keeps rebooting on loading GDT
PostPosted: Wed Mar 08, 2017 5:13 am 
Offline
Member
Member

Joined: Tue Mar 04, 2014 5:27 am
Posts: 813
Check your struct gdt_entry. Is everything in the right order? Or did you somehow make it reverse or something? I don't believe JamesMolly's tutorial had it defined incorrectly. Failed copy'n'paste?

Also you'll need to sort out all the issues with word vs long suffixes. In 32-bit mode your addresses are 32-bit, exception error codes are 32-bit, EFLAGS is 32-bit.

AFAIR, the TSS limit should include the I/O port map. Double check it. And there's probably no point in setting any general-purpose registers or segment registers in it (other than SS0:ESP0).


Top
 Profile  
 
 Post subject: Re: qemu keeps rebooting on loading GDT
PostPosted: Wed Mar 08, 2017 11:41 am 
Offline
Member
Member

Joined: Mon Feb 02, 2015 7:11 pm
Posts: 228
dream21 wrote:
kzinti wrote:
You are setting ax to 0x10, but then using eax to set the segment. Try loading eax with 0x10 instead?


Loading eax would be incorrect for sure.


Can you elaborate? That's what my code does and it works perfectly fine.

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


Top
 Profile  
 
 Post subject: Re: qemu keeps rebooting on loading GDT
PostPosted: Wed Mar 08, 2017 11:55 am 
Offline
Member
Member

Joined: Thu Aug 18, 2016 12:54 pm
Posts: 25
kzinti wrote:
Can you elaborate? That's what my code does and it works perfectly fine.

I figured out now that it was not the cause of the reboot. Those values were setting up correctly instead, the problem was that the TSS was setup incorrectly. I haven't figured out how to setup TSS correctly. If you could give me some hint about it.


Top
 Profile  
 
 Post subject: Re: qemu keeps rebooting on loading GDT
PostPosted: Wed Mar 08, 2017 9:57 pm 
Offline
User avatar

Joined: Mon Feb 20, 2017 1:01 am
Posts: 13
Location: The Moon
I had this same problem. When qemu reboots, it's called a Triple Fault. When the processor does an operation like dividing by zero, it calls an interrupt with the exception's interrupt code. If that fails to execute, it calls a double fault. When that fails to execute, it does the procedure for a triple fault, where the CPU resets.

I had the same problem and posted this on the forum. Make sure your struct's are packed.
http://forum.osdev.org/viewtopic.php?f=1&t=31400 that was my forum post, check it out. Their suggestions might fix your problem.

_________________
Developing an OS that is so early in development, it can't do anything because stupid me can't figure out interrupts
Image


Top
 Profile  
 
 Post subject: Re: qemu keeps rebooting on loading GDT
PostPosted: Thu Mar 09, 2017 5:20 am 
Offline
Member
Member

Joined: Thu Aug 18, 2016 12:54 pm
Posts: 25
beauhefley wrote:
I had the same problem and posted this on the forum. Make sure your struct's are packed.
http://forum.osdev.org/viewtopic.php?f=1&t=31400 that was my forum post, check it out. Their suggestions might fix your problem.


I have attached the source files above and I don't think that there is a problem with the packed structs because structs are properly packed.


Top
 Profile  
 
 Post subject: Re: qemu keeps rebooting on loading GDT
PostPosted: Wed Jun 21, 2017 4:11 pm 
Offline

Joined: Wed Jun 21, 2017 4:07 pm
Posts: 1
Same issue for me. Just solved it. Just sure about your structures are packed.


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 22 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