Skip to content

Commit b58dbe1

Browse files
committed
Add riscv64 support.
1 parent 103ee7a commit b58dbe1

File tree

6 files changed

+463
-1
lines changed

6 files changed

+463
-1
lines changed

riscv64/Linux/bootstrap.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* Copyright (C) 2016 Jeremiah Orians
2+
* Copyright (C) 2021 Andrius Štikonas
23
* This file is part of M2-Planet.
34
*
45
* M2-Planet is free software: you can redistribute it and/or modify
@@ -46,7 +47,7 @@ int fgetc(FILE* f)
4647
void fputc(char s, FILE* f)
4748
{
4849
asm("RD_A0 RS1_FP MV"
49-
"RD_A0 RS1_A0 !-8 ADDI"
50+
"RD_A1 RS1_A0 !-8 ADDI"
5051
"RD_A0 RS1_FP MV"
5152
"RD_A0 RS1_A0 !-16 ADDI"
5253
"RD_A0 RS1_A0 LD"

riscv64/Linux/fcntl.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* Copyright (C) 2016 Jeremiah Orians
2+
* Copyright (C) 2021 Andrius Štikonas
3+
* This file is part of M2-Planet.
4+
*
5+
* M2-Planet is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* M2-Planet is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with M2-Planet. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#define O_RDONLY 0
20+
#define O_WRONLY 1
21+
#define O_RDWR 2
22+
#define O_CREAT 00100
23+
#define S_IXUSR 00100
24+
#define S_IWUSR 00200
25+
#define S_IRUSR 00400
26+
#define S_IRWXU 00700
27+
#define O_TRUNC 001000
28+
29+
int open(char* name, int flag, int mode)
30+
{
31+
asm("RD_A0 RS1_FP MV"
32+
"RD_A0 RS1_A0 !-24 ADDI"
33+
"RD_A3 RS1_A0 LD"
34+
"RD_A0 RS1_FP MV"
35+
"RD_A0 RS1_A0 !-16 ADDI"
36+
"RD_A2 RS1_A0 LD"
37+
"RD_A0 RS1_FP MV"
38+
"RD_A0 RS1_A0 !-8 ADDI"
39+
"RD_A1 RS1_A0 LD"
40+
"RD_A0 !-100 ADDI" /* AT_FDCWD */
41+
"RD_A7 !56 ADDI"
42+
"ECALL");
43+
}
44+
45+
#define STDIN_FILENO 0
46+
#define STDOUT_FILENO 1
47+
#define STDERR_FILENO 2

riscv64/Linux/sys/stat.h

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/* Copyright (C) 2020 Jeremiah Orians
2+
* Copyright (C) 2021 Andrius Štikonas
3+
* This file is part of M2-Planet.
4+
*
5+
* M2-Planet is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* M2-Planet is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with M2-Planet. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#include <sys/types.h>
20+
21+
#define S_IRWXU 00700
22+
#define S_IXUSR 00100
23+
#define S_IWUSR 00200
24+
#define S_IRUSR 00400
25+
26+
#define S_ISUID 0400
27+
#define S_ISGID 02000
28+
#define S_IXGRP 00010
29+
#define S_IXOTH 00001
30+
#define S_IRGRP 00040
31+
#define S_IROTH 00004
32+
#define S_IWGRP 00020
33+
#define S_IWOTH 00002
34+
#define S_IRWXG 00070
35+
#define S_IRWXO 00007
36+
37+
38+
int chmod(char *pathname, int mode)
39+
{
40+
asm("RD_A0 RS1_FP MV"
41+
"RD_A0 RS1_A0 !-16 ADDI"
42+
"RD_A2 RS1_A0 LD"
43+
"RD_A0 RS1_FP MV"
44+
"RD_A0 RS1_A0 !-8 ADDI"
45+
"RD_A1 RS1_A0 LD"
46+
"RD_A0 !-100 ADDI" /* AT_FDCWD */
47+
"RD_A7 !53 ADDI"
48+
"ECALL");
49+
}
50+
51+
52+
int fchmod(int a, mode_t b)
53+
{
54+
asm("RD_A0 RS1_FP MV"
55+
"RD_A0 RS1_A0 !-16 ADDI"
56+
"RD_A2 RS1_A0 LD"
57+
"RD_A0 RS1_FP MV"
58+
"RD_A0 RS1_A0 !-8 ADDI"
59+
"RD_A1 RS1_A0 LD"
60+
"RD_A0 !-100 ADDI" /* AT_FDCWD */
61+
"RD_A7 !52 ADDI"
62+
"ECALL");
63+
}
64+
65+
66+
int mkdir(char const* a, mode_t b)
67+
{
68+
asm("RD_A0 RS1_FP MV"
69+
"RD_A0 RS1_A0 !-16 ADDI"
70+
"RD_A2 RS1_A0 LD"
71+
"RD_A0 RS1_FP MV"
72+
"RD_A0 RS1_A0 !-8 ADDI"
73+
"RD_A1 RS1_A0 LD"
74+
"RD_A0 !-100 ADDI" /* AT_FDCWD */
75+
"RD_A7 !34 ADDI"
76+
"ECALL");
77+
}
78+
79+
80+
int mknod(char const* a, mode_t b, dev_t c)
81+
{
82+
asm("RD_A0 RS1_FP MV"
83+
"RD_A0 RS1_A0 !-24 ADDI"
84+
"RD_A3 RS1_A0 LD"
85+
"RD_A0 RS1_FP MV"
86+
"RD_A0 RS1_A0 !-16 ADDI"
87+
"RD_A2 RS1_A0 LD"
88+
"RD_A0 RS1_FP MV"
89+
"RD_A0 RS1_A0 !-8 ADDI"
90+
"RD_A1 RS1_A0 LD"
91+
"RD_A0 !-100 ADDI" /* AT_FDCWD */
92+
"RD_A7 !33 ADDI"
93+
"ECALL");
94+
}
95+
96+
97+
mode_t umask(mode_t m)
98+
{
99+
asm("RD_A0 RS1_FP MV"
100+
"RD_A0 RS1_A0 !-8 ADDI"
101+
"RD_A0 RS1_A0 LD"
102+
"RD_A7 !166 ADDI"
103+
"ECALL");
104+
}

