阅读笔记:如何给OpenSolaris增加一个系统调用

原作者: Eric Schrock
原文来自: http://blogs.sun.com/roller/page/eschrock

译注者: Badcoffee
Email: blog.oliver@gmail.com
Blog: http://blog.csdn.net/yayong
2005年7月


按:给操作系统增加系统一个简单的系统调用是熟悉OS的内核代码的一个好方法,本文给出了在Solaris内核中增加一个系统调用的基本步骤。

How to add a system call to OpenSolaris


When I first started in the Solaris group, I was faced with two equally difficult tasks: learning the development model, and understanding the source code. For both these tasks, the recommended method is usually picking a small bug and working through the process. For the curious, the first bug I putback to ON was 4912227 (ptree call returns zero on failure), a simple bug with near zero risk. It was the first step down a very long road.

As a another first step, someone suggested adding a very simple system call to the kernel. This turned out to be a whole lot harder than one would expect, and has so many subtle(细微的) aspects(方面) that experienced Solaris engineers (myself included) still miss some of the necessary changes. With that in mind, I thought a reasonable first OpenSolaris blog would be describing exactly how to add a new system call to the kernel.

For the purposes of this post, we will assume that it's a simple system call that lives in the generic kernel code, and we'll put the code into an existing file to avoid having to deal with Makefiles. The goal is to print an arbitrary(随意的) message to the console whenever the system call is issued.

注:
1. 做Solaris研发面临2个难题,一个是需要了解Solaris研发的模式,或说是process上的东西;而另一个就是理解Solairs源代码 了。有一个最好的办法就是选择Solaris上一个很小的bug来熟悉process上的东西。
2. 而理解Solaris的源代码,最好是从增加一个很简单的系统调用开始。但是这有一点难,有很多细微之处即便是有经验的Solaris工程师也会遗漏。 而本篇文章的作者将以此为起点,描述如何给Solaris的kernel增加一个系统调用。
3. 为尽量简化,作者把新增调用的代码放到了已存在的源文档中,来避免对Makefile的改变。这个新的系统调用只是在被调用时输出任意的信息到 console上。

1. Picking a syscall number

Before writing any real code, we first have to pick a number that will represent our system call. The main source of documentation here is syscall.h, which describes all the available system call numbers, as well as which ones are reserved. The maximum number of syscalls is currently 256 (NSYSCALL), which doesn't leave much space for new ones. This could theoretically be extended - I believe the hard limit is in the size of sysset_t, whose 16 integers must be able to represent a complete bitmask of all system calls. This puts our actual limit at 16*32, or 512, system calls. But for the purposes of our tutorial, we'll pick system call number 56, which is currently unused. For my own amusement(娱乐), we'll name our (my?) system call 'schrock'. So first we add the following line to syscall.h

#define SYS_uadmin      55
#define SYS_schrock 56
#define SYS_utssys 57

注:
4. 第1步,需要选择一个系统调用号,需要在syscall.h里 增加一个定义,这个头文档包含了现在系统任何可用的系统调用号。
5. 系统最大的调用号数是systm.h文 件的NSYSCALL定义的,现在的值是256,实际上256被占用,没有空间增加新的调用号。
6. 理论上,能够扩展最大调用号,但sysset_t对 这个有限制,他是任何系统调用的位掩码,在syscall.h的 定义中表明,他的最大位数是16*32=512。
7. 为简化问题,作者使用了调用号56,这个号恰好没有被使用过,而系统调用的名字就叫“schrock"。

2. Writing the syscall handler

Next, we have to actually add the function that will get called when we invoke the system call. What we should really do is add a new file schrock.c to usr/src/uts/common/syscall, but I'm trying to avoid Makefiles. Instead, we'll just stick it in getpid.c:

#include <sys/cmn_err.h>

int
schrock(void *arg)
{
char buf[1024];
size_t len;

if (copyinstr(arg, buf, sizeof (buf), &len) != 0)
return (set_errno(EFAULT));

cmn_err(CE_WARN, "%s", buf);

return (0);
}

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!