编写OpenSolaris内核模块

原创 2007年09月20日 14:10:00

On opening day, I chose to post an entry on adding a system call to OpenSolaris. Considering the feedback, I thought I'd continue with brief "How-To add to OpenSolaris" documents for a while. There's a lot to choose from here, so I'll just pick them off as quick as I can. Todays topic as adding a new kernel module to OpenSolaris.

For the sake of discussion, we will be adding a new module that does nothing apart from print a message on load and unload. It will be architecture-neutral, and be distributed as part of a separate package (to give you a taste of our packaging system). We'll continue my narcissistic tradition and name this the "schrock" module.

1. Adding source

To begin, you must put your source somewhere in the tree. It must be put somewhere under usr/src/uts/common, but exactly where depends on the type of module. Just about the only real rule is that filesystems go in the "fs" directory, but other than that there are no real rules. The bulk of the modules live in the "io" directory, since the majority of modules are drivers of some kind. For now, we'll put 'schrock.c' in the "io" directory:

#include <sys/modctl.h>
#include <sys/cmn_err.h>

static struct modldrv modldrv = {
&mod_miscops,
"schrock module %I%",
NULL
};

static struct modlinkage modlinkage = {
MODREV_1, (void *)&modldrv, NULL
};

int
_init(void)
{
cmn_err(CE_WARN, "OpenSolaris has arrived");
return (mod_install(&modlinkage));
}

int
_fini(void)
{
cmn_err(CE_WARN, "OpenSolaris has left the building");
return (mod_remove(&modlinkage));
}

int
_info(struct modinfo *modinfop)
{
return (mod_info(&modlinkage, modinfop));
}

The code is pretty simple, and is basically the minimum needed to add a module to the system. You notice we use 'mod_miscops' in our modldrv. If we were adding a device driver or filesystem, we would be using a different set of linkage structures.

2. Creating Makefiles

We must add two Makefiles to get this building:

usr/src/uts/intel/schrock/Makefile
usr/src/uts/sparc/schrock/Makefile

With contents similar to the following:

UTSBASE = ../..

MODULE = schrock
OBJECTS = $(SCHROCK_OBJS:%=$(OBJS_DIR)/%)
LINTS = $(SCHROCK_OBJS:%.o=$(LINTS_DIR)/%.ln)
ROOTMODULE = $(ROOT_MISC_DIR)/$(MODULE)

include $(UTSBASE)/intel/Makefile.intel

ALL_TARGET = $(BINARY)
LINT_TARGET = $(MODULE).lint
INSTALL_TARGET = $(BINARY) $(ROOTMODULE)

CFLAGS += $(CCVERBOSE)

.KEEP_STATE:

def: $(DEF_DEPS)

all: $(ALL_DEPS)

clean: $(CLEAN_DEPS)

clobber: $(CLOBBER_DEPS)

lint: $(LINT_DEPS)

modlintlib: $(MODLINTLIB_DEPS)

clean.lint: $(CLEAN_LINT_DEPS)

install: $(INSTALL_DEPS)

include $(UTSBASE)/intel/Makefile.targ

3. Modifying existing Makefiles

There are two remaining Makefile chores before we can continue. First, we have to add the set of files to usr/src/uts/common/Makefile.files:

KMDB_OBJS += kdrv.o

SCHROCK_OBJS += schrock.o

BGE_OBJS += bge_main.o bge_chip.o bge_kstats.o bge_log.o bge_ndd.o /
bge_atomic.o bge_mii.o bge_send.o bge_recv.o

If you had created a subdirectory for your module instead of placing it in "io", you would also have to add a set of rules to usr/src/uts/common/Makefile.rules. If you need to do this, make sure you get both the object targets and the lint targets, or you'll get build failures if you try to run lint.

You'll also need to modify the usr/src/uts/intel/Makefile.intel file, as well as the corresponding SPARC version:

MISC_KMODS      += usba usba10
MISC_KMODS += zmod
MISC_KMODS += schrock

#
# Software Cryptographic Providers (/kernel/crypto):
#

4. Creating a package

As mentioned previously, we want this module to live in its own package. We start by creating usr/src/pkgdefs/SUNWschrock and adding it to the list of COMMON_SUBDIRS in usr/src/pkgdefs/Makefile:

        SUNWsasnm /
SUNWsbp2 /
SUNWschrock /
SUNWscpr /
SUNWscpu /

Next, we have to add a skeleton package system. Since we're only adding a miscellaneous module and not a full blown driver, we only need a simple skeleton. First, there's the Makefile:

include ../Makefile.com

.KEEP_STATE:

all: $(FILES)
install: all pkg

include ../Makefile.targ

A 'pkgimfo.tmpl' file:

