EmbLogic's Blog

problem in scull_read()

int scull_read(struct file *filp, char *buff, size_t count, loff_t *fpos)
{
scull_dev *dev;

printk(“%s \n”,__func__);

dev=(scull_dev *)filp->private_data;

struct scull_qset *dptr;         // Pointer to the beginning of the data
int quantum = dev->quantum;      // Size of a quantum
int qset = dev->qset;            // Number of quantums
int itemsize = quantum * qset;   // number of bytes in the listitem
int item, s_pos, q_pos, rest;
ssize_t retval = 0;

char read_buff[100];
int pos=0;
int temp=count/quantum;

if(*fpos >= dev->size){
goto out;
}

if(*fpos+count > dev->size){
count = dev->size – *fpos;
}

item = (long) *fpos / itemsize;
rest = (long) *fpos % itemsize;
s_pos = rest / quantum;
q_pos = rest % quantum;
dptr = scull_follow(dev,item);

if(dptr == NULL || !dptr->data || !dptr->data[s_pos]){
goto out;
}

while(temp!=0)
{
read_buff[pos]=*dptr->data[s_pos]+q_pos;
printk(“the address of quantums is %x\n”,dptr->data[s_pos]);
if(q_pos <= quantum)
{
pos++;
q_pos++;
}

if(q_pos > quantum)
{
s_pos++;
q_pos=0;
pos–;
temp–;
}
}

if(count > quantum – q_pos)
{
count = quantum – q_pos;
}

if(copy_to_user(buff, read_buff, count))
{
retval = -EFAULT;
goto out;
}
printk(“the data in the buff is %s\n”,read_buff);
*fpos += count;
retval = count;
out:
return retval;
}

Posted in Uncategorized | Leave a comment

Regarding Kernel Execution

What is the difference b/w concurrent execution and sequential execution??? I have the idea abt the sequential execution ie applications are sequentially executed.. But how Kernel modules are concurrent in execution ?? Plz elaborate.

Posted in Uncategorized | Leave a comment

Inside structure declaration of variable of same type is not allowed

 struct node {
     struct node x;    //not allowed
     struct node *head;   //allowed
     };

Why does we can declare a pointer of same type but not a variable of same type?
Posted in Uncategorized | 1 Comment

Alternative method of newline

Q. How to print output in multiple line without using \n character?

Ans. printf(“\x0a”);

Posted in Uncategorized | Leave a comment

character driver -> scull open ->container_of

the prototype for container_of is as follows:

container_of(pointer,container_type,container_field)

so what exactly does this return?

considering an example,

ptr=container_of(inode->i_cdev,struct scull_dev,cdev)

here the pointer ‘inode->i_cdev’ has a field cdev.’cdev’ is CONTAINED IN struct scull_dev.

so ultimately,’container_of’ takes pointer ‘inode->i_cdev’ to struct scull_dev and returns a pointer containing struct scull_dev.

is it so????

kindly elaborate and add…….

Posted in Uncategorized | Leave a comment

partial write

can any bdy tell me …how to accomplish partial write in scull…???….when i call write cmd from d application,it jst fill only 1 quantum.But when i try to call write cmd multiple times ,it fills a few quantum with d same data…how to prevent this…????

Posted in Uncategorized | Leave a comment

The Algorithm to trim.

int scull_trim(struct scull_dev *dev)

-> Iterate through scull_qsets

-> for(*s_q=s_d->data;s_q;s_q=next)

-> In every iteration

-> Free the quantums

-> Finaly free the qset array

-> Make pointer to qset as NULL

-> Free scull_qset

-> Finally initialize the default values

Posted in Uncategorized | Leave a comment

The Macro “container_of”

#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr – offsetof(type,member) );})

Posted in Uncategorized | Leave a comment

Regarding Char Device Registration

When we used : register_chrdev(unsigned int major, const char *name,
struct file_operations *fops);

We never initialized struct cdev. Still we were very much able to call all the functions mapped in the file_operatins structure using fop.

Why we have to initialize it in the case of alloc_chardev_region
or register_chardev_region ???? Will these function not work ,if we dont initialize cdev in this case.

Why struct cdev is so nessary.

Posted in Uncategorized | 3 Comments

Registering the chr device

In int register_chrdev_region(dev_t first, unsigned int count,char *name);
if we make count large what can go wrong.

Posted in Uncategorized | Leave a comment

Makefile

INSTALL_DIR=modules
ifneq (${KERNELRELEASE},)
obj-m := new.o

