matos/core/uaccess.c
2023-11-08 21:00:38 +01:00

55 lines
1.1 KiB
C

#include "assert.h"
#include "errno.h"
#include "mmuContext.h"
#include "paging.h"
#include "process.h"
#include "thread.h"
#include "uaccess.h"
static int bindtoUserContext()
{
struct thread *currentThread;
struct mmu_context *ctx;
currentThread = getCurrentThread();
assert(NULL != currentThread->process);
/* Switch to the user's address space */
ctx = processGetMMUContext(currentThread->process);
return threadChangeCurrentContext(ctx);
}
static int unbindUserContext()
{
return threadChangeCurrentContext(NULL);
}
static int memcpyUserMemNoCheck(vaddr_t dest, vaddr_t src, size_t size)
{
int ret;
ret = bindtoUserContext();
if (ret != 0)
return ret;
memcpy((void *)dest, (void *)src, size);
ret = unbindUserContext();
if (ret != 0)
return ret;
return size;
}
int memcpyFromUser(vaddr_t to, uaddr_t from, size_t size)
{
if ((uint)from < PAGING_BASE_USER_ADDRESS)
return -EPERM;
if ((uint)from > PAGING_TOP_USER_ADDRESS - size)
return -EPERM;
return memcpyUserMemNoCheck(to, from, size);
}