Page 1 of 1

MOV DS, AX causes fault [solved]

Posted: Tue Apr 24, 2018 10:15 pm
by quadrant
Hi there!

I am following James Molloy's Kernel tutorial, which is in turn based on Brandon Friesen's Kernel tutorial. Looking at old posts in this forum, it seems these are popular tutorials.

Unlike James, I opted to use QEMU (with GDB) instead of Bochs. Apart from the makefile (where I redirected the kernel elf to QEMU instead of Bochs) my code is essentially identical to his.

However, I cannot get the GDT part of the code/tutorial to work. Specifically, the gdt_flush assembly:

Code: Select all

global gdt_flush

gdt_flush:

	mov  eax, [esp+4]
	lgdt [eax]

	mov  ax, 0x10
	mov  ds, ax   ; Causes a triple fault
	mov  es, ax
	mov  fs, ax
	mov  gs, ax
	mov  ss, ax

	jmp  0x08:.flush

.flush:

	ret
When I stepi in GDB, I can see that all is going as expected until it reaches the

Code: Select all

move ds, ax
instruction. At which point a triple fault is raised.

I've googled around and some of the answers I have seen mention something about this instruction not being allowed in protected mode. What does that mean? Is it the cause of the fault? If it is, what can I do to address it? I suspect it is due to my use of QEMU (and GDB) rather than Bochs as otherwise the tutorial would have mentioned (and addressed) it.

Edit:
Link to my code

Re: MOV DS, AX causes fault - QEMU

Posted: Wed Apr 25, 2018 4:08 am
by alexfru
It means the segment register cannot be loaded with a segment selector.
There are multiple specific reasons, but the most likely ones in your case are:
  • the selector falls outside the table (selector too large or table too small)
  • the descriptor that the selector points to is somehow wrong (e.g. wrong segment type)
So you need to make sure your GDTR and GDT are correct and the selector that you're loading into the segment register makes sense.

Re: MOV DS, AX causes fault - QEMU

Posted: Wed Apr 25, 2018 12:50 pm
by quadrant
If my code is exactly the same as the tutorial's (apart from the makefile), why does it fail for my case but work in the tutorial? I haven't modified any of the values.

Re: MOV DS, AX causes fault - QEMU

Posted: Wed Apr 25, 2018 1:05 pm
by iansjack
It might help if you gave a link to the repository of your code. Otherwise it's just guesswork.

Re: MOV DS, AX causes fault - QEMU

Posted: Wed Apr 25, 2018 2:06 pm
by quadrant
I added a link to my code.

Re: MOV DS, AX causes fault - QEMU

Posted: Wed Apr 25, 2018 2:45 pm
by iansjack
The only thing that strikes me immediately is that you are using a 16-bit pointer for the base address in gdt_entry_struct, when it should be a 32-bit pointer. That's certainly going to cause problems.

If it's not that, you should set a breakpoint just before you load the segment registers. Then you can inspect the GDT in memory to see that it is correct. Also, you can use the qemu monitor to obtain information about the GDT.

Edit: I'm not sure why I said "if it's not that"; your code is loaded above the 64K mark so any pointers must be 32-bit. Using a 16-bit pointer is an absolute show-stopper.

Re: MOV DS, AX causes fault - QEMU

Posted: Wed Apr 25, 2018 3:34 pm
by simeonz
The order of the fields in the gdt entry is incorrect as well. It doesn't match the one in the osdev wiki, and the osdev wiki matches the intel manual, so the source must be wrong.

Re: MOV DS, AX causes fault - QEMU

Posted: Wed Apr 25, 2018 3:45 pm
by quadrant
iansjack wrote:The only thing that strikes me immediately is that you are using a 16-bit pointer for the base address in gdt_entry_struct, when it should be a 32-bit pointer. That's certainly going to cause problems.
Facepalm! #-o Thank you! :)
I guess my code isn't an exact copy (I'll have to comb over it again).

Re: MOV DS, AX causes fault - QEMU

Posted: Wed Apr 25, 2018 4:01 pm
by quadrant
simeonz wrote:The order of the fields in the gdt entry is incorrect as well. It doesn't match the one in the osdev wiki, and the osdev wiki matches the intel manual, so the source must be wrong.
The order matters? 0.0! The tutorial's order follows that in the link you shared. I just changed it for legibility, I didn't think it mattered.

Re: MOV DS, AX causes fault - QEMU

Posted: Wed Apr 25, 2018 4:03 pm
by quadrant
Thank you, both things pointed out were the issue. The code now works!