Page 1 of 1

Error code is 32 ,GP.

Posted: Thu Aug 09, 2012 12:14 am
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.

Re: Error code is 32 ,GP.

Posted: Thu Aug 09, 2012 12:38 am
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)

Re: Error code is 32 ,GP.

Posted: Thu Aug 09, 2012 12:54 am
by xenos
And I can imagine why:
ckzippo wrote:the SS selector. Same with kernel data selector

Re: Error code is 32 ,GP.

Posted: Thu Aug 09, 2012 1:06 am
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.

Re: Error code is 32 ,GP.

Posted: Thu Aug 09, 2012 1:06 am
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.

Re: Error code is 32 ,GP.

Posted: Thu Aug 09, 2012 1:31 am
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.

Re: Error code is 32 ,GP.

Posted: Thu Aug 09, 2012 2:08 am
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.

Re: Error code is 32 ,GP.

Posted: Thu Aug 09, 2012 2:38 am
by bluemoon
I would suggest F2 for user data, but that should be OK.

How about paging? is the stack writable by ring3?

Re: Error code is 32 ,GP.

Posted: Thu Aug 09, 2012 3:10 am
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?^_^

Re: Error code is 32 ,GP.

Posted: Thu Aug 09, 2012 3:13 am
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.

Re: Error code is 32 ,GP.

Posted: Thu Aug 09, 2012 3:21 am
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.

Re: Error code is 32 ,GP.

Posted: Thu Aug 09, 2012 3:22 am
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. :)

Re: Error code is 32 ,GP.

Posted: Thu Aug 09, 2012 4:16 am
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?

Re: Error code is 32 ,GP.

Posted: Thu Aug 09, 2012 4:17 am
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?

Re: Error code is 32 ,GP.

Posted: Thu Aug 09, 2012 4:34 am
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?