Skip to contentSkip to conversation

Hardware watchdogs (minor update, version 4)

PATCH In review Richard W.M. Jones 3 participants 5 messages Mar 6, 2009 – Mar 13, 2009 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Hardware watchdogs (minor update, version 4)

This is the hardware virtual watchdogs patch. The only change since the last time I posted it[1] is that I've rebased to latest SVN and I've changed the header files to use non-reserved QEMU_* symbols instead of _QEMU_* (thanks Marc for pointing this out).

Rich.

[1] http://lists.gnu.org/archive/html/qemu-devel/2009-03/msg00050.html

    http://lists.gnu.org/archive/html/qemu-devel/2009-02/msg01434.html
    http://lists.gnu.org/archive/html/qemu-devel/2009-02/msg01404.html
Signature · 4 lines
Richard Jones, Emerging Technologies, Red Hat  http://et.redhat.com/~rjones
Read my OCaml programming blog: http://camltastic.blogspot.com/
Fedora now supports 68 OCaml packages (the OPEN alternative to F#)
http://cocan.org/getting_started_with_ocaml_on_red_hat_and_fedora
Makefile.target +1 -0
Index: Makefile.target
===================================================================
@@ -584,6 +584,7 @@
 OBJS+= cirrus_vga.o apic.o parallel.o acpi.o piix_pci.o
 OBJS+= usb-uhci.o vmmouse.o vmport.o vmware_vga.o hpet.o
 OBJS += device-hotplug.o pci-hotplug.o
+OBJS+= wdt_ib700.o wdt_i6300esb.o
 CPPFLAGS += -DHAS_AUDIO -DHAS_AUDIO_CHOICE
 endif
 ifeq ($(TARGET_BASE_ARCH), ppc)
vl.c +13 -0
Index: vl.c
===================================================================
@@ -43,6 +43,7 @@
 #include "migration.h"
 #include "kvm.h"
 #include "balloon.h"
+#include "watchdog.h"
 
 #include <unistd.h>
 #include <fcntl.h>
@@ -4103,6 +4104,8 @@
            "-old-param      old param mode\n"
 #endif
            "-tb-size n      set TB size\n"
+           "-watchdog ib700|i6300esb[,action=reset|shutdown|poweroff|pause]\n"
+           "                enable virtual hardware watchdog [default=none]\n"
            "-incoming p     prepare for incoming migration, listen on port p\n"
 #ifndef _WIN32
            "-chroot dir     Chroot to dir just before starting the VM.\n"
@@ -4219,6 +4222,7 @@
     QEMU_OPTION_startdate,
     QEMU_OPTION_icount,
     QEMU_OPTION_echr,
+    QEMU_OPTION_watchdog,
     QEMU_OPTION_virtiocon,
     QEMU_OPTION_show_cursor,
     QEMU_OPTION_semihosting,
@@ -4347,6 +4351,7 @@
     { "startdate", HAS_ARG, QEMU_OPTION_startdate },
     { "icount", HAS_ARG, QEMU_OPTION_icount },
     { "echr", HAS_ARG, QEMU_OPTION_echr },
+    { "watchdog", HAS_ARG, QEMU_OPTION_watchdog },
     { "virtioconsole", HAS_ARG, QEMU_OPTION_virtiocon },
     { "show-cursor", 0, QEMU_OPTION_show_cursor },
 #if defined(TARGET_ARM) || defined(TARGET_M68K)
@@ -5086,6 +5091,14 @@
                 serial_devices[serial_device_index] = optarg;
                 serial_device_index++;
                 break;
+            case QEMU_OPTION_watchdog:
+                if (wdt_option) {
+                    fprintf (stderr,
+                             "qemu: only one watchdog option may be given\n");
+                    exit (1);
+                }
+                wdt_option = optarg;
+                break;
             case QEMU_OPTION_virtiocon:
                 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
                     fprintf(stderr, "qemu: too many virtio consoles\n");
Makefile +1 -0
Index: Makefile
===================================================================
@@ -85,6 +85,7 @@
 OBJS+=bt.o bt-host.o bt-vhci.o bt-l2cap.o bt-sdp.o bt-hci.o bt-hid.o usb-bt.o
 OBJS+=buffered_file.o migration.o migration-tcp.o net.o qemu-sockets.o
 OBJS+=qemu-char.o aio.o net-checksum.o savevm.o cache-utils.o
+OBJS+=watchdog.o
 
 ifdef CONFIG_BRLAPI
 OBJS+= baum.o
watchdog.c +161 -0

161 lines not on this page · raw message

qemu-doc.texi +32 -0
Index: qemu-doc.texi
===================================================================
@@ -1161,6 +1161,38 @@
 @item -echr 20
 @end table
 
+@item -watchdog @var{model}[,action=@var{action}]
+Create a virtual hardware watchdog device.  Once enabled (by a guest
+action), the watchdog must be periodically polled by an agent inside
+the guest or else the guest will be restarted.
+
+The @var{model} is the model of hardware watchdog to emulate.  Choices
+for model are: @code{ib700} (iBASE 700) which is a very simple ISA
+watchdog with a single timer, or @code{i6300esb} (Intel 6300ESB I/O
+controller hub) which is a much more featureful PCI-based dual-timer
+watchdog.  Choose a model for which your guest has drivers.
+
+The @var{action} controls what QEMU will do when the timer expires.
+The default is
+@code{reset} (forcefully reset the guest).
+Other possible actions are:
+@code{shutdown} (attempt to gracefully shutdown the guest),
+@code{poweroff} (forcefully poweroff the guest), or
+@code{pause} (pause the guest).
+
+Note that the @code{shutdown} action requires that the guest responds
+to ACPI signals, which it may not be able to do in the sort of
+situations where the watchdog would have expired, and thus
+@code{action=shutdown} is not recommended for production use.
+
+Use @code{-watchdog ?} to list available hardware models.  Only one
+watchdog can be enabled for a guest.
+
+@table @code
+@item -watchdog i6300esb,action=pause
+@item -watchdog ib700
+@end table
+
 @item -chroot dir
 Immediately before starting guest execution, chroot to the specified
 directory.  Especially useful in combination with -runas.
watchdog.h +56 -0

56 lines not on this page · raw message

wdt_i6300esb.c +538 -0

538 lines not on this page · raw message

wdt_i6300esb.h +28 -0
Index: hw/wdt_i6300esb.h
===================================================================
@@ -0,0 +1,28 @@
+/*
+ * Virtual hardware watchdog.
+ *
+ * Copyright (C) 2009 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * By Richard W.M. Jones ([email protected]).
+ */
+
+#ifndef QEMU_WDT_I6300ESB_H
+#define QEMU_WDT_I6300ESB_H
+
+extern void wdt_i6300esb_init (void);
+
+#endif /* QEMU_WDT_I6300ESB_H */
wdt_ib700.c +137 -0

137 lines not on this page · raw message

wdt_ib700.h +28 -0
Index: hw/wdt_ib700.h
===================================================================
@@ -0,0 +1,28 @@
+/*
+ * Virtual hardware watchdog.
+ *
+ * Copyright (C) 2009 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * By Richard W.M. Jones ([email protected]).
+ */
+
+#ifndef QEMU_WDT_IB700_H
+#define QEMU_WDT_IB700_H
+
+extern void wdt_ib700_init (void);
+
+#endif /* QEMU_WDT_IB700_H */
pc.c +7 -0
Index: hw/pc.c
===================================================================
@@ -37,6 +37,9 @@
 #include "virtio-balloon.h"
 #include "virtio-console.h"
 #include "hpet_emul.h"
+#include "wdt_ib700.h"
+#include "wdt_i6300esb.h"
+#include "watchdog.h"
 
 /* output Bochs bios info messages */
 //#define DEBUG_BIOS
@@ -1007,6 +1010,10 @@
         }
     }
 
