主要是根据以下步骤来的,不过原网页有些错误,少了些步骤,我加上了,经试验在我的Minix3.1.2a中可以成功了~
参考:http://www.phien.org/ucdavis/ta/ecs150-f03/syscall.html
《MINIX on Linux/VMware》 Silvia Figueira – Fall 2007 Lab 3
《How to Add a New System Call for Minix 3》 Karthick Jayaraman
Department of Electrical Engineering & Computer Science Syracuse University, Syracuse, New York
《System Call Implementation on Minix 3》 Felipe Caballero
Universidad Adolfo Ibá?ez, Santiago de Chile Sistemas Operativos 2006-Segundo Semestre
以下为http://www.phien.org/ucdavis/ta/ecs150-f03/syscall.html按我的步骤的修改版:
Example Specifics
We want to add a system call int foo( long num )
to Minix. The specifics for foo
are below:
Function: | Print message 'syscall foo( <#> ) called! ' to screen, where <#> is the value of parameter num . |
Returns: | If num is a long integer greater than 0, do_foo returns 0 . Otherwise, foo fails and returns -1 . |
Please note that foo
is actually the interface to the system call do_foo
that we will implement. Remember, the user calls a library function (in our case foo
). The library function then makes the actual system call (do_foo
).
Step 1: Kernel Modifications
All system calls in Minix correspond to a specific integer, as defined in /usr/include/minix/callnr.h
. So, the first thing we should do is associate our new system call to an integer value.
- Open
/usr/include/minix/callnr.h
- Increase the value of
NCALLS
by one - At the bottom of the document, add the line '
#define FOO 78
'. This assigns the value of78
to our system callfoo
- At the botton of /usr/src/include/minix/callnr.h, add '#define FOO 78'.
- Save changes and exit
Now we must modify the system call tables for FS
and PM
(file system, process manager).
Lets take care of the system call table in FS
first. Since we want to implement this code in PM
, we do not want FS
to do anything for our system call. However, we will need to add an entry in the FS
call table. This is done as follows:
- Open
/usr/src/servers/fs/table.c
- Go to the end of the
call_vec
array - Add the line
no_sys, /* 78 = FOO */
at the end ofcall_vec
- Save and exit
Our new call_vec
should look like this:
PUBLIC _PROTOTYPE( int (*call_vec[]), (void) ) = { |
Next we must modify the system call table in PM
. Since this is where we want our system call to actually be implemented, we must do the following:
- Open
/usr/src/servers/pm/table.c
- Go to the end of the
call_vec
array - Add the line
do_foo, /* 78 = FOO */
at the end ofcall_vec
- Save and exit
Our new call_vec
should look like this:
PUBLIC _PROTOTYPE( int (*call_vec[]), (void) ) = { |
Now that FS
and PM
know about our new system call, we should implement it. This can be done in a couple of ways, but only one is presented here:
- Open
/usr/src/servers/pm/misc.c
- Add the line
#include <stdio.h>
with the other#include
lines. - Go to the bottom of the file, and add the following code:
PUBLIC int do_foo() |
Now, we should give PM
the prototype for our new system call. This is done as follows:
- Open
/usr/src/servers/pm/proto.h
- Go to the
/* misc.c */
section (on line 41) - Add the line
_PROTOTYPE( int do_foo, (void) );
- Save changes and exit
The misc.c
section should look like this:
/* misc.c */ |
At this point, we have made all the necessary kernel modifications. More information on the passing of parameter num
from user space to kernel space can be found towards the bottom of this document.
Step 2: Library Modifications
Now we must make a library function call foo()
, so that users can call our new do_foo
system call. (This is just like how users call the fork()
library function call to indirectly call the do_fork
system call.) Again, there are multiple ways to do this, but only one is given here.
- Open
/usr/include/unistd.h
- Add
_PROTOTYPE( int foo, (long _num) );
at the end of the/* Function Prototypes*/
section (around line 126) - Save and exit
The end of the /* Function Prototypes */
section should look like:
/* Function Prototypes. */ |
Now we must create a small user library for our foo
function call. This is done as follows:
- Create file
/usr/src/include/foolib.h
- Add the following code:
#include <lib.h> |
This completes all modifications we must make to add the do_foo
system call. Please notice the underscores in the variable name!
Recompile Kernel
To compile the kernel, do the following commands in Minix (will take awhile):
cd /usr/src/tools/
- make includes
make hdboot
sync
Finally, type shutdown
and restart Minix. (Note: You might be able to find a faster way to compile the kernel.)
Testing New System Call
Here is some sample test code (foo.c
) that will test to make sure our new system call works properly.
#include <sys/types.h> |
This program should output the following:
called foo( -5 )... rval was -1
syscall foo( 2 ) called!
called foo( 2 )... rval was 0