views:

530

answers:

3

Hi guys! I read the following statement:

The x86 architecture includes a specific segment type called the Task State Segment (TSS), to store hardware contexts. Although Linux doesn't use hardware context switches, it is nonetheless forced to set up a TSS for each distinct CPU in the system.

I am wondering:

  • Why doesn't Linux use the hardware support for context switch?
  • Isn't the hardware approach much faster than the software approach?
  • Is there any OS which does take advantage of the hardware context switch? Does windows use it?

At last and as usual, thanks for your patience and reply.

-----------Added--------------

http://wiki.osdev.org/Context_Switching got some explanation.

People as confused as me could take a look at it. 8^)

+3  A: 

Linux doesn't use a segmented memory model, so this segmentation specific feature isn't used.

x86 CPUs have many different kinds of hardware support for context switching, so the distinction isn't hardware vs software, but more how does an OS use the various hardware features available. It isn't necessary to use them all.

Linux is so efficiency focussed that you can bet that someone has profiled every option that is possible, and that the options currently used are the best available compromise.

Andrew McGregor
Thank you Andrew. Could you tell me what other hardware supports for context switch a x86 CPU provides? I only heard of the TSS.
smwikipedia
Most of the MMU features only make sense in a multithreaded environment, for example. It seems I wasn't quite right: the CPU forces Linux to use a TSS for the ESP register, even though none of the other fields are used. I guess the section on software task switching here has pointers to most of what you need: http://wiki.osdev.org/Task_State_Segment
Andrew McGregor
Building upon Andrew's last comment: the TSS is required for things such as ring3 -> ring0 transitions where it picks up the ESP0 value. This prevents the kernel using the ring3 stack when entering ring0 - a security feature. Linux uses one TSS per CPU for this transition.
Matthew Iselin
Thanks Andrew and Matthew. I could only mark one as the answer. Your answers are informative, too. :D
smwikipedia
+2  A: 

Linux used to use HW-based switching, in the pre-1.3 timeframe iirc. I believe sw-based context switching turned out to be faster, and it is more flexible.

Another reason may have been minimizing arch-specific code. The first port of Linux to a non-x86 architecture was Alpha. Alpha didn't have TSS, so more code could be shared if all archs used SW switching. (Just a guess.) Unfortunately the kernel changelogs for the 1.2-1.3 kernel period are not well-preserved, so I can't be more specific.

Andy Grover
Thanks Andy for the history info. :D
smwikipedia
+4  A: 

The x86 TSS is very slow for hardware multitasking and offers almost no benefits when compared to software task switching. (In fact, I think doing it manually beats the TSS a lot of times)

The TSS is known also for being annoying and tedious to work with and it is not portable, even to x86-64. Linux aims at working on multiple architectures so they probably opted to use software task switching because it can be written in a machine independent way. Also, Software task switching provides a lot more power over what can be done and is generally easier to setup than the TSS is.

I believe Windows 3.1 used the TSS, but at least the NT >5 kernel does not. I do not know of any Unix-like OS that uses the TSS.

Do note that the TSS is mandatory. The thing that OSs do though is create a single TSS entry(per processor) and everytime they need to switch tasks, they just change out this single TSS. And also the only fields used in the TSS by software task switching is ESP0 and SS0. This is used to get to ring 0 from ring 3 code for interrupts. Without a TSS, there would be no known Ring 0 stack which would of course lead to a GPF and eventually triple fault.

Earlz
Thanks Earlz. I mark your answer as the answer. Also thanks to the other people. :D
smwikipedia