new_mod-objs := new.o

else
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE)  -C $(KERNELDIR) M=$(PWD) modules
@rm -rf ${INSTALL_DIR}
@mkdir ${INSTALL_DIR}
@mv -f *.o *.ko *.mod.c .*.cmd ${INSTALL_DIR}
clean:
rm -rf ${INSTALL_DIR}
endif

Posted in Uncategorized | 1 Comment

Facing Problem in Write()

I’m trying to update the struct file -> f_pos with count number of bytes in my write function,

but everytime i call write(), it will display the value of f_pos = 0.

and i call 5 times write() in my user application at a time.

I am not able to update the f_pos value………..

Posted in Uncategorized | 1 Comment

Algo for Write operation

* Define Local Device.

*Fetch device from local device.

*Allocate space for buffer,pointed to by dev->data.

*Check for error ,clear the allocated space using memset.

*Check if the fpos is beyond the device size.

*Check for errors.

*Check if the sufficient amount of space is available to write the count no of bytes.

** if not modify the count accordingly.

*Copy from user.

*Modify the file position.

*Identify the no. of bytes yet to be written.

*Modify the device data size.

*Return space for buffer,pointed to by dev->data.

Posted in Uncategorized | Leave a comment

Allocation Test Function.

// Function To allocate bytes .
// Test function for writing write operation.
#include<stdio.h>
#include<stdlib.h>
#define SIZE 1000
char arr[SIZE];
char *p=arr;

char * allocate(unsigned char  );
char * allocate(unsigned char j)
{
if(arr+SIZE-p>j)
{
p=p+j;
return p-j;
}
else
{
printf(“Error in size\n”);
return NULL;
}
}
int main()
{
int *j;
char *l;
j=(int *)allocate(sizeof(int));
if(!j)
printf(“Error\n”);
*j=67;
l=(char *)allocate(sizeof(char));
*l=77;
printf(“%d %d\n”,*j,sizeof(*l));
return 0;
}

Posted in Uncategorized | Leave a comment

Memory Management Strategies.

# The memory management algos vary from primitive bare-machine to paging and segmentation strategies.
# Memory contains large array of words and bytes each having its own address.
# The computer fetches the instructions from the cpu , according to the value in program counter.
# The memory unit just sees the streams of memory addresses , it does not know how they are created or what they are for.
# The binding of actual address and the logical address has to be seen as far as memory management has to be considered.

Basic Hardware

# Main memory and the processor’s registers build into the cpu memory is what cpu can access directly.
# There are machine instructions that take memory addresses as arguments.?? What are those instructions like.
# Thus any instruction in execution or any data used by these instructions must be in any of these (direct-access)storage devices. If they are not they have to moved before cpu can operate them.
# The registers that are build into the cpu are accessible in one cycle of the cpu clock.
# Most cpu’s can decode the instructions in registers and perform simple operations at the rate of one operation per clock tick. This is not the case for the main memory which is held via memory bus.
# The memory access may take many cycles to complete during which the processor has to stall. In the case of main memory.
# The remedy is install a fast memory b/w cpu and main memory called cache.
# The issues are more than this, like protecting the os from the user process.
# This protection is provided by the hardware.
# We need to make sure each process has a separate memory space.
# This can be done by using base and limit register.
# The base register holds the smallest legal phy add. and the limit register holds size of the range.
# The hw protection is obtained by comparison bw these registers.
# If add beyond or below is issued a fatal error is a result of this.
# These registers can only be loaded by kernel.
# The os is only allowed to do such modifications.Thus allowing it dump the code in case of errors.

Address Binding

# The process that are waiting to be brought for the execution form the input queue.
# As the process executes it accesses the data and the instructions from the memory.
# After this its memory space becomes available.
# Many os allow process to reside any where in the memory.
# Add. in the source program are generally symbolic.
# The compiler binds these symbolic add to relocatable add.

Logical vs physical add.

# The add generated by the cpu is generally called the logical add.
# But the add in the memory is generally referred to as physical add.
# In the execution time the add can be referred to as logical add.
# The set of all logical add generated by the program is called the logical add space.
# The set of all physical add corresponding to these logical add is called physical add space.
# In the run time the logical to physical add is done by a hardware called MMU(memory management unit).
# The very basic example is of ms-dos which uses relocation register .
# In relocation register each add is added with the relocation value to form the phy add.
# The program never sees the real add. It can have a pointer pointing to 346 and can do what ver with it ie. add compare.

Posted in Uncategorized | Leave a comment