The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

OS: Context switching on x86

Hallo Reader,
I have the following question to pose:
According to the Intel manual, a protected mode software should have a TSS structure to implement multitasking. So does Linux which uses one TSS structure per CPU.
But, IMHO theoretically, all a context switch requires is to switch the stack of one process to another. (Since context switch happens in Kernel Mode, the return address etc are stored on the stack by the processor itself. Additionally, the system call does store the other General Purpose Registers as in Linux).
Can anyone throw more light on this topic of how these TSS work ?

Thanks in advance,
Kiran Send private email
Monday, February 28, 2005
They want you to use them, since Intel's engineers had a field day with the CPU design here, but you don't actually *have* to.

You can arrange for the system timer interrupt to switch to a kernel-mode stack, and then record the registers for the interrupted (outgoing) thread yourself, substitute the registers with a new set of values (for the incoming thread), and execute an interrupt return (IRET) instruction. This roughly implements context switching.

Context switching of the FPU registers can be done separately to the main register set as an optimisation so you don't have to save and restore these for threads that aren't doing floating point operations. This will typically be the majority of threads.

A major case for using a TSS structure is to handle the so called 'double fault' situation, which would occur if your kernel itself messes up badly. In which case, the IDT vector for 'double fault' points to a TSS structure that contains an entire register state with which you rescue yourself! Generally, design to avoid this.

Also, you don't have to use a TSS to switch from kernel mode to user mode. You can just fake an 'interrupt return' stack frame, and execute IRET.
Monday, February 28, 2005
I think that you are referring to "light" or "collaborative" threads. Their official name is coroutines (according to Knuth's Art of programming).

Linux and other UNIX flavors does not have native support for coroutines. Windows does (they are called "Fiber"s).

There are many simple C packages that add support for coroutines. They are based on longjmp() tricks ( libPCL - ) or explicit stack manipulations ( CORO - ).

Some languages have native support for coroutines: Io Language ( ) and Lua ( ).

Coroutines are also natively supported in actor based languages ( ). Although most of these languages remain in the experimental stage, I guess that the concept will soon be included in today's popular languages (such as Python) and operating systems.
Developer next door
Monday, February 28, 2005

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
Powered by FogBugz