Chinaunix

标题: x86_64究竟有几个ring? [打印本页]

作者: sanbiangongzi    时间: 2012-05-23 17:49
标题: x86_64究竟有几个ring?

看到很多地方都说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确实是不同的吧?

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

作者: sanbiangongzi    时间: 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 */
复制代码

作者: sanbiangongzi    时间: 2012-05-23 17:51
回复 2# sanbiangongzi


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

Intel(R) Xeon(R) CPU           E5540  @ 2.53GHz 虚拟机
作者: folklore    时间: 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
作者: sanbiangongzi    时间: 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的数量“是出于什么样的背景呢?应该不是乱说吧。
作者: atz0001    时间: 2012-05-24 11:32
本帖最后由 atz0001 于 2012-05-24 11:55 编辑

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

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

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

猜的.




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2