[SeaBIOS] [PATCH] boot: Add boot failure catch-all handler to prevent VM crashes

Alexander Graf via SeaBIOS posted 1 patch 4 weeks, 1 day ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/seabios tags/patchew/20260106004901.21059-1-graf@amazon.com
There is a newer version of this series
src/boot.c      | 36 ++++++++++++++++++++++++++++++++++++
src/post.c      |  4 ++++
src/romlayout.S |  5 +++++
src/util.h      |  1 +
4 files changed, 46 insertions(+)
[SeaBIOS] [PATCH] boot: Add boot failure catch-all handler to prevent VM crashes
Posted by Alexander Graf via SeaBIOS 4 weeks, 1 day ago
Implement catch-all mechanism to handle invalid boot loaders that execute
random instructions and reach the VGA hole at 0xa0000, which would lead to
VM crashes with KVM_INTERNAL_ERROR.

When a BIOS boot loader gets corrupted, it can end up jumping across
address space and execute stray code. The typical symptom of that is
that it executes 0x0 (addw) instructions until the code reaches an MMIO
region, such as the VGA window. When running in KVM, attempting to
execute code from the MMIO window results in KVM_INTERNAL_ERROR exits
which crash the VM.

To prevent VM crashes before we reach such an MMIO window, introduce an
internal int 0xf0 handler and call it at strategic locations that should
never get executed in the first place. When we now have stray code
executing, these int calls cause an emergency print of "BIOS failed to
boot volume" and cleanly shut down the machine.

This is a nicer experience for users as it prints out why their system
broke and in addition it avoids KVM_INTERNAL_ERROR calls when a virtual
machine attempts to execute from MMIO because of a broken boot loader.

Signed-off-by: Alexander Graf <graf@amazon.com>
---
 src/boot.c      | 36 ++++++++++++++++++++++++++++++++++++
 src/post.c      |  4 ++++
 src/romlayout.S |  5 +++++
 src/util.h      |  1 +
 4 files changed, 46 insertions(+)

diff --git a/src/boot.c b/src/boot.c
index 5c37dafd..39a4f9a7 100644
--- a/src/boot.c
+++ b/src/boot.c
@@ -1044,3 +1044,39 @@ handle_19(void)
     BootSequence = 0;
     do_boot(0);
 }
+
+// INT f0h Boot Failure Service Entry Point
+void VISIBLE32FLAT
+handle_f0(void)
+{
+    printf("\n\nBIOS failed to boot volume\n\n ");
+
+    /* Try to shut down. Will busy loop on failure to shut down. */
+    apm_shutdown();
+}
+
+static const u8 catchall[0x10] = {
+    0xcd,  // INT
+    0xf0,  // interrupt number 0xF0
+    0xeb,  // JMP short
+    0xfc,  // -3 (jump back to INT)
+};
+
+static void
+install_bootfail_catchall_one(u8 *catchall_addr)
+{
+    memcpy(catchall_addr, catchall, sizeof(catchall));
+}
+
+/*
+ * Install the catch-all code just before VGA hole at 0xa0000 and at the end
+ * of the PMM zero region.
+ */
+void
+install_bootfail_catchall(void)
+{
+    /* Install just before the VGA hole */
+    install_bootfail_catchall_one((u8*)BUILD_LOWRAM_END - sizeof(catchall));
+    /* and after the PMM zero region */
+    install_bootfail_catchall_one((u8*)BUILD_EBDA_MINIMUM);
+}
diff --git a/src/post.c b/src/post.c
index f93106a1..bdacbdb8 100644
--- a/src/post.c
+++ b/src/post.c
@@ -68,6 +68,9 @@ ivt_init(void)
     // set vector 0x79 to zero
     // this is used by 'gardian angel' protection system
     SET_IVT(0x79, SEGOFF(0, 0));
+
+    // Boot failure catch-all interrupt (INT 0xF0)
+    SET_IVT(0xf0, FUNC16(entry_f0));
 }
 
 static void
@@ -115,6 +118,7 @@ interface_init(void)
 
     // Other interfaces
     boot_init();
+    install_bootfail_catchall();
     bios32_init();
     pmm_init();
     pnp_init();
diff --git a/src/romlayout.S b/src/romlayout.S
index c4a4635e..283a8f5a 100644
--- a/src/romlayout.S
+++ b/src/romlayout.S
@@ -684,6 +684,11 @@ entry_iret_official:
         ORG 0xff54
         IRQ_ENTRY_ARG 05
 
+        // Boot failure catch-all interrupt handler (INT 0xF0)
+        .global entry_f0
+entry_f0:
+        ENTRY_INTO32 _cfunc32flat_handle_f0
+
         ORG 0xfff0 // Power-up Entry Point
         .global reset_vector
 reset_vector:
