Page 1 of 3

problem with task-switching

Posted: Sun Apr 24, 2005 9:12 am
by DIGIT4L_PUNK
Hi all. I have a problem with task switching. There are 2 tasks: SYS_TSS and USR_TSS. I successfully switch from SYS_TSS to USR_TSS. USR_TSS checks for NT-bit set and for USR_TSS.LINK = SYS_TSS, it finds no problems. then it does IRET and REBOOTS. Please, help me :-\

Re:problem with task-switching

Posted: Sun Apr 24, 2005 11:09 am
by DIGIT4L_PUNK
ok, here's some more info:
USR_TSS states that:

usr_tss.link = SYS_TSS
cpu.eflags = 0x00004002
sys_tss.eflags = 0x00000046

I have added int00-1F. and it generated int 0x0A (invalid TSS) after IRET. the interrupt handler says:

'int 0A at 0008:00100089 eflgs: 00460000 code: 0000'

0008:00100089 is the address of the first instruction of SYS_TSS after return. and what about eflags = 00460000?!!
as you guess it might be 00000046... I have checked the exception handler code, but found no errors. here's the code, maybe someone can help me:

; al = interrupt number
exception_handler:
???mov???dword[.msg_int + 46],'0000'
???push???word gdt.linear_data_sel
???pop???ds
???call???byte2hex ; al = byte
???mov???word [.msg_int + 04],bx ; bx = hex byte

???cmp???al,0x07
???jle???.no_code
???cmp???al,0x09
???je???.no_code
???cmp???al,0x0f
???je???.no_code
???cmp???al,0x10
???je???.no_code
???cmp???al,0x12
???je???.no_code
???cmp???al,0x13
???je???.no_code

???pop???ax ; <- error code from stack
???call???word2hex
???mov???dword[.msg_int + 46],ebx
.no_code:
???mov???eax,dword[ss:esp + 0] ; <- eip
???call???dword2hex
???mov???dword[.msg_int + 15],ebx
???mov???dword[.msg_int + 19],ecx
???mov???ax,word [ss:esp + 4] ; <- cs
???call???word2hex
???mov???dword[.msg_int + 10],ebx
???mov???eax,dword[ss:esp + 6] ; < - eflags
???call???dword2hex
???mov???dword[.msg_int + 31],ebx
???mov???dword[.msg_int + 35],ecx
???lea???esi,[.msg_int]
???mov???bl,0x4f
???call???disp_str
???iret

.msg_int???db 'int 00 at 0000:00000000 eflgs: 00000000 code: 0000',13,10,0

PLEASE, HELP ME ??? :-\ :-[ :'(

Re:problem with task-switching

Posted: Sun Apr 24, 2005 11:49 am
by Brendan
Hi,

Code: Select all

   mov   eax,dword[ss:esp + 6]          ; < - eflags
   call   dword2hex
   mov   dword[.msg_int + 31],ebx
Try replacing the first instruction above with "mov eax,dword[ss:esp + 8]" - it
should display the correct value for eflags after that.

As for your task switching problem, I've got no idea. How do you switch tasks, do you do an "LDTR" during boot (before the first task switch), and can you display (e.g. with Bochs debugger) the contents of both TSSs immediately after it crashes?

One suggestion would be to take a look at the description of the invalid TSS exception in Intel's programmer's guide. The guide lists possible causes for the exception - it's possible to use this list and check each cause, crossing them off as you go until you find the problem.


Cheers,

Brendan

Re:problem with task-switching

Posted: Mon Apr 25, 2005 1:04 pm
by DIGIT4L_PUNK
Try replacing the first instruction above with "mov eax,dword[ss:esp + 8]" - it
should display the correct value for eflags after that.
yes, but it shouldn't need to be replaced, because everithing is 32-bit, code, data, tss-es, everything.

I do not use LDTR, I use task.ldtr = 0x0000, which doesn't (or shouldn't) cause any problems.

I've seen the int 0x0a description, I've checked the descriptors and TSS-es at runtime, but faund nothing useful. I'm getting nervious!!! >:( maybe my CPU has some bug??!!
and, there's also some strange thing, I do the same thing in DOS, and have no problems. strange.... ??? ::)

Re:problem with task-switching

