Is there any easy step-by-step tutorial of TSS? (v2)

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
InsightSoft
Member
Member
Posts: 76
Joined: Mon Aug 18, 2008 6:17 am

Re: Is there any easy step-by-step tutorial of TSS? (v2)

Post by InsightSoft »

Code: Select all

dtVoid _cls_baselayer::DispatchMessage()
{
	_Task00.Id			= 0;
	_Task00.Name		= "BaseLayer.DispatchMessage";
	_Task00.Description = "BaseLayer DispatchMessages";
	_Task00.OwnerId		= (dtUInteger32Bits)this;
	_Task00.TSS.EIP		= (dtUInteger32Bits)&__BaseLayer_DispatchMessage;								
	__asm																								
	{
		push eax																																			
		  mov eax, cr3
		  mov _Task00.TSS.CR3,    eax
		  pushfd
		  pop eax
		  mov _Task00.TSS.EFlags, eax
		pop eax
		mov _Task00.TSS.EAX,  eax																	//general proposer									
		mov _Task00.TSS.EBX,  ebx
		mov _Task00.TSS.ECX,  ecx
		mov _Task00.TSS.EDX,  edx
		mov _Task00.TSS.ESP,  esp
		mov _Task00.TSS.EBP,  ebp
		mov _Task00.TSS.ESI,  esi																		//indexes									
		mov _Task00.TSS.EDI,  edi	
		mov _Task00.TSS.ES,	  es																		//segments
		mov _Task00.TSS.CS,   cs
		mov _Task00.TSS.SS,   ss
		mov _Task00.TSS.DS,   ds
		mov _Task00.TSS.FS,   fs
		mov _Task00.TSS.GS,   gs
	}
	_Task00.Install(3, 0x89, 0xC0);
	_Task00.Start();

Code: Select all

dtVoid _cls_task::Install(dtUInteger16Bits gdtIndex, dtUInteger08Bits access, dtUInteger08Bits granularity)
{
	if(!this->isInstalled)
	{
		BaseLayer.gdt.SetEntry(gdtIndex, (dtUInteger32Bits)&this->TSS, ((dtUInteger32Bits)&this->TSS) + ((sizeof(this->TSS)-1)), access, granularity);
                this->isInstalled=true;
		this->Index = gdtIndex * 8;
	}
}

Code: Select all

dtVoid _cls_task::Start()
{
	dtUInteger16Bits	_gdtIndex, _cs, _ss;
	dtUInteger32Bits	_eip, _eflags;

	if(this->isInstalled || !this->isRunning)
	{
		this->State		= TASK_STATE_RUNNIG;
		this->isRunning = true;
		_gdtIndex		= this->Index;
		_cs				= this->TSS.CS;
		_ss				= this->TSS.SS;
		_eip			        = this->TSS.EIP;
		_eflags			= this->TSS.EFlags;
		__asm
		{
			ltr		_gdtIndex
// 			push	_ss
// 			push	0
// 			push	_eflags
// 			push	_cs
// 			push	_eip
// 			iretd
		}
		_executeTask();
	}
	else
	{
		this->State= TASK_STATE_INVALID;
	}
}

This is only an experience

Code: Select all

.386p
.MODEL Flat, C

.CODE

	_executeTask	proc
		db  9Ah																                    
		dd  0														
		dw  18h
		ret                                                                     
	_executeTask	endp
END
ru2aqare
Member
Member
Posts: 342
Joined: Fri Jul 11, 2008 5:15 am
Location: Hungary

Re: Is there any easy step-by-step tutorial of TSS? (v2)

Post by ru2aqare »

InsightSoft wrote:Yap... you are right... (cs, eip, eflags, etc)

But Im struggling with TSS. Im concerning more about the steps taken.

::start task1

Code: Select all

1. LTR ax (ax=index on GDT)
2. a far jmp to that gdt index (offset ignored)
3. tss -> registers
4. task is running
The sequence is wrong. If I remember correctly (I haven't used hardware task switching for quite some time), it should be

Code: Select all

prepare a dummy TSS, will be used only once.
ltr ax with the selector of that TSS
prepare a new TSS, will be used to host a hardware task
far jump to this selector (offset ignored)
reuse first TSS or scrap it
After this, you are running in the context of the second TSS. There is no need to modify TR hereafter. If you want to switch to a new hawrdware task, simply far jump to the selector of its TSS. Also there is no need to save/restore the stack before or after the task switch. When control gets back to the instruction after the far jump, every register is restored by the processor. Just like if the far jump never happened. This is in contrast with software multitasking (stack-based multitasking), where you use one TSS (that rules all :)) and have to save/restore the stack and (at least nonvolatile) registers manually.
InsightSoft
Member
Member
Posts: 76
Joined: Mon Aug 18, 2008 6:17 am

Re: Is there any easy step-by-step tutorial of TSS? (v2)

Post by InsightSoft »

ru2aqare wrote:
InsightSoft wrote: After this, you are running in the context of the second TSS. There is no need to modify TR hereafter. If you want to switch to a new hawrdware task, simply far jump to the selector of its TSS. Also there is no need to save/restore the stack before or after the task switch. When control gets back to the instruction after the far jump, every register is restored by the processor. Just like if the far jump never happened. This is in contrast with software multitasking (stack-based multitasking), where you use one TSS (that rules all :)) and have to save/restore the stack and (at least nonvolatile) registers manually.

Well, actually I already have my MT up and running using save/restore stack-based... but I want try to use intel facility (better performance)...
...on my sequences, the steps 3 and 4 are automatic... I will remove the TR update (step 1)...

Ooops...

Code: Select all

(0) [0x00100b26] 0008:0000000000100b26 (unk. ctxt): jmp far 0018:00000000     ; ea000000001800
00023190252e[CPU0 ] jump_protected: gate type 11 unsupported
I don't understand why i get this error...
ru2aqare
Member
Member
Posts: 342
Joined: Fri Jul 11, 2008 5:15 am
Location: Hungary

Re: Is there any easy step-by-step tutorial of TSS? (v2)

Post by ru2aqare »

InsightSoft wrote: Ooops...

Code: Select all

(0) [0x00100b26] 0008:0000000000100b26 (unk. ctxt): jmp far 0018:00000000     ; ea000000001800
00023190252e[CPU0 ] jump_protected: gate type 11 unsupported
I don't understand why i get this error...
If don't know offhand what gate type 11 is, but are you sure you are not jumping to a busy TSS?
InsightSoft
Member
Member
Posts: 76
Joined: Mon Aug 18, 2008 6:17 am

Re: Is there any easy step-by-step tutorial of TSS? (v2)

Post by InsightSoft »

ru2aqare wrote:
If don't know offhand what gate type 11 is, but are you sure you are not jumping to a busy TSS?
I want to resume a task that was suspended...
ru2aqare
Member
Member
Posts: 342
Joined: Fri Jul 11, 2008 5:15 am
Location: Hungary

Re: Is there any easy step-by-step tutorial of TSS? (v2)

Post by ru2aqare »

InsightSoft wrote:
ru2aqare wrote:
If don't know offhand what gate type 11 is, but are you sure you are not jumping to a busy TSS?
I want to resume a task that was suspended...
Still, the descriptor of that TSS should be available, not busy. A busy TSS is the hardware task the processor is currently executing, if I remember correctly.
InsightSoft
Member
Member
Posts: 76
Joined: Mon Aug 18, 2008 6:17 am

Re: Is there any easy step-by-step tutorial of TSS? (v2)

Post by InsightSoft »

ru2aqare wrote:
InsightSoft wrote:
ru2aqare wrote:
If don't know offhand what gate type 11 is, but are you sure you are not jumping to a busy TSS?
I want to resume a task that was suspended...
Still, the descriptor of that TSS should be available, not busy. A busy TSS is the hardware task the processor is currently executing, if I remember correctly.
I'm confused right now... I have two different task up and running (each with its own TSS)
when the task 2 take place, the system saves automatically the sate of the task 1 (right?).
To back to the task 1. Should I clear the bit P???

(This two task are currently running the ring 0).
ru2aqare
Member
Member
Posts: 342
Joined: Fri Jul 11, 2008 5:15 am
Location: Hungary

Re: Is there any easy step-by-step tutorial of TSS? (v2)

Post by ru2aqare »

InsightSoft wrote:I'm confused right now... I have two different task up and running (each with its own TSS)
when the task 2 take place, the system saves automatically the sate of the task 1 (right?).
That's right. Whenever you far jump to another task, the processor saves its state to the current TSS (pointer to by the TR), loads its new state from the new TSS (which must be available - busy TSS will result in #GP), and continues execution.
InsightSoft wrote: To back to the task 1. Should I clear the bit P???

(This two task are currently running the ring 0).
I have no idea what you are refering to here.
InsightSoft
Member
Member
Posts: 76
Joined: Mon Aug 18, 2008 6:17 am

Re: Is there any easy step-by-step tutorial of TSS? (v2)

Post by InsightSoft »

Please, lets see a big picture (Im getting more confused than I was before)

Code: Select all

I have 2 entries on GDT reserved for TSS (just two simple endless loops)
(0=reserved; 1=4gb/32bits/code; 2=4gb/32bits/data; 3=TSS/32bits task 1; 4=TSS/32bits/task 2)
task 1: creating first run (gdt:3)

Code: Select all

-configure the values of TSS records
-load TR
-jmp to that gdt index (and... is running)
then, time expired. Its time to move to next task

task 2: creating first run (gdt:4)

Code: Select all

-configure the values of TSS records
-load TR
-jmp to that gdt index (and... is running)
then, time expired. Its time to move back to task 1

task 1: resume

Code: Select all

