^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* UML hardware watchdog, shamelessly stolen from:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * SoftDog 0.05: A Software Watchdog Device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * (c) Copyright 1996 Alan Cox <alan@redhat.com>, All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * http://www.redhat.com
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * This program is free software; you can redistribute it and/or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * modify it under the terms of the GNU General Public License
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * as published by the Free Software Foundation; either version
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * 2 of the License, or (at your option) any later version.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * warranty for any of this software. This material is provided
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * "AS-IS" and at no charge.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * Software only watchdog driver. Unlike its big brother the WDT501P
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * driver this won't always recover a failed machine.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * 03/96: Angelo Haritsis <ah@doc.ic.ac.uk> :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * Modularised.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * Added soft_margin; use upon insmod to change the timer delay.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * NB: uses same minor as wdt (WATCHDOG_MINOR); we could use separate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * minors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * 19980911 Alan Cox
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * Made SMP safe for 2.3.x
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * 20011127 Joel Becker (jlbec@evilplan.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * Added soft_noboot; Allows testing the softdog trigger without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * requiring a recompile.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * Added WDIOC_GETTIMEOUT and WDIOC_SETTIMOUT.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #include <linux/miscdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #include <linux/watchdog.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #include <linux/reboot.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #include "mconsole.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static DEFINE_MUTEX(harddog_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) static DEFINE_SPINLOCK(lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) static int timer_alive;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static int harddog_in_fd = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static int harddog_out_fd = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * Allow only one person to hold it open
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) extern int start_watchdog(int *in_fd_ret, int *out_fd_ret, char *sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) static int harddog_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) int err = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) char *sock = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) mutex_lock(&harddog_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) spin_lock(&lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if(timer_alive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) #ifdef CONFIG_WATCHDOG_NOWAYOUT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) __module_get(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) #ifdef CONFIG_MCONSOLE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) sock = mconsole_notify_socket();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) err = start_watchdog(&harddog_in_fd, &harddog_out_fd, sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if(err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) timer_alive = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) spin_unlock(&lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) mutex_unlock(&harddog_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) return stream_open(inode, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) spin_unlock(&lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) mutex_unlock(&harddog_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) extern void stop_watchdog(int in_fd, int out_fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static int harddog_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * Shut off the timer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) spin_lock(&lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) stop_watchdog(harddog_in_fd, harddog_out_fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) harddog_in_fd = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) harddog_out_fd = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) timer_alive=0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) spin_unlock(&lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) extern int ping_watchdog(int fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static ssize_t harddog_write(struct file *file, const char __user *data, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * Refresh the timer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if(len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) return ping_watchdog(harddog_out_fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) static int harddog_ioctl_unlocked(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) void __user *argp= (void __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static struct watchdog_info ident = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) WDIOC_SETTIMEOUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) "UML Hardware Watchdog"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) case WDIOC_GETSUPPORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if(copy_to_user(argp, &ident, sizeof(ident)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) case WDIOC_GETSTATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) case WDIOC_GETBOOTSTATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return put_user(0,(int __user *)argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) case WDIOC_KEEPALIVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return ping_watchdog(harddog_out_fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) static long harddog_ioctl(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) mutex_lock(&harddog_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) ret = harddog_ioctl_unlocked(file, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) mutex_unlock(&harddog_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) static const struct file_operations harddog_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) .write = harddog_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) .unlocked_ioctl = harddog_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) .compat_ioctl = compat_ptr_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) .open = harddog_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) .release = harddog_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) .llseek = no_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) static struct miscdevice harddog_miscdev = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) .minor = WATCHDOG_MINOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) .name = "watchdog",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) .fops = &harddog_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) module_misc_device(harddog_miscdev);