Error code is 32 ,GP.

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
ckzippo
Member
Member
Posts: 27
Joined: Wed Jun 06, 2012 4:10 am

Error code is 32 ,GP.

Post by ckzippo »

Hello,everyone, i'm trying to jmp from ring 0 to ring 3.I use :

Code: Select all

void switch_to_user(){
   asm volatile(
     "cli \n\t"             
     "mov $init_stktop, %eax \n\t"
     "pushl $0x23 \n\t"     // the SS selector. Same with user data selector
     "pushl %eax \n\t" 
     "pushf \n\t" 
     "pushl $0x1B \n\t"    //the user code selector
     "pushl $1f \n\t" 
     "iret \n\t" 
     "1: \n\t"
     "jmp  ." 
     );
}
just after iret instruction, GP occurs. I checked the error code is 32.
Anybody know what does this error code mean? I checked Intel Manual, but can not find.
Last edited by ckzippo on Thu Aug 09, 2012 1:04 am, edited 1 time in total.
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Error code is 32 ,GP.

Post by bluemoon »

IRET Reference:
#GP(selector) If a segment selector index is outside its descriptor table limits.
If the return code segment selector RPL is greater than the CPL.
If the DPL of a conforming-code segment is greater than the return code segment selector RPL.
If the DPL for a nonconforming-code segment is not equal to the RPL of the code segment selector.
If the stack segment descriptor DPL is not equal to the RPL of the return code segment selector.
If the stack segment is not a writable data segment.
If the stack segment selector RPL is not equal to the RPL of the return code segment selector.
If the segment descriptor for a code segment does not indicate it is a code segment.
If the segment selector for a TSS has its local/global bit set for local.
If a TSS segment descriptor specifies that the TSS is not busy.
If a TSS segment descriptor specifies that the TSS is not avail- able.
It seems match the SS selector (32)
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: Error code is 32 ,GP.

Post by xenos »

And I can imagine why:
ckzippo wrote:the SS selector. Same with kernel data selector
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
ckzippo
Member
Member
Posts: 27
Joined: Wed Jun 06, 2012 4:10 am

Re: Error code is 32 ,GP.

Post by ckzippo »

bluemoon wrote:
IRET Reference:
#GP(selector) If a segment selector index is outside its descriptor table limits.
If the return code segment selector RPL is greater than the CPL.
If the DPL of a conforming-code segment is greater than the return code segment selector RPL.
If the DPL for a nonconforming-code segment is not equal to the RPL of the code segment selector.
If the stack segment descriptor DPL is not equal to the RPL of the return code segment selector.
If the stack segment is not a writable data segment.
If the stack segment selector RPL is not equal to the RPL of the return code segment selector.
If the segment descriptor for a code segment does not indicate it is a code segment.
If the segment selector for a TSS has its local/global bit set for local.
If a TSS segment descriptor specifies that the TSS is not busy.
If a TSS segment descriptor specifies that the TSS is not avail- able.
yes? could you explain it in detail please, why 32 match SS?thank you.
And im sorry, 0x23 is user data selector.i made a mistake.
Last edited by ckzippo on Thu Aug 09, 2012 1:13 am, edited 1 time in total.
ckzippo
Member
Member
Posts: 27
Joined: Wed Jun 06, 2012 4:10 am

Re: Error code is 32 ,GP.

Post by ckzippo »

XenOS wrote:And I can imagine why:
ckzippo wrote:the SS selector. Same with kernel data selector
i'm sorry.i made a mistake,0x23 is user data selector.
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Error code is 32 ,GP.

Post by bluemoon »

unless by 32 you meant 0x32, it does match 0x20, the selector index of ss after trim the lower ring bits.

And it will be great if you post your GDT tables and paging setup so we don't have to do wild guess.
With my 6th sense it may be:
If the stack segment is not a writable data segment.
ckzippo
Member
Member
Posts: 27
Joined: Wed Jun 06, 2012 4:10 am

Re: Error code is 32 ,GP.

Post by ckzippo »

bluemoon wrote:unless by 32 you meant 0x32, it does match 0x20, the selector index of ss after trim the lower ring bits.

And it will be great if you post your GDT tables so we don't have to do wild guess.
thank for your patience.
the 32 is decimal.
the GDT tables are :

Code: Select all

