|
| 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