Using our device-drivers How can an application display the data in a device special file?

Slides:



Advertisements
Similar presentations
Linux device-driver issues
Advertisements

The UNIX File System Harry Chen Department of CSEE University of MD Baltimore County.
Memory Protection: Kernel and User Address Spaces  Background  Address binding  How memory protection is achieved.
Slide 2-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Using the Operating System 2.
Using VMX within Linux We explore the feasibility of executing ROM-BIOS code within the Linux x86_64 kernel.
Chapter 10: File-System Interface
COSC 120 Computer Programming
Engineering Problem Solving With C++ An Object Based Approach Fundamental Concepts Chapter 1 Engineering Problem Solving.
1 Engineering Problem Solving With C++ An Object Based Approach Fundamental Concepts Chapter 1 Engineering Problem Solving.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Embedded Real-time Systems The Linux kernel. The Operating System Kernel Resident in memory, privileged mode System calls offer general purpose services.
Linux+ Guide to Linux Certification, Second Edition
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition by Tony Gaddis, Judy Walters,
1 Device Management The von Neumann Architecture System Architecture Device Management Polling Interrupts DMA operating systems.
Portability CPSC 315 – Programming Studio Spring 2008 Material from The Practice of Programming, by Pike and Kernighan.
UNIX chapter 03 Getting Started Mr. Mohammad Smirat.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 1 Introduction.
Practical Session No. 10 Input &Output (I/O). I/O Devices Input/output (I/O) devices provide the means to interact with the “outside world”. An I/O device.
Chapter 13: I/O Systems I/O Hardware Application I/O Interface
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
Chapter 2 Software Tools and Assembly Language Syntax.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 1 Introduction to Computers and Programming.
Why Program? Computer – programmable machine designed to follow instructions Program – instructions in computer memory to make it do something Programmer.
Chapter Introduction to Computers and Programming 1.
CSC 125 Introduction to C++ Programming Chapter 1 Introduction to Computers and Programming.
DOS Understanding what you can do. Operating System Traits An OS only works with one type of processor –X86 processors for us; Motorola for Mac –Must.
System Calls 1.
Systems Software & Operating systems
CS 0004 –Lecture 1 Wednesday, Jan 5 th, 2011 Roxana Gheorghiu.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Chapter 1: Introduction to Computers and Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Chapter Three The UNIX Editors. 2 Lesson A The vi Editor.
Recall: Three I/O Methods Synchronous: Wait for I/O operation to complete. Asynchronous: Post I/O request and switch to other work. DMA (Direct Memory.
Computer Engineering 1 nd Semester Dr. Rabie A. Ramadan 2.
Fundamental Programming: Fundamental Programming K.Chinnasarn, Ph.D.
Operating Systems COMP 4850/CISG 5550 File Systems Files Dr. James Money.
Mrs. Ulshafer August, 2013 Java Programming Chapter 1.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Memory: Relocation.
Manage Directories and Files in Linux. 2 Objectives Understand the Filesystem Hierarchy Standard (FHS) Identify File Types in the Linux System Change.
CNIT 127: Exploit Development Ch 3: Shellcode. Topics Protection rings Syscalls Shellcode nasm Assembler ld GNU Linker objdump to see contents of object.
Chapter Three The UNIX Editors.
We will focus on operating system concepts What does it do? How is it implemented? Apply to Windows, Linux, Unix, Solaris, Mac OS X. Will discuss differences.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
Implementing ‘noecho’ Programming details regarding the Linux implementation for ‘struct termios’ objects.
Device Driver Concepts Digital UNIX Internals II Device Driver Concepts Chapter 13.
NCurses Tutorial Filiz Şenyüzlüler Güzelbey. What is nCurses? Ncurses is a library, which is used for creating and using windows, screen painting and.
Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems.
Brief Version of Starting Out with C++ Chapter 1 Introduction to Computers and Programming.
Lecture 5 Rootkits Hoglund/Butler (Chapters 1-3).
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Introduction to Computer Programming Concepts M. Uyguroğlu R. Uyguroğlu.
1 Chapter 2: Operating-System Structures Services Interface provided to users & programmers –System calls (programmer access) –User level access to system.
CSCE451/851 Introduction to Operating Systems
Computer System Structures
Chapter 1: Introduction to Computers and Programming
Binary Representation in Text
Clocks, I/O devices, Thin Clients, and Power Management
Protection of System Resources
Guide To UNIX Using Linux Third Edition
Computer Engineering 1nd Semester
Chapter 1: Introduction to Computers and Programming
CSCI 315 Operating Systems Design
Computer Science I CSC 135.
I/O Systems I/O Hardware Application I/O Interface
Memory Management Tasks
Introduction to Computer Systems
Chapter 1: Introduction to Computers and Programming
Presentation transcript:

Using our device-drivers How can an application display the data in a device special file?

The ‘fileview.cpp’ application Purpose: a tool for viewing arbitrary files Some files have ascii text Other files have binary data So display in both hexadecimal and ascii Some ascii characters are ‘control codes’ Some bytes are not ascii characters at all So show a substitute mark as necessary

Easy file navigation Files may be quite large Can only fit small pieces on one screen And only some parts may be of interest Also some hex formats can be confusing Need a way to find what we want to see Need it to remain visible while we read it Need flexibility to adjust display format

The ‘curses’ library Offers users an ‘interactive’ interface Allows use of normal keyboard input Allows use of a mouse Allows use of ‘windows’ and ‘colors’ Allows use cursor-keys for navigation Allows use of ‘unbuffered’ keystrokes Offers instant refresh of display screen

Concept behind ‘curses’ Works on screens, windows, subwindows Maintains several internal data-structures Two of these are ‘stdscr’ and ‘curscr’ Both are ‘maps’ of your physical screen Drawing is done to the ‘stdscr’ window But it doesn’t show up immediately ‘refresh()’: compares ‘stdscr’ to ‘curscr’ and then only copies the parts that are ‘new’

Using ‘ncurses’ with Linux #include ‘initscr()’ initializes library data-structures ‘noecho()’ turns off keystroke echoing ‘raw()’ and ‘cbreak()’ set special behaviors ‘refresh()’ updates the display screen ‘endwin()’ restores normal tty-behavior

The typical ‘curses’ program #include int main( int argc, char **argv ) { initscr(); … endwin(); }

Compiling ‘ncurses’ programs Must use the ‘-l’ command-line switch: Example: $ g++ fileview.cpp –lncurses –o fileview This is lowercase ‘L’ (not uppercase ‘i’) It means link with ‘libncurses.so’ library Object libraries are in directory: ‘/usr/lib’

Some frequent functions ‘clear()’ ‘move()’ ‘printw()’ ‘mvprintw()’ ‘refresh()’ ‘newwin()’ ‘box()’

Reference Good introductory tutorial appears in: Richard Stones and Neil Matthew, “Beginning Linux Programming (2 nd Ed.)” (WROX Press Ltd, 1999), Chapter 6.

Unusual goal of ‘fileview.cpp’ Wanted to look at VERY large device-files Eample:/dev/hda This device file represents the hard disk Size of today’s hard disk could be 180GB! So file-positions could not be of type ‘long’ Linux introduces a 64-bit type: loff_t Kernel offers ‘sys_llseek()’ system-call

Linux uses ‘glibc’ C-library The Gnu version of Standard C Library ‘glibc’ implements system-call interfaces for the standard UNIX C functions, like open(), close(), read(), write(), and lseek() But Gnu C Library omitted ‘llseek()’ So can’t do seek-operations on big files!

Calling ‘llseek()’ anyway Programmers can call the kernel directly Can bypass the ‘glibc’ Standard C Library But need to obey system-call conventions Transition from user-mode to kernel-mode Requires use of a special CPU instruction This instruction is ‘architecture-specific’ For Pentium CPU: asm(“ int $0x80 “);

Some ‘macros’ make it easier #include Calling ‘sys_llseek()’ requires 5 arguments Arguments must go into CPU registers ‘sys_call_table[ ]’ array-index goes in EAX The macro to use is named ‘_syscall5’

A programming ‘bug’ Standard C ‘read()’ function returns ‘int’ Meaning of the ‘read()’ return-value: retval > 0: ‘retval’ bytes successfully read retval = 0 : end-of-file reached, so no data retval < 0: some error prevented reading Return-value wasn’t checked in ‘fileview’!

Why wasn’t ‘bug’ found? ‘fileview’ always tried to read 256 bytes Never tried to read beyond ‘end-of-file’ Never tried to read data that ‘wasn’t there’ So no reason why retval wouldn’t be 256

But project #3 is different ‘ram.c’ must read ALL physical pages ‘high memory’ pages not always ‘mapped’ And pages are not ‘mapped’ contiguously 256 bytes could cross a page-boundary –Starting address: 0x00000F80 –Ending address:0x So some ‘read()’ errors could easily occur

How can ‘bug’ be fixed? Several solutions are possible Best to try minimizing the code-changes Should focus on correct ‘fix’ for all drivers Obey rules for driver ‘read()’ functions

Some pseudo-code int my_read( int fd, char *cp, int count ) { intmore = count; while ( more ) { intn = read( fd, cp, more ); if ( n <= 0 ) return n; cp += n; more -= n; } returncount; }

Recommendations Consult “Linux Device Drivers” text ‘read()’ method is described on page 80 Do everything that’s actually necessary But keep your code as simple as possible During development: use ‘printk()’ output For ‘release version’: omit ‘printk()’ output

Exercise Write a ‘hello world’ program in C But don’t use ANY header-files!! (i.e., bypass the Standard C Library) How? 1) setup static message-string 2) setup registers with parameters 3) use ‘int 0x80’ to enter the kernel

‘sys_write( int fd, void *msg, int n)’ The system-call number is 4 The STDOUT file-descriptor is 1 The message-string address is $msg The message-length (you count the bytes) These 4 arguments go in CPU registers: EAX, EBX, ECX, EDX (in that order) So your ‘main()’ needs only 5 instructions!