Special Topics on Operating System R. C. Chang 1 Tentative Topics Linux Internal Microkernels Window CE Internal Inferno Other Embedded Operating Systems 2 Trends of Operating System Server : UNIX/Linux, Windows/NT Desktop : Windows 98/NT, UNIX/Linux Embedded : WIN/CE, Inferno,VxWork, JINI,… 3 Operating System Structure Monolithic Kernel – UNIX, Windows/98, Linux First Generation Microkernel – Mach, Chorus,… Second Generation Microkernel – L4, Exokernel,... 4 Linux Internal Text Book: The Linux Kernel by David A. Rusling, (Please Download the book from Internet) Reference Books: Linux Kernel Internals (Second Edition) by M. Beck et al., Addison-Wesley, 1998 5 Hardware Basics A typical PC Motherboard 6 PC Hardware PC, SP, PS Memory – D-Cache, I-Cache – Cache Coherence Bus – ISA, PCI Controllers and Peripherals – IDE, SCSI, NIC,… Address Space 7 Software Basics What is an operating system? – Memory management – Processes $ ps PID TTY STAT TIME COMMAND 158 pRe 1 0:00 -bash 174 pRe 1 0:00 sh /usr/X11R6/bin/startx 175 pRe 1 0:00 xinit /usr/X11R6/lib/X11/xinit/xinitrc -178 pRe 1 N 0:00 bowman 182 pRe 1 N 0:01 rxvt -geometry 120x35 -fg white -bg black 184 pRe 1 < 0:00 xclock -bg grey -geometry -1500-1500 -padding 0 185 pRe 1 < 0:00 xload -bg grey -geometry -0-0 -label xload 187 pp6 1 9:26 /bin/bash 202 pRe 1 N 0:00 rxvt -geometry 120x35 -fg white -bg black 203 ppc 2 0:00 /bin/bash 1796 pRe 1 N 0:00 rxvt -geometry 120x35 -fg white -bg black 1797 v06 1 0:00 /bin/bash 3056 pp6 3 < 0:02 emacs intro/introduction.tex 3270 pp6 3 0:00 ps $ 8 Device Drivers – Controller Chips • NCR810 SCSI The Filesystems – EXT2,CDROM,... 9 Kernel Data Structures Link Lists Hash Tables – Caches Abstract Interfaces – /proc/filessystems 10 Memory Management Virtual Memory – – – – – large address space protection memory mapping fair physical memory allocation shared virtual memory 11 An Abstract Model of VM 12 Demand Paging Load virtual pages that are currently bein used by the executing program Page fault handling Linux uses demand paging to load executable images into a processes virtual memory 13 Swapping If a process needs to bring a virtual page into physical memory and there are no free physical pages available… – Clean or read-only pages --> Disacrd – Dirty page --> swapping Linux uses a Least Recently Used page aging technique to choose pages 14 Physical and Virtual Addressing Modes OS usually runs in physical address model Alpha AXP processor does not have a special physical addressing model. – It divides up the memory space into several areas and designates two of them as physical address area – KSEG (0xfffffc000310000) 15 Access Control V : Valid FOE: Fault on Execute FOW: Write FOR: Read ASM: Address Space Match KRE: kernel mode can read URE: user mode can read PFN: Page Frame Number Page_Dirty, Page_ACCESSED 16 Caches Buffer Cache : block device drivers(hard disks) Page Caches : Speed up access to images and data on disk. Swap Cache: only modified(dirty) pages are saved in the swap file. Hardware Caches : Data/Instruction, TLB 17 Linux Page Tables Each Platform that Linux runs on must provide translation macro that allow the kernel to traverse the page tables for a particular processor 18 Page Allocation and Deallocation Physical page : mem_map structure – mem_map_t for each physical page(initialized at the boot time) – Important fields • count, age, man_nr (physical frame number) free-area vector is used to find free pages 19 Free_area data structure Allocated groups of pages 20 Page Allocation/Deallocation Buddy algorithm Pages are allocated in blocks which are powers of 2 in size The page deallocation code recombine pages into large blocks of free pages whenever it can 21 Memory Mapping Virtual memory is represented by an mm_structure data structure – vm_area_structure 22 Demand Paging Page fault – Search for vm_area_struct (AVL Tree) • If no (access illegal virtual address)--> SIGSEGV – Check for type of access – Decide the page in swap file or somewhere in disk • if the page’ page table entry is invalid but not empty then it is in swap file – nopage operation(by using page cache) 23 The Linux Page Cache 24 The Linux Page Cache Memory mapped files are read a page at a time and these pages are stored in the page cache Search through page_hash_table Linux --> single page read ahead 25 Swapping Out and Discarding Pages Kernel Swap Daemon(kswapd) – kernel thread – keep enough free pages in the system – started by init process and wait for kernel swap timer to periodcally expire – if # of free pages > free_page_high do nothing – otherwise kswapd try to reduce the number of physical pages being used by the system: 26 Swap out pages Reduce the size of the buffer and page caches – clock algorithm Swapping out System V shared memory pages – update multiple page table entries Swapping out and discarding pages – clock algorithm 27 The Swap Cache When swapping pages out, Linux avoids writing pages if it does not have to Swap cache --> a PTE for a swapped out page If a swap cache is non-zero, a page in swap file has not been modified --> no need to write the page to the swap file 28 Swapping Pages In Demand paging swapin operation 29