Page 1 of 1
cpl3->cpl0
Posted: Sun Oct 23, 2005 12:45 pm
by HardEnough
hi guys
sorry for a new thread.
but if i want to context switch from cpl 3 -> cpl 0 i should push ss3 and esp3 , right ?
so for the first time i create a task i need to initialize its kernel stack.
so which one should point to the userstack and changes automatically as the stack grows or shrinks with the task, is it (2) or (3) ??
please can any one make it clear to me ?
thank you
Code: Select all
create_task:
push ebp
mov ebp, esp
mov esp, dword [ebp+08]
;---------------------;
; push dword ss3 ; -----------> (1) ?
; push dword esp3 ; -----------> (2) ?
;---------------------;
push dword 0202h ; eflags
push dword 08h ; cs
mov eax, dword [ebp+12]
push dword eax ; eip
push dword 00h ; eax
push dword 00h ; ecx
push dword 00h ; edx
push dword 00h ; ebx
mov eax, dword [ebp+16]
push dword eax ; esp -------------> (3) ?
push dword 00h ; ebp
push dword 00h ; esi
push dword 00h ; edi
push dword 10h ; gs
push dword 10h ; fs
push dword 10h ; es
push dword 10h ; ds
push dword 00h ; error code
push dword 20h ; interrupt number
mov eax, esp
mov esp, ebp
pop ebp
ret
Re:cpl3->cpl0
Posted: Sun Oct 23, 2005 1:47 pm
by Cjmovie
Ugh, I've no idea what you're doing.
But I can point out that you (It doesn't seem so, at least) don't have a TSS installed.
When the processor switching from Ring 3 to Ring 0 code, it will load all of the values (kernel stack, etc.) from the first TSS enty.
Hope I was of, at least, some help.
Re:cpl3->cpl0
Posted: Sun Oct 23, 2005 2:07 pm
by HardEnough
But I can point out that you (It doesn't seem so, at least) don't have a TSS installed.
When the processor switching from Ring 3 to Ring 0 code, it will load all of the values (kernel stack, etc.) from the first TSS enty.
ofcourse i know that, i'll update the TSS (ss0,esp0) during context switching.
i meant what should point to the real esp of user process which changes automatically as the process do pushes and pops , is it (1) or (2) ?
and the more important issues, when creating a new process which one ( (1) or (2) ) should i point to the end of the memory allocated for the process user stack ?
Re:cpl3->cpl0
Posted: Mon Oct 24, 2005 10:16 am
by HardEnough
hi again,
it seems that no one understood me. i will try to make it clearer.
for example if a user process called a syscall by an int instruction, and the parameters where pushed on the process stack rather than the registers, which is the same technique used by *BSD. so which should point to the real user stack of the process esp3 number (2) or esp number (3) ?
another thing what should i initially set the eso3 number (2) & esp number (3) when i create a new process ?
please can any one help me.
thanks
Re:cpl3->cpl0
Posted: Mon Oct 24, 2005 2:37 pm
by distantvoices
well, the first part I for one do otherwise: I pass pointers to the system call by putting these values into registers. the systemcall pries the parameters from the saved processor state image in tcb->esp0 then.
second: Hm. You allocate a stack area. Then you pass your ring3 selector into ss3 field and your freshly allocated stack image+sizeof stackimage-4 to the esp3 field. Voila.
third: for ring3->ring0 transitions, you don't need to save off esp3/ss3 (althou in the irq stub one also pushes esp). That's done by int [xx]. YOu need the tss to provide the processor with info about where is the esp0 stack of that task and the according ss0 value.
stay safe.
Re:cpl3->cpl0
Posted: Mon Oct 24, 2005 6:56 pm
by HardEnough
hi beyond infinity,
Here is my GDT
Code: Select all
gdt:
null_desc dd 0h
dd 0h
flat_code dw 0ffffh
dw 0h
db 0h
db 10011110b ; 9eh
db 11001111b ; cfh
db 0h
flat_data dw 0ffffh
dw 0h
db 0h
db 10010010b ; 92h
db 11001111b ; cfh
db 0h
user_code dw 0ffffh
dw 0h
db 0h
db 11111110b
db 11001111b
db 0h
user_data dw 0ffffh
dw 0h
db 0h
db 11110010b
db 11001111b
db 0h
task_state dd 0h
dd 0h
and here is my TSS
Code: Select all
tss:
back_link dw 0h
dw 0h
esp0 dd 0h
ss0 dw 20h ;------> constant
dw 0h
esp1 dd 0h
ss1 dw 0h
dw 0h
esp2 dd 0h
ss2 dw 0h
dw 0h
ccr3 dd 0h
eip dd 0h
eflags dd 0h
eeax dd 0h
eecx dd 0h
eedx dd 0h
eebx dd 0h
eesp dd 0h
eebp dd 0h
eesi dd 0h
eedi dd 0h
ees dw 0h
dw 0h
ecs dw 0h
dw 0h
eds dw 0h
dw 0h
efs dw 0h
dw 0h
egs dw 0h
dw 0h
eldt dw 0h
dw 0h
dw 0h
bitmap dw 0h
Here is my IRQ0 handler
Code: Select all
irq0:
pusha
push gs
push fs
push es
push ds
mov ax, 10h
mov gs, ax
mov fs, ax
mov es, ax
mov ds, ax
push dword 00h
push dword 20h
push dword esp
call interrupt
mov esp, [eax+4]
mov [tss+4], dword eax
mov al, 20h
out 20h, al
add esp, 08h
pop ds
pop es
pop fs
pop gs
popa
iret
Code: Select all
create_task:
push ebp
mov ebp, esp
mov esp, dword [ebp+08]
push dword 00h ; ss3 i don't know what to put here as initial value
push dword 00h ; esp3 also here
push dword 0202h ; eflags
push dword 18h ; cs
mov eax, dword [ebp+12]
push dword eax ; eip
push dword 00h ; eax
push dword 00h ; ecx
push dword 00h ; edx
push dword 00h ; ebx
mov eax, dword [ebp+16]
push dword eax ; esp ;------------> i don't what should is put here as an initial value
push dword 00h ; ebp
push dword 00h ; esi
push dword 00h ; edi
push dword 20h ; gs
push dword 20h ; fs
push dword 20h ; es
push dword 20h ; ds
push dword 00h ; error code
push dword 20h ; interrupt number
mov eax, esp
mov esp, ebp
pop ebp
ret
Here the Code
Code: Select all
void koko(void); /* a test process */
void wawa(void); /* another test process */
int create_task(int, int, int);
int koko_kstack[76]; /* there is no mm yet */
int wawa_kstack[76];
int koko_ustack[90];
int wawa_ustack[90];
#define TOP 0 /* Stack Top */
#define END 1 /* Stack Buttom */
int koko_s[2];
int wawa_s[2];
#define NONE -1
#define KOKO 1
#define WAWA 2
int task;
main()
{
koko_s[TOP]=(int)&koko_kstack[0];
wawa_s[TOP]=(int)&wawa_kstack[0];
koko_s[END]=create_task((int)&koko_kstack[75],(int)&koko, (int)&koko_ustack[89]);
wawa_s[END]=osdk_create_task((int)&wawa_kstack[75],(int)&wawa, (int)&wawa_ustack[89]);
task=NONE;
for(;;);
}
int interrupt(int esp, int num, int err)
{
if(task!=NONE)
{
if (task==KOKO){
koko_s[END]=esp;
task=WAWA;
return (int)&wawa_s[TOP];
}
else{
wawa_s[END]=esp;
task=KOKO;
return (int)&koko_s[TOP];
}
}else{
task=KOKO;
return koko_s[TOP];
}
}
void koko()
{
kputchar('K');
while(1){
if(seconds==10){
kputchar('K');
while(1){
if(seconds==11){
kputchar('W');
break;
}
}
}
}
}
void wawa()
{
kputchar('W');
while(1){
if(seconds==12){
kputchar('W');
while(1){
if(seconds=15){
kputchar('W');
break;
}
}
}
}
}
Please can any body figure what i've foolishly done :-p
thank alot