Embedded future is still the world of Linux, and through the kernel learning to explain the mechanism of the kernel

First explain why I want to learn about the Linux kernel. The earliest is because of the temporary heat of the embedded, I bought the development board, bought a lot of information, and invested more than 1,000 before and after. However, it is still a bit rewarding, although it has not been adjusted, but it seems to broaden the knowledge.

Slowly, I found that for me to learn from the software, the weakness of hardware knowledge is a flaw that cannot be ignored. After all, the generation gap between software and hardware is still not small, just like the old lady and the fashion prostitute, the gap is still Ignore it. This is a bit of a disappointment to me, but no matter how difficult it is, I can't stop my progress. I made a slight adjustment to the strategy, because I found that embedded is still the world of Linux both now and in the future; in addition, the real big cattle who do Linux seem to have the experience of getting involved in the embedded system. This makes it only manage Linux systems and services, simply use a few Shell commands, compile and install several Linux applications, read a few Makefiles... can't meet the requirements at all; in addition, in order to prevent learning OS knowledge is only empty theory. This requires that you must have the ability to write your own Shell program; requires the ability to do program development in Linux; requires the ability to attach custom system calls, reorganize the kernel, add self-write drivers to the kernel. . This requires an in-depth understanding. Just like any technology, if you have been in contact for a long time, you will have the impulse to realize the excavation.

The adjusted strategy is to understand the mechanism of the Linux OS before you can migrate the system that meets the requirements to write a highly efficient Linux application. The overall route is to surround the city in the countryside, constantly supplementing the necessary knowledge, step by step, and finally showing the fire of the stars can pour the original. The amount is far enough. Not much nonsense, start below.

Kernel entry, to choose this good introduction book, I downloaded a "Linux kernel design and implementation" from the Internet. This book is simple and easy to read. People who have the basics of OS and Linux application understand it at a glance. I have read 3 chapters and it feels very good. In addition, the "Linux operating system kernel internship" works better. I want to explain the mechanics of the kernel to you in a more informal way. Let us take the kernel together like a cold water.
Embedded future is still the world of Linux, and through the kernel learning to explain the mechanism of the kernel

First introduce the root directory description of the kernel source:

Arch(architecture) source code for a specific architecture

Crypto crypto API

Documention kernel source documentation

Drivers

Fs VFS and various file systems

Include kernel header file

Init kernel boot and initialization

Ipc interprocess communication code

Kernel core subsystem like dispatcher

Lib generic kernel function

Mm memory management subsystem and VM

Net network subsystem

Scripts scripts used to compile the kernel

Security Linux security module

Sound speech subsystem

Usr early user space code (so-called initramfs)

Here is just a brief description of the directory and system module distribution. As I slowly learn, I believe I can understand them all.

In addition, you need to understand some of the common sense terminology knowledge that Linux must have. Here are some nouns. If you don't understand, you will not elaborate one by one. Baidu, Google: Governance, Stereo, Kernel Space, User Space, POSIX, System V, GNU, GPL, GNOME, KDE, QT, GTK+, openGL, shell, awk, Makefile, CC, GCC, G++, GDB, Perl...

Let's talk about the differences between kernel development and application development:

I can't access the C library when programming the kernel (because many C library functions under Linux are encapsulation of Linux system calls, how can I call myself?)

GNU C must be used when programming the kernel.

Kernel programming lacks a memory protection mechanism like user space.

Floating point numbers are difficult to use when programming in the kernel.

The kernel has only a small fixed length stack.

Since the kernel supports asynchronous interrupts, preemption, and SMP, you must always be aware of synchronization and concurrency.

Consider the importance of portability.

These points are described.