/* GDT selectors.*/
#define SEL_NULL		0x0
#define SEL_KERNEL_CODE		0x8
#define SEL_KERNEL_DATA		0x10
#define SEL_USER_CODE		0x18
#define SEL_USER_DATA		0x20
and i set GDT as follows:

Code: Select all

        set_segment(&gdt[SEG_KERNEL_CODE], 0,0xFFFFFFFF, 0x9A, 0xCF);// kernel Code segment
	set_segment(&gdt[SEG_KERNEL_DATA], 0,0xFFFFFFFF, 0x92, 0xCF);// kernel Data segment
	set_segment(&gdt[SEG_USER_CODE], 0, 0xFFFFFFFF, 0xFA, 0xCF);// User mode code segment
	set_segment(&gdt[SEG_USER_DATA], 0, 0xFFFFFFFF, 0xFA, 0xCF);// User mode data segment
set_segment is a function,

Code: Select all

set_segment(struct SegmentDescriptor *ptr, uint_32 base, uint_32 limit, uint_8 access, uint_8 gran)
and the 0xFA of user mode data segment means type = 1010(exec and readable), p = 1,DPL = 11.
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Error code is 32 ,GP.

Post by bluemoon »

I would suggest F2 for user data, but that should be OK.

How about paging? is the stack writable by ring3?
ckzippo
Member
Member
Posts: 27
Joined: Wed Jun 06, 2012 4:10 am

Re: Error code is 32 ,GP.

Post by ckzippo »

bluemoon wrote:I would suggest F2 for user data, but that should be OK.

How about paging? is the stack writable by ring3?
i set the page with U/S bit 1,and the page can be accessed in any privilege level.

i have a question here, i only set the page directory for kernel, but i set all the page with U/S bit 1,when i iret to ring 3,i did not change the CR3,
i thought the U/S bit is 1,so ring 3 should be able to access the bit. Is this right?

and what do you mean " is the stack writable by ring3?"
the stack is :

Code: Select all

# the stack for the first user process.
.bss                # i thought whether should i use .data? but the same error.
.globl init_stack,init_stktop
init_stack:
	.space	4096
init_stktop:
the esp points to init_stktop , so,the stack should be writable.anything that sounds not that OK?^_^
Last edited by ckzippo on Thu Aug 09, 2012 3:17 am, edited 1 time in total.
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: Error code is 32 ,GP.

Post by Owen »

ckzippo wrote:and the 0xFA of user mode data segment means type = 1010(exec and readable), p = 1,DPL = 11.
bluemoon wrote: #GP(selector) If the stack segment is not a writable data segment.
ckzippo
Member
Member
Posts: 27
Joined: Wed Jun 06, 2012 4:10 am

Re: Error code is 32 ,GP.

Post by ckzippo »

bluemoon wrote:I would suggest F2 for user data, but that should be OK.

How about paging? is the stack writable by ring3?
thank you very much! question fixed. :D
should be F2. not FA.
Last edited by ckzippo on Thu Aug 09, 2012 3:22 am, edited 1 time in total.
ckzippo
Member
Member
Posts: 27
Joined: Wed Jun 06, 2012 4:10 am

Re: Error code is 32 ,GP.

Post by ckzippo »

Owen wrote:
ckzippo wrote:and the 0xFA of user mode data segment means type = 1010(exec and readable), p = 1,DPL = 11.
bluemoon wrote: #GP(selector) If the stack segment is not a writable data segment.
thank you. :)
ckzippo
Member
Member
Posts: 27
Joined: Wed Jun 06, 2012 4:10 am

Re: Error code is 32 ,GP.

Post by ckzippo »

Owen wrote:
ckzippo wrote:and the 0xFA of user mode data segment means type = 1010(exec and readable), p = 1,DPL = 11.
bluemoon wrote: #GP(selector) If the stack segment is not a writable data segment.
could you please tell me how to analyse error code? any reference?
ckzippo
Member
Member
Posts: 27
Joined: Wed Jun 06, 2012 4:10 am

Re: Error code is 32 ,GP.

Post by ckzippo »

bluemoon wrote:I would suggest F2 for user data, but that should be OK.

How about paging? is the stack writable by ring3?
could you please tell me how to analyse error code? any reference?
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 is 32 ,GP.

Post by Combuster »

ckzippo wrote:could you please tell me how to analyse error code? any reference?
ckzippo wrote:could you please tell me how to analyse error code? any reference?
This obviously qualifies for an RTFM. Where do you think we got the information from?
"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 ]
Post Reply