0

I've just added a system call to the linux kernel. It simply takes a int argument and return a int. This is system call code:

asmlinkage int sys_mycall(int num) 
{ printk("This is mycall.");
  if(num%2==0)
  num = 100003;
  else
  num = 3; 
  return num; 
}

This is the code which should run the system call for testing:

#include<stdio.h>
#include<unistd.h>
int main()
{
  int num;
  num = syscall(335,1);
  printf("%d",num);
}

My problem is that no matter what my argument is(like num = 1 or num = 2), the system only output 100003, never have 3.

New: I changed 3 places. enter image description here enter image description here enter image description here

dmesg enter image description here

uname -a enter image description here

Shaw
  • 9
  • 2
  • How about having your `printk` print out the value of `num`? – Nate Eldredge May 21 '20 at 01:09
  • 1
    What architecture are you running? If x86, is it 32 or 64 bit? – Nate Eldredge May 21 '20 at 01:12
  • Thanks for your comment. It's OK, but I have to recompile and wait for a long long time. And I want to know what is wrong. – Shaw May 21 '20 at 01:13
  • I installed Ubuntu 16.04 64 bit on the virtual machine – Shaw May 21 '20 at 01:14
  • Another thought: what do you get when you run your userspace program under `strace`? You should see a line like `syscall_0x14f` near the end; what is the rest of it? – Nate Eldredge May 21 '20 at 01:14
  • Thank you. I'm new in Linux, please wait a moment and I will have a look. – Shaw May 21 '20 at 01:18
  • 1
    It's just a hypothesis, but it seems that the syscall receives always a null parameter, so probably you are not passing "num". Have you defined the macro with the correct number of parameters? It's all explained here: https://stackoverflow.com/questions/53735886/how-to-pass-parameters-to-linux-system-call – Michael Moretti May 21 '20 at 01:36
  • 1
    In order to add a syscall there is a lot more stuff that needs to be modified in the kernel than what you show. Please list **all** the modifications you applied, and also your kernel version and architecture (`uname -a`). Otherwise it's impossible to answer. – Marco Bonelli May 21 '20 at 01:38
  • Thank you, I'll edit immediately. And I think it's possible that I haven't passing the num argument actually. But I have no idea what to modify. – Shaw May 21 '20 at 01:47
  • I have uploaded three pictures, and can you see that? I'm in China, maybe I can't visit i.stack.imgur.com – Shaw May 21 '20 at 02:02
  • Just at a quick glance: (1) you are not using `SYSCALL_DEFINE1` to define your syscall as you should; (2) you are adding the syscall signature right under a comment that says "these definitions are only valid on a pure 32bit system" and yours is not; (3) you did not add your syscall to `arch/x86/entry/syscalls/syscall_64.tbl`. Take a look at the post linked above by @MichaelMoretti, it looks like the same issue. – Marco Bonelli May 21 '20 at 02:07
  • Thank you, I actually haven't done (1) and (2), "syscall_64.tbl" I have modified. – Shaw May 21 '20 at 02:13
  • 1
    **DO NOT post images of code, data, error messages, etc.** - copy or type the text into the question. [ask] – Rob May 21 '20 at 02:14
  • Sorry for that, I'll edit next time. I'm new in stackoverflow :) – Shaw May 21 '20 at 02:16

0 Answers0