From 3512e937ae019bcd734ccfd6c0574decc1f8d5a4 Mon Sep 17 00:00:00 2001 From: AI Date: Mon, 23 Feb 2026 13:48:33 +0000 Subject: [PATCH] Fix keyboard init: add timeout to controller flush loop The PS/2 keyboard controller flush loop could hang infinitely on some VMs (UTM) where the controller keeps reporting data. Add a 1024-byte timeout to prevent the kernel from hanging during keyboard_init(). Also add debug prints showing flush progress for diagnostics. --- src/keyboard.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/keyboard.c b/src/keyboard.c index 27c0636..ea46e74 100644 --- a/src/keyboard.c +++ b/src/keyboard.c @@ -101,11 +101,21 @@ void keyboard_init(void) { kb_waiting_pid = 0; shift_pressed = 0; - /* Flush any pending data from the keyboard controller */ - while (inb(0x64) & 0x01) { + offset_print(" KEYBOARD: flushing controller...\n"); + + /* Flush any pending data from the keyboard controller. + * Use a timeout to avoid hanging if the controller keeps reporting data + * (some emulators/VMs behave differently). */ + int flush_count = 0; + while ((inb(0x64) & 0x01) && flush_count < 1024) { inb(KB_DATA_PORT); + flush_count++; } + offset_print(" KEYBOARD: flushed "); + print_hex((uint32_t)flush_count); + offset_print(" KEYBOARD: bytes, unmasking IRQ1...\n"); + /* Unmask IRQ1 (keyboard) in the PIC */ pic_clear_mask(1);