1

I want to implement a system call where I pass in a reference to a structure variable, then I would like display the values in the same file.

For example I have the following structure.

struct procInfo{
int processID[64]; // the PID of each process
};

Let's say the system call I want to implement is int getProcessIds(struct procInfo*) and I call it inside a file called pcid.c.

I want the system call to grab the process ids from the scheduler inside proc.c file so I can print them inside my pcid.c file.

I know how to create regular system calls that don't have input parameters. I also know how to print the process ids using this system call inside proc.c file, however I don't know how to print them inside pcid.c file. I don't understand how the strucutre pointer is returned so I can print it inside the pcid.c.

I followed a similar system call int fstat(int fd, struct stat*) but I don't see how the structure pointer is returned.

I hope my question is clear, I am using XV6 operating system, thanks!

Updated

I was able to get it to work, I didn't need to use malloc to allocate memory. Here is the strange thing though, I added another variable to my structure so here is what it became.

struct procInfo{
int processID[64]; // the PID of each process
char processname[64][16] // the name of each process
};

After the system call inside the proc.c file, here is how I am printing the values.

printf(1,"Name = %s\n" ,procInfo->processname[0]);
printf(1,"PID = %d\n" , procInfo->processID[0] );

But the strange thing is that I get the trap 14 err 4 on cpu 1 eip 0x510 addr 0x7417ba08--kill proc, however I tried printing only one value and it worked.

printf(1,"Name = %s\n" ,procInfo->processname[0]);
//printf(1,"PID = %d\n" , procInfo->processID[0] );

Or

//printf(1,"Name = %s\n" ,procInfo->processname[0]);
printf(1,"PID = %d\n" , procInfo->processID[0] );

Why did it work when I only print one of them ? Am I printing correctly?

Ammar
  • 1,203
  • 5
  • 27
  • 64

1 Answers1

1

The pointer is not returned but points to an allocated memory where the syscall will write. The memory needs to be allocated by the caller before the syscall is called.

If there is already such a struct allocated inside proc.c, you should copy it to the provided buffer with memcpy. You should never pass reference to kernel memory to user-space programs, apart from being huge security risk it may also change at any time without the program's knowledge or be in a memory region not accessible by the program.

A typical usage would be this:

A user-space part:

struct procInfo info;
getProcessIds(&info);

A kernel-space part:

int getProcessIds(struct procInfo *info)
{
    struct procInfo *localInfo = getProccessInfoFromScheduler();
    memcpy(info, localInfo, sizeof(struct procInfo));
    return 0;
}
StenSoft
  • 9,369
  • 25
  • 30
  • so if I declare the following `struct procInfo *procInfo;`, how would I allocate memory for it before I make the system call? Right now I make the system call but I am not allocating any memory for the structure. – Ammar Feb 11 '15 at 02:38
  • 1
    `malloc(sizeof(struct procInfo))`. Or you can just allocate it on stack (define it without `*`) as is commonly used for `fstat`. – StenSoft Feb 11 '15 at 02:39
  • Are you saying like this? `struct procInfo *procInfo= malloc(sizeof(struct procInfo));` I don't think it worked! – Ammar Feb 11 '15 at 02:45
  • When I try to print the `procInfo->processID[0]` after the system call in `proc.c`I get this error `trap 14 err 4 on cpu 1 eip 0x7000631 addr 0x7000631--kill proc` – Ammar Feb 11 '15 at 02:53
  • The trap means your instruction pointer got corrupted probably by some buffer over/underrun. I guess the system call never successfully returns. – StenSoft Feb 11 '15 at 02:59
  • Strange, inside my `user.h` I had `int getProcessIds(struct procInfo *);`, then I changed it to `int getProcessIds(struct procInfo*);` then the error disappeared. It didn't like the space before the *. Anyway right now, I am not getting back the expected values. I get back some process id but its not correct. – Ammar Feb 11 '15 at 03:15
  • Are you sure about the malloc because right now I think `struct procInfo *procInfo;` in the `pcid.c` is pointing to a different memory location than the `struct procInfo *procInfo;` in the `proc.c` file. I have my system call implemented in there so I can read what's in the scheduler table. – Ammar Feb 11 '15 at 03:26
  • could you show me how to use the `memcpy` for such implementation? – Ammar Feb 11 '15 at 16:56