模式位在哪里? [英] Where is the mode bit?

查看:150
本文介绍了模式位在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在Silberschatz,p.的操作系统概念"中阅读了此内容. 18:

I just read this in "Operating System Concepts" from Silberschatz, p. 18:

称为 mode bit 的位已添加到计算机的硬件中 指示当前模式:kernel(0)或user(1).通过模式位, 我们能够区分代表执行的任务 操作系统,以及代表该操作系统执行的操作系统 用户.

A bit, called the mode bit, is added to the hardware of the computer to indicate the current mode: kernel(0) or user(1). With the mode bit, we are able to distinguish between a task that is executed on behalf of the operating system and one that is executed on behalf of the user.

模式位存储在哪里?

(这是CPU中的寄存器吗?您可以读取模式位吗?据我所知,CPU必须能够读取模式位.它如何知道哪个程序将模式位0设为0?具有特殊地址的程序获取模式位0?谁设置模式位/如何设置?)

(Is it a register in the CPU? Can you read the mode bit? As far as I understand it, the CPU has to be able to read the mode bit. How does it know which program gets mode bit 0? Do programs with a special adress get mode bit 0? Who does set the mode bit / how is it set?)

推荐答案

请注意,您的问题在很大程度上取决于CPU的版本.尽管这种情况很少见,但您可能会遇到某些处理器,其中甚至没有这种用户级别/内核级别的概念.

Please note that your question depends highly on the CPU itselt; though it's uncommon you might come across certain processors where this concept of user-level/kernel-level does not even exist.

cs寄存器还有另一个重要功能:它包含一个2位 字段,用于指定CPU的当前特权级别(CPL).这 值0表示最高特权级别,而值3表示 最低的. Linux仅使用级别0和3,分别是 称为内核模式和用户模式.

The cs register has another important function: it includes a 2-bit field that specifies the Current Privilege Level (CPL) of the CPU. The value 0 denotes the highest privilege level, while the value 3 denotes the lowest one. Linux uses only levels 0 and 3, which are respectively called Kernel Mode and User Mode.

(摘自了解Linux内核3e"的2.2.1节)
另外请注意,您可以清楚地看到,这取决于CPU,并且会从一个CPU变为另一个CPU,但是通常情况下,这个概念仍然有效.

(Taken from "Understanding the Linux Kernel 3e", section 2.2.1)
Also note, this depends on the CPU as you can clearly see and it'll change from one to another but the concept, generally, holds.


谁设定的?通常,内核/cpu和用户进程无法更改它,但让我在这里进行解释.

这是一个过分的简化,请不要原谅
假设已经加载了内核,并且第一个应用程序刚刚启动(第一个外壳程序),内核加载了该应用程序要启动的所有内容,将cs寄存器中的位置1(如果您正在运行x86),然后跳转到代码外壳程序的过程.


Who sets it? Typically, the kernel/cpu and a user-process cannot change it but let me explain something here.

This is an over-simplification, do not take it as it is
Let's assume that the kernel is loaded and the first application has just started(the first shell), the kernel loads everything for this application to start, sets the bit in the cs register(if you are running x86) and then jumps to the code of the Shell process.

在这种情况下,shell将继续执行其所有指令,如果进程包含某些特权指令,则cpu将获取该指令并将不执行该指令;它将给出一个异常(硬件异常),该异常告诉内核有人试图执行特权指令,并且此处内核代码处理该作业(CPU将cs设置为内核模式并跳转到某个已知位置以处理此类错误) (也许终止该过程,也许还有其他事情).

The shell will continue to execute all of its instructions in this context, if the process contains some privileged instruction, the cpu will fetch it and won't execute it; it'll give an exception(hardware exception) that tells the kernel someone tried to execute a privileged instruction and here the kernel code handles the job(CPU sets the cs to kernel mode and jumps to some known-location to handle this type of errors(maybe terminating the process, maybe something else).

那么一个进程如何做一些特权呢?例如与某个设备通话? 系统调用到了;内核将为您完成这项工作.

So how can a process do something privileged? Talking to a certain device for instance? Here comes the System Calls; the kernel will do this job for you.

会发生以下情况:
您可以在某些寄存器(内核文档会告诉您)中将所需的内容设置在某个位置(例如,您要设置要访问的文件,文件位置为x,您要进行读取等).然后(在x86上)您将调用int0x80指令.

What happens is the following:
You set what you want in a certain place(For instance you set that you want to access a file, the file location is x, you are accessing for reading etc) in some registers(the kernel documentation will let you know about this) and then(on x86) you will call int0x80 instruction.

这会中断CPU,停止您的工作,将模式设置为内核模式,将IP寄存器跳转到某个已知位置,该位置具有用于处理文件IO请求并从那里移出的代码.
数据准备好后,内核会将这些数据设置在您可以访问的位置(内存位置,注册;这取决于CPU/内核/您所请求的内容),将cs标志设置为用户模式,然后跳回到您的指令位于int 0x80指令旁边.

This interrupts the CPU, stops your work, sets the mode to kernel mode, jumps the IP register to some known-location that has the code which serves file-IO requests and moves from there.
Once your data is ready, the kernel will set this data in a place you can access(memory location, register; it depends on the CPU/Kernel/what you requested), sets the the cs flag to user-mode and jumps back to your instruction next to the it int 0x80 instruction.

最后,只要发生切换,就会发生这种情况,内核会得到通知,因此CPU终止了您当前的指令,更改了CPU状态,并跳转到处理该事件的代码处.粗略地说,上面解释的过程适用于内核模式和用户模式之间的切换.

Finally, this happens whenever a switch happens, the kernel gets notified something happened so the CPU terminates your current instruction, changes the CPU status and jumps to where the code that handles this thing; the process explained above, roughly speaking, applies to how a switch between kernel mode and user-mode happens.

这篇关于模式位在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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