System Calls: A Kernel Project
Hunter Bell
CS-550-3
Fall 2003
1
Introduction
• System Call Linkage• Generating a New Kernel Function• Defining the System Call Number• Rebuilding the Kernel• Generation and Running of User-space
Program• Problem encountered with programming
exercise2
System Call LinkageMacro (called in user program)
Stub (trap instruction)
Sys_call_table
User space
Kernel Function
Kernel/Supervisor Space
3
What is a Macro?
• In this case, a predefined function in the Operating System that can be called from user space
•Generates a system call stub with zero to five parameters
•For example, generating a system call stub with two parameters:
_syscall2(type,name,type1,arg1,type2,arg2);
•This generates a stub that makes a system call to the kernel function with matching type and name with the argument signature provided 4
Generating a New Kernel Function
• Update System Call Number / Linkage
• Determine location of body for function
• Write code for new Function
• Rebuild Kernel to include new Function
5
Defining the System Call Number (entry.S)
ENTRY(sys_call_table)
.long SYMBOL_NAME(sys_ni_syscall) /* 0 - old "setup()" system call*/
.long SYMBOL_NAME(sys_exit)
.long SYMBOL_NAME(sys_fork)
.long SYMBOL_NAME(sys_read)
.long SYMBOL_NAME(sys_write)
.long SYMBOL_NAME(sys_open) /* 5 */
…
.long SYMBOL_NAME(sys_ni_syscall) /* streams2 */
.long SYMBOL_NAME(sys_vfork) /* 190 */
.long SYMBOL_NAME(sys_pedagogictime) /*191 - Hunter Bell*/
.rept NR_syscalls-191
.long SYMBOL_NAME(sys_ni_syscall)
F’n name
# of f’ns
6
Defining the System Call Linkage (unistd.h)
#define __NR_exit 1
#define __NR_fork 2
…
#define __NR_vfork 190
#define __NR_pedagogictime 191
New entry in table - for generating a stub 7
Source Code for Kernel Functionasmlinkage int sys_pedagogictime(int flag,struct timeval *thetime)
{
/*declarations*/
int write_failed;
struct timeval *temp;
/*verify we can write to user space*/
write_failed = verify_area(VERIFY_WRITE,thetime,sizeof(thetime));
if(!write_failed)
{
printk("skelcall: Cannot write into user space");
return 0;
}/*end if*/
cli(); /*disable interrupts*/
8
Source Code for Kernel Functioncli(); /*disable interrupts*/
/* write into user space the values in xtime */
__copy_to_user(&thetime->tv_sec,&xtime.tv_sec,sizeof(xtime.tv_sec));
__copy_to_user(&thetime->tv_usec,&xtime.tv_usec,sizeof(xtime.tv_usec));
sti(); /*enable interrupts*/
/* verify we can read from user space*/
write_failed = verify_area(VERIFY_READ,thetime,sizeof(thetime));
if(!write_failed)
{
printk("skelcall: Cannot read from user space");
return 0;
}
/*copy back from user space into temporary variable for printing*/
temp = NULL;
__copy_from_user(&temp->tv_sec,&thetime->tv_sec,sizeof(thetime->tv_sec));
__copy_from_user(&temp->tv_usec,&thetime->tv_usec,sizeof(thetime->tv_usec));
9
Source Code for Kernel Function
/*Print time if flag is true*/
if(flag)
printk("%s",ctime(temp->tv_sec));
return (!(write_failed));
}//end pedagogictime
10
Rebuilding the Kernel5 steps:
• make clean
• make oldconfig
• make depend
• make
• make bzImage11
Rebuilding the Kernel (commands)
Command Functionality
make clean Removes old object files and other temporary files in kernel environment
make oldconfig
Generic form: make <config_opt>
Defines logical environment for new kernel – in this case, the same as the older configuration
make depend Specifies compile-order of kernel files
make Compiles all kernel source code into an executable file
make bzImage
Generic form : make <boot_opt>
Creates a bootable image of the kernel
12
Generation and Running of User-space Program
/* ****************************************
* Hunter Bell - CS550
* exercise-5 (term project)
* this program executes in user space and
* makes a system call to kernel space
***************************************** */
#include <linux/unistd.h>
#include <sys/syscall.h>
#include <stdio.h>
#include <time.h>
int main(int argc, char * argv[])
{
struct timeval *thetime = (struct timeval *) malloc(sizeof(struct timeval *));
int flag = 1;
/* generate a stub for System call for int pedagogictime through a Macro*/
_syscall2(int,pedagogictime,int,flag,struct timeval *,thetime);
return 0;
}//end main
13
Summary – System Call LinkageMacro (called in user program)
Stub (trap instruction)
Sys_call_table
User space
Kernel Function
14
Problem encountered with creating kernel function
• memcpy_fromfs() and memcpy_tofs() did not compile, and a grep produced nothing
15
Summary•System Call Linkage
•Generating a New Kernel Function
•Defining the System Call Number
•Rebuilding the Kernel
•Generation and Running of User-space Program 16