qemu keeps rebooting on loading 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
dream21
Member
Member
Posts: 25
Joined: Thu Aug 18, 2016 12:54 pm

qemu keeps rebooting on loading GDT

Post by dream21 »

I am trying to load GDT but experiencing some problems. The code is from JamesMolly tutorial

Code: Select all

	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: Select all

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?
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: qemu keeps rebooting on loading GDT

Post by kzinti »

dream21 wrote:

Code: Select all

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?
alexfru
Member
Member
Posts: 1112
Joined: Tue Mar 04, 2014 5:27 am

Re: qemu keeps rebooting on loading GDT

Post by alexfru »

retw looks at best suspicious in the context of 32-bit code.
dream21
Member
Member
Posts: 25
Joined: Thu Aug 18, 2016 12:54 pm

Re: qemu keeps rebooting on loading GDT

Post by dream21 »

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.
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: qemu keeps rebooting on loading GDT

Post by bluemoon »

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: Select all

gdpt.limit = (sizeof(struct gdt_entry)*3)-1;
Do you meant 4+1 entries?
dream21
Member
Member
Posts: 25
Joined: Thu Aug 18, 2016 12:54 pm

Re: qemu keeps rebooting on loading GDT

Post by dream21 »

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 63 times

[The extension s has been deactivated and can no longer be displayed.]

gdt.c
(1.91 KiB) Downloaded 120 times
alexfru
Member
Member
Posts: 1112
Joined: Tue Mar 04, 2014 5:27 am

Re: qemu keeps rebooting on loading GDT

Post by alexfru »

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).
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: qemu keeps rebooting on loading GDT

Post by kzinti »

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.
dream21
Member
Member
Posts: 25
Joined: Thu Aug 18, 2016 12:54 pm

Re: qemu keeps rebooting on loading GDT

Post by dream21 »

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.
User avatar
beauhefley
Posts: 13
Joined: Mon Feb 20, 2017 1:01 am
Location: The Moon
Contact:

Re: qemu keeps rebooting on loading GDT

Post by beauhefley »

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
dream21
Member
Member
Posts: 25
Joined: Thu Aug 18, 2016 12:54 pm

Re: qemu keeps rebooting on loading GDT

Post by dream21 »

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.
fmehmetun
Posts: 1
Joined: Wed Jun 21, 2017 4:07 pm

Re: qemu keeps rebooting on loading GDT

Post by fmehmetun »

Same issue for me. Just solved it. Just sure about your structures are packed.
Post Reply