+    wdt_ib700_init ();
+    wdt_i6300esb_init ();
+    watchdog_pc_init (pci_bus);
+
     for(i = 0; i < nb_nics; i++) {
         NICInfo *nd = &nd_table[i];
 
#1 lore raw
Quoted 1 line · Richard W.M. Jones

instead of _QEMU_* (thanks Marc for pointing this out).

s/Marc/malc/

Signature · 4 lines
Richard Jones, Emerging Technologies, Red Hat  http://et.redhat.com/~rjones
virt-df lists disk usage of guests without needing to install any
software inside the virtual machine.  Supports Linux and Windows.
http://et.redhat.com/~rjones/virt-df/
#2 lore raw
In reply to Richard W.M. Jones
Quoted 1 line · Richard W.M. Jones

This is the hardware virtual watchdogs patch.

Anyone? This is a pretty useful little feature ...

Rich.

Signature · 4 lines
Richard Jones, Emerging Technologies, Red Hat  http://et.redhat.com/~rjones
virt-p2v converts physical machines to virtual machines.  Boot with a
live CD or over the network (PXE) and turn machines into Xen guests.
http://et.redhat.com/~rjones/virt-p2v
#3 lore raw
In reply to Richard W.M. Jones
Quoted 38 lines · Richard W.M. Jones

This is the hardware virtual watchdogs patch. The only change since the last time I posted it[1] is that I've rebased to latest SVN and I've changed the header files to use non-reserved QEMU_* symbols instead of _QEMU_* (thanks Marc for pointing this out).

Rich.

[1] http://lists.gnu.org/archive/html/qemu-devel/2009-03/msg00050.html

    http://lists.gnu.org/archive/html/qemu-devel/2009-02/msg01434.html
    http://lists.gnu.org/archive/html/qemu-devel/2009-02/msg01404.html

Index: Makefile.target
===================================================================

Makefile.target +1 -0
@@ -584,6 +584,7 @@
 OBJS+= cirrus_vga.o apic.o parallel.o acpi.o piix_pci.o
 OBJS+= usb-uhci.o vmmouse.o vmport.o vmware_vga.o hpet.o
 OBJS += device-hotplug.o pci-hotplug.o
+OBJS+= wdt_ib700.o wdt_i6300esb.o
 CPPFLAGS += -DHAS_AUDIO -DHAS_AUDIO_CHOICE
 endif
 ifeq ($(TARGET_BASE_ARCH), ppc)

Index: vl.c
===================================================================

vl.c +1 -0
@@ -43,6 +43,7 @@
 #include "migration.h"
 #include "kvm.h"
 #include "balloon.h"
+#include "watchdog.h"
 
 #include <unistd.h>
 #include <fcntl.h>

vl.c line 4109

Quoted 6 lines
vl.c +2 -0
@@ -4103,6 +4104,8 @@
            "-old-param      old param mode\n"
 #endif
            "-tb-size n      set TB size\n"
+           "-watchdog ib700|i6300esb[,action=reset|shutdown|poweroff|pause]\n"
+           "                enable virtual hardware watchdog [default=none]\n"
 

It would be good to have two separate options. One that enabled the watch dog device and then another that controlled what the behavior of it is. The goal is to separate guest configuration information from how the emulated device is configured in the host. If it's not already there, the host configuration should be changable via the monitor too.

Quoted 39 lines
vl.c +0 -0
 ifdef CONFIG_BRLAPI
 OBJS+= baum.o

Index: watchdog.c
===================================================================

watchdog.c +31 -0
@@ -0,0 +1,161 @@
+/*
+ * Virtual hardware watchdog.
+ *
+ * Copyright (C) 2009 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * By Richard W.M. Jones ([email protected]).
+ */
+
+#include "qemu-common.h"
+#include "sysemu.h"
+#include "hw/wdt_ib700.h"
+#include "hw/wdt_i6300esb.h"
+#include "watchdog.h"
+
+/* Possible values for action parameter. */
+#define WDT_RESET        1
+#define WDT_SHUTDOWN     2

watchdog.c line 41

Quoted 10 lines
watchdog.c +10 -0
+#define WDT_POWEROFF     3
+#define WDT_PAUSE        4
+
+static void parse_option (const char *arg, int *action_r);
+static void parse_rest (const char *rest, int *action_r);
+
+/* Linked list of models - virtual watchdog devices add themselves
+ * to this list.
+ */
+wdt_model *wdt_models = NULL;

This isn't QEMU style. WatchdogTimerModel would be. Also, you can use the macros in sys-queue.h for lists.

watchdog.c line 44

Quoted 3 lines
watchdog.c +2 -0
+/* The raw -watchdog option specified on the command line. */
+const char *wdt_option = NULL;

I'd rather that command line arguments be parsed in vl.c to cleanly separate things. If you do my above suggestion about splitting up options though, this problem goes away.

Quoted 6 lines
watchdog.c +5 -0
+static wdt_model *wdt = NULL;
+
+/* Called from the PC code to parse the option and finally configure
+ * the device.
+ */

watchdog.c line 63

Quoted 10 lines
watchdog.c +10 -0
+void
+watchdog_pc_init (PCIBus *pci_bus)
+{
+    int action;
+
+    if (!wdt_option) return;
+    parse_option (wdt_option, &action);
+    if (!wdt) return;           /* No watchdog configured. */
+    wdt->wdt_methods->wdt_pc_init (pci_bus, action);
+}

If this is a PCI device, it should be hot pluggable, right?

The normal thing is for pc.c to directly call the device initialization. Definitely don't want to wait until then to parse the options.

Quoted 31 lines
watchdog.c +30 -0
+/* Device state. */
+struct state {
+    PCIDevice dev;              /* PCI device state, must be first field. */
+
+    int action;                 /* Action on expiry. */
+
+    int reboot_enabled;         /* "Reboot" on timer expiry.  The real action
+                                 * performed depends on the action=* param
+                                 * passed on QEMU command line.
+                                 */
+    int clock_scale;            /* Clock scale. */
+#define CLOCK_SCALE_1KHZ 0
+#define CLOCK_SCALE_1MHZ 1
+
+    int int_type;               /* Interrupt type generated. */
+#define INT_TYPE_IRQ 0          /* APIC 1, INT 10 */
+#define INT_TYPE_SMI 2
+#define INT_TYPE_DISABLED 3
+
+    int free_run;               /* If true, reload timer on expiry. */
+    int locked;                 /* If true, enabled field cannot be changed. */
+    int enabled;                /* If true, watchdog is enabled. */
+
+    QEMUTimer *timer;           /* The actual watchdog timer. */
+
+    uint32_t timer1_preload;    /* Values preloaded into timer1, timer2. */
+    uint32_t timer2_preload;
+    int stage;                  /* Stage (1 or 2). */
+
+    int unlock_state;           /* Guest writes 0x80, 0x86 to unlock the

watchdog.c line 98

Quoted 10 lines
watchdog.c +10 -0
+                                 * registers, and we transition through
+                                 * states 0 -> 1 -> 2 when this happens.
+                                 */
+
+    int previous_reboot_flag;   /* If the watchdog caused the previous
+                                 * reboot, this flag will be set.
+                                 */
+};
+
+typedef struct state state;

Way too generic of a type name.

Quoted 7 lines
watchdog.c +6 -0
+static void i6300esb_pc_init (PCIBus *pci_bus, int action);
+static void i6300esb_map (PCIDevice *dev, int region_num, uint32_t addr, uint32_t size, int type);
+static void i6300esb_config_write (PCIDevice *dev, uint32_t addr, uint32_t data, int len);
+static uint32_t i6300esb_config_read (PCIDevice *dev, uint32_t addr, int len);
+static uint32_t i6300esb_mem_readb (void *vp, target_phys_addr_t addr);
+static uint32_t i6300esb_mem_readw (void *vp, target_phys_addr_t addr);

watchdog.c line 114

Quoted 10 lines
watchdog.c +10 -0
+static uint32_t i6300esb_mem_readl (void *vp, target_phys_addr_t addr);
+static void i6300esb_mem_writeb (void *vp, target_phys_addr_t addr, uint32_t val);
+static void i6300esb_mem_writew (void *vp, target_phys_addr_t addr, uint32_t val);
+static void i6300esb_mem_writel (void *vp, target_phys_addr_t addr, uint32_t val);
+static void i6300esb_restart_timer (state *, int stage);
+static void i6300esb_disable_timer (state *);
+static void i6300esb_timer_expired (void *vp);
+static void i6300esb_reset (state *d);
+static void i6300esb_save (QEMUFile *f, void *vp);
+static int i6300esb_load (QEMUFile *f, void *vp, int version);

If you have to predefine all of these, you're probably doing something wrong...

Quoted 26 lines
watchdog.c +25 -0
+static wdt_methods i6300esb_wdt = {
+    .wdt_pc_init = i6300esb_pc_init,
+};
+
+static wdt_model model = {
+    .wdt_name = "i6300esb",
+    .wdt_description = "Intel 6300ESB",
+    .wdt_methods = &i6300esb_wdt,
+};
+
+void
+wdt_i6300esb_init (void)
+{
+    model.wdt_next = wdt_models;
+    wdt_models = &model;
+}
+
+/* Create and initialize a virtual Intel 6300ESB during PC creation. */
+static void
+i6300esb_pc_init (PCIBus *pci_bus, int action)
+{
+    state *d;
+    uint8_t *pci_conf;
+
+    if (!pci_bus) {

watchdog.c line 149

Quoted 10 lines
watchdog.c +10 -0
+        fprintf (stderr, "wdt_i6300esb: no PCI bus in this machine\n");
+        return;
+    }
+
+    d = (state *)
+        pci_register_device (pci_bus, "i6300esb_wdt", sizeof (state),
+                             -1,
+                             i6300esb_config_read, i6300esb_config_write);
+
+    d->action = action;

The device emulation shouldn't need to know the action. It should notify something else via a callback.

watchdog.c line 160

Quoted 12 lines
watchdog.c +11 -0
+
+static void
+i6300esb_config_write (PCIDevice *dev, uint32_t addr, uint32_t data, int len)
+{
+    state *d = (state *) dev;
+    int old;
+
+#ifdef I6300ESB_DEBUG
+    fprintf (stderr, "i6300esb_config_write: addr = %x, data = %x, len = %d\n",
+             addr, data, len);
+#endif

A debug macro would be nicer.

Quoted 11 lines
watchdog.c +10 -0
+static void
+i6300esb_save (QEMUFile *f, void *vp)
+{
+    state *d = (state *) vp;
+
+    pci_device_save (&d->dev, f);
+    qemu_put_be32 (f, d->action);
+    qemu_put_be32 (f, d->reboot_enabled);
+    qemu_put_be32 (f, d->clock_scale);
+    qemu_put_be32 (f, d->int_type);

watchdog.c

Quoted 10 lines
watchdog.c +10 -0
+    qemu_put_be32 (f, d->free_run);
+    qemu_put_be32 (f, d->locked);
+    qemu_put_be32 (f, d->enabled);
+    qemu_put_timer (f, d->timer);
+    qemu_put_be32 (f, d->timer1_preload);
+    qemu_put_be32 (f, d->timer2_preload);
+    qemu_put_be32 (f, d->stage);
+    qemu_put_be32 (f, d->unlock_state);
+    qemu_put_be32 (f, d->previous_reboot_flag);
+}

host state such as action should not be in the save/restore format.

Regards,

Anthony Liguori

#4 lore raw
Quoted 7 lines · Anthony Liguori
Quoted 5 lines · Richard W.M. Jones

This is the hardware virtual watchdogs patch. The only change since the last time I posted it[1] is that I've rebased to latest SVN and I've changed the header files to use non-reserved QEMU_* symbols instead of _QEMU_* (thanks Marc for pointing this out).

Quoted 5 lines
Quoted 5 lines

Rich.

+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

Please update the FSF address.