Hi,
mariuszp wrote:Also, which IRQs do I have to forward to the guest BIOS, if any?
Potentially, anything involving timing (e.g. PIT/IRQ0), IRQ13 (used by FPU errors), and anything that belongs to the video card itself (e.g. vertical retrace IRQ). Note that I'd expect that most video card ROMs don't need/use any IRQs (and don't use protected mode); it's just that there is no guarantee and nothing preventing a video card ROM from doing anything it likes.
mariuszp wrote:@Brendan: why would I need to emulate PCI chips? I will simply allow the BIOS to in/out any port it wants to.
PIC chips (Programmable Interrupt Controllers).
If real mode code expects to handle an IRQ, then it will also send an EOI to the PIC chip/s. If the OS handles some IRQs and the guest handles others IRQs (and if the guest uses the typical "non-specific EOI") then it will cause problems due to the way the PIC chip's IRQ priorities work (e.g. the guest's EOI can end the host OS's IRQ and not the IRQ it thinks it's ending). You'd also have a problem if/when the OS is using IO APICs and not using PIC chips at all.
mariuszp wrote:alternatively, is it possible to somehow switch to Real Mode from Long Mode? I know you can do it from Protected Mode, but when I once accidentally attempted to disable Long Mode, the WRMSR threw a #GP.
It's possible to switch from long mode back to real mode. The problem is that the BIOS will expect any/all hardware to remain in the state it expects, and this cripples the OS's ability to be an OS (rather than a "BIOS extender"). For example; if you start using IO APICs then you'd either have to disable IO APICs and enable PICs when switching to real mode or you'd have to implement a "clever" hack/work-around to trick the BIOS into thinking its using PIC when it isn't; if you change anything in PCI configuration space you'd have to reverse those changes; etc. The other problem is that (if it's done while an OS is running and not just during boot) you'll have other unrelated device drivers (e.g. disk, network, sound, serial, etc) trying to do their job at the same time and won't want them to be broken by video mode switches; so if (e.g.) network card sends an IRQ to network card driver while you're in real mode then somehow you're going to need to deal with that (e.g. have IRQ handlers in real mode that switch back to long mode and let the network card driver handle the IRQ; or keep track of all "missed" IRQs and handle them later on when you return to long mode).
It's easier and cleaner (especially when you start thinking about how you're going to support UEFI, now or in future) to assume the firmware owns all the hardware initially (and the OS shouldn't touch any hardware initially), and at some point during boot the OS takes ownership of hardware (and never uses firmware after that).
Cheers,
Brendan