Page 1 of 1

how can i create a task that works in user mode ?

Posted: Sat Jun 18, 2005 6:20 am
by HellHybrid
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 :)
   

Re:how can i create a task that works in user mode ?

Posted: Sat Jun 18, 2005 6:37 am
by AR
You don't have a TSS is what the "iret: get_SS_ESP_from_TSS: TR.cache invalid" is most likely complaining about.

Re:how can i create a task that works in user mode ?

Posted: Sat Jun 18, 2005 3:07 pm
by mystran
Bochs source code can be used to figure out exactly why it throws that error, but my guess is that you either didn't load the TR correctly with your TSS address.

Re:how can i create a task that works in user mode ?

Posted: Sat Jun 18, 2005 5:43 pm
by HellHybrid
TR ??? :-\

Re:how can i create a task that works in user mode ?

Posted: Sat Jun 18, 2005 5:46 pm
by AR
Task Register, you have to create a TSS, attach it to the GDT with a TSS Descriptor and then load it with the LTR instruction (or IRET to it as the code segment)

Re:how can i create a task that works in user mode ?

Posted: Sat Jun 18, 2005 7:21 pm
by HellHybrid
i have writed:

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 <<<<<<<<<< simuls = simulates...

it isn't the tss hardware because i use a software task switching
it simulates the "stack" from wich i restore the registers

help me ::)

Re:how can i create a task that works in user mode ?

Posted: Sat Jun 18, 2005 11:14 pm
by AR
And I'm telling you that you need at least one hardware TSS, task switching cannot be properly performed (at least not anything beyond all at Ring 0) without a TSS for the CPU to get the Kernel Stack Segment and Stack Pointer from.

Software Task Switching involves simply loading the first task into the hardware TSS and IRETing/jmping to the TSS Selector, or filling the Kernel Stack and Kernel Stack Pointer fields and LTRing it before IRETing to the task using your stack approach. The TSS' Stack Segment and/or Stack Pointer will need to be swapped for each task if you're using stack based task switching.

Re:how can i create a task that works in user mode ?

Posted: Sat Jun 18, 2005 11:15 pm
by Brendan
Hi,

When the CPU goes from CPL=3 to CPL=0 (for a system call, exception, IRQ, etc) it always loads a new SS:ESP from the TSS's SS0:ESP0 fields. This is mainly so that CPL=0 code uses a different stack (in case the CPL=3 stack isn't working).

This means that for software task switching you must have a TSS, and you need to change the SS0:ESP0 fields in that TSS every time you do a software task switch.
HellHybrid wrote:it isn't the tss hardware because i use a software task switching it simulates the "stack" from wich i restore the registers
This structure isn't necessary, as you can just push values on the stack. For e.g.:

Code: Select all

struct thread_data
  .saved_ESP    resd 4
  .saved_ESP0  resd 4
  .saved_CR3    resd 4
end_struct

;Switch tasks
; ebx = address of new thread's data

goto_thread:
   pushad
   pushfd
   cli
   mov eax,[address_of_current_thread_data]
   mov [eax+thread_data.saved_ESP],esp

   mov [address_of_current_thread_data],ebx
   mov esp,[ebx+thread_data.saved_ESP]
   mov ecx,[ebx+thread_data.saved_ESP0]
   mov eax,[ebx+thread_data.saved_CR3]
   mov [TSS.ESP0],ecx
   mov cr3,eax
   popfd
   popad
   ret
That's the basic thread switch code - you'd also need to do something with FPU/MMX/SSE state - FXSAVE/FXLOAD instructions for newer computers or FNSAVE/FNLOAD for older computers, and possibly delayed FPU/MMX/SSE state saving using the TS flag and the "device not available" exception (see Intel's system programming guide, section 12.5.1 "Using the TS Flag to Control the Saving of the x87 FPU, MMX, SSE, SSE2 and SSE3 State").


Cheers,

Brendan