Linux device driver learning ---- 2. Comparative kernel modules and applications

Comparative kernel module with the application

For more information, please refer to Linux device driver learning ---- directory

1. Comparative kernel module with the application

Between kernel modules and applications differences:

  1. Most small and medium-scale applications is to perform a single task from start to finish, but it is only pre-register their modules in order to serve a future request, then the end of the initialization function immediately. That module initialization function (hello_init) task for the future is to call a function module to prepare in advance. Exit function modules (hello_exit) will be called before the module is unloaded.

  2. This event-driven programming is somewhat similar, but not all applications are event-driven, and each kernel modules are like this. Another difference between event-driven programming and kernel modules is that when you exit the application, can be released regardless of resources or other cleanup work, but the exit function module initialization function must withdraw all done to ensure that no excess residue content in the system.

  3. An application can call functions it does not define, because the linking process can resolve external references, thus using the appropriate link libraries. The modules are linked only to the kernel, so the function module can call only those functions exported by the kernel, without any linkable libraries. Because there is no function module library and links, so the source file does not include the header file is usually the application. Kernel modules can only be used as a function of the core part. Most relevant kernel header files and the associated stored in include / linux and include / asm directory.

  4. Different treatment wrong way applications and kernel programming section of the application error debugger can be used to trace the source of the problem, even if the kernel error does not affect the system will kill the current process.

Benefits module unload help shorten the development cycle of modular drivers.

2. The user space and kernel space

  Module runs in kernel space, the application is running in user space.

  In Unix kernel running at the highest level, i.e., super user mode, this level can perform all operations. While the application is running at the lowest level, that user mode, the processor controls the level of direct access to hardware and unauthorized access to memory.

  Kernel space and user space, not only shows the two modes have different priority levels, but also a description of each mode has its own memory mapping, that is its own address space.

  When the application executes a system call or a hardware interrupt is pending, Unix execution mode switching from user space to kernel space. Kernel system call code to run in process context, on behalf of the calling process to perform the operation. So all the data can access the process address space. Regardless of the kernel code and process asynchronous processor hardware interrupts, and any one process.

  Modular code running in kernel space for extending the kernel function. Driver to perform two types of tasks, some part of the execution function modules as system calls; others function is responsible for interrupt handling.

3. Kernel Concurrency

  The difference between the kernel and application programming that the programming process is concurrent . Most applications, in addition to multi-threaded applications, usually performed sequentially. Operating Environment kernel code more complicated, even the simplest kernel module, you need to pay attention to: the same time, there may be a lot of things happen.

Kernel programming must take into account the concurrent cause of the problem:

  1. Linux systems usually are running multiple concurrent processes, and there may be multiple processes simultaneously using a driver;
  2. Most devices can interrupt the processor, and interrupt handlers run asynchronously, and may be called when the driver is trying to handle other tasks;
  3. Some software abstraction (such as: core timer) also running asynchronously;
  4. Linux may run on a symmetric multiprocessor system (SMP), and therefore may be more than one CPU while running the driver;
  5. The core code Linux2.6 is preempted, even there is a similar multiprocessor system on a single processor system concurrency issues.

  Linux kernel code (including the driver) must be reentrant , and must be able to run simultaneously in multiple contexts. Kernel data structures to ensure the implementation of multiple threads separate, shared code to access the data must be destroyed to avoid sharing data. To be able to handle concurrency issues drive, while avoiding race. Kernel code can not be assumed that the code segment can be given exclusive processor.

4. The current process

  Although the kernel module application as not executed sequentially, but most of the operations performed by the kernel or a particular process and relevant. Kernel code can be obtained current process by accessing the global item current.

  In the current definition of <asm / current.h> in, it is a pointer to a struct task_struct. current pointer to the processes that are currently running. You can print the current process by accessing certain members of struct task_struct process ID and command name:

printk(KERN_INFO "The process is \"%s\" (pid %i)\n", 
        current->comm, current->pid);

  Command name stored in current-> comm members is the basic file name of the program executed by the current process, cutting less than 15 characters.

5. Other details

  Applications in virtual memory layout, and has a lot of stack space. Stack is used to save the function calls automatically variable function of history and current events. The core has a small stack, it may be a page size of 4096 bytes of space. Driver functions must call chain and the entire kernel space shared with the stack. Therefore, a large automatic variable can not be declared, if need large structure, the structure should be dynamically allocated when you call.

  Function name in the kernel API having double underscore (__), typically underlying component interface should be used sparingly.

  Kernel code can not implement floating-point operations, floating point operations need not kernel code.

For more information, please refer to Linux device driver learning ---- directory

Guess you like

Origin www.cnblogs.com/microxiami/p/11258216.html