Page 1 of 1

qemu keeps rebooting on loading GDT

Posted: Wed Mar 08, 2017 1:55 am
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?

Re: qemu keeps rebooting on loading GDT

Posted: Wed Mar 08, 2017 2:11 am
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?

Re: qemu keeps rebooting on loading GDT

Posted: Wed Mar 08, 2017 2:22 am
by alexfru
retw looks at best suspicious in the context of 32-bit code.

Re: qemu keeps rebooting on loading GDT

Posted: Wed Mar 08, 2017 3:15 am
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.

Re: qemu keeps rebooting on loading GDT

Posted: Wed Mar 08, 2017 3:40 am
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?

Re: qemu keeps rebooting on loading GDT

Posted: Wed Mar 08, 2017 4:21 am
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.

Re: qemu keeps rebooting on loading GDT

Posted: Wed Mar 08, 2017 5:13 am
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).

Re: qemu keeps rebooting on loading GDT

Posted: Wed Mar 08, 2017 11:41 am
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.

Re: qemu keeps rebooting on loading GDT

Posted: Wed Mar 08, 2017 11:55 am
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.

Re: qemu keeps rebooting on loading GDT

Posted: Wed Mar 08, 2017 9:57 pm
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.

Re: qemu keeps rebooting on loading GDT

Posted: Thu Mar 09, 2017 5:20 am
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.

Re: qemu keeps rebooting on loading GDT

Posted: Wed Jun 21, 2017 4:11 pm
by fmehmetun
Same issue for me. Just solved it. Just sure about your structures are packed.