未对齐的内存访问 [英] Unaligned memory access

查看:135
本文介绍了未对齐的内存访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我玩弄在C内存地址,不知道这个话题名为对齐的内存访问的.

I am playing around with memory addresses in C and wondering about this topic named unaligned memory access.

我目前在使用Linux内核的x86 Intel上,但是本着Arch和OS不可知论的精神问这个话题-尽管以下内容是 特定于Linux和硬件的:

I am currently on a x86 Intel with Linux kernel, but ask the topic in the spirit of arch and OS agnosticism – though the following is rather Linux and HW specific:

当我读/写从/简单类型到未对齐的地址我得到无故障.日志中没有消息或其他任何内容.我也尝试过:

When I read/write a simple type from/to an unaligned address I get no fault. No messages in logs or anything. I have also tried:

perf top -e alignment-faults
# And with PID
perf top -p NNN -e alignment-faults

,但没有命中.

but no hits.

打开对准通过检查:

__asm__("pushf\norl $0x40000,(%esp)\npopf");

给出需要" 结果:

Bus error (core dumped)

(但perf中仍然没有消息.)

(but still no messages in perf.)

我的问题是如何通过硬件+ OS处理,什么是最佳的.我的想法和问题无处不在,但是我将尝试表达一些具体点:

My question is how this is handled by the hardware + OS and what is optimal. My thoughts and questions are all over the place, but I'll try to phrase some concrete points:

  1. 是否CPU具有对齐检查的的默认,但内核发现的关闭的支持,并指示它的不检查的?
  2. 内核,至少我在其他HW遇到过,可以得到哎呀,由于一些司机试图访问未对齐的内存:不中的对齐检查的 - 模式的内核运行?或者是认为呢?
  3. 的代码或许只有某些部分
  4. 作为未对齐的内存的存取需要更多的资源;在软件的测试阶段,例如在装配线上方进行对齐检查,是一个好主意吗?请问这也让它更加轻便?
  1. Does the CPU have alignment checking on by default, but the kernel detects that off is supported and instructs it to do not check?
  2. As the kernel, at least I have experienced this on other HW, can get oops due to some driver trying to access unaligned memory: does the kernel run in alignment check-mode? Or is it perhaps only certain parts of the code that does?
  3. As access of unaligned memory require more resources; is it a good idea to enable alignment checking, as by for example above assembly line, in a test-phase for software? Would this also make it more portable?

关于这个问题,我还有很多其他问题,但请暂时保留.

I have a lot of more questions around this, but leave it at this for now.

推荐答案

请尝试给出部分答案.

CPU默认情况下是否启用对齐检查,但是内核 检测到支持关闭功能并指示其不进行检查?

Does the CPU have alignment checking on by default, but the kernel detects that off is supported and instructs it to do not check?

这取决于拱,甚至在同一拱上,可能是HW可以处理未对齐内存中的某些指令,而其他则不能.

It depends on the arch and even on same arch it might be that some instructions on unaligned memory can be handled by HW while others can not.

作为内核,至少我在其他硬件上有过这样的经验,可以得到 由于某些驱动程序尝试访问未对齐的内存而导致操作失败: 内核对齐检查模式下运行?或者是也许只有某些 代码的一部分呢?

As the kernel, at least I have experienced this on other HW, can get oops due to some driver trying to access unaligned memory: does the kernel run in alignment check-mode? Or is it perhaps only certain parts of the code that does?

硬件不支持的未对齐内存访问将导致陷阱,并且内核具有用于陷阱/异常的处理程序. 我一直在研究ppc,这种异常将根据指令(从PC中获取)进行处理;一些指令已得到处理,程序将继续运行;其他一些可能导致程序终止,因为内核无法处理它.这样的示例之一就是stwcx指令,该指令用于实现比较和交换逻辑.

Unaligned memory access no supported by HW would cause a trap and kernel has handler for the trap/exception. I've been working on ppc and such exception would be handled based on the instruction (gotten from the PC); some instructions are taken care of and the program would resume; some others might cause the program to terminate as kernel cannot handle it. One such example is the the stwcx instruction which is used to implement compare-and-swap logic.

作为未对齐的内存的存取需要更多的资源;好不好 启用对齐检查的想法,例如上面的程序集 行,在测试阶段的软件?这还会使它更多吗 便携式?

As access of unaligned memory require more resources; is it a good idea to enable alignment checking, as by for example above assembly line, in a test-phase for software? Would this also make it more portable?

在实践中,如果您在大型项目中有很多遗留代码,这可能不是一个好主意……但是,对于新代码而言,它应该是一个好主意.

In practice it might not be a good idea if you have a lot of legacy code in a large project..... but it should be good for new code.

这篇关于未对齐的内存访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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