OSDev.org
https://forum.osdev.org/

qemu keeps rebooting on loading GDT
https://forum.osdev.org/viewtopic.php?f=1&t=31403
Page 1 of 1

Author:  dream21 [ Wed Mar 08, 2017 1:55 am ]
Post subject:  qemu keeps rebooting on loading GDT

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?

Author:  kzinti [ Wed Mar 08, 2017 2:11 am ]
Post subject:  Re: qemu keeps rebooting on loading GDT

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?

Author:  alexfru [ Wed Mar 08, 2017 2:22 am ]
Post subject:  Re: qemu keeps rebooting on loading GDT

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

Author:  dream21 [ Wed Mar 08, 2017 3:15 am ]
Post subject:  Re: qemu keeps rebooting on loading GDT

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.

Author:  bluemoon [ Wed Mar 08, 2017 3:40 am ]
Post subject:  Re: qemu keeps rebooting on loading GDT

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?

Author:  dream21 [ Wed Mar 08, 2017 4:21 am ]
Post subject:  Re: qemu keeps rebooting on loading GDT

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 45 times
boot.s [6.29 KiB]
Downloaded 47 times
gdt.c [1.91 KiB]
Downloaded 55 times

Author:  alexfru [ Wed Mar 08, 2017 5:13 am ]
Post subject:  Re: qemu keeps rebooting on loading GDT

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).

Author:  kzinti [ Wed Mar 08, 2017 11:41 am ]
Post subject:  Re: qemu keeps rebooting on loading GDT

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.

Author:  dream21 [ Wed Mar 08, 2017 11:55 am ]
Post subject:  Re: qemu keeps rebooting on loading GDT

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.

Author:  beauhefley [ Wed Mar 08, 2017 9:57 pm ]
Post subject:  Re: qemu keeps rebooting on loading GDT

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.

Author:  dream21 [ Thu Mar 09, 2017 5:20 am ]
Post subject:  Re: qemu keeps rebooting on loading GDT

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.

Author:  fmehmetun [ Wed Jun 21, 2017 4:11 pm ]
Post subject:  Re: qemu keeps rebooting on loading GDT

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

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/