aboutsummaryrefslogtreecommitdiff
path: root/kernel/features/bus.c
blob: 811ad88d59bc2fd971401b34c934d4ab4b59e4d0 (plain) (blame)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// MIT License, Copyright (c) 2021 Marvin Borner

#include <assert.h>
#include <bus.h>
#include <crypto.h>
#include <def.h>
#include <dev.h>
#include <drivers/cpu.h>
#include <errno.h>
#include <list.h>
#include <mem.h>
#include <mm.h>
#include <stack.h>
#include <str.h>

// TODO: Consider different hashing method (possible duplicates in crc32)

// Bus conns are always between *two* clients!
struct bus_conn {
	u32 bus; // Bus name hash
	u32 conn;
	u32 pid; // Client pid
	struct stack *in; // Queue of bus owner
	struct stack *out; // Queue of other client
};

struct bus_message {
	void *data;
	u32 conn;
	u32 size;
};

struct bus {
	u32 pid; // Owner
	char name[128];
	u32 hash;
};

PROTECTED static struct list *bus_list = NULL;
PROTECTED static struct list *bus_conns = NULL;
static u32 conn_cnt = 1; // 0 is reserved

static struct bus *bus_find_bus(u32 hash)
{
	struct node *iterator = bus_list->head;
	while (iterator) {
		struct bus *bus = iterator->data;
		if (bus->hash == hash)
			return bus;

		iterator = iterator->next;
	}

	return NULL;
}

static struct bus_conn *bus_find_conn(u32 conn)
{
	struct node *iterator = bus_conns->head;
	while (iterator) {
		struct bus_conn *bus_conn = iterator->data;
		if (bus_conn->conn == conn)
			return bus_conn;

		iterator = iterator->next;
	}

	return NULL;
}

static struct bus *bus_find_owner(u32 pid)
{
	struct node *iterator = bus_list->head;
	while (iterator) {
		struct bus *bus = iterator->data;
		if (bus->pid == pid)
			return bus;

		iterator = iterator->next;
	}

	return NULL;
}

static void bus_add_conn(u32 hash, u32 conn)
{
	struct bus_conn *bus_conn = zalloc(sizeof(*bus_conn));
	bus_conn->bus = hash;
	bus_conn->pid = proc_current()->pid;
	bus_conn->conn = conn;
	bus_conn->in = stack_new();
	bus_conn->out = stack_new();
	list_add(bus_conns, bus_conn);
}

static res bus_conn_ready(struct bus_conn *bus_conn)
{
	struct bus *bus = bus_find_bus(bus_conn->bus);
	if (!bus)
		return -ENOENT;

	u8 ready = 0;
	if (bus->pid == proc_current()->pid)
		ready = !stack_empty(bus_conn->in);
	else
		ready = !stack_empty(bus_conn->out);

	return ready ? EOK : -EAGAIN;
}

static res bus_register(const char *name)
{
	if (!memory_readable(name))
		return -EFAULT;

	u32 len = strlen_user(name);
	if (len >= 128)
		return -E2BIG;

	u32 hash = crc32_user(0, name, len);
	if (bus_find_bus(hash))
		return -EINVAL;

	if (bus_find_owner(proc_current()->pid))
		return -EEXIST;

	struct bus *bus = zalloc(sizeof(*bus));
	strlcpy_user(bus->name, name, sizeof(bus->name));
	bus->pid = proc_current()->pid;
	bus->hash = hash;
	list_add(bus_list, bus);

	return EOK;
}

static res bus_connect_bus(const char *bus, u32 *conn)
{
	if (!memory_readable(bus) || !memory_writable(conn))
		return -EFAULT;

	u32 len = strlen_user(bus);
	if (len >= 128)
		return -E2BIG;

	u32 hash = crc32_user(0, bus, len);
	if (!bus_find_bus(hash))
		return -ENOENT;

	u32 conn_id = conn_cnt++;
	bus_add_conn(hash, conn_id);
	proc_current()->bus_conn = conn_id;

	stac();
	*conn = conn_id;
	clac();

	return EOK;
}

static res bus_connect_conn(u32 conn)
{
	struct bus_conn *bus_conn = bus_find_conn(conn);
	if (!bus_conn)
		return -ENOENT;

	struct bus *bus = bus_find_bus(bus_conn->bus);
	if (!bus)
		return -ENOENT;

	if (bus_conn->pid != proc_current()->pid && bus->pid != proc_current()->pid)
		return -EACCES;

	proc_current()->bus_conn = conn;

	return EOK;
}

