0
votes

Ok, so I have successfully added some system calls to kernel space. Currently I have their primary intention commented out, I wanted to be sure data was being passed from user space to kernel space successfully and as expected. I am currently having an issue where a declare a variable in user space as unsigned long, and when it is printed via printk, the value is not the same. I have viewed other issues on this topic which seem to be relevant and they were no help, i think the people were just using printk wrong. I had previous tested this with the kernel space only printing a string, all was fine, so then I added the data being passed from user space and hit this issue, So...

User space:

#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <linux/kernel.h>
#include <sys/syscall.h>

#define __NR_createQueue 350

long createQueue_syscall(unsigned long id){
     return syscall(__NR_createQueue);
}

int main(int argc, char *argv[]){
     unsigned long qid = 47;  // ID for a queue

     createQueue_syscall(qid);
     return 0;
}

Kernel Space:

#include linux/kernel.h  

asmlinkage long createQueue(unsigned long id){

  printk("  The queue ID is: %lu \n", id);

  return 0;
}

All compiles fine, no warnings or errors. But when i run: dmesg | tail -20 , I get a value like 1334886164 instead of the 47 I was expecting. (yes I did all of the make, make module_install install, reboot, etc required). It is as though it is grabbing garbage/over running the memory or something. But I am at a loss. Any thoughts/ideas?

Many Thanks!
1
One is in user space one is in kernel space, so i mean it doesnt matter?; the " id " is just a place holder for some unsigned long...user2152383
Because i am testing that the data being passed to kernel space is being handled properly, so i am printing to to kernel log to make sure that it is the right value. I dont want to cause a kernel panic or etc. There are notes that one is in kernel space, and the other is in user space.user2152383
There shuld also be a return 0 in the kernel space syscall but that is kind of irrelevant to the issue....user2152383
qid is a value to simulate a user/program input, that is being passed to a system call that takes a unsigned long as an arg.user2152383
I am not passing ID, ID is in the signature of the syscall definition. It is a custom syscall that I implemented. it takes a unsigned long as an arg. the top is the call to the syscall, the lower is the definition of the syscall.user2152383

1 Answers

0
votes

You need to change the function to:

long createQueue_syscall(unsigned long id){

    return syscall(__NR_createQueue, id); /* note the additional parameter */

}

I am guessing id is not passed and is hence assigned some random integer.