Math CoProcessor

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.
Post Reply
User avatar
piranha
Member
Member
Posts: 1391
Joined: Thu Dec 21, 2006 7:42 pm
Location: Unknown. Momentum is pretty certain, however.
Contact:

Math CoProcessor

Post by piranha »

So, I read the Intel Manuals and looked at early Linux code, and my question is, how does the kernel know when a program is using the math processor?
There is the function math_state_restore, but when is that called?

-JL
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

The processor is built to do lazy task switching for coprocessor state. If you look at all FP and vector instructions, you'll notice they all cause an exception when CR0.TS is set. You can set CR0.TS by doing a hardware task switch or by manually setting it. You can clear it via the CLTS instruction.

The routine is roughly as follows:

1) Process A uses the FPU
2) process A gets scheduled
3) the scheduler sets CR0.TS
4) process B runs
5) process B gets scheduled
6) process C runs
7) process C uses the FPU
8) The coprocessor exception is thrown (#NP, IIRC)
9) The FPU state for process A is saved
10) The FPU state for process C is loaded
11) CLTS is executed to clear CR0.TS
12) process C runs, and can now use the FPU

Notice that there is virtually no FPU-related overhead for process B. And if process A were the only one using the FPU, there would be no floating point save/restore overhead at all.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
piranha
Member
Member
Posts: 1391
Joined: Thu Dec 21, 2006 7:42 pm
Location: Unknown. Momentum is pretty certain, however.
Contact:

Post by piranha »

Thanks.

-JL
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
Post Reply