Repository navigation
Expand file tree
/
Copy pathcommon.h
More file actions
409 lines (348 loc) · 11.1 KB
/
Copy pathcommon.h
File metadata and controls
409 lines (348 loc) · 11.1 KB
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#ifndef COMMON_H
#define COMMON_H
#include <stdarg.h>
#include <stdint.h>
#include <stddef.h>
#include "minux.h"
#include "elf.h"
#define MAX_OPEN_FILES 32
typedef uint64_t pte_t;
typedef uint64_t pde_t;
typedef pte_t* pagetable_t; // 512
extern pagetable_t kernel_pagetable;
struct context_t {
uint64_t ra;
uint64_t sp;
uint64_t gp;
uint64_t tp;
uint64_t t0;
uint64_t t1;
uint64_t t2;
uint64_t t3;
uint64_t t4;
uint64_t t5;
uint64_t t6;
uint64_t a0;
uint64_t a1;
uint64_t a2;
uint64_t a3;
uint64_t a4;
uint64_t a5;
uint64_t a6;
uint64_t a7;
uint64_t s0;
uint64_t s1;
uint64_t s2;
uint64_t s3;
uint64_t s4;
uint64_t s5;
uint64_t s6;
uint64_t s7;
uint64_t s8;
uint64_t s9;
uint64_t s10;
uint64_t s11;
uint64_t mepc;
};
struct sKernelState
{
struct context_t gYieldContext;
struct context_t gYieldReturnContext;
char* gYieldStack;
uint64_t gYieldUserSatp;
uint64_t gYieldUserSP;
uint64_t gYieldUserActiveProc;
};
struct file;
struct process_pages
{
char** process_kalloc_address;
int num_process_kalloc_address;
void* process_kalloc_address_list;
};
struct proc {
struct context_t trapframe;
struct context_t context; // swtch() here to run process
struct proc *parent; // Parent process
int parent_pid; // Parent process
// char* stack;
char* stack_top;
uint64_t vaddr;
uint64_t memsz;
uint64_t sz; // Size of process memory (program break)
int zombie;
pagetable_t pagetable;
char* program;
int program_size;
int xstatus; // exit
struct file* file_table[MAX_OPEN_FILES];
struct process_pages** process_pages;
int num_process_pages;
void* process_pages_list;
int debug_;
int deleted;
struct proc* next;
// current working directory (absolute path)
char cwd[128];
unsigned short umask; // creation mask (e.g., 0022)
// credentials
uint16_t uid;
uint16_t gid;
uint16_t pgrp;
int nsupp;
void* supp_gid_list;
char username[32];
};
// ── Pipe core ─────────────────────────────────────────────────────────
#define PIPE_SIZE 512
struct file;
struct spipe {
char data[PIPE_SIZE]; // Ring buffer
uint32_t nread; // Bytes already read
uint32_t nwrite; // Bytes already written
int read_open; // Reader side open flag
int write_open; // Writer side open flag
int nreader;
int nwriter;
int used;
int num_linked_file;
void* linked_file_list;
};
// File table entry
struct proc;
#define PROC_MAX 64
struct file {
enum { FK_STDIN, FK_STDOUT, FK_STDERR, FK_FILE, FK_PIPE, FK_TTY } kind;
uint32_t inum; // inode number
struct dinode din; // on-disk inode information
uint32_t off; // Current read offset
int used; // In-use flag
struct spipe* pipe;
int pipe_used;
int read_pipe;
int write_pipe;
// open() flags (subset) for this file; used for behavior like O_TRUNC
int oflags;
int num_owner_process;
void* owner_process_list;
};
int fs_open(const char *path);
// New open with flags/mode
int fs_open2(const char *path, int flags, int mode);
ssize_t fs_read(int fd, void *buf, size_t count);
ssize_t fs_write(int fd, const void *buf, size_t count);
int fs_close(long fd, int massive);
int fs_mkdir(const char *path, int mode);
int fs_rmdir(const char *path);
int fs_unlink(const char *path);
int fs_link(const char *oldpath, const char *newpath);
int fs_symlink(const char *target, const char *linkpath);
int fs_stat(const char *path, struct stat *st);
int fs_readlink(const char *path, char *buf, int bufsz);
int fs_lstat(const char *path, struct stat *st);
int fs_chmod(const char *path, uint32_t mode);
int fs_chown(const char *path, uint16_t uid, uint16_t gid);
int fs_realpath(const char *path, char *out, int outsz);
void file_owner_clear(struct file* f);
void file_owner_add(struct file* f, struct proc* owner);
void file_owner_remove(struct file* f, struct proc* owner);
void pipe_linked_file_clear(struct spipe* p);
void pipe_linked_file_add(struct spipe* p, struct file* f);
void pipe_linked_file_remove(struct spipe* p, struct file* f);
void pipe_linked_file_clear_pipe_refs(struct spipe* p);
void proc_process_pages_clear(struct proc* p);
void proc_process_pages_add(struct proc* p, struct process_pages* page);
struct process_pages* proc_process_pages_get(struct proc* p, int index);
struct process_pages* proc_process_pages_last(struct proc* p);
void process_page_kalloc_clear(struct process_pages* page);
void process_page_kalloc_add(struct process_pages* page, char* addr);
char* process_page_kalloc_get(struct process_pages* page, int index);
void proc_supp_gid_clear(struct proc* p);
void proc_supp_gid_add(struct proc* p, uint16_t gid);
void proc_copy_supp_gids(struct proc* dest, struct proc* src);
int proc_has_supp_gid(struct proc* p, uint16_t gid);
void fs_pool_init(void);
struct spipe* fs_pop_free_pipe(void);
void fs_push_free_pipe(struct spipe* p);
struct file* fs_pop_free_file(void);
void fs_push_free_file(struct file* f);
int exec_load_user_string_vector(pagetable_t pagetable, uint64_t user_vec, int max_items, int max_len, int allow_null_vec, char*** out_items, int* out_count);
void exec_free_string_vector(char** items, int count);
int exec_push_string_vector(pagetable_t pagetable, char** items, int count, uint64_t* sp_io, uint64_t* vec_base_out);
int fs_rename(const char *oldpath, const char *newpath);
int fs_utimes(const char *path, uint32_t atime, uint32_t mtime);
int is_pipe(int fd);
int is_stdin(int fd);
int is_stdout(int fd);
int is_tty(int fd);
void pipe_open(int* fd1, int* fd2);
int piperead(int fd, char *addr, int n);
int pipewrite(int fd, char *addr, int n);
void fs_dup2(int oldfd, int newfd);
void fs_init(struct file** file_table);
struct file** get_current_file_table();
void fs_dup_table(struct proc* owner, struct file** result, struct file** orig);
ssize_t fs_size(int fd);
void fs_exit(struct file** file_table);
void free_fs_table(struct file** file_table);
void file_system_init();
extern void* gProc;
struct proc* proc_slot_get(int pid);
void proc_slot_set(int pid, struct proc* p);
void proc_slot_reset(void);
extern int gActiveProc;
void proc_list_init(void);
void proc_list_add(struct proc* p);
void proc_list_remove(struct proc* p);
void proc_list_replace(struct proc* oldp, struct proc* newp);
struct proc* proc_find_zombie_in_pgrp(uint16_t pgrp);
struct proc* proc_find_zombie_child(int parent_pid);
bool proc_has_child(int parent_pid);
bool proc_is_file_open(uint32_t inum);
void kernel_state_init(void);
void kernel_state_clear(void);
void kernel_state_add(struct sKernelState* state);
struct sKernelState* kernel_state_find(int active_proc);
struct sKernelState* kernel_state_take(int active_proc);
struct sKernelState* kernel_state_take_first(void);
int proc_find_pid(struct proc* p);
#ifndef O_RDONLY
#define O_RDONLY 0
#endif
#ifndef O_WRONLY
#define O_WRONLY 1
#endif
#ifndef O_RDWR
#define O_RDWR 2
#endif
#ifndef O_CREAT
#define O_CREAT (1<<9)
#endif
#ifndef O_TRUNC
#define O_TRUNC (1<<10)
#endif
#ifndef O_APPEND
#define O_APPEND (1<<11)
#endif
void *malloc(size_t size);
void free(void *ptr);
void* memset(void *dst, int c, unsigned int n);
void *calloc(size_t nmemb, size_t size);
int strlen(const char *s);
void* memcpy(void *dst, const void *src, unsigned int n);
char* strdup(const char* s);
int strcmp(const char* s1, const char* s2);
char* strstr(const char* haystack, const char* needle);
void* memset(void *dst, int c, unsigned int n);
int memcmp(const void *v1, const void *v2, unsigned int n);
void* memmove(void *dst, const void *src, unsigned int n);
void* memcpy(void *dst, const void *src, unsigned int n);
int strncmp(const char *p, const char *q, unsigned int n);
char* strncpy(char *s, const char *t, int n);
int strlen(const char *s);
void puts(const char *s);
char* strncat(char* dest, const char* src, size_t n);
char* strchr(const char* s, int c);
char *strtok(char *s, const char *delim);
void exit(int n);
char* itoa(char* buf, unsigned long val_, int base, int is_signed);
int vasprintf(char** out, const char* fmt, va_list ap);
int snprintf(char* out, unsigned long out_size, const char* fmt, ...);
void printint(int val_, int base, int sign);
void printlong(unsigned long val_, int base, int sign);
void printlonglong(unsigned long long val_, int base, int sign);
int printf(const char* fmt, ...);
void* sbrk(ptrdiff_t incr);
int Sys_execve();
int Sys_execved();
int Sys_getpid();
int Sys_tcsetpgrp();
int Sys_fstat();
int Sys_lseek();
int Sys_execv();
int Sys_fork();
int Sys_closedir();
int Sys_readdir();
int Sys_realpath();
int Sys_setgid();
int Sys_setuid();
int Sys_getuid();
int Sys_getgid();
int Sys_gettimeofday();
int Sys_umask();
int Sys_utimes();
int Sys_settimeofday();
int Sys_chmod();
int Sys_lstat();
int Sys_readlink();
int Sys_write();
int Sys_exit();
int Sys_wait();
int Sys_waitdirect();
int Sys_isatty();
int Sys_open();
int Sys_opendir();
int Sys_getcwd();
int Sys_chdir();
int Sys_mkdir();
int Sys_rmdir();
int Sys_unlink();
int Sys_rename();
int Sys_link();
int Sys_symlink();
int Sys_stat();
int Sys_chown();
int Sys_pipe(void);
int Sys_read();
int Sys_dup2(void);
int Sys_pipe(void);
int Sys_brk();
int Sys_sleep();
int uvm_alloc(struct proc *p, pagetable_t pagetable, uint64_t old_sz, uint64_t new_sz);
void uvm_dealloc(pagetable_t pagetable, uint64_t old_sz, uint64_t new_sz);
extern char TRAPFRAME[];
extern char TRAPFRAME2[];
extern char TRAMPOLINE[];
extern char COMMON[];
void panic(char* str);
extern uint64_t kernel_sp __attribute__((section(".common")));
extern uint64_t user_sp __attribute__((section(".common")));
extern uint64_t kernel_satp __attribute__((section(".common"))); // Referenced from trap.S
extern uint64_t user_satp __attribute__((section(".common")));
int copyin(pagetable_t pagetable, char *dst, uint64_t srcva, uint64_t len);
int copyinstr(pagetable_t pagetable, char *dst, uint64_t srcva, uint64_t max);
int copyout(pagetable_t pagetable, uint64_t dstva, void *src, uint64_t len);
void putchar(char c);
void alloc_prog(char* elf_buf, int elf_buf_size, int fork_flag, int exec_flag, int* child_proc_index, int debug_);
void free_proc(struct proc *p);
void remove_kernel_state(int active_proc);
void yield();
static inline uint64_t r_time()
{
uint64_t x;
asm volatile("csrr %0, time" : "=r" (x) );
return x;
}
static inline void w_stimecmp(uint64_t x)
{
asm volatile("csrw 0x14d, %0" : : "r" (x));
}
static inline void w_sie(uint64_t x) {
asm volatile("csrw sie, %0" : : "r"(x));
}
static inline uint64_t r_sie() {
uint64_t x;
asm volatile("csrr %0, sie" : "=r"(x));
return x;
}
static inline uint64_t r_sstatus() {
uint64_t x;
asm volatile("csrr %0, sstatus" : "=r" (x) );
return x;
}
static inline void w_sstatus(uint64_t x) {
asm volatile("csrw sstatus, %0" : : "r"(x));
}
extern int gNumProc;
extern uint32_t g_time_offset;
#endif