riscv64/Linux/unistd.h

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
/* Copyright (C) 2020 Jeremiah Orians
2+
* Copyright (C) 2021 Andrius Štikonas
3+
* This file is part of M2-Planet.
4+
*
5+
* M2-Planet is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* M2-Planet is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with M2-Planet. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
#define NULL 0
19+
#define __PATH_MAX 4096
20+
21+
void* malloc(unsigned size);
22+
23+
int access(char* pathname, int mode)
24+
{
25+
asm("RD_A0 RS1_FP MV"
26+
"RD_A0 RS1_A0 !-16 ADDI"
27+
"RD_A2 RS1_A0 LD"
28+
"RD_A0 RS1_FP MV"
29+
"RD_A0 RS1_A0 !-8 ADDI"
30+
"RD_A1 RS1_A0 LD"
31+
"RD_A0 !-100 ADDI" /* AT_FDCWD */
32+
"RD_A3 ADDI" /* flags = 0 */
33+
"RD_A7 !48 ADDI"
34+
"ECALL");
35+
}
36+
37+
int chdir(char* path)
38+
{
39+
asm("RD_A0 RS1_FP MV"
40+
"RD_A0 RS1_A0 !-8 ADDI"
41+
"RD_A0 RS1_A0 LD"
42+
"RD_A7 !49 ADDI"
43+
"ECALL");
44+
}
45+
46+
int fchdir(int fd)
47+
{
48+
asm("RD_A0 RS1_FP MV"
49+
"RD_A0 RS1_A0 !-8 ADDI"
50+
"RD_A0 RS1_A0 LD"
51+
"RD_A7 !50 ADDI"
52+
"ECALL");
53+
}
54+
55+
void _exit(int value);
56+
57+
int fork()
58+
{
59+
asm("RD_A7 !220 ADDI"
60+
"RD_A0 !17 ADDI" /* SIGCHLD */
61+
"RD_A1 MV" /* Child uses duplicate of parent's stack */
62+
"ECALL");
63+
}
64+
65+
66+
int waitpid (int pid, int* status_ptr, int options)
67+
{
68+
/* Uses wait4 with struct rusage *ru set to NULL */
69+
asm("RD_A0 RS1_FP MV"
70+
"RD_A0 RS1_A0 !-24 ADDI"
71+
"RD_A2 RS1_A0 LD"
72+
"RD_A0 RS1_FP MV"
73+
"RD_A0 RS1_A0 !-16 ADDI"
74+
"RD_A1 RS1_A0 LD"
75+
"RD_A0 RS1_FP MV"
76+
"RD_A0 RS1_A0 !-8 ADDI"
77+
"RD_A0 RS1_A0 LD"
78+
"RD_A3 ADDI"
79+
"RD_A7 !260 ADDI"
80+
"ECALL");
81+
}
82+
83+
84+
int execve(char* file_name, char** argv, char** envp)
85+
{
86+
asm("RD_A0 RS1_FP MV"
87+
"RD_A0 RS1_A0 !-24 ADDI"
88+
"RD_A2 RS1_A0 LD"
89+
"RD_A0 RS1_FP MV"
90+
"RD_A0 RS1_A0 !-16 ADDI"
91+
"RD_A1 RS1_A0 LD"
92+
"RD_A0 RS1_FP MV"
93+
"RD_A0 RS1_A0 !-8 ADDI"
94+
"RD_A0 RS1_A0 LD"
95+
"RD_A7 !221 ADDI"
96+
"ECALL");
97+
}
98+
99+
int read(int fd, char* buf, unsigned count)
100+
{
101+
asm("RD_A0 RS1_FP MV"
102+
"RD_A0 RS1_A0 !-24 ADDI"
103+
"RD_A2 RS1_A0 LD"
104+
"RD_A0 RS1_FP MV"
105+
"RD_A0 RS1_A0 !-16 ADDI"
106+
"RD_A1 RS1_A0 LD"
107+
"RD_A0 RS1_FP MV"
108+
"RD_A0 RS1_A0 !-8 ADDI"
109+
"RD_A0 RS1_A0 LD"
110+
"RD_A7 !63 ADDI"
111+
"ECALL");
112+
}
113+
114+
int write(int fd, char* buf, unsigned count)
115+
{
116+
asm("RD_A0 RS1_FP MV"
117+
"RD_A0 RS1_A0 !-24 ADDI"
118+
"RD_A2 RS1_A0 LD"
119+
"RD_A0 RS1_FP MV"
120+
"RD_A0 RS1_A0 !-16 ADDI"
121+
"RD_A1 RS1_A0 LD"
122+
"RD_A0 RS1_FP MV"
123+
"RD_A0 RS1_A0 !-8 ADDI"
124+
"RD_A0 RS1_A0 LD"
125+
"RD_A7 !64 ADDI"
126+
"ECALL");
127+
}
128+
129+
int lseek(int fd, int offset, int whence)
130+
{
131+
asm("RD_A0 RS1_FP MV"
132+
"RD_A0 RS1_A0 !-24 ADDI"
133+
"RD_A2 RS1_A0 LD"
134+
"RD_A0 RS1_FP MV"
135+
"RD_A0 RS1_A0 !-16 ADDI"
136+
"RD_A1 RS1_A0 LD"
137+
"RD_A0 RS1_FP MV"
138+
"RD_A0 RS1_A0 !-8 ADDI"
139+
"RD_A0 RS1_A0 LD"
140+
"RD_A7 !62 ADDI"
141+
"ECALL");
142+
}
143+
144+
145+
int close(int fd)
146+
{
147+
asm("RD_A0 RS1_FP MV"
148+
"RD_A0 RS1_A0 !-8 ADDI"
149+
"RD_A0 RS1_A0 LD"
150+
"RD_A7 !57 ADDI" /* close */
151+
"ECALL");
152+
}
153+
154+
155+
int _getcwd(char* buf, int size)
156+
{
157+
asm("RD_A0 RS1_FP MV"
158+
"RD_A0 RS1_A0 !-16 ADDI"
159+
"RD_A1 RS1_A0 LD"
160+
"RD_A0 RS1_FP MV"
161+
"RD_A0 RS1_A0 !-8 ADDI"
162+
"RD_A0 RS1_A0 LD"
163+
"RD_A7 !17 ADDI"
164+
"ECALL");
165+
}
166+
167+
168+
char* getcwd(char* buf, unsigned size)
169+
{
170+
int c = _getcwd(buf, size);
171+
if(0 == c) return NULL;
172+
return buf;
173+
}
174+
175+
176+
char* getwd(char* buf)
177+
{
178+
return getcwd(buf, __PATH_MAX);
179+
}
180+
181+
182+
char* get_current_dir_name()
183+
{
184+
return getcwd(malloc(__PATH_MAX), __PATH_MAX);
185+
}
186+
187+
188+
int brk(void *addr)
189+
{
190+
asm("RD_A0 RS1_FP MV"
191+
"RD_A0 RS1_A0 !-8 ADDI"
192+
"RD_A0 RS1_A0 LD"
193+
"RD_A7 !214 ADDI"
194+
"ECALL");
195+
}
196+
197+
struct utsname
198+
{
199+
char sysname[65]; /* Operating system name (e.g., "Linux") */
200+
char nodename[65]; /* Name within "some implementation-defined network" */
201+
char release[65]; /* Operating system release (e.g., "2.6.28") */
202+
char version[65]; /* Operating system version */
203+
char machine[65]; /* Hardware identifier */
204+
};
205+
206+
int uname(struct utsname* unameData)
207+
{
208+
asm("RD_A0 RS1_FP MV"
209+
"RD_A0 RS1_A0 !-8 ADDI"
210+
"RD_A0 RS1_A0 LD"
211+
"RD_A7 !160 ADDI"
212+
"ECALL");
213+
}

0 commit comments

Comments
 (0)