Presentation is loading. Please wait.

Presentation is loading. Please wait.

(C) High Speed Networks Lab, CIS, NCTU 1 Operating Systems, Spring 2002 Project #2 Adding a kernel module INSTRUCTOR: Prof. Ying-Dar Lin TAs: I-Wei Chen,

Similar presentations


Presentation on theme: "(C) High Speed Networks Lab, CIS, NCTU 1 Operating Systems, Spring 2002 Project #2 Adding a kernel module INSTRUCTOR: Prof. Ying-Dar Lin TAs: I-Wei Chen,"— Presentation transcript:

1 (C) High Speed Networks Lab, CIS, NCTU 1 Operating Systems, Spring 2002 Project #2 Adding a kernel module INSTRUCTOR: Prof. Ying-Dar Lin TAs: I-Wei Chen, Yi-Hsiang Lin

2 (C) High Speed Networks Lab, CIS, NCTU2 Agenda Purpose Purpose Assignment Assignment Report content Report content Deadline Deadline General steps to add a module in FreeBSD and Linux General steps to add a module in FreeBSD and Linux Testing programs Testing programs

3 (C) High Speed Networks Lab, CIS, NCTU3 Purpose Learning kernel modules - Dynamically load or unload functions - Can do anything that kernel code can do Learning kernel modules - Dynamically load or unload functions - Can do anything that kernel code can do The mechanism of kernel modules The mechanism of kernel modules Practice the “ system call ” again Practice the “ system call ” again Real-world implementations in FreeBSD and Linux Real-world implementations in FreeBSD and Linux

4 (C) High Speed Networks Lab, CIS, NCTU4 Assignment Platform : FreeBSD or Linux Platform : FreeBSD or Linux Adding a “ hello ” system call by using a kernel module The output of executing “./hello Yourname ” Adding a “ hello ” system call by using a kernel module The output of executing “./hello Yourname ” [239] Hello! Yourname is 8823088!! Ret=8 Notes: 1. [239] “ 239 ” is the system call number 2. “ Student ID ” should be written in kernel module. 3. Return code should be the last digital of your student ID (ex. 8823088 should return “ 8 ” ) Any extra functionality will get some bonus Any extra functionality will get some bonus Report: 50 % Report: 50 % Implementation: 50 % Implementation: 50 %

5 (C) High Speed Networks Lab, CIS, NCTU5 Report content 1. What ’ s kernel modules? How its work on Linux or FreeBSD? 2. What can be implemented as modules? And what cannot? 3. How to add a kernel module? 4. (*) Describe your extra functions.

6 (C) High Speed Networks Lab, CIS, NCTU6 Deadline Demo : Demo : 6/20(Thu) pm6:00-11:00 6/21(Fri) pm1:00-6:00 at 701 EECS Research Building Report : turn in when you demo Report : turn in when you demo

7 (C) High Speed Networks Lab, CIS, NCTU7 General steps to add a kernel module in FreeBSD(4.5-STABLE) 1. Edit /usr/share/examples/kld/syscall/module/syscall.c SYSCALL_MODULE(syscall, &offset, &hello_sysent, load, NULL); static int hello (struct proc *p, struct args *uap) { char buf[128]; printf ("Hello! Kernel!!\n"); printf ("Hello! Kernel!!\n"); sprintf(buf,"Hello! Kernel!!", uap->buf); strcpy(uap->buf, buf); sprintf(buf,"Hello! Kernel!!", uap->buf); strcpy(uap->buf, buf); p->p_retval[0] = 0; p->p_retval[0] = 0; return 0; return 0;} static struct sysent hello_sysent = { 1, /* sy_narg */ 1, /* sy_narg */ hello /* sy_call */ hello /* sy_call */}; 2. product kernel module file: syscall.ko

8 (C) High Speed Networks Lab, CIS, NCTU8 General steps to add a kernel module in Linux(RedHat 7.3) 1. edit /usr/src/linux-2.4/arch/i386/kernel/entry.S.long SYMBOL_NAME(sys_hello) 2. edit /usr/src/linux-2.4/include/asm-i386/unistd.h #define __NR_hello 239 3. edit syscall.c asmlinkage long our_sys_hello(char *name) { char buf[128]; sprintf(buf,"Hello! %s!!\n", name); sprintf(buf,"Hello! %s!!\n", name);strcpy(name,buf); printk("(our_sys_hello)Hello! Kernel!!\n"); printk("(our_sys_hello)Hello! Kernel!!\n"); return 1; return 1;} 4. edit Makefile (Notice: replace the kernel version with “ uname -r ” ) 5. product kernel module file: syscall.o

9 (C) High Speed Networks Lab, CIS, NCTU9 Testing programs in FreeBSD 1. edit hello.c stat.version = sizeof(stat); modstat(modfind("syscall"), &stat); modstat(modfind("syscall"), &stat); syscall_num = stat.data.intval; syscall_num = stat.data.intval; syscall (syscall_num, argv[1]); syscall (syscall_num, argv[1]); printf("%s\n", argv[1]); printf("%s\n", argv[1]); 2. Loading kernel module % kldload./syscall.ko % kldload./syscall.ko 3. Testing %./hello einstein Hello! einstein!!

10 (C) High Speed Networks Lab, CIS, NCTU10 Testing programs in Linux 1. edit hello.c static inline _syscall1(int, hello, char *, buf) int main(int argc, char **argv) { printf("Before calling syscall hello(), argv[1]= %s\n", argv[1]); printf("return : %d\n", hello(argv[1])); printf("return : %d\n", hello(argv[1])); printf("After calling syscall hello(), argv[1]= %s\n", argv[1]); printf("After calling syscall hello(), argv[1]= %s\n", argv[1]); return 0; return 0;} 2. Loading kernel module % insmod syscall.o 3. Testing %./hello einstein Hello! einstein!!


Download ppt "(C) High Speed Networks Lab, CIS, NCTU 1 Operating Systems, Spring 2002 Project #2 Adding a kernel module INSTRUCTOR: Prof. Ying-Dar Lin TAs: I-Wei Chen,"

Similar presentations


Ads by Google