The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
uaccess.h
Go to the documentation of this file.
1#pragma once
2
3/**
4 * safe access to user memory from syscalls.
5 *
6 * a pointer from user space can be anything: unmapped, kernel memory, or a
7 * buffer that runs off the end of a mapping. touching it blindly page-faults
8 * in the kernel (a panic) or lets a program read or write kernel memory.
9 * these helpers check the current process's page tables first, so a bad
10 * pointer becomes KERR_FAULT (-EFAULT) instead. pages an area allows but
11 * that are not backed yet (demand paging) are backed before the access.
12 *
13 * the check and the access are not atomic, which is fine for now: there is
14 * one CPU and one thread per address space, and only the process itself can
15 * change its mappings, never while it is inside a syscall.
16 */
17
18#include "common/stdint.h"
19#include "common/error.h"
20
21/**
22 * check that the current process could access a whole user range.
23 * @param ptr start of the range (a user address).
24 * @param len length in bytes; 0 is always accessible.
25 * @param write true if the kernel will write to the range.
26 * @return true if every byte is mapped user memory with that access.
27 */
28bool user_access_ok(const void *ptr, size_t len, bool write);
29
30/**
31 * copy bytes from user memory into a kernel buffer.
32 * @param dst kernel destination.
33 * @param user_src user source.
34 * @param len number of bytes.
35 * @return KERR_OK, or KERR_FAULT (nothing copied) if the source is bad.
36 */
37kerr_t copy_from_user(void *dst, const void *user_src, size_t len);
38
39/**
40 * copy bytes from a kernel buffer into user memory.
41 * @param user_dst user destination (must be writable).
42 * @param src kernel source.
43 * @param len number of bytes.
44 * @return KERR_OK, or KERR_FAULT (nothing copied) if the destination is bad.
45 */
46kerr_t copy_to_user(void *user_dst, const void *src, size_t len);
47
48/**
49 * copy a NUL-terminated user string into a kernel buffer, checking each page
50 * as the copy reaches it.
51 * @param dst kernel destination.
52 * @param user_src user string.
53 * @param size size of dst, including room for the NUL.
54 * @return the string length (without the NUL), KERR_FAULT if the string
55 * runs into memory the process cannot read, or KERR_RANGE if it
56 * does not fit in size bytes.
57 */
58int32_t strncpy_from_user(char *dst, const char *user_src, size_t size);
kerr_t
unified kernel error codes.
Definition error.h:14
int int32_t
Definition stdint.h:14
int32_t strncpy_from_user(char *dst, const char *user_src, size_t size)
copy a NUL-terminated user string into a kernel buffer, checking each page as the copy reaches it.
Definition uaccess.c:71
bool user_access_ok(const void *ptr, size_t len, bool write)
safe access to user memory from syscalls.
Definition uaccess.c:10
kerr_t copy_from_user(void *dst, const void *user_src, size_t len)
copy bytes from user memory into a kernel buffer.
Definition uaccess.c:49
kerr_t copy_to_user(void *user_dst, const void *src, size_t len)
copy bytes from a kernel buffer into user memory.
Definition uaccess.c:60
ssize_t write(int fd, const void *buf, size_t count)
write bytes to a file descriptor.