I've been considering a possible architectural change to SeaBIOS.
Currently, SeaBIOS contains a mix of 16bit code and 32bit code. All
of the initialization and bootup code is done in regular 32bit mode,
but runtime code (the callbacks the OS uses) is generally run in 16bit
mode. I have been thinking about possibly changing this so that
hardware driver code runs exclusively in 32bit mode.
Specifically, this would involve running the disk controller code, the
keyboard/mouse controller code, and all hardware interrupt handlers
exclusively in 32bit mode. To support this, some BIOS calls would
require SeaBIOS to transition from 16bit mode to 32bit mode in order
to satisfy the request. This trampolining to 32bit mode at runtime is
already done for the AHCI driver, the XHCI driver, and the pvscsi
driver - as these devices can not function with only a 16bit driver.
Even with this proposed change, SeaBIOS would still continue to have
16bit code, though the remaining 16bit code would be almost
exclusively for interface support and it would be noticeably smaller.
In initial tests, the final 16bit code size drops from ~35K to ~16K.
The main benefit of this change is that it makes it easier to develop
and enhance the SeaBIOS hardware driver code, it makes understanding
the code simpler, and reduces the overall size of the SeaBIOS binary.
The biggest downside to this change would be problems running old DOS
era programs that attempt to run the BIOS in vm86 mode. Specifically,
the dos emm386 program is known to prevent 32 bit trampolines in
SeaBIOS from working. (There's been a bit of experience with AHCI
drivers running in 32bit mode (and now XHCI) so we have good
confidence that modern OSes wont present a problem.) To continue to
support these old DOS era programs I'm proposing we implement an SMI
to help trampoline to 32bit mode. This can be done within SeaBIOS for
QEMU TCG, but it would require work on kvm, coreboot, and ovmf to
support it. (If we go down this path and these projects aren't
extended to help trampoline to 32bit mode, then these projects would
no longer be able to run these old DOS era programs with SeaBIOS.)
For testing purposes, I have put together a series of patches to run
the SeaBIOS drivers in 32bit mode and to implement the QEMU TCG SMI
helper. The code is in a very rough state, but it demonstrates the
idea. It's available at:
Unfortunately, the QEMU TCG SMI doesn't seem to help in vm86 mode, but
I believe it is due to the QEMU code not handling CPL changes properly
in SMM mode. I still need to confirm this though.
In closing, this email is to start discussion on the proposal. This
would not impact the next release of SeaBIOS.