35
I/O Subsystem Overview of I/O Hardware Access Overview of I/O Hardware Access Device Independent Routines Device Independent Routines Device Dependent Routines Device Dependent Routines Device Device io.ppt CS 105 “Tour of the Black Holes of Computing”

I/O Subsystem Overview of I/O Hardware Access Device Independent Routines Device Dependent Routines Device io.ppt CS 105 “Tour of the Black Holes of Computing”

Embed Size (px)

Citation preview

Page 1: I/O Subsystem Overview of I/O Hardware Access Device Independent Routines Device Dependent Routines Device io.ppt CS 105 “Tour of the Black Holes of Computing”

I/O Subsystem I/O Subsystem

Overview of I/O Hardware AccessOverview of I/O Hardware Access

Device Independent RoutinesDevice Independent Routines

Device Dependent RoutinesDevice Dependent Routines

DeviceDevice

io.ppt

CS 105“Tour of the Black Holes of Computing”

Page 2: I/O Subsystem Overview of I/O Hardware Access Device Independent Routines Device Dependent Routines Device io.ppt CS 105 “Tour of the Black Holes of Computing”

– I/O – CS 105

I/O: A Typical Hardware SystemI/O: A Typical Hardware System

mainmemory

I/O bridge

bus interface

ALU

register file

CPU chip

system bus memory bus

disk controller

graphicsadapter

USBcontroller

mousekeyboard monitor

disk

I/O bus Expansion slots forother devices suchas network adapters.

Page 3: I/O Subsystem Overview of I/O Hardware Access Device Independent Routines Device Dependent Routines Device io.ppt CS 105 “Tour of the Black Holes of Computing”

– I/O – CS 105

Reading a Disk Sector: Step 1Reading a Disk Sector: Step 1

mainmemory

ALU

register file

CPU chip

disk controller

graphicsadapter

USBcontroller

mousekeyboard monitor

disk

I/O bus

bus interface

CPU initiates a disk read by writing a command, logical block number, and destination memory address to a port (address) associated with disk controller.

Page 4: I/O Subsystem Overview of I/O Hardware Access Device Independent Routines Device Dependent Routines Device io.ppt CS 105 “Tour of the Black Holes of Computing”

– I/O – CS 105

Reading a Disk Sector: Step 2Reading a Disk Sector: Step 2

mainmemory

ALU

register file

CPU chip

disk controller

graphicsadapter

USBcontroller

mousekeyboard monitor

disk

I/O bus

bus interface

Disk controller reads the sector and performs a direct memory access (DMA) transfer into main memory.

Page 5: I/O Subsystem Overview of I/O Hardware Access Device Independent Routines Device Dependent Routines Device io.ppt CS 105 “Tour of the Black Holes of Computing”

– I/O – CS 105

Reading a Disk Sector: Step 3Reading a Disk Sector: Step 3

mainmemory

ALU

register file

CPU chip

disk controller

graphicsadapter

USBcontroller

mousekeyboard monitor

disk

I/O bus

bus interface

When the DMA transfer completes, the disk controller notifies the CPU with an interrupt (i.e., asserts a special “interrupt” pin on the CPU)

Page 6: I/O Subsystem Overview of I/O Hardware Access Device Independent Routines Device Dependent Routines Device io.ppt CS 105 “Tour of the Black Holes of Computing”

– I/O – CS 105

Abstracting I/OAbstracting I/OLow level requires complex device commandsLow level requires complex device commands

Vary from device to device Device models can be very different

Tape: read or write sequentially, or rewindDisk: “random” access at block levelTerminal: sequential, no rewind, must echo and allow editingVideo: write-only, with 2-dimensional structure

Operating system should hide these differencesOperating system should hide these differences “read” and “write” should work regardless of device

OS builds table of specific driver routines for each device

Sometimes impossible to generalize (e.g., video) Still need access to full power of hardware

Page 7: I/O Subsystem Overview of I/O Hardware Access Device Independent Routines Device Dependent Routines Device io.ppt CS 105 “Tour of the Black Holes of Computing”

– I/O – CS 105

Layers of I/O SystemLayers of I/O System1.1. User Level – processesUser Level – processes

• make I/O call, format I/O, spooling

2.2. Device-Independent softwareDevice-Independent software

• naming, protection blocking, buffering, allocation• perform I/O functions common to all devices• provide uniform interface to the user level software• buffering, block sizes, etc.• e.g., map symbolic device names onto driver

3.3. Device-Dependent softwareDevice-Dependent software

• setup device registers, check status• specific code for device operations, i.e., driver routines for read,

write, etc.• accept abstract requests, e.g., open, and execute

4.4. HardwareHardware

• controller, map I/O ops to device ops, e.g., print,• Interrupts

Page 8: I/O Subsystem Overview of I/O Hardware Access Device Independent Routines Device Dependent Routines Device io.ppt CS 105 “Tour of the Black Holes of Computing”

– I/O – CS 105

UNIX I/OUNIX I/ODerivDerived from Multics and earlier systems, the Unix ed from Multics and earlier systems, the Unix

