Progasn4

download Progasn4

of 13

Transcript of Progasn4

  • 8/13/2019 Progasn4

    1/13

    User Mode Linux & adding systemcall

  • 8/13/2019 Progasn4

    2/13

    User Mode Linux is a port of Linux to run as a

    program inside Linux

    We can modify and test kernel without crashingentire system

  • 8/13/2019 Progasn4

    3/13

    Hardware (Disk, CPUetc)

    Architecture Layer Drivers

    General Kernel

    Applications User modearch Drivers

    General Kernel

    Applications

    User Mode Linux As a process of host system

  • 8/13/2019 Progasn4

    4/13

    All UML devices are virtual

    constructed from the abstractions provided by the hostkernel.

    The UML devices: Consoles and serial linesMain console, virtual console and serial lines.

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

    Network devicesNetwork access between UML to host OS/UML and

    between UML to host in outside network.

  • 8/13/2019 Progasn4

    5/13

    Hardware platforms provide a built-in

    mechanism for switching between Kernel modeand user mode

    UML use ptrace system call tracing mechanismThe transition from user mode to kernel mode isdone by the tracing thread.

  • 8/13/2019 Progasn4

    6/13

    Virtualization of system call

    Virtual kernel stack Process stack Tracing thread

    Int 0x80Enter kernelNotify parent

    Execute system callSignal self when done

    Nullify system callSave process stateForce process ontoKernel stack

    Continue After system call

    Restore process stateSet system callReturn value

  • 8/13/2019 Progasn4

    7/13

    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;

    }

  • 8/13/2019 Progasn4

    8/13

    After TRAP instruction, kernel doesIDT (interrupt descriptor table)

    ..system call 0x80....

    ..313 . mysyscall..

    System call table

    (Call mysyscall handler)

  • 8/13/2019 Progasn4

    9/13

    System Call is generated by user process through

    TRAP ( software generated interrupt )

    Add new system call number and function nameto system call table and header file

    arch/x86/syscalls/syscall 64.tbl (system call table)arch/x86/include/generated/asm/unistd 64.h

  • 8/13/2019 Progasn4

    10/13

    Add implementation of new system call to

    Add asmlinkage long sys mysyscall(void) toinclude/linux/syscalls.h Add the implementation below to kernel/sys.c:

  • 8/13/2019 Progasn4

    11/13

    Download Linux kernel and build with

    make ARCH=um ( target architecture is User Mode)

    Need a root file system for UMLEmulated as a file in a host file system.

    Running UMLlinux ubd0=./Debian-Squeeze-AMD64-root_fsmem=128M

  • 8/13/2019 Progasn4

    12/13

    Host file system can be mounted to UML

    directory mount none /mnt -t hostfs -o fullPathToYourHostDirectory

  • 8/13/2019 Progasn4

    13/13

    dont forget rebuild Linux kernel to test newsystem call function