Xv6 Operating System Organization
作者:互联网
Operating System Organization
参考 xv6-riscv-book Chapter 2 Operating system organization
文章目录
Three requirements for OS:
- multiplexing
- isolation
- interaction
Abstracting physical resources
Transparency: simplifies interaction
- Abstract the resources into services
- Applications don’t have to be aware of time sharing
- Allows the OS to decide the usage of memory
User mode, supervisor mode, and system calls
Kernel organization
Organization | Description | Upside | Downside |
---|---|---|---|
monolithic kernel | the entire OS resides in the kernel with full privilege | convenient, easier for different parts of the OS to cooperate | complex interfaces, easy to make a mistake, a mistake is fatal |
microkernel | minimize the code that runs in supervisor mode, execute the bulk of the OS in user mode | reduce the risk of mistakes in the kernel |
(NOTE> OS services running as processes are called servers)
Xv6 organization
Xv6 kernel source files:
File | Description |
---|---|
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. |
(From: pbpaste | awk '{ printf("| %s |", $1); for (i=2; i<=NF; i++) printf(" %s", $i); printf(" |\n"); }' | pbcopy
)
The inter-module interfaces are defined in kernel/defs.h.
Process overview
Process
The unit of isolation: a process: an illusion to a program that it has its own private machine (private memory, CPU, file descriptors, etc.). Process is defined as struct proc
(kernel/proc.h:86).
p->state
: whether the process is allocated, ready to run, running, waiting for I/O, or exiting:
enum procstate { UNUSED, USED, SLEEPING, RUNNABLE, RUNNING, ZOMBIE };
p->pagetable
: holds the process’s page table.
Thread
Thread (of execution): executes the process’s instructions.
A thread can be suspended and later resumed.
Threads can “block” in the kernel to wait for I/O, and resume where it left off when the I/O has finished.
Virtual address
Virtual address: Isolation of memory: virtual address -- page tables --> physical address
:
MAXVA
↑ trampoline
| trapframe
| Heap
| Stack (user stack)
| Global Variables (text and data)
| Instructions
0
- VA is starting at zero
- MAXVA (the maximum virtual address) defined in kernel/riscv.h:348: Xv6 uses 38 bits to look up virtual addresses in page tables: MAXVA = 2 38 − 1 = 0x3fffffffff \textrm{MAXVA}=2^{38}-1=\textrm{0x3fffffffff} MAXVA=238−1=0x3fffffffff
- Each process has two stacks: user stack & kernel stack (
p->kstack
, for a system call or interrupt, separate and protected from user code).
System call
ecall
: a RISC-V instruction to make a system call:
- raises hardware privilege level
- change PC to a kernel-defined entry point, switches to a kernel stack
- executes the kernel instructions
- (system call completes) switches back to the user stack
- returns to user space by calling the
sret
instruction (lowers the hardware privilege level) - resumes executing user instructions just after the system call instruction
Starting xv6 and the first process
- RISC-V computer powers on, self initializes
- runs a boot loader (stored in ROM): loads the xv6 kernel into memory at physical address 0x80000000 (range 0x0:0x80000000 contains I/O devices)
- (in machine mode) executes xv6 starting at
_entry
(kernel/entry.S:6), sets up a stack (stack0
) for C code (sp = stack0 + (hartid * 4096)
) _entry
calls into C code: functionstart
(kernel/start.c:11)start
performs configuration(page-table, interrupts…)- switches to supervisor mode, PC change to
main
(kernel/main.c:11) main
initializes several devices and subsystems- creates the first process by calling userinit (kernel/proc.c:212)
- run
initcode.S
(user/initcode.S:1), doexec("/init")
init
(user/init.c:15) creates a console device file, opens it as file descriptors 0, 1, and 2- starts a shell on the console
- The system is up.
EOF
// By CDFMLR 2021-02-27
printf("See you.\n");
顶部图片来自于小歪API,系随机选取的图片,仅用于检测屏幕显示的机械、光电性能,与文章的任何内容及观点无关,也并不代表本人局部或全部同意、支持或者反对其中的任何内容及观点。如有侵权,联系删除。
标签:kernel,process,system,System,Operating,user,address,Organization,mode 来源: https://blog.csdn.net/u012419550/article/details/114192498