summaryrefslogtreecommitdiff
path: root/src/kernel/arch/amd64/driver/util.c
blob: 0f0ee34470bf971b1368898e364391798bc3e6e1 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <camellia/fsutil.h>
#include <kernel/arch/amd64/driver/util.h>
#include <kernel/panic.h>
#include <kernel/proc.h>
#include <kernel/vfs/request.h>

int req_readcopy(VfsReq *req, const void *buf, size_t len) {
	if (!req->caller) return -1;
	assert(req->type == VFSOP_READ);
	fs_normslice(&req->offset, &req->output.len, len, false);
	/* read errors are ignored. TODO write a spec */
	pcpy_to(req->caller, req->output.buf, buf + req->offset, req->output.len);
	return req->output.len;
}

void postqueue_join(VfsReq **queue, VfsReq *req) {
	if (req->postqueue_next)
		panic_invalid_state();

	while (*queue)
		queue = &(*queue)->postqueue_next;
	*queue = req;
}

bool postqueue_pop(VfsReq **queue, void (*accept)(VfsReq *)) {
	VfsReq *req = *queue;
	if (req == NULL) return false;
	*queue = req->postqueue_next;
	accept(req);
	return true;
}

void postqueue_ringreadall(VfsReq **queue, ring_t *r) {
	VfsReq *req;
	char tmp[64];
	size_t mlen = 0;
	if (ring_used(r) == 0) return;

	/* read as much as the biggest request wants */
	for (req = *queue; req; req = req->postqueue_next)
		mlen = max(mlen, req->output.len);
	mlen = min(mlen, sizeof tmp);
	mlen = ring_get(r, tmp, mlen);

	while (*queue) {
		req = *queue;
		*queue = req->postqueue_next;

		size_t ret = min(mlen, req->output.len);
		assert(req->type == VFSOP_READ);
		if (req->caller) {
			pcpy_to(req->caller, req->output.buf, tmp, ret);
		}
		vfsreq_finish_short(req, ret);
	}
}

size_t ring_to_virt(ring_t *r, Proc *proc, void __user *ubuf, size_t max) {
	char tmp[32];
	if (max > sizeof tmp) max = sizeof tmp;
	max = ring_get(r, tmp, max);
	return pcpy_to(proc, ubuf, tmp, max);
}