<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 &amp; 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 &amp; 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">&lt;<a href="mailto:vm.rod25@gmail.com" target="_blank">vm.rod25@gmail.com</a>&gt;</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 &lt;<a href="mailto:me.storage126@gmail.com">me.storage126@gmail.com</a>&gt; wrote:<br>
&gt; I am learning char drivers.But i didn&#39;t understand write operation of char<br>
&gt; device driver properly. the below is my write operation<br>
&gt;<br>
&gt; static ssize_t dev_write(struct file *fil,const char __user *buff,size_t<br>
&gt; len,loff_t *off)<br>
&gt; {<br>
&gt;     pr_info(&quot;user input string %s\n&quot;,buff);<br>
&gt;     pr_info(&quot;user input string len %d\n&quot;,len);<br>
&gt;     return len;<br>
&gt; }<br>
&gt;<br>
&gt; my doubt is if i write into my device like<br>
&gt; echo &quot;hello&quot; &gt; /dev/myDev<br>
&gt;<br>
&gt; it is giving different behaviour like below is the dmesg<br>
&gt; [20596.975355] user input string hello<br>
&gt; [20596.975355] 77b9e4<br>
&gt; [20596.975355] insmod insmod<br>
&gt; [20596.975355] n/zeitgeist-daemon<br>
&gt; [20596.975355] atives<br>
&gt; [20596.975355]<br>
&gt; [20596.975355] vars &quot;${upargs[@]}&quot;<br>
&gt; [20596.975355]  cur cword words=();<br>
&gt; [20596.975355]     local upargs=() upvars=() vcur vcword vprev vwords;<br>
&gt; [20596.975355]     while getopts &quot;c:i:n:p:w:&quot; flag &quot;$@&quot;; do<br>
&gt; [20596.975355]         case $flag in<br>
&gt; [20596.975355]             c)<br>
&gt; [20596.975355]                 vcur=$OPTARG<br>
&gt; [20596.975355]             ;;<br>
&gt; [20596.975355]             i)<br>
&gt; [20596.975355]                 vcword=$OPTARG<br>
&gt; [20596.975355]             ;;<br>
&gt; [20596.975355]             n)<br>
&gt; [20596.975355]                 exclude=$OPTARG<br>
&gt; [20596.975355]             ;;<br>
&gt; [20596.975355]             p)<br>
&gt; [20596.975355]                 vprev=$OPTARG<br>
&gt; [20596.975355]             ;;<br>
&gt; [20596.975355]             w)<br>
&gt; [20596.975355]                 vwords=$OPTARG<br>
&gt; [20596.975355]             ;;<br>
&gt; [20596.975358] user input string len 6<br>
&gt; [20596.975361] Device closed<br>
&gt;<br>
&gt; so i didn&#39;t understand what is happening inside .Can any one please explain<br>
&gt; 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(&quot;user input string %s\n&quot;,buffer_data);<br>
121         //pr_info(&quot;user input string len<br>
%lu\n&quot;,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>
&gt; Thanks &amp; Regards<br>
&gt; Prasad<br>
&gt;<br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; Kernelnewbies mailing list<br>
&gt; <a href="mailto:Kernelnewbies@kernelnewbies.org">Kernelnewbies@kernelnewbies.org</a><br>
&gt; <a href="http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies" target="_blank">http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies</a><br>
&gt;<br>
</blockquote></div><br></div></div></div></div></div></div></div></div>