[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Yet, another question about writing a module.....
Hi,
O.k here it goes...
Actually you are trying to use a system call from kernel space instead off from user space(which is why system calls are for).
In conventional use (i.e system calls from user space) before switching from user land to kernel space the system call does some checking whether to see the buffer address is valid i.e it has to be within user space( < 3GB limit ) if it is then it has to transfer control to proper kernel func like sys_read,sys_write etc..(actually it saves the process registers on a stack before switching to kernel mode for it to return user space as soon the kernel stuff is done with.
In your scenario you are trying to call the system call from kernel space so the system call check will fail because tyhe buffer address is in kernel space( > 3GB)
so the code given below is to make the system call check pass
It makes use of field called addr_limit in task_struct which gives the max valid address for making a sys call from user land.getfs gets that and saves it and setfs sets the new max valid address using KERNEL_DS which is the segment descriptor associated with kernel space so now system call check passes as we have passed a new valid address from kernel space thro KERNEL_DS
then do whatever u want in kernel space...use filp directly to open file and read blah,blah,...
but remember to put original address back before switching back otherwise the user program will have access to kernel space and god knows what will happen after that....
bye
sand
Thank you for your help!! Can you explain the code you suggested me? Where I can find prototype of functions get_fs(), set_fs() and why you use this functions?
----- Original Message -----
From: Tiziano Fagni
To: kernelnewbies@nl.linux.org
Sent: Friday, February 16, 2001 11:40 AM
Subject: Yet, another question about writing a module.....
The first thing: THANK YOU to all people that give me any tips!!
Ok, suppose I want to write a module and I want to access to a file in the filesystem to read and save its content into a previous allocated kernel buffer.
The use of module could be "modprobe module /tmp/pippo".
I think I can't use open() function because I know that I can olny call function listed in ksym.c file. It's true?
So, is there any function that I can use to read the content of a file?
Any tip will be useful.....
So, is there any function that I can use to read the content of a file?Any tip will be useful.....
try this
struct file *filp;
mm_segment_t old_fs;
filp = filp_open(name,flags,mode);
old_fs= getfs();
set_fs(KERNEL_DS);
filp->f_op->read(filp,buffer,count,&filp->f_pos);
But you should keep one thing in mind that before this code is executed you have the filesystem on this this particular file exists has been already mounted
otherwise you risk the crashing of kernel.
_____________________________________________________
Chat with your friends as soon as they come online. Get Rediff Bol at
http://bol.rediff.com
-
Kernelnewbies: Help each other learn about the Linux kernel.
Archive: http://mail.nl.linux.org/
IRC Channel: irc.openprojects.net / #kernelnewbies
Web Page: http://www.surriel.com/kernelnewbies.shtml