Skip to contentSkip to conversation

at_keyboard flush on i386-qemu

PATCH In review Robert Millan 2 participants 7 messages Jun 27, 2009 – Jul 10, 2009

lore
Share & tools
lore
#0 lore raw

Hi,

Pavel pointed out earlier that sometimes when starting on i386-qemu GRUB receives spurious events from AT keyboard. It seems that it is the role of the firmware to flush this buffer at startup.

Unless someone has a better idea, I would fix this with:

- Moving at_keyboard to kernel on i386-qemu.

- [ifdef GRUB_MACHINE_QEMU]: flush the input buffer at at_keyboard startup by reading and discarding events for a fixed amount of time.

I don't like that we have a race here. Suggestions welcome on how to improve that, but TTBOMK if there's more than one event you can't tell when you're processing the last one.

Signature · 5 lines
Robert Millan

  The DRM opt-in fallacy: "Your data belongs to us. We will decide when (and
  how) you may access your data; but nobody's threatening your freedom: we
  still allow you to remove your data and not access it at all."
#1 lore raw
Quoted 4 lines · Robert Millan

Hi,

Pavel pointed out earlier that sometimes when starting on i386-qemu GRUB receives spurious events from AT keyboard.

Yes, it's still happening, but not always. Approximately half of the time I'm getting "2" at the prompt.

Quoted 9 lines

It seems that it is the role
of the firmware to flush this buffer at startup.

Unless someone has a better idea, I would fix this with:

- Moving at_keyboard to kernel on i386-qemu.

- [ifdef GRUB_MACHINE_QEMU]: flush the input buffer at at_keyboard startup by reading and discarding events for a fixed amount of time.

I would just read and discard the keyboard data from the port at startup without embedding at_keyboard.

Quoted 3 lines

I don't like that we have a race here. Suggestions welcome on how to improve that, but TTBOMK if there's more than one event you can't tell when you're processing the last one.

I think there is at most one event. And I suspect it's due to a qemu bug. Or at least qemu could do better by starting in the same state every time.

Signature · 2 lines
Regards,
Pavel Roskin
#2 lore raw
Quoted 3 lines · Pavel Roskin

I would just read and discard the keyboard data from the port at startup without embedding at_keyboard.

Seems fine.

Quoted 5 lines
Quoted 3 lines

I don't like that we have a race here. Suggestions welcome on how to improve that, but TTBOMK if there's more than one event you can't tell when you're processing the last one.

I think there is at most one event.

Actually, you need two events for a single key press (MAKE + BREAK), and I've seen it print two keys sometimes.

Quoted 2 lines

And I suspect it's due to a qemu
bug.

Well, other firmwares (coreboot, bios.bin) don't have this problem. It's probably something that you need to do on real hardware anyway (which we might want to support someday).

Signature · 5 lines
Robert Millan

  The DRM opt-in fallacy: "Your data belongs to us. We will decide when (and
  how) you may access your data; but nobody's threatening your freedom: we
  still allow you to remove your data and not access it at all."
#3 lore raw

Quoting Robert Millan <[email protected]>:

Quoted 7 lines · Pavel Roskin
Quoted 3 lines

I would just read and discard the keyboard data from the port at startup without embedding at_keyboard.

Seems fine.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

I don't like that we have a race here. Suggestions welcome on how to improve that, but TTBOMK if there's more than one event you can't tell when you're processing the last one.

I think there is at most one event.

Actually, you need two events for a single key press (MAKE + BREAK), and I've seen it print two keys sometimes.

OK, let's read e.g. 8 events to be safe.

Quoted 6 lines
Quoted 2 lines

And I suspect it's due to a qemu
bug.

Well, other firmwares (coreboot, bios.bin) don't have this problem. It's probably something that you need to do on real hardware anyway (which we might want to support someday).

That's what coreboot is for. Otherwise we would need to reimplement coreboot.

Signature · 2 lines
Regards,
Pavel Roskin
#4 lore raw

PATCH 1/1 use ljmp to reboot (Re: at_keyboard flush on i386-qemu)

In reply to Robert Millan
Quoted 9 lines · Robert Millan

Hi,

Pavel pointed out earlier that sometimes when starting on i386-qemu GRUB receives spurious events from AT keyboard. It seems that it is the role of the firmware to flush this buffer at startup.

Unless someone has a better idea, I would fix this with:

Quoted 8 lines

- Moving at_keyboard to kernel on i386-qemu.

- [ifdef GRUB_MACHINE_QEMU]: flush the input buffer at at_keyboard startup by reading and discarding events for a fixed amount of time.

I don't like that we have a race here. Suggestions welcome on how to improve that, but TTBOMK if there's more than one event you can't tell when you're processing the last one.

Actually I diagnosed this completely wrong. The real problem is we're using the keyboard to reboot the machine, which (at least on QEMU) results in spurious keys being put in the buffer.

I think we should just jump to 0xffff0 to reboot. I didn't know back when I wrote reboot.c, but this is supposedly more reliable since we're jumping to code in ROM which hasn't been overwritten (kern/i386/pc/startup.S uses the same trick for its own grub_reboot()).

See attached patch.

Signature · 5 lines
Robert Millan

  The DRM opt-in fallacy: "Your data belongs to us. We will decide when (and
  how) you may access your data; but nobody's threatening your freedom: we
  still allow you to remove your data and not access it at all."

