Page 1 of 2

A few questions on hardware based Multitasking

Posted: Sun Oct 28, 2007 6:18 am
by ChristianF
Hi
I have currently enabled paging and can allocate memory dynamically.
No I started implementing hardwarebased multitasking and so there I have a few questions.
  • 1.) Do I need more than one instance of my tss structure?
    2.) Is the Accessnumber for the gdt 0x09?
    3.) What is the base of the tss and what the limit?
Here is my tss structure:

Code: Select all

typedef struct tss
{
	u16int backlink, __blh;
	u32int esp0;
	u16int ss0, __ss0h;
	u32int esp1;
	u16int ss1, __ss1h;
	u32int esp2;
	u16int ss2, __ss2h;
	u32int cr3;
	u32int eip;
	u32int eflags;
	u32int eax, ecx, edx , ebx;
	u32int esp, ebp, esi, edi;
	u16int es, __esh;
	u16int cs, __csh;
	u16int ss, __ssh;
	u16int ds, __dsh;
	u16int fs, __fsh;
	u16int gs, __gsh;
	u16int ldt, __ldth;
	u16int trace, bitmap;
} tss_t;

And this is the way i initialize it:

Code: Select all


tss_t *ptr_tss;
void initialise_multitasking(void)
{
	/*
	 * For Hardware Task switching set up an empty tss Table and
	 * and set the gdt gate 5
	 */
	// Allocate the memory for ptr_tss
	ptr_tss = (tss_t *)kmalloc(sizeof(tss_t));
	// set a new GDT entry for the tss
        /*Arguments of 'gdt_set_gate':number,base,limit,access, gran*/
	gdt_set_gate(5, /*Is this right?->*/0, sizeof(tss_t), 0x28, 0xCF)
	// Flush GDT
	gdt_flush(/*gdt pointer here*/);
	__asm__ __volatile__("ltr %%ax" :: "a" (0x09));
}

Is this the right way of initialising multitasking or do I have made some failures.

Greetings
Christian

PS: My English is sometimes not very good.

Posted: Sun Oct 28, 2007 9:51 am
by nooooooooos
1) You need this structure for every task!!
2) That can be various of your Configuration but sure not 0x9...When its your 3rd entry in the GDT than its 3*8 also, 0x18...
3) The base is the beginn of the structure! (When you don't stuff like this...) And the limit is 104 (when you don't have a IO-Permission-Map)...

Cheers
Noooooooooos

Posted: Sun Oct 28, 2007 10:03 am
by iammisc
1. You do need a tss for every task if you're doing hardware multitasking
2. the segment is definitely not 0x9 if it's the 5th entry(where did you even get that from). The segment selector is found by multiplying 0x5*8 because it represents the offset in the gdt. In your case the 5th gdt entry would be 0x28
3. The base is the physical address of the tss and the limit is the size of the tss(104 bytes without the permissions bitmap). How else, would the processor even know where your tss is?

Also, merely doing an ltr is not multitasking, that simply loads the current tss, you need to do a far jump to the tss's segment selector.

Posted: Sun Oct 28, 2007 10:37 am
by ChristianF
So I have to create every time an gdt entry when i want to create a task?
Or is this wrong?

:oops:

Posted: Sun Oct 28, 2007 10:49 am
by nooooooooos
It's right...and thats one of many reasons to use software multitasking...HW-MT is not usable in 64-Bit Mode and more difficult to implement.

Posted: Sun Oct 28, 2007 11:20 am
by JAAman
OdinPG wrote:So I have to create every time an gdt entry when i want to create a task?
yes and no...

each task will need a separate TSS structure, but it is possible to use only 2 GDT entries... (change the GDT entry to point to the TSS you want to switch to, each time you switch tasks...)

but the simplest way, yes, you need a GDT entry for each task you create

this is one (but not the only) reason most people dont use hardware-multi-tasking -- even when it was universally availible (now it isnt -- you cannot use hardware-multi-tasking in LMode -- required for 64bit OSs)

Posted: Mon Oct 29, 2007 4:56 am
by ChristianF
mmmhhh...
Ok then i will look how to implement Software based Multitasking.
Thanks for the answers.

Christian

Posted: Mon Oct 29, 2007 5:27 am
by JamesM
OdinPG wrote:mmmhhh...
Ok then i will look how to implement Software based Multitasking.
Thanks for the answers.

