在操作系统中,Ring 0和Ring 3是什么? [英] What are Ring 0 and Ring 3 in the context of operating systems?

查看:341
本文介绍了在操作系统中,Ring 0和Ring 3是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在学习有关Windows驱动程序开发的基础知识,我不断找到术语 Ring 0 Ring 3 .这些指的是什么?它们是否与内核模式用户模式相同?

I've been learning basics about driver development in Windows I keep finding the terms Ring 0 and Ring 3. What do these refer to? Are they the same thing as kernel mode and user mode?

推荐答案

Linux x86环使用概述

了解环在Linux中的用法将使您对环的用途有所了解.

Understanding how rings are used in Linux will give you a good idea of what they are designed for.

在x86保护模式下,CPU始终处于4个振铃之一. Linux内核仅使用0和3:

In x86 protected mode, the CPU is always in one of 4 rings. The Linux kernel only uses 0 and 3:

  • 0表示内核
  • 3个用户

这是内核vs用户态最硬,最快的定义.

This is the most hard and fast definition of kernel vs userland.

为什么Linux不使用环1和2: CPU特权环:为什么不使用环1和2?

Why Linux does not use rings 1 and 2: CPU Privilege Rings: Why rings 1 and 2 aren't used?

如何确定当前铃声?

当前铃声是通过以下方式选择的:

The current ring is selected by a combination of:

  • 全局描述符表:GDT条目的内存表,每个条目都有一个字段Privl,该字段对环进行编码.

  • global descriptor table: a in-memory table of GDT entries, and each entry has a field Privl which encodes the ring.

LGDT指令将地址设置为当前描述符表.

The LGDT instruction sets the address to the current descriptor table.

另请参见: http://wiki.osdev.org/Global_Descriptor_Table

该段寄存器CS,DS等指向GDT中某个条目的索引.

the segment registers CS, DS, etc., which point to the index of an entry in the GDT.

例如,CS = 0表示GDT的第一个条目当前对于执行代码是活动的.

For example, CS = 0 means the first entry of the GDT is currently active for the executing code.

每个环能做什么?

CPU芯片的物理结构如下:

The CPU chip is physically built so that:

  • 环0可以做任何事情

  • ring 0 can do anything

ring 3不能运行多个指令并写入多个寄存器,最值得注意的是:

ring 3 cannot run several instructions and write to several registers, most notably:

  • 无法更改自己的铃声!否则,它可能会将自己设置为环0,并且环将无用.

  • cannot change its own ring! Otherwise, it could set itself to ring 0 and rings would be useless.

换句话说,不能修改当前段描述符,它确定了当前环.

In other words, cannot modify the current segment descriptor, which determines the current ring.

无法修改页表: x86分页如何工作?

换句话说,不能修改CR3寄存器,分页本身会阻止页表的修改.

In other words, cannot modify the CR3 register, and paging itself prevents modification of the page tables.

这出于安全性/易于编程的原因而阻止一个进程查看其他进程的内存.

This prevents one process from seeing the memory of other processes for security / ease of programming reasons.

无法注册中断处理程序.这些是通过写入内存位置来配置的,这也可以通过分页来防止.

cannot register interrupt handlers. Those are configured by writing to memory locations, which is also prevented by paging.

处理程序在环0中运行,并且会破坏安全模型.

Handlers run in ring 0, and would break the security model.

换句话说,不能使用LGDT和LIDT指令.

In other words, cannot use the LGDT and LIDT instructions.

无法执行inout之类的IO指令,因此可以进行任意硬件访问.

cannot do IO instructions like in and out, and thus have arbitrary hardware accesses.

例如,否则,如果任何程序都可以直接从磁盘读取文件权限,则文件权限将毫无用处.

Otherwise, for example, file permissions would be useless if any program could directly read from disk.

更确切地说,要感谢

More precisely thanks to Michael Petch: it is actually possible for the OS to allow IO instructions on ring 3, this is actually controlled by the Task state segment.

如果环3最初没有它,则不可能授予自己这样做的权限.

What is not possible is for ring 3 to give itself permission to do so if it didn't have it in the first place.

Linux始终禁止使用它.另请参见:为什么Linux不使用通过TSS进行硬件上下文切换?

Linux always disallows it. See also: Why doesn't Linux use the hardware context switch via the TSS?

程序和操作系统如何在环之间转换?

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