Page 2 of 2

Posted: Sun Dec 30, 2007 5:26 am
by ChristianF
Ok i forgott that.
But now i have a little question:
When I switch to the Ring3 Task, I get an Error like this:

Code: Select all

get_SS_ESP_from_TSS: TR is bogus type (3)
So do I have to jump to the TSS before switching to the Task?

This is the Code for initializing a dummy TSS.

Code: Select all

void set_tss(void)
{
	unsigned long base	= (unsigned long)&v86_task;
	unsigned long limit	= base + sizeof(tss_t);
	unsigned short trace = 5<<3;

	memset(&v86_task, 0, sizeof(tss_t));
	
	v86_task.bitmap	= (u16int)sizeof(tss_t);
	v86_task.ss0		= KERNEL_DATA_SEL;

	gdt_set_gate(5, base, limit, /*0x89*/0xE9, 0x67);
	asm ( "ltr %%ax"::"a"(trace) );
	gdt_flush();
}
And this is the Code for the Task Switch:

Code: Select all

u32int switch_task(u32int OldEsp)
{
	// Initialise return_esp with the old esp
	u32int return_esp = OldEsp;
	if(current_process == NULL)
	{
		if(head_process == NULL)
		{
			return_esp = OldEsp;
		}
		else
		{
			if(head_process->first == NULL && head_process->last == NULL)
			{
				return_esp = OldEsp;
			}
			else
			{
				current_process = head_process->first;
				if(current_process->data->thread.v86 == 1)
				{
					memcpy(&v86_task, current_process->data->thread.v86_tss, sizeof(tss_t));
					return_esp = current_process->data->thread.esp3;
				}
				else
				{
					return_esp = current_process->data->thread.esp0;
				}
			}
		}
	}
	else
	{
		if(current_process->data->thread.v86 == 1)
		{
			current_process->data->thread.esp3 = OldEsp;
		}
		else
		{
			current_process->data->thread.esp0 = OldEsp;
		}
		if(current_process->next == NULL)
		{
			if(current_process->data->thread.v86 == 1)
			{
				memcpy(current_process->data->thread.v86_tss, &v86_task, sizeof(tss_t));
			}
			current_process = head_process->first;
			if(current_process->data->thread.v86 == 1)
			{
				memcpy(&v86_task, current_process->data->thread.v86_tss, sizeof(tss_t));
				return_esp = current_process->data->thread.esp3;
			}
			else
			{
				return_esp = current_process->data->thread.esp0;
			}
		}
		else
		{
			if(current_process->data->thread.v86 == 1)
			{
				memcpy(current_process->data->thread.v86_tss, &v86_task, sizeof(tss_t));
			}
			current_process = current_process->next;
			if(current_process->data->thread.v86 == 1)
			{
				memcpy(&v86_task, current_process->data->thread.v86_tss, sizeof(tss_t));
				return_esp = current_process->data->thread.esp3;
			}
			else
			{
				return_esp = current_process->data->thread.esp0;
			}
		}
	}
	return(return_esp);
}
Is this the right way or is something wrong in my code?

greetings
Christian

Posted: Sun Dec 30, 2007 2:13 pm
by ChristianF
Ok I got it now.
I moved 0x08 to cs at every Task Switch.
This is the code:

Code: Select all

; 32: IRQ0
_irq0:
	pusha          ;Push all standard registers
	push cs			;Push segment c
	push ds        ;Push segment d
	push es        ;Push segment e
	push fs        ;Push segment f
 	push gs        ;Push segment g

 	mov eax, 0x10  ;Get kernel data segment
 	mov ds, eax    ;Put it in the data segment registers
 	mov es, eax
	mov fs, eax
	mov gs, eax
	
	mov eax, 0x08
	mov cs, eax

 	push esp            ;Push pointer to all the stuff we just pushed
 	call _switch_task   ;Call C code

 	mov esp, eax   ;Replace the stack with what the C code gave us

 	mov al, 0x20   ;Port number AND command number to Acknowledge IRQ
 	out 0x20,al

 	pop gs         ;Put the data segments back
 	pop fs
 	pop es
 	pop ds
 	pop cs
 	popa           ;Put the standard registers back
 	;We didn't push an error code or IRQ number, so we don't have to edit esp now
 	iret           ;Interrupt-Return

But now every Time it switches to Ring 3 I get an invalide Opcode Exception.
When I comment out these two lines:

Code: Select all

mov eax, 0x08
mov cs, eax
I got an error from bochs that cs is null.
What is wrong?

cheers
Christian

Posted: Sun Dec 30, 2007 4:15 pm
by Combuster
You can never load CS directly, and you can not load CS without loading IP as well.
Also, a segment register is 16 bits. Loading it with a 32-bits value is wrong.

hence,

Code: Select all

pop CS
and

Code: Select all

mov CS, eax
should not occur in any sane assembly code.

Since I get the idea that you have no clue what you're doing, I suggest you do the following:
- get the intel manuals. Look up iret in the instruction set manual
- read the chapter on exceptions and interrupts at least twice.
- install the debugger version of bochs and learn how to use it.

Posted: Tue Jan 01, 2008 3:00 pm
by ChristianF
Ok
I got it.
Now the Task generates a general protection fault.
But there is a last little question:
When I switch to a vm86 Task I copy the TSS from the Task to the dummy TSS. But now i get this bochs message again:

Code: Select all

00021982838e[CPU0 ] iret: return CS selector null
I set ss0 and ESP0 like this:

Code: Select all

pE->data->thread.v86_tss->esp0	= KERNEL_DATA_SEL|3;
pE->data->thread.v86_tss->ss0		= KERNEL_DATA_SEL|3;
So i have to ask the last question: Is this the right way?

Cheers
Christian