TR still pointing to gdt 4. should I change to gdt:3?
how to back to task 1? Simple far jmp to the gdt index 3? jmp using saved cs:eip? (on tss saved? (regarding to gdt:3))
Im really lost at this stage... I just want to put system switching between this two endless loops...
ru2aqare
Member
Member
Posts: 342
Joined: Fri Jul 11, 2008 5:15 am
Location: Hungary

Re: Is there any easy step-by-step tutorial of TSS? (v2)

Post by ru2aqare »

InsightSoft wrote:task 1: resume

Code: Select all

TR still pointing to gdt 4. should I change to gdt:3?
how to back to task 1? Simple far jmp to the gdt index 3? jmp using saved cs:eip? (on tss saved? (regarding to gdt:3))
Im really lost at this stage... I just want to put system switching between this two endless loops...
No, you dont need to touch TR once it was set by some initialization code. You should leave it as selector 4, and perform a simple far jump to selector 3. For example,

Code: Select all

  push selector
  push 0
  retf
or

Code: Select all

  push selector
  call switch_task
  ...

switch_task proc near
  jmp far ptr [esp+0] ; make use of the fact that offset is ignored, and caller's EIP can be used as offset
  retn
switch_task endp
Hope this helps.
InsightSoft
Member
Member
Posts: 76
Joined: Mon Aug 18, 2008 6:17 am

Re: Is there any easy step-by-step tutorial of TSS? (v2)

Post by InsightSoft »

Thanks,
but this is my big problem... it is exactly what Im doing right now...
when jmps to, I get the bochs error: "jump_protected: gate type 11 unsupported" (reading the bochs source code: this is a default switch escape message)
InsightSoft
Member
Member
Posts: 76
Joined: Mon Aug 18, 2008 6:17 am

Re: Is there any easy step-by-step tutorial of TSS? (v2)

Post by InsightSoft »

ru2aqare wrote:
InsightSoft wrote:task 1: resume

Code: Select all

TR still pointing to gdt 4. should I change to gdt:3?
how to back to task 1? Simple far jmp to the gdt index 3? jmp using saved cs:eip? (on tss saved? (regarding to gdt:3))
Im really lost at this stage... I just want to put system switching between this two endless loops...
No, you dont need to touch TR once it was set by some initialization code. You should leave it as selector 4, and perform a simple far jump to selector 3. For example,

Code: Select all

  push selector
  push 0
  retf
the pushed selector is the TSS??? or is the CS of task???
ru2aqare
Member
Member
Posts: 342
Joined: Fri Jul 11, 2008 5:15 am
Location: Hungary

Re: Is there any easy step-by-step tutorial of TSS? (v2)

Post by ru2aqare »

InsightSoft wrote: the pushed selector is the TSS??? or is the CS of task???
The selector of the TSS. If it would be the selector of the cs: descriptor, the far jump wouldn't be a task switch, now would it?
when jmps to, I get the bochs error: "jump_protected: gate type 11 unsupported" (reading the bochs source code: this is a default switch escape message)
Check your descriptors, I can't say anything more specific.

edit: checked my sources. gate type 11 is the busy TSS descriptor. Are you sure you are not loading/reloading TR with the selector of the target task's TSS?

Code: Select all

...
DescAR1SegTSS32B        equ     0Bh             ; 32bit busy TSS (system segment) descriptor
DescAR1Reserved0A       equ     0Ah             ; reserved
DescAR1SegTSS32A        equ     09h             ; 32bit available TSS (system segment) descriptor
...
InsightSoft
Member
Member
Posts: 76
Joined: Mon Aug 18, 2008 6:17 am

Re: Is there any easy step-by-step tutorial of TSS? (v2)

Post by InsightSoft »

ru2aqare wrote: edit: checked my sources. gate type 11 is the busy TSS descriptor. Are you sure you are not loading/reloading TR with the selector of the target task's TSS?

Code: Select all

...
DescAR1SegTSS32B        equ     0Bh             ; 32bit busy TSS (system segment) descriptor
DescAR1Reserved0A       equ     0Ah             ; reserved
DescAR1SegTSS32A        equ     09h             ; 32bit available TSS (system segment) descriptor
...
No. Im not reloading TR. I only use it when the task is created.
the definition of my descriptors (for TSS)

byte 5=10001001b (0x89h) p;dpl;s;x;0;b;1
byte 6=00000000b (0x00h) g;0;0;avl;upper nible of size
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Is there any easy step-by-step tutorial of TSS? (v2)

Post by Combuster »

using a far return, far jump, far call, or task gate have severe implications on how the task state works.

I do this from memory, but you should *really* look all this up and not do all this shotgun debugging. If something did work out here, you would have learned little from it. And regrettably, you have not been doing your homework AT ALL (and next time you WILL be left without answer)

A far jump will mark the current tss as idle, and the new one as busy (must have been idle)
A far call will mark the new tss as busy (must have been idle), store a pointer in the backlink field, and leaves the original busy as well
An iret (with NT set) will jump to the task in the backlink pointer (must be busy) and marks the current one as idle
A far return may never return to another TSS.


There is a reason why everybody else uses the far more simple software task switching.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply