Download presentation
Presentation is loading. Please wait.
Published byTyrone Hampton Modified over 9 years ago
1
1 CSE 451 Section 2: Interrupts, Syscalls, Virtual Machines, and Project 1
2
2 Project 0 How is it going? What's happening here? key == ht[loc]->key
3
3 Project 0 How is it going? What's happening here? key == ht[loc]->key Bad! Comparing pointers. Need a “deep equals” Must pass in an equality function Worry about it for this project Also, don’t forget about resizing! And memory management And memory leaks And edge cases And fun
4
Interrupts Interrupt Hardware or software Hardware interrupts caused by devices signalling CPU Software interrupts caused by code Exception Unintentional software interrupt E.g. errors, divide-by-zero, general protection fault Trap Intentional software interrupt Controlled method of entering kernel mode System calls
5
What happens in an interrupt? Execution halted CPU switched from user mode to kernel mode State saved Registers, stack pointer, PC With interrupt number index interrupt descriptor table to find handler Run handler Handler is (mostly) just a function pointer Restore state CPU switched from kernel mode to user mode Resume execution
6
Interrupts What happens if there's another interrupt during the handler? What happens if an interrupt fires when they are disabled?
7
System calls An invocation of an OS service – So the OS can manage and protect services Requires architectures support – But the most basic mechanism is just an interrupt
8
Syscall control flow User application calls a library User-level library call Invoke system call through stub _syscallN() sets up arguments for the OS Interrupt (probably) Syscall handler indexes system call table to find a vector to jump to OS performs operation Must check arguments! Cannot allow user to trick the OS into performing an unsafe operation! OS prepares return OS returns from interrupt and resume user
9
How Linux does syscalls You can see what happens when the kernel invokes a syscall here: Arch/x86/kernel/entry_32.S If you don't mind reading assembly Doesn't really use “interrupts” anymore “int 0x80” and “iret” replaced by “sysenter” and “sysexit” Similar operations supported by architecture
10
Syscalls with a SW VMM read() syscall trap handler handle read syscall read from disk() trap privileged instruction If “kernel” mode: emulate virtual disk else: raise protection violation finish read syscall copy data to user buffer return from system call return from read() Application Guest OSVMM
11
But really... ApplicationGuest OSVMMHardware read() syscall trap detected trap handler; change VM to “kernel” mode trap handler handle read syscall read from disk() priv insc. detected trap handler; emulate I/O........
12
Syscalls with a HW VMM System call and interrupt vectors for the guest OS are loaded into hardware. There's actually no VMM intervention required (if it doesn't require a host OS service) In a read, we still need to go to the VMM for the hardware I/O. But, this is only one case. Sometimes SW beats HW.
13
Paravirtualization Duplicate a similar architecture Compromise: use HW and guest OS in harmony Reduce duplicated work in guest OS Breaks fidelity! Requires changes to guest OS like its being implemented for a new platform
14
Not enough? For masochists: A Comparison of Software and Hardware Techniques for x86 Virtualization http://www.vmware.com/pdf/asplos235_ada ms.pdf
15
15 Project 1 Two main parts: Write a simple shell in C Add a simple system call to Linux kernel Due: Mon., Oct 19, 11:59pm Electronic turnin: code + writeup
16
16 The CSE451 shell Print out prompt Accept input Parse input If built-in command do it directly Else spawn new process Launch specified program Wait for it to finish Repeat CSE451Shell% /bin/date Fri Jan 16 00:05:39 PST 2004 CSE451Shell% pwd /root CSE451Shell% cd / CSE451Shell% pwd / CSE451Shell% exit
17
17 CSE451 Shell Hints In your shell: Use fork to create a child process Use execvp to execute a specified program Use wait to wait until child process terminates Useful library functions (see man pages): Strings: strcmp, strncpy, strtok, atoi I/O: fgets Error report: perror Environment variables: getenv
18
18 Adding a System Call Add execcounts system call to Linux: Purpose: collect statistics Count number of times you call fork, vfork, clone, and exec system calls. Steps: Modify kernel to keep track of this information Add execcounts to return the counts to the user Use execcounts in your shell to get this data from kernel and print it out.
19
19 Programming in kernel mode Your shell will operate in user mode Your system call code will be in the Linux kernel, which operates in kernel mode Be careful - different programming rules, conventions, etc.
20
20 Programming in kernel mode Can’t use application libraries (e.g. libc) E.g. can’t use printf Use only functions defined by the kernel E.g. use printk instead Include files are different in the kernel Don’t forget you’re in kernel space You cannot trust user space E.g. unsafe to access a pointer from user space directly
21
21 Kernel development hints Best way to learn: read existing code Use grep –r search_string * – -I for case-insensitive Use LXR (Linux Cross Reference): http://lxr.linux.no/ http://lxr.linux.no/
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.