PKG=SUNWschrock
NAME="Sample kernel module"
ARCH="ISA"
VERSION="ONVERS,REV=0.0.0"
SUNW_PRODNAME="SunOS"
SUNW_PRODVERS="RELEASE/VERSION"
SUNW_PKGVERS="1.0"
SUNW_PKGTYPE="root"
MAXINST="1000"
CATEGORY="system"
VENDOR="Sun Microsystems, Inc."
DESC="Sample kernel module"
CLASSES="none"
HOTLINE="Please contact your local service provider"
EMAIL=""
BASEDIR=/
SUNW_PKG_ALLZONES="true"
SUNW_PKG_HOLLOW="true"

And 'prototype_com', 'prototype_i386', and 'prototype_sparc' (elided) files:

# prototype_i386
!include prototype_com

d none kernel/misc/amd64 755 root sys
f none kernel/misc/amd64/schrock 755 root sys
# prototype_com
i pkginfo

d none kernel 755 root sys
d none kernel/misc 755 root sys
f none kernel/misc/schrock 755 root sys

5. Putting it all together

If we pkgadd our package, or BFU to the resulting archives, we can see our module in action:

halcyon# modload /kernel/misc/schrock
Jun 19 12:43:35 halcyon schrock: WARNING: OpenSolaris has arrived
halcyon# modunload -i 197
Jun 19 12:43:50 halcyon schrock: WARNING: OpenSolaris has left the building

This process is common to all kernel modules (though packaging is simpler for those combined in SUNWckr, for example). Things get a little more complicated and a little more specific when you begin to talk about drivers or filesystems in particular. I'll try to create some simple howtos for those as well.


---------------------------------

This article is originally posted at http://blogs.sun.com/eschrock/entry/adding_a_module_to_opensolaris

内核模块的基本编写以及Makefile的编写

前面讲了内核模块怎么安装和卸载,那么现在就正式讲解一下内核模块怎么编写。 以一个在内核中打印出”hello world”到终端的例子讲解。 1、  首先要写模块加载时的入口函数和模块卸载时的入口函...
  • yby19870627
  • yby19870627
  • 2012年03月16日 13:13
  • 5243

编写一个简单的内核模块程序Hello World!

一、Linux内核模块基本原理Linux 内核模块(LKM)是一些在启动的操作系统内核需要时可以载入内核执行的代码块,不需要时由操作系统卸载。它们扩展了操作系统内核功能却不需要重新编译内核、启动系统。...
  • sh21_
  • sh21_
  • 2017年03月09日 16:47
  • 3031

编写一个简单的内核模块

#include #include #include static int __init lkp_init(void) { printk(KERN_EMERG"Hello,Kernel!...
  • FallingU
  • FallingU
  • 2017年07月26日 23:17
  • 320

Linux内核模块的编写方法和技巧

内核模块可以在不重新编译内核的情况下添加到内核代码中运行,其可以动态加载和卸载,因此可以根据需要将内核某些功能独立出来作为模块,而不是编译到内核中,这样可以减少内核大小,并且可以按照实际需求选择裁剪或...
  • jasonchen_gbd
  • jasonchen_gbd
  • 2015年04月12日 12:44
  • 1700

Linux内核模块编程-HelloWorld

HelloWorld内核开始断断续续学习内核,大概半年了,多少开始对内核有点感悟了,但是对于这个庞然大物我显得很渺小,在枯燥的内核源码之中似乎没有一点点成功的喜悦,因此我选择学习内核模块编程,通过编写...
  • zhangyifei216
  • zhangyifei216
  • 2015年11月07日 17:00
  • 2348

【Linux开发】编写属于你的第一个Linux内核模块

曾经多少次想要在内核游荡?曾经多少次茫然不知方向?你不要再对着它迷惘,让我们指引你走向前方…… 内核编程常常看起来像是黑魔法,而在亚瑟 C 克拉克的眼中,它八成就是了。Linux内核和它的用户空...
  • LG1259156776
  • LG1259156776
  • 2016年05月15日 20:54
  • 2170

Linux之使用内核模块增加一个系统调用

使用内核模块的方式添加系统调用 1,为什么? 编译内核的方式费时间,一般的PC机都要两三个小时,而且不方便调试,一旦出现问题前面的工作都前功尽弃,所以我使用内核模块的方式添加系统调用。 2,怎...
  • qq_26811393
  • qq_26811393
  • 2016年03月01日 10:22
  • 448

OpenSolaris的下载地址

本文发表于: http://www.usenix.cn/viewthread.php?tid=4&extra=page%3D1  转载请保留该地址OpenSolaris下载:http://www.op...
  • enjoyo
  • enjoyo
  • 2007年09月19日 09:40
  • 1510

linux ubuntu编写内核模块并添加

linux ubuntu编写内核模块并添加
  • haimianxiaodao
  • haimianxiaodao
  • 2016年04月19日 20:37
  • 1358

内核模块开发中makefile的制作(多文件)

main.c #include #include //MODULE_LICENSE("GPL"); //MODULE_AUTHOR("CFC"); //MODULE_DESCRIPTIO...
  • cfc1243570631
  • cfc1243570631
  • 2013年06月20日 16:47
  • 1106
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:编写OpenSolaris内核模块
举报原因:
原因补充:

(最多只允许输入30个字)