1

I am trying to implement a system call in Minix and I'm having trouble understanding where I need to go. This is the system call I need to add which returns 0 on success and -1 on failure while also returning a+b, a-b, and a*b:

int mycall(int a, int b, int *sum, int *difr, int *prod);

What I've done:

  • main.c:
#include <stdio.h>
int main()
{
    int a, b, d, p, r, s;
    a = 3;
    b = 2;
    int mycall(int a, int b, int *s, int *d, int *p);
    r = mycall(a, b, &s, &d, &p);
    fprintf(stderr, "%d,%d,%d,%d\n", r, s, d, p);
    return 0;
}
  • added mycall.c to same directory (given code has typos, I assume that k should be r):
#include <lib.h>
int mycall(int a, int b, int *sum, int *difr, int *prod)
{
    message m;
    int r;

    m.m1_i1 = a;
    m.m1_i2 = b;

    k = _syscall(MM, 58, &m);
    if(r < 0) return -1;
    *sum = m.m1_i1;
    *difr = m.m1_i2;
    *prod = m.m1_i3;
    return k;
}
  • added to /usr/mm/proto.h:
    _PROTOTYPE( int do_mycall, (void));
  • added do_mycall /* 58 = unused */ to /usr/mm/table.c

All that needs to be done is to implement the call in /usr/mm/utility.c. This is what I have so far:

PUBLIC int do_mycall()
{
    int a, b;
    a = mm_in.m1_i1;
    b = mm_in.m1_i2;

    /* my guess */
    mm_in.m1_i1 = a+b;
    mm_in.m1_i2 = a-b;
    mm_in.m1_i3 = a*b;

    /* mp_reply is in mproc struct of this process */

    return OK;
}

The current code when ran (after rebuilding the kernel) gives -1,0,0,0 which is wrong, but I am not sure how to proceed. When opening mproc.h, I can see message mp_reply; but I don't know how this helps.

Josh Brown
  • 47
  • 1
  • 9
  • I'm not sure what's wrong here, but I suggest you read their syscall page thoroughly and see an implementation of it. – Unmanned Player Mar 02 '20 at 04:49
  • Just to see where the code goes wrong, I threw in a `printf()` in utility.c function and got nothing, so I'm thinking the call never gets there which would explain why I'm not getting the correct results though I'm not sure where the error occurs. – Josh Brown Mar 02 '20 at 05:59
  • I wonder if `printf()` like functions will work inside a syscall. Maybe it does, given its a MT kernel. Please do post your reply here. Minix is awfully under represented. – Unmanned Player Mar 02 '20 at 21:05
  • According to this [exercise](http://homepages.cs.ncl.ac.uk/nick.cook/csc2025/minix/syscall-exercise1.html), they show they can use `printf()` so I assume I can though I'm not sure which version Minix they're using (I'm using 2), or if that would cause a difference. And Minix is way under represented. It's so hard to find any help. I'll probably end up talking to my professor, and once I get it working, post an answer here just in case anyone else finds themselves in my position. – Josh Brown Mar 02 '20 at 21:18
  • After almost an hour of searching I finally found the mproc struct file (no clue it was in `/usr/src/mm/mproc.h`), I know to use `mproc->mp_reply.m1_i1` (for example) but even after assigning values I still get the same output. I think my problem is that the function never gets called though I'm not sure where the link needs to be made... – Josh Brown Mar 02 '20 at 21:51

1 Answers1

0

Finally got it working thanks to a classmate. proto.h and table.c files are correct. Had to change all k's to r's in mycall.c. The big change was in utility.c and how I rebuilt the image.

utility.c code:

PUBLIC int do_mycall()
{
    int a, b;
    a = mm_in.m1_i1;
    b = mm_in.m1_i2;

    mproc[mm_in.m_source].mp_reply.m1_i1 = a+b;
    mproc[mm_in.m_source].mp_reply.m1_i2 = a-b;
    mproc[mm_in.m_source].mp_reply.m1_i3 = a*b;

    return OK;

}

Rebuilding the image (in Minix):

> make hdboot        (KEEP NOTE OF VERSION NUMBER AT END OF SCRIPT!)
> shutdown

In Minix boot monitor (QEMU):

d0p0>set
# change image to /minix/(YOUR VERSION)
# change net on image to ...;image=/minix/(YOUR VERSION NUMBER)
# save
d0p0>boot
Josh Brown
  • 47
  • 1
  • 9