<div dir="ltr"><div><div><div><div>Hi All,<br></div>Thanks Victor<br><br></div>Can any one please tell me difference between kernal space & user space in code perspective i.e in write function const char *buffer should point to address of *hello* in RAM so how it is changing when i am accessing through some kernel function because it is also accessing the same RAM address?<br><br></div>Thanks & Regards<br></div>Prasad <br><div><div><div><div><div><div><div class="gmail_extra"><br><div class="gmail_quote">On 1 January 2015 at 21:20, Victor Rodriguez <span dir="ltr"><<a href="mailto:vm.rod25@gmail.com" target="_blank">vm.rod25@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On Thu, Jan 1, 2015 at 2:24 AM, me storage <<a href="mailto:me.storage126@gmail.com">me.storage126@gmail.com</a>> wrote:<br>
> I am learning char drivers.But i didn't understand write operation of char<br>
> device driver properly. the below is my write operation<br>
><br>
> static ssize_t dev_write(struct file *fil,const char __user *buff,size_t<br>
> len,loff_t *off)<br>
> {<br>
> pr_info("user input string %s\n",buff);<br>
> pr_info("user input string len %d\n",len);<br>
> return len;<br>
> }<br>
><br>
> my doubt is if i write into my device like<br>
> echo "hello" > /dev/myDev<br>
><br>
> it is giving different behaviour like below is the dmesg<br>
> [20596.975355] user input string hello<br>
> [20596.975355] 77b9e4<br>
> [20596.975355] insmod insmod<br>
> [20596.975355] n/zeitgeist-daemon<br>
> [20596.975355] atives<br>
> [20596.975355]<br>
> [20596.975355] vars "${upargs[@]}"<br>
> [20596.975355] cur cword words=();<br>
> [20596.975355] local upargs=() upvars=() vcur vcword vprev vwords;<br>
> [20596.975355] while getopts "c:i:n:p:w:" flag "$@"; do<br>
> [20596.975355] case $flag in<br>
> [20596.975355] c)<br>
> [20596.975355] vcur=$OPTARG<br>
> [20596.975355] ;;<br>
> [20596.975355] i)<br>
> [20596.975355] vcword=$OPTARG<br>
> [20596.975355] ;;<br>
> [20596.975355] n)<br>
> [20596.975355] exclude=$OPTARG<br>
> [20596.975355] ;;<br>
> [20596.975355] p)<br>
> [20596.975355] vprev=$OPTARG<br>
> [20596.975355] ;;<br>
> [20596.975355] w)<br>
> [20596.975355] vwords=$OPTARG<br>
> [20596.975355] ;;<br>
> [20596.975358] user input string len 6<br>
> [20596.975361] Device closed<br>
><br>
> so i didn't understand what is happening inside .Can any one please explain<br>
> what is happening?<br>
<br>
</div></div>HI Prasad<br>
<br>
The problem is ont he part where you try to print the buffer on dmesg.<br>
I tested this code ( the base full code is on my github repo as<br>
char_simple.c *):<br>
<br>
106 static ssize_t device_write(struct file *filp,<br>
107 const char *buf,<br>
108 size_t len,<br>
109 loff_t * off)<br>
110 {<br>
111<br>
112 procfs_buffer_size = len;<br>
113<br>
114<br>
115 if ( copy_from_user(buffer_data, buf, procfs_buffer_size)<br>
) {<br>
116 return -EFAULT;<br>
117 }<br>
118 *off += len;<br>
119<br>
120 pr_info("user input string %s\n",buffer_data);<br>
121 //pr_info("user input string len<br>
%lu\n",procfs_buffer_size);<br>
122<br>
123 return procfs_buffer_size;<br>
124 }<br>
<br>
And the dmesg output is :<br>
<br>
[ 2735.251589] Hello from the Kernel !!! (how cool is that)<br>
[ 2735.251600] Major Number = 244<br>
[ 2735.251604] Name = mychardriver<br>
[ 2735.251607] Generate the device file with mknod<br>
/dev/mychardriver c 244 0<br>
[ 2766.806455] user input string hello there<br>
<br>
Remember to first do the copy from user space and then print that<br>
variable instead of just the buff variable. Print the len is fine<br>
(<a href="https://gist.github.com/17twenty/6313566" target="_blank">https://gist.github.com/17twenty/6313566</a> ) however when you try to<br>
print the string from the user space , there is where we have the<br>
problems (I did the experiment )<br>
<br>
Here are some useful links:<br>
<br>
<a href="http://www.ibm.com/developerworks/library/l-kernel-memory-access/" target="_blank">http://www.ibm.com/developerworks/library/l-kernel-memory-access/</a><br>
<a href="https://www.kernel.org/doc/htmldocs/kernel-api/ch04s02.html" target="_blank">https://www.kernel.org/doc/htmldocs/kernel-api/ch04s02.html</a><br>
<br>
Hope it helps<br>
<br>
Regards<br>
<br>
Victor Rodriguez<br>
<br>
*char_simple.c :<br>
<a href="https://github.com/VictorRodriguez/linux_device_drivers_tutorial/blob/master/char_simple.c" target="_blank">https://github.com/VictorRodriguez/linux_device_drivers_tutorial/blob/master/char_simple.c</a><br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
> Thanks & Regards<br>
> Prasad<br>
><br>
><br>
> _______________________________________________<br>
> Kernelnewbies mailing list<br>
> <a href="mailto:Kernelnewbies@kernelnewbies.org">Kernelnewbies@kernelnewbies.org</a><br>
> <a href="http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies" target="_blank">http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies</a><br>
><br>
</blockquote></div><br></div></div></div></div></div></div></div></div>