2009-07-07 Robert Millan <[email protected]>

* conf/i386-coreboot.rmk (reboot_mod_SOURCES): Remove `kern/i386/reboot.c'.
* kern/i386/qemu/startup.S: Include `"../realmode.S"'. (grub_reboot): New function.
* kern/i386/realmode.S: Include `<grub/machine/machine.h>'. (prot_to_real): Only enable i8086 interrupts on i386-pc.
* include/grub/i386/coreboot/init.h (grub_reboot): New function prototype.

i386-coreboot.rmk +1 -1
Index: conf/i386-coreboot.rmk
===================================================================
@@ -174,7 +174,7 @@ linux_mod_CFLAGS = $(COMMON_CFLAGS)
 linux_mod_LDFLAGS = $(COMMON_LDFLAGS)
 
 # For reboot.mod.
-reboot_mod_SOURCES = commands/reboot.c kern/i386/reboot.c
+reboot_mod_SOURCES = commands/reboot.c
 reboot_mod_CFLAGS = $(COMMON_CFLAGS)
 reboot_mod_LDFLAGS = $(COMMON_LDFLAGS)
 
i386/qemu/startup.S +8 -0
Index: kern/i386/qemu/startup.S
===================================================================
@@ -95,3 +95,11 @@ codestart:
 
 	/* This should never happen.  */
 	jmp	EXT_C(grub_stop)
+
+#include "../realmode.S"
+
+FUNCTION(grub_reboot)
+	call    prot_to_real
+	.code16
+	ljmp	$0xf000, $0xfff0
+	.code32
i386/realmode.S +4 -0
Index: kern/i386/realmode.S
===================================================================
@@ -41,6 +41,8 @@
  *       with "ret $N" where N is ((the number of arguments) - 3) * 4.
  */
 
+#include <grub/machine/machine.h>
+
 /*
  *  This is the area for all of the special variables.
  */
@@ -215,8 +217,10 @@ realcseg:
 	movw	%ax, %gs
 	movw	%ax, %ss
 
+#ifdef GRUB_MACHINE_PCBIOS
 	/* restore interrupts */
 	sti
+#endif
 
 	/* return on new stack! */
 	DATA32	ret
grub/i386/coreboot/init.h +2 -1
Index: include/grub/i386/coreboot/init.h
===================================================================
@@ -1,6 +1,6 @@
 /*
  *  GRUB  --  GRand Unified Bootloader
- *  Copyright (C) 2007  Free Software Foundation, Inc.
+ *  Copyright (C) 2007,2009  Free Software Foundation, Inc.
  *
  *  GRUB is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -24,5 +24,6 @@
 
 void EXPORT_FUNC(grub_stop) (void) __attribute__ ((noreturn));
 void EXPORT_FUNC(grub_stop_floppy) (void);
+void EXPORT_FUNC (grub_reboot) (void) __attribute__ ((noreturn));
 
 #endif
#5 lore raw

Re: [PATCH] use ljmp to reboot (Re: at_keyboard flush on i386-qemu)

Quoted 4 lines · Robert Millan

I think we should just jump to 0xffff0 to reboot. I didn't know back when I wrote reboot.c, but this is supposedly more reliable since we're jumping to code in ROM which hasn't been overwritten (kern/i386/pc/startup.S uses the same trick for its own grub_reboot()).

I have no objections about the patch except that it breaks compilation for i386-coreboot:

kernel_img-symlist.o:(.data+0x3b4): undefined reference to `grub_reboot'

Perhaps kern/i386/reboot.c should be kept, but the code should only be enabled for coreboot.

By the way, GRUB_MACHINE_LINUXBIOS should be renamed to GRUB_MACHINE_COREBOOT. Its use in loader/multiboot_loader.c is dubious.

Signature · 2 lines
Regards,
Pavel Roskin
#6 lore raw

Re: [PATCH] use ljmp to reboot (Re: at_keyboard flush on i386-qemu)

Quoted 7 lines · Pavel Roskin
Quoted 4 lines · Robert Millan

I think we should just jump to 0xffff0 to reboot. I didn't know back when I wrote reboot.c, but this is supposedly more reliable since we're jumping to code in ROM which hasn't been overwritten (kern/i386/pc/startup.S uses the same trick for its own grub_reboot()).

Quoted 7 lines

I have no objections about the patch except that it breaks compilation for i386-coreboot:

kernel_img-symlist.o:(.data+0x3b4): undefined reference to `grub_reboot'

Perhaps kern/i386/reboot.c should be kept, but the code should only be enabled for coreboot.

Actually, coreboot has the same problem and should use the same approach (these two ports are almost identical).

Quoted 2 lines

By the way, GRUB_MACHINE_LINUXBIOS should be renamed to GRUB_MACHINE_COREBOOT.

Fine with me. Only lack of time prevents this :-)

Quoted 1 line

Its use in loader/multiboot_loader.c is dubious.

This should become "ifdef __i386__" someday. Unfortunately some things (e.g. mmap) associated with the multiboot loader are not portable.

Ah, but GRUB_MACHINE_QEMU should be there too. This can be added already.

Signature · 5 lines
Robert Millan

  The DRM opt-in fallacy: "Your data belongs to us. We will decide when (and
  how) you may access your data; but nobody's threatening your freedom: we
  still allow you to remove your data and not access it at all."