aboutsummaryrefslogtreecommitdiff
path: root/arch/riscv64/conf/init.c
blob: 001d5ebb874e039a6cf229a787cfd2487bcdee16 (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
/* SPDX-License-Identifier: copyleft-next-0.3.1 */
/* Copyright 2023 Kim Kuparinen < kimi.h.kuparinen@gmail.com > */

/**
 * Header for silencing scripts/warn-undocumented
 *
 * @file init.c
 *
 * Test init program.
 *
 * This file is only temporarily in this tree.
 * At some point in the (hopefully) near future, I intend to move the
 * kernel code and initrd generation stuff into separate repositories, to make
 * things easier for myself. For now though, this is good enough.
 *
 * Compile with
 *	riscv64-unknown-elf-gcc -ffreestanding -nostdlib
 *
 * Create initrd with
 *	echo init | cpio -H newc -o > initrd
 */

#include <stdint.h>
#include <stddef.h>
#include "../../../include/kmi/syscalls.h"

struct sys_ret {
	long a0, a1, a2, a3, a4, a5;
};

char *strcpy(char * restrict dst, const char * restrict src)
{
	const char *s1 = src;
	char *s2 = dst;

	while (*s1)
		*(s2++) = *(s1++);

	return dst;
}

struct sys_ret ecall(struct sys_ret s)
{
	register long a0 asm ("a0") = s.a0;
	register long a1 asm ("a1") = s.a1;
	register long a2 asm ("a2") = s.a2;
	register long a3 asm ("a3") = s.a3;
	register long a4 asm ("a4") = s.a4;
	register long a5 asm ("a5") = s.a5;

	asm volatile ("ecall"
	              : "=r" (a0), "=r" (a1), "=r" (a2), "=r" (a3), "=r" (a4),
	              "=r" (a5)
	              : "r" (a0), "r" (a1), "r" (a2), "r" (a3), "r" (a4),
	              "r" (a5));

	return (struct sys_ret){a0, a1, a2, a3, a4, a5};
}

static void sys_noop()
{
	struct sys_ret r = {.a0 = SYS_NOOP};
	ecall(r);
}

static void sys_putch(char c)
{
	struct sys_ret r = {.a0 = SYS_PUTCH, .a1 = c};
	ecall(r);
}

static uint64_t sys_timebase()
{
	struct sys_ret r = {.a0 = SYS_TIMEBASE};
	r = ecall(r);
#if defined(_LP64)
	return r.a1;
#else
	uint64_t t = r.a1;
	t <<= 32;
	return t + r.a2;
#endif
}

static uint64_t sys_ticks()
{
	struct sys_ret r = {.a0 = SYS_TICKS};
	r = ecall(r);
#if defined(_LP64)
	return r.a1;
#else
	uint64_t t = r.a1;
	t <<= 32;
	return t + r.a2;
#endif
}

static void puts(const char *s)
{
	while (*s)
		sys_putch(*s++);
}

static void _print_number(uint64_t v)
{
	if (v == 0)
		return;

	_print_number(v / 10);
	sys_putch((v % 10) + '0');
}

static void print_number(uint64_t v)
{
	if (v == 0) {
		sys_putch('0');
		return;
	}

	_print_number(v);
}

static void print_value(const char *s, uint64_t v)
{
	puts(s);
	puts(": ");

	print_number(v);
	sys_putch('\n');
}

static uint64_t sys_fork()
{
	struct sys_ret r = {.a0 = SYS_FORK};
	r = ecall(r);

	if (r.a0 != 0)
		print_value("fork() failed with error ", r.a0);

	return r.a1;
}

static uint64_t sys_swap(long tid)
{
	struct sys_ret r = {.a0 = SYS_SWAP, .a1 = tid};
	r = ecall(r);

	if (r.a0 != 0)
		print_value("swap() failed with error ", r.a0);

	return r.a0;
}

static void sys_ipc_server(void *f)
{
	struct sys_ret r = {.a0 = SYS_IPC_SERVER, .a1 = (long)f};
	r = ecall(r);

	if (r.a0 != 0)
		print_value("ipc_server() failed with error ", r.a0);
}

/** Helper for ipc arguments/return values. */
struct ipc_args {
	long a0, a1, a2, a3;
};

static struct ipc_args sys_ipc_req(long tid, long d0, long d1, long d2, long d3)
{
	struct sys_ret r = {.a0 = SYS_IPC_REQ,
		            .a1 = tid,
		            .a2 = d0,
		            .a3 = d1,
		            .a4 = d2,
		            .a5 = d3};

	r = ecall(r);

	if (r.a0)
		print_value("ipc_req() failed with error ", r.a0);

	return (struct ipc_args){r.a1, r.a2, r.a3, r.a4};
}

static void sys_ipc_resp(long d0, long d1, long d2, long d3)
{
	struct sys_ret r = {.a0 = SYS_IPC_RESP,
		            .a1 = d0,
		            .a2 = d1,
		            .a3 = d2,
		            .a4 = d3};

	ecall(r);
}

static void sys_poweroff(long type)
{
	struct sys_ret r = {.a0 = SYS_POWEROFF, .a1 = type};
	ecall(r);
}

static void *sys_req_mem(size_t count)
{
	struct sys_ret r = {.a0 = SYS_REQ_MEM, .a1 = count,
		            .a2 = (1 << 0) | (1 << 1) | (1 << 2) | (1 << 4)};
	r = ecall(r);

	if (r.a0)
		print_value("sys_req_mem() failed with error ", r.a0);

	return (void *)r.a1;
}

static void sys_free_mem(void *p)
{
	struct sys_ret r = {.a0 = SYS_FREE_MEM, .a1 = (long)p};
	r = ecall(r);

	if (r.a0)
		print_value("sys_free_mem() failed with error ", r.a0);
}

static void *sys_req_sharedmem(long tid, unsigned long size, void **cbuf)
{
	struct sys_ret r = {.a0 = SYS_REQ_SHAREDMEM, .a1 = tid, .a2 = size,
		            .a3 = (1 << 0) | (1 << 1) | (1 << 2) | (1 << 4),
		            .a4 = (1 << 0) | (1 << 1) | (1 << 2) | (1 << 4)};
	r = ecall(r);

	if (r.a0)
		print_value("sys_req_sharedmem() failed with error ", r.a0);

	*cbuf = (void *)r.a2;
	return (void *)r.a1;
}

/* I'm guessing my elf parser doesn't handle data pages correctly yet... */
static char *rw_buf = 0;
static size_t rw_buf_size = 4096;

void callback(long status, long tid, long d0, long d1, long d2, long d3)
{
	(void)status;

	void *cbuf = 0;
	if (d0 == 1)
		rw_buf = sys_req_sharedmem(tid, rw_buf_size, &cbuf);

	if (d0 == 2)
		puts(rw_buf);

	sys_ipc_resp((long)cbuf, rw_buf_size, 0, 0);
}

#define CSR_TIME "0xc01"
#define csr_read(csr, \
	         res) __asm__ volatile ("csrr %0, " csr : "=r" (res) :: "memory")

