kernel: usercopy functions

This commit is contained in:
Matheus
2024-06-09 19:31:26 -03:00
parent 429f56aced
commit 50b4b45d00
53 changed files with 712 additions and 260 deletions
+1
View File
@@ -51,6 +51,7 @@ typedef struct thread_t {
int wakeupreason;
bool shouldexit;
void *kernelarg;
context_t *usercopyctx;
struct {
spinlock_t lock;
stack_t stack;
+14 -3
View File
@@ -5,6 +5,7 @@
#include <kernel/vfs.h>
#include <mutex.h>
#include <kernel/poll.h>
#include <kernel/usercopy.h>
#define SOCKET_STATE_UNBOUND 0
#define SOCKET_STATE_BOUND 1
@@ -93,11 +94,16 @@ static inline int sock_addrtoabiaddr(int socktype, sockaddr_t *sockaddr, abisock
}
static inline int sock_copymsghdr(msghdr_t *khdr, msghdr_t *uhdr) {
memcpy(khdr, uhdr, sizeof(msghdr_t));
if (usercopy_fromuser(khdr, uhdr, sizeof(msghdr_t)))
return EFAULT;
iovec_t *iovectmp = alloc(sizeof(iovec_t) * khdr->iovcount);
if (iovectmp == NULL)
return ENOMEM;
memcpy(iovectmp, khdr->iov, sizeof(iovec_t) * khdr->iovcount);
if (usercopy_fromuser(iovectmp, khdr->iov, sizeof(iovec_t) * khdr->iovcount))
return EFAULT;
khdr->iov = iovectmp;
if (khdr->addr) {
@@ -106,7 +112,12 @@ static inline int sock_copymsghdr(msghdr_t *khdr, msghdr_t *uhdr) {
free(iovectmp);
return ENOMEM;
}
memcpy(addrtmp, khdr->addr, khdr->addrlen);
if (usercopy_fromuser(addrtmp, khdr->addr, khdr->addrlen)) {
free(iovectmp);
return EFAULT;
}
khdr->addr = addrtmp;
}
+1
View File
@@ -6,6 +6,7 @@
#include <kernel/vfs.h>
#include <kernel/file.h>
#include <kernel/scheduler.h>
#include <kernel/usercopy.h>
typedef struct {
uint64_t ret;
+29
View File
@@ -0,0 +1,29 @@
#ifndef _USERCOPY_H
#define _USERCOPY_H
#include <stddef.h>
#include <stdint.h>
#include <string.h>
//IS_USER_ADDRESS(a) is provided by arch/mmu.h
#include <arch/mmu.h>
static inline int _usercopy_memcpy_wrapper(void *dst, void *src, size_t size) {
memcpy(dst, src, size);
return 0;
}
static inline int _usercopy_strlen_wrapper(void *str, size_t *size) {
*size = strlen(str);
return 0;
}
#define USERCOPY_POSSIBLY_FROM_USER(kernel, user, size) (IS_USER_ADDRESS(user) ? usercopy_fromuser(kernel, user, size) : _usercopy_memcpy_wrapper(kernel, user, size))
#define USERCOPY_POSSIBLY_TO_USER(user, kernel, size) (IS_USER_ADDRESS(user) ? usercopy_touser(user, kernel, size) : _usercopy_memcpy_wrapper(user, kernel, size))
#define USERCOPY_POSSIBLY_STRLEN_FROM_USER(user, sizep) (IS_USER_ADDRESS(user) ? usercopy_strlen(user, sizep) : _usercopy_strlen_wrapper(user, sizep))
int usercopy_touser(void *user, void *kernel, size_t size);
int usercopy_fromuser(void *kernel, void *user, size_t size);
int usercopy_fromuseratomic32(uint32_t *user32, uint32_t *value);
int usercopy_strlen(const char *, size_t *size);
#endif