BSP (Board Support Package) learning hardware resource management

Kernel management hardware information seen from the application

Applications in user space are used when the kernel space accesses hardware control. The following three methods are explained

  • proc file system and sys file system
  • mmap() system call
  • Hardware resource sharing management (exclusive control)

mmap is one of UNIX system calls. It is a function that maps some or all of the resources on the operating system such as files or devices to a continuous virtual address space.

Viewing the hard control device from the user application program

Linux applications are trapped and run in a virtual CPU environment, which is called a process with completely independent memory space. This is a safety measure to ensure that even if an error occurs in the program, other applications will not be stopped.

When the application uses the hardware, you need to request the device driver (actually call) and operate from the kernel space. This is because the kernel hides the hardware resources so as not to operate the hardware directly from each application to ensure system consistency.

The applications in each process think that they can be independent of all hardware resources, but in some cases, multiple applications may merge the same hardware. To this end, the kernel includes arbitration management for the use of hardware resources.

Next, I will explain how the kernel displays hardware control methods in user space.

Process information and device information

You can talk to the kernel without going through the device driver

PCs and smartphones are now GUI (Graphical User Interface) operations. Previously, programs were started by CUI (Character User Interface) operations where commands were input from the keyboard. The program that accepts CUI keyboard input is called shell. A shell called bash is used as standard in Linux. The Linux shell is not a keyboard input, and it can also be called and used from a simple program that describes the batch processing steps of a shell script. The original meaning of shell is shell, and bash is to allow users to see the program of the kernel that is wrapped in the shell. Shell contains an interpreter function that talks to the kernel. When you execute commands from the shell, you can interactively access kernel management information without going through device drivers.
Insert picture description here

Through the pseudo file, you can see the hardware information of the file access feeling

This time, we will focus on the two directories /proc and /sys in the root directory of the file system. At first glance, /proc and /sys seem to be an ordinary directory, but the files in the directory are not data entities on the HDD. These files are files that the kernel temporarily deploys on RAM to display hardware information to the application. Unlike ordinary files, since there is no data entity on the storage device, /proc and /sys can also be called pseudo files.

In the Linux environment, virtual files and remote files (such as NFS accessed through the network) are placed flat in the directory. This allows applications to access files without knowing where they are actually placed. The location of the file and the Windows bound with the driver letter are different parts of the idea.

The mechanism of displaying process information.../proc file system

As inferred from the name, /proc is the mechanism by which the kernel displays process information to the application. There are many numerically named subdirectories under the /proc directory. It stores the internal information of all processes currently existing on the Linux system in a subdirectory of the process number (=PID) name.

For example, if you use the cat command from the shell to view a file named /proc/1/status, you can view the internal information of the init process with PID=1. Most of the information in the /proc directory is read-only. The timestamp of the file is always the time the file was read, because the kernel is updating the data sequentially. In addition, according to the different data, users with root authority can see the contents of the set access authority.

The information stored under /proc can also be read as ordinary files from the application.

Kernel internal information outside the process is also stored in /proc

The mechanism for displaying device-related information.../sys file system

Physical memory information

Not being able to access the hardware directly from the application is also an obstacle

How to access the kernel space directly from the application... Mmap() system call

To use mmap(), contiguous free space is required in physical memory

Hardware resource sharing management (exclusive control) information

In a Linux environment where multiple programs are running at the same time, exclusive control of hardware resources is indispensable

Semaphore...Used when there are two or more shared resources

Mutex(MUTual EXclusion)...Used when sharing a resource in a multi-core environment, etc.

Guess you like

Origin blog.csdn.net/qq_18191333/article/details/108949789