1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
// See LICENSE for license details.
#include "usermem.h"
#include "mmap.h"
#include <string.h>
#include <stdint.h>
void memset_user(void* dst, int ch, size_t n)
{
if ((uintptr_t)dst + n < (uintptr_t)dst || !is_uva(dst + n - 1))
handle_page_fault((uintptr_t)dst, PROT_WRITE);
uintptr_t sstatus = set_csr(sstatus, SSTATUS_SUM);
memset(dst, ch, n);
write_csr(sstatus, sstatus);
}
void memcpy_to_user(void* dst, const void* src, size_t n)
{
if ((uintptr_t)dst + n < (uintptr_t)dst || !is_uva(dst + n - 1))
handle_page_fault((uintptr_t)dst, PROT_WRITE);
uintptr_t sstatus = set_csr(sstatus, SSTATUS_SUM);
memcpy(dst, src, n);
write_csr(sstatus, sstatus);
}
void memcpy_from_user(void* dst, const void* src, size_t n)
{
if ((uintptr_t)src + n < (uintptr_t)src || !is_uva(src + n - 1))
handle_page_fault((uintptr_t)src, PROT_READ);
uintptr_t sstatus = set_csr(sstatus, SSTATUS_SUM);
memcpy(dst, src, n);
write_csr(sstatus, sstatus);
}
bool strcpy_from_user(char* dst, const char* src, size_t n)
{
bool res = false;
uintptr_t sstatus = set_csr(sstatus, SSTATUS_SUM);
while (n > 0) {
if (!is_uva(src))
handle_page_fault((uintptr_t)src, PROT_READ);
char ch = *(volatile const char*)src;
*dst = ch;
if (ch == 0) {
res = true;
break;
}
src++;
dst++;
n--;
}
write_csr(sstatus, sstatus);
return res;
}
|