lecture for lab 3, exp1 of ee505 (developing device driver) t.a. chulmin kim corelab. mar, 11, 2011...

21
Lecture for Lab 3, Exp1 of EE505 (Developing Device Driver) T.A. Chulmin Kim Mar, 11, 2011 [XenSchedulerPaper_Hotcloud- commits] r21 - /

Upload: ray-russum

Post on 30-Mar-2015

215 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Lecture for Lab 3, Exp1 of EE505 (Developing Device Driver) T.A. Chulmin Kim CoreLab. Mar, 11, 2011 [XenSchedulerPaper_Hotcloud-commits] r21 -

Lecture for Lab 3, Exp1 of EE505

(Developing Device Driver)

T.A. Chulmin KimCoreLab.

Mar, 11, 2011[XenSchedulerPaper_Hotcloud-commits] r21 - /

Page 2: Lecture for Lab 3, Exp1 of EE505 (Developing Device Driver) T.A. Chulmin Kim CoreLab. Mar, 11, 2011 [XenSchedulerPaper_Hotcloud-commits] r21 -

Contents Introduction to Device Driver

Implementation of Device Driver

Usage of Device Driver

Problems

Environment & Policies

Page 3: Lecture for Lab 3, Exp1 of EE505 (Developing Device Driver) T.A. Chulmin Kim CoreLab. Mar, 11, 2011 [XenSchedulerPaper_Hotcloud-commits] r21 -

Introduction to Device Driver

What is Device Driver?

Page 4: Lecture for Lab 3, Exp1 of EE505 (Developing Device Driver) T.A. Chulmin Kim CoreLab. Mar, 11, 2011 [XenSchedulerPaper_Hotcloud-commits] r21 -

Introduction to Device Driver

What is Device Driver?

Software

Hardware

Page 5: Lecture for Lab 3, Exp1 of EE505 (Developing Device Driver) T.A. Chulmin Kim CoreLab. Mar, 11, 2011 [XenSchedulerPaper_Hotcloud-commits] r21 -

Introduction to Device Driver

Inside of Device Driver

Basically, it is usually generated by C code. In other word, it can be simple as much as ‘hello world’ in C

programming. You can freely imagine your device driver structure.

Even though it is not linked with real HW, it works.

Closely related with kernel. (use of system calls)

Page 6: Lecture for Lab 3, Exp1 of EE505 (Developing Device Driver) T.A. Chulmin Kim CoreLab. Mar, 11, 2011 [XenSchedulerPaper_Hotcloud-commits] r21 -

Introduction to Device Driver

Kinds of Device Drivers Character Device

Bypass Buffer Cache Serialized Data User receives Row Data of the device Ex. Terminal, video card, sound card…

Block Device Through Buffer Cache Random Accessible Ex. HDD, CDROM …

Network Device Ex. Ethernet

Page 7: Lecture for Lab 3, Exp1 of EE505 (Developing Device Driver) T.A. Chulmin Kim CoreLab. Mar, 11, 2011 [XenSchedulerPaper_Hotcloud-commits] r21 -

Introduction to Device Driver

How can we access to the device driver? Through Device File (special file)

In the previous lab, you heard that /dev/sdb to mount.

Char device and Block device have this file. ‘Mknod /dev/dummy c 254 0’ – creation of the device

file.

Real Usage (system call)   fd = open("/ dev/dummy ", O_RDWR);

 write(fd,buf,1);  read(fd,buf,1);  close(fd);

Page 8: Lecture for Lab 3, Exp1 of EE505 (Developing Device Driver) T.A. Chulmin Kim CoreLab. Mar, 11, 2011 [XenSchedulerPaper_Hotcloud-commits] r21 -

Implementation of Device Driver

Specification of Dummy Device Driver No linkage with Real HW. Using printk, only printout the name of the

device driver operation called by a user. Implementations

Module init and cleanup 4 Operations in Dummy Device Driver

