problem with task-switching

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.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re:problem with task-switching

Post by Brendan »

Hi,
DIGIT4L_PUNK wrote: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...
When you switch from SYS_TSS to USR_TSS you're using a CALL instruction, which shouldn't clear the busy bit for SYS_TSS. This is how CALL works, and is done like this to prevent recursive task switching.

When you switch from USR_TSS to SYS_TSS you're using an IRET instruction, which should clear the busy bit for USR_TSS and return to SYS_TSS. In this case the busy bit for SYS_TSS must be set for a successful return.

From Intel's System Programmer's Guide, Section 6.4.1 "Use of Busy Flag To Prevent Recursive Task Switching":

"When switching to the new task (initiated by a CALL instruction, interrupt, or exception), the processor generates a general-protection exception (#GP) if the busy flag of the new task is already set. (If the task switch is initiated with an IRET instruction, the exception is not raised because the processor expects the busy flag to be set.)"

Now there's 2 things to note here. Firstly your busy bit is behaving exactly how it should for CALL/IRET. Secondly, if it was wrong you'd get a general protection exception, not an invalid TSS exception.

To save all the confusion please use the JMP instruction only (instead of CALL/IRET). You will need to use JMP anyway when you write the scheduler, as the scheduler should be able to switch to any task without caring about the busy bit (or NT flag).


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
DIGIT4L_PUNK

Re:problem with task-switching

Post by DIGIT4L_PUNK »

From Intel's System Programmer's Guide...
where can I find this?

thanks! ;)
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:problem with task-switching

Post by distantvoices »

Google or any search engine of your choice is your friend. Mine has found this quickly ;-)

Here:

http://developer.intel.com/design/Penti ... tm#manuals

check out the section MANUALS.
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
DIGIT4L_PUNK

Re:problem with task-switching

Post by DIGIT4L_PUNK »

OK. I decided to use JMP instruction to switch between tasks. and here's what I got (S = system task, U = user task):

1. (S) SYS_TSS.type=0x8B (busy); USR_TSS.type=0x89 (free)
2. (S) JMP USR_TSS:0
3. (U) SYS_TSS.type=0x89 (free);USR_TSS.type=0x8B (busy)
4. (U) JMP SYS_TSS:0... and here it goes again:INT 0x0A with pointer to the first instruction of SYS_TSS after return.
and TR = SYS_TSS already

WHAT THE HELL IS GOING ON?!! >:( ???
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re:problem with task-switching

Post by Brendan »

Hi,
DIGIT4L_PUNK wrote:WHAT THE HELL IS GOING ON?!! >:( ???
I'd refer you back to the list I posted (derived from Intel's System Programmer's Reference Guide, Table 6-1 "Exception Conditions Checked During a Task Switch"). This is a list of every possible check the CPU does that could be causing your problem.

BTW what is the error code you get from the invalid TSS exception?


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
DIGIT4L_PUNK

Re:problem with task-switching

Post by DIGIT4L_PUNK »

the error code is 0x0000f000 ???
DIGIT4L_PUNK

Re:problem with task-switching

Post by DIGIT4L_PUNK »

here are the conditions being checked:

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)

let's analyze them step-by-step:

1. I don't use LDT, LDTR flag in both TSSs are set to 0x0000
2. all descriptors and selectors are PL 0
3. descriptors are valid, because INT 0x0A resumes execution
without any exceptions and errors.
4. all descriptors and selectors are PL 0
5. I don't use LDT, LDTR flag in both TSSs are set to 0x0000
6. descriptors are valid, because INT 0x0A resumes execution
without any exceptions and errors.
7. all descriptors and selectors are PL 0
8. descriptors are valid, because INT 0x0A resumes execution
without any exceptions and errors.
9. descriptors are valid, because INT 0x0A resumes execution
without any exceptions and errors.
10. I don't use conforming segments

plus, info from my last post.
WHAT THE HELL IS GOING ON?!! >:( ???
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re:problem with task-switching

Post by Brendan »

Hi,
DIGIT4L_PUNK wrote: the error code is 0x0000f000 ???
That makes it easier :).

Do you think 0x0F000 would be CS, SS, a data segment or the LDT? At a guess I'd say that either the TSS descriptor in the GDT isn't right, or something has corrupted the TSS's data (0x0F000 doesn't seem sane).

Easiest thing to do would be to use Bochs debugger to check the TSS descriptor in the GDT, and then check the TSS's data.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
DIGIT4L_PUNK

Re:problem with task-switching

Post by DIGIT4L_PUNK »

I display TSS descriptor contents at every step (before task-switch, from USR_TSS, from INT 0x0A and after switch-back)
they are correct.
something has corrupted the TSS's data
yes, I think this is much real. but exactly which part/item of TSS must be corrupted to have such results? and what about 0xf000. I have changed the exception handler code. I thought that the error code was 16-bits, but it is 32-bits (tell me if I'm wrong). the handler displayed 0x0000 before the change, but now it displayed 0x0000F000... and besides the correction made in the code, EFLAGS on the stack has the offset [ESP + 8] (it should have [ESP + 6])... :-[ ??? ??? ???

About the bochs debugger, I downloaded the source, but I'm a bit lazy with compiling it, I don't even know how to do it or with which compiler ;) I have very slow computer and I wanna know, what's the minimal system requirements for bochs?

Thanks :)
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re:problem with task-switching

Post by Brendan »

Hi,
DIGIT4L_PUNK wrote:yes, I think this is much real. but exactly which part/item of TSS must be corrupted to have such results? and what about 0xf000.
All of the general registers can be trashed without an exception. SS0:ESP0, SS1:ESP1 and SS2:ESP2 aren't used until the CPU changes privilege levels (not during the task switch). Corrupted CR3 would cause a triple fault (if you're using paging). Corrupted EFLAGS won't cause an invalid TSS exception. The IO permission bitmap isn't used until you access an IO port when CPL > IOPL.

It would have to be a segment register (CD, SS, DS, ES, FS, GS) or the LDT.

One possibility would be that the LDT field in the SYS_TSS contains 0x0000F000 before any task switch. The CPU won't change/correct this when it switches from SYS_TSS to USR_TSS (as the CPU assumes the LDT is constant). Later, when the CPU switches from USR_TSS to SYS_TSS it would load the LDT from the TSS's LDT field (which would still be 0x0000F000) and...

If this isn't the case then something is being corrupted - all the segment registers are written during the first task switch, and should be fine for the second task switch.
DIGIT4L_PUNK wrote:I have changed the exception handler code. I thought that the error code was 16-bits, but it is 32-bits (tell me if I'm wrong). the handler displayed 0x0000 before the change, but now it displayed 0x0000F000... and besides the correction made in the code, EFLAGS on the stack has the offset [ESP + 8] (it should have [ESP + 6])... :-[ ??? ??? ???
The CPU enforces a 32 bit aligned stack (if the D/B flag is set to 32 bit in the segment descriptor used by SS, like it should be for a 32 bit OS). For e.g. if you do "PUSH AL" the CPU will automatically use 4 bytes for it, even though it only needs one byte. The same goes for all other operations that use the stack. This means that [ESP + 6] isn't a possibility because it's NOT aligned to 32 bits.
DIGIT4L_PUNK wrote:About the bochs debugger, I downloaded the source, but I'm a bit lazy with compiling it, I don't even know how to do it or with which compiler ;) I have very slow computer and I wanna know, what's the minimal system requirements for bochs?
IFAIK there are no minimum system requirements. If your computer is very slow then Bochs will be extremely slow, and if you don't have much memory Bochs will restrict the amount of emulated memory in the emulated computer accordingly (and you may end up using the real computer's swap space). I'd guess that a 80386 with 1 Mb of memory would do (as long as there's at least 2 Mb of swap space free).

As for compiling it, it's meant to be portable so any C++ compiler should do. This is a bit of a joke though because C/C++ projects are never portable (even though the huge "./configure" script is meant to smooth over the differences). To minimize hassles I'd use GCC and associated tools (e.g. CYGWIN or MINGW on Windows). I have heard of other people having trouble compiling it on Windows though (and I gave up on Bochs 2.1.1 after some error about a library being missing that I couldn't resolve at the time). If you've got Linux/Unix it'd be much easier (in my experience it always compiles without any problems on Linux).

In any case, you'd use "configure" then "make" to compile it. Then you'd need to setup "bochsrc.txt" which it uses when it starts to figure out what settings to use. If the configuration script detects tools to convert docbook into html it will also create html documentation which can be used to figure out what to put in the "bochsrc.txt", but there should also be a demonstration file (called ".bochsrc" I think) that should be close enough.

As an alternative QEMU would also be suitable for your purposes (see http://www.h7.dion.ne.jp/~qemu-win/ for a pre-compiled windows version). QEMU uses command line options to for it's setup (type "qemu" without options to get a list of options).


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
DIGIT4L_PUNK

Re:problem with task-switching

Post by DIGIT4L_PUNK »

OK.
1. I don't use PL protection (yet)
2. I don't use pagination (yet)
3. I don't use IOPL (yet), but if the eflags are corrupt and the
IOPL bit was set by accident, then the CPU would try to
access the IOPL bitmap, and as the IOPL offset in TSS is
set to 0x0000 and the TSS limit is 0x67, the CPU would
generate INT 0x0A for invalid TSS limit ??? (am I wrong?)
but USR_TSS.eflags (from CPU and from image) is 0x4006
and SYS_TSS.eflags = 0x2 (?) and I use no IO.
4. though I have Pentium 1, The AC (alignment check) bit is
set to 0, so the memory (and the stack) are unaligned. (?)
5. I don't use LDT, but now I'll try to use it and if the problem
disapears... ;)
6. the seg-regs are OK, because after resuming the execution
from INT 0x0A, everithing goes fine (without reloading the
registers manually). I use CS,DS,SS,ES. CS is ok, DS is also
ok because I address strings with DS and display them
successfully, SS is also ok, because I call some routines
after switch-back. so seg-regs are OK.
7. TSS descriptors are also ok.

So the only thing left is LDTR... Jevici, Jevici (<-dont ask me what it means ;D )
DIGIT4L_PUNK

Re:problem with task-switching

Post by DIGIT4L_PUNK »

guys!!! I have just installed QEMU and tested my OS on it. and do you know what happened ;) it generated no INT 0x0A. everithyng went just fine!!!!!
....... so WHAT THE HELL IS WRONG WITH MY COMPUTER
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:problem with task-switching

Post by Pype.Clicker »

qemu seems to 'skip' some tests here and there. It used no to report some of the 'protection faults' i'd like it to report here, for instance.
That makes it faster than Bochs to emulate working software, but less reliable for bugged software inspection.

Moreover, both Bochs and Qemu will automagically wipe memory with zeroes while your real PC's memory is a mess when you access it for the first time ... which means uninitialized data are in unpredictable state (and unlikely to be 0x0000000 :)

My guess too is that you have some of the "static" fields (e.g. LDT iirc) of your SYS_TSS which are left uninitialized. When running SYS_TSS in first place, you're using the 'current content' of LDTR, but that one is a "static" field, meaning when getting out of SYS_TSS for USR_TSS, the content of SYS_TSS.ldtr is left unmodified by the CPU.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re:problem with task-switching

Post by Brendan »

Hi,
DIGIT4L_PUNK wrote: guys!!! I have just installed QEMU and tested my OS on it. and do you know what happened ;) it generated no INT 0x0A. everithyng went just fine!!!!!
....... so WHAT THE HELL IS WRONG WITH MY COMPUTER
Hmm, either a real computer isn't compatible with QEMU, or QEMU isn't compatible with a real computer. Guess which is most likely :)...

IMHO QEMU (while quite good) skips some (usually unnecessary) checks to get better performance, which is why I prefer Bochs (Bochs is much slower but more accurate). It's also possible that QEMU fills memory with zeros before it's used (while a real computer won't), or there's a timing related problem. In any case trust the real computer rather than the tool emulating a real computer.
DIGIT4L_PUNK wrote: 3. I don't use IOPL (yet), but if the eflags are corrupt and the
IOPL bit was set by accident, then the CPU would try to
access the IOPL bitmap, and as the IOPL offset in TSS is
set to 0x0000 and the TSS limit is 0x67, the CPU would
generate INT 0x0A for invalid TSS limit ??? (am I wrong?)
but USR_TSS.eflags (from CPU and from image) is 0x4006
and SYS_TSS.eflags = 0x2 (?) and I use no IO.
When your running at CPL=0 the IO permission bitmap isn't used because the CPU allows all IO port accesses :). If you were running at CPL=3 you'd probably get a general protection fault when you tried to access an IO port.

However, to disable the IO permission bitmap properly, the IO bitmap offset in the TSS should be higher than the TSS's limit (e.g. 0x68 or 0xFFFF). I'm not sure if the CPU will think that the first 104 bytes of the TSS are the IO permission bitmap AND the CPU state storage area at the same time...
DIGIT4L_PUNK wrote:4. though I have Pentium 1, The AC (alignment check) bit is
set to 0, so the memory (and the stack) are unaligned. (?)
The alignment check bit will cause an aligment check exception to be generated when an un-aligned access is used. This doesn't have anything to do with the alignment the CPU uses for the stack. Try doing "push byte 0xAA" and see if ESP changes by 4, by 2 or by 1. It should change by 4 if you've setup the SS descriptor correctly, but may change by 2 if you haven't (or if you're in real mode). It will never change by 1, and never has.

You might be interested in reading through Section 6.2.2 "Stack Alignment" in Intel's "Software Developer's Manual, Basic Architecture" (note: this isn't the "Software Developer's Manual, System Programmer's Guide").
DIGIT4L_PUNK wrote:5. I don't use LDT, but now I'll try to use it and if the problem
disapears... ;)
6. the seg-regs are OK, because after resuming the execution
from INT 0x0A, everithing goes fine (without reloading the
registers manually). I use CS,DS,SS,ES. CS is ok, DS is also
ok because I address strings with DS and display them
successfully, SS is also ok, because I call some routines
after switch-back. so seg-regs are OK.
7. TSS descriptors are also ok.

So the only thing left is LDTR... Jevici, Jevici (<-dont ask me what it means ;D )
IIRC the CPU should be happy if the LDT field in the TSS is set to 0x0000 (the null descriptor), but anything else must be a valid LDT. Don't take my word for this though - it's been a while and I couldn't find anything in Intel's manuals to confirm this.

Also, don't forget about FS and GS when you're counting segment registers! The CPU probably checks them even though you don't use them...

Hmm, I'm wondering if FS or GS has been set to 0xF000 since the BIOS booted the computer - do you change them to something valid (or to the null descriptor 0x0000) when you enter protected mode?


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
zyp

Re:problem with task-switching

Post by zyp »

When the operating system does something wrong qemu only tends to behave weird, often just hanging. Bochs on the other hand print errors.
Therefore I test code in both qemu, bochs, vmware and on real computers. When the code bugs in either one of them, and especially on real computers, I know for sure that it's an error in the code.
Post Reply