Skip to content

OS organization

约 1451 个字 27 行代码 预计阅读时间 5 分钟

user/kernel mode virtual memory

为了支持user/kernel mode,处理器会有两种操作模式,第一种是user mode,第二种是kernel mode。当运行在kernel mode时,CPU可以运行特定权限的指令(privileged instructions);当运行在user mode时,CPU只能运行普 通权限的指令(unprivileged instructions)。

privileged instructions are instructions that basically are involved in manipulating the hardware directly.

page table 将虚拟内存地址与物理内存地址做了对应。每一个进程都会有自己独立的 page table,这样的话,每一个进程只能访问出现在自己 page table中的物理内存。操作系统会设置 page table,使得每一个进程都有不重合的物理内存,这样一个进程就不能访问其他进程的物理内存,因为其他进程的物理内存都不在它的 page table中。一个进程甚至都不能随意编造一个内存地址,然后通过这个内存地址来访问其他进程的物理内存。

有一种方式能够让应用程序可以将控制权转移给内核(Entering Kernel)。在RISC-V中,有一个专门的指令用来实现这个功能,叫做 ECALLECALL 接收一个数字参数,当一个用户程序想要将程序执行的控制权转移到内核,它只需要执行ECALL 指令,并传入一个数字。这里的数字参数代表了应用程序想要调用的 System Call

  • 当它在用户空间执行 fork 时,它并不是直接调用操作系统中对应的函数,而是调用 ECALL 指令,并将 fork 对应的数字作为参数传给 ECALL。之后再通过 ECALL 跳转到内核。

XV6 Manual

An operating system must fulfill three requirements: multiplexing, isolation, and interaction.

RISC-V is a 64-bit CPU, and xv6 is written in “LP64” C, which means long (L) and pointers (P) in the C programming language are 64 bits(八个字节), but int is 32-bit(三个字节).

Xv6 is written for the support hardware simulated by qemu’s “-machine virt” option. This includes RAM, a ROM containing boot code, a serial connection to the user’s keyboard/screen, and a disk for storage.

Abstract physical resources

To achieve strong isolation it’s helpful to forbid applications from directly accessing sensitive hardware resources, and instead to abstract the resources into services.

Unix transparently switches hardware CPUs among processes, saving and restoring register state as necessary, so that applications don’t have to be aware of time sharing. This transparency allows the operating system to share CPUs even if some applications are in infinite loops.

User/Kernel mode

CPUs provide hardware support for strong isolation.

For example, RISC-V has three modes in which the CPU can execute instructions: machine mode, supervisor mode, and user mode.

  • Instructions executing in machine mode have full privilege;
  • a CPU starts in machine mode.
  • Machine mode is mostly intended for configuring a computer.

Xv6 executes a few lines in machine mode and then changes to supervisor mode.

在 supervisor mode 下,CPU被允许执行 privileged instructions :例如,启用和禁用中断,读取和写保存页表地址的寄存器,等等。如果 user mode 下的应用程序试图执行 privileged instructions,那么CPU不会执行指令,而是切换到 supervisor mode,以便 supervisor-mode code 可以终止应用程序,因为它做了它不应该做的事情。

An application can execute only user-mode instructions and is said to be running in user space.

The software in supervisor mode can execute privileged instructions and is said to be running in kernel space.

The software running in kernel space (or in supervisor mode) is called the kernel.

CPU提供了一个特殊的指令,它将CPU从 user mode 切换到 supervisor mode,并在内核指定的入口点进入内核。(RISC-V provides the ecall instruction for this purpose.)

一旦CPU切换到 supervisor mode,内核就可以验证系统调用的参数(例如,检查传递给系统调用的地址是否是应用程序内存的一部分),决定是否允许应用程序执行请求的操作(例如,检查是否允许应用程序写入指定的文件),然后拒绝或执行它。

XV6 organization

bio.c: Disk block cache for the file system.
console.c: Connect to the user keyboard and screen.
entry.S: Very first boot instructions.
exec.c: exec() system call.
file.c: File descriptor support.
fs.c: File system.
kalloc.c: Physical page allocator.
kernelvec.S: Handle traps from kernel, and timer interrupts.
log.c: File system logging and crash recovery.
main.c: Control initialization of other modules during boot.
pipe.c: Pipes.
plic.c: RISC-V interrupt controller.
printf.c: Formatted output to the console.
proc.c: Processes and scheduling.
sleeplock.c: Locks that yield the CPU.
spinlock.c: Locks that don’t yield the CPU.
start.c: Early machine-mode boot code.
string.c: C string and byte-array library.
swtch.S Thread switching.
syscall.c: Dispatch system calls to handling function.
sysfile.c: File-related system calls.
sysproc.c: Process-related system calls.
trampoline.S: Assembly code to switch between user and kernel.
trap.c: C code to handle and return from traps and interrupts.
uart.c: Serial-port console device driver.
virtio_disk.c: Disk device driver.
vm.c: Manage page tables and address spaces

Process

进程是一个抽象概念,它让一个程序可以假设它独占一台机器。进程向程序提供“看上去”私有的,其他进程无法读写的内存系统(或地址空间),以及一颗“看上去”仅执行该程序的CPU。

在 xv6 中,trampoline 主要是指用于处理用户态和内核态之间切换的汇编代码,而 trapframe 是一个用于保存处理器状态的数据结构,用于在中断或异常处理过程中保存和恢复相关的执行状态。

  • Xv6 uses these two pages to transition into the kernel and back;
  • the trampoline page contains the code to transition in and out of the kernel
  • the trapframe is necessary to save/restore the state of the user process,

xv6 使用结构体 struct proc 来维护一个进程的状态,其中最为重要的状态是 its page table, its kernel stack, and its run state .

We’ll use the notation p->xxx to refer to elements of the proc structure; for example, p->pagetable is a pointer to the process’s page table.

Each process has a thread of execution (or thread for short) that executes the process’s instructions.

A thread can be suspended(挂起) and later resumed(恢复). To switch transparently between processes, the kernel suspends the currently running thread and resumes another process’s thread.

Alt text

A process can make a system call by executing the RISC-V ecall(environment call) instruction. This instruction raises the hardware privilege level and changes the program counter to a kernel-defined entry point.

The code at the entry point switches to a kernel stack and executes the kernel instructions that implement the system call.

When the system call completes, the kernel switches back to the user stack and returns to user space by calling the sret(system return) instruction, which lowers the hardware privilege level and resumes executing user instructions just after the system call instruction.

A process’s thread can “block” in the kernel to wait for I/O, and resume where it left off when the I/O has finished.

p->state indicates whether the process is allocated, ready to run, running, waiting for I/O, or exiting.

p->pagetable holds the process’s page table, in the format that the RISC-V hardware expects. A process’s page table also serves as the record of the addresses of the physical pages allocated to store the process’s memory(也记录了它的物理地址).

In summary, a process bundles two design ideas: an address space to give a process the illusion of its own memory, and, a thread, to give the process the illusion of its own CPU.


Last update: January 17, 2024
Created: January 17, 2024