免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123下一页
最近访问板块 发新帖
查看: 3910 | 回复: 22
打印 上一主题 下一主题

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

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-10-22 21:08 |只看该作者 |倒序浏览
本帖最后由 wanyuan111 于 2013-10-25 21:18 编辑

为了测试cache机制对程序运行速度的提升倍数,我需要测试程序在内存页不能被cache的情况下的运行速度。已知的是页表项有一个PCD位用于标识内存页是否可以被cache,但问题是应该怎样才能操作这个PCD位呢?当然有其他的方法也行!用C语言编程~~求大牛解惑!

论坛徽章:
15
射手座
日期:2014-02-26 13:45:082015年迎新春徽章
日期:2015-03-04 09:54:452015年辞旧岁徽章
日期:2015-03-03 16:54:15羊年新春福章
日期:2015-02-26 08:47:552015年亚洲杯之卡塔尔
日期:2015-02-03 08:33:45射手座
日期:2014-12-31 08:36:51水瓶座
日期:2014-06-04 08:33:52天蝎座
日期:2014-05-14 14:30:41天秤座
日期:2014-04-21 08:37:08处女座
日期:2014-04-18 16:57:05戌狗
日期:2014-04-04 12:21:33技术图书徽章
日期:2014-03-25 09:00:29
2 [报告]
发表于 2013-10-23 10:55 |只看该作者
貌似没有用户态的接口,要实现的话,恐怕必须在内核态实现。

论坛徽章:
3
双鱼座
日期:2013-09-04 19:47:39天蝎座
日期:2013-12-11 20:30:532015年亚洲杯之澳大利亚
日期:2015-04-20 00:28:02
3 [报告]
发表于 2013-10-23 23:09 |只看该作者
回复 1# wanyuan111


    我想到一种方法,不知可不可行.  

        (x86)通过汇编语言读取  cr3寄存器的值, 去掉PCDbit,再写入cr3中...
         
          像这种,  asm{ movl  %cr3, %1; \n\t
                               %1 |= ~PCD...
                              movl   %1, %cr3;}
          差不多这样子:wink:

论坛徽章:
0
4 [报告]
发表于 2013-10-23 23:44 |只看该作者
本帖最后由 wanyuan111 于 2013-10-23 23:45 编辑

回复 3# kiongf
PCD是PTE(页表项)里面的cache控制位,而CR3里面存放的是页表的物理地址,所以是不能通过CR3来更改PCD的。我也觉得关键是更改PCD,但就是不知道该如何得到这个页表项,并加以更改~


   

论坛徽章:
3
双鱼座
日期:2013-09-04 19:47:39天蝎座
日期:2013-12-11 20:30:532015年亚洲杯之澳大利亚
日期:2015-04-20 00:28:02
5 [报告]
发表于 2013-10-24 17:56 |只看该作者
回复 4# wanyuan111

PCD Page-level Cache Disable (bit 4 of CR3) — Controls the memory type used to access the first paging
structure of the current paging-structure hierarchy. See Section 4.9, “Paging and Memory Typing”. This bit
is not used if paging is disabled, with PAE paging, or with IA-32e paging if CR4.PCIDE=1.

cr3存储的是pgd的物理地址的PFN, 后面的10bit还是包括这个 PCDbit的吧

论坛徽章:
0
6 [报告]
发表于 2013-10-25 18:36 |只看该作者
回复 5# kiongf
CR3[PCD]=0 permits the processor to cache entries from the page directory, while CR3[PCD] = 1 inhibits caching from the directory.(引自《Pretected Mode Software Architecture》Tom Shanley,MindShare著)
CR3中的PCD位是决定对“页目录项”的缓存的,而不是“页”的缓存。
页目录项中的PCD位才是决定“页”的缓存的,所以说不能用CR3的PCD位。
我查了下CR0貌似可以实现这个功能:
CR0的PCD位功能如下:
CD位用于启用或禁用高速缓存,注意这里的高速缓存所对应的是L1,L2数据或指令高速缓存而并非是用于加速线性地址转换的TLB。


   

