The ‘system-call’ interface We see how an application program can invoke privileged kernel services.

Slides:



Advertisements
Similar presentations
CS 4284 Systems Capstone Godmar Back Processes and Threads.
Advertisements

Chapter 9 TRAP Routines and Subroutines. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 9-2 Subroutines.
Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
Using VMX within Linux We explore the feasibility of executing ROM-BIOS code within the Linux x86_64 kernel.
Lecture 1: Overview of Computers & Programming
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
1 Homework / Exam Turn in mp2 at start of class today Reading –PAL, pp 3-6, Exam #1 next class –Open Book / Open Notes –NO calculators or other.
The Environment of a UNIX Process. Introduction How is main() called? How are arguments passed? Memory layout? Memory allocation? Environment variables.
Creating a device-file node An introduction to some privileged Linux system-calls (needed for an upcoming programming exercise)
The ‘system-call’ ID-numbers How can Linux applications written in assembly language create and access files?
Inline Assembly Section 1: Recitation 7. In the early days of computing, most programs were written in assembly code. –Unmanageable because No type checking,
Context Switch Animation Another one by Anastasia.
Timeout for some demos An instructive look at how the Linux Operating System sets up system data-structures.
CS 536 Spring Run-time organization Lecture 19.
PC hardware and x86 3/3/08 Frans Kaashoek MIT
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
Facilities for x86 debugging Introduction to Pentium features that can assist programmers in their debugging of software.
Standard C Library Application Programming Interface to System-Calls.
Chapter 12: High-Level Language Interface. Chapter Overview Introduction Inline Assembly Code C calls assembly procedures Assembly calls C procedures.
1 Homework Reading –PAL, pp , Machine Projects –Finish mp2warmup Questions? –Start mp2 as soon as possible Labs –Continue labs with your.
Unix Process Environment. main Function A C program starts execution with a function called main. The prototype for the main function is: int main (int.
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
Operating Systems 1 K. Salah Module 1.2: Fundamental Concepts Interrupts System Calls.
Run-time Environment and Program Organization
Stacks and Frames Demystified CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
Copyright Arshi Khan1 System Programming Instructor Arshi Khan.
Operating Systems Concepts 1. A Computer Model An operating system has to deal with the fact that a computer is made up of a CPU, random access memory.
Process. Process Concept Process – a program in execution Textbook uses the terms job and process almost interchangeably A process includes: – program.
1 CSC 2405: Computer Systems II Spring 2012 Dr. Tom Way.
Chapter 3.1:Operating Systems Concepts 1. A Computer Model An operating system has to deal with the fact that a computer is made up of a CPU, random access.
1 CS503: Operating Systems Part 1: OS Interface Dongyan Xu Department of Computer Science Purdue University.
System Calls 1.
6.828: PC hardware and x86 Frans Kaashoek
Fall 2008CS 334: Computer SecuritySlide #1 Smashing The Stack A detailed look at buffer overflows as described in Smashing the Stack for Fun and Profit.
Chapter 2: Operating-System Structures. 2.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 2: Operating-System Structures Operating.
Compiler Construction
1 Linux Operating System 許 富 皓. 2 Processes Switch.
Practical Session 4. Labels Definition - advanced label: (pseudo) instruction operands ; comment valid characters in labels are: letters, numbers, _,
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 3: Operating-System Structures System Components Operating System Services.
Shell (Part 2). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
UNIX Files File organization and a few primitives.
Assembly Language for x86 Processors 7th Edition Chapter 13: High-Level Language Interface (c) Pearson Education, All rights reserved. You may modify.
Sahar Mosleh California State University San MarcosPage 1 Stack operations, Applications and defining procedures.
Lecture Topics: 10/29 Architectural support for operating systems –timers –kernel mode –system calls –protected instructions.
Operating Systems 1 K. Salah Module 1.2: Fundamental Concepts Interrupts System Calls.
Low Level Programming Lecturer: Duncan Smeed The Interface Between High-Level and Low-Level Languages.
Functions/Methods in Assembly
Assembly 07. Outline Boxes within Boxes Procedure Definition call, ret Saving / Restoring Registers Argument(s) Return Value(s) Global vs. Local Data.
1 The Stack and Procedures Chapter 5. 2 A Process in Virtual Memory  This is how a process is placed into its virtual addressable space  The code is.
Interrupt driven I/O Computer Organization and Assembly Language: Module 12.
October 1, 2003Serguei A. Mokhov, 1 SOEN228, Winter 2003 Revision 1.2 Date: October 25, 2003.
Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1.
Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope.
Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.
1 Assembly Language: Function Calls Jennifer Rexford.
Lecture 7 Interrupt ,Trap and System Call
CS 177 Computer Security Lecture 9
C function call conventions and the stack
Homework Reading Machine Projects Labs PAL, pp ,
CS 3305 System Calls Lecture 7.
Homework In-line Assembly Code Machine Language
Assembly Language Programming I: Introduction
Stack Frames and Advanced Procedures
Assembly Language Programming II: C Compiler Calling Sequences
Discussions on HW2 Objectives
System Calls David Ferry CSCI 3500 – Operating Systems
Discussions on HW2 Objectives
System Calls System calls are the user API to the OS
Computer Organization and Assembly Language
Presentation transcript:

The ‘system-call’ interface We see how an application program can invoke privileged kernel services

Recall our previous lesson First, we presented a (simplified) diagram of the major components in a modern OS Then, we focused our attention on one of the kernel’s interfaces, namely, its role in controlling hardware devices (specifically the IDE fixed disk). We saw typical code for performing an actual device-command. Today we look at another kernel interface.

A Modern OS Design Hardware Application Shared Runtime Libraries user-mode supervisor-mode System Call Interface Device Driver Components memory manager task manager file manager network manager OS Kernel

Our demo ‘bypassed’ the OS Hardware Application Shared Runtime Libraries user-mode supervisor-mode System Call Interface Device Driver Components memory manager task manager file manager network manager OS Kernel Application (idnumber) Required IOPL==3

Role of ‘runtime libraries’ To understand what role is played by the kernel’s interface with ‘runtime libraries,’ let’s see how we could go around them. We can create a demo-program that does not need to use the standard C library – it will perform all their work on its own. If you fire your janitor, you will very quickly come to appreciate all that he did for you!

Demo will call kernel directly Hardware Application demo Shared Runtime Libraries user-mode supervisor-mode System Call Interface Device Driver Components memory manager task manager file manager network manager OS Kernel

A normal C program example # include // for write() # include // for exit() char message[ ] = “Hello!\n”; int main( void ) { write( 1, message, 7 ); exit( 0 ); }

Standard device-files Whenever a new program is launched, the operating system will automatically ‘open’ three device-files, named ‘stdin’, ‘stdout’, and ‘stderr’, using file-drescriptors 0, 1, 2. –‘stdin’ is the standard input device (keyboard) –‘stdout’ is the standard output device (screen) –‘stderr’ is the standard error device (screen)

Standard C library functions The functions ‘write()’ and ‘exit()’ were not actually defined within our program-code They are examples of ‘external’ functions Header-files let the compiler know how to generate assembler code that calls them Their actual definitions are part of the standard GNU/C runtime-library (‘glibc’) The linker connects our code to ‘glibc’

The function ‘prototypes’ int write( int fd, char * buf, int count ); void exit( int status ); The C compiler needs this information to correctly generate the machine-code for calls to these ‘external’ library-functions.

C program example revised (we’ve omitted the header-files) extern int write( int, char *, int ); extern void exit( int ); char message[ ] = “Hello!\n”; int main( void ) { write( 1, message, 7 ); exit( 0 ); }

How these calls get compiled pushl $7 # parameter no. 3 pushl buf # parameter no. 2 pushl $1 # parameter no. 1 call write # call to C library addl $12, %esp # discard params pushl $0 # parameter no. 1 call exit # call to C library

Stack’s layout on entering ‘write’ Parameter number 3 (count) Parameter number 2 (buf) parameter number 1 (fd) Return-address (from EIP) ESP 32-bits

What ‘write’ function does write:pushl %ebp# save caller’s EBP movl %esp, %ebp# point EBP to stacktop # copy the parameters into general registers movl $4, %eax# sys_WRITE ID-number movl 8(%ebp), %ebx# parameter no. 1 to EBX movl 12(%ebp), %ecx# parameter no. 2 to ECX movl 16(%ebp), %edx# parameter no. 3 to EDX int $0x80# enter the linux kernel popl %ebp# restore previous EBP ret # return to the caller

What ‘exit’ function does exit:pushl %ebp# save caller’s EBP movl %esp, %ebp# point EBP to stacktop # copy the parameter into a general registers movl $1, %eax# sys_EXIT ID-number movl 8(%ebp), %ebx# parameter no. 1 to EBX int $0x80# enter the linux kernel # Note: The ‘exit()’ function never returns to the application

So we don’t really need library If we are willing to rewrite our program using assembly language (so that we can refer to cpu registers by name), we can make system-calls directly to the kernel (using the ‘int $0x80’ trap- instruction) without needing to link our code to those shared C library functions Doing this helps us convince ourselves that we do indeed understand what the role of the C runtime function-library is – it’s a programming convenience (puts a pretty face on ugly code).

Program ‘portability’ If we want a program that can be compiled and executed on machines with different CPU designs (e.g., PCs versus MACs), then we will need to avoid using register- names in our program-code (i.e., the PC’s EAX register doesn’t exist on the MAC) The shared library ‘hides’ the differences between CPUs, since all the CPU-specific work is done by code within the library

POSIX The ‘Portable Operating System Interface’ Defines a conformance ‘standard’ that lets application programmers write code which is ‘portable’ across differing architectures Implemented with header-files and shared runtime libraries Documented as an ANSI standard Online reference available via ‘man’ pages

Frequently needed functions Here are a few of the POSIX standard functions which are most frequently used by applications: –open() –read() –write() –close() –exit() It’s advisable to learn them! (will save you time)

Their C function-prototypes void exit( int status ); int read( int fd, char *buf, int count ); int write( int fd, char *buf, int count ); int open( char *fname, int flags, … ); int close( int fd ); NOTE: These POSIX functions correspond directly to Linux kernel system-calls

function semantics We will give a rough general description of the actions taken by the kernel when these five frequently needed functions are called Additional details about each function can be found online, using the ‘man’ command Example: $ man 2 read

int write( int fd, char *buf, int count ) This function asks the kernel to transfer ‘count’ bytes from the array pointed to by ‘buf’ to the previously-opened file specified by the file-descriptor ‘fd’. If the kernel cannot transfer any bytes, it returns the value -1; otherwise it returns the number of bytes that the kernel was able to transfer successfully and advances the file-pointer accordingly.

int read( int fd, char *buf, int count ) This function asks the kernel to transfer ‘count’ bytes into the array pointed to by ‘buf’ from the previously-opened file specified by ‘fd’. If the kernel cannot transfer any bytes, it returns a value of unless the file-pointer is already at the end of the file; otherwise, it returns the number of bytes successfully transferred and advances the file-pointer accordingly.

int open( char *fname, int flags, … ) This function asks the kernel to set up an internel record establishing a connection between the file named ‘fname’ and an unused nonnegative integer known as a file-descriptor, provided the file’s access permissions allow the kind of access that is specified by the ‘flags’ argument The function returns -1 if it cannot perform this operation; otherwise, it returns the file-descriptor value. An additional argument when creating a new file

int close( int fd ); This function asks the kernel to remove the connection it had previously set up between the file-descriptor ‘fd’ and the internal kernel record of an associated file. The value -1 is returned if the kernel was not succesful in performing this operation (e.g., the file had already been closed); otherwise, this function returns 0.

void exit( int status ) This function asks the kernel to terminate the current program, and place the value of ‘status’ into the internal record that the kernel had associated with this program when it was first launched. This function will not return any value (because it does not return at all!)

In-class exercises Modify the ‘mywrite.c’ demo (see class website) so it that writes to device 2 instead of device 1 Try using the debugging tool to single-step through the execution of ‘filedemo’. Watch which values the kernel returns (in EAX) after each ‘int $0x80’ trap-instruction. Verify that it indeed created the ‘testfile.dat’ file. Write a short program (named ‘myread.c’) which uses standard C library functions to ‘read’ the ‘testfile.dat’ file and ‘write’ its contents to ‘stdout’