Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable floating point unit (FPU)?

I want to disable FPU/MMX/SSE instructions in x86 system, and I will implement a handler for the Device-Not-Available exception. I have referred to Control register wiki page; It seems that I have to set some flags in cr0 register. How to set these flags in cr0 and Do this work do at boot time?

like image 465
Hsiao-Hui Chiu Avatar asked May 26 '11 02:05

Hsiao-Hui Chiu


1 Answers

The Linux kernel code for managing FPU state can be found in arch/x86/kernel/traps.c, do_device_not_available(). By default, the Linux kernel disables the FPU for all processes, and enables it on first access. This allows the kernel to reduce context switch overhead for processes that don't use the FPU. However, it also means that setting TS once at startup is insufficient; you must alter the Linux kernel code that manages the TS flag to maintain this state.

By adding an early check to do_device_not_available() for a disable flag and raising a signal or taking some other action, you can disable access to the FPU. Note that if you're doing this after the process's first use of the FPU on that particular CPU, the FPU may remain usable for some time, until the FPU registers are context-switched out, and the FPU is re-disabled. If you wish to avoid this, you will have to explicitly re-disable the FPU with stts().

Note that as the Linux ABI assumes you have a FPU (either emulated FPU or hardware FPU - if you have neither the kernel will not boot), this may cause unexpected behavior in applications. Additionally, any internal kernel code using the FPU (not sure if there is any) is likely to break as well. Implement this at your own risk.

like image 184
bdonlan Avatar answered Oct 23 '22 09:10

bdonlan