How to implement syscall in XV6

89 Views Asked by At

I have this assignment:

Implement a syscall that returns information about the current process (e.g. its process ID and state). Think about who is responsible for allocating the memory that contains the process information that will be returned and how it can be freed again after use (if it needs to be freed).

But im stuck at making the syscall produce any meaningful output...

This is the code in my kernel space, implemented in sysproc.c:

uint64
sys_getprocinfo(struct proc* user_pi)
{
  struct proc *curproc = myproc();
  struct proc pi;

  acquire(&curproc->lock);
  pi.pid = curproc->pid;
  pi.state = curproc->state;
  release(&curproc->lock);

  if(copyout(curproc->pagetable, (uint64)user_pi, (char*)&pi, sizeof(pi)) < 0)
        return -1;

  return 0;
}

And the user space program in user/getproc.c

#include "user.h"
//#include "kernel/types.h"


//blueprint for proc info strukturen
struct proc {
        int pid;
        int state;
        //evt annet?
};

//state nr tilsvarer nr i array her:
//enum procstate { UNUSED, USED, SLEEPING, RUNNABLE, RUNNING, ZOMBIE}

int main(void)

{
  //info blir av typen proc info over
  struct proc pi;
  //kaller syscallet med &info som argument
  int result = getprocinfo(&pi);

  if (result < 0) {
        printf("Error: Not able to access process information!\n");
  } else {
        printf("Process ID: %d, Process state: %d\n", pi.pid, pi.state);
  }

  exit(0);

}

When i the run XV6 with make qemu, i only get this message: Error: Not able to access process information!. I cant make it produce any processes. Please help!!

1

There are 1 best solutions below

3
Mathieu On

I believe you use xv6-riscv flavor?

You have several problem in you code:

  • sys_getprocinfo prototype should be int64 sys_getprocinfo(void): system calls have no parameters, they have to get them with argaddr(int, uint64 *);
  • pi is defined local to sys_getprocinfo, but you use it as a user pointer in copyout(...)
  • You have two struct proc definition: one in kernel in proc.h one in you getproc.c file. They are incompatible. You should declare a new structure with a different name in the kernel.