43

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 always, 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^)

smwikipedia
  • 61,609
  • 92
  • 309
  • 482

3 Answers3

48

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
  • 62,085
  • 98
  • 303
  • 499
  • Thanks Earlz. I mark your answer as the answer. Also thanks to the other people. :D – smwikipedia May 06 '10 at 02:24
  • 2
    TSS-based switching provides hardware-level state management (read: security) which a software switch does not. The TSS and privilege system introduced to the IA in the 80s BOTH play a role in securing processes from one another. So, to summarize, "Some OSes do not use hardware-based task switching because they prefer performance and portability over security." Seems legit. – Shaun Wilson Aug 27 '14 at 12:20
  • 1
    As an aside, NT uses an M:N threading model, unlike 'other systems' there is not a 1:1 relationship between hardware and software threads, this solves more than just the "task switch performance problem". Not all systems can implement an M:N model (due to their tight-coupling of hardware threads to a particular CPU and process, for example) and instead suffer performance penalties in trying to do so (since such systems lack a proper hardware=>user-mode signalling mechanism that doesn't also tie up the original execution thread.) – Shaun Wilson Aug 27 '14 at 12:32
  • 1
    @ShaunWilson I have doubts about the accuracy of that claim. The CPU checks the permissions of selectors regardless, when they are loaded. There are no selector permissions to check on x86_64. What "extra security" is claimed by using TSS exactly? – doug65536 May 13 '16 at 09:35
20

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
  • 608
  • 3
  • 6
7

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
  • 31,730
  • 2
  • 29
  • 28
  • 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 Apr 26 '10 at 04:50
  • 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 Apr 26 '10 at 05:02
  • 1
    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 May 04 '10 at 05:04
  • Thanks Andrew and Matthew. I could only mark one as the answer. Your answers are informative, too. :D – smwikipedia May 06 '10 at 02:27