为什么内核代码不能使用红色区域 [英] Why can't kernel code use a Red Zone

查看:167
本文介绍了为什么内核代码不能使用红色区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

强烈建议在创建64位内核(对于x86_64平台)时指示编译器不要使用用户空间ABI所使用的128字节的Red Zone. (对于GCC,编译器标志为-mno-red-zone.)

It is highly recommended when creating a 64-bit kernel (for x86_64 platform), to instruct the compiler not to use the 128-byte Red Zone that the user-space ABI does. (For GCC the compiler flag is -mno-red-zone).

如果启用,内核将不是中断安全的.

The kernel would not be interrupt-safe if it is enabled.

那为什么呢?

推荐答案

引用AMD64 ABI:

Quoting from the AMD64 ABI:

%rsp所指向的位置之外的128字节区域被视为已保留,并且不得由信号或中断处理程序修改.因此,函数可以使用该区域存储函数调用不需要的临时数据.特别是,叶函数可以在整个堆栈框架中使用此区域,而不是在序言和结尾中调整堆栈指针.该区域被称为红色区域.

The 128-byte area beyond the location pointed to by %rsp is considered to be reserved and shall not be modified by signal or interrupt handlers. Therefore, functions may use this area for temporary data that is not needed across function calls. In particular, leaf functions may use this area for their entire stack frame, rather than adjusting the stack pointer in the prologue and epilogue. This area is known as the red zone.

从本质上讲,这是一种优化-用户区编译器确切地知道在任何给定时间使用了多少红色区域(在最简单的实现中,局部变量的整个大小),并且可以在调用a之前相应地调整%rsp.子功能.

Essentially, it's an optimization - the userland compiler knows exactly how much of the Red Zone is used at any given time (in the simplest implementation, the entire size of local variables) and can adjust the %rsp accordingly before calling a sub-function.

尤其是在叶函数中,这可以带来一些无需调整%rsp的性能优势,因为我们可以确定在函数中不会运行任何不熟悉的代码. (POSIX信号处理程序可能被视为一种协同例程的形式,但是您可以指示编译器在信号处理程序中使用堆栈变量之前先调整寄存器).

Especially in leaf functions, this can yield some performance benefits of not having to adjust %rsp as we can be certain no unfamiliar code would run while in the function. (POSIX Signal Handlers might be seen as a form of a co-routine, but you can instruct the compiler to adjust the registers before using stack variables in a signal handler).

在内核空间中,一旦您开始考虑中断,如果这些中断对%rsp作了任何假设,则它们可能是不正确的-关于使用红色区域没有确定性.因此,您要么假设所有变量都是脏的,并且不必要地浪费了堆栈空间(有效地在每个函数中使用128字节保证的局部变量运行),要么,您保证中断不对%rsp作任何假设-这很棘手.

In the kernel space, once you start thinking about interrupts, if those interrupts make any assumptions about %rsp, they will likely be incorrect - there is no certainty with regards to the utilization of the Red Zone. So, you either assume all of it is dirty, and needlessly waste stack space (effectively running with a 128-byte guaranteed local variable in every function), or, you guarantee that the interrupts make no assumptions about %rsp - which is tricky.

在用户空间中,上下文切换+堆栈的128字节过度分配为您处理.

In user space, context switches + 128-byte overallocation of stack handle it for you.

这篇关于为什么内核代码不能使用红色区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