免费注册 查看新帖 |

Chinaunix

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

x86_64究竟有几个ring? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-05-23 17:49 |只看该作者 |倒序浏览

看到很多地方都说x86_64只有两个ring:ring 0和ring 3
比如:
1、 http://wiki.osdev.org/Security 提到"However, many architectures have only two rings (e.g. x86_64), corresponding to ring 0 and 3 in this description."


2、Prentice.Hall.The.Definitive.Guide.to.the.Xen.Hypervisor.Nov.2007.pdf 一本讲虚拟化的书
     1.7.1 The Hypervisor, the OS, and the Applications 提到 When AMD tidied up the IA32 architecture as part of the process of creating x86-64, one of the things it did was reduce the number of rings.


但是我翻遍了 《Intel 64 and IA_32 Architectures Software Developer's Manual》也没有找到说x86_64只有2个ring的地方

于是自己写了一段代码:
对 arch/x86/boot/compressed/head_64.S 做了修改

选择这个文件没有别的意思,只是因为bzImage内核启动过程中,在这个文件中,进入了long mode。

在刚进入(ring0)long mode之后,利用iret依次切换到ring1 ring2 ring3 然后在ring3中死循环。

在每一个ring中在屏幕上输出一个表示当前ring number的数字,
(在物理机DELL R710和KVM虚拟机中验证的)结果表明,输出了0123

这应该改说明x86_64是有4个ring吧?

另外还测试了不同ring的代码对不同ring的数据访问能力,如下表
  1.             data level   0    1     2    3
  2. code level
  3. 0                        Y    Y     Y    Y
  4. 1                        N    Y     Y    Y
  5. 2                        N    N     Y    Y
  6. 3                        N    N     N    Y
复制代码
这应该说明,四个ring确实是不同的吧?

有没有懂这方面的兄弟,科普一下!

论坛徽章:
0
2 [报告]
发表于 2012-05-23 17:50 |只看该作者
回复 1# sanbiangongzi


   
对arch/x86/boot/compressed/head_64.S 修改内容如下:
  1. 140c140,141
  2. <       movl    $0x00000183, %eax
  3. ---
  4. > /*    movl    $0x00000183, %eax */
  5. >       movl    $0x00000187, %eax /* assign RING3 access right */
  6. 214a216,323
  7. > #define __KERNEL_CS_RING1  (6 * 8 + 1)
  8. > #define __KERNEL_DS_RING1  (7 * 8 + 1)
  9. > #define __KERNEL_CS_RING2  (8 * 8 + 2)
  10. > #define __KERNEL_DS_RING2  (9 * 8 + 2)
  11. > #define __KERNEL_CS_RING3  (10 * 8 + 3)
  12. > #define __KERNEL_DS_RING3  (11 * 8 + 3)
  13. >
  14. > .global ring_test
  15. > ring_test:
  16. >       # ring 0 print
  17. >       movw $(0x0500 + '0'), %ax
  18. >       movw %ax, 0xb80a0
  19. >
  20. >
  21. >       # to higher ring
  22. >       push    $__KERNEL_DS_RING1
  23. >       push    %rsp
  24. >         pushf
  25. >         pop     %rax
  26. >         or      $0x1000, %rax
  27. >         push    %rax
  28. >       push    $__KERNEL_CS_RING1
  29. >       lea     1f(%rbp), %rax
  30. >       push    %rax
  31. >       iretq
  32. > 1:
  33. >       xorl    %eax, %eax
  34. >       movl    $__KERNEL_DS_RING1, %eax
  35. >       movl    %eax, %ds
  36. >       # print
  37. >       movw $(0x0500 + '1'), %ax
  38. >       movw %ax, 0xb80a0 + 2 * 1
  39. >
  40. >       /*
  41. >       movl $__KERNEL_DS, %eax
  42. >       movl %eax, %es
  43. >       movl %es:(0x100000), %eax
  44. >       */
  45. >
  46. >       movl $__KERNEL_DS_RING2, %eax
  47. >       movl %eax, %es
  48. >       movl %es:(0x100000), %eax
  49. >
  50. >       movl $__KERNEL_DS_RING3, %eax
  51. >       movl %eax, %es
  52. >       movl %es:(0x100000), %eax
  53. >
  54. >       # to higher ring
  55. >       push    $__KERNEL_DS_RING2
  56. >       push    %rsp
  57. >         pushf
  58. >         pop     %rax
  59. >         or      $0x2000, %rax
  60. >         push    %rax
  61. >       push    $__KERNEL_CS_RING2
  62. >       lea     1f(%rbp), %rax
  63. >       push    %rax
  64. >       iretq
  65. > 1:
  66. >       xorl    %eax, %eax
  67. >       movl    $__KERNEL_DS_RING2, %eax
  68. >       movl    %eax, %ds
  69. >       # print
  70. >       movw $(0x0500 + '2'), %ax
  71. >       movw %ax, 0xb80a0 + 2 * 2
  72. >
  73. >       /*
  74. >       movl $__KERNEL_DS_RING1, %eax
  75. >       movl %eax, %es
  76. >       movl %es:(0x100000), %eax
  77. >       */
  78. >
  79. >       movl $__KERNEL_DS_RING3, %eax
  80. >       movl %eax, %es
  81. >       movl %es:(0x100000), %eax
  82. >
  83. >       # to higher ring
  84. >       push    $__KERNEL_DS_RING3
  85. >       push    %rsp
  86. >         pushf
  87. >         pop     %rax
  88. >         or      $0x3000, %rax
  89. >         push    %rax
  90. >       push    $__KERNEL_CS_RING3
  91. >       lea     1f(%rbp), %rax
  92. >       push    %rax
  93. >       iretq
  94. > 1:
  95. >       xorl    %eax, %eax
  96. >       movl    $__KERNEL_DS_RING3, %eax
  97. >       movl    %eax, %ds
  98. >       # print
  99. >       movw $(0x0500 + '3'), %ax
  100. >       movw %ax, 0xb80a0 + 2 * 3
  101. >
  102. >       /*
  103. >       movl $__KERNEL_DS_RING1, %eax
  104. >       movl %eax, %es
  105. >       movl %es:(0x100000), %eax
  106. >
  107. >       movl $__KERNEL_DS_RING2, %eax
  108. >       movl %eax, %es
  109. >       movl %es:(0x100000), %eax
  110. >       */
  111. >
  112. >       jmp .
  113. >
  114. >
  115. 299a409,410
  116. >
  117. >         
  118. 309a421,427
  119. >
  120. >       .quad   0x00afba000000ffff      /* __KERNEL_CS_RING1 */
  121. >       .quad   0x00cfb2000000ffff      /* __KERNEL_DS_RING1 */
  122. >       .quad   0x00afda000000ffff      /* __KERNEL_CS_RING2 */
  123. >       .quad   0x00cfd2000000ffff      /* __KERNEL_DS_RING2 */
  124. >       .quad   0x00affa000000ffff      /* __KERNEL_CS_RING3 */
  125. >       .quad   0x00cff2000000ffff      /* __KERNEL_DS_RING3 */
