310 likes | 360 Views
제 03 강 : Introduction to Kernel. UNIX Kernel. Christian, The UNIX Operating System, 2nd Ed, Wiley. Silberschatz, Galvin & Gagne, Applied Operating System Concepts, John Wiley & Sons Inc. Kernel memory resident part of UNIX majority written in C
E N D
제03강 : Introduction to Kernel UNIX Kernel Christian, The UNIX Operating System, 2nd Ed, Wiley. Silberschatz, Galvin & Gagne, Applied Operating System Concepts, John Wiley & Sons Inc.
Kernel • memory resident part of UNIX • majority written in C rest in assembler language (HW dependent, speed) • a.out (a plain C a.out program) • consists of functions • other programs can call (some of) these functions • called “system call function” • 3 parts • process management • file system • I/O system
1. Instruction 2. function 3. process (a.out) Kernel is the first a.out to be loaded into memory kernel a.out
1. Instruction 2. function 3. process (a.out) sh a.out Terminal Power-on Kernel loads sh a.out for this terminal as its child Now two processes are active kernel a.out
Multi-user System -- multiple shells User A: sh a.out User B: sh a.out Second terminal -- power on Kernel creates sh for this terminal ------ Three processes active now Only one CPU though. Who’s running on CPU NOW? kernel sh sh kernel a.out
Shell creates a child process and waits for it: sh a.out A: <sleep> vi a.out User B: sh a.out User types a command (a.out) on terminal B Sh creates command process as its child <sleep> creates <run> csh csh a.out kernel a.out
P2 P1 P2’s Data P1’s Data • Multi-user system -- Protection • what if process PA (bug or virus, …) illegally accesses PB ’s information? (read/write) • Shall we detect it and recover from it (after 抹消)? • No we should prevent it (豫防before happening). • Private Information -- stored in • memory • disk Access to these should be 事前 統制
Protection Between Process Domain “domain” : {memory, files, CRT, ...} Allow sh to do disk I/O? Allow any a.out to do disk I/O? How can you trust them (Allow them to do disk I/O instruction?) No I/O instructions allowed except kernel Must ask kernel to do disk I/O via system call kernel checks access right first then do I/O for them sh a.out csh a.out kernel a.out
1. Instruction 2. function 3. process (a.out) --> “domain” : memory, files, CRT, ... I/O not allowed here So ask kernel other a.out System calls (依賴) Kernel has functions To do I/O for you kernel a.out (代行)
CPU Memory 1. Address (instruction) MAR PC Control Unit ALU unit MBR IR 2. Return Content R0 ALU ... R7 mode
CPU Memory 4. Address (operand) MAR PC Control Unit ALU unit IR MBR 3. Op-code i R0 j ... ALU R7 op-code operands mode bit i j add
CPU “mode bit (CPU-現在役割-bit)” CPU mode memory op-code kernelaccess any(全域) execute any (全體 權限) user mine only (局地) restricted * (部分 權限) No I/O instruction * privileged op-code No special-register access any thing that can harm others (特殊 命令語?) (惡影響)
instruction memory [Kernel mode] Add Sub Disk write Disk read Tape write Disable interrupt Enable interrupt [User mode] Add Sub Disk write Disk read Tape write Disable interrupt Enable interrupt a.out
CPU_mode_bit : • SW need HW’s help to “prevents” illegal action • one HW bit in CPU (usually part of PSW) • machine instruction (SW) can read/write this • Access to mode_bit is privileged op-code Machine Instruction Cycle: “CPU-現在役割-bit” < if mode_bit = user > PC to memory Instruction Fetch Decode (解讀) Execute Increment PC Monitor(檢閱) addressto memory. Stop this if address is outside a.out scope while CPU is in user_mode (trap) CPU: Monitor (檢閱)op-code Stop this if privileged op-code is attempted while CPU is in user_mode (trap)
CPU Memory Control Unit ALU unit MAR PC MBR IR R0 ALU ... R7 mode “CPU-現在役割-bit”
When my program runs, CPU mode bit = user_mode • cannot execute I/O instruction • cannot access special registers • cannot access memory outside current a.out • When OS kernel runs, CPU mode bit = kernel_mode • no restriction at all …. Wait a minute. …. My program runs in user_mode read(); write(); How did my program handle it?
My program Source: “read next byte from disk file X into my variable Y” • Binary:“prepare all parameters (for disk read)” “execute chmodk” instruction” /* Yes, disk I/O instructions (read, write functions) are not included in my a.out Instead they are kept in kernel My a.out has to call those functions at run time This is all done by (compiler, OS and HW) */ Kernel a.out read: write: My a.out read() write() invoke I/O invoke I/O
At run time (part I: hardware) • my program executes “chmodk” • this is privileged instruction • CPU cannot continue (in user_mode) • HW trap • HW saves CPU state vector (including return address) • HW sets CPU_mode_bit <== kernel mode • HW jumps to trap handling routine (in kernel a.out) My a.out (read para) chmodk Kernel a.out trap: read: trap To kernel_mode I/O “CPU-現在役割-bit”
Run time (part I: software) • Now trap handler (in kernel a.out) starts • inspects what caused trap • system call, divide by zero, memory bound, ….? • invoke appropriate kernel function (system call) • All done? …. CPU_mode <== user_mode • restore state vector • return to interrupted location in “my a.out” My a.out (read para) chmodk Kernel a.out trap: read: trap Kernel read( ) checks permission first user_mode I/O “CPU-現在役割-bit”
Main Memory (1) P1 is executing in user mode” P1 invokes a kernel function (system call) P1 a.out P2 a.out P3 a.out (2) causes HW trap (in kernel mode) call appropriate kernel function kernel returns to P1’s a.out kernel a.out
Main Memory We say “P1 is running in user mode” calling functions in P1 a.out using the stack in P1.a.out P1 a.out P2 a.out P3 a.out We say “P1 is running in kernel mode” calling functions in kernel a.out using the stack in kernel a.out kernel a.out
Main Memory User stack Kernel stack P1 a.out P2 a.out User stack Kernel stack P3 a.out kernel a.out
Program execution Kernel a.out P1 a.out P1 a.out user mode kernel mode user mode kernel mode CPU mode 現在役割-bit My own code Kernel Service My own code System call Return from Kernel To my a.out System call Program begins Program ends
OS Kernel (plain C program with variables and functions) Process 1 Process 2 Process 3 PCB PCB PCB CPU mem disk tty CPU mem disk tty
User a.out Kernel a.out Process 1 Process 2 Process 3 PCB PCB PCB CPU mem disk tty Hardware CPU mem disk tty
memory [User mode] [Kernel mode] a.out • Kernel a.out can access any memory location • That includes your program’s main( ), stack. • Kernel can push any values into your stack • They become parameters to main( ) • Examples --- main(argv, envp) • Kernel can call any function in memory • Kernel sets CPU user mode calls your main( )
Second shell runs: User A: sh a.out <sleep> <run> cat cat ch1 ch2 > ch12 kernel sh sh vi cat User B: sh a.out kernel a.out
main() printf() -- output to screen {scanf( ) scanf() – read from keyboard $PATH, search files load cat (a.out) file initialize it put it into ready queue wait system call(sleep) printf(prompt) go to top Read from the standard input file cat ch1 ch2 > ch12 sh (B) when child finish
main() printf() -- output to screen {scanf( ) scanf() – read from keyboard $PATH, search files takes 1st word (which is cat) load cat (a.out) file from disk (command - /bin) initialize it push (env, arg) to stack of cat put it into ready queue cat is now in ready-queue wait system call(sleep) sh goes to sleep – cat runs printf(prompt) go to top child } Read from the standard input file cat ch1 ch2 > ch12 sh (B) when child finish
OS Kernel (plain C program with variables and functions) Process 4 Process 5 Process 1 Process 2 Process 3 PCB PCB PCB PCB PCB ready queue disk I/O queue CPU mem disk tty CPU mem disk tty
main() {scanf(eg cat ch1 ch2 > ch12) $PATH, search files load a.out file intialize stack (argv, argc, envp) put it into ready queue sleep (wait system call) printf(prompt) go to top } main(argv, argc, envp) { any program } /* CPU goes back to kernel kernel wakes up parent parent proceeds */ cat ch1 ch2 > ch12 Passed via Stack sh child