Re:tss and user space
Posted: Sat Jan 07, 2006 12:22 pm
Probbably ypu have bad value of CS on stack while CPU executing IRET.
Code: Select all
int* stack = (int*)kmalloc(512); //setup 512 byte size stack
/* 512 bytes give space for 128 values on the stack - we will have 15 values on the stack - the stack grows to smaller addresses therefore goto stacktop*/
stack+=113;
stack[0]=0x10; //es
stack[1]=0x10; //ds
stack[2]=0x10; //fs
stack[3]=0x10; //gs
stack[4]=0; //edi
stack[5]=0; //esi
stack[6]=0; //ebp
stack[7]=0; //esp
stack[8]=0; //ebx
stack[9]=0; //edx
stack[10]=0; //ecx
stack[11]=0; //eax
stack[12]=(uint)entry; //entry point
stack[13]=0x08;
stack[14]=0x0202;
task->p_kstack = (uint)stack+14; // stackend will be stacktop + pushed values
Code: Select all
void make_task(int pri, char *name, void (*entry)(), int ring)
{
void *stack_mem;
stack_data_t *stack;
stack_mem = (unsigned int *)malloc(STACK_SIZE);
stack_mem += STACK_SIZE - sizeof(stack_data_t);
stack = stack_mem;
if(ring == 0)
{
stack->gs = KERNEL_DATA_SEG;
stack->fs = KERNEL_DATA_SEG;
stack->es = KERNEL_DATA_SEG;
stack->ds = KERNEL_DATA_SEG;
stack->cs = KERNEL_CODE_SEG;
}
else
{
stack->gs = USER_DATA_SEG;
stack->fs = USER_DATA_SEG;
stack->es = USER_DATA_SEG;
stack->ds = USER_DATA_SEG;
stack->cs = USER_CODE_SEG;
}
stack->edi = 0;
stack->esi = 0;
stack->esp = (unsigned int)stack;
stack->ebp = stack->esp;
stack->ebx = 0;
stack->edx = 0;
stack->ecx = 0;
stack->eax = 0;
stack->eip = (unsigned int)entry;
stack->eflags = 0x00000202;
strncpy(rrq[end].name, name, 32);
rrq[end].stack = stack;
rrq[end].ss = KERNEL_STACK_SEG;
rrq[end].priority = pri;
rrq[end].time = get_pri_time(pri);
end++;
}
Code: Select all
_task_timer:
push ss ; <------------- NEW -------------
pushad
push ds
push es
push fs
push gs
mov ax, 0x10
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
; cli
push esp
mov eax, _task_timer_c
call eax
; sti
pop esp
mov esp, eax
pop gs
pop fs
pop es
pop ds
popad
pop ss ; <------------- NEW -------------
iretd
Code: Select all
set_a_gdt(5, (unsigned long)&TSS, sizeof(TSS) - 1, 0x89, 0x10);