week 7 system calls, kernel threads, kernel...
TRANSCRIPT
![Page 1: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/1.jpg)
Week 7
System Calls, Kernel
Threads, Kernel Debugging
1
Sarah Diesburg
Florida State University
![Page 2: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/2.jpg)
First…
� Any questions on
� Part 1 – 5 system calls
� Part 2 – xtime proc module
2
![Page 3: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/3.jpg)
Story of Kernel
Development
Some context…
3
![Page 4: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/4.jpg)
In the old days…
� There were no modules or virtual machines
� The kernel is a program
� Has code, can compile, re-compile, make executableexecutable
� When changes needed to be made, developers make changes in source and re-compile
4
![Page 5: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/5.jpg)
How is the kernel different from a
regular program?� Mostly in how it is executed
� Boot loader loads the kernel image/executable during boot time
� Sets kernel modeSets kernel mode
� Jumps to the entry point in the image/executable
� Remember the generic booting sequence?
5
![Page 6: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/6.jpg)
Quick Question
� How would you make changes to the kernel
and run those changes?
1. Make changes to the source
2. Re-complie the kernel source2. Re-complie the kernel source
3. Re-install the kernel source
4. Make sure the bootloader sees the new kernel image (grub)
5. Reboot and profit!
6
![Page 7: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/7.jpg)
Getting more modern..
� Modules were created as bits of code that
can be loaded and unloaded by the kernel in
kernel mode
� Made development easier� Made development easier
� Instead of re-compiling, re-installing, and rebooting into the new kernel, one could just re-compile and load a module
7
![Page 8: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/8.jpg)
Quick Question
� How would you make changes to a module
and run those changes?
1. Make changes to module source code
2. Re-compile the module2. Re-compile the module
3. Load the new module
8
![Page 9: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/9.jpg)
Present Day
� Reboots into new kernels and loading new
modules often freezes machines
� Enter virtual machine software
� Process that emulates the hardware necessary to � Process that emulates the hardware necessary to run an OS in user-space
� Guest OS is executed inside the virtual machine process!
9
![Page 10: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/10.jpg)
New System Calls
Fun but tricky!
10
![Page 11: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/11.jpg)
Implementing System Calls
int start_elevator(void);
int issue_request(int #1, int #2,
int #3);
int stop_elevator(void);
� Need to implement the functions above. But
how?
int stop_elevator(void);
![Page 12: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/12.jpg)
Adding a System Call to Kernel
� Files to add:� LINUX_DIR/PROJECT_NAME/Makefile
� LINUX_DIR/PROJECT_NAME/PROJECT_NAME.c
� LINUX_DIR/PROJECT_NAME/NEW_SYSCALLS.c
Files to modify:� Files to modify:� LINUX_DIR/arch/x86/kernel/syscall_table_32.S
� LINUX_DIR/include/asm-generic/unistd.h
� LINUX_DIR/include/linux/syscalls.h
� LINUX_DIR/Makefile
![Page 13: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/13.jpg)
Sample System Call
� Let’s add a sample module to the kernel that
defines a sample system call
� test_newsyscall(int test_int);� test_newsyscall(int test_int);
� Takes an int test_int and issues a printk on it
� Returns test_int
� Problem – If our module isn’t loaded, what
happens if we call our sample system call?
13
![Page 14: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/14.jpg)
Project 2 System Call Model
14
Elevator module Core kernel
User program
![Page 15: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/15.jpg)
Project 2 System Call Model
15
Elevator module Core kernel
User program
User issues system call,
core kernel looks up
system call in system call
table
![Page 16: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/16.jpg)
Project 2 System Call Model
Elevator module
performs system call
action
16
Elevator module Core kernel
User program
![Page 17: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/17.jpg)
Project 2 System Call Model
Elevator module
returns result of
system call
17
Elevator module Core kernel
User program
![Page 18: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/18.jpg)
Project 2 System Call Model
18
Elevator module Core kernel
User program
Core kernel forwards
result of system call to
user program
![Page 19: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/19.jpg)
What happens if elevator module is
not loaded?
19
Core kernel
User program
![Page 20: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/20.jpg)
What happens if elevator module is
not loaded?
20
Core kernel
User program
User issues system call,
core kernel looks up
system call in system call
table
![Page 21: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/21.jpg)
What happens if elevator module is
not loaded?
Elevator module is not
loaded to perform the
action…. OOPS!
21
Core kernel
User program
![Page 22: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/22.jpg)
Module System Calls
� We must create a wrapper system call!
� Wrapper will call module function if module loaded, else returns an error
� Must be created in a separate, built-in kernel file Must be created in a separate, built-in kernel file in the project folder
22
![Page 23: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/23.jpg)
Function Pointers
� We will implement our system call wrapper
with a function pointer
� Pointer to a function
� Function pointer can point to any function � Function pointer can point to any function
that you implement that
� Takes the same input variable types
� Returns the same return type
23
![Page 24: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/24.jpg)
Function Pointers
long (*STUB_test_newsyscall)(int test_int) = NULL;
� Function pointer that
� Returns a long
� Name is STUB_test_newsyscall
� Takes parameter int test_int
� Function pointer set to NULL
� Can set function pointer to a local function you implement
24
![Page 25: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/25.jpg)
Elevator Project
� Create a file in your elevator project that just
contains the system call information
� KERNEL_DIR/PROJECT_DIR/newsyscalls.c
25
![Page 26: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/26.jpg)
KERNEL_DIR/PROJECT_DIR/
newsyscalls.c#include <linux/linkage.h>
#include <linux/kernel.h>
#include <linux/module.h>
/* System call stub. We initialize the stub function to be NULL.
*/
long (*STUB_test_newsyscall)(int test_int) = NULL;long (*STUB_test_newsyscall)(int test_int) = NULL;
EXPORT_SYMBOL(STUB_test_newsyscall);
/* System call wrapper. If the stub is not NULL, it will be run,
otherwise returns -ENOSYS */
asmlinkage long sys_test_newsyscall(int test_int)
{
if (STUB_test_newsyscall)
return STUB_test_newsyscall(test_int)
else
return -ENOSYS;
}26
![Page 27: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/27.jpg)
KERNEL_DIR/PROJECT_DIR/
newsyscalls.c#include <linux/linkage.h>
#include <linux/kernel.h>
#include <linux/module.h>
/* System call stub. We initialize the stub function to be NULL.
*/
long (*STUB_test_newsyscall)(int test_int) = NULL;
Function pointer
long (*STUB_test_newsyscall)(int test_int) = NULL;
EXPORT_SYMBOL(STUB_test_newsyscall);
/* System call wrapper. If the stub is not NULL, it will be run,
otherwise returns -ENOSYS */
asmlinkage long sys_test_newsyscall(int test_int)
{
if (STUB_test_newsyscall)
return STUB_test_newsyscall(test_int)
else
return -ENOSYS;
}27
![Page 28: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/28.jpg)
KERNEL_DIR/PROJECT_DIR/
newsyscalls.c#include <linux/linkage.h>
#include <linux/kernel.h>
#include <linux/module.h>
/* System call stub. We initialize the stub function to be NULL.
*/
long (*STUB_test_newsyscall)(int test_int) = NULL;
Export the pointer
so we can access
it later
long (*STUB_test_newsyscall)(int test_int) = NULL;
EXPORT_SYMBOL(STUB_test_newsyscall);
/* System call wrapper. If the stub is not NULL, it will be run,
otherwise returns -ENOSYS */
asmlinkage long sys_test_newsyscall(int test_int)
{
if (STUB_test_newsyscall)
return STUB_test_newsyscall(test_int)
else
return -ENOSYS;
}28
![Page 29: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/29.jpg)
KERNEL_DIR/PROJECT_DIR/
newsyscalls.c#include <linux/linkage.h>
#include <linux/kernel.h>
#include <linux/module.h>
/* System call stub. We initialize the stub function to be NULL.
*/
long (*STUB_test_newsyscall)(int test_int) = NULL;long (*STUB_test_newsyscall)(int test_int) = NULL;
EXPORT_SYMBOL(STUB_test_newsyscall);
/* System call wrapper. If the stub is not NULL, it will be run,
otherwise returns -ENOSYS */
asmlinkage long sys_test_newsyscall(int test_int)
{
if (STUB_test_newsyscall)
return STUB_test_newsyscall(test_int)
else
return -ENOSYS;
}29
System call
wrapper
![Page 30: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/30.jpg)
Elevator Project
� Next create a separate file that
� Holds your module code
� Registers the system call pointer
� Actually implements the system call behavior� Actually implements the system call behavior
30
![Page 31: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/31.jpg)
Inside KERNEL_DIR/PROJECT_DIR/
PROJECT_NAME.C
/* Extern system call stub declarations */
extern long (*STUB_test_newsyscall)(int test_int);
long my_test_newsyscall(int test)
{
printk("%s: Your int is %i\n", __FUNCTION__, test);
return test;
}}
my_module_init() {
STUB_test_newsyscall=&(my_test_newsyscall);
return 0;
}
my_module_exit() {
STUB_test_newsyscall=NULL;
}
31
![Page 32: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/32.jpg)
Inside KERNEL_DIR/PROJECT_DIR/
PROJECT_NAME.C
/* Extern system call stub declarations */
extern long (*STUB_test_newsyscall)(int test_int);
long my_test_newsyscall(int test)
{
printk("%s: Your int is %i\n", __FUNCTION__, test);
return test;
}
Gain access to stub
function pointer.
}
my_module_init() {
STUB_test_newsyscall=&(my_test_newsyscall);
return 0;
}
my_module_exit() {
STUB_test_newsyscall=NULL;
}
32
![Page 33: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/33.jpg)
Inside KERNEL_DIR/PROJECT_DIR/
PROJECT_NAME.C
/* Extern system call stub declarations */
extern long (*STUB_test_newsyscall)(int test_int);
long my_test_newsyscall(int test)
{
printk("%s: Your int is %i\n", __FUNCTION__, test);
return test;
}
Local function that
implements syscall
}
my_module_init() {
STUB_test_newsyscall=&(my_test_newsyscall);
return 0;
}
my_module_exit() {
STUB_test_newsyscall=NULL;
}
33
![Page 34: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/34.jpg)
Inside KERNEL_DIR/PROJECT_DIR/
PROJECT_NAME.C
/* Extern system call stub declarations */
extern long (*STUB_test_newsyscall)(int test_int);
long my_test_newsyscall(int test)
{
printk("%s: Your int is %i\n", __FUNCTION__, test);
return test;
} Set stub function pointer }
my_module_init() {
STUB_test_newsyscall=&(my_test_newsyscall);
return 0;
}
my_module_exit() {
STUB_test_newsyscall=NULL;
}
34
Set stub function pointer
to local function in init
![Page 35: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/35.jpg)
Inside KERNEL_DIR/PROJECT_DIR/
PROJECT_NAME.C
/* Extern system call stub declarations */
extern long (*STUB_test_newsyscall)(int test_int);
long my_test_newsyscall(int test)
{
printk("%s: Your int is %i\n", __FUNCTION__, test);
return test;
}}
my_module_init() {
STUB_test_newsyscall=&(my_test_newsyscall);
return 0;
}
my_module_exit() {
STUB_test_newsyscall=NULL;
}
35
Reset stub function
pointer to NULL on
module unload
![Page 36: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/36.jpg)
KERNEL_DIR/PROJECT_DIR/
Makefileobj-m := my_module.o
obj-y := newsyscalls.o
KDIR := /lib/modules/2.6.32/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
36
![Page 37: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/37.jpg)
KERNEL_DIR/PROJECT_DIR/
Makefileobj-m := my_module.o
obj-y := newsyscalls.o
KDIR := /lib/modules/2.6.32/build
PWD := $(shell pwd)
Compile as a module
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
37
![Page 38: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/38.jpg)
KERNEL_DIR/PROJECT_DIR/
Makefileobj-m := my_module.o
obj-y := newsyscalls.o
KDIR := /lib/modules/2.6.32/build
PWD := $(shell pwd)
Compile as kernel built-in
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
38
![Page 39: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/39.jpg)
Core Kernel Additions
� Add the new system call to the core kernel
system call table
� Modify three files
� Add the project directory to the main Makefile� Add the project directory to the main Makefile
� Modify one file
39
![Page 40: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/40.jpg)
Modifying syscall_table_32.S
…
.long sys_preadv
.long sys_pwritev
.long sys_rt_tgsigqueueinfo /* 335 */
.long sys_perf_event_open
![Page 41: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/41.jpg)
Modifying syscall_table_32.S
…
.long sys_preadv
.long sys_pwritev
.long sys_rt_tgsigqueueinfo /* 335 */
.long sys_perf_event_open
.long sys_test_newsyscall /* 337 */
� Add new system call to the end of the file. � Remember the number – you’ll need it in userspace!
![Page 42: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/42.jpg)
Modifying unistd.h
/* midfile */
#define __NR_perf_event_open 241
__SYSCALL(__NR_perf_event_open, sys_perf_event_open)
#undef __NR_syscalls
#define __NR_syscalls 242
/* midfile */
� Can be found around line 623…
![Page 43: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/43.jpg)
Modifying unistd.h
/* midfile */
#define __NR_perf_event_open 241
__SYSCALL(__NR_perf_event_open, sys_perf_event_open)
#define __NR_test_newsyscall 242
__SYSCALL(__NR_test_newsyscall, sys_test_new_syscall)
#undef __NR_syscalls
#define __NR_syscalls 242
/* midfile */
![Page 44: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/44.jpg)
Modifying unistd.h
/* midfile */
#define __NR_perf_event_open 241
__SYSCALL(__NR_perf_event_open, sys_perf_event_open)
#define __NR_test_newsyscall 242
__SYSCALL(__NR_test_newsyscall, sys_test_new_syscall)
#undef __NR_syscalls
#define __NR_syscalls 243
/* midfile */
![Page 45: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/45.jpg)
Modifying syscalls.h
asmlinkage long sys_perf_event_open(
struct perf_event_attr __user *attr_uptr,
pid_t pid, int cpu, int group_fd, unsigned
long flags);
#endif
/* EOF *//* EOF */
45
![Page 46: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/46.jpg)
Modifying syscalls.h
asmlinkage long sys_perf_event_open(
struct perf_event_attr __user *attr_uptr,
pid_t pid, int cpu, int group_fd, unsigned
long flags);
asmlinkage long sys_test_newsyscall(int test_int);asmlinkage long sys_test_newsyscall(int test_int);
#endif
/* EOF */
46
![Page 47: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/47.jpg)
Modifying
KERNEL_DIR/Makefile# Objects we will link into vmlinux /
subdirs we need to visit
init-y := init/
drivers-y := drivers/ sound/ firmware/
net-y := net/net-y := net/
libs-y := lib/
core-y := usr/
endif # KBUILD_EXTMOD
47
![Page 48: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/48.jpg)
Modifying
KERNEL_DIR/Makefile# Objects we will link into vmlinux /
subdirs we need to visit
init-y := init/
drivers-y := drivers/ sound/ firmware/
net-y := net/net-y := net/
libs-y := lib/
core-y := usr/ my_module/
endif # KBUILD_EXTMOD
48
� Found around line 475…
� Can replace “my_module” with the name of your
PROJECT_DIR
![Page 49: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/49.jpg)
Getting it all to work
1. Re-compile the kernel
2. Install modules, install kernel
3. Make new initramfs image
Reboot4. Reboot
5. Test with a user-space program…
![Page 50: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/50.jpg)
Sample User-space Program
#include <stdio.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <linux/unistd.h>
#define __SYS_TEST_ELEVATOR 337
int main()
{
int test=5;int test=5;
long ret;
ret=syscall(__SYS_TEST_ELEVATOR, test);
if(ret<0)
perror("system call error");
else
printf("Function successful, returned %i\n", ret);
return 0;
}
50
![Page 51: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/51.jpg)
syscall()
int syscall(int number, ...);
� Performs the system call based on the system call’s
number
� Number can be found in the syscall_table_32.S file (our example � Number can be found in the syscall_table_32.S file (our example was 337)
51
![Page 52: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/52.jpg)
User-space Program Output
� Output when my_module not loaded
system call error: Function not implemented
� Output when my_module loaded
Function successful, returned 5
52
![Page 53: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/53.jpg)
Kthreads
Run the main logic of your module in a
kthread!
53
![Page 54: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/54.jpg)
Refresher: hello.c
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE(“Dual BSD/GPL”);
static int hello_init(void)
{
printk(KERN_ALERT “Hello, world!\n”);
return 0;return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT “Goodbye, sleepy world.\n”);
}
module_init(hello_init);
module_exit(hello_exit);
54
![Page 55: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/55.jpg)
Kernel Modules
� Remember, kernel modules are very event-
based
� We need a way to start an independent
thread of execution in response to an eventthread of execution in response to an event
� e.g. start_elevator() for project 2…
55
![Page 56: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/56.jpg)
kthread_run
kthread_run(threadfn, data, namefmt, ...)
� Creates a new thread and tells it to run� threadfn – the name of the function the thread should run
� data – data pointer for threadfn (can be NULL if the function � data – data pointer for threadfn (can be NULL if the function
does not take any args)
� namefmt – name of the thread (displayed during “ps” command)
� Returns a task_struct
56
![Page 57: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/57.jpg)
kthread_run example
struct task_struct *t;
t = kthread_run(run, NULL, “my_elevator");
if (IS_ERR(t)){if (IS_ERR(t)){
ret=PTR_ERR(t);
}
57
![Page 58: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/58.jpg)
kthread_stop
int kthread_stop(struct task_struct * k);
� Sets kthread_should_stop for k to return true, wakes
the thread, and waits for the thread to exit
Returns the result of the thread function� Returns the result of the thread function
58
![Page 59: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/59.jpg)
kthread_stop_example
ret=kthread_stop(t);
if(ret != -EINTR)
printk("Main logic tread stopped.\n“);
59
![Page 60: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/60.jpg)
Thread Function Example
static int run(void *arg)
{
/* Lock here */
while(!kthread_should_stop()) {
/* Do stuff */
/* Unlock here */
schedule();
/* Lock here */
}
/* Unlock here */
printk("%s: kernel thread exits.\n", __FUNCTION__);
return 0;
}
60
![Page 61: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/61.jpg)
Thread Function Example
static int run(void *arg)
{
/* Lock here */
while(!kthread_should_stop()) {
/* Do stuff */schedule() is very
important here. Why?/* Unlock here */
schedule();
/* Lock here */
}
/* Unlock here */
printk("%s: kernel thread exits.\n", __FUNCTION__);
return 0;
}
61
important here. Why?
![Page 62: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/62.jpg)
Inefficient Solution
� Thread will continue to run even though it has
nothing to do
� Eats up resources
� Investigate the kthread interface to find ways � Investigate the kthread interface to find ways
to
� Put thread to sleep
� Wake up thread
� There is more than one way to do this…
62
![Page 63: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/63.jpg)
Debugging
63
![Page 64: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/64.jpg)
Kernel Debugging Configurations
� Timing info on printks
� __depreciated logic
� Detection of hung tasks
� SLUB debugging� SLUB debugging
� Kernel memory leak detector
� Mutex/lock debugging
� Kmemcheck
� Check for stack overflow
� Linked list debugging
64
![Page 65: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/65.jpg)
Select Kernel Hacking
65
![Page 66: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/66.jpg)
Enable Debugging Options
66
![Page 67: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/67.jpg)
Debugging through procfs
� Necessary for elevator project!
� General process
� Identify data to monitor in your module
� Create a proc entry to monitor this data� Create a proc entry to monitor this data
� Run your module
� Query /proc/<entry> for that information at any time
67
![Page 68: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/68.jpg)
Kernel Oops and Other Errors
� Kernel errors often only appear on first tty
(terminal interface)
� Why?
� How can I see my first tty?� How can I see my first tty?
� On regular system – CTRL+ALT+F1
� CTRL+ALT+F7 to go back to X screen
� On VMware – CTRL+ALT+SPACE+F1
� CTRL+ALT+SPACE+F7 to go back to X screen
68
![Page 69: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/69.jpg)
Oops!
69
![Page 70: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/70.jpg)
Reason for failure
70
for failure
![Page 71: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/71.jpg)
Current drivers
71
![Page 72: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/72.jpg)
72
Call Trace
![Page 73: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/73.jpg)
73
Call Trace
![Page 74: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/74.jpg)
Failed command
74
command
![Page 75: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/75.jpg)
Defensive Programming
• Infinite loops and deadlocks at the kernel
level hang your machine
– Ctrl-Alt-Del has NO effect
– Ctrl-C does not matter– Ctrl-C does not matter
– Ctrl-D does not matter
– You may only reboot
• How do you protect yourself?
– Use schedule() strategically
– Use preemptable versions of functions
![Page 76: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/76.jpg)
Debugging Tools not Covered
� LTT – Linux Tracing Framework
� gdb – Invoking gbd on the kernel image
� kgdb – A remote debugger for the kernel
Magic SysRq� Magic SysRq
� printk – Rate limiting, turning on/off
![Page 77: Week 7 System Calls, Kernel Threads, Kernel Debuggingdiesburg/courses/cop4610_fall10/week07/week7.pdf · System Calls, Kernel Threads, ... Part 2 – xtime proc module 2. Story of](https://reader034.vdocuments.site/reader034/viewer/2022050610/5fb13ce6eb8f5c7d274eaa48/html5/thumbnails/77.jpg)
Next Time
� Locks
� Linked lists
� Elevator algorithms
77