Nineth Chapter Experience:
HAL (Hardware abstraction Layer, Hardware abstraction cavity) is a set of turn-up fonts built on Linux drivers. The program 's J rate does not belong to the linux kernel, but is the application layer on top of the Linux kernel layer.
The purpose of joining Hal :
(1 ) unified hardware calling interface. Because hal There are standard calls to the bedroom, so you can use the Span style= "FONT-FAMILY:CALIBRI;" >hal shield linux driving complex, non-uniform excuses
(2 ) resolved GPL copyright issues. Because linux kernel based on GPL protocol, and android based on apache Licence 2.0 , agreement. So google played a" crossing the fly will be originally located in the Span style= "FONT-FAMILY:CALIBRI;" >linux The sensitive code in the drive moves up one level two so that these sensitive code get rid of the GPL The binding of the agreement. Those who do not want to open source linux Driver authors have no need to open up.
(3) for some special requirements. For some hardware, you may need to access the resources of some user space, or work that is inconvenient in the kernel space and special needs. In this case, the HAL code located in the user space can be used to assist the Linux driver to do some work.
To add a HAL step to the led driver :
Step 1 , write the Linux driver
"Writing Linux drivers" is nonsense from the show, but if you want to add HALto the Linux driverand want to protect sensitive data as much as possible. Linux - driven code needs to be as concise as possible to put business logic into hallibrary .
Step 2 : Write Hal Library Hal Library
Hal Library Hal Library is the normal Linux library *.so ) file. However, this type of library file has an interface. Implemented by the HAL _module_info _ SYM variable. the Service Library locates the HAL Library by the ID defined in this interface .
Step 3 : Write the Service Library
Although this step is not required, the newHALarchitecture requires us to do so. Service Libraryis alsoLinux Library. This step is more flexible. Service Libraryit can be normal.Linux Library, you can also identifyLibrary. In this chapter,LEDThe driver example willService Libratyand theLibrarytogether. In other words,Service Libraryis thatJNI Library.In fact, this step is in addition toCICimplementation of the item *(s)library file, you should also include aJavathe service management classes written(ServiceManager). ServiceManageris calledService Library. andAPKprogram will callServiceManagerClass meter AccessService Library.
Tenth Chapter Experience
Print debug Information printk. the printk function has been used in most of the previous chapters. The function is similar to the printf function, but the printk function runs in kernel space.
PRINTK is a function that runs in the kernel to the console output , and thelinux kernel allocates a static buffer in the kernel space as a display space and then calls the sprintf , formats the display string, and finally calls the Tty_write The display of information to the terminal.
Printkwith theprintfThe difference is what leads to a run inKernel state and another running user state?actually the two functions are almost the same, and this difference occurs becauseTty_writefunction needs to useFSpointer to the displayed string, while theFSis specifically used to store user-state segment selectors, so in the case of a kernel state, in order to matchTty_writefunction,PRINTKwill putFSchange to Kernel stateData Segment SelectorDSin order to correctly point to the data buffer of the kernel, which, of course, firstFSto save the stack, callTty_writewhen you are finished ,recovery from the stack. In conclusion,PRINTKwith theprintfthe difference is determined by theFScaused, so the difference is also around theFSthe processing.
Proc_mkdir
Name: virtual directory names.
Parent: The proc_dir_entry struct pointerfor the parent directory of the virtual directory . If the virtual directory is created directly under the/proc Directory, the value of this parameter is NULL.
Create_proc_entry
Name: Virtual file names. mode: Access to the virtual file, equivalent to The access rights of the Linux file. Parent: The proc_ dir _ entry struct pointerof the parent directory of the virtual file . Ifthe virtual file is created directly under/proc , the value of this parameter is NULL.
Create_proc_read_entry
name: Virtual file name. mode : Access rights to virtual files, equivalent to linux file access rights. Base: proc_ dir _ entry proc null
Read_proc: A function pointer that handles read actions. data: For the virtual file system (any type of pointer). The value is the last parameter value of the proc_ dir_entry.read _proc function. The equivalent of data that is permanently bound to a virtual file. If you do not want to set this data, you can null. Remove _proc _ entry . Name: The names of the virtual files to be deleted. Parent: The proc_ dir _ entry struct refersto the Zhao of the virtual file parents directory . If the virtual file is created directly under the/proc Directory, the value of this parameter is NULL.
11th Chapter Experience:
Interrupt masking, atomic manipulation, spin locks, semaphores, and mutexes are all mechanisms to solve concurrency problems. Interrupt masking is rarely used alone, atomic operations can only operate on integers and bits, while spin locks, semaphores, and mutexes should be more extensive. Of course, if you emphasize the order in which code snippets are executed, you can use the completion amount. The spin lock will cause a dead loop (that is, a deadlock) due to constant spin, and the lock state will cause the mortar U to be idle, so the code of the critical section protected by the spin lock cannot take too long, and of course, the blocking situation in the critical section is not allowed. The signal volume allows the critical zone to be confused and therefore suitable for large critical areas. Read/write ↓ spin lock and read and write signals are relaxed the condition of the spin lock and signal volume, they allow multiple execution units simultaneously. The shared resource performs a read operation.
actions related to the completion amount in Linux:
(1) Define the complete amount of the struct completion my_completion;
(2) Initial completion amount init_completion (&my_completion);
(3) Wait for the completion amount wait_for_completion;
(4) The amount of wake-up complete();
There are 4 main types of operations related to mutexes in Linux systems .
- Defining mutexes
Defining the mutex requires the use of the mutex structure, the code is as follows: struc.t mutex My_mutex;
2. Initialize the nutex_init If a mutex is defined with a pure structure variable F-ear to make the spacer function to initialize the variable,
3. get the Mutex
4. Releasing and closing the mutex
The classification of Signal volume
Before we learn the semaphore, we must first know that --linux provides two semaphores:
(1) kernel semaphore, used by kernel control path
(2) The signal volume used by the user-state process, which is divided into POSIX semaphore and SYSTEMV signal volume.
The POSIX semaphore is divided into a well-known semaphore and a nameless signal volume.
A well-known semaphore whose value is stored in a file , so it can be used for threads or for synchronization between processes. The nameless semaphore, whose value is stored in memory. Without a complete understanding of the semaphore, you will soon find yourself lost in the semaphore forest.
12th Chapter Experience
A blocking operation is a process that hangs when a resource is not available while performing a device operation until the operational conditions are met.
Because the blocked process goes into hibernation, you must ensure that there is a place to wake the dormant process. The most likely place to wake up the process is in the interrupt, as the hardware resource is often accompanied by an interrupt.
Note: The driver needs to provide a blocking (waiting queue, interrupt) and non-blocking mode (polling, asynchronous notification) to access the device.
Dormancy(be blocked)process is in a special, non-executable state. This is very important, otherwise, without this particular state, the scheduler may elect a process that is unwilling to be executed, and, worse, hibernate must be done in the form of polling. Process hibernation can be a variety of reasons, but it must be to wait for some events. The event may be a period of time, from a fileI/Oread more data, or a hardware event. A process may also be forced into hibernation when trying to acquire an already occupied kernel semaphore. A common cause of hibernation is the fileI/O--as the process executes on a fileRead ()operation, which needs to be read from the disk. Also, the process needs to wait while getting the keyboard input. In either case, the kernel operates in the same way:The process marks itself as dormant, moves itself out of the executable queue, puts it in the waiting queue, and then callsSchedule ()Select and execute a different process. The process of awakening is just the opposite.:The process is set to an executable state and then moved from the wait queue to the executable queue.
Hibernation has two related process states :task_interruptible and Task_uninterruptible. The only difference is that processes in the task_uninterruptible State ignore the signal and are in the task_interruptible State of the process if the received signal is awakened and the signal is processed ( and then again into the waiting sleep state ). Processes in both states are on the same waiting queue, waiting for certain events to run. Hibernation is processed by waiting for a queue. The wait queue is a simple list of processes that wait for certain events to occur. The kernel uses wake_queue_head_t to represent the waiting queue.
The wait queue can be created statically by Declare_waitqueue ().
Can also be created dynamically using Init_waitqueue_head () . The process puts itself in the waiting queue and is set to a non-executable state. When events related to the wait queue occur, the processes on the queue are awakened. To avoid a competitive condition, sleep and wake-up implementations cannot be flawed .
Wait queue
In Linux drivers, you can use the wait queue to wake up a blocking process.
The process adds itself to a wait queue by performing the following steps:
Of course, the first is to define the wait queue header and initialize:
(1) wait_queue_head_t wait;
(2) Init_waitqueue_head (&wait);
Android Monthly Exam