OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Apr 19, 2024 8:50 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: MOV DS, AX causes fault [solved]
PostPosted: Tue Apr 24, 2018 10:15 pm 
Offline
Member
Member

Joined: Tue Apr 24, 2018 9:46 pm
Posts: 74
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:
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:
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


Last edited by quadrant on Wed Apr 25, 2018 4:11 pm, edited 3 times in total.

Top
 Profile  
 
 Post subject: Re: MOV DS, AX causes fault - QEMU
PostPosted: Wed Apr 25, 2018 4:08 am 
Offline
Member
Member

Joined: Tue Mar 04, 2014 5:27 am
Posts: 1108
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.


Top
 Profile  
 
 Post subject: Re: MOV DS, AX causes fault - QEMU
PostPosted: Wed Apr 25, 2018 12:50 pm 
Offline
Member
Member

Joined: Tue Apr 24, 2018 9:46 pm
Posts: 74
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.


Top
 Profile  
 
 Post subject: Re: MOV DS, AX causes fault - QEMU
PostPosted: Wed Apr 25, 2018 1:05 pm 
Online
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4594
Location: Chichester, UK
It might help if you gave a link to the repository of your code. Otherwise it's just guesswork.


Top
 Profile  
 
 Post subject: Re: MOV DS, AX causes fault - QEMU
PostPosted: Wed Apr 25, 2018 2:06 pm 
Offline
Member
Member

Joined: Tue Apr 24, 2018 9:46 pm
Posts: 74
I added a link to my code.


Top
 Profile  
 
 Post subject: Re: MOV DS, AX causes fault - QEMU
PostPosted: Wed Apr 25, 2018 2:45 pm 
Online
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4594
Location: Chichester, UK
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.


Top
 Profile  
 
 Post subject: Re: MOV DS, AX causes fault - QEMU
PostPosted: Wed Apr 25, 2018 3:34 pm 
Offline
Member
Member

Joined: Fri Aug 19, 2016 10:28 pm
Posts: 360
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.


Top
 Profile  
 
 Post subject: Re: MOV DS, AX causes fault - QEMU
PostPosted: Wed Apr 25, 2018 3:45 pm 
Offline
Member
Member

Joined: Tue Apr 24, 2018 9:46 pm
Posts: 74
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).


Top
 Profile  
 
 Post subject: Re: MOV DS, AX causes fault - QEMU
PostPosted: Wed Apr 25, 2018 4:01 pm 
Offline
Member
Member

Joined: Tue Apr 24, 2018 9:46 pm
Posts: 74
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.


Top
 Profile  
 
 Post subject: Re: MOV DS, AX causes fault - QEMU
PostPosted: Wed Apr 25, 2018 4:03 pm 
Offline
Member
Member

Joined: Tue Apr 24, 2018 9:46 pm
Posts: 74
Thank you, both things pointed out were the issue. The code now works!


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot], SemrushBot [Bot] and 84 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