DOC PREVIEW
UT CS 378 - Linux Kernel Programming

This preview shows page 1-2-3-24-25-26 out of 26 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Linux Kernel ProgrammingYongguang Zhang([email protected])Department of Computer Sciences THE UNIVERSITY OF TEXAS AT AUSTINCS 378 (Spring 2003)Copyright 2003, Yongguang ZhangSpring 2003 © 2003 Yongguang Zhang 2This Lecture• Any Questions?• /proc file system• Memory ManagementSpring 2003 © 2003 Yongguang Zhang 3/proc File System• Pseudo-filesystem as an interface to kernel data structures– Each file correspond to a data structure in kernel– Purpose of /proc filesystem: an extensible mechanism to support all types of data transfer in a common interface–Most files in /proc are read-only, but some allow kernel variables to be changed• Top-level subdirectories– Numerical: for processes– Non-numericalSpring 2003 © 2003 Yongguang Zhang 4Numerical Subdirectory• Kernel data structures for the running processes– One subdirectory per process– Named by process ID• Example files:cmdline the complete command line for the process.cpu CPU usage by this processcwd symbolic link to the $CWD of the process. environ contains the environment for the process. exe symbolic link to the running binary. fd/ a subdirectory of open files by the process.…Spring 2003 © 2003 Yongguang Zhang 5Non-Numerical Files/Directoriescpuinfo information about the CPUs…net/ many files/subdir for many parts of Linux networkingnet/arpnet/devnet/route…sys/ sysctl files (dynamic configurable kernel parameters)…Too many of them!Spring 2003 © 2003 Yongguang Zhang 6Read/Write at User Space• Just like a normal file–open, read, write, close• Examples:– To read in shell:• cat /proc/cpuinfo– To write in shell:• echo cool-name > /proc/sys/kernel/hostname• For configurable kernel parameters (/proc/sys/)–Also with sysctl command (read/write)• /sbin/sysctl -w kernel/hostname= "cool-name"Spring 2003 © 2003 Yongguang Zhang 7Creating New /proc File in Kernel• Understand the struct proc_dir_entry data object• Write callback functions• Create struct proc_dir_entry data objectSpring 2003 © 2003 Yongguang Zhang 8/proc File Data Object•include/linux/proc_fs.hstruct proc_dir_entry { unsigned short low_ino; unsigned short namelen; const char *name; mode_t mode; nlink_t nlink; uid_t uid; gid_t gid; unsigned long size; struct inode_operations * proc_iops; struct file_operations * proc_fops; get_info_t *get_info; struct module *owner; struct proc_dir_entry *next, *parent, *subdir; void *data; read_proc_t *read_proc; write_proc_t *write_proc; atomic_t count; /* use count */ int deleted; /* delete flag */ kdev_t rdev;};Spring 2003 © 2003 Yongguang Zhang 9Callbacks• Function types (include/linux/proc_fs.h)–typedef int (read_proc_t)(char *page, char **start, off_t off, int count, int *eof, void *data);– typedef int (write_proc_t)(struct file *file, const char *buffer, unsigned long count, void *data);• Example use:struct proc_dir_entry *e = create_proc_entrance(…);…int my_read_proc(…) { … }E->read_proc = my_read_proc;Spring 2003 © 2003 Yongguang Zhang 10Communicate with User Space• User space to read data from kernel–User Space: to make read() system call– Kernel: will call the corresponding e.read_proc()• sys_read() ⇒ … ⇒ e.read_proc()• User space to write data to kernel–User Space: to make write() system call–Kernel: will call the corresponding e.write_proc()• sys_write() ⇒ … ⇒ e.write_proc()• Q: how/where is the transfer of data/control really taken place?Spring 2003 © 2003 Yongguang Zhang 11Adding Entries to /proc File System• To add “regular” file:–struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode, struct proc_dir_entry *parent);• To make symbolic link:– struct proc_dir_entry *proc_symlink(const char *, struct proc_dir_entry *, const char *);• To create sysctl file• To make subdirectorySpring 2003 © 2003 Yongguang Zhang 12System Calls vs /proc File System• Both used to extend the user-kernel interface• Limitation of system calls:– Not scalable: each new service needs a new system call– Not extensible: how to manage the system call numbers, confirmation to POSIX standard?• /proc file system is considered a better way– Still, standardization is always a problemSpring 2003 © 2003 Yongguang Zhang 13Lectures From Now On• How to deal with Linux kernel in general– Basics data structures, utility routines– Learning and problem solving skills• Topics about different parts of the kernel– I will go very fast (high level)– Details you can learn by doing the projectsSpring 2003 © 2003 Yongguang Zhang 14Linux Memory Management• Memory Model: Demand Paged Virtual Memory• Time to review related topics in CS372!– What is virtual memory? What is physical memory?– What is address translation?– What is primary memory? What is secondary memory?– What is paging? What is a page?– What is page replacement policy?– What is a “dirty” page?– What is page fault?Spring 2003 © 2003 Yongguang Zhang 15Where to Look?• Source code– ./mm: architecture-independent linux memory model– ./arch/xxx/mm : architecture dependent– ./include/asm-xxx/:• page.h, pg*.h, mmu*.h, ...• Textbook– LKP: §4– ULK: §2, §7Spring 2003 © 2003 Yongguang Zhang 16Memory Model• Linear Address Space– Each process has its own memory address space– Mapping to Physical Memory by kernel MM– X86 architecture: 32bit (4GB)• Actual Physical Memory– Can range from KB to TB– Larger than 4GB in i386 architecture? Need PAE• Linux Memory Model– Architecture-independent (mm/)– Must be mapped to architecture specific (arch/xxx/mm)Spring 2003 © 2003 Yongguang Zhang 17Process Memory Layout• User Space– Code segment, data segment (include/asm/page.h)– Addressable: 0 to PAGE_OFFSET-1• Kernel Space– Code segment, data segment– Shared by all processes–Addressable: PAGE_OFFSET-1 to (unsigned)(-1)• In i386 architecture–PAGE_OFFSET=0xc0000000– This means: 1GB reserved for kernel, 3GB for userSpring 2003 © 2003 Yongguang Zhang 18A Very Abstract ModelVirtual memory Virtual memoryPhysical memoryPage table Page tableProcess A Process B0PAGE_OFFSETSpring 2003 © 2003 Yongguang Zhang 19Architecture Specific• Page: Basic unit of memory management–PAGE_SIZE (include/asm/page.h)– i386 architecture: page size is 4KB (212)– Alpha architecture: page size is 8KB (213)• Addressable memory


View Full Document

UT CS 378 - Linux Kernel Programming

Documents in this Course
Epidemics

Epidemics

31 pages

Discourse

Discourse

13 pages

Phishing

Phishing

49 pages

Load more
Download Linux Kernel Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Linux Kernel Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Linux Kernel Programming 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?