I/O primitives follow a paradigm referred as I/O primitives follow a paradigm referred as ““open-read-write-closeopen-read-write-close””. Before a user process can . Before a user process can perform I/O ops, it calls perform I/O ops, it calls ‘‘openopen’’ to specify the to specify the file to be used and obtains permission. The call file to be used and obtains permission. The call to to ‘‘openopen’’ returns a small integer, returns a small integer, ‘‘file file descriptordescriptor’’ that the process uses when performing that the process uses when performing I/O ops on the opened file or device. Once an I/O ops on the opened file or device. Once an object has been opened, the user process makes one object has been opened, the user process makes one or more or more ‘‘readread’’ or or ‘‘writewrite’’ calls to read or write calls to read or write data. Both data. Both ‘‘readread’’ and and ‘‘writewrite’’ take 3 arguments take 3 arguments that specify the file descriptor, address of a that specify the file descriptor, address of a buffer, and count of bytes to be transferred. buffer, and count of bytes to be transferred. After all transfer operations are complete, the After all transfer operations are complete, the user process calls user process calls ‘‘closeclose’’ to inform the OS that to inform the OS that the process is finished with the object.the process is finished with the object.

Page 9: I/O Subsystem Overview of I/O Hardware Access Device Independent Routines Device Dependent Routines Device io.ppt CS 105 “Tour of the Black Holes of Computing”

– I/O – CS 105

Layers of I/O SystemLayers of I/O System1.1. User Level – processesUser Level – processes

• make I/O call, format I/O, spooling

2.2. Device-Independent softwareDevice-Independent software

• naming, protection blocking, buffering, allocation• perform I/O functions common to all devices• provide uniform interface to the user level software• buffering, block sizes, etc.• e.g., map symbolic device names onto driver

3.3. Device-Dependent softwareDevice-Dependent software

• setup device registers, check status• specific code for device operations, i.e., driver• accept abstract requests, e.g., open, and execute

4.4. HardwareHardware

• controller, map I/O ops to device ops, e.g., print,• Interrupts

Page 10: I/O Subsystem Overview of I/O Hardware Access Device Independent Routines Device Dependent Routines Device io.ppt CS 105 “Tour of the Black Holes of Computing”

– I/O – CS 105

Device Independent: Dev TableDevice Independent: Dev Tablestruct dvtype {struct dvtype {

char *dvname; /* device name */char *dvname; /* device name */

char *dvtname; /* type name */char *dvtname; /* type name */

int dvtnum; /* symbol table index of type */int dvtnum; /* symbol table index of type */

char *dvdevice; /* device name */char *dvdevice; /* device name */

int dvcsr; /* Control Status Register addr */int dvcsr; /* Control Status Register addr */

int dvivec; /* input interrupt vector */int dvivec; /* input interrupt vector */

int dvovec; /* Output interrupt vector */int dvovec; /* Output interrupt vector */

char dviint[20]; /* input interrupt routine */char dviint[20]; /* input interrupt routine */

char dvoint[20]; /* output interrupt routine */char dvoint[20]; /* output interrupt routine */

char dvinit[20]; /* init routine name */char dvinit[20]; /* init routine name */

char dvopen[20]; /* open routine name */char dvopen[20]; /* open routine name */

char dvclose[20]; /* close routine name */char dvclose[20]; /* close routine name */

char dvread[20]; /* read routine name */char dvread[20]; /* read routine name */

char dvwrite[20]; /* write routine name */char dvwrite[20]; /* write routine name */

char dvcntl[20]; /* control routine name */char dvcntl[20]; /* control routine name */

char dvseek[20]; /* seek routine name */char dvseek[20]; /* seek routine name */

char dvgetc[20]; /* getc routine name */char dvgetc[20]; /* getc routine name */

char dvputc[20]; /* putc routine name */char dvputc[20]; /* putc routine name */

int dvminor; /* device number 0,1,... */int dvminor; /* device number 0,1,... */

struct dvtype *dvnext; /* next node on the list struct dvtype *dvnext; /* next node on the list */*/

};};

typedef struct dvtype *dvptr;typedef struct dvtype *dvptr;

dvptr ftypes = NIL; /* linked list of device types */dvptr ftypes = NIL; /* linked list of device types */

dvptr devs = NIL; /* linked list of device decls. */dvptr devs = NIL; /* linked list of device decls. */

dvptr lastdv = NIL;dvptr lastdv = NIL;

dvptr currtype = NIL;dvptr currtype = NIL;

• struct dvtype is common to all devices

• uses linked list of dytype

• allows dynamic creation of new dvtype(device)

Page 11: I/O Subsystem Overview of I/O Hardware Access Device Independent Routines Device Dependent Routines Device io.ppt CS 105 “Tour of the Black Holes of Computing”

– I/O – CS 105

Device Independent: init.cDevice Independent: init.c/* init.c - init */ /* init.c - init */ /*include files do not appear in following modules *//*include files do not appear in following modules */

#include <conf.h>#include <conf.h>

#include <kernel.h>#include <kernel.h>

#include <io.h>#include <io.h>

/*------------------------------------------------------------------------/*------------------------------------------------------------------------

* init - initialize a device* init - initialize a device

• could be done by the OS, system level setup of the devicecould be done by the OS, system level setup of the device

• done at boot time or when first accesseddone at boot time or when first accessed

*------------------------------------------------------------------------*------------------------------------------------------------------------

*/*/

init(descrp)init(descrp)

int descrp;int descrp;

