2019-2020-1 20199302 "Linux kernel principle and Analysis" in the third week of work

Cloud class lesson learning content

A, C language embedded assembler code

1, inline assembler syntax

Writing embedded assembly language code (1) C:

ASM (
assembler statement template:
output section:
an input section:
destruction description part);

Description : the input section and the output section corresponds to the parameter when the C language function calls (return is an output section)

Example:

printf("val1:%d,val2:%d,val3:%d\n",val1,val2,val3);
asm volatile(                 
/*asm是GCC关键字asm的宏定义,表示内嵌汇编语句,与__sam__同,volatile是GCC关键字volatile的宏定义,告诉编译器不要对代码进行优化,与__volatile同*//
"movl $0,%%eax\n\t"    /*clear %eax to 0,两个%中第一个用于转义*/
"addl  %1,%%eax\n\t"   /*%eax+=val1,是输入输出中的第二个数*/
"addl %2,%%eax\n\t"    /*%eax+=val2,是输入输出中的第三个数*/
"movl %%eax,%0\n\t"   /*val3=%eax*/
:"=m"(val3)                    /*output=m,只写,m表示写入内存
:"c"(val1),"d"(val2)         /*input c:ecx  d:edx*/
);
printf("val1:%d+val2:%d=val3:%d\n",val1,val2,val3);

return 0;

(2) common inline assembly qualifier

Qualifier description
“a” The input variables into eax
“b” The input variables into ebx
“c” The input variables into ecx
“d” The input variables into edx
“s” The input variables into esi
“D” The input variables into edi
“q” The input variables into the eax, ebx, ecx, edx one
“r” The general-purpose register into the input variables, i.e. eax, ebx, ecx, edx, esi, edi one
“A” Into the eax and edx, bars, eax and edx the synthesis of a 64-bit register
“m” Memory variable
"The" Operand is a memory variable, but it is offset addressing type
"V" Operand is a memory variable, but not addressing offset type
“.” Operand is a memory variable, but is an autoincrement addressing mode
“p” Operand is a valid memory address (pointer)
“g” The input variables into the eax, ebx, ecx, edx is used as a memory variable, or
“X” Operands can be any type
“I” Immediate 0-31 (for 32-bit shift instructions)
“J” Immediate between 0-63 (for 64-bit shift instructions)
“N” Immediate between 0-255 (for out instruction)
“i” Immediate
“n” Immediate, some systems outside the immediate figures do not support the use of these systems should be n
“=” Operand in the instruction is write-only (output operands)
“+” Operand is (the number of input and output operations) read in the instruction type
“%” The operands may be operands and a next switching position

Third, the three magic weapons: stored program computer, the function call stack, interrupt

1, the computer How does it work? (Summary) - Three magic

(1) storing a computer program: the basis for all of the logical framework computer.
(2) function call stack: computer very basic things. The earliest computer does not have high-level language, machine language concepts and not only a function of the time of assembly language. The concept of high-level language function, we need to stack mechanism is the basis for high-level languages can run.

Stack is a space must call path and parameters recorded C language program is running.
- function call frame
- passing parameters
- saving the return address (return value, such as the EAX)
- to provide space for local variables
- like

C language compiler stack a set of rules, the assembly code generated by the same paragraph C language program on different operating systems may be some differences.
Rules for more stack the purpose of existence and compiler of the stack is used to understand the underlying operating system some of the key code.

(3) Interrupt: computer programmers to help do some of the work.

Interrupted
early computer is not interrupted, you need to perform and then execute another program after completing a program.
With the break, there will be a multi-channel programming, multi-channel program that is running simultaneously in the system.
When an interrupt occurs, CPU will current eip, esp, ebp are down to a kernel stack. CPU and achieve a common kernel code to save and return to the scene.

2, the use of computer simulation experiments mykernel hardware platform.

(1) set up a virtual platform
(2) using the Linux source code CPU configuration configured
(3) execution

cd LinuxKernel/Linux-3.9.4
qeum -kernel arch/x86/boot/bzImage  /*加载内核*/

Experiments have problems

① compile the Linux kernel appear include / linux / compiler-gcc.h: 103: 30: fatal error: linux / compiler-gcc5.h: No such file or directory
Solution


Solution: users convert root can

Compilation:

Completed:

qemu -kernel arch/x86/boot/bzImage

Kernel boot renderings
In mykernel directory:

cd mykernel
enter the ls command will find the folder containing mymain.c and myinterrupt.c two files.

Operating system entry

/*mymain.c中开始启动操作系统,入口*/
void __init_my_start_kernel(void)
{
    int i = 0;
    while(1)
    {
        i++;
        if(i%100000 == 0)
            printk(KERN_NOTICE "my_start_kernel here %d \n",i);/*每循环十万次,打印一条消息*/
    }

Clock interrupt handler entrance

void my_timer_handler(void)/*myinterrupt.c中,时钟中断处理入口*/
{
    printk(KERN_NOTICE "\N>>>>>>>>>>>>>>>>>>>>>my_timer_handler here <<<<<<<<<<<\n\n");
}

According to the code only when Qiaowan textbooks, there have been many mistakes, because the code is not the time to seriously knock, more letters, small letters or codes mistype. It is worth mentioning that a mistake is initialization function in mymain.c file, init front two underscores, rather than one.
After running successfully:

5, in-depth understanding of the function call stack

(1) esp: stack pointer
(2) ebp: base pointer
(3) stack operations: ①push address stack decrease of 4 bytes (32 bits)
②pop address increment stack 4 bytes (32 bits)
(4) ebp used as the recording current in the C language calls the base address
(5) other key register
①cs: eip always point to the address of the next instruction

Sequentially performed: always points to the next instruction address consecutive
jump / branch: when executing such instructions, cs: eip value will need to be modified according to the procedure
call: the current CS: value of eip onto stack, cd : eip entry point address of the called function
RET: pop cs previously saved here from the stack: the value of eip placed cs: eip in

(6) establish the stack frame of the calling function

pushl %ebp
movl %esp,%ebp
/*被调用者函数体*/
/*拆除被调用者函数的堆栈框架*/
movl %ebp,%esp
popl %ebp
ret

Guess you like

Origin www.cnblogs.com/eosmomo/p/11596700.html