Sei sulla pagina 1di 13

Programming Assignment 4

User Mode Linux & adding system call

What is user mode Linux (UML)


User Mode Linux is a port of Linux to run as a program inside Linux We can modify and test kernel without crashing entire system

User Mode Linux


User Mode Linux As a process of host system

Applications General Kernel Applications


User mode arch

Drivers

General Kernel Architecture Layer Drivers

Hardware (Disk, CPUetc)

User Mode Linux


All UML devices are virtual
constructed from the abstractions provided by the host kernel.

The UML devices:


Consoles and serial lines
Main console, virtual console and serial lines.

Block devices
Access to anything on the host which can be mounted, e.g. CDROM, disk partition

Network devices
Network access between UML to host OS/UML and between UML to host in outside network.

Kernel mode and User mode (UML)


Hardware platforms provide a built-in mechanism for switching between Kernel mode and user mode UML use ptrace system call tracing mechanism
The transition from user mode to kernel mode is done by the tracing thread.

System Call in UML


Virtualization of system call
Process stack Virtual kernel stack Tracing thread

Int 0x80 Enter kernel Notify parent Execute system call Signal self when done

Nullify system call Save process state Force process onto Kernel stack

Continue After system call

Restore process state Set system call Return value

Test program
For arch/x86/syscalls/syscall 64.tbl. 311 64 process_vm_writev sys_process_vm_writev 312 64 kcmp sys_kcmp 313 common mysyscall sys_mysyscall // Acts as a library call that makes the actual system call int mysyscall() {
int ret; // initializing the system call register with the system call number 313 __asm__("mov $313, %rax"); // executing the TRAP instruction for x86 64 bit architectures __asm__("syscall"); return ret;

} int main() {
printf("Making a system call...\n"); mysyscall(); return 0;

} asmlinkage long sys_mysyscall(void) {


printk("My custom system call!\n"); // the kernel's version of printf! But not host kernel printk!! return 0;

After TRAP instruction, kernel does


IDT (interrupt descriptor table) System call table

.. system call .. ..

0x80

.. 313 . mysyscall ..
(Call mysyscall handler )

Adding a System Call in Linux


System Call is generated by user process through TRAP ( software generated interrupt ) Add new system call number and function name to system call table and header file
arch/x86/syscalls/syscall 64.tbl (system call table) arch/x86/include/generated/asm/unistd 64.h

Adding a System Call in Linux


Add implementation of new system call to
Add asmlinkage long sys mysyscall(void) to include/linux/syscalls.h Add the implementation below to kernel/sys.c:

Installing and using UML


Download Linux kernel and build with
make ARCH=um ( target architecture is User Mode)

Need a root file system for UML


Emulated as a file in a host file system.

Running UML
linux ubd0=./Debian-Squeeze-AMD64-root_fs mem=128M

Installing and using UML


Host file system can be mounted to UML directory
mount none /mnt -t hostfs -o fullPathToYourHostDirectory

dont forget rebuild Linux kernel to test new system call function

Potrebbero piacerti anche