判断kernel栈底的方法有两种。假设定义了变量uint32_t *ebp, 则既可比较ebp是否为栈底位置,也可比较ebp是否为0.
方法1: 文件entry.S中有这样几行:
bootstack:
.space KSTKSIZE
.globl bootstacktop
可知栈底地址为变量bootstacktop的位置,栈长为KSTKSIZE=32KB。
方法2: 文件entry.S中有如下内容:
# Clear the frame pointer register (EBP)
# so that once we get into debugging C code,
# stack backtraces will be terminated properly.
movl $0x0,%ebp # nuke frame pointer
# Set the stack pointer
方法1: 文件entry.S中有这样几行:
bootstack:
.space KSTKSIZE
.globl bootstacktop
可知栈底地址为变量bootstacktop的位置,栈长为KSTKSIZE=32KB。
方法2: 文件entry.S中有如下内容:
# Clear the frame pointer register (EBP)
# so that once we get into debugging C code,
# stack backtraces will be terminated properly.
movl $0x0,%ebp # nuke frame pointer
# Set the stack pointer
movl $(bootstacktop),%esp
by: chunchengfh, from: chunchengfh.cublog.cn
为什么这里read_eip()是个普通函数而不是一个inline函数,众所周知,inline函数会在编译的时候直接嵌入其调用者的代码里,这样尝试读取eip寄存器值这个动作本身就会改变eip寄存器的值,使read_eip()方法变的毫无意义。
因此作为一个普通函数,在调用的时候会把其调用者的eip入栈,然后在从栈中找到这个eip返回,栈中确切的位置之前分析过是ebp的上4个字节处,所以使用嵌入汇编高效的完成这个功能。