W4118 Operating Systems. Junfeng Yang



Similar documents
The Plan Today... System Calls and API's Basics of OS design Virtual Machines

COS 318: Operating Systems

CHAPTER 6 TASK MANAGEMENT

W4118 Operating Systems. Instructor: Junfeng Yang

CS 377: Operating Systems. Outline. A review of what you ve learned, and how it applies to a real operating system. Lecture 25 - Linux Case Study

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

Under The Hood: The System Call

Process Description and Control william stallings, maurizio pizzonia - sistemi operativi

Understanding a Simple Operating System

Assembly Language: Function Calls" Jennifer Rexford!

CSC 2405: Computer Systems II

Jorix kernel: real-time scheduling

sys socketcall: Network systems calls on Linux

System Calls and Standard I/O

How do Users and Processes interact with the Operating System? Services for Processes. OS Structure with Services. Services for the OS Itself

Encryption Wrapper. on OSX

Phoenix Technologies Ltd.

Andreas Herrmann. AMD Operating System Research Center

Operating Systems. Lecture 03. February 11, 2013

64-Bit NASM Notes. Invoking 64-Bit NASM

Lecture 6: Interrupts. CSC 469H1F Fall 2006 Angela Demke Brown

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

CS5460: Operating Systems. Lecture: Virtualization 2. Anton Burtsev March, 2013

Project Adding a System Call to the Linux Kernel

Operating System Structure

Introduction. What is an Operating System?

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

There s a kernel security researcher named Dan Rosenberg whose done a lot of linux kernel vulnerability research

CS161: Operating Systems

Outline. Outline. Why virtualization? Why not virtualize? Today s data center. Cloud computing. Virtual resource pool

Operating System Overview. Otto J. Anshus

Example of Standard API

Exceptions in MIPS. know the exception mechanism in MIPS be able to write a simple exception handler for a MIPS machine

Review from last time. CS 537 Lecture 3 OS Structure. OS structure. What you should learn from this lecture

CS3600 SYSTEMS AND NETWORKS

Application-Level Debugging and Profiling: Gaps in the Tool Ecosystem. Dr Rosemary Francis, Ellexus

Lecture 5. User-Mode Linux. Jeff Dike. November 7, Operating Systems Practical. OSP Lecture 5, UML 1/33

Monitoring, Tracing, Debugging (Under Construction)

Lecture 7: Machine-Level Programming I: Basics Mohamed Zahran (aka Z)

Linux Kernel Architecture

ELEC 377. Operating Systems. Week 1 Class 3

Exploiting the x86 Architecture to Derive Virtual Machine State Information

Assignment 5: Adding and testing a new system call to Linux kernel

Buffer Overflows. Security 2011

OS Virtualization Frank Hofmann

Operating Systems and Networks

Automatic Logging of Operating System Effects to Guide Application-Level Architecture Simulation

Sistemi Operativi. Lezione 25: JOS processes (ENVS) Corso: Sistemi Operativi Danilo Bruschi A.A. 2015/2016

Hacking Techniques & Intrusion Detection. Ali Al-Shemery arabnix [at] gmail

Red Hat Linux Internals

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

Have both hardware and software. Want to hide the details from the programmer (user).

4.1 Introduction 4.2 Explain the purpose of an operating system Describe characteristics of modern operating systems Control Hardware Access