{{

structstruct devswdevsw *devptr;*devptr;

if (isbaddev(descrp) )if (isbaddev(descrp) )

return(SYSERR);return(SYSERR);

devptr = &devtab[descrp]; devptr = &devtab[descrp]; // get pointer to device struct// get pointer to device struct

return( (*devptr->dvinit)(devptr) ); return( (*devptr->dvinit)(devptr) ); // run device// run device’’s init routines init routine

}}

Page 12: I/O Subsystem Overview of I/O Hardware Access Device Independent Routines Device Dependent Routines Device io.ppt CS 105 “Tour of the Black Holes of Computing”

– I/O – CS 105

Device Independent: control.cDevice Independent: control.c//* control.c - control */* control.c - control */

/*------------------------------------------------------------------------/*------------------------------------------------------------------------

* control - control a device (e.g., set the mode)* control - control a device (e.g., set the mode)

* OS level operation, * OS level operation,

*------------------------------------------------------------------------*------------------------------------------------------------------------

*/*/

SYSCALLSYSCALL control(descrp, func, addr, addr2)control(descrp, func, addr, addr2)

int descrp, func;int descrp, func;

char *addr,*addr2;char *addr,*addr2;

{{

structstruct devswdevsw *devptr;*devptr;

if (isbaddev(descrp) )if (isbaddev(descrp) )

return(SYSERR);return(SYSERR);

devptr = &devtab[descrp];devptr = &devtab[descrp];

return(return( (*devptr->dvcntl)(devptr, func, addr, addr2) ); (*devptr->dvcntl)(devptr, func, addr, addr2) ); ////’’ run device run device’’s cntl routine passing parmss cntl routine passing parms

}}

Page 13: I/O Subsystem Overview of I/O Hardware Access Device Independent Routines Device Dependent Routines Device io.ppt CS 105 “Tour of the Black Holes of Computing”

– I/O – CS 105

Device Independent: open.cDevice Independent: open.c/* open.c - open *//* open.c - open */

/*------------------------------------------------------------------------/*------------------------------------------------------------------------

* open - open a connection to a device/file (parms 2 &3 are optional)* open - open a connection to a device/file (parms 2 &3 are optional)

*------------------------------------------------------------------------*------------------------------------------------------------------------

*/*/

SYSCALL open(descrp, nam, mode)SYSCALL open(descrp, nam, mode)

int descrp;int descrp;

char *nam;char *nam;

char *mode;char *mode;

{{

struct devsw *devptr;struct devsw *devptr;

if ( isbaddev(descrp) )if ( isbaddev(descrp) )

return(SYSERR);return(SYSERR);

devptr = &devtab[descrp]; devptr = &devtab[descrp]; //get a pointer to the appropriate device structure in device //get a pointer to the appropriate device structure in device tabletable

return( (*devptr->dvopen)(devptr, nam, mode) ); return( (*devptr->dvopen)(devptr, nam, mode) ); // use device pointer to call device specific // use device pointer to call device specific openopen

}}

Page 14: I/O Subsystem Overview of I/O Hardware Access Device Independent Routines Device Dependent Routines Device io.ppt CS 105 “Tour of the Black Holes of Computing”

– I/O – CS 105

Device Independent: getc.cDevice Independent: getc.c/* getc.c - getc *//* getc.c - getc */

/*------------------------------------------------------------------------/*------------------------------------------------------------------------

* getc - get one character from a device* getc - get one character from a device

*------------------------------------------------------------------------*------------------------------------------------------------------------

*/*/

SYSCALLSYSCALL getc(descrp)getc(descrp)

int descrp;int descrp;

{{

structstruct devswdevsw *devptr;*devptr;

if (isbaddev(descrp) )if (isbaddev(descrp) )

return(SYSERR);return(SYSERR);

devptr = &devtab[descrp]devptr = &devtab[descrp]; // get pointer to device structure; // get pointer to device structure

return( (*devptr->dvgetc)(devptr) ); return( (*devptr->dvgetc)(devptr) ); // run device// run device’’s getcs getc

}}

Page 15: I/O Subsystem Overview of I/O Hardware Access Device Independent Routines Device Dependent Routines Device io.ppt CS 105 “Tour of the Black Holes of Computing”

– I/O – CS 105

Device Independent: putc.cDevice Independent: putc.c/* putc.c - putc *//* putc.c - putc */

/*------------------------------------------------------------------------/*------------------------------------------------------------------------

* putc - write a single character to a device* putc - write a single character to a device

*------------------------------------------------------------------------*------------------------------------------------------------------------

*/*/

SYSCALLSYSCALL putc(descrp, ch)putc(descrp, ch)

int descrp;int descrp;

char ch;char ch;

{{

structstruct devswdevsw *devptr;*devptr;

if (isbaddevif (isbaddev (descrp) )(descrp) )

return(SYSERR);return(SYSERR);

devptr = &devtab[descrp];devptr = &devtab[descrp];

return(return( (*devptr->dvputc)(devptr,ch) );(*devptr->dvputc)(devptr,ch) );

}}

Page 16: I/O Subsystem Overview of I/O Hardware Access Device Independent Routines Device Dependent Routines Device io.ppt CS 105 “Tour of the Black Holes of Computing”

– I/O – CS 105

Device Independent: seek.cDevice Independent: seek.c/* seek.c seek *//* seek.c seek */

