config option vs. run-time detection (the debate continues ...)
I love this topic!
Instead of replying to 10 different emails, I decide to sort out my best understanding of this topic and list them here, including some of MY responses to some of the issues.
Definition:
------------
1. Config option approach :
In the kernel config menu, one picks one or more CPUs. One also specifies
whether the CPU(s) have a FPU.
All FPU related code in kernel is configured in or out based on the CONFIG
setting.
2. run-time detection approach:
CPU probing code probes CPU and sets has_fpu field in the mips_cpu structure.
All FPU related code is on a conditional branch based on has_fpu value.
Run-time Detection Approach
---------------------------
Plus:
. the same kernel image can run on CPUs either with or without a FPU.
Minus:
. run-time overhead
a) cpu detection
b) conditional checking for FPU related code
. extra code size (FPU detection and conditional branch for FPU related code) . do we have a reliable detection method without any help from CONFIG (I am
not 100% sure here)
Config Option Approach
----------------------
Plus:
. The reverse of the minus for run-time detection approach
Minus:
. The same kernel image cannot run on CPUs both with and without a FPU.
My Conclusion
--------------
Clearly, it is a trade-off decision based how much one values the minuses and pluses of both approaches.
While I do agree the minus for run-time detection are not serious ones, I think the minus for config option is even less serious. Having the same kernel image runs on multiple CPUs is very nice. I just doubt about the usefulness of requiring the same kernel image to run on CPUs both with a FPU and without a FPU. Therefore in the light of minus of the run-time detection approach, I still favor config option.
(aha, I proved my intuition. It is a theory now. :-0)
Jun
Quoted 1 line
I love this topic!
Well, I'm glad *you're* having fun! ;-)
Quoted 2 lines
Instead of replying to 10 different emails, I decide to sort out my best understanding of this topic and list them here, including some of MY
responses
Quoted 5 lines
to some of the issues.
Definition:
------------
Quoted 7 lines
1. Config option approach :
In the kernel config menu, one picks one or more CPUs. One also specifies whether the CPU(s) have a FPU.
All FPU related code in kernel is configured in or out based on the CONFIG setting.
As has been noted in other messages in this exchange, whether one has an FPU or not isn't really the determining factor in including FP support code in the kernel. The bulk of it is the emulator, and the emulator needs to be there if you want to execute binaries built to include MIPS FP instructions, whether in full emulation or using the FPU (for the denormal cases, etc.).
So your CONFIG option would really be to say, regardless of the CPU, whether your kernel can handle an FPU-ful userland, or is stripped down to support only 100% integer binaries.
Quoted 3 lines
2. run-time detection approach:
CPU probing code probes CPU and sets
has_fpufield in themips_cpu
structure.
Quoted 2 lines
All FPU related code is on a conditional branch based on
has_fpuvalue.
Again, the FPU-related code has to be there any time you're going to run FP binaries. The MIPS_CPU_FPU bit simply tells the kernel how/when to use it.
...
Quoted 4 lines
My Conclusion
--------------Clearly, it is a trade-off decision based how much one values the minuses
and
Quoted 6 lines
pluses of both approaches.
While I do agree the minus for run-time detection are not serious ones, I think the minus for config option is even less serious. Having the same kernel image runs on multiple CPUs is very nice. I just doubt about the usefulness of requiring the same kernel image to run on CPUs both with a
FPU
Quoted 1 line
and without a FPU.
If you're building kernels for a workstation, that may be true. If you're building kernels for a testbed where you're swapping CPU modules, it's actually rather nice to have a single kernel that boots on any of them. I personally find myself in this situation. Or to provide a less lab-oriented example, NEC has the R43xx family which have FPUs,
and Toshiba markets some R49xx parts that are pin-compatible
but cheaper - because they have no FPU. If I were building
a Linux-based system around either one, memory permitting, I would want to have a kernel that could cope with either of them, to simplify the product management problem if I ever ended up wanting to cut over from one to the other.
Kevin K.
Quoted 21 lines · Kevin D. Kissell
Quoted 2 lines
I love this topic!
Well, I'm glad *you're* having fun! ;-)
Quoted 2 lines
Instead of replying to 10 different emails, I decide to sort out my best understanding of this topic and list them here, including some of MY
responses
Quoted 5 lines
to some of the issues.
Definition:
------------Quoted 7 lines
1. Config option approach :
In the kernel config menu, one picks one or more CPUs. One also specifies whether the CPU(s) have a FPU.
All FPU related code in kernel is configured in or out based on the CONFIG setting.
Quoted 7 lines
As has been noted in other messages in this exchange, whether one has an FPU or not isn't really the determining factor in including FP support code in the kernel. The bulk of it is the emulator, and the emulator needs to be there if you want to execute binaries built to include MIPS FP instructions, whether in full emulation or using the FPU (for the denormal cases, etc.).
That needs a little more explanation.
. When I say "All FPU related code", I really meant FPU code which is not a part of FPU emaulator. One example is the code in exit_thread() (arch/mips/process.c) as brough up by flo. I believe there are also such code in ptrace.c.
. Regarding whether we should have FPU emulator, I think it should be a separate CONFIG option. It is orthorgal to HAS_FPU option.
In other words, we will have four combinations:
a) HAS_FPU & FPU_EMULATION - which is necessary when FPU is not a full
implementation.
b) !HAS_FPU & FPU_EMULATION - which allows one to run fpu-ful userland
application
c) HAS_FPU & !FPU_EMULATION - when FPU is a full implementaion (or use the
old incomplete emaulation?)
d) !HAS_FPU & !FPU_EMULATION - it mandates non-fpu-ful userland (which to me
is perfectly fine)
I start to feel a little "shaky" here as I have not written any FPU code. Will such a classification make life easier or worse? Is there any feasibility issue here?
Quoted 14 lines
If you're building kernels for a workstation, that may be true. If you're building kernels for a testbed where you're swapping CPU modules, it's actually rather nice to have a single kernel that boots on any of them. I personally find myself in this situation. Or to provide a less lab-oriented example, NEC has the R43xx family which have FPUs,
and Toshiba markets some R49xx parts that are pin-compatible
but cheaper - because they have no FPU. If I were building
a Linux-based system around either one, memory permitting, I would want to have a kernel that could cope with either of them, to simplify the product management problem if I ever ended up wanting to cut over from one to the other.
I think this example shifted my bias a little bit, although it has changed it yet.
Are we confident we can do a clean run-time detection of all existing CPUs?
Jun
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
1. Config option approach :
In the kernel config menu, one picks one or more CPUs. One also
specifies
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
whether the CPU(s) have a FPU.
All FPU related code in kernel is configured in or out based on the
CONFIG
Quoted 9 lines
Quoted 9 lines
Quoted 1 line
setting.
As has been noted in other messages in this exchange, whether one has an FPU or not isn't really the determining factor in including FP support code in the kernel. The bulk of it is the emulator, and the emulator needs to be there if you want to execute binaries built to include MIPS FP instructions, whether in full emulation or using the FPU (for the denormal cases, etc.).
Quoted 3 lines
That needs a little more explanation.
. When I say "All FPU related code", I really meant FPU code which is not
a
Quoted 2 lines
part of FPU emaulator. One example is the code in
exit_thread()(arch/mips/process.c) as brough up by flo. I believe there are also such
code
Quoted 1 line
in
ptrace.c.
Understood. My point is that that code can be lumped with the emulator as the set of support needed to run FPU-full binaries. Even with an emulator, one needs to manage and communicate FP state. The only difference is that the "floating point registers" are in the thread structure, not in the CPU.
Quoted 5 lines
. Regarding whether we should have FPU emulator, I think it should be a separate CONFIG option. It is orthorgal to
HAS_FPUoption.In other words, we will have four combinations:
Quoted 7 lines
a)
HAS_FPU&FPU_EMULATION- which is necessary when FPU is not a full
implementation.b) !HAS_FPU &
FPU_EMULATION- which allows one to run fpu-ful userland
applicationc)
HAS_FPU& !FPU_EMULATION - when FPU is a full implementaion (or use
the
Quoted 1 line
old incomplete emaulation?)
We're talking about MIPS/Linux here. To the best of my knowledge there are *no* "full" implementations of IEEE floating point on a MIPS CPU. The number of unimplemented cases varies somewhat from design to design, but I know of no MIPS CPU where it is a null set.
Quoted 1 line
d) !HAS_FPU & !FPU_EMULATION - it mandates non-fpu-ful userland (which to
me
Quoted 5 lines
is perfectly fine)
I start to feel a little "shaky" here as I have not written any FPU code. Will such a classification make life easier or worse? Is there any feasibility issue here?
It's not so much that I doubt the feasibility, I just wonder if there's any point to adding the complexity. As noted above, if you're going to support FP-full binaries, you have to support the processor model of FPU. The user will be manipulating what he sees as FP registers, and all of the state, signal, and context management logic associated with them has to be there regardless of whether they exist in the CPU or in the kernel. It's true that there are a few paths through the code, the ones that actually load and store the FP registers, that are distinct. Those could certainly be suppressed at compile time if you wanted a kernel that would never allow a real FPU to be used, but the memory savings would be smaller than you seem to think. It's not "HAS_FPU" versus "EMULATOR",
it's "SUPPORTS_FP" and "HAS_FPU".
But my main objection to treating the two options as orthogonal is that your (c) case above will simply create kernels that are guaranteed fail for some cases of IEEE math. It's just not good engineering to give people a 25% chance of building a kernel that seems to be just fine, at first...
My own recommendation would be to either have full FP support for binaries or none at all. If someone really wants to put the FPU-specific assembler routines under a different conditional, that's cool, but the configuration options should be such that the
(c) cannot be generated by the config scripts.
Regards,
Kevin K.
Quoted 7 lines · Kevin D. Kissell
It's not so much that I doubt the feasibility, I just wonder if there's any point to adding the complexity. As noted above, if you're going to support FP-full binaries, you have to support the processor model of FPU. The user will be manipulating what he sees as FP registers, and all of the state, signal, and context management logic associated with them has to be there regardless of whether
Quoted 8 lines
they exist in the CPU or in the kernel. It's true that there are a few paths through the code, the ones that actually load and store the FP registers, that are distinct. Those could certainly be suppressed at compile time if you wanted a kernel that would never allow a real FPU to be used, but the memory savings would be smaller than you seem to think. It's not "
HAS_FPU" versus "EMULATOR",
it's "SUPPORTS_FP" and "HAS_FPU".
Let me remind the actual problem the discussion started from was whether
we want to hardcode FP hw presence based on a CPU identification or to check for it explicitly. I hope we agree the latter is saner.
But the code that needs to know whether there is a real FPU present is
indeed minimal (as it should be) thus the gain from removing the detection altogether in favour to a config option is at least questionable if not insane.
Quoted 6 lines
My own recommendation would be to either have full FP support for binaries or none at all. If someone really wants to put the FPU-specific assembler routines under a different conditional, that's cool, but the configuration options should be such that the
(c) cannot be generated by the config scripts.
I think I may research what the gain from leaving parts of the FPU
emulator apart would be for systems that have FP hw. It's not a priority task for me at the moment -- the current configuration works and having unused code in a running kernel is ugly but non-fatal.
Maciej
Signature · 3 lines
+ Maciej W. Rozycki, Technical University of Gdansk, Poland + +--------------------------------------------------------------+ + e-mail: [email protected], PGP key available +
Quoted 3 lines
Let me remind the actual problem the discussion started from was whether we want to hardcode FP hw presence based on a CPU identification or to check for it explicitly. I hope we agree the latter is saner.
Absolutely.
Quoted 4 lines
But the code that needs to know whether there is a real FPU present is
indeed minimal (as it should be) thus the gain from removing the detection altogether in favour to a config option is at least questionable if not insane.
Well, I might not have put it that strongly, but I quite agree.
Quoted 9 lines
Quoted 6 lines
My own recommendation would be to either have full FP support for binaries or none at all. If someone really wants to put the FPU-specific assembler routines under a different conditional, that's cool, but the configuration options should be such that the
(c) cannot be generated by the config scripts.I think I may research what the gain from leaving parts of the FPU
emulator apart would be for systems that have FP hw.
Surprisingly little. Basically, you could remove emulation for moves, loads, and stores. Any instruction that actually computes a value can potentially turn out to be unimplemented, depending on the inputs. If you *knew* you had an FPU, you could also make the emulation fractionallly more efficient by having it operate directly on the FP registers. When the current kernels emulate an instruction that failed on a HW FPU, they copy the HW FPU state into the software FPU registers, invoke the emulator, and when the emulation has completed, restore the HW FPU state. It made for
a clean interface to do things that way, and my judgement was that the operation was too infrequent
to justify complicating things further.
Quoted 3 lines
It's not a priority
task for me at the moment -- the current configuration works and having unused code in a running kernel is ugly but non-fatal.
"If it ain't broke..."
Kevin K.
Quoted 1 line · Kevin D. Kissell
"If it ain't broke..."
Well, it's actually one of strengths of Linux we break things from time
to time to make them better.
Signature · 3 lines
+ Maciej W. Rozycki, Technical University of Gdansk, Poland + +--------------------------------------------------------------+ + e-mail: [email protected], PGP key available +
Quoted 6 lines · Maciej W. Rozycki
But the code that needs to know whether there is a real FPU present is
indeed minimal (as it should be) thus the gain from removing the detection altogether in favour to a config option is at least questionable if not insane.
Do you like run-time detection better because it allows a kernel to run on CPUs both with a FPU and without a FPU? Or there is something else to it?
Another question. I know with mips32 and mips64 we can do run-time detection reliably. What about other existing processors?
Jun
Quoted 2 lines · Jun Sun
Do you like run-time detection better because it allows a kernel to run on CPUs both with a FPU and without a FPU? Or there is something else to it?
Nope. There are certain explicit actions that are to be performed by the
kernel if a real FPU is present, such as saving and restoring its registers or setting the control register. Therefore the kernel has to know if a real part is present and act accordingly. Maintaining a table of all CPU ids ever manufactured and manually setting the FPU presence bit is unreliable, especially as there are chips which cannot be classified this way, e.g. knowing your CPU is an R3000A you don't know if an R3010A FPU is soldered as well or not.
Quoted 2 lines
Another question. I know with mips32 and mips64 we can do run-time detection reliably. What about other existing processors?
I've sent a quote from an IDT manual recently. It recommended to use the
FPU implementation ID to check if an FP hw is present. I believe it should work for any sane implementation of a MIPS CPU. See the mail for details.
Maciej
Signature · 3 lines
+ Maciej W. Rozycki, Technical University of Gdansk, Poland + +--------------------------------------------------------------+ + e-mail: [email protected], PGP key available +
Quoted 6 lines · Maciej W. Rozycki
Quoted 2 lines · Jun Sun
Do you like run-time detection better because it allows a kernel to run on CPUs both with a FPU and without a FPU? Or there is something else to it?
Quoted 9 lines
Nope. There are certain explicit actions that are to be performed by the
kernel if a real FPU is present, such as saving and restoring its registers or setting the control register. Therefore the kernel has to know if a real part is present and act accordingly. Maintaining a table of all CPU ids ever manufactured and manually setting the FPU presence bit is unreliable, especially as there are chips which cannot be classified this way, e.g. knowing your CPU is an R3000A you don't know if an R3010A FPU is soldered as well or not.
Apparently you did not read my first email on this thread. :-)
I agree "Maintaining a table of all CPU ids ever manufactured and manually setting the FPU presence bit is unreliable ...".
I was debating about how we let kernel know if there is a real FPU:
a) an explicit config option, CONFIG_HAS_FPU, (which is not associated with
PrID), plus "#ifdef CONFIG_HAS_FPU ..." code. Or
b) have run-time detection and many "if .. then .." code.
I listed some pro's and con's for both of them in my first email. Right now, I found myself not having a strong preference but still biased towards config option approach ( - as if that really matters. :-0)
Quoted 8 lines
Quoted 2 lines
Another question. I know with mips32 and mips64 we can do run-time detection reliably. What about other existing processors?
I've sent a quote from an IDT manual recently. It recommended to use the
FPU implementation ID to check if an FP hw is present. I believe it should work for any sane implementation of a MIPS CPU. See the mail for details.
I actually don't understand your IDT quote. It requires one to call mfc1 to get FCR0. On many CPUs without a FPU, this will generate an exception. Are you suggesting that we should catch the exception and from that we conclude there is no FPU present?
Jun
Quoted 8 lines · Jun Sun
a) an explicit config option,
CONFIG_HAS_FPU, (which is not associated with
PrID), plus "#ifdefCONFIG_HAS_FPU..." code. Orb) have run-time detection and many "if .. then .." code.
I listed some pro's and con's for both of them in my first email. Right now, I found myself not having a strong preference but still biased towards config option approach ( - as if that really matters. :-0)
Well, the number of places a run-time condition exist is small and they
are not performance-critical.
Quoted 4 lines
I actually don't understand your IDT quote. It requires one to call mfc1 to get FCR0. On many CPUs without a FPU, this will generate an exception. Are you suggesting that we should catch the exception and from that we conclude there is no FPU present?
I don't have an FPU-less system and I can't check such code. I need to
depend on others (I couldn't test all possible configurations anyway). If we have a chance to get an exception we have to catch it, of course (that's trivial to handle in Linux).
Signature · 3 lines
+ Maciej W. Rozycki, Technical University of Gdansk, Poland + +--------------------------------------------------------------+ + e-mail: [email protected], PGP key available +
Quoted 11 lines · Maciej W. Rozycki
Well, the number of places a run-time condition exist is small and they
are not performance-critical.Quoted 4 lines
I actually don't understand your IDT quote. It requires one to call mfc1 to get FCR0. On many CPUs without a FPU, this will generate an exception. Are you suggesting that we should catch the exception and from that we conclude there is no FPU present?
I don't have an FPU-less system and I can't check such code. I need to
depend on others (I couldn't test all possible configurations anyway).
However, with CONFIG_HAS_FPU approach I know for sure it will work for any MIPS CPU, as long as the programmer specifies it correctly. :-)
Quoted 4 lines
If
we have a chance to get an exception we have to catch it, of course (that's trivial to handle in Linux).
No effort (as in CONFIG_HAS_FPU approach) is still better than trivial or small effort ( as in run-time detection). :-)
---
I am very curious what makes you object to the CONFIG_HAS_FPU approach, especially you said earlier it was not about the inability to support both FPU and FPU-less CPUs with the same kernel image.
Jun
Quoted 5 lines · Jun Sun
Quoted 2 lines
I don't have an FPU-less system and I can't check such code. I need to
depend on others (I couldn't test all possible configurations anyway).However, with
CONFIG_HAS_FPUapproach I know for sure it will work for any MIPS CPU, as long as the programmer specifies it correctly. :-)
Tha latter being a BIG if.
Quoted 7 lines
Quoted 4 lines
If
we have a chance to get an exception we have to catch it, of course (that's trivial to handle in Linux).No effort (as in
CONFIG_HAS_FPUapproach) is still better than trivial or small effort ( as in run-time detection). :-)
Regardless of the CONFIG_HAS_FPU option you still need to check if a FPU
is present.
Quoted 3 lines
I am very curious what makes you object to the
CONFIG_HAS_FPUapproach, especially you said earlier it was not about the inability to support both FPU and FPU-less CPUs with the same kernel image.
That's pretty orthogonal -- I do not object the CONFIG_HAS_FPU option (I
even favor it, if it could save us unneeded bits), but whether having it or not, we still have to properly detect FP hardware. A silent crash or an obscure oops is not an option in case of a config error.
Signature · 3 lines
+ Maciej W. Rozycki, Technical University of Gdansk, Poland + +--------------------------------------------------------------+ + e-mail: [email protected], PGP key available +
Quoted 1 line
Quoted 1 line
Another question. I know with mips32 and mips64 we can do run-time
detection
Quoted 6 lines
Quoted 1 line
reliably. What about other existing processors?
I've sent a quote from an IDT manual recently. It recommended to use the
FPU implementation ID to check if an FP hw is present. I believe it should work for any sane implementation of a MIPS CPU. See the mail for details.
The best method I know for post-R3000 CPUs is to write and read back the CU1 bit of the Status register. CPUs without an integrated FPU will not have a flip-flop for the bit, and will read back a 0 even after writing a 1. There was never any architectural requirement that this be so, however, and this cannot be absolutely guaranteed to work. If anyone has a counter-example, however, I'd be interested in hearing about it.
Kevin K.
Quoted 8 lines · Kevin D. Kissell
The best method I know for post-R3000 CPUs is to write and read back the CU1 bit of the Status register. CPUs without an integrated FPU will not have a flip-flop for the bit, and will read back a 0 even after writing a 1. There was never any architectural requirement that this be so, however, and this cannot be absolutely guaranteed to work. If anyone has a counter-example, however, I'd be interested in hearing about it.
OK, then we may try to toggle CU1 and only if that succeeds check the FPU
implementation id. Thanks for the point.
Signature · 3 lines
+ Maciej W. Rozycki, Technical University of Gdansk, Poland + +--------------------------------------------------------------+ + e-mail: [email protected], PGP key available +
Quoted 5 lines · Jun Sun
a)
HAS_FPU&FPU_EMULATION- which is necessary when FPU is not a full
implementation.b) !HAS_FPU &
FPU_EMULATION- which allows one to run fpu-ful userland
application
These 2 cases are perfectly good
Quoted 5 lines
c)
HAS_FPU& !FPU_EMULATION - when FPU is a full implementaion (or use the
old incomplete emaulation?)d) !HAS_FPU & !FPU_EMULATION - it mandates non-fpu-ful userland (which to me
is perfectly fine)
These 2 cases present a user/developer who decided not to have any fpu support kernel/cpu wise. Kill his apps if using "illegal" instructions.
Flo
Signature · 2 lines
Florian Lohoff [email protected] +49-5201-669912 Why is it called "common sense" when nobody seems to have any?