#define CSR_CYCLE "0xc00"

void _start()
{
	sys_noop();
	puts("Hello, world!\n");

	uint64_t second = sys_timebase();
	print_value("Timebase", second);

	uint64_t n = 0, i, start, cn, cstart, cend;
	start = i = sys_ticks();
	csr_read(CSR_CYCLE, cstart);
	while (i < start + second) {
		i = sys_ticks();
		n++;
	}
	csr_read(CSR_CYCLE, cend);

	print_value("Start ticks", start);
	print_value("End ticks", i);
	print_value("Syscalls per second", n);
	print_value("Executed cycles per second", cend - cstart);

	puts("Setting callback...");
	sys_ipc_server(callback);

	puts("Starting fork():\n");
	long pid = sys_fork();
	if (pid != 0) {
		print_value("Child pid", pid);
		puts("Swapping to child...\n");
		while(1) sys_swap(pid);
	}

	puts("Hello from child!\n");

	puts("Doing swaps...\n");
	csr_read(CSR_TIME, i);
	start = i; n = 0;
	while (i < start + second) {
		sys_swap(1);
		csr_read(CSR_TIME, i);
		n++;
	}

	print_value("Swaps (both ways) per second", n);

	puts("Doing ipc requests...\n");
	csr_read(CSR_TIME, i);
	start = i; n = 0;
	while (i < start + second) {
		sys_ipc_req(1, 0, 0, 0, 0);
		csr_read(CSR_TIME, i);
		n++;
	}

	print_value("IPC requests per second", n);

	puts("Doing memory allocations...\n");
	for (i = 0; i < 1000000; ++i) {
		char *p = sys_req_mem(10);
		*p = 'c';
		sys_free_mem(p);
	}

	puts("Checking shared memory\n");
	struct ipc_args r = sys_ipc_req(1, 1, 0, 0, 0);
	rw_buf = (char *)r.a1;
	rw_buf_size = (size_t)r.a2;
	print_value("Shared memory ptr", (uintptr_t)rw_buf);
	print_value("Shared memory size", rw_buf_size);

	rw_buf[0] = 0;
	strcpy(rw_buf, "Hello from the other side!\n");
	sys_ipc_req(1, 2, 0, 0, 0);

	sys_poweroff(0);
}