kernel and user stack

进程创建的时候每个进程分配了8K的系统栈,除了一部分用来存放task_struct,

内核栈 用来 表示 进程 由于某种原因进入内核(比如系统调用/Exception)的时候用的。你想啊,进程进入内核后,执行一系列函数,保持函数的返回地址,传递参数啥的,就要用到这个内核栈了。 所以说,在内核中如果使用 较多的递归 会溢出内核栈的。oops

用户栈就是在用户空间指向函数调用的时候用的。 这个栈就大多了,因为其用 MMU映射,可以具有很大的虚拟空间的栈。
Linux下每个用户空间进程(不是kernel thread)都有两个堆栈,一个内核栈,一个系统栈。其中内核栈在创建进程或者线程(do_fork)是创建,在2.6内核中,他的内容如下:
union thread_union {
struct thread_info thread_info;
unsigned long stack[THREAD_SIZE/sizeof(long)];
};
由此可见,此内核栈的高端用于作为堆栈,底端用于存放thread_info(不是task_struct)。此堆栈用于存放进程在内核时的call frames或者接收到中断时的现场状态(pt_regs),在有些体系结构下,也存放同步上下文切换时的状态(switch_stack)。内核栈不能动态增长。

用户栈在进程调用execve的时候(参见fs/binfmt_*.c文件中的load_binary函数,大概是这个函数)创建,对于用clone创建的线程来说,它可以由用户来指定,用户栈和内核没有任何关系,它用于存放进程在用户空间时的call frames。用户堆栈可以动态增长。



Kernel stack, also known as the kernel stack memory or the kernel's thread stack, is a crucial component in an operating system (OS) that manages the memory space used by threads executing within the kernel context. The kernel stack is essentially a portion of the memory specifically allocated for storing stack frames, which are temporary memory areas that hold information about the function calls and local variables for each thread running at the kernel level. Each thread created by the kernel has its own kernel stack, where data structures like function return addresses, saved registers, and other thread-specific information are stored. When a kernel function is invoked, the necessary data is pushed onto the stack to keep track of the current execution state, and upon return, the state is restored from the stack. Here are some key points about the kernel stack: 1. **Synchronization**: Since kernel stack accesses are critical sections, they must be protected by appropriate synchronization mechanisms to prevent race conditions or data corruption. 2. **Memory Management**: The OS kernel ensures efficient management of the kernel stack space, allocating and deallocating it as needed. 3. **Exception Handling**: Kernel stacks help in handling exceptions and traps that occur during kernel-level operations, allowing the system to return control to the correct context. 4. **Isolation**: The kernel stack is typically separated from user-space stacks to provide an additional layer of security and to prevent malicious code from accessing sensitive kernel data.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值