我可以将rsp用作通用寄存器吗? [英] Can I use rsp as a general purpose register?

查看:157
本文介绍了我可以将rsp用作通用寄存器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人告诉我是否使用 rsp 作为通用寄存器,如果发生中断,操作系统可能会将寄存器转储到它指向的位置,从而导致问题行为。 / p>

是真的,如果不是这样,如果不需要堆栈,可以使用 rsp 作为



编辑:在用户空间中运行。

解决方案

< blockquote>

如果发生中断,您是否不知所措?


在DOS中进行编程的人可能会在这一点上对中断的可能性感到困惑。通常,像这样重用堆栈指针是一个非常糟糕的主意,因为您不知道何时可能会发生中断,并且当中断发生时,CPU会忠实地将当前程序计数器和标志压入堆栈。如果重用了ESP,这将导致随机数据结构被破坏。在这种环境下,ESP必须始终指向有效且足够的堆栈空间来服务于中断,并且只要此中断不成立,就必须禁用中断。在长时间禁用中断的情况下运行会降低系统响应能力(中断丢失和延迟延迟),并且对于大型例程而言不可行。


但是,我们在此处以保护模式运行


在Win32中的用户空间中运行时,中断不会压入用户堆栈,而是压入内核堆栈。如果考虑到这一点,就不可能使用用户堆栈。如果线程空间不足或什至只有无效的堆栈,则当CPU尝试推送EIP和EFLAGS时,它将出现页面错误,并且您无法在中断处理程序中页面错误。因此,调度程序可以在运行无堆栈例程的同时执行任意数量的上下文切换,并且不会影响任何指向ESP的数据结构。


来自 http://www.virtualdub.org/ blog / pivot / entry.php?id = 85


I was told if I use rsp as a general purpose register the operating system may dump registers to where it points in the case of an interrupt, causing problematic behavior.

Is this true, and if not hence, if I don't need a stack, could I use rsp as a general purpose register?

Edit: Running in user space.

解决方案

Aren't you screwed if an interrupt occurs?

Those of you who have programmed in DOS are likely squirming at this point about the possibility of interrupts. Ordinarily, reusing the stack pointer like this is a really bad idea because you have no idea when an interrupt might strike, and when one does, the CPU dutifully pushes the current program counter and flags onto the stack. If you have reused ESP, this would cause random data structures to be trashed. In this kind of environment, ESP must always point to valid and sufficient stack space to service an interrupt, and whenever this does not hold, interrupts must be disabled. Running with interrupts disabled for a long time lowers system responsiveness (lost interrupts and bad latency), and isn't practical for a big routine.

However, we're running in protected mode here.

When running in user space in Win32, interrupts do not push onto the user stack, but onto a kernel stack instead. If you think about it, it isn't possible for the user stack to be used. If the thread were out of stack space, or even just had an invalid stack, when the CPU tried to push EIP and EFLAGS, it would page fault, and you can't page fault in an interrupt handler. Thus, the scheduler can do any number of context switches while a no-stack routine is running, and any data structures that are being pointed to be ESP will not be affected.

From http://www.virtualdub.org/blog/pivot/entry.php?id=85

这篇关于我可以将rsp用作通用寄存器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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