1, the first is the kernel can not access the C library, you think that many C library functions under Linux is a package of Linux system calls, how can you call yourself? The system call here to learn the OS should be clear, is the programming interface provided by the system application to the user, note that the object here is the user. Note that the programmer is also hierarchical. At the kernel level (here, pure kernel programming), you can't see the system call. At this point, your responsibility may be to add a system call to the system (described later) And the C library is the encapsulation of the underlying system to the underlying system (system calls, in order to facilitate my typing, may appear multiple times later), you should understand from a logical point of view. Then the problem comes out, what if there is no c library, and what modularity is it to talk about, is it difficult for Chengdu to write it yourself? This is the next question.

2, since the kernel can not call the C library, then it has to have its own independent c language library, in order to achieve high cohesion and low coupling, and improve its security, so GNU C is used. Short and precise, high efficiency, after all, is dedicated to professionals. It should be noted here that there is no printf() implemented in the kernel; however, there is a more powerful printk(); in fact, it is not powerful (because printf() itself is very powerful, especially when debugging, here too Out of printk's advantage), the significant difference between it and printf() is that printk() allows you to set a priority by specifying a flag. Based on this priority flag, Syslog will decide where to display this system message. Such as:

Printk ( KERN_ERR "This is an error!"); / / Do not understand, I do not understand, after the kernel debugging will definitely be this thing, not afraid.

3. Kernel programming lacks a memory protection mechanism like user space.

what are you doing? The kernel is good, this is the core part of an OS, which controls the operation of the whole system. Naturally, there must be the right to handle and coordinate the whole system. The content of the coding in the kernel is part of the OS core, which is for others or for the OS. Layer used. Since you are grasping all of this, and it is the first layer of abstraction on the basis of hardware, and also grasp the overall situation, the control of memory naturally cannot bind you, just like working in a company, the leader should be within the possible range. As far as possible, the subordinates can play his extremes. It is estimated that the kernel is also the same. Memory access, including other kernel structures, is not bound by the kernel, and of course there is no corresponding protection mechanism. Because this is the kernel is yours, you don't have to be stupid enough to write programs to crash your system, haha. This also leaves a problem, that is, we should consider it in the coding process.

4. Floating point numbers are difficult to use when programming in the kernel. What you need to know here is that when the user space process performs floating point operations, the kernel will complete the mode conversion from integer to floating point number, which is generally achieved by capturing traps and processing accordingly. / / Trap can be regarded as a special kind of exception, is the way to enter the kernel state from the user state, will be further introduced in the future.

Unlike user space, the kernel does not support floating-point operations perfectly because it can't fall into itself. When using floating point numbers in the kernel, there are a lot of trivial things to do in addition to manually saving and restoring floating point registers. Straightforward is: don't use floating point numbers in the kernel! ! !

5. The kernel has only a small fixed length stack. On x86, the kernel stack is configured at compile time, either 4k or 8k, and each processor has its own stack. Why is it so small? Isn't it big? My understanding is that now I am pursuing the microkernel. This is one reason. The kernel is also a software. It is just a process that contains hardware abstraction and is rich in system management functions, while other functional processes (systems) Call, other kernel processes), in the scheduling is, there is a situation where the kernel preempts and replaces the kernel execution. This is to switch the context of the process to be executed. Of course, the data will appear on the stack. If you want the stack to be large, then The memory access address will be very long during operation, and it takes time and space for access, which will reduce efficiency, so it should be as small and flexible as possible...

6. Since the kernel supports asynchronous interrupts, preemption, and SMP, you must always pay attention to synchronization and concurrency. This is not just a Linux kernel. It is a must for all existing multi-process/multi-threaded concurrent OSs, including multi-threaded programming. You think of the kernel as a multi-threaded executable. OK, it's not difficult. understanding.

7. Consider the importance of portability. This needless to say, because this is a gray and often prominent feature of linux, large to the enterprise server, small to embedded android, IP camera ... and many other different platform architecture cpu ... should be running The platform has the most OS, so his kernel needs to be portable enough. It seems to be a bit of java...

Household Electrical Appliances

gree , https://www.greegroups.com