CS350 COS
COS
Loading...
Searching...
No Matches
copy.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2006-2023 Ali Mashtizadeh
3 * All rights reserved.
4 * Generic Copyin/Copyout routines
5 */
6
7#include <stdbool.h>
8#include <stdint.h>
9
10#include <errno.h>
11
12#include <sys/kassert.h>
13#include <machine/pmap.h>
14
15extern int copy_unsafe(void *to_addr, void *from_addr, uintptr_t len);
16extern int copystr_unsafe(void *to_addr, void *from_addr, uintptr_t len);
17
33int
34Copy_In(uintptr_t fromuser, void *tokernel, uintptr_t len)
35{
36 if (len == 0)
37 return 0;
38
39 // Kernel space
40 if (fromuser >= MEM_USERSPACE_TOP) {
41 kprintf("Copy_In: address exceeds userspace top\n");
42 return EFAULT;
43 }
44
45 // Wrap around
46 if (len > (MEM_USERSPACE_TOP - fromuser)) {
47 kprintf("Copy_In: length exceeds userspace top\n");
48 return EFAULT;
49 }
50
51 return copy_unsafe(tokernel, (void *)fromuser, len);
52}
53
69int
70Copy_Out(void *fromkernel, uintptr_t touser, uintptr_t len)
71{
72 if (len == 0)
73 return 0;
74
75 // Kernel space
76 if (touser >= MEM_USERSPACE_TOP) {
77 kprintf("Copy_Out: address exceeds userspace top\n");
78 return EFAULT;
79 }
80
81 // Wrap around
82 if (len > (MEM_USERSPACE_TOP - touser)) {
83 kprintf("Copy_Out: length exceeds userspace top\n");
84 return EFAULT;
85 }
86
87 return copy_unsafe((void *)touser, fromkernel, len);
88}
89
105int
106Copy_StrIn(uintptr_t fromuser, void *tokernel, uintptr_t len)
107{
108 if (len == 0)
109 return 0;
110
111 // Kernel space
112 if (fromuser >= MEM_USERSPACE_TOP) {
113 kprintf("Copy_StrIn: address exceeds userspace top\n");
114 return EFAULT;
115 }
116
117 // Wrap around
118 if (len > (MEM_USERSPACE_TOP - fromuser)) {
119 kprintf("Copy_StrIn: length exceeds userspace top\n");
120 return EFAULT;
121 }
122
123 return copystr_unsafe(tokernel, (void *)fromuser, len);
124}
125
141int
142Copy_StrOut(void *fromkernel, uintptr_t touser, uintptr_t len)
143{
144 if (len == 0)
145 return 0;
146
147 // Kernel space
148 if (touser >= MEM_USERSPACE_TOP) {
149 kprintf("Copy_StrOut: address exceeds userspace top\n");
150 return EFAULT;
151 }
152
153 // Wrap around
154 if (len > (MEM_USERSPACE_TOP - touser)) {
155 kprintf("Copy_StrOut: length exceeds userspace top\n");
156 return EFAULT;
157 }
158
159 return copystr_unsafe((void *)touser, fromkernel, len);
160}
161
int Copy_StrIn(uintptr_t fromuser, void *tokernel, uintptr_t len)
Definition: copy.c:106
int Copy_StrOut(void *fromkernel, uintptr_t touser, uintptr_t len)
Definition: copy.c:142
int Copy_In(uintptr_t fromuser, void *tokernel, uintptr_t len)
Definition: copy.c:34
int copystr_unsafe(void *to_addr, void *from_addr, uintptr_t len)
int Copy_Out(void *fromkernel, uintptr_t touser, uintptr_t len)
Definition: copy.c:70
int copy_unsafe(void *to_addr, void *from_addr, uintptr_t len)
#define EFAULT
Definition: errno.h:13
int kprintf(const char *fmt,...)
Definition: printf.c:210
uint64_t len
Definition: multiboot.h:2
#define MEM_USERSPACE_TOP
Definition: pmap.h:34
uint64_t uintptr_t
Definition: types.h:16