2019-2020-20199316 "Linux kernel principle and Analysis" in the fifth week of work

Video Learning

First, the user mode, kernel mode and interrupt

  • Kernel mode: execution is at a high level code can execute a privileged instruction, access to any physical address, when the kernel mode corresponding to the CPU

  • User-mode: execution is at a low level, the code can only be active within a particular range allowable level. Under routine operation, the mode is performed through a system call library function call library function package system, to provide users with an interface for direct use.

  • Intel x86 CPU has four different execution level 0-3, Linux uses only one of the 0 3 respectively kernel mode and user mode. Minimum two cs register indicates that the current privilege level code, 00 or 11.

  • Kernel mode cs: eip value is arbitrary, i.e. can access all address spaces. Wherein a portion of the user mode can access only the memory address (0x00000000-0xbbbbbbbf), more 0xc0000000 address (logical address rather than a physical address) accessible only in kernel mode.

  • Interrupt handling is the main way to enter the kernel mode from user mode, the system calls a special interrupt. When the user mode to kernel mode is switched from the interrupt / int instruction register context saving state on user stack, wherein the stack comprises a user mode address, then the status word, then cs: eip values, as well as kernel-mode stack address, the status word kernel mode interrupt handler entry. The first thing is to preserve the scene of an interrupt occurs, the value of saving a series of registers; the last thing before the end of the interrupt handler is to restore the site, exits the interrupt routine, recovering the data stored in the register. Special note: to protect the site: interrupt routine is to enter and save data registers need to use; recovery site: that exits the interrupt routine, recovering the data stored in the register.

Second, using the API library functions and the code C embedded in the same assembly code to trigger a system call

Learning the current system time to get this library function call time by using the system

As shown, "= m" represents a variable val3 written into memory inside, "c" represents a bonding ecx, "d" edx. The following inputs and outputs are represented by numbers 1, 2, ..., such as "addl% 1, %% eax \ n \ t" indicates the input to the second output, i.e., "c" (ecx) value was added to the eax, eax why there are two front% percent previous escape character.

Content Experiments

  • Mkdir.c files to compile, compile successfully if it returns 0.

#include <stdio.h>
int main()
{
        int flag;
        flag = mkdir("/home/shiyanlou/testdir");
        if (flag == -1)
                printf("mkdir failed!\n");
        else
                printf("make dirctory success!\n");
        return 0;
}
  • Use gcc to compile

gcc -o makedir mkdir.c

  • Then mkdir.c be added to the assembly language file to modify, edit and compile the following code

gcc -o makedir2 mkdir.c -m32

  • So we can compile successfully!

Problems encountered

Is the question of calling a function numbering system, mkdir number is 39, I am looking at those systems laboratory building to the call list, in front of the number 48 as a program inside the system call number, and always wrong, and later a closer look only to find 39, the corresponding hexadecimal 0x27.

Learning materials

1. System Call Features

  • The user freed from the underlying hardware in the hardware programming. The operating system does not deal directly with hardware for our management hardware, user processes.
  • Greatly improve the security of the system. If a user mode process to deal directly with the hardware devices, will produce security risks may cause the system to crash.
  • It allows the user to program portability. The user program and the collective hardware interfaces have been replaced by decoupled merge, and will not have a close relationship for portability between different systems.

2.API relations and system calls

  • Library function system call is the API (Application Programming Interface) operating system used to provide the reader, API just defined functions. System call is issued by the kernel interrupt soft interrupt request, int instruction execution will trigger an interrupt request. Some internal API Libc library functions defined using routine encapsulation system call, its main purpose is to call distribution system that allows programmers to write code does not require the assembly instructions and registers for passing parameters call trigger system. Usually the package corresponding to each system call routines, a system library calls routines use these packages define the API call to the programmer so that the final package system calls to library functions programmers to use.

Guess you like

Origin www.cnblogs.com/destiny-love/p/11708070.html