ULK --- Chap3 Processes: Process Descriptor Handling

Processes are dynamic entities whose lifetimes range from a few milliseconds to months. Thus, the

kernel must be able to handle many processes at the same time, and process descriptors are stored

in dynamic memory rather than in the memory area permanently assigned to the kernel. For each

process, Linux packs two different data structures in a single per-process memory area: a small data

structure linked to the process descriptor, namely the thread_info structure, and the Kernel Mode

process stack. The length of this memory area is usually 8192 bytes (two page frames). For reasons

of efficiency the kernel stores the 8-KB memory area in two consecutive page frames with the first

page frame aligned to a multiple of 8KB; this may turn out to be a problem when little dynamic memory

is available, because the free memory may become highly fragmented. Therefore, in the 80x86 architecture

the kernel can be configured at compilation time so that the memory area including stack and thread_info

structure spans a single page frame (4096 bytes).

In the section "Segmentation in Linux" in Chapter 2, we learned that a process in Kernel Mode accesses

a stack contained in the kernel data segment, which is different from the stack used by the process in User

Mode. Because kernel control paths make little use of the stack, only a few thousands bytes of kernel 

stack are required. Therefore, 8KB is ample space for the stack and the therad_info structure. However,

when stack and thread_info structure are contained in a single page frame, the kernel uses a few additional

stacks to avoid the overflows caused by deeply nested intertupts and exceptions.

Fig 3-2 in textbook shows how the two data structures are stored in the 2-page (8KB) memory area. The

thread_info structure resides at the beginning of the memory area, and the stack grows downward from 

the end. The figure also shows that the thread_info structure and the task_struct structure are mutually

linked by means of the fields taks and thrad_info, repectively.

The esp register is the CPU stack pointer, which is used to address the stack's top location. On 80x86

systems, the stack starts at the end and grows toward the beginning of the memory area. Right after

switching from User Mode to Kernel Mode, the kernel stack of a process is always empty, and therefore

the esp register points to the byte immediately following the stack.

The value of the esp register is decreased as soon as data is written into the stack.

原文地址:https://www.cnblogs.com/miaoyong/p/4938274.html