Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
09-18-2009, 05:36 AM
|
#1
|
|
LQ Newbie
Registered: Aug 2009
Posts: 16
Rep:
|
copy_from_user and copy_to_user example
Dear all,
I wonder if anyone could provide the example for the usage of copy_from_usr and copy_to_usr including the programs both in kernel space and in user space.
how to define the memory or buffer where the data is communicated.
thanks very much,
k
|
|
|
|
09-18-2009, 06:35 AM
|
#2
|
|
Member
Registered: Sep 2009
Distribution: Fedora
Posts: 835
Rep: 
|
Quote:
Originally Posted by krisonearth
Dear all,
I wonder if anyone could provide the example for the usage of copy_from_usr and copy_to_usr including the programs both in kernel space and in user space.
how to define the memory or buffer where the data is communicated.
thanks very much,
k
|
No one will help you unless and until you acknowledge that this is homework. It is an almost word-for-word quote from your class assignment.
|
|
|
|
09-25-2009, 09:52 PM
|
#3
|
|
LQ Newbie
Registered: Sep 2009
Posts: 2
Rep:
|
copy_from_user
I have a similar question, and it is part of my homework assignment. I'm coming here only as a last resort as the professor won't discuss this with the students and refers us to the TA, who is nice, but barely speaks English and doesn't understand our questions. Our assignment was to add a system call to the linux 2.6.30 kernel, then make a user program that calls the new system call we created. The professor detailed everything extremely well, so we got that running fine.
For the second part, we need to figure out what happens when a null pointer is passed to copy_from_user() for the userspace address. Here is how I thought about approaching the problem: create another system call that takes in 1 parameter and pass it to uses copy_from_user(). Then, after we compile the new system call into our uml kernel, we run a usermode program that is supposed to call our system call, but pass in a null value.
Is this the most efficient way to solve this question? If so, how do I pass in variables to copy_from_user() for a system call?
|
|
|
|
09-25-2009, 10:20 PM
|
#4
|
|
Senior Member
Registered: Sep 2003
Posts: 3,171
Rep: 
|
You can do it that way. Why not? As for how to load up copy_from_user, the syntax is copy_from_user(*dest,src,size) and the return value is the number of bytes NOT copied. Just set src = 0x0 and see what happens.
You have to kmalloc the destination buffer in the kernel, or else have it statically assigned, and the source is a userspace memory pointer, in the context of the process that has invoked your command. Size is the number of bytes to copy.
Last edited by jiml8; 09-25-2009 at 10:21 PM.
|
|
|
|
09-27-2009, 08:13 PM
|
#5
|
|
LQ Newbie
Registered: Sep 2009
Posts: 2
Rep:
|
Ok, I tried it but I'm not doing something correctly. I successfully added this function to the kernel (and the .h file, not shown):
#include <linux/kernel.h>
#include <linux/slab.h>
#include "asm/unistd.h"
#include "../include/asm/uaccess.h"
asmlinkage int sys_copycall(long *userData)
{
char* pData;
pData = kmalloc(4*sizeof(char), GFP_KERNEL);
copy_from_user(pData, *userData, sizeof(*userData));
printk(KERN_ALERT "copycall\n");
kfree(pData);
return 1;
}
EXPORT_SYMBOL(sys_copycall);
After I compile and run the kernel with the new system call, I compile and call the program below from within the new kernel:
#include <stdio.h>
#include <errno.h>
#include "unistd_32.h"
#include <unistd.h>
#define __NR_copycall 336
int main()
{
int ret = 0;
ret = syscall(__NR_copycall);
printf("ret = %d \n",ret);
return 0;
}
When I run this program, it prints "ret = 1" to the screen, then exits. I thought by not passing anything to the function, it would be the same thing as passing null. I also don't know how to pass a variable to the function since I don't know which register to modify and how to modify it right before the system call. Could someone please tell me what I need to change to get this figured out?
Last edited by txguy09; 09-27-2009 at 09:07 PM.
|
|
|
|
09-29-2009, 05:08 PM
|
#6
|
|
Senior Member
Registered: Sep 2003
Posts: 3,171
Rep: 
|
Quote:
|
When I run this program, it prints "ret = 1" to the screen, then exits
|
What would you expect to have happen? You told your kernel routine to return 1. That is what it did. Any error messages will be in /var/log/messages. Look there.
Quote:
|
I thought by not passing anything to the function, it would be the same thing as passing null.
|
Why would you think that? The registers are saved when you make your syscall, but they are not cleared. You want a 0x0 in a register, you have to put it there.
Look at /usr/src/linux/kernel/sys.c
Also look at sys_call.S which may have some other name on your system (on my workstation it is /usr/src/linux/arch/um/sys-i386/sys_call_table.S).
Registers are modified in the following order as specified by the order of the arguments in the syscall() invocation: %eax is the syscall number; %ebx, %ecx, %edx, %esi, %edi and %ebp are the registers used as arguments 0 to 5.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 05:41 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|