-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.c
57 lines (48 loc) · 1 KB
/
user.c
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
#include"user.h"
extern char __stack_top[];
int syscall(int sysno, int arg0, int arg1, int arg2)
{
register int a0 __asm__("a0") = arg0;
register int a1 __asm__("a1") = arg1;
register int a2 __asm__("a2") = arg2;
register int a3 __asm__("a3") = sysno;
__asm__ __volatile__(
"ecall"
: "=r"(a0)
: "r"(a0), "r"(a1), "r"(a2), "r"(a3)
: "memory"
);
return a0;
}
__attribute__((noreturn)) void exit(void)
{
syscall(SYS_EXIT, 0, 0, 0);
for(;;);
}
void putchar(char ch)
{
syscall(SYS_PUTCHAR, ch, 0, 0);
}
int getchar(void)
{
return syscall(SYS_GETCHAR, 0, 0, 0);
}
int read(const char *filename, char *buf, int len)
{
return syscall(SYS_READ, (int)filename, (int)buf, len);
}
int write(const char *filename, const char *buf, int len)
{
return syscall(SYS_WRITE, (int)filename, (int)buf, len);
}
__attribute__((section(".text.start")))
__attribute__((naked))
void start(void)
{
__asm__ __volatile__(
"mv sp, %[stack_top]\n"
"call main \n"
"call exit \n"
:: [stack_top] "r" (__stack_top)
);
}