Christian
Thank us later. It's easier by far! (and more portable)

Problem with v86

Posted: Wed Dec 19, 2007 3:41 pm
by ChristianF
Hi
it's me again...
I have implemented an easy software based multitasking.
Normal ring0 Task are working as they should, but the v86 task activates an hardware reset.

My TSS is initialised successfully, but then he resets.
here is the information from bochs:

Code: Select all

00000000000i[SYS  ] bx_pc_system_c::Reset(HARDWARE) called
And this is my code for initialising a v86 task:

Code: Select all

void createV86Task(void(*thread), const char *name)
{
     if( tss_registered == false )
     {
         register_tss();
         tss_registered = true;
     }
     pE = createElement();
     pD = createData();
     addDataToElement(pE, pD);
     addElementToList(pE, head);
     
     unsigned int *stack;
     unsigned int *userstack;
     
     pE->pData->thread_struct.userstack = kmalloc(4096);
	 pE->pData->thread_struct.esp0 = kmalloc(4096)+4096;
	 stack = (unsigned int *)pE->pData->thread_struct.esp0;
	 userstack = (unsigned int *)pE->pData->thread_struct.userstack;
     
     
     *(--stack) = 0x20|3;                     // gs (User Data)
     *(--stack) = 0x20|3;                     // fs (User Data)
     *(--stack) = 0x20|3;                     // es (User Data)
     *(--stack) = 0x20|3;                     // ds (User Data)
     *(--stack) = 0x20|3;                     // ss (User Data)
     *(--stack) = (unsigned int)userstack;    // sp
     //*(--stack) = 0x00;
     *(--stack) = EFLAGS_VM | EFLAGS_IOPL0 | EFLAGS_IF | 0x20;//0x246 | 0x20000;            // eflags
     *(--stack) = 0x18|3;                     // cs (User Code)
     *(--stack) = (unsigned int)thread;       // ip
     *(--stack) = 0x00;                       // EDI
     *(--stack) = 0x00; 	                  // ESI
     *(--stack) = 0x00;  	                  // EBP
     //*(--stack) = 0x00;  	                  // Just an offset, no value
     *(--stack) = 0x00;  	                  // EBX
     *(--stack) = 0x00;  	                  // EDX
     *(--stack) = 0x00;  	                  // ECX
     *(--stack) = 0x00;  	                  // EAX
     *(--stack) = 0x10; 	                  // DS
     *(--stack) = 0x10; 	                  // ES
     *(--stack) = 0x10; 	                  // FS
 	 *(--stack) = 0x10; 	                  // GS
     
     
     pE->pData->thread_struct.esp0 = (unsigned int)stack;
          
     strcpy(pE->pData->thread_struct.thread_name, name);
     pE->pData->thread_struct.pid = (++pid);
     pE->pData->thread_struct.v86 = 1;
     pE->pData->thread_struct.tss = (tss_t *)kmalloc(sizeof(tss_t));
     
     pE = NULL;
     pD = NULL;
}
All Threads are in a linked list and here is my thread structure:

Code: Select all

typedef struct thread
{
        unsigned int esp0, esp3;
        unsigned int v86;        // Will be set to 1 If the task is running in vm.
        unsigned int state;      // The Process state. There will be saved numbers from 0 to 5.
        unsigned int pid;        // The Process-ID
        char thread_name[64];    // Name of the thread
        
        // only needed for v86
        tss_t *tss;              // Only needed for v86 Tasks
        unsigned int userstack;
} thread_t;
I don't find the Place in the Source which calls the hardware reset. Is it a wrong value in eflags or something else?

greetings:
OdinPG

Re: Problem with v86

Posted: Wed Dec 19, 2007 4:58 pm
by Combuster
OdinPG wrote: here is the information from bochs:

Code: Select all

00000000000i[SYS  ] bx_pc_system_c::Reset(HARDWARE) called
There must be more, no?

Posted: Sat Dec 22, 2007 5:33 am
by ChristianF
Ok I found the failure.
i set the eflags vallue wrong.
but now i become this error message from bochs (not really an error message):

Code: Select all

00019752553e[CPU0 ] fetch_raw_descriptor: GDT: index (3227)644 > limit (1f)
could yo sy me what this should say to me?
My GDT isinitialised at the begin like this:

