1

目前我正在尝试开发一个内核模块来控制硬件看门狗,当它没有被输入时关闭系统,看门狗应该。我知道已经有一个可以从 /dev/watchdog 文件“控制”的用户空间守护进程,但我需要一个内核模块。为了扩展这一点,我已经有一个内核模块来接收 GPIO 输入,为此注册了一个 IRQ,并希望该 IRQ 为看门狗提供数据。因此,如果 GPIO 中断在 X 时间内没有触发,系统就会关闭。

但是,我无法让看门狗头文件与我合作。我从这里https://www.kernel.org/doc/Documentation/watchdog/watchdog-kernel-api.txt阅读了关于看门狗内核 api 的小文档,但这似乎说它会起作用。

下面我粘贴了我的测试代码,用于让看门狗注册,然后是我遇到的错误。

测试看门狗.c

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/fcntl.h>
#include <linux/watchdog.h>

static int __init watchdog_init(void)
{
    struct watchdog_device test;
    printk(KERN_INFO "WATCHDOG\n");
    watchdog_register_device(&test);
    test.ops->start(&test);
    test.ops->ping(&test);
    return 0;
}

static void __exit watchdog_exit(void)
{
    printk(KERN_INFO "GG!!!\n");
}

MODULE_LICENSE("GPL");

module_init(watchdog_init);
module_exit(watchdog_exit);

生成文件

obj-m += testWatchdog.o

export EXTRA_CFLAGS := -std=gnu99

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

可以制作我拥有的代码,但是在运行 insmod 时出现错误

insmod:插入“./testWatchdog.ko”时出错:-1 参数无效

运行 dmesg 后,我看到以下错误

未知符号 watchdog_register_device (err 0)

如果有人可以请至少指出我正确的方向,我将不胜感激。我对内核模块相当陌生,但对此充满希望。

更新:出于某种原因,我决定重新运行这个 insmod 命令并且我的输出已经改变。据我所知,我没有改变任何东西,但也许我更新了一个文件。无论如何,当我现在尝试 insmod 时,我没有收到错误,但我的内核说我正在执行空指针取消引用。这是我的错误的输出。

[68964.787385] WATCHDOG
[68964.787405] BUG: unable to handle kernel NULL pointer dereference at 00000005
[68964.787409] IP: [<f84c7020>] watchdog_init+0x20/0x1000 [testWatchdog]
[68964.787420] *pdpt = 000000003676a001 *pde = 0000000000000000 
[68964.787423] Oops: 0000 [#1] SMP 
[68964.787427] Modules linked in: testWatchdog(OF+) vboxsf(OF) vesafb(F) microcode(F) vboxguest(OF) psmouse(F) serio_raw(F) i2c_piix4(F) mac_hid(F) vboxvideo(OF) rfcomm(F) bnep(F) parport_pc(F) bluetooth(F) ppdev(F) drm(F) binfmt_misc(F) lp(F) parport(F) pcnet32(F) ahci(F) libahci(F)
[68964.787446] Pid: 3180, comm: insmod Tainted: GF          O 3.8.0-34-generic #49~precise1-Ubuntu innotek GmbH VirtualBox/VirtualBox
[68964.787448] EIP: 0060:[<f84c7020>] EFLAGS: 00010296 CPU: 0
[68964.787451] EIP is at watchdog_init+0x20/0x1000 [testWatchdog]
[68964.787453] EAX: e1989e3c EBX: f84c4000 ECX: 00000000 EDX: 00000001
[68964.787455] ESI: 00001299 EDI: e1989f60 EBP: e1989eb8 ESP: e1989e38
[68964.787456]  DS: 007b ES: 007b FS: 00d8 GS: 00e0 SS: 0068
[68964.787461] CR0: 8005003b CR2: 00000005 CR3: 34865000 CR4: 000006f0
[68964.787477] DR0: 00000000 DR1: 00000000 DR2: 00000000 DR3: 00000000
[68964.787482] DR6: ffff0ff0 DR7: 00000400
[68964.787484] Process insmod (pid: 3180, ti=e1988000 task=f3f5e680 task.ti=e1988000)
[68964.787485] Stack:
[68964.787487]  f84c3024 c103fb63 00000000 e1989ed0 00000000 00000000 e1989eac c1040885
[68964.787492]  00000000 e1989e6c 00000000 f84c8000 00000001 00000000 80000000 e1989ed0
[68964.787496]  00000000 80000000 00000000 00000000 00000001 00000001 0003d5dc c10decd6
[68964.787501] Call Trace:
[68964.787528]  [<c103fb63>] ? cpa_flush_range+0x43/0xc0
[68964.787532]  [<c1040885>] ? change_page_attr_set_clr+0x225/0x250
[68964.787543]  [<c10decd6>] ? tracepoint_module_notify+0x26/0xc0
[68964.787546]  [<f84c7000>] ? 0xf84c6fff
[68964.787549]  [<c1040a7f>] ? set_memory_nx+0x5f/0x70
[68964.787552]  [<c1003054>] do_one_initcall+0x34/0x170
[68964.787555]  [<f84c7000>] ? 0xf84c6fff
[68964.787567]  [<c161b039>] do_init_module+0x80/0x1c6
[68964.787583]  [<c10af03b>] load_module+0x41b/0x5f0
[68964.787586]  [<c10acee0>] ? add_kallsyms+0x1e0/0x1e0
[68964.787611]  [<c12f92f2>] ? _copy_from_user+0x42/0x60
[68964.787615]  [<c10af296>] sys_init_module+0x86/0xa0
[68964.787620]  [<c163518d>] sysenter_do_call+0x12/0x28
[68964.787622] Code: <ff> 52 04 8b 55 d0 8d 45 84 ff 52 0c 31 c0 c9 c3 00 00 00 00 00 00
[68964.787642] EIP: [<f84c7020>] watchdog_init+0x20/0x1000 [testWatchdog] SS:ESP 0068:e1989e38
[68964.787646] CR2: 0000000000000005
[68964.787650] ---[ end trace fd9234fd8f14c123 ]---
4

0 回答 0