diff --git a/src/util.h b/src/util.h
index aff8e888..3c5d075c 100644
--- a/src/util.h
+++ b/src/util.h
@@ -47,6 +47,7 @@ int boot_lchs_find_scsi_device(struct pci_device *pci, int target, int lun,
                                struct chs_s *chs);
 int boot_lchs_find_ata_device(struct pci_device *pci, int chanid, int slave,
                               struct chs_s *chs);
+void install_bootfail_catchall(void);
 
 // bootsplash.c
 void enable_vga_console(void);
-- 
2.47.1




Amazon Web Services Development Center Germany GmbH
Tamara-Danz-Str. 13
10243 Berlin
Geschaeftsfuehrung: Christof Hellmis, Andreas Stieger
Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B
Sitz: Berlin
Ust-ID: DE 365 538 597

_______________________________________________
SeaBIOS mailing list -- seabios@seabios.org
To unsubscribe send an email to seabios-leave@seabios.org
[SeaBIOS] Re: [PATCH] boot: Add boot failure catch-all handler to prevent VM crashes
Posted by Gerd Hoffmann via SeaBIOS 2 weeks, 5 days ago
On Tue, Jan 06, 2026 at 12:49:01AM +0000, Alexander Graf via SeaBIOS wrote:
> Implement catch-all mechanism to handle invalid boot loaders that execute
> random instructions and reach the VGA hole at 0xa0000, which would lead to
> VM crashes with KVM_INTERNAL_ERROR.
> 
> When a BIOS boot loader gets corrupted, it can end up jumping across
> address space and execute stray code. The typical symptom of that is
> that it executes 0x0 (addw) instructions until the code reaches an MMIO
> region, such as the VGA window. When running in KVM, attempting to
> execute code from the MMIO window results in KVM_INTERNAL_ERROR exits
> which crash the VM.
> 
> To prevent VM crashes before we reach such an MMIO window, introduce an
> internal int 0xf0 handler and call it at strategic locations that should
> never get executed in the first place. When we now have stray code
> executing, these int calls cause an emergency print of "BIOS failed to
> boot volume" and cleanly shut down the machine.

Does it make sense to include a hint in the error message that a
corrupted disk / boot loader might be the root cause?

I've never seen that, care to share some real world examples where
this actually happens?

take care,
  Gerd

_______________________________________________
SeaBIOS mailing list -- seabios@seabios.org
To unsubscribe send an email to seabios-leave@seabios.org
[SeaBIOS] Re: [PATCH] boot: Add boot failure catch-all handler to prevent VM crashes
Posted by Alexander Graf via SeaBIOS 2 weeks, 5 days ago
On 15.01.26 10:17, Gerd Hoffmann via SeaBIOS wrote:
> On Tue, Jan 06, 2026 at 12:49:01AM +0000, Alexander Graf via SeaBIOS wrote:
>> Implement catch-all mechanism to handle invalid boot loaders that execute
>> random instructions and reach the VGA hole at 0xa0000, which would lead to
>> VM crashes with KVM_INTERNAL_ERROR.
>>
>> When a BIOS boot loader gets corrupted, it can end up jumping across
>> address space and execute stray code. The typical symptom of that is
>> that it executes 0x0 (addw) instructions until the code reaches an MMIO
>> region, such as the VGA window. When running in KVM, attempting to
>> execute code from the MMIO window results in KVM_INTERNAL_ERROR exits
>> which crash the VM.
>>
>> To prevent VM crashes before we reach such an MMIO window, introduce an
>> internal int 0xf0 handler and call it at strategic locations that should
>> never get executed in the first place. When we now have stray code
>> executing, these int calls cause an emergency print of "BIOS failed to
>> boot volume" and cleanly shut down the machine.
> Does it make sense to include a hint in the error message that a
> corrupted disk / boot loader might be the root cause?


Definitely! Will add in v2.


> I've never seen that, care to share some real world examples where
> this actually happens?


We've seen it multiple times over the last few years, usually when users 
corrupt their grub stage 1.5 partition. The latest iteration was a 
customer downgrading Debian 13 to 11 which wiped the stage 1.5 partition 
("BIOS boot"), but then failed to install the old boot loader into it 
again. dd if=/dev/zero of=/dev/nvme0n1p14 on Ubuntu 20.04 was also how I 
reproduced it.


Alex




Amazon Web Services Development Center Germany GmbH
Tamara-Danz-Str. 13
10243 Berlin
Geschaeftsfuehrung: Christof Hellmis, Andreas Stieger
Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B
Sitz: Berlin
Ust-ID: DE 365 538 597
_______________________________________________
SeaBIOS mailing list -- seabios@seabios.org
To unsubscribe send an email to seabios-leave@seabios.org