/* seek -- position a device (very common special case of control) *//* seek -- position a device (very common special case of control) */

SYSCALLSYSCALL seek(descrp, pos)seek(descrp, pos)

int descrp;int descrp;

long pos;long pos;

{{

structstruct devswdevsw *devptr;*devptr;

if (isbaddev(descrp) )if (isbaddev(descrp) )

return(SYSERR);return(SYSERR);

devptr = &devtab[descrp]; devptr = &devtab[descrp]; // get pointer to devices structure// get pointer to devices structure

return( (*devptr->dvseek)(devptr,pos) ); return( (*devptr->dvseek)(devptr,pos) ); // run device// run device’’s seek function, which s seek function, which may be a noopmay be a noop

}}

Page 17: I/O Subsystem Overview of I/O Hardware Access Device Independent Routines Device Dependent Routines Device io.ppt CS 105 “Tour of the Black Holes of Computing”

– I/O – CS 105

Device Dependent: ionull.cDevice Dependent: ionull.c/* ionull.c - ionull *//* ionull.c - ionull */

/*------------------------------------------------------------------------/*------------------------------------------------------------------------

* ionull - * ionull - do nothing (used for "don't care" entries in devtab)do nothing (used for "don't care" entries in devtab)

*------------------------------------------------------------------------*------------------------------------------------------------------------

*/*/

ionull()ionull()

{{

return(OK);return(OK);

}}

Page 18: I/O Subsystem Overview of I/O Hardware Access Device Independent Routines Device Dependent Routines Device io.ppt CS 105 “Tour of the Black Holes of Computing”

– I/O – CS 105

Device Independent: read.cDevice Independent: read.c/* read.c - read *//* read.c - read */

/* read - read one or more bytes from a device *//* read - read one or more bytes from a device */

SYSCALLSYSCALL read(descrp, buff, count)read(descrp, buff, count)

int descrp, count;int descrp, count;

char *buff;char *buff;

{{

structstruct devswdevsw *devptr;*devptr;

if (isbaddev(descrp) )if (isbaddev(descrp) )

return(SYSERR);return(SYSERR);

devptr = &devtab[descrp]; // get pointer to devicedevptr = &devtab[descrp]; // get pointer to device’’s IO structures IO structure

return(return( (*devptr->dvread)(devptr,buff,count) ); (*devptr->dvread)(devptr,buff,count) ); // device// device’’s read may be an s read may be an ‘‘errorerror’’, e.g., on , e.g., on keyboardkeyboard

}}

Page 19: I/O Subsystem Overview of I/O Hardware Access Device Independent Routines Device Dependent Routines Device io.ppt CS 105 “Tour of the Black Holes of Computing”

– I/O – CS 105

Device Independent: write.cDevice Independent: write.c/* write.c - write *//* write.c - write */

/*------------------------------------------------------------------------/*------------------------------------------------------------------------

* write - write 1 or more bytes to a device* write - write 1 or more bytes to a device

*------------------------------------------------------------------------*------------------------------------------------------------------------

*/*/

SYSCALLSYSCALL write(descrp, buff, count)write(descrp, buff, count)

int descrp, count;int descrp, count;

char *buff;char *buff;

{{

structstruct devswdevsw *devptr;*devptr;

if (isbaddev(descrp) )if (isbaddev(descrp) )

return(SYSERR);return(SYSERR);

devptr = &devtab[descrp];devptr = &devtab[descrp];

return(return( (*devptr->dvwrite)(devptr,buff,count) );(*devptr->dvwrite)(devptr,buff,count) );

}}

Page 20: I/O Subsystem Overview of I/O Hardware Access Device Independent Routines Device Dependent Routines Device io.ppt CS 105 “Tour of the Black Holes of Computing”

– I/O – CS 105

Device Independent: closeDevice Independent: close/* close.c - close *//* close.c - close */

/*------------------------------------------------------------------------/*------------------------------------------------------------------------

* close - close a device* close - close a device

*------------------------------------------------------------------------*------------------------------------------------------------------------

*/*/

SYSCALLSYSCALL close(descrp)close(descrp)

int descrp;int descrp;

{{

structstruct devswdevsw *devptr;*devptr;

if (isbaddev(descrp) )if (isbaddev(descrp) )

return(SYSERR);return(SYSERR);

devptr = &devtab[descrp];devptr = &devtab[descrp];

return( (*devptr->dvclose)(devptr));return( (*devptr->dvclose)(devptr));

}}

Page 21: I/O Subsystem Overview of I/O Hardware Access Device Independent Routines Device Dependent Routines Device io.ppt CS 105 “Tour of the Black Holes of Computing”

– I/O – CS 105

Layers of I/O SystemLayers of I/O System1.1. User Level – processesUser Level – processes

• make I/O call, format I/O, spooling

2.2. Device-Independent softwareDevice-Independent software

• naming, protection blocking, buffering, allocation• perform I/O functions common to all devices• provide uniform interface to the user level software• buffering, block sizes, etc.• e.g., map symbolic device names onto driver

3.3. Device-Dependent softwareDevice-Dependent software

• setup device registers, check status• specific code for device operations, i.e., driver• accept abstract requests, e.g., open, and execute

4.4. HardwareHardware

• Controller. map I/O ops to device ops, e.g., print,• Interrupts

Page 22: I/O Subsystem Overview of I/O Hardware Access Device Independent Routines Device Dependent Routines Device io.ppt CS 105 “Tour of the Black Holes of Computing”

– I/O – CS 105

Device Dependent: slu.hDevice Dependent: slu.h/* slu.h - standard serial line unit device constants *//* slu.h - standard serial line unit device constants */

#define SLUENABLE 0100 /* device interrupt enable bit */#define SLUENABLE 0100 /* device interrupt enable bit */

#define SLUREADY 0200 /* device ready bit */#define SLUREADY 0200 /* device ready bit */

#define SLUDISABLE 0000 /* device interrupt disable mask*/#define SLUDISABLE 0000 /* device interrupt disable mask*/

#define SLUTBREAK 0001 /* transmitter break-mode bit */#define SLUTBREAK 0001 /* transmitter break-mode bit */

#define SLUERMASK 0170000 /* mask for error flags on input*/#define SLUERMASK 0170000 /* mask for error flags on input*/

#define SLUCHMASK 0377 /* mask for input character */#define SLUCHMASK 0377 /* mask for input character */

/* SLU device register layout and correspondence to vendor's names *//* SLU device register layout and correspondence to vendor's names */

struct csr {struct csr {

int crstat; /* receiver control and status (RCSR) */int crstat; /* receiver control and status (RCSR) */

int crbuf; /* receiver data buffer (RBUF) */int crbuf; /* receiver data buffer (RBUF) */

int ctstat; /* transmitter control & status (XCSR) */int ctstat; /* transmitter control & status (XCSR) */

int ctbuf; /* transmitter data buffer (XBUF) */int ctbuf; /* transmitter data buffer (XBUF) */

};};

// memory mapped IO// memory mapped IO

// real registers// real registers

Page 23: I/O Subsystem Overview of I/O Hardware Access Device Independent Routines Device Dependent Routines Device io.ppt CS 105 “Tour of the Black Holes of Computing”

– I/O – CS 105

Layers of I/O SystemLayers of I/O System1.1. User Level – processesUser Level – processes

• make I/O call, format I/O, spooling

2.2. Device-Independent softwareDevice-Independent software

• naming, protection blocking, buffering, allocation• perform I/O functions common to all devices• provide uniform interface to the user level software• buffering, block sizes, etc.• e.g., map symbolic device names onto driver

3.3. Device-Dependent softwareDevice-Dependent software

• setup device registers, check status• specific code for device operations, i.e., driver• accept abstract requests, e.g., open, and execute

4.4. HardwareHardware

• controller, map I/O ops to device ops, e.g., print,• Interrupts

Page 24: I/O Subsystem Overview of I/O Hardware Access Device Independent Routines Device Dependent Routines Device io.ppt CS 105 “Tour of the Black Holes of Computing”

– I/O – CS 105

Device Dependent: ttyDevice Dependent: ttyttycntl.cttycntl.c

ttyiin.cttyiin.c

ttyoin.cttyoin.c

ttyputc.cttyputc.c

ttywrite.cttywrite.c

ttygetc.cttygetc.c

ttyinit.cttyinit.c

ttyopen.cttyopen.c

ttyread.cttyread.c

Page 25: I/O Subsystem Overview of I/O Hardware Access Device Independent Routines Device Dependent Routines Device io.ppt CS 105 “Tour of the Black Holes of Computing”

– I/O – CS 105

