From 80570dcdbf293ee457fcc89ae60b62c730f4c3b2 Mon Sep 17 00:00:00 2001 From: xiaoyi1212 Date: Sat, 31 Aug 2024 12:36:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E7=94=A8=E6=88=B7=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=E9=80=80=E5=87=BA=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/def.mk | 2 +- apps/include/libc.h | 4 + apps/include/stdio.h | 4 + apps/include/string.h | 16 +++ apps/include/syscall.h | 31 ++--- apps/init/init.c | 32 +---- apps/libs/Makefile | 2 +- apps/libs/libc.c | 13 ++ apps/libs/print.c | 288 +++++++++++++++++++++++++++++++++++++++++ apps/libs/string.c | 75 +++++++++++ apps/libs/syscall.c | 42 ++++++ src/include/syscall.h | 3 + src/kernel/syscall.c | 24 +++- src/kernel/task.c | 2 - src/sysapp/shell.c | 4 - 15 files changed, 485 insertions(+), 57 deletions(-) create mode 100644 apps/include/libc.h create mode 100644 apps/include/string.h create mode 100644 apps/libs/libc.c create mode 100644 apps/libs/print.c create mode 100644 apps/libs/string.c diff --git a/apps/def.mk b/apps/def.mk index 20b70cb..5ae14c2 100644 --- a/apps/def.mk +++ b/apps/def.mk @@ -10,7 +10,7 @@ NASM = nasm LIBS_PATH := ../libo LD = ld -LD_FLAGS = -Ttext 0xb0000010 -m elf_i386 -static -e main +LD_FLAGS = -Ttext 0xb0000010 -m elf_i386 -static -e _start LINK = $(LD) $(LD_FLAGS) BASIC_LIB_C = $(LIBS_PATH)/libp.a \ No newline at end of file diff --git a/apps/include/libc.h b/apps/include/libc.h new file mode 100644 index 0000000..89be91b --- /dev/null +++ b/apps/include/libc.h @@ -0,0 +1,4 @@ +#ifndef CRASHPOWEROS_LIBC_H +#define CRASHPOWEROS_LIBC_H + +#endif diff --git a/apps/include/stdio.h b/apps/include/stdio.h index bc62729..5c68fdf 100644 --- a/apps/include/stdio.h +++ b/apps/include/stdio.h @@ -3,7 +3,11 @@ #include #include +#include void put_char(char a); +int printf(const char* fmt, ...); +int puts(const char *s); +int vsprintf(char *buf, const char *fmt, va_list args); #endif diff --git a/apps/include/string.h b/apps/include/string.h new file mode 100644 index 0000000..3644653 --- /dev/null +++ b/apps/include/string.h @@ -0,0 +1,16 @@ +#ifndef CRASHPOWEROS_STRING_H +#define CRASHPOWEROS_STRING_H + +#include + +int isspace(int c); +int isdigit(int c); +int isalpha(int c); +int isupper(int c); +size_t strnlen(const char *s, size_t maxlen); +size_t strlen(const char *str); +int strcmp(const char *s1, const char *s2); +char *strcpy(char *dest, const char *src); +char *strcat(char *dest, const char *src); + +#endif diff --git a/apps/include/syscall.h b/apps/include/syscall.h index 03b8040..bbdc2a1 100644 --- a/apps/include/syscall.h +++ b/apps/include/syscall.h @@ -1,30 +1,21 @@ #ifndef CRASHPOWEROS_SYSCALL_H #define CRASHPOWEROS_SYSCALL_H -#define SYSCALL_PUTCHAR 1 +#define SYSCALL_PUTC 1 #define SYSCALL_PRINT 2 #define SYSCALL_GETC 3 +#define SYSCALL_MALLOC 4 +#define SYSCALL_FREE 5 +#define SYSCALL_EXIT 6 #include +#include -static inline void syscall_print(char* c){ - uint32_t rets; - uint32_t __arg1 = c; - register uint32_t ebx asm("ebx") = __arg1; - asm volatile("int $31\n\t" : "=a"(rets) : "0"(SYSCALL_PRINT), "r"(ebx) : "memory", "cc"); -} - -static inline void syscall_putchar(char c){ - uint32_t rets; - uint32_t __arg1 = (uint32_t)(c); - register uint32_t ebx asm("ebx") = __arg1; - asm volatile("int $31\n\t" : "=a"(rets) : "0"(SYSCALL_PUTCHAR), "r"(ebx) : "memory", "cc"); -} - -static inline char syscall_getc(){ - uint32_t rets; - asm volatile("int $31\n\t" : "=a"(rets) : "0"(SYSCALL_GETC) : "memory", "cc"); - return rets; -} +void syscall_print(char* c); +void syscall_putchar(char c); +char syscall_getc(); +void* syscall_malloc(size_t size); +void syscall_free(void *ptr); +void syscall_exit(int code); #endif diff --git a/apps/init/init.c b/apps/init/init.c index 9582230..5baf018 100644 --- a/apps/init/init.c +++ b/apps/init/init.c @@ -1,34 +1,10 @@ -#include "../include/syscall.h" - -int gets(char *buf) { - int index = 0; - char c; - while ((c = syscall_getc()) != '\n') { - if (c == '\b') { - if (index > 0) { - index--; - syscall_print("\b \b"); - } - } else { - buf[index++] = c; - syscall_putchar(c); - } - } - buf[index] = '\0'; - syscall_putchar(c); - return index; -} +#include "../include/stdio.h" void hlt(){ while (1); } int main(){ - syscall_print("Debug info\n"); - //char* info; - //gets(info); - //syscall_print(info); - //put_char('A'); - hlt(); - return 0; -} \ No newline at end of file + printf("User application %d\n",12); + return -1; +} diff --git a/apps/libs/Makefile b/apps/libs/Makefile index 64c460a..7c4fb38 100644 --- a/apps/libs/Makefile +++ b/apps/libs/Makefile @@ -1,4 +1,4 @@ -OBJS_PACK = out/syscall.obj +OBJS_PACK = out/syscall.obj out/print.obj out/string.obj out/libc.obj default : $(OBJS_PACK) diff --git a/apps/libs/libc.c b/apps/libs/libc.c new file mode 100644 index 0000000..1fbf216 --- /dev/null +++ b/apps/libs/libc.c @@ -0,0 +1,13 @@ +#include "../include/stdio.h" +#include "../include/syscall.h" + +void put_char(char c){ + syscall_putchar(c); +} + +void _start(){ + extern int main(); + int ret = main(); + syscall_exit(ret); + while (1); +} \ No newline at end of file diff --git a/apps/libs/print.c b/apps/libs/print.c new file mode 100644 index 0000000..5526b06 --- /dev/null +++ b/apps/libs/print.c @@ -0,0 +1,288 @@ +#include "../include/stdio.h" +#include "../include/syscall.h" +#include "../include/string.h" + +static int skip_atoi(const char **s) { + int i = 0; + + while (isdigit(**s)) + i = i * 10 + *((*s)++) - '0'; + return i; +} + +#define ZEROPAD 1 /* pad with zero */ +#define SIGN 2 /* unsigned/signed long */ +#define PLUS 4 /* show plus */ +#define SPACE 8 /* space if plus */ +#define LEFT 16 /* left justified */ +#define SMALL 32 /* Must be 32 == 0x20 */ +#define SPECIAL 64 /* 0x */ + +#define __do_div(n, base) ({ \ +int __res; \ +__res = ((unsigned long) n) % (unsigned) base; \ +n = ((unsigned long) n) / (unsigned) base; \ +__res; }) + +int printf(const char* fmt, ...) { + static char buf[4096]; + va_list va; + va_start(va, fmt); + int rets = vsprintf(buf, fmt, va); + va_end(va); + syscall_print(buf); + return rets; +} + +int puts(const char *s) { + syscall_print(s); + syscall_print("\n"); + return 0; +} + +static char *number(char *str, long num, int base, int size, int precision, + int type) { + static const char digits[16] = "0123456789ABCDEF"; + + char tmp[66]; + char c, sign, locase; + int i; + + locase = (type & SMALL); + if (type & LEFT) + type &= ~ZEROPAD; + if (base < 2 || base > 16) + return NULL; + c = (type & ZEROPAD) ? '0' : ' '; + sign = 0; + if (type & SIGN) { + if (num < 0) { + sign = '-'; + num = -num; + size--; + } else if (type & PLUS) { + sign = '+'; + size--; + } else if (type & SPACE) { + sign = ' '; + size--; + } + } + if (type & SPECIAL) { + if (base == 16) + size -= 2; + else if (base == 8) + size--; + } + i = 0; + if (num == 0) + tmp[i++] = '0'; + else + while (num != 0) + tmp[i++] = (digits[__do_div(num, base)] | locase); + if (i > precision) + precision = i; + size -= precision; + if (!(type & (ZEROPAD + LEFT))) + while (size-- > 0) + *str++ = ' '; + if (sign) + *str++ = sign; + if (type & SPECIAL) { + if (base == 8) + *str++ = '0'; + else if (base == 16) { + *str++ = '0'; + *str++ = ('X' | locase); + } + } + if (!(type & LEFT)) + while (size-- > 0) + *str++ = c; + while (i < precision--) + *str++ = '0'; + while (i-- > 0) + *str++ = tmp[i]; + while (size-- > 0) + *str++ = ' '; + return str; +} + +int vsprintf(char *buf, const char *fmt, va_list args) { + int len; + unsigned long num; + int i, base; + char *str; + const char *s; + + int flags; /* flags to number() */ + + int field_width; /* width of output field */ + int precision; /* min. # of digits for integers; max + number of chars for from string */ + int qualifier; /* 'h', 'l', or 'L' for integer fields */ + + for (str = buf; *fmt; ++fmt) { + if (*fmt != '%') { + *str++ = *fmt; + continue; + } + + /* process flags */ + flags = 0; + repeat: + ++fmt; /* this also skips first '%' */ + switch (*fmt) { + case '-': + flags |= LEFT; + goto repeat; + case '+': + flags |= PLUS; + goto repeat; + case ' ': + flags |= SPACE; + goto repeat; + case '#': + flags |= SPECIAL; + goto repeat; + case '0': + flags |= ZEROPAD; + goto repeat; + } + + /* get field width */ + field_width = -1; + if (isdigit(*fmt)) + field_width = skip_atoi(&fmt); + else if (*fmt == '*') { + ++fmt; + /* it's the next argument */ + field_width = va_arg(args, + int); + if (field_width < 0) { + field_width = -field_width; + flags |= LEFT; + } + } + + /* get the precision */ + precision = -1; + if (*fmt == '.') { + ++fmt; + if (isdigit(*fmt)) + precision = skip_atoi(&fmt); + else if (*fmt == '*') { + ++fmt; + /* it's the next argument */ + precision = va_arg(args, + int); + } + if (precision < 0) + precision = 0; + } + + /* get the conversion qualifier */ + qualifier = -1; + if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') { + qualifier = *fmt; + ++fmt; + } + + /* default base */ + base = 10; + + switch (*fmt) { + case 'c': + if (!(flags & LEFT)) + while (--field_width > 0) + *str++ = ' '; + *str++ = (unsigned char) va_arg(args, + int); + while (--field_width > 0) + *str++ = ' '; + continue; + + case 's': + s = va_arg(args, + char *); + len = strnlen(s, precision); + + if (!(flags & LEFT)) + while (len < field_width--) + *str++ = ' '; + for (i = 0; i < len; ++i) + *str++ = *s++; + while (len < field_width--) + *str++ = ' '; + continue; + + case 'p': + if (field_width == -1) { + field_width = 2 * sizeof(void *); + flags |= ZEROPAD; + } + str = number(str, (unsigned long) va_arg(args, + void *), 16, + field_width, precision, flags); + continue; + + case 'n': + if (qualifier == 'l') { + long *ip = va_arg(args, + long *); + *ip = (str - buf); + } else { + int *ip = va_arg(args, + int *); + *ip = (str - buf); + } + continue; + + case '%': + *str++ = '%'; + continue; + + /* integer number formats - set up the flags and "break" */ + case 'o': + base = 8; + break; + + case 'x': + flags |= SMALL; + case 'X': + base = 16; + break; + + case 'd': + case 'i': + flags |= SIGN; + case 'u': + break; + + default: + *str++ = '%'; + if (*fmt) + *str++ = *fmt; + else + --fmt; + continue; + } + if (qualifier == 'l') + num = va_arg(args, + unsigned long); + else if (qualifier == 'h') { + num = (unsigned short) va_arg(args, + int); + if (flags & SIGN) + num = (short) num; + } else if (flags & SIGN) + num = va_arg(args, + int); + else + num = va_arg(args, + unsigned int); + str = number(str, num, base, field_width, precision, flags); + } + *str = '\0'; + return str - buf; +} \ No newline at end of file diff --git a/apps/libs/string.c b/apps/libs/string.c new file mode 100644 index 0000000..f23f5dd --- /dev/null +++ b/apps/libs/string.c @@ -0,0 +1,75 @@ +#include "../include/string.h" + +int isspace(int c) { + return (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' || + c == '\v'); +} + +// isdigit +int isdigit(int c) { + return (c >= '0' && c <= '9'); +} + +// isalpha +int isalpha(int c) { + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); +} + +// isupper +int isupper(int c) { + return (c >= 'A' && c <= 'Z'); +} + +size_t strnlen(const char *s, size_t maxlen) { + const char *es = s; + while (*es && maxlen) { + es++; + maxlen--; + } + + return (es - s); +} + +size_t strlen(const char *str) { + size_t len = 0; + while (str[len]) + len++; + return len; +} + +int strcmp(const char *s1, const char *s2) { + char is_equal = 1; + + for (; (*s1 != '\0') && (*s2 != '\0'); s1++, s2++) { + if (*s1 != *s2) { + is_equal = 0; + break; + } + } + + if (is_equal) { + if (*s1 != '\0') { + return 1; + } else if (*s2 != '\0') { + return -1; + } else { + return 0; + } + } else { + return (int) (*s1 - *s2); + } +} + +char *strcpy(char *dest, const char *src) { + do { + *dest++ = *src++; + } while (*src != 0); + *dest = 0; +} + +char *strcat(char *dest, const char *src) { + char *temp = dest; + while (*temp != '\0') + temp++; + while ((*temp++ = *src++) != '\0'); +} \ No newline at end of file diff --git a/apps/libs/syscall.c b/apps/libs/syscall.c index 6dde983..80bf9d8 100644 --- a/apps/libs/syscall.c +++ b/apps/libs/syscall.c @@ -1,2 +1,44 @@ #include "../include/syscall.h" +void syscall_print(char* c){ + uint32_t rets; + uint32_t __arg1 = c; + register uint32_t ebx asm("ebx") = __arg1; + asm volatile("int $31\n\t" : "=a"(rets) : "0"(SYSCALL_PRINT), "r"(ebx) : "memory", "cc"); +} + +void syscall_putchar(char c){ + uint32_t rets; + uint32_t __arg1 = (uint32_t)(c); + register uint32_t ebx asm("ebx") = __arg1; + asm volatile("int $31\n\t" : "=a"(rets) : "0"(SYSCALL_PUTC), "r"(ebx) : "memory", "cc"); +} + +char syscall_getc(){ + uint32_t rets; + asm volatile("int $31\n\t" : "=a"(rets) : "0"(SYSCALL_GETC) : "memory", "cc"); + return rets; +} + +void* syscall_malloc(size_t size){ + uint32_t rets; + uint32_t __arg1 = (uint32_t)(size); + register uint32_t ebx asm("ebx") = __arg1; + asm volatile("int $31\n\t" : "=a"(rets) : "0"(SYSCALL_MALLOC), "r"(ebx) : "memory", "cc"); + return rets; +} + +void syscall_free(void *ptr){ + uint32_t rets; + uint32_t __arg1 = (uint32_t)(ptr); + register uint32_t ebx asm("ebx") = __arg1; + asm volatile("int $31\n\t" : "=a"(rets) : "0"(SYSCALL_FREE), "r"(ebx) : "memory", "cc"); +} + +void syscall_exit(int code){ + uint32_t rets; + uint32_t __arg1 = (uint32_t)(code); + register uint32_t ebx asm("ebx") = __arg1; + asm volatile("int $31\n\t" : "=a"(rets) : "0"(SYSCALL_EXIT), "r"(ebx) : "memory", "cc"); + return rets; +} \ No newline at end of file diff --git a/src/include/syscall.h b/src/include/syscall.h index 892d313..8c1db53 100644 --- a/src/include/syscall.h +++ b/src/include/syscall.h @@ -8,6 +8,9 @@ #define SYSCALL_PUTC 1 #define SYSCALL_PRINT 2 #define SYSCALL_GETC 3 +#define SYSCALL_MALLOC 4 +#define SYSCALL_FREE 5 +#define SYSCALL_EXIT 6 void syscall_install(); diff --git a/src/kernel/syscall.c b/src/kernel/syscall.c index 7f19735..99cee4b 100644 --- a/src/kernel/syscall.c +++ b/src/kernel/syscall.c @@ -5,6 +5,8 @@ #include "../include/graphics.h" #include "../include/io.h" #include "../include/shell.h" +#include "../include/heap.h" + static void syscall_puchar(uint32_t ebx,uint32_t ecx,uint32_t edx,uint32_t esi,uint32_t edi){ printf("%c",ebx); @@ -15,13 +17,33 @@ static void syscall_print(uint32_t ebx,uint32_t ecx,uint32_t edx,uint32_t esi,ui } static char syscall_getc(uint32_t ebx,uint32_t ecx,uint32_t edx,uint32_t esi,uint32_t edi){ - return getc(); + io_sti(); + char c = getc(); + printf("SYSCALL: %c\n",c); + return c; +} + +static void syscall_exit(uint32_t ebx,uint32_t ecx,uint32_t edx,uint32_t esi,uint32_t edi){ + task_kill(get_current()->pid); + printf("PID[%d] exit code: %d",get_current()->pid,ebx); +} + +static void* syscall_malloc(uint32_t ebx,uint32_t ecx,uint32_t edx,uint32_t esi,uint32_t edi){ + void* address = user_alloc(get_current(),ebx); + return address; +} + +static void syscall_free(uint32_t ebx,uint32_t ecx,uint32_t edx,uint32_t esi,uint32_t edi){ + use_free(get_current(),ebx); } void *sycall_handlers[MAX_SYSCALLS] = { [SYSCALL_PUTC] = syscall_puchar, [SYSCALL_PRINT] = syscall_print, [SYSCALL_GETC] = syscall_getc, + [SYSCALL_MALLOC] = syscall_malloc, + [SYSCALL_FREE] = syscall_free, + [SYSCALL_EXIT] = syscall_exit, }; typedef size_t (*syscall_t)(size_t, size_t, size_t, size_t, size_t); diff --git a/src/kernel/task.c b/src/kernel/task.c index eb5183c..ece4bc4 100644 --- a/src/kernel/task.c +++ b/src/kernel/task.c @@ -145,8 +145,6 @@ void task_kill(int pid) { } argv->state = TASK_DEATH; printf("Taskkill process PID:%d Name:%s\n", argv->pid, argv->name); - printf("Task [%s] exit code: -130.\n", argv->name); - kfree(argv); struct task_struct *head = running_proc_head; struct task_struct *last = NULL; diff --git a/src/sysapp/shell.c b/src/sysapp/shell.c index 6b9bc07..4775e08 100644 --- a/src/sysapp/shell.c +++ b/src/sysapp/shell.c @@ -387,7 +387,6 @@ void setup_shell() { char *buffer[255]; while (1) { - logk("DEBUG I\n"); if(hasFS) vfs_getPath(buffer); else{ buffer[0] = 'n'; @@ -397,11 +396,8 @@ void setup_shell() { buffer[4] = 's'; buffer[5] = '\0'; } - logk("DEBUG II\n"); printf("\03343cd80;default@localhost: \0334169E1;%s\\\033c6c6c6;$ ", buffer); - logk("DEBUG III\n"); if (gets(com, MAX_COMMAND_LEN) <= 0) continue; - logk("DEBUG IIII\n"); argc = cmd_parse(com, argv, ' ');