static res bus_send(u32 conn, const void *buf, u32 count)
{
	if (!count)
		return EOK;

	if (!memory_readable(buf))
		return -EFAULT;

	struct bus_conn *bus_conn = bus_find_conn(conn);
	if (!bus_conn)
		return -ENOENT;

	struct bus *bus = bus_find_bus(bus_conn->bus);
	if (!bus)
		return -ENOENT;

	void *data = zalloc(count);
	memcpy_user(data, buf, count);
	struct bus_message *msg = zalloc(sizeof(*msg));
	msg->data = data;
	msg->conn = conn;
	msg->size = count;

	if (bus->pid == proc_current()->pid) {
		stack_push_bot(bus_conn->out, msg);
		dev_unblock_pid(bus_conn->pid);
	} else {
		stack_push_bot(bus_conn->in, msg);
		dev_unblock_pid(bus->pid);
	}

	return count;
}

static res bus_conn_receive(struct bus_conn *bus_conn, void *buf, u32 offset, u32 count)
{
	struct bus *bus = bus_find_bus(bus_conn->bus);
	if (!bus)
		return -ENOENT;

	struct bus_message *msg = NULL;
	if (bus->pid == proc_current()->pid)
		msg = stack_pop(bus_conn->in);
	else
		msg = stack_pop(bus_conn->out);

	if (!msg)
		return -EIO;

	struct bus_header h = { .conn = bus_conn->conn };
	memcpy_user(buf, &h, sizeof(h));
	memcpy_user((u8 *)buf + sizeof(h), (u8 *)msg->data + offset, MIN(count, msg->size));

	free(msg->data);
	free(msg);

	return MIN(count, msg->size);
}

static res bus_receive(void *buf, u32 offset, u32 count)
{
	if (!count)
		return EOK;

	if (!memory_readable(buf))
		return -EFAULT;

	struct bus *bus_owner = bus_find_owner(proc_current()->pid);
	struct bus_conn *bus_conn = bus_find_conn(proc_current()->bus_conn);
	if (!bus_owner && !bus_conn)
		return -ENOENT;

	if (bus_owner) {
		struct node *iterator = bus_conns->head;
		while (iterator) {
			struct bus_conn *sub_bus = iterator->data;
			if (sub_bus->bus == bus_owner->hash && bus_conn_ready(sub_bus) == EOK)
				return bus_conn_receive(sub_bus, buf, offset, count);

			iterator = iterator->next;
		}
	}

	if (bus_conn && bus_conn_ready(bus_conn) == EOK)
		return bus_conn_receive(bus_conn, buf, offset, count);

	return -EAGAIN;
}

/**
 * I/O integrations
 */

static res bus_control(u32 request, void *arg1, void *arg2, void *arg3)
{
	UNUSED(arg3);

	switch (request) {
	case DEVCTL_BUS_CONNECT_BUS: {
		return bus_connect_bus(arg1, arg2);
	}
	case DEVCTL_BUS_CONNECT_CONN: {
		return bus_connect_conn((u32)arg1);
	}
	case DEVCTL_BUS_REGISTER: {
		return bus_register(arg1);
	}
	default: {
		return -EINVAL;
	}
	}
}

static res bus_write(const void *buf, u32 offset, u32 count)
{
	if (offset)
		return -EINVAL;

	return bus_send(proc_current()->bus_conn, buf, count);
}

static res bus_read(void *buf, u32 offset, u32 count)
{
	return bus_receive(buf, offset, count);
}

static res bus_ready(void)
{
	struct bus *bus_owner = bus_find_owner(proc_current()->pid);
	struct bus_conn *bus_conn = bus_find_conn(proc_current()->bus_conn);
	if (!bus_owner && !bus_conn)
		return -ENOENT;

	if (bus_owner) {
		struct node *iterator = bus_conns->head;
		while (iterator) {
			struct bus_conn *sub_bus = iterator->data;
			if (sub_bus->bus == bus_owner->hash && bus_conn_ready(sub_bus) == EOK)
				return EOK;

			iterator = iterator->next;
		}
	}

	if (bus_conn && bus_conn_ready(bus_conn) == EOK)
		return EOK;

	return -EAGAIN;
}

CLEAR void bus_install(void)
{
	bus_list = list_new();
	bus_conns = list_new();

	struct dev_dev *dev = zalloc(sizeof(*dev));
	dev->control = bus_control;
	dev->read = bus_read;
	dev->ready = bus_ready;
	dev->write = bus_write;
	dev_add(DEV_BUS, dev);
}