Access from a normal function call to the operating system kernel space

Source: Internet
Author: User
Tags what interface

Access from a normal function call to the operating system kernel space

First, we realize the function call by understanding the principle of cognition function.

1) function

First what is a function, a function is a program code self-contained unit used to accomplish a particular task

(The self-contained unit here may not understand, actually is the code snippet {} curly braces in each code, a semicolon-terminated collection, they are to complete a specific function to write the program) then why use the function, in general, The use of functions can omit the repetition of code, which involves the invocation of a function, when we write a program, we may repeatedly invoke a particular function to implement a particular phenomenon or result. Then consider the question of how the function is written and the way in which the called function can be used.

2) The general writing form of the function (here is just a personal habit and someone else can read what the function is called):

Descriptive function name, shaped like   INT  LED_ON  ( void ) {...} led int int return 0 ; Indicates the successful execution of a function, RETURN -1/1

Note here is the function of the return value, only can represent a function of the execution state, but this also function exactly what kind of function, is not the same concept, each function of the implementation of what kind of function only in its own function module only know, when we want to use the function of a function, there are two ways to achieve

1: Using the return value of the function to use the returned function for the user

2: This involves the concept of function arguments: value passing and address passing in the way that the function is passed

When we use the value to pass, for example, by passing two value arguments (arguments) to the calling function, in the called function through two number exchange, and then in the main function to print out, you will find that the value of two number has not changed at all, this is why? The variable inside the called function has only local variables, its variables hold the stack space, when the function call is finished, it will be freed, but why pass through the address, in the called function to achieve two number of exchanges, in the main function to print two numbers, you will find that two values have changed, In a function call process, but also call other procedures, in fact, the general stack space has two pointers, respectively, is the stack pointer and frame pointer, for a program call process to stack space of the stack pointer is relative, he used to store some storage variables and local variables, and for the procedure called process will involve the frame pointer, It is used to hold parameters and return values, for a function call, the stack parameters of the function call and the general function, and the tumbler stack order of the variables: the first tumbler stack is the execution address of the next instruction, multiple parameters (that is, the address of the memory space corresponding to the storage) and the return value, Here are the BP frame pointers, in the call Tumbler stack is generally fixed, the following is a number of local variables, storage variables, is relative to the SP stack top pointer to represent, when a call function to pass the address of the time, corresponding to the actual address information, and in the internal operation, is the address related operations, the change of the variable is also a real change, at the end of the operation of the stack space refresh is released, but when the general literature that the return value of the function can not return the array name, actually can see the return value of the relevant information, it will be questioned, Only in the creation of the stack space, in the last use was released, but the last operation stored in the value, only the next time the use of this address space, will be refreshed, resulting in this illusion.

3) How to establish communication between functions

When you want to use the function called in the main function, at this time you just have to call the function name and function corresponding to the required parameters can be implemented in the call and communication between functions, then how he jumps to our previous set of called function, In fact, the compiler will be compiled by default to each different code module by connecting to the corresponding memory address space, and at this point is for a function name, equivalent in the Assembly Keil software by declaring function entry shape like _start or the assembly format in the Eclipse software . Start, To tell the compiler the entry address of the function (in fact it simply means that the entry address is the first valid executable instruction in the function body) at this time when we encounter the function called in the main function, for the CPU processor in the PC The pointer jumps to the called function entry to execute the code block within the calling function block, and we notice that the variable within a function code block is only local.

4) Convention function parameters

Void char (char ch, int num) Such a form represents what interface is provided, used by the caller, only the caller can use such a type to be successfully invoked, where the formal parameter can be a general data type and pointer type, depending on the different types of parameters, The actual arguments are written before the caller, that is, arguments, which can be generic local variables, or address-related numeric values, if an int char is passed in to represent an incoming storage variable, and an address register-related operation is involved in the incoming address.

5) system calls to kernel space

The above call is just a function call of user space, when we understand the function call form of user space and the General Convention, it can be used to understand kernel space system call,Linuxmanagement of virtual memory management mechanisms, in4GThe virtual space is divided into0~3gfor the user space, to run the normal application, the normal function call is also executed in this address space, when we want to invoke the kernel call function, theLinuxIn order not to allow programmers to arbitrarily modify access to hardware devices, the hardware protection mechanism, when we want to access the physical device disk, will fall into the kernel space, in fact, before falling into the kernel space,CPUis through the soft interrupt mechanism into the kernel space, whenCPUrefers to the function name is the system call function, will jump to the corresponding soft interrupt number to perform related operations, such asOpena file, and then to perform read-write related operations, in fact, the kernel space program execution is not directly contact with the device, is throughVFS(virtual file system) for related operations, and at the end of theVFSrelated operations to the corresponding physical address of the associated device operation, in the state of the kernel process to switch back to the app's user process.

Access from a normal function call to the operating system kernel space

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.