一、硬件watchdog和软件watchdog
Linux内核不仅为各种不同类型的watchdog硬件电路提供了驱动,还提供了一个基于定时器的纯软件watchdog驱动,软件watchdog基于内核的定时器实现,当内核或中断出现异常时,软件watchdog是无法复位系统,由于硬件电路设计无看门狗电路,只能选择软件watchdog,但是无法完全解决目前系统长时间运行偶尔宕机的问题,后续只能增加硬件设计,再从软件层面解决问题。
二、开启内核软件看门狗
make kernel_menuconfig — 进入内核配置界面:
输入 / 检索 watchdog
高通芯片ipq40XXX选择QCOM watchdog
2、重新编译
会在/dev/下生成/dev/watchdog和/dev/watchdog0,ps命令可以找到一个watchdogd的内核守护线程
3、驱动代码分析:
生成watchdogd线程和字符设备驱动/dev/watchdog驱动代码位置:linux/drivers/watchdog/watchdog_dev.c,
int __init watchdog_dev_init(void)
1127 {
1128 int err;
1129 struct sched_param param = {.sched_priority = MAX_RT_PRIO - 1,};
1130
1131 watchdog_kworker = kthread_create_worker(0, "watchdogd");//在CPU0上创建一个watchdogd线程
1132 if (IS_ERR(watchdog_kworker)) {
1133 pr_err("Failed to create watchdog kworker\n");
1134 return PTR_ERR(watchdog_kworker);
1135 }
1136 sched_setscheduler(watchdog_kworker->task, SCHED_FIFO, ¶m);
1137
1138 err = class_register(&watchdog_class);
1139 if (err < 0) {
1140 pr_err("couldn't register class\n");
1141 goto err_register;
1142 }
1143
1144 err = alloc_chrdev_region(&watchdog_devt, 0, MAX_DOGS, "watchdog");//字符设备号注册:内核自动分配主设备号
1145 if (err < 0) {
1146 pr_err("watchdog: unable to allocate char dev region\n");
1147 goto err_alloc;
1148 }
1149
static int watchdog_cdev_register(struct watchdog_device *wdd)962 {963 struct watchdog_core_data *wd_data;964 int err;965 966 wd_data = kzalloc(sizeof(struct watchdog_core_data), GFP_KERNEL);967 if (!wd_data)968 return -ENOMEM;969 mutex_init(&wd_data->lock);970 971 wd_data->wdd = wdd;972 wdd->wd_data = wd_data;973 974 if (IS_ERR_OR_NULL(watchdog_kworker)) {975 kfree(wd_data);976 return -ENODEV;977 }978 979 device_initialize(&wd_data->dev);980 wd_data->dev.devt = MKDEV(MAJOR(watchdog_devt), wdd->id);981 wd_data->dev.class = &watchdog_class;982 wd_data->dev.parent = wdd->parent;983 wd_data->dev.groups = wdd->groups;984 wd_data->dev.release = watchdog_core_data_release;985 dev_set_drvdata(&wd_data->dev, wdd);986 dev_set_name(&wd_data->dev, "watchdog%d", wdd->id);987 988 kthread_init_work(&wd_data->work, watchdog_ping_work);989 hrtimer_init(&wd_data->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);990 wd_data->timer.function = watchdog_timer_expired;991 992 if (wdd->id == 0) {993 old_wd_data = wd_data;994 watchdog_miscdev.parent = wdd->parent;995 err = misc_register(&watchdog_miscdev);
二、openwrt提供了watchdog的应用程序
通过make menuconfig搜索watchdog
经过编译会生成watchdog的应用程序,可以通过命令watchdog -t 10 -T 4 -F /dev/watchdog ,执行喂狗的操作,watchdog -help可以查看帮助,但是当用了kernel的内核软件watchdog以后,运行此命令会提示资源繁忙,主要是因为watchdogd线程使用了/dev/watchdog,如果想自己单独使用应用程序执行喂狗操作,需要改写驱动,只注册字符设备驱动,不生成watchdog线程即可。