Why doesn’t Linux use the hardware context switch with TSS?

All off topic discussions go here. Everything from the funny thing your cat did to your favorite tv shows. Non-programming computer questions are ok too.
Post Reply
User avatar
smwikipedia
Member
Member
Posts: 49
Joined: Tue Apr 20, 2010 1:11 am

Why doesn’t Linux use the hardware context switch with TSS?

Post by smwikipedia »

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.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Why doesn’t Linux use the hardware context switch with T

Post by Solar »

The answer to 2) is, it's not faster on modern architectures. Which pretty much answers 1), too. ;-)
Every good solution is obvious once you've found it.
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Re: Why doesn’t Linux use the hardware context switch with T

Post by earlz »

This can now be seen at Stack Overflow: http://stackoverflow.com/questions/2711 ... ia-the-tss
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: Why doesn’t Linux use the hardware context switch with T

Post by qw »

AFAIK, hardware context switching with a TSS is slow mainly because it loads all segment registers, causing a lot of protected mode checks. This is unnecessary overhead in a flat memory model. If you are using a segmented memory model, hardware context switching may be faster (though I've never tested this).
quok
Member
Member
Posts: 490
Joined: Wed Oct 18, 2006 10:43 pm
Location: Kansas City, KS, USA

Re: Why doesn’t Linux use the hardware context switch with T

Post by quok »

Let us not forget that in long mode, hardware context switching is not even supported at all.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Why doesn’t Linux use the hardware context switch with T

Post by Solar »

Consider it on par with segment registers: It was a solution for a given problem of its time, has long since been left behind by newer developments, and would have gone the way of the Dodo if it wasn't for x86_64 still being downward compatible to CPU architecture from the seventies and eighties.
Every good solution is obvious once you've found it.
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: Why doesn’t Linux use the hardware context switch with T

Post by Pype.Clicker »

multi-segment architecture was aimed at MULTICS environment style. Programming 16-bit code forced you to toy with multiple segments ... you had no choice. It was sort of a nightmare to do, but it was worth it. The UNIX model is simpler, and while it doesn't offer the same level of protection, at least it lets you build things rather than trying work around "artificial" constraints.

IIrc, selective flushing of page tables and fast user-to-system call makes inter-process switches roughly as efficient as your single-address-space-with-multi-segments switches could be. And if that doesn't fullfill your needs for protection, we received Java and other language-oriented protection mechanism to offer the same kind of safeguards as segments where meant to offer.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: Why doesn’t Linux use the hardware context switch with T

Post by Love4Boobies »

As for OSes that use it - I don't think any modern ones are (except perhaps for some hobby ones and that's just because their authors don't know it's slower). Linux 0.01 used it; perhaps later versions too, I don't know when they've switched to software-based task switching.

Btw, nice to see you again, Pype.Clicker :)
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
mkruk
Posts: 11
Joined: Thu Apr 15, 2010 9:34 am

Re: Why doesn’t Linux use the hardware context switch with T

Post by mkruk »

Love4Boobies wrote:except perhaps for some hobby ones and that's just because their authors don't know it's slower
I'm using hardware task switching even though I know its slower :p
at the moment I'm not aiming to provide a fast OS but a stable one (or one that works at all) but that may change in the future.. anyway I'll run some tests as soon as the kernel is done. spose it might be interesting to have some actual measurement data
User avatar
thepowersgang
Member
Member
Posts: 734
Joined: Tue Dec 25, 2007 6:03 am
Libera.chat IRC: thePowersGang
Location: Perth, Western Australia
Contact:

Re: Why doesn’t Linux use the hardware context switch with T

Post by thepowersgang »

Personally, I think that using software task switching is easier to implement. The TSS is messy and, as everyone else said, reloading segment registers is slow. Plus, if you use software switching, porting to another arch is a breeze (when I moved to x86_64 a week ago, I only needed to alter the IDT handlers and memory management, the process manager code stayed virtually identical)
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
mkruk
Posts: 11
Joined: Thu Apr 15, 2010 9:34 am

Re: Why doesn’t Linux use the hardware context switch with T

Post by mkruk »

that's true, but hardware task switching has the advantage that task's dont need to have operating system code in their address space. In a software switched environment you need to have kernel functions such as save and restore (well that's what they're called in Minix) mapped to each task's pagedir.

Hardware task switching also has the advantage that you don't have to take care of the stacks. When an exception causes (for instance) the page fault handler task to be called, the error code is pushed on the stack of the page fault handler. You don't have to execute the page fault handler on behalf of the user task. This is also the reason why double faults can not be handled reliably without hardware task switching.

Anyway, I know that software task switching is a lot more efficient and the advantages of hardware task switching are probably invalid as soon as one has a deep understanding of what is done during a software task switch.
Still, I'll first implement hardware task switching and software task switching afterwards.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Why doesn’t Linux use the hardware context switch with T

Post by Brendan »

Hi,
mkruk wrote:that's true, but hardware task switching has the advantage that task's dont need to have operating system code in their address space. In a software switched environment you need to have kernel functions such as save and restore (well that's what they're called in Minix) mapped to each task's pagedir.
If the OS is lame (e.g. no SMP and no FPU/MMX/SSE support, no keeping track of how much CPU time tasks have used, etc) then it might work, but only if you're willing to accept severe performance problems (due to excessive task switches and excessive TLB misses) for no reason.
mkruk wrote:Hardware task switching also has the advantage that you don't have to take care of the stacks. When an exception causes (for instance) the page fault handler task to be called, the error code is pushed on the stack of the page fault handler. You don't have to execute the page fault handler on behalf of the user task. This is also the reason why double faults can not be handled reliably without hardware task switching.
For the double fault exception handler using hardware task switching (even if/when the OS uses software task switching for everything else) can be a good idea, because the double fault exception handler needs to assume the rest of the kernel has been trashed and typically does a kernel panic and never returns. Of course a double fault only happens if an exception occurs while the CPU is trying to start a different exception handler, so in practice (for a kernel that doesn't have a broken IDT or unreliable kernel stacks) double fault never happens.

For something like the page fault exception handler hardware task switching is a complete nightmare, because hardware task switching isn't reentrant. For example, if you get a page fault while you're trying to handle a previous page fault then the previous page fault handler's stack gets trashed; and this makes it hard to implement support for things like swap, memory mapped files, etc. For example, a page fault occurs, the page fault handler asks the disk driver to load a page of data from disk, the disk driver gets pre-empted by another task (or blocks, waiting for hardware/IRQ), the next task causes a second page fault, and the first page fault handler's stack gets trashed.


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.
Post Reply