Skip to content

Commit db066e2

Browse files
committed
wip
1 parent 82f1f41 commit db066e2

File tree

17 files changed

+1379
-672
lines changed

17 files changed

+1379
-672
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#pragma once
2+
#ifdef SOFTWARE_BREAKPOINTS
3+
#define BREAK do { asm volatile ("brk #0"); } while (0)
4+
#else
5+
#define BREAK
6+
#endif
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
// Minimal syscall interface required by the stub.
2+
#pragma once
3+
#include <linux/unistd.h>
4+
#include <stddef.h>
5+
#include <stdint.h>
6+
7+
8+
#define PROT_READ 0x1
9+
#define PROT_WRITE 0x2
10+
#define PROT_EXEC 0x4
11+
#define MAP_PRIVATE 0x02
12+
#define MAP_ANONYMOUS 0x20
13+
#define MAP_FIXED 0x10
14+
#define MAP_FIXED_NOREPLACE 0x100000
15+
#define MAP_FAILED (void*)-1
16+
17+
#define RLIMIT_STACK 3
18+
19+
#define O_RDONLY 0
20+
#define SEEK_SET 0
21+
#define SEEK_CUR 1
22+
#define SEEK_END 2
23+
24+
#define STDOUT_FILENO 1
25+
#define STDERR_FILENO 2
26+
27+
typedef long off_t;
28+
typedef unsigned long __rlim_t;
29+
typedef struct {
30+
__rlim_t soft;
31+
__rlim_t hard;
32+
} rlimit_t;
33+
34+
typedef struct {
35+
uint8_t buf[256];
36+
} stat_t;
37+
38+
__attribute__((naked))
39+
static long write (int fd, const void *buf, size_t count) {
40+
register long x8 asm("x8") = __NR_write;
41+
register long x0 asm("x0") = (long)fd;
42+
register long x1 asm("x1") = (long)buf;
43+
register long x2 asm("x2") = (long)count;
44+
asm volatile(
45+
"svc 0\n\t"
46+
"ret\n\t"
47+
:
48+
: "r"(x8), "0"(x0), "r"(x1), "r"(x2)
49+
: "rcx", "r11", "memory"
50+
);
51+
}
52+
53+
__attribute__((naked))
54+
static int open (const char* path, int mode) {
55+
register long x8 asm("x8") = __NR_open;
56+
register long x0 asm("x0") = (long)path;
57+
register long x1 asm("x1") = (long)mode;
58+
asm volatile(
59+
"svc 0\n\t"
60+
"ret\n\t"
61+
:
62+
: "r"(x8), "0"(x0), "r"(x1)
63+
: "memory", "cc"
64+
);
65+
}
66+
67+
__attribute__((naked))
68+
static int close(int fd) {
69+
register long x8 asm("x8") = __NR_close;
70+
register long x0 asm("x0") = (long)fd;
71+
asm volatile(
72+
"svc 0\n\t"
73+
"ret\n\t"
74+
:
75+
: "r"(x8), "0"(x0)
76+
: "memory", "cc"
77+
);
78+
79+
}
80+
81+
__attribute__((naked))
82+
static long lseek (int fd, off_t offset, int whence) {
83+
register long x8 asm("x8") = __NR_lseek;
84+
register long x0 asm("x0") = (long)fd;
85+
register long x1 asm("x1") = (long)offset;
86+
register long x2 asm("x2") = (long)whence;
87+
asm volatile(
88+
"svc 0\n\t"
89+
"ret\n\t"
90+
:
91+
: "r"(x8), "0"(x0), "r"(x1), "r"(x2)
92+
: "memory", "cc"
93+
);
94+
}
95+
96+
__attribute__((naked))
97+
static int getrlimit (int resource, rlimit_t* rlim) {
98+
register long x8 asm("x8") = __NR_getrlimit;
99+
register long x0 asm("x0") = (long)resource;
100+
register long x1 asm("x1") = (long)rlim;
101+
asm volatile(
102+
"svc 0\n\t"
103+
"ret\n\t"
104+
:
105+
: "r"(x8), "0"(x0), "r"(x1)
106+
: "memory", "cc"
107+
);
108+
}
109+
110+
__attribute__((naked))
111+
static int pread64 (int fd, void* buf, size_t count, off_t offset) {
112+
register long x8 asm("x8") = __NR_pread64;
113+
register long x0 asm("x0") = (long)fd;
114+
register long x1 asm("x1") = (long)buf;
115+
register long x2 asm("x2") = (long)count;
116+
register long x1 asm("x1") = (long)offset;
117+
asm volatile(
118+
"svc 0\n\t"
119+
"ret\n\t"
120+
:
121+
: "r"(x8)
122+
: "memory", "cc"
123+
);
124+
}
125+
126+
__attribute__((naked))
127+
static void exit (int status)
128+
{
129+
register long x8 asm("x8") = __NR_exit;
130+
register long x0 asm("x0") = (long)status;
131+
asm volatile(
132+
"svc 0\n\t"
133+
"ret\n\t"
134+
:
135+
: "r"(x8), "0"(x0)
136+
: "memory", "cc"
137+
);
138+
__builtin_unreachable();
139+
}
140+
141+
__attribute__((naked))
142+
static void* mmap(
143+
void* addr,
144+
uint64_t length,
145+
uint64_t prot,
146+
uint64_t flags,
147+
int64_t fd,
148+
uint64_t offset
149+
) {
150+
register long x8 asm("x8") = __NR_mmap;
151+
register long x0 asm("x0") = (long)addr;
152+
register long x1 asm("x1") = (long)length;
153+
register long x2 asm("x2") = (long)prot;
154+
register long x3 asm("x3") = (long)flags;
155+
register long x4 asm("x4") = (long)fd;
156+
register long x5 asm("x5") = (long)offset;
157+
asm volatile(
158+
"svc 0\n\t"
159+
"ret\n\t"
160+
:
161+
: "r"(x8), "0"(x0), "r"(x1), "r"(x2), "r"(x3), "r"(x4), "r"(x5)
162+
: "memory", "cc"
163+
);
164+
}
165+
166+
__attribute__((naked))
167+
static int munmap(
168+
void* addr,
169+
uint64_t len
170+
) {
171+
register long x8 asm("x8") = __NR_munmap;
172+
register long x0 asm("x0") = (long)addr;
173+
register long x1 asm("x1") = (long)len;
174+
asm volatile(
175+
"svc 0\n\t"
176+
"ret\n\t"
177+
:
178+
: "r"(x8), "0"(x0), "r"(x1)
179+
: "memory", "cc"
180+
);
181+
}
182+
183+
__attribute__((naked))
184+
static char* getcwd(char* buf, size_t size) {
185+
register long x8 asm("x8") = __NR_getcwd;
186+
register long x0 asm("x0") = (long)buf;
187+
register long x1 asm("x1") = (long)size;
188+
asm volatile(
189+
"svc 0\n\t"
190+
"ret\n\t"
191+
:
192+
: "r"(x8), "0"(x0), "r"(x1)
193+
: "memory", "cc"
194+
);
195+
}
196+
197+
__attribute__((naked))
198+
int stat (const char* pathname, stat_t* statbuf) {
199+
register long x8 asm("x8") = __NR_stat;
200+
register long x0 asm("x0") = (long)pathname;
201+
register long x1 asm("x1") = (long)statbuf;
202+
asm volatile(
203+
"svc 0\n\t"
204+
"ret\n\t"
205+
:
206+
: "r"(x8), "0"(x0), "r"(x1)
207+
: "memory", "cc"
208+
);
209+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#pragma once
2+
#include <stddef.h>
3+
4+
// Memcpy implementation.
5+
__attribute__((naked))
6+
static void* memcpy (
7+
void* dst,
8+
const void* src,
9+
size_t len
10+
) {
11+
// TODO
12+
}
13+
14+
// Memset implementation.
15+
__attribute__((naked))
16+
static void* memset (
17+
void* dst,
18+
int c,
19+
size_t n
20+
) {
21+
// TODO
22+
}

packages/std/packages/stub/include/debug.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ static void __putc (int ch, void*) {
1414

1515
// printf/snprintf/etc.
1616
#define NANOPRINTF_IMPLEMENTATION
17+
#define NANOPRINTF_VISIBILITY_STATIC
1718
#include "nanoprintf.h"
1819
#undef NANOPRINTF_IMPLEMENTATION
1920

packages/std/packages/stub/include/deserialize.h

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
enum Error {
1313
OK,
14-
OVERFLOW,
1514
UNEXPECTED_EOF,
1615
UNKNOWN_KIND,
1716
EXPECTED_UNIT,
@@ -115,7 +114,6 @@ typedef struct {
115114
static const char* deserializer_error (int ret) {
116115
switch(ret) {
117116
case OK: return "ok";
118-
case OVERFLOW: return "overflow";
119117
case UNEXPECTED_EOF: return "unexpected eof";
120118
case UNKNOWN_KIND: return "unknown kind";
121119
case EXPECTED_UNIT: return "expected unit";
@@ -222,8 +220,7 @@ static inline int deserialize_option (Deserializer* de, Option* value) {
222220
return OK;
223221
}
224222
case 1: {
225-
*value = (Value*)alloc(de->arena, sizeof(Value), _Alignof(Value));
226-
if (!*value) { return OVERFLOW; }
223+
*value = ALLOC(de->arena, Value);
227224
return deserialize_value(de, *value);
228225
}
229226
default: return EXPECTED_OPTION;
@@ -236,9 +233,8 @@ static inline int deserialize_array (Deserializer* de, Array* value) {
236233
if (ret) { return ret; }
237234

238235
// Allocate space for the array itself.
239-
value->data = (Value*)alloc(de->arena, value->len * sizeof(Value), _Alignof(Value));
236+
value->data = ALLOC_N(de->arena, value->len, Value);
240237
memset(value->data, 0, sizeof(Value) * value->len);
241-
if (!value->data) { return OVERFLOW; }
242238

243239
// Recurse.
244240
Value* itr = value->data;
@@ -256,8 +252,7 @@ static inline int deserialize_map (Deserializer* de, Map* value) {
256252
if (ret) { return ret; }
257253

258254
// Allocate space for entries.
259-
value->keys = (Value*)alloc(de->arena, 2 * value->len * sizeof(Value), _Alignof(Value));
260-
if(!value->keys) { return OVERFLOW; }
255+
value->keys = ALLOC_N(de->arena, 2 * value->len, Value);
261256
value->vals = value->keys + value->len;
262257

263258
// Deserialize entries.
@@ -278,8 +273,7 @@ static inline int deserialize_struct (Deserializer* de, Struct* value) {
278273
if (ret) { return ret; }
279274

280275
// Allocate space for fields.
281-
value->fields = (Field*)alloc(de->arena, sizeof(Field) * value->len, _Alignof(Field));
282-
if (!value->fields) { return OVERFLOW; }
276+
value->fields = ALLOC_N(de->arena, value->len, Field);
283277

284278
// Deserialize fields.
285279
Field* itr = value->fields;
@@ -291,8 +285,7 @@ static inline int deserialize_struct (Deserializer* de, Struct* value) {
291285
itr->id = de->data[de->cursor++];
292286

293287
// Allocate space for the value.
294-
itr->value = (Value*)alloc(de->arena, sizeof(Value), _Alignof(Value));
295-
if (!itr->value) { return OVERFLOW; }
288+
itr->value = ALLOC(de->arena, Value);
296289

297290
// Deserialize the value.
298291
ret = deserialize_value(de, itr->value);
@@ -308,8 +301,7 @@ static inline int deserialize_enum (Deserializer* de, Enum* value) {
308301
value->id = de->data[de->cursor++];
309302

310303
// Allocate space for the value.
311-
Value* v = (Value*)alloc(de->arena, sizeof(Value), _Alignof(Value));
312-
if (!v) { return OVERFLOW; }
304+
Value* v = ALLOC(de->arena, Value);
313305

314306
// Deserialize the value.
315307
int ret = deserialize_value(de, v);

packages/std/packages/stub/include/footer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22
#include <stdint.h>
3+
34
typedef struct {
45
uint64_t entry;
56
uint64_t size;

0 commit comments

Comments
 (0)