Device Dependent: tty structDevice Dependent: tty structstruct tty { /* tty line control block */struct tty { /* tty line control block */

// set up input queue// set up input queue

int ihead; int ihead; /* head of input queue *//* head of input queue */

int itailint itail; /* tail of input queue */; /* tail of input queue */

char ibuff[IBUFLEN]; /* input buffer for this line */char ibuff[IBUFLEN]; /* input buffer for this line */

int isem; /* input semaphore */int isem; /* input semaphore */

// set up output queue// set up output queue

int ohead; /* head of output queue */int ohead; /* head of output queue */

int otail; /* tail of output queue */int otail; /* tail of output queue */

char obuff[OBUFLEN]; /* output buffer for this line */char obuff[OBUFLEN]; /* output buffer for this line */

int osem; /* output semaphore */int osem; /* output semaphore */

int odsend; /* sends delayed for space */int odsend; /* sends delayed for space */

…………

Page 26: I/O Subsystem Overview of I/O Hardware Access Device Independent Routines Device Dependent Routines Device io.ppt CS 105 “Tour of the Black Holes of Computing”

– I/O – CS 105

Device Dependent: tty structDevice Dependent: tty struct…………..

/// set up echo queue/// set up echo queue

int ehead; /* head of echo queue */int ehead; /* head of echo queue */

int etail; /* tail of echo queue */int etail; /* tail of echo queue */

char ebuff[EBUFLEN]; /* echo queue */char ebuff[EBUFLEN]; /* echo queue */

char imode; /* IMRAW, IMCBREAK, IMCOOKED char imode; /* IMRAW, IMCBREAK, IMCOOKED */*/

Bool iecho; /* is input echoed? */Bool iecho; /* is input echoed? */

Bool ieback; /* do erasing backspace on echo?*/Bool ieback; /* do erasing backspace on echo?*/

Bool evis; /* echo control chars as ^X ? */Bool evis; /* echo control chars as ^X ? */

Bool ecrlf; /* echo CR-LF for newline? */Bool ecrlf; /* echo CR-LF for newline? */

Bool icrlf; /* map '\r' to '\n' on input? */Bool icrlf; /* map '\r' to '\n' on input? */

Bool ierase; /* honor erase character? */Bool ierase; /* honor erase character? */

……..

RAW – puts characters into ibuff without processingignores all user editingCBREAK – honors all control characters except those related to line editingCOOKED – character echo, honors suspend or restart gets complete lines

Page 27: I/O Subsystem Overview of I/O Hardware Access Device Independent Routines Device Dependent Routines Device io.ppt CS 105 “Tour of the Black Holes of Computing”

– I/O – CS 105

Device Dependent: tty struct cont.Device Dependent: tty struct cont./* struct continuid/* struct continuid

char ierasec; /* erase character (backspace) */char ierasec; /* erase character (backspace) */

Bool ikill; /* honor line kill character? */Bool ikill; /* honor line kill character? */

char ikillc; /* line kill character */char ikillc; /* line kill character */

Bool iintr; /* is interrupt char honored? */Bool iintr; /* is interrupt char honored? */

char iintrc; /* interrupt character */char iintrc; /* interrupt character */

int iintpid; /* interrupt process id */int iintpid; /* interrupt process id */

Bool ieof; /* honor end-of-file char? */Bool ieof; /* honor end-of-file char? */

char ieofc; /* end-of-file character */char ieofc; /* end-of-file character */

int icursor; /* current cursor position */int icursor; /* current cursor position */

Bool oflow; /* honor ostop/ostart? */Bool oflow; /* honor ostop/ostart? */

Bool oheld; /* output currently being held? */Bool oheld; /* output currently being held? */

char ostop; /* character that stops output */char ostop; /* character that stops output */

char ostart; /* character that starts output */char ostart; /* character that starts output */

Bool ocrlf; /* echo CR/LF for LF ? */Bool ocrlf; /* echo CR/LF for LF ? */

char ifullc; /* char to send when input full */char ifullc; /* char to send when input full */

struct csr *ioaddr; /* device address of this unit */struct csr *ioaddr; /* device address of this unit */

};};

extern struct tty tty[];extern struct tty tty[];

Page 28: I/O Subsystem Overview of I/O Hardware Access Device Independent Routines Device Dependent Routines Device io.ppt CS 105 “Tour of the Black Holes of Computing”

– I/O – CS 105

Device Dependent: tty.hDevice Dependent: tty.h#define BACKSP '\b' /* backspace one character pos. */#define BACKSP '\b' /* backspace one character pos. */

#define BELL '\007' /* usually an audiable tone */#define BELL '\007' /* usually an audiable tone */

#define BLANK ' ' /* used to print a "space" */#define BLANK ' ' /* used to print a "space" */

#define EOFC '\004' /* end-of-file character (^D) */#define EOFC '\004' /* end-of-file character (^D) */

#define KILLCH '\025' /* line kill character (^U) */#define KILLCH '\025' /* line kill character (^U) */

#define NEWLINE '\n' /* line feed */#define NEWLINE '\n' /* line feed */

#define RETURN '\r' /* carriage return */#define RETURN '\r' /* carriage return */

#define STOPCH '\023' /* control-S stops output */#define STOPCH '\023' /* control-S stops output */

#define STRTCH '\021' /* control-Q restarts output */#define STRTCH '\021' /* control-Q restarts output */

#define INTRCH '\002' /* control-B is interrupt */#define INTRCH '\002' /* control-B is interrupt */

#define UPARROW '^' /* usually for visuals like ^X */#define UPARROW '^' /* usually for visuals like ^X */

…………....

Page 29: I/O Subsystem Overview of I/O Hardware Access Device Independent Routines Device Dependent Routines Device io.ppt CS 105 “Tour of the Black Holes of Computing”

– I/O – CS 105

Device Dependent: tty.hDevice Dependent: tty.h…………....

//* ttycontrol function codes ** ttycontrol function codes *//

#define TCSETBRK 1 /* turn on BREAK in transmitter */#define TCSETBRK 1 /* turn on BREAK in transmitter */

#define TCRSTBRK 2 /* turn off BREAK " " */#define TCRSTBRK 2 /* turn off BREAK " " */

#define TCNEXTC 3 /* look ahead 1 character */#define TCNEXTC 3 /* look ahead 1 character */

#define TCMODER 4 /* set input mode to raw */#define TCMODER 4 /* set input mode to raw */

#define TCMODEC 5 /* set input mode to cooked */#define TCMODEC 5 /* set input mode to cooked */

#define TCMODEK 6 /* set input mode to cbreak */#define TCMODEK 6 /* set input mode to cbreak */

#define TCICHARS 8 /* return number of input chars */#define TCICHARS 8 /* return number of input chars */

#define TCECHO 9 /* turn on echo */#define TCECHO 9 /* turn on echo */

#define TCNOECHO 10 /* turn off echo */#define TCNOECHO 10 /* turn off echo */

#define TCINT 11 /* set input interrupt pid */#define TCINT 11 /* set input interrupt pid */

#define TCINTCH 12 /* set input interrupt char */#define TCINTCH 12 /* set input interrupt char */

#define TCNOINT 13 /* turn off input interrupt */#define TCNOINT 13 /* turn off input interrupt */

#define TFULLC BELL /* char to echo when buffer full*/#define TFULLC BELL /* char to echo when buffer full*/

Page 30: I/O Subsystem Overview of I/O Hardware Access Device Independent Routines Device Dependent Routines Device io.ppt CS 105 “Tour of the Black Holes of Computing”

– I/O – CS 105

Device Dependent: tty initDevice Dependent: tty init/* ttyinit.c – ttyinit - initialize buffers and modes for a tty line *//* ttyinit.c – ttyinit - initialize buffers and modes for a tty line */

ttyinit(devptr)ttyinit(devptr)

structstruct devswdevsw *devptr;*devptr;

{{

register structregister struct tty *iptr;tty *iptr;

register structregister struct csr *cptr; csr *cptr; //pointer to actual i/o registers//pointer to actual i/o registers

intint junk, isconsole; junk, isconsole;

/* set up interrupt vector and interrupt dispatch table *//* set up interrupt vector and interrupt dispatch table */

iptr = &tty[devptr->dvminor];iptr = &tty[devptr->dvminor];

iosetvec(devptr->dvnum, (int)iptr, (int)iptr);iosetvec(devptr->dvnum, (int)iptr, (int)iptr);

devptr->dvioblk = (char *)iptrdevptr->dvioblk = (char *)iptr /* fill tty control blk/* fill tty control blk */*/

isconsole = (devptr->dvnum == CONSOLE) /* make console cookedisconsole = (devptr->dvnum == CONSOLE) /* make console cooked */*/

iptr->ioaddr = (struct csr *)devptr->dvcsr;/* copy in csr addr.iptr->ioaddr = (struct csr *)devptr->dvcsr;/* copy in csr addr. */*/

iptr->ihead = iptr->itail = 0; /* empty input queueiptr->ihead = iptr->itail = 0; /* empty input queue */*/

iptr->isem = screate(0);iptr->isem = screate(0); /* chars. read so far=0/* chars. read so far=0 */*/

iptr->osem = screate(OBUFLEN);iptr->osem = screate(OBUFLEN); /* buffer available=al, create semaphorne and initialize *//* buffer available=al, create semaphorne and initialize */

iptr->odsend = 0;/* sends delayed so fariptr->odsend = 0;/* sends delayed so far */*/

iptr->ohead = iptr->otail = 0; iptr->ohead = iptr->otail = 0; /* output queue empty, buffer pointer*//* output queue empty, buffer pointer*/

Page 31: I/O Subsystem Overview of I/O Hardware Access Device Independent Routines Device Dependent Routines Device io.ppt CS 105 “Tour of the Black Holes of Computing”

– I/O – CS 105

Device Dependent: tty init, contDevice Dependent: tty init, cont iptr->ehead = iptr->etail = 0;iptr->ehead = iptr->etail = 0; /* echo queue empty/* echo queue empty */*/

iptr->imode = (isconsole ? IMCOOKED : IMRAW);iptr->imode = (isconsole ? IMCOOKED : IMRAW);

iptr->iecho = iptr->evis = isconsole;iptr->iecho = iptr->evis = isconsole; /* echo console input/* echo console input */*/

iptr->ierase = iptr->ieback = isconsole;/* console honors eraseiptr->ierase = iptr->ieback = isconsole;/* console honors erase */*/

iptr->ierasec = BACKSP;iptr->ierasec = BACKSP; /* using ^h/* using ^h */*/

iptr->ecrlf = iptr->icrlf = isconsole;iptr->ecrlf = iptr->icrlf = isconsole; /* map RETURN on input/* map RETURN on input */*/

iptr->ocrlf = iptr->oflow = isconsole;iptr->ocrlf = iptr->oflow = isconsole; /* map RETURN on output/* map RETURN on output */*/

iptr->ieof = iptr->ikill = isconsole;iptr->ieof = iptr->ikill = isconsole; /* set line kill == @/* set line kill == @ */*/

iptr->iintr = FALSE;iptr->iintr = FALSE;

iptr->iintrc = INTRCH;iptr->iintrc = INTRCH;

iptr->iintpid = BADPID;iptr->iintpid = BADPID;

iptr->ikillc = KILLCH;iptr->ikillc = KILLCH;

iptr->ieofc = EOFC;iptr->ieofc = EOFC;

iptr->oheld = FALSE;iptr->oheld = FALSE;

iptr->ostart = STRTCH;iptr->ostart = STRTCH;

iptr->ostop = STOPCH;iptr->ostop = STOPCH;

iptr->icursor = 0;iptr->icursor = 0;

iptr->ifullc = TFULLC;iptr->ifullc = TFULLC;

cptr = (struct csr *)devptr->dvcsr;cptr = (struct csr *)devptr->dvcsr;

junk = cptr->crbuf;junk = cptr->crbuf; /* clear receiver and/* clear receiver and */*/

cptr->crstat = SLUENABE;cptr->crstat = SLUENABE; /* enable in. interrupts*//* enable in. interrupts*/

cptr->ctstat = SLUDISABL E;/* disable out. "cptr->ctstat = SLUDISABL E;/* disable out. " */*/

}}

Page 32: I/O Subsystem Overview of I/O Hardware Access Device Independent Routines Device Dependent Routines Device io.ppt CS 105 “Tour of the Black Holes of Computing”

