[SOLVED]Ring3 Prob: Int XX, Solar's malloc & Durand printf
Posted: Sat Nov 05, 2011 2:47 am
Hi,
I have been trying to switch to ring 3. I can switch to it but I cannot execute Durand's port of printf or Solar's malloc from ring 3. From ring 0 both works fine . But I can execute my own version of puts(which hooks to Durand's printf). When the above are executed from ring 3, the qemu "info registers" displays cs and ss as changed to ring 0 descriptors after GPF and all other segment registers as Ring 3 data descriptors i,e no change occurs .
The Page Directory and Page Table of the whole kernel has been marked user/rw/present just for testing purposes. Also I cannot switch back to ring 0 by "int 0" with jmp $ inside IRQ0_ISR_Handler but instead a GPF occurs placing cs and ss in ring 0 and all else in ring 3.
Asm Code
---------------
C Code
-----------
Compiler: GCC Cross Compiler i586-elf
Virtual Machine: qemu
develpment machine: x86. OS: lubuntu 11.04
I have been trying to switch to ring 3. I can switch to it but I cannot execute Durand's port of printf or Solar's malloc from ring 3. From ring 0 both works fine . But I can execute my own version of puts(which hooks to Durand's printf). When the above are executed from ring 3, the qemu "info registers" displays cs and ss as changed to ring 0 descriptors after GPF and all other segment registers as Ring 3 data descriptors i,e no change occurs .
The Page Directory and Page Table of the whole kernel has been marked user/rw/present just for testing purposes. Also I cannot switch back to ring 0 by "int 0" with jmp $ inside IRQ0_ISR_Handler but instead a GPF occurs placing cs and ss in ring 0 and all else in ring 3.
Asm Code
---------------
Code: Select all
Mov Eax , TASK_STATE_SEGMENT
Mov [First] , Al
Mov [Second] , Ah
Shr Eax , 16
Mov [Third] , Al
Mov [Last] , Ah
Lgdt [GDTR]
Jmp dword 8: ReLoad_Protected ; Reloading GDTR
ReLoad_Protected :
Mov Ax , 0x10 ; Data Segment Initialize
Mov Ds , Ax
Mov Es , Ax
Mov Fs , Ax
Mov Gs , Ax
Mov Ss , Ax ; Stack Segment
Mov Ax , 0x2B ; TSS Descriptor
Ltr Ax ; Load TSS
Call Main ; Call C routines
Hang: Hlt
Jmp Hang
; GDT WITH TSS SUPPORT
GDTR :
dw GDT_End - GDT - 1 ; 16 Bit Size Limit Of GDT
dd GDT ; 32 Bit Linear Address Of GDT
GDT :
dd 0x00000000 , 0x00000000 ; Null
dd 0x0000FFFF , 0x00CF9A00 ; Kernel Code
dd 0x0000FFFF , 0x00CF9200 ; Kernel Data
dd 0x0000FFFF , 0x00CFFA00 ; User Code
dd 0x0000FFFF , 0x00CFF200 ; User Data
TSS:
db 103
db 0
First: db 0
Second: db 0
Third: db 0
db 233
db 0
Last: db 0
GDT_End :
C Code
-----------
Code: Select all
void TSS_init(void){
memset_byte(&TASK_STATE_SEGMENT,sizeof(TSS),0); // Clearing the TSS structure
TSS = &TASK_STATE_SEGMENT;
TSS->cs = 0x0B;
TSS->ds = TSS->es = TSS->fs = TSS->gs = TSS->ss = 0x13;
TSS->ss0 = 0x10;
TSS->esp0 = 0xC03FF000;
TSS->iomap_base = ( unsigned short ) sizeof(TSS); // set to point beyond the TSS limit
switch_to_user_mode();
}
void switch_to_user_mode()
{
// James's Code to 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:");
puts("Blah-blah"); // No GPF. Status OK. All in Ring 3
//printf ("%d",65); // GPF. Status: Cs in RING 0. SS in Ring 0
//malloc(5); // GPF. Status: Cs in RING 0. SS in Ring 0
a: goto a; // Tempororily Stopping for debugging purposes
}
Virtual Machine: qemu
develpment machine: x86. OS: lubuntu 11.04