免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1665 | 回复: 0
打印 上一主题 下一主题

FreeBSD模块开发最简单示例 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-10-28 12:20 |只看该作者 |倒序浏览
FreeBSD运行时,能够将需要的功能编译到内核中去,这就是宏内核模式,但目前的FreeBSD内核支持动态内核的载入和卸载。能够在使用的时候,动态的将模块加载到内存中。
举个例子,可以将网卡驱动编译成单独的模块,而不是将网卡编导内核中去,这样内核能够减小体积,效率更高,在需要的时候可以动态的加载到内核中去。使用kldload命令,kldunload命令卸载模块。kldstat命令查看当前运行的模块。
下面给出一个最简单的模块实例:(/usr/src/share/example/kld)

/*-
* Copyright (c) 1999 Assar Westerlund
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD: src/share/examples/kld/syscall/module/syscall.c,v 1.5 2007/07/22 06:48:34 kevlo Exp $
*/
#include sys/types.h>
#include sys/param.h>
#include sys/proc.h>
#include sys/module.h>
#include sys/sysproto.h>
#include sys/sysent.h>
#include sys/kernel.h>
#include sys/systm.h>
/*
* The function for implementing the syscall.
*/
static int
hello (struct thread *td, void *arg)
{
    printf ("hello kernel\n");
    return 0;
}
/*
* The `sysent' for the new syscall
*/
static struct sysent hello_sysent = {
    0,            /* sy_narg */
    hello            /* sy_call */
};
/*
* The offset in sysent where the syscall is allocated.
*/
static int offset = NO_SYSCALL;
/*
* The function called at load/unload.
*/
static int
load (struct module *module, int cmd, void *arg)
{
    int error = 0;
    switch (cmd) {
    case MOD_LOAD :
        printf ("syscall loaded at %d\n", offset);
        break;
    case MOD_UNLOAD :
        printf ("syscall unloaded from %d\n", offset);
        break;
    default :
        error = EOPNOTSUPP;
        break;
    }
    return error;
}
SYSCALL_MODULE(syscall, &offset, &hello_sysent, load, NULL);
这里定义了一个模块,模块的功能是在载入和卸载的时候打印信息在接入调用的时候显示“hello kernel”,下面是一个调用这个模块的函数:

/*-
* Copyright (c) 1999 Assar Westerlund
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD: src/share/examples/kld/syscall/test/call.c,v 1.3 1999/08/28 00:19:24 peter Exp $
*/
#include stdio.h>
#include sys/syscall.h>
#include sys/types.h>
#include sys/module.h>
static void usage (void);
static void
usage (void)
{
    fprintf (stderr, "call syscall-number\n");
    exit (1);
}
int
main(int argc, char **argv)
{
    char *endptr;
    int syscall_num;
    struct module_stat stat;
    stat.version = sizeof(stat);
    modstat(modfind("syscall"), &stat);
    syscall_num = stat.data.intval;
    return syscall (syscall_num);
}
这个程序,在每次运行的时候都会查找叫做syscall的模块,并调用这个模块,这是会显示“hello kernel”。
对于模块编程,编译也是需要考虑的,下面是Makefile文件:

# Makefile for simple caller of syscall
# $FreeBSD: src/share/examples/kld/syscall/test/Makefile,v 1.4 2006/03/15 10:40:01 ru Exp $
PROG=    call
NO_MAN=
.include bsd.prog.mk>

这是第二个调用程序的Makefile,编译时会自动生成编译环境,从而编译模块内容。
几乎所有的模块编程都是这样的一个结构,这里只给出一个最简单的例子,对应这个例子,可以开发很多复杂的模块。
而对于驱动开发,还有另外一些方法,包括在初始化阶段对设备的探测,挂在,初始化,中断处理,在卸载阶段的中断处理和配置清零等等。驱动开发重要的是读取硬件参数,然后使用相应的命令去执行硬件命令,从而操作硬件运行。


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/3176/showart_1346484.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP