error code of general protection fault

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
vjain20
Member
Member
Posts: 73
Joined: Wed Apr 04, 2012 9:12 pm

error code of general protection fault

Post by vjain20 »

Hi,

I am following the JamesM tutorial to switch to user mode in my kernel. The tutorial
provides the following function to switch to user mode which uses iret to jump to user mode
by pushing appropriate values on stack.

Code: Select all

void switch_to_user_mode()
{
   // Set up a stack structure for switching to user mode.
   asm volatile("  \ 
     cli; \ 
     mov $0x23, %ax; \ 
     mov %ax, %ds; \ 
     mov %ax, %es; \ 
     mov %ax, %fs; \ 
     mov %ax, %gs; \ 
                   \ 
     mov %esp, %eax; \ 
     pushl $0x23; \ 
     pushl %eax; \ 
     pushf; \ 
     pushl $0x1B; \ 
     push $1f; \ 
     iret; \ 
   1: \ 
     ");
}
[/size]

I call this function directly from my main() function and as expected it fails as I did not make changes to the permissions in page-tables.
It throws a General Protection Exception. However I am unable to understand the error code pushed by the exception which is 0x87F0. According to intel manual
If the fault condition was detected while loading a segment descriptor, the error code contains a segment selector to or IDT
vector number for the descriptor; otherwise, the error code is 0.
But I have only 5 entries in my GDT (indices 0x0, 0x8, 0x10, 0x18, 0x20). Therefore 0x87F0 does not seem to be a valid selector neither does it seem to be a valid interrupt vector number. From the description of intel manual it doesn't seem that garbage error code can be pushed.
Is my understanding wrong somewhere ?
- Thanks
Vaibhav jain
LindusSystem
Member
Member
Posts: 63
Joined: Sat Apr 28, 2012 9:41 am
Location: Earth -> Asia

Re: error code of general protection fault

Post by LindusSystem »

Is this code

mov $0x23, %ax
mov %ax, %ds
mov %ax, %es
mov %ax, %fs
mov %ax, %gs

supposed to be this

mov %ax,$0x23
mov %ds,%ax
mov %es,%ax
mov %fs,%ax
mov %gs,%ax

The mov instruction is written as "mov dest,source" then if you copy the segments into ax ,whats the use?

EDIT:From http://www.brokenthorn.com/Resources/OSDev23.html

Code: Select all

       cli
		mov ax, 0x23	
		mov ds, ax
		mov es, ax
		mov fs, ax
		mov gs, ax

		push 0x23		
		push esp		
		pushfd		
		push 0x1b		
		lea eax, [a]		
		push eax

		iretd
	a:
		add esp, 4
Anyone has a idea of making a ntfs bootsector?if yes PM me , plz.
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: error code of general protection fault

Post by gerryg400 »

LyndusSystem, the order of src and dst depends on the assembler. Vjain is using a different assembler than you.
If a trainstation is where trains stop, what is a workstation ?
LindusSystem
Member
Member
Posts: 63
Joined: Sat Apr 28, 2012 9:41 am
Location: Earth -> Asia

Re: error code of general protection fault

Post by LindusSystem »

K, but he is using inline assembly.
Anyone has a idea of making a ntfs bootsector?if yes PM me , plz.
Nable
Member
Member
Posts: 453
Joined: Tue Nov 08, 2011 11:35 am

Re: error code of general protection fault

Post by Nable »

LindusSystem
inline asembly of GCC is in AT&T syntax by default.

vjain20
what about using bochs debugger?
Also, "If the fault condition was detected while loading a segment descriptor". Are you sure that exception was at this stage?
User avatar
iansjack
Member
Member
Posts: 4711
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: error code of general protection fault

Post by iansjack »

Two thoghts:

1. Are you absolutely sure that it's a GPF, not a PF?

2. Are you sure you are looking at the error code? Are you looking at the right stack?
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: error code of general protection fault

Post by bluemoon »

Have you setup a proper TSS?
once entered ring3 you need at least a minimal TSS(with SS0/ESP0 or 64-bit fields) for exception to work.
vjain20
Member
Member
Posts: 73
Joined: Wed Apr 04, 2012 9:12 pm

Re: error code of general protection fault

Post by vjain20 »

Two thoghts:

1. Are you absolutely sure that it's a GPF, not a PF?

2. Are you sure you are looking at the error code? Are you looking at the right stack?
Yes I am sure that it's a GPF (interrupt 13). I have tested my interrupt handling mechanism.
Also I am sure that I am looking at the error code. This is as per tutorial and I have confirmed by
causing page faults in code which also throw error code.

-Thanks
Vaibhav Jain
- Thanks
Vaibhav jain
User avatar
serviper
Member
Member
Posts: 31
Joined: Sat Jul 16, 2011 6:05 am
Location: China
Contact:

Re: error code of general protection fault

Post by serviper »

Check your IDT entries. Some of them should have gate DPL 3.
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: error code of general protection fault

Post by bluemoon »

You don't need DPL3 for IDT entry unless you use that for your syscall interface(i.e. directly callable from ring3).
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: error code of general protection fault

Post by Combuster »

From the description of intel manual it doesn't seem that garbage error code can be pushed.
Ever heard of the phrase "garbage in, garbage out"? That number is coming from somewhere and I'm pretty sure you're responsible for it :wink:
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
vjain20
Member
Member
Posts: 73
Joined: Wed Apr 04, 2012 9:12 pm

Re: error code of general protection fault

Post by vjain20 »

what about using bochs debugger?
Also, "If the fault condition was detected while loading a segment descriptor". Are you sure that exception was at this stage?
I debugged using the bochs debugger and found that the instruction mov %ax, %ds is faulting and causing a GPF.

Code: Select all

asm volatile("  \ 
     cli; \ 
     mov $0x23, %ax; \     
     mov %ax, %ds; \       \\ <<<<<<<<<<< FAULTING <<<<<<<<<<<<<<<
     mov %ax, %es; \ 
     mov %ax, %fs; \ 
     mov %ax, %gs; \ 
                   \ 
     mov %esp, %eax; \ 
     pushl $0x23; \ 
     pushl %eax; \ 
     pushf; \ 
     pushl $0x1B; \ 
     push $1f; \ 
     iret; \ 
   1: \ 
     ");
[/size]

I don't know why this is. I checked the stack and the error code is indeed 0x87F which I am unable to interpret.
The code for setting descriptors in gdt is same as given in the tutorial.

Thanks
- Thanks
Vaibhav jain
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: error code of general protection fault

Post by Combuster »

A good thing to learn is to stop guessing that things work and actually check it. In this case, check the GDT because that would determine if an exception would be thrown. Bochs has commands for that.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
iansjack
Member
Member
Posts: 4711
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: error code of general protection fault

Post by iansjack »

Well that narrows it down. You are loading a selector into a segment register and getting a GPF. That means that the value you are loading doesn't point to a valid segment descriptor or that you have your privilege levels wrong. As Combuster says, check your GDT.
vjain20
Member
Member
Posts: 73
Joined: Wed Apr 04, 2012 9:12 pm

Re: error code of general protection fault

Post by vjain20 »

I am sorry. It was a stupid mistake which caused this. I wrote 023 in place of 0x23 in the code above
while I pasted the code directly from the tutorial. I figured it out while debugging for some time.
Really stupid of me. :(
- Thanks
Vaibhav jain
Post Reply