dummy_read : read system call dummy_write : write system call dummy_open : open system call dummy_release : close system call

Page 9: Lecture for Lab 3, Exp1 of EE505 (Developing Device Driver) T.A. Chulmin Kim CoreLab. Mar, 11, 2011 [XenSchedulerPaper_Hotcloud-commits] r21 -

Implementation of Device Driver

How can we implement it? Initialization (in init_module function)

Mapping a device file with the device driver

Registration of a Char Device : offered by OS Needs Major Number, Name, and File_operation

Structure of the device driver.

int register_chrdev( unsigned int major, const * name, struct file_operations * fops);

int register_chrdev( unsigned int major, const * name, struct file_operations * fops);

struct file_operations dummy_fops = { open : dummy_open, // open    read :  dummy_read,               // read     write : dummy_write,               // write     release : dummy_release,      // release };

Mapping system calls with the internal functions of the device drivers

Page 10: Lecture for Lab 3, Exp1 of EE505 (Developing Device Driver) T.A. Chulmin Kim CoreLab. Mar, 11, 2011 [XenSchedulerPaper_Hotcloud-commits] r21 -

Implementation of Device Driver

How can we implement it?#define DUMMY_MAJOR_NUMBER 254 struct file_operations dummy_fops = { open : dummy_open, // open    read :  dummy_read,               // read     write : dummy_write,               // write     release : dummy_release,      // release };

char devicename[20]; int init_module(void) {         printk("init module\n");         strcpy(devicename, "Dummy_Driver");         register_chrdev( DUMMY_MAJOR_NUMBER, devicename, &dummy_fops);         return 0; }

void cleanup_module(void) {                  printk("Clean Up Module\n");         unregister_chrdev(DUMMY_MAJOR_NUMBER,devicename); }

Page 11: Lecture for Lab 3, Exp1 of EE505 (Developing Device Driver) T.A. Chulmin Kim CoreLab. Mar, 11, 2011 [XenSchedulerPaper_Hotcloud-commits] r21 -

Implementation of Device Driver

How can we implement it?

How can we build this? T.A. and the material will show you the way.

int dummy_open(struct inode *inode, struct file *file) {         printk("Open call \n");         return 0; }

ssize_t dummy_read(struct file *file, char *buffer,   size_t length, loff_t *offset) {         printk("Here is Read Call \n");         buffer[0] = 0x34;         return 0; }

ssize_t dummy_write(struct file *file, const char *buffer, size_t length,  loff_t *offset) {         printk("Here is Write Call [%x]\n ",buffer[0]);         return 0; }

Page 12: Lecture for Lab 3, Exp1 of EE505 (Developing Device Driver) T.A. Chulmin Kim CoreLab. Mar, 11, 2011 [XenSchedulerPaper_Hotcloud-commits] r21 -

Usage of Device Driver Module Instructions

Insmod dummy.ko -> the module output file built Rmmod dummy Check the kernel message -> since we used

printk to see the device driver operations. Test this through your own user application.

Simple C program appeared in the material. Insmod should be done first before executing the

user application.

Page 13: Lecture for Lab 3, Exp1 of EE505 (Developing Device Driver) T.A. Chulmin Kim CoreLab. Mar, 11, 2011 [XenSchedulerPaper_Hotcloud-commits] r21 -

Problems 1. device driver with FIFO queue

FIFO Queue structure

In (write)& Creation of Entry in the Queue

Out (read)& Deletion of Entry

APP1 APP2Check the written data sequence and the read data sequence are same or not.

Page 14: Lecture for Lab 3, Exp1 of EE505 (Developing Device Driver) T.A. Chulmin Kim CoreLab. Mar, 11, 2011 [XenSchedulerPaper_Hotcloud-commits] r21 -

Problems 2. flush operation makes queue

empty

APP1 APP2APP3

Flush OP

Thrash all the entries in the queue

Page 15: Lecture for Lab 3, Exp1 of EE505 (Developing Device Driver) T.A. Chulmin Kim CoreLab. Mar, 11, 2011 [XenSchedulerPaper_Hotcloud-commits] r21 -

Problems 3. Make a block device driver.

With simple write and read operation. The unit of data of read and write should

be a block. (kernel structure should be used)

Hints are given in the material. Further hints will be given by T.A. Not Necessary, it is the extra-credit

problem. But should be in the report.

Page 16: Lecture for Lab 3, Exp1 of EE505 (Developing Device Driver) T.A. Chulmin Kim CoreLab. Mar, 11, 2011 [XenSchedulerPaper_Hotcloud-commits] r21 -

Environment & Policies Environment

Same with Lab2

Policies Same with Lab2 Due : 3/25 11:59 PM Submission :

(hard copy) : not decided yet. (soft copy) : [email protected]

Page 17: Lecture for Lab 3, Exp1 of EE505 (Developing Device Driver) T.A. Chulmin Kim CoreLab. Mar, 11, 2011 [XenSchedulerPaper_Hotcloud-commits] r21 -

Appendix Queue Example

Page 18: Lecture for Lab 3, Exp1 of EE505 (Developing Device Driver) T.A. Chulmin Kim CoreLab. Mar, 11, 2011 [XenSchedulerPaper_Hotcloud-commits] r21 -

Appendix Queue Example

Page 19: Lecture for Lab 3, Exp1 of EE505 (Developing Device Driver) T.A. Chulmin Kim CoreLab. Mar, 11, 2011 [XenSchedulerPaper_Hotcloud-commits] r21 -

Appendix Queue Example

#define QUEUE_SIZE 1024typedef {

int head, tail; int item[QUEUE_SIZE];} CQ_t;static inline void InitCQ(CQ_t *q)

{ q->head = q->tail = 0;}static inline int IsFull(CQ_t *q) {

return ( (q->head+1)%QUEUE_SIZE == q->tail );

}static inline int IsEmpty(CQ_t *q)

{return (q->head == q->tail);

}

#define QUEUE_SIZE 1024typedef {

int head, tail; int item[QUEUE_SIZE];} CQ_t;static inline void InitCQ(CQ_t *q)

{ q->head = q->tail = 0;}static inline int IsFull(CQ_t *q) {

return ( (q->head+1)%QUEUE_SIZE == q->tail );

}static inline int IsEmpty(CQ_t *q)

{return (q->head == q->tail);

}

int Queue(CQ_t *q, int value){

if (q==NULL) return -1;if (IsFull(q)) return -2;q->item[q->head] = value;q->head = (q->head + 1)%QUEUE_SIZE;return 0;

}

int Dequeue(CQ_t *q, int *value){

if (q==NULL || value==NULL) return -1;if (IsEmpty(q)) return -2;*value = q->item[q->tail];q->tail = (q->tail + 1)%QUEUE_SIZE;return 0;

}

int Queue(CQ_t *q, int value){

if (q==NULL) return -1;if (IsFull(q)) return -2;q->item[q->head] = value;q->head = (q->head + 1)%QUEUE_SIZE;return 0;

}

int Dequeue(CQ_t *q, int *value){

if (q==NULL || value==NULL) return -1;if (IsEmpty(q)) return -2;*value = q->item[q->tail];q->tail = (q->tail + 1)%QUEUE_SIZE;return 0;

}

Page 20: Lecture for Lab 3, Exp1 of EE505 (Developing Device Driver) T.A. Chulmin Kim CoreLab. Mar, 11, 2011 [XenSchedulerPaper_Hotcloud-commits] r21 -
Page 21: Lecture for Lab 3, Exp1 of EE505 (Developing Device Driver) T.A. Chulmin Kim CoreLab. Mar, 11, 2011 [XenSchedulerPaper_Hotcloud-commits] r21 -

Thank You! Good Luck with your finishing.