summaryrefslogtreecommitdiff
path: root/src/kernel/mem/virt.h
blob: 1f9ef1473420c2ff72d2d13020b7eaad5927cbfc (plain)
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
/* contains utilities for interacting with virtual memory */
#pragma once
#include <camellia/types.h>
#include <kernel/mem/alloc.h>
#include <stdbool.h>
#include <stddef.h>

struct virt_iter {
	void *frag;
	size_t frag_len;
	size_t prior; // sum of all prior frag_lens
	bool error;

	void __user *_virt;
	size_t _remaining;
	struct pagedir *_pages;
	bool _user;
	bool _writeable;
};

struct virt_cpy_error {
	bool read_fail, write_fail;
};

/* if pages == NULL, create an iterator over physical memory. */
void virt_iter_new(
		struct virt_iter *iter, void __user *virt, size_t length,
		struct pagedir *pages, bool user, bool writeable);

bool virt_iter_next(struct virt_iter *);

size_t virt_cpy(
		struct pagedir *dest_pages,       void __user *dest,
		struct pagedir  *src_pages, const void __user *src,
		size_t length, struct virt_cpy_error *err);


/* copies to virtual memory, returns true on success */
static inline bool virt_cpy_to(struct pagedir *dest_pages,
		void __user *dest, const void *src, size_t length) {
	return length == virt_cpy(dest_pages, dest, NULL, (userptr_t)src, length, NULL);
}

/* copies from virtual memory, returns true on success */
static inline bool virt_cpy_from(struct pagedir *src_pages,
		void *dest, const void __user *src, size_t length) {
	return length == virt_cpy(NULL, (userptr_t)dest, src_pages, src, length, NULL);
}