论坛徽章:
3
双鱼座
日期:2013-09-04 19:47:39天蝎座
日期:2013-12-11 20:30:532015年亚洲杯之澳大利亚
日期:2015-04-20 00:28:02
7 [报告]
发表于 2013-10-25 19:33 |只看该作者
回复 6# wanyuan111



  CR0.CD是禁止整个RAM的硬件缓存.  恩没错, Pagee-level Cache Disable (bit 4 of CR3) — Controls the memory type
used to access the first paging structure of the current paging-structure hierarchy.   
红色字没注意.

另外,下面是x86禁止cache/TLB的指令: IA-32 Intel Architecture Software Developer's Manual_Volume 3...
    2.7.4 Invalidating Caches and TLBs
The processor provides several instructions for use in explicitly invalidating its caches
and TLB entries. The INVD (invalidate cache with no writeback) instruction invalidates all data and instruction entries in the internal caches and sends a signal to the
external caches indicating that they should be also be invalidated.
The WBINVD (invalidate cache with writeback) instruction performs the same function as the INVD instruction, except that it writes back modified lines in its internal
caches to memory before it invalidates the caches. After invalidating the internal
caches, WBINVD signals external caches to write back modified data and invalidate
their contents.
The INVLPG (invalidate TLB entry) instruction invalidates (flushes) the TLB entry for
a specified page

论坛徽章:
0
8 [报告]
发表于 2013-10-25 20:29 |只看该作者
回复 7# kiongf
非常感谢你的回答!大家共同进步!

   

论坛徽章:
3
双鱼座
日期:2013-09-04 19:47:39天蝎座
日期:2013-12-11 20:30:532015年亚洲杯之澳大利亚
日期:2015-04-20 00:28:02
9 [报告]
发表于 2013-10-26 11:49 |只看该作者
回复 1# wanyuan111


    对于你之前说的通过pte entry来禁止cache, 我有一个想法 : 通过遍历映射了 进程地址空间的所有vma的虚拟页的pte,来去除相应的PCD, PWT bit.
   
     需要在mm/memorc.c增加一个接口. 不过很多细节没考虑到, 只能是参考.
     
   

int walk_mm_cancel_cache(struct task_struct *task)
  {
          struct mm_struct *mm = task->mm;
          struct vm_area_struct vma;
          int    ret;
          spinlock_t *ptl;
          pte_t   *ptep;
          unsigned long address;
              if(!task || !mm)
                      return -EINVAL;
  
          for( vma = mm->mmap ; vma ; vma = vma->next){
  
                  for( address = vma->vm_start; address < vma->vm_end; address += PAGE_SIZE){
  
                          ret = follow_pte(vma->vm_mm, address , &ptep, &ptel);/*find which pte  maps this address*/
                          if(!ret){
                                  *ptep = pte_val(*ptep) & ~PAGE_CACHE_MASK;
                                  /* PAGE_CACHE_MASK:_PAGE_PCD|_PAGE_PWT */
                          }
                  }
          }
              return 0;
  }
EXPORT_SYMBOL(walk_mm_cancel_cache);


其实仔细想想, 这种新增方法好有风险,, 还不如直接写CR0.CD bit来禁止整个RAM的cache.

论坛徽章:
0
10 [报告]
发表于 2013-10-26 14:08 |只看该作者
本帖最后由 wanyuan111 于 2013-10-26 14:09 编辑

回复 9# kiongf
我已经测试了CR0.CD置为1(禁用cache),但是没有效果。目前还不知道原因,只是觉得很匪夷所思。
同时我还使用了MTRRs来设置某一范围的虚拟地址为“uncachable”。但也不能达到屏蔽cache的目的....我会试一试你的修改页表项的方法,有了结果通知你。


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

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP