Call read() in OS161

2018-05-05  本文已影响0人  Jack_Jiang

1. asst2.c -> main() -> read(fd, &buf[i], MAX_BUF - i)

read() function only has prototype in unistd.h
ssize_t read(int filehandle, void *buf, size_t size);
but has no defination, call read() function will call the magic assembly code

2. the magic assembly code

the magic assembly code will create a trapframe structure (defined in trapframe.h) as following:

and then use this trapframe as an argument to invoke mips_trap() function

3. trap.c -> mips_trap(struct trapframe *tf)

in this function, it pass the argument tf and invoke syscall(tf)
of course, it does something else, but these are not important to know for this assignment.

4. syscall.c -> syscall(struct trapframe *tf)

switch (callno) {
        case SYS_read:
        Call our function, for example, sys_read() in file.c
        break;

remember we already have filehandle, *buf and size in trapframe, we can easily retreive these information

int filehandle = tf->tf_a0
void * buf = (void*)tf->tf_a1
size_t size = (size_t)tf->tf_a2

5. file.c -> sys_read()

in this file, we write our sys_read() function, the return value is worth to be noticed.

the return of read() function

Based on OS/161 Reference Manual - Read, read function should return like this:

the return of sys_read() function

the reason why the return value of sys_read() will lead to the return value of read() is following. This is not important to know for this assignment.

6. syscall.c -> syscall()

this code save err and retval to some particular trapframe, when those function returns, the magic assembly code will use the trapframe to generate the return value for read().

    if (err) {
        /*
         * Return the error code. This gets converted at
         * userlevel to a return value of -1 and the error
         * code in errno.
         */
        tf->tf_v0 = err;
        tf->tf_a3 = 1;      /* signal an error */
    }
    else {
        /* Success. */
        tf->tf_v0 = retval;
        tf->tf_a3 = 0;      /* signal no error */
    }
上一篇下一篇

猜你喜欢

热点阅读