Code: Select all

/* Setup the GDT pointer and limit */
    gp.limit = (sizeof(struct gdt_entry) * 4) - 1;
    gp.base = (unsigned int)&gdt;

    /* Our NULL descriptor */
    gdt_set_gate(0, 0, 0, 0, 0);

    /* The second entry is our Code Segment. The base address
    *  is 0, the limit is 4GBytes, it uses 4KByte granularity,
    *  uses 32-bit opcodes, and is a Code Segment descriptor.
    *  Please check the table above in the tutorial in order
    *  to see exactly what each value means */
    gdt_set_gate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF);

    /* The third entry is our Data Segment. It's EXACTLY the
    *  same as our code segment, but the descriptor type in
    *  this entry's access byte says it's a Data Segment */
    gdt_set_gate(2, 0, 0xFFFFFFFF, 0x92, 0xCF);
    
    
    gdt_set_gate(4, 0, 0xFFFFFFFF, 0xFA, 0xCF);     /*User Code*/
    gdt_set_gate(5, 0, 0xFFFFFFFF, 0xF2, 0xCF);     /*User Data*/
        
    /* Flush out the old GDT and install the new changes! */
    gdt_flush();
And later when I initialise the TSS for the vm it will be initialised like this:

Code: Select all

unsigned long base = (unsigned long)&myTSS;
     unsigned long size = sizeof(tss_t);
     int num = 3;
     int trace = num << 3;
     
     printf("calling gdt_set_gate(%i, 0x%x, 0x%x, 0x89, 0xCF)...\n", num, base, (base+size));
     gdt_set_gate(num, base, (base+size), 0x89, 0xCF);
     printf("flushing the gdt...\n");
     gdt_flush();
     myTSS.ss0 = 0x10;
     myTSS.iomap = (unsigned short)sizeof(tss_t);
     printf("calling ltr 0x%x...\n", trace);
     __asm__ __volatile__("ltr %%ax"::"a"(trace));
When I want to initialize the tss with the position 5 it went into an endless loop.
so i initialized it with 3 and user- code and data with 4 and 5.
Is this the right way?

greetingd
Christian

Posted: Sat Dec 22, 2007 11:38 am
by frank
Instead of 0xCF for the flags of the TSS GDT entry try 0x67.

Posted: Thu Dec 27, 2007 4:30 am
by ChristianF
Ok
I have rewritten some parts of the kernel and now it works too, when I want to install the tss on place 5 in the array.
But now I have a new Problem:
The Thread will be initialized and the Multitasking works but when it switchs to the v86 task bochs restarts. The Message I got from bochs was like this:

Code: Select all

00022167635e[CPU0 ] interrupt(): SS selector null
00022167635e[CPU0 ] interrupt(): SS selector null
00022167635i[CPU0 ] v8086 mode
00022167635i[CPU0 ] CS.d_b = 16 bit
00022167635i[CPU0 ] SS.d_b = 16 bit
00022167635i[CPU0 ] EFER   = 0x00000000
00022167635i[CPU0 ] | RAX=0000000000000000  RBX=0000000000000000
00022167635i[CPU0 ] | RCX=0000000000000000  RDX=0000000000000000
00022167635i[CPU0 ] | RSP=0000000000000000  RBP=0000000000000000
00022167635i[CPU0 ] | RSI=0000000000000000  RDI=0000000000000000
00022167635i[CPU0 ] |  R8=0000000000000000   R9=0000000000000000
00022167635i[CPU0 ] | R10=0000000000000000  R11=0000000000000000
00022167635i[CPU0 ] | R12=0000000000000000  R13=0000000000000000
00022167635i[CPU0 ] | R14=0000000000000000  R15=0000000000000000
00022167635i[CPU0 ] | IOPL=0 id vip vif ac VM RF nt of df IF tf sf zf af pf cf
00022167635i[CPU0 ] | SEG selector     base    limit G D
00022167635i[CPU0 ] | SEG sltr(index|ti|rpl)     base    limit G D
00022167635i[CPU0 ] |  CS:0018( 0001| 0|  3) 00000180 0000ffff 0 0
00022167635i[CPU0 ] |  DS:0020( 0002| 0|  3) 00000200 0000ffff 0 0
00022167635i[CPU0 ] |  SS:0020( 0002| 0|  3) 00000200 0000ffff 0 0
00022167635i[CPU0 ] |  ES:0020( 0002| 0|  3) 00000200 0000ffff 0 0
00022167635i[CPU0 ] |  FS:0020( 0002| 0|  3) 00000200 0000ffff 0 0
00022167635i[CPU0 ] |  GS:0020( 0002| 0|  3) 00000200 0000ffff 0 0
00022167635i[CPU0 ] |  MSR_FS_BASE:0000000000000200
00022167635i[CPU0 ] |  MSR_GS_BASE:0000000000000200
00022167635i[CPU0 ] | RIP=0000000000003740 (0000000000003740)
00022167635i[CPU0 ] | CR0=0x80000011 CR1=0x0 CR2=0x0000000000000200
00022167635i[CPU0 ] | CR3=0x0010a000 CR4=0x00000000
00022167635i[CPU0 ] >> add byte ptr ds:[bx+si], al : 0000
00022167635e[CPU0 ] exception(): 3rd (10) exception with no resolution, shutdown status is 00h, resetting
00022167635i[SYS  ] bx_pc_system_c::Reset(SOFTWARE) called
I don't find what is wrong in the code because the function initializes ss with the user data selector.
This is my Code:

