Loading [MathJax]/jax/output/HTML-CSS/config.js
CS350 COS
COS
All Data Structures Files Functions Variables Typedefs Macros
copy.c File Reference
#include <stdbool.h>
#include <stdint.h>
#include <errno.h>
#include <sys/kassert.h>
#include <machine/pmap.h>
Include dependency graph for copy.c:

Go to the source code of this file.

Functions

int copy_unsafe (void *to_addr, void *from_addr, uintptr_t len)
 
int copystr_unsafe (void *to_addr, void *from_addr, uintptr_t len)
 
int Copy_In (uintptr_t fromuser, void *tokernel, uintptr_t len)
 
int Copy_Out (void *fromkernel, uintptr_t touser, uintptr_t len)
 
int Copy_StrIn (uintptr_t fromuser, void *tokernel, uintptr_t len)
 
int Copy_StrOut (void *fromkernel, uintptr_t touser, uintptr_t len)
 

Function Documentation

◆ Copy_In()

int Copy_In ( uintptr_t  fromuser,
void *  tokernel,
uintptr_t  len 
)

Copy_In –

Safely copy memory from userspace. Prevents userspace pointers from reading kernel memory.

Side effects: Kernel page fault may have occurred.

Parameters
[in]fromuserUser address to copy from.
[in]tokernelKernel address to copy to.
[in]lenLength of the data to copy.
Return values
EFAULTif the address is invalid or causes a fault.

Definition at line 34 of file copy.c.

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}
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
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Copy_Out()

int Copy_Out ( void *  fromkernel,
uintptr_t  touser,
uintptr_t  len 
)

Copy_Out –

Safely copy memory to userspace. Prevents userspace pointers from writing kernel memory.

Side effects: Kernel page fault may have occurred.

Parameters
[in]fromkernelKernel address to copy from.
[in]touserUser address to copy to.
[in]lenLength of the data to copy.
Return values
EFAULTif the address is invalid or causes a fault.

Definition at line 70 of file copy.c.

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}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Copy_StrIn()

int Copy_StrIn ( uintptr_t  fromuser,
void *  tokernel,
uintptr_t  len 
)

Copy_StrIn –

Safely copy a string from userspace. Prevents userspace pointers from reading kernel memory.

Side effects: Kernel page fault may have occurred.

Parameters
[in]fromuserUser address to copy from.
[in]tokernelKernel address to copy to.
[in]lenMaximum string length.
Return values
EFAULTif the address is invalid or causes a fault.

Definition at line 106 of file copy.c.

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}
int copystr_unsafe(void *to_addr, void *from_addr, uintptr_t len)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Copy_StrOut()

int Copy_StrOut ( void *  fromkernel,
uintptr_t  touser,
uintptr_t  len 
)

Copy_StrOut –

Safely copy a string to userspace. Prevents userspace pointers from writing kernel memory.

Side effects: Kernel page fault may have occurred.

Parameters
[in]fromkernelKernel address to copy from.
[in]touserUser address to copy to.
[in]lenMaximum string length.
Return values
EFAULTif the address is invalid or causes a fault.

Definition at line 142 of file copy.c.

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}
Here is the call graph for this function:

◆ copy_unsafe()

int copy_unsafe ( void *  to_addr,
void *  from_addr,
uintptr_t  len 
)
Here is the caller graph for this function:

◆ copystr_unsafe()

int copystr_unsafe ( void *  to_addr,
void *  from_addr,
uintptr_t  len 
)
Here is the caller graph for this function: