Day-18 Kernel Mode

Kernel Mode

tags: IT铁人

kernel mode & user mode

为了安全的管理不同的user process,保护他们之间不会互相干扰,我们需要不同的模式来管理,就像是上下层一样,分为系统的kernel mode以及使用者的user mode。

简单来说kernel mode负责分配谁能使用哪些资源,现在轮到谁来执行,以及一些重要或是可能危害系统的指令都会交给kernel mode处理。

mode 差别
kernel mode 又叫System/Privileged/Supervisor mode,代表kernel取得对系统的控制权。(即kernel取得CPU在执行一些system process)在此mode下,可以执行特权指令(Privileged Instruction)。
user mode 代表user process取得CPU在执行之mode,在此mode下,不允许执行特权指令。

此外,Dual mode之实现需要硬体额外支援。例如提供一个mode bit决定现在是哪一种mode。

Privileged Instruction

任何可能造成系统重大危害的指令都可以称为Privileged Instruction。只能在kernel mode下执行,不允许在user mode下执行。

一般而言,下列这些指令是Privileged Instruction:

  • I/O instruction(I/O protection)
  • Base/limit register值修改(Memory protection)
  • Time值修改(CPU protection)
  • Turn off interrupt
  • Switch mode to kernel mode
  • Clear Memory

如果没有区分Privileged Instruction的话,User process可能可以随意修改Memory的值,造成程序危害;让自己可以永远占用CPU;随意写入档案;不接受任何原先预计要对该process产生影响的interrupt。

Protection

以下会介绍前面提到的各种protection,并叙述该protection的过程:

I/O Protection

为了防止user process执行I/O指令操控I/O Devices,降低出错机率及使用复杂度。

将I/O设为Privileged Instruction。

步骤 内容
1 User process发出system call通知需要执行I/O Instruction。
2 OS trap进入kernel mode。
3 I/O subsystem执行I/O指令。
4 I/O结果回传给I/O subsystem。
5 I/O subsystem回传给user process并切换回user mode。

Memory Protection

为了防止user process任意存取其他process及kernel所在的记忆体区域。

假设Memory管理采用Contiguous Allocation的方法,则利用Base register及Limit register管理process能使用的记忆体区间。

步骤 内容
1 存取Memory时,将位址传给OS trap进入的kernel mode。
2 计算位址是否大於等於Base且小於Base+Limit。
3 如果结果为true则回传legal同意存取;反之则回传illegal不同意存取。

CPU Protection

为了防止user process长期/无限期占用CPU而不释放。

利用Timer,OS会规定一个process使用CPU之MAX-Time-Quantum,当process取得CPU後,Time值则设为Max-Time-Quantum,随着process使用CPU的时间增加,Timer值会逐步递减,当Time值=0时,Timer会发出"Time-Out" Interrupt通知OS,OS会强迫此process放掉CPU。

上一篇 下一篇
I/O运作 System Call & OS架构


<<:  [Day29]C# 鸡础观念- 物件导向(oop)~介面(Interface)

>>:  Day 18 一切都不是理所当然的!

06. DB x tinker x seeder

连线设定其实也没什麽好介绍的,改 env 这种事 sail 已经弄好了。 即便现在,用 compos...

Day 25:[Android] 将 LiveData 用 Flow 替代吧

LiveData 是 Android 中一个很有用的项目,它是一种可观察(Observe)的资料存储...

Vue.js 从零开始:元件

Vue 元件概念 Component元件,作为SPA的灵魂功能,可以将程序码以及模组封装起来,增加程...

Day4-"回圈"

回圈结构是高阶语言其中一项重要的发展,回圈事实上结合了低阶语言的部分功能,使得程序部分的叙述区块能够...

Day 10 Odoo Action

Odoo模组开发实战 目录 Action 1.1 Window Actions (ir.action...