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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With