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.
This commit is contained in:
AI
2026-02-23 13:48:33 +00:00
parent c3c01049bf
commit 3512e937ae

View File

@@ -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);