免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123
最近访问板块 发新帖
楼主: wanyuan111
打印 上一主题 下一主题

[内存管理] 如何使得程序所在的内存页不能被cacke? [复制链接]

论坛徽章:
1
双鱼座
日期:2013-08-28 13:47:26
21 [报告]
发表于 2013-11-01 10:53 |只看该作者
Had anybody had test the 'memory' in the clobbered list? I tested it in my machine by using gcc with -O and it took no effect.
Here is my code:


#include <stdio.h>
void foo(int x)
{
        __asm__ __volatile__ ("add $0x0a,%0"::"c"(x));
        __asm__ __volatile__("": : :"memory");
        printf("%d\n",x);
}
int main(int argc,char* argv[])
{
        foo(10);
        return 0;
}

It seems should output 10,but the result is 20. Gcc still use the ecx instead the variable 'x' in memory.

BTW, i thought the 'memory' in clobbered list did not invalid the cache, it just told the gcc to reload the variables when being used, quite like the 'volatile' key word in C language, other than
invalid the data in caches and registers.

论坛徽章:
0
22 [报告]
发表于 2013-11-02 11:49 |只看该作者
回复 21# firkraag
firstly,I don't know what you want to get in your function.Uncachable? or something else?
secondly,whatever you want ,I think ,you should put the instruction :__asm__ __volatile__("": : :"memory") at the front of the instruction : __asm__ __volatile__ ("add $0x0a,%0"::"c"(x));   
And then,please read the memrory barrier carefully,and make sure that you have  understood the instruction :__asm__ __volatile__("": : :"memory") .

最后,请说汉语!

   

论坛徽章:
1
双鱼座
日期:2013-08-28 13:47:26
23 [报告]
发表于 2013-11-02 13:53 |只看该作者
本帖最后由 firkraag 于 2013-11-02 13:56 编辑

The gcc manual said:

If your assembler instructions access memory in an unpredictable fashion, add ‘memory’ to the list of clobbered registers. This causes GCC to not keep memory values cached in registers across the assembler instruction and not optimize stores or loads to that memory.

What my code doing is to proving that the gcc does not do things  as it claimed. Put 'memory' in the clobbered list would prevent the program from loading value from cpu cache is mistranslated by somebody.It just prevents the compiler from reusing the value stored in the registers when optimization applied(which means reload the value from memory, if the value is cached in the cpu cache, it will load it from the cpu cache of course.)

A memory barrier that only affects the compiler is a software memory barrier.The barrier() macro is the only software memory barrier.This function inserts a software memory barrier that affects the compiler code generation, but it does not affect the hardware's execution of instructions.

I can not type characters under the os i am using now. If i can, i am glad to use Chinese.

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP