dev_drv
-
Upload
sakshi-goel -
Category
Documents
-
view
216 -
download
0
Transcript of dev_drv
-
8/8/2019 dev_drv
1/9
Device Drivers in Xinu
What is a device driver ?
Device driver is a piece of software associated with device to
provide support for using that device. Xinu has device drivers
for tty devices (terminal & keyboard), disk, network, files and
windows. In Xinu files and windows are treated as pseudo-
devices.
What support does a device drivers provide?
Convenience: a simple interface to the device which makes
the user programming simple. Users are spared of many
programming details for using the device. For example, lowerlevel details of the device hardware are handled by the device
driver.
Portability of user programs: Since the device specific
details are hidden in the device driver, the user programs can
operate with a range of devices without requiring
modifications. For example, print command works correctly
whether it is a Panasonic or Epson printer as long as the correct
driver for the printer is installed.
Resource Management: Each device is managed as a
resource by the device driver to take care of issues such as
resource sharing, security.
-
8/8/2019 dev_drv
2/9
How device drivers in Xinu work ?
U
s
e
r
system
call
Device switch table
devtab[ ]
Device
specific
upper-
half
routine
Device
specific
lower-
half
routine
Device
An Example: Read operation
system call upper-half routine lower-half routine
read( descrp, buff, count ) ttyread(devptr, buff, count) ttyiproc( )
devptr = &devtab[descrp]
Data transfer during a read operation for tty device:
buff
user buffer
*buff++ = ttygetc(devptr) ttyiproc( )
called within ttyread()
system buffer
Uniform interface across devices: The system call is the same across different devices,
but the parameters are interpreted differently.
read( descrp, buff, count )
interpreted as the number of
characters for a tty device
interpreted as the block number for a
disk device
-
8/8/2019 dev_drv
3/9
Device Switch Table
int dvnum;
long int (*dvinit)();
long int (*dvshutdown)();
long int (*dvopen)();
long int (*dvclose)();
long int (*dvread)();
long int (*dvwrite)();
long int (*dvseek)();
long int (*dvgetc)();
long int (*dvputc)();
long int (*dvcntl)();
long int (*dviint)();
long int (*dvoint)();
long int dvcsr;
long int dvivec;
long int dvovec;
char *dvioblk;
int dvminor;
device switch table entry
( struct devsw)
device switch table
struct devsw devtab[ ]
Ptrs to device
specific high-level routines
Ptr to device
control block
The structure of the control block varies from device to device. The control block
contains information associated with the device.
-
8/8/2019 dev_drv
4/9
Data Structures for Disk Device Driver
devtab[ ]
dvioblk
dstab[ ]
dstab[ ] : array of
disk control block.
One control block
per disk,
*dreq : ptr to list of pending
disk requestsdnum : device number
dibsem : semaphore for i-block
ddirsem : semaphore for directory
dflsem : semaphore for free list
dnfiles : # currently open files
*ddr : ptr to incore directory
fields of each dstab entry
List of pending requests
dreq
structure of each node in the
list of pending requests
drdba : disk block #
drpid : id of process making the requestdrbuff : buffer address
drstat : return address
drnext : ptr to next request node
Requests are addid to the list by the high-level device driver routine (DDR) and
requests are removed from the list by the low-level DDR.
Req
list
upper-half DDR lower-half DDR
-
8/8/2019 dev_drv
5/9
How readand write calls works
dsread( n , buff , m )
disk block numberdevice numberptr to user buffer
buff
user buffer
drbuff
read
dsread( )
1. allocate memory for a request
node using getbuf( ).
2. fill pid and other fields of the
request node.
3. enqueue the request
4. suspend the process
dswrite( )
1. allocate memory for a request
node using getbuf( ).
2. fill pid and other fields of the
request node.
3. enqueue the request
5. lower-half routine executes to
read the data from disk
4. lower-half routine executes to
write the data to disk
5. deallocate memory used for
request node
6. deallocate memory used for
request node
The process is blocked in case ofread.
Exception: readis optimized
read
write
-
8/8/2019 dev_drv
6/9
Disk Scheduling Algorithm in Xinu
List of pending requests
dreqnull
First request on the list is
the one that hardware is
performing; hardware is
idle when the list is empty.
10 15 9 20 40 2
23
New request
for block 23
45
New request
for block 45
Optimization of disk requests
Situation :New request is for the same block as one of the pending requests
ld request New request ptimization (Y/N)AD AD
AD W IT
W IT AD
W IT W IT
S K AD (or W IT )AD (or W IT ) S K
-
8/8/2019 dev_drv
7/9
Basics of Xinu File System
File : A sequence of zero or more bytes; any further structure is to be enforced by user level
programs
Index Mechanism : To support file growth and random movement within a file Xinu uses
an indexing mechanism. Each file is stored as a link-list of index nodes. Each
index node points to a subset of datablock associated with the file.
Layout on the Disk : Block0 contains the directory, next few disk blocks are reserved for
storing the index node, the remaining blocks are used for storing the data
blocks.
directory
3 blocks for index
nodes
data blocks
A logical view of an hypothetical disk
0 1 2 3 4 5 6 7
Example:A file with index node #2, and data blocks: 5, 4, 7
first index node of the file
54 7
On our system we simulate a disk where:
Each disk block is 512 bytes
Each index node is 64 bytes
Each data block is 512 bytes
Each index node can point to 29 data blocks
-
8/8/2019 dev_drv
8/9
More About Index Nodes
Struct iblk{
long ib_byte; /* first data byte */
IBADDR ib_next; /* next iblock # */
IBADDR ib_dba[IBLEN]; /* ptrs data
block */ }
On our system:
sizeof(data block) = sizeof(disk block) =512 bytes
sizeof (iblk) = 64 bytes
sizeof(ib_byte) = 4 bytes
sizeof(ib_next) = 2 bytes
sizeof(ptr) = 2 bytes
IBLEN = 29
For the first index node of a file ib_byte = 0; for the n-th index node
ib_byte = (n - 1) sizeof(data block)
ibtodb(ib) : calculates the address of the disk block containing the index node ib .
For example, ibtodb(37) = 5
ibdisp(ib) : calculates the displacement of the index node within the disk block.
For example, ibdisp(37) = 5 * 64
Operations on index nodes:
ibclear(ibptr, ibbyte) : initializes an in-core index node
ibptr->ib_dba[i] = DBNULL; ibptr->ib_byte = ibbyte; ibptr -> ib_next = IBNULL;
ibget(diskdev, inum, loc) : copy the index node inum from the disk to the memory
location pointed by the pointerloc .
Note a temporary buffer is needed to read the disk block containing the specified index
block. Since a disk block is bigger than a index block, the disk block should not be
read into the location pointed by loc .
Use ibtodb( ) to calculate the disk block #, copy the disk block into a temporary
buffer. Use ibdisp( ) to calculate the location where the specified index block is located
within the temporary buffer.
-
8/8/2019 dev_drv
9/9
fd = open(diskdev, sample , orw); /*open file sample */
How a file gets opened in Xinu
Device Table
disk entry
file entry
m
n
dvioblk ptr
dvioblk ptr
dstab[ ] : an array of
disk control blocks
dir
ptr
incore
directory
file
entries
fdptr
dstab[ ] : an array of
disk control blocks
file entry
file entry
file entry
n mfl_id
(static)
fl_dev
(dynamic) fl-dent ptr
This ptr is
established when
a file is opened
dfdsrch : locates the file in the incore directory, returns fdptr
dfalloc : allocates an unused file control block, returns findex
dsopen : returns n, index of the dev table entry for file
dsopen( ) calls : dfckmd( ), dfdsrch( ), and dfallaoc( ).
mn