how can i create a task that works in user mode ?
Posted: Sat Jun 18, 2005 6:20 am
Hi *
I would want to create a task that works in user mode.
I have implemented a software tasks switching with "iret"
this is the code:
task.h file
typedef struct {
unsigned int edi;
unsigned int esi;
unsigned int ebp;
unsigned int null;
unsigned int ebx;
unsigned int edx;
unsigned int ecx;
unsigned int eax;
unsigned int eip;
unsigned int cs;
unsigned int eflags;
unsigned int end;
} tss_t;
//this structure "simuls" the TSS
typedef struct
{
pid_t pid;
unsigned int esp;
tss_t* tss;
char *name;
unsigned char state;
} task_t;
-------------------------------------------------
task.c file
task_t process_table[100];
void create_task(void (*func)(), char *name, unsigned char privilege)
{
process_table[free_pid].tss = (tss_t*)((unsigned
int)kmalloc(STACK_SIZE) + STACK_SIZE-100);
process_table[free_pid].tss->eax = 0; //init eax
process_table[free_pid].tss->ebx = 0; //init ebx
process_table[free_pid].tss->ecx = 0; //init ecx
process_table[free_pid].tss->edx = 0; //init edx
process_table[free_pid].tss->edi = 0; //init edi
process_table[free_pid].tss->esi = 0; //init esi
if(privilege == SUPERVISOR_PRIVILEGE)
process_table[free_pid].tss->cs = KERNEL_CODE_SELECTOR;
else
process_table[free_pid].tss->cs = USER_CODE_SELECTOR;
process_table[free_pid].tss->eip = (unsigned)func;
process_table[free_pid].tss->eflags = 0x202;
process_table[free_pid].tss->end = (unsigned)end_task;
process_table[free_pid].esp = (unsigned
int)process_table[free_pid].tss;
process_table[free_pid].name = name;
When there is a switch context i put process_table[free_pid].esp on the
stack and i restore from it
---------------------------------------------------------------
when arrive interrupt i do:
"pusha \n"
"movl %esp, %eax \n"
"pushl %eax \n"
"call switch \n" //Switch.. put on the stack the new_Stack
create..
//restore new stack
"popl %eax \n"
"mov %eax, %esp \n"
"popa \n" //restore general register
"iret \n"
BUT this code doesn't work fine
bochs gives me: iret: SS selector NULL:
then i modify my structure(i added ss and esp)
typedef struct {
unsigned int edi;
unsigned int esi;
unsigned int ebp;
unsigned int null;
unsigned int ebx;
unsigned int edx;
unsigned int ecx;
unsigned int eax;
unsigned int eip;
unsigned int cs;
unsigned int eflags;
unsigned int esp;
unsigned int ss;
unsigned int fine;
} tss_t;
and when i create new task i have initialized them
if(privilege == SUPERVISOR_PRIVILEGE)
{
process_table[free_pid].tss->ss = KERNEL_DATA_SELECTOR;
process_table[free_pid].tss->esp =
(int)process_table[free_pid].tss
}
else
}
process_table[free_pid].tss->ss = USER_DATA_SELECTOR;
process_table[free_pid].tss->esp =
(int)process_table[free_pid].tss
}
BUT bochs gives me: iret: get_SS_ESP_from_TSS: TR.cache invalid
My gdt is:
0-4gb code/data kernel
0-4gb code/data user
KERNEL_CODE_SELECTOR = 0x8
KERNEL_DATA_SELECTOR = 0x10
USER_CODE_SELECTOR = 0x1b
USER_DATA_SELECTOR = 0x23
Help me plese! ... and sorry for my long post
I would want to create a task that works in user mode.
I have implemented a software tasks switching with "iret"
this is the code:
task.h file
typedef struct {
unsigned int edi;
unsigned int esi;
unsigned int ebp;
unsigned int null;
unsigned int ebx;
unsigned int edx;
unsigned int ecx;
unsigned int eax;
unsigned int eip;
unsigned int cs;
unsigned int eflags;
unsigned int end;
} tss_t;
//this structure "simuls" the TSS
typedef struct
{
pid_t pid;
unsigned int esp;
tss_t* tss;
char *name;
unsigned char state;
} task_t;
-------------------------------------------------
task.c file
task_t process_table[100];
void create_task(void (*func)(), char *name, unsigned char privilege)
{
process_table[free_pid].tss = (tss_t*)((unsigned
int)kmalloc(STACK_SIZE) + STACK_SIZE-100);
process_table[free_pid].tss->eax = 0; //init eax
process_table[free_pid].tss->ebx = 0; //init ebx
process_table[free_pid].tss->ecx = 0; //init ecx
process_table[free_pid].tss->edx = 0; //init edx
process_table[free_pid].tss->edi = 0; //init edi
process_table[free_pid].tss->esi = 0; //init esi
if(privilege == SUPERVISOR_PRIVILEGE)
process_table[free_pid].tss->cs = KERNEL_CODE_SELECTOR;
else
process_table[free_pid].tss->cs = USER_CODE_SELECTOR;
process_table[free_pid].tss->eip = (unsigned)func;
process_table[free_pid].tss->eflags = 0x202;
process_table[free_pid].tss->end = (unsigned)end_task;
process_table[free_pid].esp = (unsigned
int)process_table[free_pid].tss;
process_table[free_pid].name = name;
When there is a switch context i put process_table[free_pid].esp on the
stack and i restore from it
---------------------------------------------------------------
when arrive interrupt i do:
"pusha \n"
"movl %esp, %eax \n"
"pushl %eax \n"
"call switch \n" //Switch.. put on the stack the new_Stack
create..
//restore new stack
"popl %eax \n"
"mov %eax, %esp \n"
"popa \n" //restore general register
"iret \n"
BUT this code doesn't work fine
bochs gives me: iret: SS selector NULL:
then i modify my structure(i added ss and esp)
typedef struct {
unsigned int edi;
unsigned int esi;
unsigned int ebp;
unsigned int null;
unsigned int ebx;
unsigned int edx;
unsigned int ecx;
unsigned int eax;
unsigned int eip;
unsigned int cs;
unsigned int eflags;
unsigned int esp;
unsigned int ss;
unsigned int fine;
} tss_t;
and when i create new task i have initialized them
if(privilege == SUPERVISOR_PRIVILEGE)
{
process_table[free_pid].tss->ss = KERNEL_DATA_SELECTOR;
process_table[free_pid].tss->esp =
(int)process_table[free_pid].tss
}
else
}
process_table[free_pid].tss->ss = USER_DATA_SELECTOR;
process_table[free_pid].tss->esp =
(int)process_table[free_pid].tss
}
BUT bochs gives me: iret: get_SS_ESP_from_TSS: TR.cache invalid
My gdt is:
0-4gb code/data kernel
0-4gb code/data user
KERNEL_CODE_SELECTOR = 0x8
KERNEL_DATA_SELECTOR = 0x10
USER_CODE_SELECTOR = 0x1b
USER_DATA_SELECTOR = 0x23
Help me plese! ... and sorry for my long post