– I/O – CS 105

Device Dependent: tty openDevice Dependent: tty open/* ttyopen.c – ttyopen * ttyopen - open tty device and return descriptor (for namespace) *//* ttyopen.c – ttyopen * ttyopen - open tty device and return descriptor (for namespace) */

ttyopen(devptr, nam, mode)ttyopen(devptr, nam, mode)

struct devsw *devptr;struct devsw *devptr;

char *nam;char *nam;

char *mode;char *mode;

{{

/* This routine is not usually used to open tty devices, *//* This routine is not usually used to open tty devices, */

/* but is provided so that automatic calls to open do not *//* but is provided so that automatic calls to open do not */

/* fail. It returns SYSERR unless called with a null name *//* fail. It returns SYSERR unless called with a null name */

if (*nam == '\0')if (*nam == '\0')

return( devptr->dvnum );return( devptr->dvnum );

elseelse

return(SYSERR);return(SYSERR);

}}

Page 33: I/O Subsystem Overview of I/O Hardware Access Device Independent Routines Device Dependent Routines Device io.ppt CS 105 “Tour of the Black Holes of Computing”

– I/O – CS 105

Device Dependent: tty getcDevice Dependent: tty getc/* ttygetc.c – ttygetc * ttygetc - read one character from a tty device *//* ttygetc.c – ttygetc * ttygetc - read one character from a tty device */

ttygetc(devptr)ttygetc(devptr)

structstruct devswdevsw *devptr;*devptr;

{ char{ char ps;ps;

intint ch;ch;

structstruct tty *iptr;tty *iptr;

disable(ps)disable(ps); // disable interrupts so I can mess with OS tables; // disable interrupts so I can mess with OS tables

iptr = &tty[devptr->dvminor]; iptr = &tty[devptr->dvminor]; // get moinor device # from device table// get moinor device # from device table

wait(iptr->isem);wait(iptr->isem); /* wait for a character in buff by waiting fro semaphone/* wait for a character in buff by waiting fro semaphone */*/

ch = LOWBYTE & iptr->ibuff[iptr->itail++]ch = LOWBYTE & iptr->ibuff[iptr->itail++]; // get character and update tail; // get character and update tail

if (iptr->itailif (iptr->itail >= IBUFLEN)>= IBUFLEN)

iptr->itail = 0; iptr->itail = 0; // circular buffer mod operation// circular buffer mod operation

if (iptr->ieof && (iptr->ieofc == ch) )if (iptr->ieof && (iptr->ieofc == ch) )

ch = EOF;ch = EOF;

restore(ps);restore(ps);

return(ch);return(ch);

}}

Page 34: I/O Subsystem Overview of I/O Hardware Access Device Independent Routines Device Dependent Routines Device io.ppt CS 105 “Tour of the Black Holes of Computing”

– I/O – CS 105

Device Dependent: tty putcDevice Dependent: tty putc/* ttyputc.c – ttyputc * ttyputc - write one character to a tty device *//* ttyputc.c – ttyputc * ttyputc - write one character to a tty device */

ttyputc(devptr, ch )ttyputc(devptr, ch )

struct devsw *devptr;struct devsw *devptr;

char ch;char ch;

{{

struct tty *iptr;struct tty *iptr;

char ps;char ps;

iptr = &tty[devptr->dvminor]; iptr = &tty[devptr->dvminor]; // get pointer to control block, i.e., this tty// get pointer to control block, i.e., this tty

if ( ch==NEWLINE && iptr->ocrlf )if ( ch==NEWLINE && iptr->ocrlf )

ttyputc(devptr,RETURN)ttyputc(devptr,RETURN); //convert newline to REturn, NEWLINE; //convert newline to REturn, NEWLINE

disable(ps);disable(ps);

wait(iptr->osem)wait(iptr->osem); /* wait for space in queue */; /* wait for space in queue */

iptr->obuff[iptr->ohead++] = chiptr->obuff[iptr->ohead++] = ch; //put char into output buffer; //put char into output buffer

if (iptr->ohead >= OBUFLENif (iptr->ohead >= OBUFLEN) //buffer pointer mod) //buffer pointer mod

iptr->ohead = 0;iptr->ohead = 0;

(iptr->ioaddr)->ctstat = SLUENABLE;// turn on transmit interupt(iptr->ioaddr)->ctstat = SLUENABLE;// turn on transmit interupt

restore(ps);restore(ps);

return(OK);return(OK);

}}

Page 35: I/O Subsystem Overview of I/O Hardware Access Device Independent Routines Device Dependent Routines Device io.ppt CS 105 “Tour of the Black Holes of Computing”

– I/O – CS 105

Layers of I/O SystemLayers of I/O System1.1. User Level – processesUser Level – processes

• make I/O call, format I/O, spooling

2.2. Device-Independent softwareDevice-Independent software

• naming, protection blocking, buffering, allocation• perform I/O functions common to all devices• provide uniform interface to the user level software• buffering, block sizes, etc.• e.g., map symbolic device names onto driver

3.3. Device-Dependent softwareDevice-Dependent software

• setup device registers, check status• specific code for device operations, i.e., driver• accept abstract requests, e.g., open, and execute

4.4. HardwareHardware

• controller, map I/O ops to device ops, e.g., print,• Interrupts