<p dir="ltr">Hi</p>
<p dir="ltr">I have already tried that value by checking in forums but got the same error.</p>
<div class="gmail_quote">On Jun 22, 2016 9:46 PM, "Navin P.S" <<a href="mailto:navinp1912@gmail.com">navinp1912@gmail.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Wed, Jun 22, 2016 at 8:28 PM, Avantika Rawat <<a href="mailto:avani.rawat@gmail.com">avani.rawat@gmail.com</a>> wrote:<br>
> Hi ,<br>
><br>
> Thanks for the link but i am getting "Function not implemented" error.<br>
> I have added a custom syscall in kernel for testing, getting similar<br>
> error for that also. I am working on 3.10.20 Kernel and MIPS<br>
> architecture. Pasting the kernel changes done to spport getrandom<br>
> syscall in 3.10.20 kernel. Can someone please help me here that what<br>
> needs to be done.<br>
><br>
> --- linux-3.10.20/arch/x86/syscalls/syscall_32.tbl 2016-06-15<br>
> 11:51:27.950025308 +0530<br>
> +++ linux-3.10.20_mod/arch/x86/syscalls/syscall_32.tbl 2016-06-15<br>
> 11:48:02.222020102 +0530<br>
> @@ -358,3 +358,4 @@<br>
> 349 i386 kcmp sys_kcmp<br>
> 350 i386 finit_module sys_finit_module<br>
> 351 i386 msa sys_msa<br>
> +352 i386 getrandom sys_getrandom<br>
> --- linux-3.10.20/arch/x86/syscalls/syscall_64.tbl 2016-06-15<br>
> 11:51:27.950025308 +0530<br>
> +++ linux-3.10.20_mod/arch/x86/syscalls/syscall_64.tbl 2016-06-15<br>
> 11:48:16.118020453 +0530<br>
> @@ -321,6 +321,7 @@<br>
> 312 common kcmp sys_kcmp<br>
> 313 common finit_module sys_finit_module<br>
> 314 common msa sys_msa<br>
> +315 common getrandom sys_getrandom<br>
><br>
> #<br>
> # x32-specific system call numbers start at 512 to avoid cache impact<br>
> --- linux-3.10.20/drivers/char/random.c 2016-06-15 11:51:27.998025309 +0530<br>
> +++ linux-3.10.20_mod/drivers/char/random.c 2016-06-15 11:48:34.774020925 +0530<br>
> @@ -265,6 +265,8 @@<br>
> #include <asm/irq.h><br>
> #include <asm/irq_regs.h><br>
> #include <asm/io.h><br>
> +#include <linux/syscalls.h><br>
> +#include <linux/completion.h><br>
><br>
> #define CREATE_TRACE_POINTS<br>
> #include <trace/events/random.h><br>
> @@ -397,6 +399,7 @@ static struct poolinfo {<br>
> */<br>
> static DECLARE_WAIT_QUEUE_HEAD(random_read_wait);<br>
> static DECLARE_WAIT_QUEUE_HEAD(random_write_wait);<br>
> +static DECLARE_WAIT_QUEUE_HEAD(urandom_init_wait);<br>
> static struct fasync_struct *fasync;<br>
><br>
> static bool debug;<br>
> @@ -1018,13 +1021,14 @@ static ssize_t extract_entropy_user(stru<br>
> {<br>
> ssize_t ret = 0, i;<br>
> __u8 tmp[EXTRACT_SIZE];<br>
> + int large_request = (nbytes > 256);<br>
><br>
> trace_extract_entropy_user(r->name, nbytes, r->entropy_count, _RET_IP_);<br>
> xfer_secondary_pool(r, nbytes);<br>
> nbytes = account(r, nbytes, 0, 0);<br>
><br>
> while (nbytes) {<br>
> - if (need_resched()) {<br>
> + if (large_request && need_resched()) {<br>
> if (signal_pending(current)) {<br>
> if (ret == 0)<br>
> ret = -ERESTARTSYS;<br>
> @@ -1158,7 +1162,7 @@ void rand_initialize_disk(struct gendisk<br>
> #endif<br>
><br>
> static ssize_t<br>
> -random_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)<br>
> +_random_read(int nonblock, char __user *buf, size_t nbytes)<br>
> {<br>
> ssize_t n, retval = 0, count = 0;<br>
><br>
> @@ -1183,7 +1187,7 @@ random_read(struct file *file, char __us<br>
> n*8, (nbytes-n)*8);<br>
><br>
> if (n == 0) {<br>
> - if (file->f_flags & O_NONBLOCK) {<br>
> + if (nonblock) {<br>
> retval = -EAGAIN;<br>
> break;<br>
> }<br>
> @@ -1215,6 +1219,13 @@ random_read(struct file *file, char __us<br>
> }<br>
><br>
> static ssize_t<br>
> +random_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)<br>
> + {<br>
> + return _random_read(file->f_flags & O_NONBLOCK, buf, nbytes);<br>
> + }<br>
> +<br>
> +<br>
> +static ssize_t<br>
> urandom_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)<br>
> {<br>
> return extract_entropy_user(&nonblocking_pool, buf, nbytes);<br>
> @@ -1340,6 +1351,29 @@ const struct file_operations urandom_fop<br>
> .llseek = noop_llseek,<br>
> };<br>
><br>
> +SYSCALL_DEFINE3 (getrandom, char __user *, buf, size_t, count,<br>
> + unsigned int, flags)<br>
> + {<br>
> + if (flags & ~(GRND_NONBLOCK|GRND_RANDOM))<br>
> + return -EINVAL;<br>
> +<br>
> + if (count > INT_MAX)<br>
> + count = INT_MAX;<br>
> +<br>
> + if (flags & GRND_RANDOM)<br>
> + return _random_read(flags & GRND_NONBLOCK, buf, count);<br>
> +<br>
> + if (unlikely(nonblocking_pool.initialized == 0)) {<br>
> + if (flags & GRND_NONBLOCK)<br>
> + return -EAGAIN;<br>
> + wait_event_interruptible(urandom_init_wait,<br>
> + nonblocking_pool.initialized);<br>
> + if (signal_pending(current))<br>
> + return -ERESTARTSYS;<br>
> + }<br>
> + return urandom_read(NULL, buf, count, NULL);<br>
> + }<br>
> +<br>
> /***************************************************************<br>
> * Random UUID interface<br>
> *<br>
> --- linux-3.10.20/include/linux/syscalls.h 2016-06-15 11:51:28.246025315 +0530<br>
> +++ linux-3.10.20_mod/include/linux/syscalls.h 2016-06-15<br>
> 11:49:11.918021865 +0530<br>
> @@ -852,4 +852,6 @@ asmlinkage long sys_process_vm_writev(pi<br>
> asmlinkage long sys_kcmp(pid_t pid1, pid_t pid2, int type,<br>
> unsigned long idx1, unsigned long idx2);<br>
> asmlinkage long sys_finit_module(int fd, const char __user *uargs, int flags);<br>
> +asmlinkage long sys_getrandom(char __user *buf, size_t count,<br>
> + unsigned int flags);<br>
> #endif<br>
> --- linux-3.10.20/include/uapi/asm-generic/unistd.h 2016-06-15<br>
> 11:51:28.258025315 +0530<br>
> +++ linux-3.10.20_mod/include/uapi/asm-generic/unistd.h 2016-06-15<br>
> 11:49:32.558022388 +0530<br>
> @@ -694,9 +694,11 @@ __SYSCALL(__NR_kcmp, sys_kcmp)<br>
> __SYSCALL(__NR_finit_module, sys_finit_module)<br>
> #define __NR_msa 274<br>
> __SYSCALL(__NR_msa, sys_msa)<br>
> +#define __NR_getrandom 275<br>
> +__SYSCALL(__NR_getrandom, sys_getrandom)<br>
><br>
> #undef __NR_syscalls<br>
> -#define __NR_syscalls 275<br>
> +#define __NR_syscalls 276<br>
><br>
> /*<br>
> * All syscalls below here should go away really,<br>
> --- linux-3.10.20/include/uapi/linux/random.h 2013-11-21<br>
> 01:58:01.000000000 +0530<br>
> +++ linux-3.10.20_mod/include/uapi/linux/random.h 2016-06-15<br>
> 11:50:06.350023243 +0530<br>
> @@ -40,6 +40,15 @@ struct rand_pool_info {<br>
> __u32 buf[0];<br>
> };<br>
><br>
> +/*<br>
> + * Flags for getrandom(2)<br>
> + *<br>
> + * GRND_NONBLOCK Don't block and return EAGAIN instead<br>
> + * GRND_RANDOM Use the /dev/random pool instead of /dev/urandom<br>
> + */<br>
> +#define GRND_NONBLOCK 0x0001<br>
> +#define GRND_RANDOM 0x0002<br>
> +<br>
> struct rnd_state {<br>
> __u32 s1, s2, s3;<br>
> };<br>
><br>
><br>
> On Wed, Jun 22, 2016 at 12:40 AM, Manuel Pégourié-Gonnard<br>
> <<a href="mailto:mpg@elzevir.fr">mpg@elzevir.fr</a>> wrote:<br>
>> Hi,<br>
>><br>
>> Maybe your unistd.h comes from an older kernel? That would explain why<br>
>> it doesn't define that symbol.<br>
>><br>
>> Here [1] is some code using that syscall successfully, you can see it<br>
>> looks a lot like Sayutin's code, except it does a few more compile-time<br>
>> and runtime checks to fallback on using /dev/urandom if necessary.<br>
>><br>
>> [1]:<br>
>> <a href="https://github.com/ARMmbed/mbedtls/blob/dd9895d8101f17ce804830472cbb140eba1c46a0/library/entropy_poll.c#L85" rel="noreferrer" target="_blank">https://github.com/ARMmbed/mbedtls/blob/dd9895d8101f17ce804830472cbb140eba1c46a0/library/entropy_poll.c#L85</a><br>
>><br>
>> Manuel.<br>
>><br>
>><br>
>> On 21/06/2016 15:41, Avantika Rawat wrote:<br>
>>> Hi,<br>
>>><br>
>>> Thanks for the suggestion but i am getting following error<br>
>>><br>
>>> error: 'SYS_getrandom' undeclared (first use in this function)<br>
>>><br>
>>> then i have replaced the SYS_getrandom by 275 in syscall(275, -- args--);<br>
>>> as 275 is the __NR_getrandom defines in unistd.h file, but it is<br>
>>> returning -1.<br>
>>> Can somebody tell me what i am missing here??<br>
>>><br>
>>> . Also in /proc/kallsyms it is showing two syscalls added for getrandom<br>
>>><br>
>>> ffffffff80417a38 T SyS_getrandom<br>
>>> ffffffff80417a38 T sys_getrandom<br>
>>><br>
>>> On Sat, Jun 18, 2016 at 7:12 PM, Sayutin Dmitry <<a href="mailto:cdkrot@yandex.ru">cdkrot@yandex.ru</a>> wrote:<br>
>>>> Well, in fact it is not hard.<br>
>>>><br>
>>>> Just use syscall(2) provided by libc.<br>
>>>><br>
>>>> You need to provide to this function syscall id and syscall args.<br>
>>>> Syscall id can be retrieved from macro constant<br>
>>>> Should look something like:<br>
>>>><br>
>>>> #define _GNU_SOURCE<br>
>>>> #include <unistd.h><br>
>>>> #include <sys/syscall.h><br>
>>>><br>
>>>> syscall(SYS_getrandom, -- your - args - here --);<br>
>>>><br>
>>>> It returns long value - the result of syscall.<br>
>>>> If it is between [-4095; -1] then it is negated errno, otherways it is return value.<br>
>>>><br>
>>>><br>
>>>> 18.06.2016, 16:32, "Anoop" <<a href="mailto:anoop.chargotra@gmail.com">anoop.chargotra@gmail.com</a>>:<br>
>>>>> Hi Avantika,<br>
>>>>><br>
>>>>> On Sat, Jun 18, 2016 at 5:32 PM, Avantika Rawat <<a href="mailto:avani.rawat@gmail.com">avani.rawat@gmail.com</a>> wrote:<br>
>>>>>> Hi ALL,<br>
>>>>>><br>
>>>>>> I am trying to use getrandom syscall in kernel 3.10.20 by following this<br>
>>>>>> link<br>
>>>>>><br>
>>>>>> <a href="https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=c6e9d6f38894798696f23c8084ca7edbf16ee895" rel="noreferrer" target="_blank">https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=c6e9d6f38894798696f23c8084ca7edbf16ee895</a><br>
>>>>>><br>
>>>>>> i have compiled the kernel and now want to call the getrandom syscall from<br>
>>>>>> userspace to generate random numbers. But i am getting following error while<br>
>>>>>> calling the getrandom () from userspace.<br>
>>>>>><br>
>>>>>> (.text.startup+0x18): undefined reference to `getrandom'<br>
>>>>>> (.text.startup+0x1c): undefined reference to `getrandom'<br>
>>>>><br>
>>>>> Your user space program will not know where 'getrandom' is defined<br>
>>>>> unless it's in the C library. You need to research more on how to call<br>
>>>>> custom system calls.<br>
>>>>><br>
>>>>> -Anoop<br>
>>>>><br>
>>>>>> --<br>
>>>>>> Regards,<br>
>>>>>> Avantika Rawat<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" rel="noreferrer" target="_blank">http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies</a><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" rel="noreferrer" target="_blank">http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies</a><br>
>>>><br>
>>>> -----<br>
>>>> Sayutin Dmitry <<a href="mailto:cdkrot@yandex.com">cdkrot@yandex.com</a>><br>
>>><br>
>>><br>
>>><br>
><br>
><br>
><br>
> --<br>
> Regards,<br>
> Avantika Rawat<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" rel="noreferrer" target="_blank">http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies</a><br>
<br>
I think you need to 352 or 315 ie the values from .tbl (so 315 if you<br>
are calling on a x64 kernel or 352 if you are calling x86 kernel). The<br>
275 is for the uapi but syscall accepts the 315 value on a x64.<br>
<br>
Please try with 315 and 352 after running your modified kernel and<br>
report back. Atleast that is the way it worked for me on a 4.x kernel.<br>
Ideally uapi is for userspace like glibc and they come man 3 where as<br>
the syscall is (man 2) ie system call.<br>
<br>
--<br>
-- Navin<br>
</blockquote></div>