Like attention, no more lost, your support means a lot to me!

🔥 Hi, I’m Chouchou. GitHub · Android-Notebook has been included in this article. Welcome to grow up with Chouchou Peng. (Contact information at GitHub)

preface

  • In the process of analyzing Android source code, it often goes through the process of APP -> Framework -> native -> kernel, and finally comes to the boundary between user program and kernel sequence, that is: System Call;
  • A clear understanding of the concepts related to system calls will be of great benefit to further understanding of other key knowledge. In this article, I will briefly analyze the concepts related to interrupts & system calls, please be sure to like and follow if you can help, it really means a lot to me.

1. Interrupt mechanism

1.1 What is interrupt?

  • define

Interrupts are one of the basic mechanisms in a computer system. That is, when an event occurs during computer operation, the CPU stops the current program flow, resolves the event, and resumes the original program flow after processing.

Relevant concepts describe
Interrupt classification Hardware interrupts & Software Interrupts
Interrupt Vector Table The mapping relationship between interrupt number and memory address of interrupt service program is recorded
Interrupt Service/Interrupt handler A specific handler that is located through the interrupt vector table

1.2 Why are Interrupts introduced?

The advantage of the interrupt mechanism is to turn the initiative into the passive, avoiding the CPU polling for a condition to be true. Without an interrupt mechanism, a “certain condition” would require CPU polling, which would add overhead to the system. Using the interrupt mechanism, you can send interrupt events to the CPU after the condition is established, forcing the interrupt CPU to execute the program and turn to execute the interrupt handler.

1.3 hard interrupt

Hard interrupts are generated by external devices (such as disks, network cards, keyboards, and clocks) to notify the operating system of changes in peripheral status.

Clock interrupt: A hard interrupt used to periodically interrupt a thread of CPU execution in order to switch to another thread for execution.

The hard interrupt processing flow is as follows:

  • 1. Peripheral sends interrupt request to interrupt controller;
  • 2, interrupt controller according to interrupt priority, orderly interrupt to the CPU;
  • 3. CPU terminates the execution of the current program flow and saves all the values of the CPU registers on the stack;
  • 4, CPU according to the interrupt vector, from the interrupt direction table to find the interrupt handler entry address, the execution of the interrupt handler;
  • 5. CPU restores the value in the register and returns to the stop position of the original program flow to continue execution.

1.4 softirqs

A soft interrupt is a CPU instruction that is generated by the currently running process.

Soft interrupts simulate hard interrupts:

  • 1,
  • 2,
  • 3. CPU terminates the execution of the current program flow and saves all the values of the CPU registers on the stack;
  • 4, CPU according to the interrupt vector, from the interrupt direction table to find the interrupt handler entry address, the execution of the interrupt handler;
  • 5. CPU restores the value in the register and returns to the stop position of the original program flow to continue execution.

System call: a soft interrupt handler used to drop a program from user state into kernel state to perform an operation.


2. System call

2.1 Boundaries between operating systems and applications

  • The kernel space

Operating System is a program that manages computer hardware and software resources. The Operating System kernel resides in the protected kernel space.

  • The user space

An application is a program that runs on an operating system and works in user space.

  • isolation

For security and stability reasons, user-space programs cannot execute kernel code directly (e.g., I/O reads and writes, create new processes/threads), nor can they access kernel data, and must do so through system calls.

2.2 Definition of system call

A system call (Syscall) is a soft interrupt handler used to drop a program from user state into kernel state to perform the corresponding operation.

2.3 Functions of System Call

When a system call occurs, it drops the program from user state into kernel state to perform the corresponding operation.

2.4 System call interrupt handler flow

  • 1, the program from user state into the kernel state
  • 2. According to the system call number, find the memory address of the corresponding system call function in the system call table and execute the system call function.
  • 3. The program returns from kernel state to user state

The resources

  • How Does a Program Run (chapters 9 and 11) by [Nishiko] Yazawa
  • The Art of Linux Kernel Design: Illustrated Principles of Linux Operating System Architecture Design and Implementation (2nd edition) (Chapter 1) by The New Design Team
  • Analysis and Application of Linux kernel (Section 1.5, Chapter 4) by Chen Ke
  • Principles of Linux System Calls (Syscall). By Gityuan
  • “Linux kernel analysis – System call from user state to kernel state flow (4, 5)” — Linux sharing official
  • Self-cultivation of programmers: Linking, Loading, and Libraries (chapter 12). By Yu Jiazi, Shi Fan, pan Aimin

Recommended reading

  • Cryptography | is Base64 encryption algorithm?
  • Interview questions | back algorithm framework to solve problems
  • The interview questions | list questions summary algorithm
  • Computer network | graphic DNS & HTTPDNS principle
  • Say from Android Android | : text to TextView process
  • Android | interview will ask Handler, are you sure you don’t look at it?
  • Android | show you explore LayoutInflater layout analysis principle
  • Android | View & fragments & Window getContext () must return to the Activity?

Thank you! Your “like” is the biggest encouragement for me! Welcome to attentionPeng XuruiThe lot!