2021-11-09 20:35:55 +01:00
|
|
|
#include "assert.h"
|
2023-11-08 20:53:13 +01:00
|
|
|
#include "errno.h"
|
2021-11-09 20:35:55 +01:00
|
|
|
#include "mmuContext.h"
|
|
|
|
#include "paging.h"
|
2023-11-08 20:53:13 +01:00
|
|
|
#include "process.h"
|
2021-11-09 20:35:55 +01:00
|
|
|
#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);
|
|
|
|
}
|