复制代码

论坛徽章:
0
3 [报告]
发表于 2012-05-23 17:51 |只看该作者
回复 2# sanbiangongzi


    忘了说两次测试 CPU分别是
Intel(R) Xeon(R) CPU           E5506  @ 2.13GHz 物理机

Intel(R) Xeon(R) CPU           E5540  @ 2.53GHz 虚拟机

论坛徽章:
59
2015年亚洲杯之约旦
日期:2015-01-27 21:27:392015年亚洲杯之日本
日期:2015-02-06 22:09:41拜羊年徽章
日期:2015-03-03 16:15:432015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015元宵节徽章
日期:2015-03-06 15:50:392015年亚洲杯之阿联酋
日期:2015-03-19 17:39:302015年亚洲杯之中国
日期:2015-03-23 18:52:23巳蛇
日期:2014-12-14 22:44:03双子座
日期:2014-12-10 21:39:16处女座
日期:2014-12-02 08:03:17天蝎座
日期:2014-07-21 19:08:47
4 [报告]
发表于 2012-05-23 20:47 |只看该作者
However, many architectures have only two rings (e.g. x86_64), corresponding to ring 0 and 3 in this description."


However, many architectures have only two rings (e.g. x86_64), corresponding to ring 0 and 3 in this description."
x86 has ring 0,1,2,3, but for portable, linux (windows too) just use two: ring 0 & ring 3.

good luck

论坛徽章:
0
5 [报告]
发表于 2012-05-24 09:06 |只看该作者
回复 4# folklore


    However, many architectures have only two rings (e.g. x86_64), corresponding to ring 0 and 3 in this description.
x86 has ring 0,1,2,3, but for portable, linux (windows too) just use two: ring 0 & ring 3.



谢谢这位兄台的回复,但是好像还是没有弄明白,我想知道的CPU究竟总共有几个RING,这与操作系统使用了其中的几个RING是,两个不同的问题。


上面提到的文档是说,x86_64减少了ring的数量,但是我的测试代码,证实了ring1/2的存在

我想知道,这些文档提到”x86_64减少了ring的数量“是出于什么样的背景呢?应该不是乱说吧。

论坛徽章:
0
6 [报告]
发表于 2012-05-24 11:32 |只看该作者
本帖最后由 atz0001 于 2012-05-24 11:55 编辑

老大,当然以 Intel 的手册为准啦,何况你在真实的 CPU 上验证过。

我看你水平很高啊,至少比我高很多。自信点。

又看了下手册,感觉 ring/Privilege-level 这个概念是 Segment protection 的,
现在推荐用  page protection 的,那么 ring 这个概念基本不相干了.

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

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP