LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 09-18-2009, 05:36 AM   #1
krisonearth
LQ Newbie
 
Registered: Aug 2009
Posts: 16

Rep: Reputation: 0
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
 
Old 09-18-2009, 06:35 AM   #2
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 101Reputation: 101
Quote:
Originally Posted by krisonearth View Post
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.
 
Old 09-25-2009, 09:52 PM   #3
txguy09
LQ Newbie
 
Registered: Sep 2009
Posts: 2

Rep: Reputation: 0
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?
 
Old 09-25-2009, 10:20 PM   #4
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 114Reputation: 114
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.
 
Old 09-27-2009, 08:13 PM   #5
txguy09
LQ Newbie
 
Registered: Sep 2009
Posts: 2

Rep: Reputation: 0
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.
 
Old 09-29-2009, 05:08 PM   #6
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 114Reputation: 114
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
copy_to_user within module. maheshstms Linux - Kernel 5 02-11-2014 05:04 AM
problems with copy_to_user() leoremoto Linux - Kernel 8 07-20-2012 01:28 PM
copy_from_user() function fails inside a kernel thread. rajneesh.gaur Linux - Newbie 1 02-18-2012 04:58 PM
copy_from_user does not copy from process-heap najoshi Linux - Kernel 0 08-16-2009 12:09 AM
copy_to_user nili Linux - Kernel 8 05-20-2008 04:08 AM


All times are GMT -5. The time now is 02:47 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration