[uncle stupid drops 3] “stack” who is cheap?

Time:2022-5-14

“Once, when having dinner with female classmate a, she took her sister-in-law Liang. During the meal, her classmate gave me a piece of tofu. Sister-in-law Liang looked at me with innocent eyes: Uncle stupid, who else have you taken advantage of besides my mother? I was speechless immediately…”

The above is a short paragraph on the Internet. In life, people often say who takes advantage of who? Does the computer also take advantage of it? There are really many cheap “stacks” in the computer, but they are not “occupied”, but various forms of “stacks”. Do you know how many stacks there are in the computer? for instance:

Kernel stack

Interrupt stack

Process stack

Thread stack

Hardware stack

Software stack

stack

Someone else is occupying the pit



    昨天我们聊了ARM32上或是奇葩或者先进的Interrupt stack,我们今天继续来聊“栈”。

01 what is stack

What is a stack first? Stack is called stack in English. How do you explain the stack in Chinese?

1. Houses for storing goods or accommodating passengers: warehouse | inn.

2. Bamboo and wood fences for livestock: Horse stacks.

What is a computer? In fact, it is a data structure type that stores data. A special linear table that can only be inserted and deleted at one end. It stores data according to the principle of first in and last out. The first entered data is pressed into the bottom of the stack, and the last data is at the top of the stack. When you need to read data, pop up data from the top of the stack (the last data is read out first). It should be clear from the following figure.

[uncle stupid drops 3]

This data structure is characterized by last in first out (LIFO). Data can only be pushed and popped at one end of the string (called top of the stack). Storing data into the stack is called push, and fetching data from the stack is called pop.

Most processor architectures have implementation hardware stacks. There are special stack pointer registers and specific hardware instructions to complete the operation of entering / leaving the stack. For example, in arm architecture, R13 (SP) pointer is a stack pointer register, push is an assembly instruction for pressing the stack, and pop is an assembly instruction out of the stack.

We often hear people say, stack, what the hell is that stack? Is it a heap or a stack? In fact, the stack itself is a stack. Just change an abstract name and a vest, and some people will faint.

What’s that pile? In data structure, heap can be regarded as a tree, such as heap sorting. In the operating system, heap is a way to manage memory in the operating system. It is generally allocated and released by the programmer. If the programmer does not release, the program may be recycled by the OS at the end of the program. The allocation method is similar to linked list. The stack is automatically allocated and released by the operating system to store the parameter values of functions and the values of local variables.

So, heap, stack and stack, are they easy to confuse.

What’s the use of stack 02?

Stack has two main functions, one is function call, the other is process scheduling.

Let’s talk about function calls first. What do we know about function calls? What you might think of is

How to transfer parameters

How to pass the return value of a function

There are different methods in different computer architectures, but the same thing is that they will use stack.

Arm and arm64 use the function calling convention of atpcs (arm thumb procedure call standard / arm thumb procedure call standard).

For arm:

Parameters 1 ~ 4 are stored in R0 ~ R3 registers respectively, the remaining parameters are stacked from right to left, and the return value is stored in R0.

For arm64:

Parameters 1 ~ 8 are stored in x0 ~ X7 registers respectively, the remaining parameters are stacked from right to left, and the return value is stored in x0.

In short, the stack will be used to save the parameters of the function call. Another thing that needs to be saved is local variables.

Taking arm32 as an example, the stack layout of one function a calling another function B is as follows. The two areas pointed to by FP register (R11) and SP register are called a stack frame. Function calls are often nested. At the same time, there will be information of multiple functions in the stack. Each unfinished function occupies an independent continuous area, namely stack frame. Stack frame stores function parameters, local variables and data needed to recover the previous stack frame.

[uncle stupid drops 3]

As shown in the above figure, assuming that the current operation is in function B, the area indicated by the current FP and SP registers is the current stack, stack frame B. When function B returns to function a, another stack constructed by SP and FP registers stored in stack frame B is stack frame a.

Therefore, the stack is a list of linked ‘frames’, and each block of the stack is allocated in descending address order. Register SP always points to the lowest used address in the current frame. The stack on arm is a little strange.

The SP stack pointer mentioned in other CPU architectures points to the top of the stack, but the stack of arm is a self decreasing stack, and the stack grows downward, that is, the bottom of the stack is at the high address and the top of the stack is at the low address. A little exotic and tongue twister.

Another use of stack is process switching. Each process has its own system stack space. This stack space refers to the kernel stack, which is allocated during fork. Therefore, during process switching, the context of the predecessor process needs to be saved to the kernel stack of the predecessor process. In the life cycle of each process, it is bound to fall into the kernel through system calls. After executing the system call and falling into the kernel, the stack used by these kernel codes is not the stack in the original process user space, but a stack in a separate kernel space, which is called the process kernel stack. Processes running in user space need a stack, and processes running in kernel space also need a stack, so the definitions of these stacks are different.

Next time we’ll talk to you about which “stacks” are cheap.

Finally, why should the first instruction be implemented by assembly when the CPU is powered on? Can the CPU execute the code of C language in the first instruction?

Yes or no? Why?

[uncle stupid drops 3]