Code: Select all

void create_v86_task(entry_t thread, const char *name)
{
	// If there was no previous creation of an v86 task...
	if(initialized == false)
	{
		// initialize the TSS and set the v86monitor
		idt_set_gate(13, (unsigned)v86_monitor, 0x08, 0x8E);
		set_tss();
		initialized = true;
	}
	struct process_element *pE = NULL;
	struct process_data *pD		= NULL;
	u32int *stack;
	int x = strlen(name);
	
	pE = create_process_element();
	pD = create_process_data();
	
	add_data_to_element(pE, pD);
	add_element_to_list(pE, head_process);
	
	pE->data->thread.esp0 = 0;
	pE->data->thread.esp1 = 0;
	pE->data->thread.esp2 = 0;
	
	pE->data->thread.esp3 = kmalloc(4096)+4096;
	stack = (u32int *)pE->data->thread.esp3;
	
	/* v86 specific registers */
	*(--stack) = USER_DATA_SEL;	// gs
	*(--stack) = USER_DATA_SEL;	// fs
	*(--stack) = USER_DATA_SEL;	// es
	*(--stack) = USER_DATA_SEL;	// ds
	*(--stack) = USER_DATA_SEL;	// ss
	*(--stack) = 0;		            // sp
	*(--stack) = 0x20202;		// eflags
	*(--stack) = USER_CODE_SEL;	// cs
	*(--stack) = (u32int)thread;	// ip
	/* general purpose registers */
	*(--stack) = 0;	// eax
	*(--stack) = 0;	// ebx
	*(--stack) = 0;	// ecx
	*(--stack) = 0;	// edx
	*(--stack) = 0;	// ebp
	*(--stack) = 0;	// esp
	*(--stack) = 0;	// esi
	*(--stack) = 0;	// edi
	/* segment registers */
 	*(--stack) = KERNEL_DATA_SEL;	// ds
   *(--stack) = KERNEL_DATA_SEL;	// es
   *(--stack) = KERNEL_DATA_SEL;	// fs
 	*(--stack) = KERNEL_DATA_SEL;	// gs
	
	pE->data->thread.esp0 = 0;
	pE->data->thread.esp1 = 0;
	pE->data->thread.esp2 = 0;
	pE->data->thread.esp3 = (u32int)stack;
	
	pE->data->pid = (++pid);
	pE->data->state = TASK_RUNABLE;
	pE->data->thread.v86 = 1;
	
	pE->data->name = (unsigned char *)kmalloc(sizeof(char)*x);
	memcpy(pE->data->name, name, sizeof(char)*x);
	pE->data->thread.v86_tss = (tss_t *)kmalloc(sizeof(tss_t));
	pE->data->state = TASK_RUNABLE;
	memset(pE->data->thread.v86_tss, 0, sizeof(tss_t));
	
	pE->data->thread.v86_tss->eflags		= 0x20202;
	pE->data->thread.v86_tss->cs			= USER_CODE_SEL;
	pE->data->thread.v86_tss->ss			= USER_DATA_SEL;
	pE->data->thread.v86_tss->es			= USER_DATA_SEL;
	pE->data->thread.v86_tss->ds			= USER_DATA_SEL;
	pE->data->thread.v86_tss->fs			= USER_DATA_SEL;
	pE->data->thread.v86_tss->gs			= USER_DATA_SEL;
	pE->data->thread.v86_tss->eip			= (u32int)thread;
	pE->data->thread.v86_tss->bitmap		= (u16int)sizeof(tss_t);
}
And this is the Code which i want to execute:

Code: Select all

[BITS 16]

[SECTION .text]

global _init_13h

_init_13h:
	mov ax, 0x13
	int 0x10
.loop
	jmp .loop
And then i have a question too.
Do I have to set up a page directory for such a task?

Greetings
Christian

Posted: Thu Dec 27, 2007 12:05 pm
by ChristianF
Ok
after trying a little bit myself if worked out this function:

Code: Select all

//Get the segment from the far pointer \p fp.
#define FP_SEG(fp)      (((u32int)fp) >> 16)
//Get the offset from the far pointer \p fp.
#define FP_OFF(fp)      (((u32int)fp) & 0xffff) 


void create_v86_task(entry_t thread, u32int cs, const char *name)
{
	// If there was no previous creation of an v86 task...
	if(initialized == 1)
	{
		// initialize the TSS and set the v86monitor
		/* Set the isr 13 (General Protection Fault)
		 * (13 because you start counting with 0)
		 * to the v86_monitor which emulates the real mode operations
		 */
		idt_set_gate(13, (unsigned)v86_monitor, 0x08, 0x8E);
		/* 
		 * Sets a dummy tss which will be used from every task by copying the
		 * content from the local tss within the thread structure to this dummy tss.
		 */
		set_tss();
		initialized = 1;
	}
	struct process_element *pE = NULL;
	struct process_data *pD		= NULL;
	u32int *stack;
	int x = strlen(name);
	
	pE = create_process_element();
	pD = create_process_data();
	
	add_data_to_element(pE, pD);
	add_element_to_list(pE, head_process);
	
	pE->data->thread.esp0 = 0;
	pE->data->thread.esp1 = 0;
	pE->data->thread.esp2 = 0;
	
	pE->data->thread.esp3 = kmalloc(4096)+4096;
	stack = (u32int *)pE->data->thread.esp3;	
	
	/* v86 specific registers */
	*(--stack) = FP_SEG(USER_DATA_SEL);	// gs
	*(--stack) = FP_SEG(USER_DATA_SEL);	// fs
	*(--stack) = FP_SEG(USER_DATA_SEL);	// ds
	*(--stack) = FP_SEG(USER_DATA_SEL);	// es
	*(--stack) = FP_SEG(USER_DATA_SEL);	// ss
	*(--stack) = 0;	/* ToDo */	// sp
	*(--stack) = EFLAGS_V86;		// eflags
	*(--stack) = FP_SEG(USER_CODE_SEL);	// cs
	*(--stack) = FP_SEG(thread);	// eip
	/* general purpose registers */
	*(--stack) = 0;	// eax
	*(--stack) = 0;	// ebx
	*(--stack) = 0;	// ecx
	*(--stack) = 0;	// edx
	*(--stack) = 0;	// ebp
	*(--stack) = 0;	// esp
	*(--stack) = 0;	// esi
	*(--stack) = 0;	// edi
	/* segment registers */
 	*(--stack) = FP_SEG(KERNEL_DATA_SEL);	// ds
   *(--stack) = FP_SEG(KERNEL_DATA_SEL);	// es
   *(--stack) = FP_SEG(KERNEL_DATA_SEL);	// fs
 	*(--stack) = FP_SEG(KERNEL_DATA_SEL);	// gs
 	*(--stack) = FP_SEG(KERNEL_DATA_SEL);	// ss
 	*(--stack) = FP_SEG(KERNEL_CODE_SEL);	// cs
 	
	
	pE->data->thread.esp0 = 0;
	pE->data->thread.esp1 = 0;
	pE->data->thread.esp2 = 0;
	pE->data->thread.esp3 = (u32int)stack;
	
	pE->data->pid = (++pid);
	pE->data->state = TASK_RUNABLE;
	pE->data->thread.v86 = 1;
	
	pE->data->name = (unsigned char *)kmalloc(sizeof(char)*x);
	memcpy(pE->data->name, name, sizeof(char)*x);
	pE->data->thread.v86_tss = (tss_t *)kmalloc(sizeof(tss_t));
	pE->data->state = TASK_RUNABLE;
	memset(pE->data->thread.v86_tss, 0, sizeof(tss_t));
	
	pE->data->thread.v86_tss->eflags		= 0x20202L | 0x4000;
	pE->data->thread.v86_tss->cs			= USER_CODE_SEL|3;
	pE->data->thread.v86_tss->ss			= USER_DATA_SEL|3;
	pE->data->thread.v86_tss->es			= USER_DATA_SEL|3;
	pE->data->thread.v86_tss->ds			= USER_DATA_SEL|3;
	pE->data->thread.v86_tss->fs			= USER_DATA_SEL|3;
	pE->data->thread.v86_tss->gs			= USER_DATA_SEL|3;
	pE->data->thread.v86_tss->eip			= (u32int)thread;
	pE->data->thread.v86_tss->bitmap		= (u16int)sizeof(tss_t);
}
But now i get this message from Bochs:

