diff options
Diffstat (limited to 'kernel/api/syscall.c')
-rw-r--r-- | kernel/api/syscall.c | 30 |
1 files changed, 17 insertions, 13 deletions
diff --git a/kernel/api/syscall.c b/kernel/api/syscall.c index 467b7d6..7c76e51 100644 --- a/kernel/api/syscall.c +++ b/kernel/api/syscall.c @@ -77,11 +77,14 @@ static long sys_read(read_args_t *args) ERROR_OUT_RET(ret); // Allocate a temporary buffer (a page-aligned block of n pages that are enough space to store the number of bytes to read) - size_t size_in_pages = (kargs.nbytes + PAGE_SIZE - 1) / PAGE_SIZE; - void *addr = (void *)page_alloc_n(size_in_pages); + size_t size_in_pages = 0; + while(++size_in_pages * PAGE_SIZE < kargs.nbytes) + ; + void *addr = page_alloc_n(size_in_pages); if (!addr) { - ERROR_OUT_RET(-ENOMEM); + ret = -ENOMEM; + ERROR_OUT_RET(ret); } // Call do_read() with the buffer and then copy the buffer to the userland args after the system call @@ -100,7 +103,7 @@ static long sys_read(read_args_t *args) } // copy the buffer to the userland args after the system call - ret = copy_to_user(kargs.buf, addr, ret); + ret = copy_to_user(kargs.buf, addr, kargs.nbytes); // if ret < 0, free the temporary buffer and return -1 if (ret < 0) { @@ -132,7 +135,9 @@ static long sys_write(write_args_t *args) ERROR_OUT_RET(ret); // Allocate a temporary buffer (a page-aligned block of n pages that are enough space to store the number of bytes to write) - size_t size_in_pages = (kargs.nbytes + PAGE_SIZE - 1) / PAGE_SIZE; + size_t size_in_pages = 0; + while(++size_in_pages * PAGE_SIZE < kargs.nbytes) + ; void *addr = (void *)page_alloc_n(size_in_pages); if (!addr) { @@ -187,10 +192,9 @@ static long sys_getdents(getdents_args_t *args) ERROR_OUT_RET(-EINVAL); } - size_t count_read = 0; - - // iterate over the directory entries - while (count_read * sizeof(dirent_t) <= kargs.count) + // iterate over the directory entries to get the number of dirs + size_t num_dirs = 0; + while (num_dirs * sizeof(dirent_t) < kargs.count) { // read count / sizeof(dirent_t) directory entries into the provided dirp and call do_getdent dirent_t d; @@ -209,13 +213,13 @@ static long sys_getdents(getdents_args_t *args) } // copy the dirent_t to the userland args after the system call - ret = copy_to_user(kargs.dirp + count_read, &d, sizeof(dirent_t)); + ret = copy_to_user(kargs.dirp + num_dirs, &d, sizeof(dirent_t)); ERROR_OUT_RET(ret); // error check - count_read++; + num_dirs++; } - return count_read * sizeof(dirent_t); + return num_dirs * sizeof(dirent_t); } #ifdef __MOUNTING__ @@ -868,4 +872,4 @@ static long syscall_dispatch(size_t sysnum, uintptr_t args, regs_t *regs) curthr->kt_errno = ENOSYS; return -1; } -} +}
\ No newline at end of file |