Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > GNU Linux Zone > The Linux Kernel > help regarding memory driver

Forgot Password?
 The Linux Kernel   Compiling, theory, programming or other discussion about the linux kernel

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds


Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Reply
 
Thread Tools Display Modes
Old 3 Weeks Ago   #1 (permalink)
Just Joined!
 
Join Date: Oct 2009
Posts: 8
help regarding memory driver

Hello

I want to write to some specific physical address in the system memory space. I am writing a driver for it. I am using following functions

in module_init:
check_region() to check the availability of that specific physical address.
request_mem_region() to request the memory resource from the kernel in case of successful check_region(). The function request_mem_region() returns success which means (as far as I assume) that the resource has been allocated to my driver.

in write function
If the request_region() returns success then I must be able to write to that memory address just freely.
For example: *(char *)myphysicaladdr = some value

But when I open the driver file from /dev and write a charater to it, segmentation fault occurs. Kindly somebody help me what should I do?

outb() is used to write to an i/o port address. Kindly tell me is there any function to write to physical address?

regards,
Atif Shabir
atif_shabir is offline  


Reply With Quote
Old 3 Weeks Ago   #2 (permalink)
Linux Guru
 
coopstah13's Avatar
 
Join Date: Nov 2007
Location: NH, USA
Posts: 1,756
debug your code to find out why you are getting a seg fault

the most common cause of seg fault is dereferencing null pointer and accessing uninitialized memory
coopstah13 is offline   Reply With Quote
Old 3 Weeks Ago   #3 (permalink)
Just Joined!
 
Join Date: Oct 2009
Posts: 8
BUG: unable to handle kernel NULL pointer dereference at 00000100

Thank you coopstah13 for your reply.

A piece of my source code is

#define PHYSICAL_ADDR 0x00000100
#define MEM_SIZE 2

int memory_init(void)
{
int result;

/*Registering device*/
result = register_chrdev(memory_major, "my_module", &memory_fops);
if(result < 0) {
printk("<1> my_module: cannot obtain major number %d\n", memory_major);
return result;
}

/*Allocating memory byte for the port*/
my_port = check_region(PHYSICAL_ADDR, MEM_SIZE); //check_region returns 0 (success)
if(my_port) {
printk("<1>my_module: cannot reserve 0x%X\n",PHYSICAL_ADDR);
result = my_port;
goto fail;
}

alloc_result = request_mem_region(PHYSICAL_ADDR, MEM_SIZE, "my_module");
//request_mem_region() returns 0 (success)
if(alloc_result) {
printk("<1>my_partport2: Requested memory region not allocated\n");
goto fail;
}
printk("<1>my_module: Requested memory region allocated successfully....Inserting Module\n");
return 0;

fail:
memory_exit();
return result;
}

ssize_t memory_read(struct file *filp, char *buf,
size_t count, loff_t *f_pos) {

char my_parport_buf;

////////////////////////////////////////////
my_parport_buf = *((char *)PHYSICAL_ADDR); //This statement gives the segmentation fault
///////////////////////////////////////////////

copy_to_user(buf, &my_parport_buf, 1);
if(*f_pos == 0) {
*f_pos+=1;
return 1;
} else {
return 0;
}
}


When I run a C program that opens and reads one character from the file /dev/my_module, segmentation fault occurs. dmesg shows following

BUG: unable to handle kernel NULL pointer dereference at 00000100
IP: [<f8b6f080>] :my_parport2:memory_read+0x1c/0x66
*pdpt = 00000000243e6001 *pde = 0000000000000000
Oops: 0000 [#2] SMP


Kindly tell me where my the problem may lie. Am I on the right path for writing on physical memory? or should I adopt a new path?

regards,
Atif Shabir
atif_shabir is offline   Reply With Quote
Old 3 Weeks Ago   #4 (permalink)
Linux Guru
 
coopstah13's Avatar
 
Join Date: Nov 2007
Location: NH, USA
Posts: 1,756
Code:
my_parport_buf = *((char *)PHYSICAL_ADDR); //This statement gives the segmentation fault
you are dereferencing a null pointer, you need to initialize PHYSICAL_ADDR
coopstah13 is offline   Reply With Quote
Old 3 Weeks Ago   #5 (permalink)
Just Joined!
 
Join Date: Oct 2009
Posts: 8
I have #defined PHYSICAL_ADDR to be 0x00000100

and how will I initiallize this pointer? I mean if I use kalloc(), the kernel will allocate me an address from any where in the memory. But If want to allocate specifically 0x00000100 physical address then what should I do?
atif_shabir is offline   Reply With Quote
Old 3 Weeks Ago   #6 (permalink)
Linux Guru
 
coopstah13's Avatar
 
Join Date: Nov 2007
Location: NH, USA
Posts: 1,756
Is there any reason you need to allocate at a specific memory address? What if something else is already using it? If you are just looking to write a driver that you can write something to memory from and read it from anywhere, then any address should be fine...
coopstah13 is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Free Magazines
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Learn about everything you'll need to build and maintain your Linux servers, and to deploy Web applications to them.
subscribe
Open Source Security Myths Dispelled
Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization
subscribe
InformationWeek
InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology.
subscribe



All times are GMT. The time now is 04:07 AM.






© 2000 - 2009 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.0 RC2