How To Understand How A Process Works In Unix (Shell) (Shell Shell) (Program) (Unix) (For A Non-Program) And (Shell).Orgode) (Powerpoint) (Permanent) (Processes

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 20: Stack Frames 7 March 08

Advanced Operating Systems and Virtualization MS degree in Computer Engineering Sapienza University of Rome Lecturer: Francesco Quaglia

Format string exploitation on windows Using Immunity Debugger / Python. By Abysssec Inc

5.14. EXCEPTION AND INTERRUPT REFERENCE

Off-by-One exploitation tutorial

Lecture 25 Symbian OS

x86 ISA Modifications to support Virtual Machines

CS 3530 Operating Systems. L02 OS Intro Part 1 Dr. Ken Hoganson

CSE 120 Principles of Operating Systems. Modules, Interfaces, Structure

Operating Systems (Linux)

FAME Operating Systems

Hotpatching and the Rise of Third-Party Patches

Module 20: The Linux System

Introduction to Operating Systems. Perspective of the Computer. System Software. Indiana University Chen Yu

OPERATING SYSTEM SERVICES

PCI-SIG ENGINEERING CHANGE REQUEST

Linux Driver Devices. Why, When, Which, How?

Minix Mini Unix (Minix) basically, a UNIX - compatible operating system. Minix is small in size, with microkernel-based design. Minix has been kept

Intel Application Software Development Tool Suite 2.2 for Intel Atom processor. In-Depth

Sensors and the Zaurus S Hardware

Linux Kernel Networking. Raoul Rivas

Introduction. Application Security. Reasons For Reverse Engineering. This lecture. Java Byte Code

CS222: Systems Programming

How To Write A Windows Operating System (Windows) (For Linux) (Windows 2) (Programming) (Operating System) (Permanent) (Powerbook) (Unix) (Amd64) (Win2) (X

Chapter 6, The Operating System Machine Level

An Implementation Of Multiprocessor Linux

System Calls Related to File Manipulation

Debugging with TotalView

CS61: Systems Programing and Machine Organization

System/Networking performance analytics with perf. Hannes Frederic Sowa

Migration of Process Credentials

Lecture 27 C and Assembly

Safety measures in Linux

A deeper look at Inline functions

Interrupts in Linux. COMS W4118 Prof. Kaustubh R. Joshi hfp://

FRONT FLYLEAF PAGE. This page has been intentionally left blank

TODAY, FEW PROGRAMMERS USE ASSEMBLY LANGUAGE. Higher-level languages such

Objectives. Chapter 2: Operating-System Structures. Operating System Services (Cont.) Operating System Services. Operating System Services (Cont.

Intel 64 and IA-32 Architectures Software Developer s Manual

Exploiting Trustzone on Android

Chapter 3: Operating-System Structures. System Components Operating System Services System Calls System Programs System Structure Virtual Machines

Transcription:

W4118 Operating Systems Junfeng Yang

Outline Linux overview Interrupt in Linux System call in Linux

What is Linux A modern, open-source OS, based on UNIX standards 1991, 0.1 MLOC, single developer Linus Torvalds wrote from scratch Main design goal: UNIX compatibility Now, 10 MLOC, developers worldwide Unique source code management model Linux distributions: ubuntu, redhat, fedora, Gentoo, CentOS, Kernel is Linux Different set of user applications and package management systems

Linux Licensing The GNU General Public License (GPL) Anyone creating their own derivative of Linux may not make the derived product proprietary; software released under GPL may not be redistributed as a binary-only product

Linux kernel structure Applications System Libraries (libc) System Call Interface Modules I/O Related File Systems Networking Device Drivers Process Related Scheduler Memory Management IPC Architecture-Dependent Code Hardware

Linux kernel structure (cont.) Core + dynamically loaded modules E.g., device drivers, file systems, network protocols Modules were originally developed to support the conditional inclusion of device drivers Early OS has to include code for all possible device or be recompiled to add support for a new device Modules are now used extensively Standard way to add new functionalities to kernel Reasonably well designed kernel-module interface

Linux kernel source Download: kernel.org Browse: lxr.linux.no (with cross reference) Directory structure include: public headers kernel: core kernel components (e.g., scheduler) arch: hardware-dependent code fs: file systems mm: memory management ipc: inter-process communication drivers: device drivers usr: user-space code lib: common libraries

Outline Linux overview Interrupt in Linux System call in Linux

Privilege level Supports four rings (privilege levels); most modern kernels use only two level ring 3: user mode ring 0: kernel mode CPU keeps track of the current privilege level (CPL) using the cs segment register In Linux USER_CS: selector for user code segment KERNEL_CS: selector for kernel code segment include/asm-i386/segment.h

Memory protection Segmentation: physical memory is organized as variable-size segments Paging: physical memory is organized as equalsize pages The (simplified) idea: memory is associated with descriptor privilege level (DPL) if CPL <= DPL, access okay

Interrupt classification Interrupts, asynchronous from device Maskable interrupts Non-Maskable interrupts (NMI): hardware error Exceptions, synchronous from CPU Intel manual used a bunch of different terms Faults: instruction illegal to execute Often correctable and instruction retried Traps: instruction intends to switch control to kernel Resume from the next instruction

Interrupt number assignment Total 255 possible interrupts 0-31: reserved for non-maskable interrupt 0: division by 0 3: breakpoint 14: page fault Remaining 224: programmable by OS 0x80: Linux interrupt

Interrupt descriptor table Gate descriptor Preventing user code from triggering random interrupts On Trap, if CPL <= Gate DPL, access ok

Seting up IDT in Linux Initialization Start by setting all descriptors to ignore_int() Then, set up the gate descriptors arch/i386/kernel/traps.c

Linux Lingo Linux interrupt gate: Intel interrupt, from device DPL = 0 Disable interrupt set_intr_gate(2, &nmi) System gate: Intel trap, instruction intends to trigger interrupt DPL = 3 Often disable interrupt set_system_gate(syscall_vector, &system_call) Trap gate: Intel fault, instruction illegal DPL = 0 set_trap_gate(0, &divide_error)

Outline Linux overview Interrupt in Linux System call in Linux

Linux system call overview { printf( hello world!\n ); } User mode libc %eax = sys_write; int 0x80 kernel mode 0x80 IDT system_call() { fn = syscalls[%eax] } sys_write( ) { // do real work } syscalls table

Syscall wrapper macros Macros with name _syscalln(), where N is the number of system call parameters _syscalln(return_type, name, arg1type, arg1name, ) in linux-2.6.11/include/asm-i386/unistd.h Macro will expands to a wrapper function Example: long open(const char *filename, int flags, int mode); _syscall3(long, open, const char *, filename, int, flags, int, mode) Note: _syscalln obsolete after 2.6.18; now syscall ( ), can take different # of args

Lib call/syscall return codes Library calls return -1 on error and place a specific error code in the global variable errno System calls return specific negative values to indicate an error Most system calls return errno The library wrapper code is responsible for conforming the return values to the errno convention

System call dispatch (arch/i386/kernel/entry.s).section.text system_call: // copy parameters from registers onto stack call sys_call_table(, %eax, 4) jmp ret_from_sys_call ret_from_sys_call: // perform rescheduling and signal-handling iret // return to caller (in user-mode) // File arch/i386/kernel/entry.s Why jump table? Can t we use if-then-else?

The system-call jump-table There are approximately 300 system-calls Any specific system-call is selected by its IDnumber (it s placed into register %eax) It would be inefficient to use if-else tests to transfer to the service-routine s entry-point Instead an array of function-pointers is directly accessed (using the ID-number) This array is named sys_call_table[] Defined in file arch/i386/kernel/entry.s

System call table definition.section.data sys_call_table:.long sys_restart_syscall.long sys_exit.long sys_fork.long sys_read.long sys_write NOTE: should avoid reusing syscall numbers (why?); deprecated syscalls are implemented by a special not implemented syscall (sys_ni_syscall)

Syscall naming convention Usually a library function foo() will do some work and then call a system call ( sys_foo() ) In Linux, all system calls begin with sys_ Reverse is not true Often sys_foo() just does some simple error checking and then calls a worker function named do_foo()

Tracing System Calls Use the strace command (man strace for info) Linux has a powerful mechanism for tracing system call execution for a compiled application Output is printed for each system call as it is executed, including parameters and return codes The ptrace() system call is used to implement strace Also used by debuggers (breakpoint, singlestep, etc) You can trace library calls using the ltrace command

Passing system call parameters The first parameter is always the syscall # eax on Intel Linux allows up to six additional parameters ebx, ecx, edx, esi, edi, ebp on Intel System calls that require more parameters package the remaining parameters in a struct and pass a pointer to that struct as the sixth parameter Problem: must validate pointers Could be invalid, e.g. NULL crash OS Or worse, could point to OS, device memory security hole

How to validate user pointers? Too expensive to do a thorough check Must check that the pointer is within all valid memory regions of the calling process Solution: no comprehensive check, but users have to use paranoid routines to access user pointers

Paranoid functions to access user pointers Function get_user(), get_user() put_user(), put_user() copy_from_user(), copy_from_user copy_to_user(), copy_to_user() strncpy_from_user(), strncpy_from_user() strnlen_user(), strnlen_user() clear_user(), clear_user() Action reads integer (1,2,4 bytes) writes integer (1,2,4 bytes) copy a block from user space copy a block to user space copies null-terminated string from user space returns length of null-terminated string in user space fills memory area with zeros

Intel Fast System Calls int 0x80 not used any more (I lied ) Intel has a hardware optimization (sysenter) that provides an optimized system call invocation Read the gory details in ULK Chapter 10

Next lecture Process Homework 2 will be out tonight