CS 6560 Operating System Design Lecture 5: System Calls Interrupts.

22
CS 6560 Operating System Design Lecture 5: System Calls Interrupts

Transcript of CS 6560 Operating System Design Lecture 5: System Calls Interrupts.

Page 1: CS 6560 Operating System Design Lecture 5: System Calls Interrupts.

CS 6560 Operating System Design

Lecture 5:

System Calls

Interrupts

Page 2: CS 6560 Operating System Design Lecture 5: System Calls Interrupts.

System Calls

• LKD: Chapter 4: System Calls

Page 3: CS 6560 Operating System Design Lecture 5: System Calls Interrupts.

References

• Our textbook: Robert Love, Linux Kernel Development, 2nd edition, Novell Press, 2005.

• Understanding the LINUX Kernel, 3rd. edition, O’Reilly, 2005. (covers 2.6)

• Class notes at Georga Tech:– http://www-static.cc.gatech.edu/classes/AY2001/cs3210_spring/

• Intel Documentation:– http://developer.intel.com/design/index.htm

Page 4: CS 6560 Operating System Design Lecture 5: System Calls Interrupts.

Why System Calls?

• Need a model and a mechanism for users to access system resources, which is– User portable: abstracts kernel to users

– Device portable: abstracts kernel to devices

– Safe: protects users and devices

– Efficient: simple and fast

Page 5: CS 6560 Operating System Design Lecture 5: System Calls Interrupts.

Standardization

• POSIX: IEEE 1003– Describes Kernel Application Interface (API)

(more at the level of a C library)

Page 6: CS 6560 Operating System Design Lecture 5: System Calls Interrupts.

How?

• User program calls a C function which jumps into the kernel

• Architecture dependent, for x86, two ways:– Software interrupt– Sysenter instruction: see Intel Docs

Page 7: CS 6560 Operating System Design Lecture 5: System Calls Interrupts.

Design Principles

• System calls should– Have a single purpose– Implement method, not policy

Page 8: CS 6560 Operating System Design Lecture 5: System Calls Interrupts.

Steps for System Call

• 1) Push registers on stack• 2) call library function• 3) place function # in register (register eax on x86) (or stack)• 4) execute INT instruction (INT 80h on x86 machines) or

special sys call instruction• 5) dispatch to sys call interrupt handler, change to privileged

mode• 6) runs sys_xx function in the kernel• 7) return to library function in user mode• 8) return back to program• 9) clean up the stack

Page 9: CS 6560 Operating System Design Lecture 5: System Calls Interrupts.

Function Call Numbers

• See man 2 intro - This is the man chapter on system calls (may be out of date)

• Locate unistd.h files in various places– In the system for app development

• /usr/include/asm/• /usr/include/kernel/

– In the kernel development files• include/asm-

• Locate entry point - depends upon arch. For example, for i386:./arch/i386/kernel/syscall_table.S: .long sys_fork

Page 10: CS 6560 Operating System Design Lecture 5: System Calls Interrupts.

Entry Points

• ENTRY(sys_call_table)• .long sys_restart_syscall /* 0 - old "setup()" system call, used for restarting */• .long sys_exit• .long sys_fork• .long sys_read• .long sys_write• .long sys_open /* 5 */• .long sys_close• .long sys_waitpid• .long sys_creat• .long sys_link• .long sys_unlink /* 10 */• .long sys_execve• .long sys_chdir• .long sys_time

Page 11: CS 6560 Operating System Design Lecture 5: System Calls Interrupts.

Numbers correspond• #ifndef _ASM_I386_UNISTD_H_• #define _ASM_I386_UNISTD_H_

• /*• * This file contains the system call numbers.• */

• #define __NR_restart_syscall 0• #define __NR_exit 1• #define __NR_fork 2• #define __NR_read 3• #define __NR_write 4• #define __NR_open 5• #define __NR_close 6• #define __NR_waitpid 7• #define __NR_creat 8• #define __NR_link 9• #define __NR_unlink 10

Page 12: CS 6560 Operating System Design Lecture 5: System Calls Interrupts.

include/asm-i386/unistd.hsame as /usr/include/

• #ifndef _ASM_I386_UNISTD_H_• #define _ASM_I386_UNISTD_H_

• /*• * This file contains the system call numbers.• */

• #define __NR_restart_syscall 0• #define __NR_exit 1• #define __NR_fork 2• #define __NR_read 3• #define __NR_write 4• #define __NR_open 5• #define __NR_close 6• #define __NR_waitpid 7• #define __NR_creat 8• #define __NR_link 9• #define __NR_unlink 10• #define __NR_execve 11

Page 13: CS 6560 Operating System Design Lecture 5: System Calls Interrupts.

Details

• Read in the book: – How it works– How to make one

• Look at Georgia Tech class notes– Diagrams– More details– Strace function

• HW assignment next week to make and install a system call

Page 14: CS 6560 Operating System Design Lecture 5: System Calls Interrupts.

Final point

• Don’t just add more system calls - keep the kernel interface simple

Page 15: CS 6560 Operating System Design Lecture 5: System Calls Interrupts.

Interrupts

• LKD Ch6

Page 16: CS 6560 Operating System Design Lecture 5: System Calls Interrupts.

Why Interupts?

• Allow CPU to exchange data with slow devices

• Handle exceptions - error conditions

• Provide protected access to resources such as the kernel

Page 17: CS 6560 Operating System Design Lecture 5: System Calls Interrupts.

How do Interrupts Work?

• Use Interrupt handlers: Interrupt service routines (ISR)– Just C functions that can be called at any time– Must return quickly– Run in special context– Must be registered (Use: request_itq)

Page 18: CS 6560 Operating System Design Lecture 5: System Calls Interrupts.

Example ISR

• See the text for an example

Page 19: CS 6560 Operating System Design Lecture 5: System Calls Interrupts.

Interrupt Context

• Context (while in kernel):– Process Context

• Executing a system call on behalf of a process

• current points to task_struct of current process

– Interrupt Context• Reponding to an interrupt

• current points to task_struct of whatever process was interrupted

Page 20: CS 6560 Operating System Design Lecture 5: System Calls Interrupts.

Top and Bottom Halves

• Conflicting needs:– Return quickly

– Get lots of work done

• Solution: Break up the interrupt processing into two parts– Top half: returns quickly, after setting up work to be

done

– Bottom half: scheduled by the kernel to get work done at a later time (see next chapter)

Page 21: CS 6560 Operating System Design Lecture 5: System Calls Interrupts.

/proc entry

• Look at /proc/interrupts

Page 22: CS 6560 Operating System Design Lecture 5: System Calls Interrupts.

Details

• Read the book for more details– Disabling and enabling interrupts– Knowing status of an interrupt

• More needed: must know a lot more before writing any code, see /usr/src/linux/Documentation

• Next time: look at example and study deferred work: top and bottom halves