Code: Select all

00018636457e[CPU0 ] iret: return CS selector null
Why do I get this Message?

Here is the rest until the software reset was executed.

Code: Select all

00018645475i[CPU0 ] protected mode
00018645475i[CPU0 ] CS.d_b = 32 bit
00018645475i[CPU0 ] SS.d_b = 32 bit
00018645475i[CPU0 ] EFER   = 0x00000000
00018645475i[CPU0 ] | RAX=0000000000000000  RBX=0000000000000000
00018645475i[CPU0 ] | RCX=0000000000000000  RDX=0000000000000000
00018645475i[CPU0 ] | RSP=00000000bfffffee  RBP=0000000000000000
00018645475i[CPU0 ] | RSI=0000000000000000  RDI=0000000000000000
00018645475i[CPU0 ] |  R8=0000000000000000   R9=0000000000000000
00018645475i[CPU0 ] | R10=0000000000000000  R11=0000000000000000
00018645475i[CPU0 ] | R12=0000000000000000  R13=0000000000000000
00018645475i[CPU0 ] | R14=0000000000000000  R15=0000000000000000
00018645475i[CPU0 ] | IOPL=0 id vip vif ac vm RF nt of df if tf SF zf AF PF cf
00018645475i[CPU0 ] | SEG selector     base    limit G D
00018645475i[CPU0 ] | SEG sltr(index|ti|rpl)     base    limit G D
00018645475i[CPU0 ] |  CS:0008( 0001| 0|  0) 00000000 000fffff 1 1
00018645475i[CPU0 ] |  DS:0000( 0000| 0|  0) 00000000 000fffff 1 1
00018645475i[CPU0 ] |  SS:0010( 0002| 0|  0) 00000000 000fffff 1 1
00018645475i[CPU0 ] |  ES:0000( 0000| 0|  0) 00000000 000fffff 1 1
00018645475i[CPU0 ] |  FS:0000( 0000| 0|  0) 00000000 000fffff 1 1
00018645475i[CPU0 ] |  GS:0000( 0000| 0|  0) 00000000 000fffff 1 1
00018645475i[CPU0 ] |  MSR_FS_BASE:0000000000000000
00018645475i[CPU0 ] |  MSR_GS_BASE:0000000000000000
00018645475i[CPU0 ] | RIP=000000000010260f (000000000010260f)
00018645475i[CPU0 ] | CR0=0x80000011 CR1=0x0 CR2=0x00000000bfffffea
00018645475i[CPU0 ] | CR3=0x0010a000 CR4=0x00000000
00018645475i[CPU0 ] >> mov eax, dword ptr ds:0x107000 : A100701000
00018645475e[CPU0 ] exception(): 3rd (14) exception with no resolution, shutdown status is 00h, resetting
00018645475i[SYS  ] bx_pc_system_c::Reset(SOFTWARE) called
Greetings
Christian

Posted: Thu Dec 27, 2007 4:35 pm
by Combuster
Have you made sure that TSS.ESP0 and TSS.SS0 are set properly? as far as I can see they're both 0 (hence the SS = NULL error)