CMSC421: Principles of Operating Systems · 5 Useful kernel API functions for bidirectional data...
Transcript of CMSC421: Principles of Operating Systems · 5 Useful kernel API functions for bidirectional data...
![Page 1: CMSC421: Principles of Operating Systems · 5 Useful kernel API functions for bidirectional data movement • access_ok (type, addr, size): type (VERIFY_READ, VERIFY_WRITE) • get_user(x,](https://reader036.fdocuments.us/reader036/viewer/2022070917/5fb772f5ab305e055634c821/html5/thumbnails/1.jpg)
1
CMSC421: Principles of Operating Systems
Nilanjan Banerjee
Principles of Operating Systems
Assistant Professor, University of Maryland
Baltimore County [email protected]
http://www.csee.umbc.edu/~nilanb/teaching/421/
![Page 2: CMSC421: Principles of Operating Systems · 5 Useful kernel API functions for bidirectional data movement • access_ok (type, addr, size): type (VERIFY_READ, VERIFY_WRITE) • get_user(x,](https://reader036.fdocuments.us/reader036/viewer/2022070917/5fb772f5ab305e055634c821/html5/thumbnails/2.jpg)
2
Announcements
• Project 0 and Homework 1 are due this week • Readings from Silberchatz [2nd chapter]
• Section 2.3
![Page 3: CMSC421: Principles of Operating Systems · 5 Useful kernel API functions for bidirectional data movement • access_ok (type, addr, size): type (VERIFY_READ, VERIFY_WRITE) • get_user(x,](https://reader036.fdocuments.us/reader036/viewer/2022070917/5fb772f5ab305e055634c821/html5/thumbnails/3.jpg)
3
Lets write a system call in the kernel (sys_strcpy)
int strcpy(char *src, char *dest, int len)
asmlinkage long sys_strcpy(char *src, char *dest, int len)
compiler directive params will be read from stack
can return values of size of at most long? Why?
![Page 4: CMSC421: Principles of Operating Systems · 5 Useful kernel API functions for bidirectional data movement • access_ok (type, addr, size): type (VERIFY_READ, VERIFY_WRITE) • get_user(x,](https://reader036.fdocuments.us/reader036/viewer/2022070917/5fb772f5ab305e055634c821/html5/thumbnails/4.jpg)
4
Issues to think about when writing system calls
• Moving data between the kernel and user process • Concerns: security and protection
• Synchronization and concurrency (will revisit) • Several (so called) kernel threads might be accessing
the same data structure that you want to read/write • Simple solution (disable interrupts “cli”)
• Usually not a good idea • Big problem in preemptive CPU (which is almost every
CPU) and multi-processor systems • CONFIG_SMP or CONFIG_PREEMPT
![Page 5: CMSC421: Principles of Operating Systems · 5 Useful kernel API functions for bidirectional data movement • access_ok (type, addr, size): type (VERIFY_READ, VERIFY_WRITE) • get_user(x,](https://reader036.fdocuments.us/reader036/viewer/2022070917/5fb772f5ab305e055634c821/html5/thumbnails/5.jpg)
5
Useful kernel API functions for bidirectional data movement
• access_ok (type, addr, size): type (VERIFY_READ, VERIFY_WRITE) • get_user(x, ptr) --- read a char or int from user-space • put_user(x, ptr) --- write variable from kernel to user space • copy_to_user(to, from, n) --- copy data from kernel to userspace • copy_from_user(to, from, n) – copy data to kernel from userspace • strnlen_user(src, n) – checks that the length of a buffer is n • strcpy_from_user(dest, src, n) ---copies from kernel to user space
Acknowledgement: http://www.ibm.com/developerworks/linux/library/l-kernel-memory-access/index.html
![Page 6: CMSC421: Principles of Operating Systems · 5 Useful kernel API functions for bidirectional data movement • access_ok (type, addr, size): type (VERIFY_READ, VERIFY_WRITE) • get_user(x,](https://reader036.fdocuments.us/reader036/viewer/2022070917/5fb772f5ab305e055634c821/html5/thumbnails/6.jpg)
6
Linux Bootup process
![Page 7: CMSC421: Principles of Operating Systems · 5 Useful kernel API functions for bidirectional data movement • access_ok (type, addr, size): type (VERIFY_READ, VERIFY_WRITE) • get_user(x,](https://reader036.fdocuments.us/reader036/viewer/2022070917/5fb772f5ab305e055634c821/html5/thumbnails/7.jpg)
7
Intel Motherboard
Acknowledgement:http://duartes.org/gustavo/blog/post/motherboard-chipsets-memory-map
![Page 8: CMSC421: Principles of Operating Systems · 5 Useful kernel API functions for bidirectional data movement • access_ok (type, addr, size): type (VERIFY_READ, VERIFY_WRITE) • get_user(x,](https://reader036.fdocuments.us/reader036/viewer/2022070917/5fb772f5ab305e055634c821/html5/thumbnails/8.jpg)
8
Memory Organization during CPU bootup
Acknowledgement:http://duartes.org/gustavo/blog/post/how-computers-boot-up
BIOS load up
Real mode
![Page 9: CMSC421: Principles of Operating Systems · 5 Useful kernel API functions for bidirectional data movement • access_ok (type, addr, size): type (VERIFY_READ, VERIFY_WRITE) • get_user(x,](https://reader036.fdocuments.us/reader036/viewer/2022070917/5fb772f5ab305e055634c821/html5/thumbnails/9.jpg)
9
Bootup Process
Acknowledgement:http://duartes.org/gustavo/blog/post/how-computers-boot-up
BIST (Built in Self Test)
CPU loads BIOS code
BIOS reads MBR and loads
Bootloader
Bootloader Starts
the kernel
One disk page (512 bytes)
![Page 10: CMSC421: Principles of Operating Systems · 5 Useful kernel API functions for bidirectional data movement • access_ok (type, addr, size): type (VERIFY_READ, VERIFY_WRITE) • get_user(x,](https://reader036.fdocuments.us/reader036/viewer/2022070917/5fb772f5ab305e055634c821/html5/thumbnails/10.jpg)
10
Reading the first disk sector
Acknowledgement:http://duartes.org/gustavo/blog/post/how-computers-boot-up
Boot loader Stage 1
(loads Stage 2)
Boot loader Stage 2
(presents users with OS options)
Boot loader Stage 3
(loads the OS)
![Page 11: CMSC421: Principles of Operating Systems · 5 Useful kernel API functions for bidirectional data movement • access_ok (type, addr, size): type (VERIFY_READ, VERIFY_WRITE) • get_user(x,](https://reader036.fdocuments.us/reader036/viewer/2022070917/5fb772f5ab305e055634c821/html5/thumbnails/11.jpg)
11
Bootup Process
Acknowledgement:http://duartes.org/gustavo/blog/post/how-computers-boot-up
BIST (Built in Self Test)
CPU loads BIOS code
BIOS reads MBR and loads
Bootloader
Bootloader Starts
the kernel
One disk page (512 bytes)
Bootloader (stage0, stage1, stage2)
![Page 12: CMSC421: Principles of Operating Systems · 5 Useful kernel API functions for bidirectional data movement • access_ok (type, addr, size): type (VERIFY_READ, VERIFY_WRITE) • get_user(x,](https://reader036.fdocuments.us/reader036/viewer/2022070917/5fb772f5ab305e055634c821/html5/thumbnails/12.jpg)
12
Lets take a look at some code (Coreboot, GRUB, Kernel)
![Page 13: CMSC421: Principles of Operating Systems · 5 Useful kernel API functions for bidirectional data movement • access_ok (type, addr, size): type (VERIFY_READ, VERIFY_WRITE) • get_user(x,](https://reader036.fdocuments.us/reader036/viewer/2022070917/5fb772f5ab305e055634c821/html5/thumbnails/13.jpg)
13
Processes
![Page 14: CMSC421: Principles of Operating Systems · 5 Useful kernel API functions for bidirectional data movement • access_ok (type, addr, size): type (VERIFY_READ, VERIFY_WRITE) • get_user(x,](https://reader036.fdocuments.us/reader036/viewer/2022070917/5fb772f5ab305e055634c821/html5/thumbnails/14.jpg)
14
Process Tree generation
system process tree
![Page 15: CMSC421: Principles of Operating Systems · 5 Useful kernel API functions for bidirectional data movement • access_ok (type, addr, size): type (VERIFY_READ, VERIFY_WRITE) • get_user(x,](https://reader036.fdocuments.us/reader036/viewer/2022070917/5fb772f5ab305e055634c821/html5/thumbnails/15.jpg)
15
But what is a process?
An operating system executes a variety of programs: Batch system – jobs Time-shared systems – user programs or tasks
Process – a program in execution; process execution must progress in sequential fashion
A process includes: program counter stack data section
![Page 16: CMSC421: Principles of Operating Systems · 5 Useful kernel API functions for bidirectional data movement • access_ok (type, addr, size): type (VERIFY_READ, VERIFY_WRITE) • get_user(x,](https://reader036.fdocuments.us/reader036/viewer/2022070917/5fb772f5ab305e055634c821/html5/thumbnails/16.jpg)
16
Process Memory looks like.
virtual Address space
why virtual?
![Page 17: CMSC421: Principles of Operating Systems · 5 Useful kernel API functions for bidirectional data movement • access_ok (type, addr, size): type (VERIFY_READ, VERIFY_WRITE) • get_user(x,](https://reader036.fdocuments.us/reader036/viewer/2022070917/5fb772f5ab305e055634c821/html5/thumbnails/17.jpg)
17
How do we create new processes in userland (fork) Lets see a demo
![Page 18: CMSC421: Principles of Operating Systems · 5 Useful kernel API functions for bidirectional data movement • access_ok (type, addr, size): type (VERIFY_READ, VERIFY_WRITE) • get_user(x,](https://reader036.fdocuments.us/reader036/viewer/2022070917/5fb772f5ab305e055634c821/html5/thumbnails/18.jpg)
18
What is really happening here
![Page 19: CMSC421: Principles of Operating Systems · 5 Useful kernel API functions for bidirectional data movement • access_ok (type, addr, size): type (VERIFY_READ, VERIFY_WRITE) • get_user(x,](https://reader036.fdocuments.us/reader036/viewer/2022070917/5fb772f5ab305e055634c821/html5/thumbnails/19.jpg)
19
What does the memory structure look like before fork()
Virtual addresses
Physical addresses
unallocated
![Page 20: CMSC421: Principles of Operating Systems · 5 Useful kernel API functions for bidirectional data movement • access_ok (type, addr, size): type (VERIFY_READ, VERIFY_WRITE) • get_user(x,](https://reader036.fdocuments.us/reader036/viewer/2022070917/5fb772f5ab305e055634c821/html5/thumbnails/20.jpg)
20
What does it look like after forking?
Parent process
Physical addresses
Child process
![Page 21: CMSC421: Principles of Operating Systems · 5 Useful kernel API functions for bidirectional data movement • access_ok (type, addr, size): type (VERIFY_READ, VERIFY_WRITE) • get_user(x,](https://reader036.fdocuments.us/reader036/viewer/2022070917/5fb772f5ab305e055634c821/html5/thumbnails/21.jpg)
21
Fork() Copy-on-write policy
Parent process
Physical addresses
Child process
write to a page
![Page 22: CMSC421: Principles of Operating Systems · 5 Useful kernel API functions for bidirectional data movement • access_ok (type, addr, size): type (VERIFY_READ, VERIFY_WRITE) • get_user(x,](https://reader036.fdocuments.us/reader036/viewer/2022070917/5fb772f5ab305e055634c821/html5/thumbnails/22.jpg)
22
communication child/parent process (Unnamed pipes)
Pipe(fid); // where int fid[2] fid[0] is the read from the pipe and fid[1] is write to the pipe
dup2(oldfid, newfid) //creates an alias to oldfid//very handy when you do not want to use file
descriptors and use standard ones
![Page 23: CMSC421: Principles of Operating Systems · 5 Useful kernel API functions for bidirectional data movement • access_ok (type, addr, size): type (VERIFY_READ, VERIFY_WRITE) • get_user(x,](https://reader036.fdocuments.us/reader036/viewer/2022070917/5fb772f5ab305e055634c821/html5/thumbnails/23.jpg)
23
Kernel data structure for processes (PCB)
Information associated with each process
Process state
Program counter
CPU registers
CPU scheduling information
Memory-management information
Accounting information
I/O status information
![Page 24: CMSC421: Principles of Operating Systems · 5 Useful kernel API functions for bidirectional data movement • access_ok (type, addr, size): type (VERIFY_READ, VERIFY_WRITE) • get_user(x,](https://reader036.fdocuments.us/reader036/viewer/2022070917/5fb772f5ab305e055634c821/html5/thumbnails/24.jpg)
24
Kernel data structure for processes (PCB)
• Represented by the C structure task_struct
pid t pid; /* process identifier */ long state; /* state of the process */ unsigned int time slice /* scheduling information */
struct task struct *parent; /* this process’s parent */
struct list head children; /* this process’s children */
struct files struct *files; /* list of open files */
struct mm struct *mm; /* address space of this pro */
![Page 25: CMSC421: Principles of Operating Systems · 5 Useful kernel API functions for bidirectional data movement • access_ok (type, addr, size): type (VERIFY_READ, VERIFY_WRITE) • get_user(x,](https://reader036.fdocuments.us/reader036/viewer/2022070917/5fb772f5ab305e055634c821/html5/thumbnails/25.jpg)
25
Process States
As a process executes, it changes state
new: The process is being created
running: Instructions are being executed
waiting: The process is waiting for some event to occur
ready: The process is waiting to be assigned to a processor
terminated: The process has finished execution
![Page 26: CMSC421: Principles of Operating Systems · 5 Useful kernel API functions for bidirectional data movement • access_ok (type, addr, size): type (VERIFY_READ, VERIFY_WRITE) • get_user(x,](https://reader036.fdocuments.us/reader036/viewer/2022070917/5fb772f5ab305e055634c821/html5/thumbnails/26.jpg)
26
Process Context Switch
When CPU switches to another process, the system must save the state of the old process and load the saved state for the new process via a context switch.
Context of a process represented in the PCB
Context-switch time is overhead; the system does no useful work while switching
The more complex the OS and the PCB -> longer the context switch
Time dependent on hardware support
Some hardware provides multiple sets of registers per CPU -> multiple contexts loaded at once
![Page 27: CMSC421: Principles of Operating Systems · 5 Useful kernel API functions for bidirectional data movement • access_ok (type, addr, size): type (VERIFY_READ, VERIFY_WRITE) • get_user(x,](https://reader036.fdocuments.us/reader036/viewer/2022070917/5fb772f5ab305e055634c821/html5/thumbnails/27.jpg)
27
Process Context Switch
![Page 28: CMSC421: Principles of Operating Systems · 5 Useful kernel API functions for bidirectional data movement • access_ok (type, addr, size): type (VERIFY_READ, VERIFY_WRITE) • get_user(x,](https://reader036.fdocuments.us/reader036/viewer/2022070917/5fb772f5ab305e055634c821/html5/thumbnails/28.jpg)
28
Process Scheduling
![Page 29: CMSC421: Principles of Operating Systems · 5 Useful kernel API functions for bidirectional data movement • access_ok (type, addr, size): type (VERIFY_READ, VERIFY_WRITE) • get_user(x,](https://reader036.fdocuments.us/reader036/viewer/2022070917/5fb772f5ab305e055634c821/html5/thumbnails/29.jpg)
29
Process Queues
![Page 30: CMSC421: Principles of Operating Systems · 5 Useful kernel API functions for bidirectional data movement • access_ok (type, addr, size): type (VERIFY_READ, VERIFY_WRITE) • get_user(x,](https://reader036.fdocuments.us/reader036/viewer/2022070917/5fb772f5ab305e055634c821/html5/thumbnails/30.jpg)
30
Lets take a kernel dive to study the process data structure and fork() system call
![Page 31: CMSC421: Principles of Operating Systems · 5 Useful kernel API functions for bidirectional data movement • access_ok (type, addr, size): type (VERIFY_READ, VERIFY_WRITE) • get_user(x,](https://reader036.fdocuments.us/reader036/viewer/2022070917/5fb772f5ab305e055634c821/html5/thumbnails/31.jpg)
31
Next class
• Process management • Inter-process communication (Named pipes, shared
memory (shmget, mmap), message passing) • Intro to threads
![Page 32: CMSC421: Principles of Operating Systems · 5 Useful kernel API functions for bidirectional data movement • access_ok (type, addr, size): type (VERIFY_READ, VERIFY_WRITE) • get_user(x,](https://reader036.fdocuments.us/reader036/viewer/2022070917/5fb772f5ab305e055634c821/html5/thumbnails/32.jpg)
32
An in-class discussion (a bit-hack)