Posted: Mon Apr 25, 2005 3:53 pm
by Brendan
Hi,
DIGIT4L_PUNK wrote:
Try replacing the first instruction above with "mov eax,dword[ss:esp + 8]" - it
should display the correct value for eflags after that.
yes, but it shouldn't need to be replaced, because everithing is 32-bit, code, data, tss-es, everything.
Sure - it'll load 32 bits from "esp+6" instead of 32 bits from "esp+8". It's still loading 32 bits, but it's the wrong 32 bits. Did you change the line to see if it had any effect?
DIGIT4L_PUNK wrote:I do not use LDTR, I use task.ldtr = 0x0000, which doesn't (or shouldn't) cause any problems.
Using "task.ldtr = 0x0000" doesn't/shouldn't cause any problems, but it probably doesn't do anything useful either.

When the CPU does a hardware task switch, it saves the existing CPU state into the current TSS and then loads the new task's state into the CPU. To do this it needs to know where the previous state must be saved. Inside the CPU there's a register called the TR (Task Register), which it uses find the current TSS to save the data. A hardware task switch looks like this:

- use TR to find the previous TSS
- save the CPU state into the previous TSS
- use the value from whatever triggered the task switch to find the address of the new TSS
- load this new value into TR
- load new CPU state from the new TSS

This means that after the first task switch TR operates automatically, but for the first task switch the CPU has no idea where to save the old state. To get around this problem there's a special instruction, "LDTR".

Because you don't use the LDTR instruction the CPU won't be able to figure out where to save the CPU state during the first task switch, and will generate an "Invalid TSS" exception when trying to save that state.

Please do not confuse the "LDTR" instruction with the "LLDT" instruction. The LLDT instruction isn't needed (unless you use selector that points to a descriptor in the LDT instead of the GDT), and you can set the LDT field in the TSS's instead of using LLDT (as long as you don't use any descriptors in the LDT before the first task switch).

Also note that there are no compilers that can generate the LDTR instruction without assembly language (e.g. inline assembly).


Cheers,

Brendan

Re:problem with task-switching

Posted: Mon Apr 25, 2005 11:22 pm
by Candy
Brendan wrote: This means that after the first task switch TR operates automatically, but for the first task switch the CPU has no idea where to save the old state. To get around this problem there's a special instruction, "LDTR".
<and on>
That would be LTR. LDTR had me confused as well.

Re:problem with task-switching

Posted: Tue Apr 26, 2005 3:45 am
by Brendan
Hi,
Candy wrote:That would be LTR. LDTR had me confused as well.
My apologies to all - surprising how quickly you forget some things but remember others (I may have had a flashback to 6502 assembly there ::))..


Cheers,

Brendan

Re:problem with task-switching

Posted: Wed Apr 27, 2005 1:57 am
by DIGIT4L_PUNK
That would be LTR. LDTR had me confused as well.
::)
ofcourse I do use LTR instruction. if not, it would be unable to switch from SYS_TSS to USR_TSS and USR_TSS.link wouldn't be equal to SYS_TSS.

??? ::)

Re:problem with task-switching

Posted: Wed Apr 27, 2005 2:49 am
by distantvoices
I have difficulty understanding the mechanisms you use.

Why do you manipulate the stack if doing the HW TS thingy?

you need to fill in a tss, which you allocate either on heap or in any array - as you like - and for which you have to keep a tss segment in your gdt (at least, there are some more possibilities). So you need an entry for your system tss and an entry for your user tss.

Then, after having filled in the tss'es with proper values for esp0/ss0 esp3/ss3 and eflags, cs and eip you say

Code: Select all

ltr [system_tss selector]
after that, if you want to switch tasks:

Code: Select all

jmp [your_tss_selector:0]
you can do this in your isr stub. Then you iret to the new(old?) task.

Hope this helps a bit. I have more experience with software task switching, but I've done HW TS too. It's not too difficult once one's wrapped some brain around the TSS TSS segemnt thing and the jmp [tss_selector:0]

you must not say LTR [segment] more than once. *gg* The rest is done via jmp's.

Re:problem with task-switching

Posted: Wed Apr 27, 2005 3:26 am
by DIGIT4L_PUNK
ok. here's my mechanism:

1. set-up 2 TSS segments:
gdt.SYS_TSS.limit=0x67
gdt.SYS_TSS.base=[sys_tss]
gdt.SYS_TSS.type=0x89
gdt.SYS_TSS.flgs=0x00

gdt.USR_TSS.limit=0x67
gdt.USR_TSS.base=[usr_tss]
gdt.USR_TSS.type=0x89
gdt.USR_TSS.flgs=0x00

sys_tss : everithing set to 0
usr_tss : tss.cs = 0x08 <code segment>
and other seg-regs set to 0x10 <data segment>

2. mov ax,gdt.SYS_TSS
ltr ax

3. lea eax,[TASK_A]
mov dword[usr_tss.eip],eax
mov dword[usr_tss.esp],esp

4. call gdt.USR_TSS:0 (maybe I should use JMP instead of call)
it does task-switch to gdt.USR_TSS and dumps current
CPU-regs into gdt.SYS_TSS (which points to [sys_tss])

5. gdt.USR_TSS doenst do anything suspicious...

6. IRET... and here it goes, INT 0x0A with CS:EIP (on stack) pointing to the first instruction of gdt.SYS_TSS (after return)

Re:problem with task-switching

Posted: Wed Apr 27, 2005 7:10 am
by Brendan
Hi,

I did about 4 kernels using hardware task switching (the last of these would have been around 8 years ago), so I'm a bit rusty - as you've probably already guessed :).

Start with Intel's System Programmer's Reference Guide, Table 6-1 "Exception Conditions Checked During a Task Switch". You can ignore everything that's checked before the CPU commits to the task switch, because the exception is generated in the context of the new task rather than in the context of the old task. You can also ignore any checks that don't generate an invalid TSS exception. This leaves the following list (I've included the contents of the exception's error code in brackets):

1. LDT segment selector of new task is invalid (new LDT)
2. CS DPL matches segment selector RPL (new CS)
3. SS selector is valid (new SS)
4. SS DPL matches CPL (new SS)
5. LDT of new task is present in memory (new LDT)
6. CS selector is valid (new CS)
7. SS DPL matches selector RPL (new SS)
8. DS, ES, FS and GS selectors are valid (new data segment)
9. DS, ES, FS and GS are readable (new data segment)
10. DS, ES, FS and GS DPL greater than or equal to CPL unless they're conforming segments (new data segment)

One of the checks listed must be causing your problem - you'd need to go through each one (eliminate each check until your left with the cause). To start with I'd find out what the exception's error code contains (but don't be surprised if the error code is 0x00000000).

I'd also recommend using an indirect jump to switch tasks, rather than call or iret. I used to use something like:

Code: Select all

    .section data
TSSjump:
TSSoffset:
    dd 0x00000000
TSSselector:
    dw 0x00
    .section text
And then:

Code: Select all

    cmp [currentTSS],ax      ;Is this task already running?
    je .skip                 ; Yes, skip the actual task switch
    mov [currentTSS],ax      ;Set new TSS to jump to
    jmp far [TSSjump]
.skip:
I also used to use the TSS's selector as a "thread ID", so that if the kernel needs to know which task is currently running it can use the value at "TSSselector".


Cheers,

Brendan

Re:problem with task-switching

Posted: Thu Apr 28, 2005 6:31 am
by DIGIT4L_PUNK
new info: I have added new function gdt.dump_selector, which displays selector descriptor contents. so I found the error. USR_TSS.type = 0x8b (busy 32-bit TSS) and SYS_TSS.type=0x8b (<- it should be 0x89) so here's the reason for the INT 0x0A: 'Switch to busy TSS', but I don't understand why should SYS_TSS-s busy bit be set to 1? ???

Re:problem with task-switching

Posted: Thu Apr 28, 2005 6:54 am
by distantvoices
I suppose you clear the busy bit of the "new" task ere you switch tasks. Admittedly it's a bit of a hack.

have you tried to "jmp" to a task instead of "call"ing it?

Re:problem with task-switching

Posted: Thu Apr 28, 2005 7:01 am
by DIGIT4L_PUNK
yes, but it still generated int 0x0A. i'll try it again...

Re:problem with task-switching

Posted: Thu Apr 28, 2005 2:36 pm
by DIGIT4L_PUNK
well, there are three interesting points in code:
1. before switching to USR_TSS
2. inside USR_TSS (TASK_A)
3. after switching back to SYS_TSS

I have placed there some calls to display descriptor contents, so here are the results:

1. before switching to USR_TSS:
SYS_TSS.type=0x8B (busy 386 TSS)
USR_TSS.type=0x89 (free 386 TSS)

2. inside USR_TSS (TASK_A):
SYS_TSS.type=still 0x8B (!!!!!!)
USR_TSS.type=0x8B

<INT 0x0A generated after IRET instruction>

3. after switching back to SYS_TSS:
SYS_TSS.type=0x8B
USR_TSS.type=0x89

as you can see, int 0x0A is generated because of switch to busy SYS_TSS (type 0x8B) but I don't understand, why doesn't the CPU clear the busy bit in SYS_TSS?!!! if it does it in case of USR_TSS...