From a2074a168f94ec9d6070a29ca976dde03dbdd378 Mon Sep 17 00:00:00 2001 From: xiaoyi1212 Date: Fri, 30 Aug 2024 16:12:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dsyscall=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E5=A4=84=E7=90=86=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/def.mk | 2 +- apps/include/stdio.h | 3 +++ apps/include/syscall.h | 23 ++++++++++++++++++++ apps/init/init.c | 5 +++-- apps/libs/Makefile | 2 +- apps/libs/syscall.asm | 8 ------- apps/libs/syscall.c | 2 ++ src/include/syscall.h | 48 ------------------------------------------ src/kernel/syscall.c | 21 ++++++++---------- src/kernel/task.c | 5 +++-- src/util/elf.c | 2 +- 11 files changed, 46 insertions(+), 75 deletions(-) create mode 100644 apps/include/syscall.h delete mode 100644 apps/libs/syscall.asm create mode 100644 apps/libs/syscall.c diff --git a/apps/def.mk b/apps/def.mk index 996c05e..20b70cb 100644 --- a/apps/def.mk +++ b/apps/def.mk @@ -1,4 +1,4 @@ -CFLAGS = -m32 -I$(INCLUDE_PATH) -nostdinc -nolibc -nostdlib -ffreestanding -fno-stack-protector -Qn -fno-pic -fno-pie -fno-asynchronous-unwind-tables -fomit-frame-pointer -finput-charset=UTF-8 -fexec-charset=GB2312 -march=pentium -Qn -O0 -w +CFLAGS = -m32 -I$(INCLUDE_PATH) -nolibc -nostdlib -ffreestanding -fno-stack-protector -Qn -fno-pic -fno-pie -fno-asynchronous-unwind-tables -fomit-frame-pointer -finput-charset=UTF-8 -fexec-charset=GB2312 -march=pentium -Qn -O0 -w CPPFLAGS = -m32 -I$(INCLUDE_PATH) -nostdinc -nolibc -nostdlib -ffreestanding -fno-exceptions -fno-stack-protector -Qn -fno-pic -fno-pie -fno-asynchronous-unwind-tables -fomit-frame-pointer -finput-charset=UTF-8 -fexec-charset=GB2312 -Qn -O3 -march=pentium -fno-rtti -w CC = gcc diff --git a/apps/include/stdio.h b/apps/include/stdio.h index 4668828..bc62729 100644 --- a/apps/include/stdio.h +++ b/apps/include/stdio.h @@ -1,6 +1,9 @@ #ifndef CRASHPOWEROS_STDIO_H #define CRASHPOWEROS_STDIO_H +#include +#include + void put_char(char a); #endif diff --git a/apps/include/syscall.h b/apps/include/syscall.h new file mode 100644 index 0000000..2a35265 --- /dev/null +++ b/apps/include/syscall.h @@ -0,0 +1,23 @@ +#ifndef CRASHPOWEROS_SYSCALL_H +#define CRASHPOWEROS_SYSCALL_H + +#define SYSCALL_PUTCHAR 1 +#define SYSCALL_PRINT 2 + +#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"); +} + +#endif diff --git a/apps/init/init.c b/apps/init/init.c index a848433..4365af0 100644 --- a/apps/init/init.c +++ b/apps/init/init.c @@ -1,11 +1,12 @@ -#include "../include/stdio.h" +#include "../include/syscall.h" void hlt(){ while (1); } int main(){ - put_char('A'); + syscall_print("Hello! User Application!\n"); + //put_char('A'); hlt(); return 0; } \ No newline at end of file diff --git a/apps/libs/Makefile b/apps/libs/Makefile index b4fc35b..64c460a 100644 --- a/apps/libs/Makefile +++ b/apps/libs/Makefile @@ -4,7 +4,7 @@ OBJS_PACK = out/syscall.obj default : $(OBJS_PACK) ar rv ../libo/libp.a $(OBJS_PACK) out/%.obj : %.c Makefile - gcc -m32 -I../include -nostdinc -nostdlib -fno-builtin -ffreestanding -fno-stack-protector -Qn -fno-pic -fno-pie -fno-asynchronous-unwind-tables -fomit-frame-pointer -march=pentium -O0 -w -c $*.c -o out/$*.obj + gcc -m32 -I../include -nostdlib -fno-builtin -ffreestanding -fno-stack-protector -Qn -fno-pic -fno-pie -fno-asynchronous-unwind-tables -fomit-frame-pointer -march=pentium -O0 -w -c $*.c -o out/$*.obj out/%.obj : %.cpp Makefile gcc -m32 -I../include -nostdinc -nostdlib -fno-builtin -ffreestanding -fno-stack-protector -Qn -fno-pic -fno-pie -fno-asynchronous-unwind-tables -fomit-frame-pointer -march=pentium -O0 -w -c $*.cpp -o out/$*.obj out/%.obj : %.asm Makefile diff --git a/apps/libs/syscall.asm b/apps/libs/syscall.asm deleted file mode 100644 index c70f964..0000000 --- a/apps/libs/syscall.asm +++ /dev/null @@ -1,8 +0,0 @@ -global put_char - -put_char: - push eax - mov eax, 21h - int 31h - pop eax - ret \ No newline at end of file diff --git a/apps/libs/syscall.c b/apps/libs/syscall.c new file mode 100644 index 0000000..6dde983 --- /dev/null +++ b/apps/libs/syscall.c @@ -0,0 +1,2 @@ +#include "../include/syscall.h" + diff --git a/src/include/syscall.h b/src/include/syscall.h index 3eb1473..a91c148 100644 --- a/src/include/syscall.h +++ b/src/include/syscall.h @@ -8,54 +8,6 @@ #define SYSCALL_PUTC 1 #define SYSCALL_PRINT 2 -#define DEFN_SYSCALL0(fn, num) \ -int syscall_##fn() \ -{ \ - int a; \ - asm volatile("int $0x80" : "=a" (a) : "0" (num)); \ - return a; \ -} - -#define DEFN_SYSCALL1(fn, num, P1) \ -int syscall_##fn(P1 p1) \ -{ \ - int a; \ - asm volatile("int $0x80" : "=a" (a) : "0" (num), "b" ((int)p1)); \ - return a; \ -} - -#define DEFN_SYSCALL2(fn, num, P1, P2) \ -int syscall_##fn(P1 p1, P2 p2) \ -{ \ - int a; \ - asm volatile("int $0x80" : "=a" (a) : "0" (num), "b" ((int)p1), "c" ((int)p2)); \ - return a; \ -} - -#define DEFN_SYSCALL3(fn, num, P1, P2, P3) \ -int syscall_##fn(P1 p1, P2 p2, P3 p3) \ -{ \ - int a; \ - asm volatile("int $0x80" : "=a" (a) : "0" (num), "b" ((int)p1), "c" ((int)p2), "d"((int)p3)); \ - return a; \ -} - -#define DEFN_SYSCALL4(fn, num, P1, P2, P3, P4) \ -int syscall_##fn(P1 p1, P2 p2, P3 p3, P4 p4) \ -{ \ - int a; \ - asm volatile("int $0x80" : "=a" (a) : "0" (num), "b" ((int)p1), "c" ((int)p2), "d" ((int)p3), "S" ((int)p4)); \ - return a; \ -} - -#define DEFN_SYSCALL5(fn, num) \ -int syscall_##fn(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) \ -{ \ - int a; \ - asm volatile("int $0x80" : "=a" (a) : "0" (num), "b" ((int)p1), "c" ((int)p2), "d" ((int)p3), "S" ((int)p4), "D" ((int)p5)); \ - return a; \ -} - void syscall_install(); #endif diff --git a/src/kernel/syscall.c b/src/kernel/syscall.c index 69b472e..d8f64af 100644 --- a/src/kernel/syscall.c +++ b/src/kernel/syscall.c @@ -5,20 +5,17 @@ #include "../include/graphics.h" #include "../include/io.h" -void syscall_handler(registers_t regs){ - io_cli(); - register uint32_t eax asm("eax"); - printf("Syscall is enable. EAX: %08x PHIEAX: %08x\n",regs.eax,eax); - if(regs.eax == 0x01){ - putchar((regs.edx)); - } - io_sti(); - return; +static void syscall_puchar(uint32_t ebx,uint32_t ecx,uint32_t edx,uint32_t esi,uint32_t edi){ + printf("%c",ebx); +} + +static void syscall_print(uint32_t ebx,uint32_t ecx,uint32_t edx,uint32_t esi,uint32_t edi){ + printf("%s",ebx); } void *sycall_handlers[MAX_SYSCALLS] = { - [SYSCALL_PUTC] = putchar, - [SYSCALL_PRINT] = print, + [SYSCALL_PUTC] = syscall_puchar, + [SYSCALL_PRINT] = syscall_print, }; typedef size_t (*syscall_t)(size_t, size_t, size_t, size_t, size_t); @@ -31,7 +28,7 @@ size_t syscall() { asm("mov %%edx, %0\n\t" : "=r"(edx)); asm("mov %%esi, %0\n\t" : "=r"(esi)); asm("mov %%edi, %0\n\t" : "=r"(edi)); - printf("eax: %d, ebx: %d, ecx: %d, edx: %d, esi: %d, edi: %d\n", eax, ebx, ecx, edx, esi, edi); + //printf("eax: %08x, ebx: %08x, ecx: %08x,\n edx: %08x, esi: %08x, edi: %08x\n", eax, ebx, ecx, edx, esi, edi); if (0 <= eax && eax < MAX_SYSCALLS && sycall_handlers[eax] != NULL) { eax = ((syscall_t)sycall_handlers[eax])(ebx, ecx, edx, esi, edi); } else { diff --git a/src/kernel/task.c b/src/kernel/task.c index 2155fc4..16180f7 100644 --- a/src/kernel/task.c +++ b/src/kernel/task.c @@ -232,20 +232,21 @@ int32_t user_process(char *path, char *name){ // 用户进程创建 memset(buffer,0,size); vfs_readfile(path,buffer); + Elf32_Ehdr *ehdr = buffer; if(!elf32Validate(ehdr)){ printf("Unknown exec file format.\n"); return -1; } - printf("Process Main Address: %08x\n",ehdr->e_entry); + // printf("Process Main Address: %08x\n",ehdr->e_entry); uint32_t main = ehdr->e_entry; load_elf(ehdr,page); - uint32_t *stack_top = (uint32_t * )((uint32_t) new_task + STACK_SIZE); *(--stack_top) = (uint32_t) main; + //*(--stack_top) = (uint32_t) buffer; *(--stack_top) = (uint32_t) kthread_exit; *(--stack_top) = (uint32_t) switch_to_user_mode; diff --git a/src/util/elf.c b/src/util/elf.c index b00f463..0cb6891 100644 --- a/src/util/elf.c +++ b/src/util/elf.c @@ -31,7 +31,7 @@ void load_segment(Elf32_Phdr *phdr,page_directory_t *dir, void *elf) { for (size_t i = lo; i < hi; i += 0x1000) { alloc_frame(get_page(i,1,dir,false),0,1); } - printf("VDDR: %08x elf: %08x offset: %08x filesz: %08x elf+offset: %08x\n",phdr->p_vaddr, elf , phdr->p_offset, phdr->p_filesz, elf + phdr->p_offset); + //printf("VDDR: %08x elf: %08x offset: %08x filesz: %08x elf+offset: %08x\n",phdr->p_vaddr, elf , phdr->p_offset, phdr->p_filesz, elf + phdr->p_offset); //if(phdr->p_vaddr == 0xaffff000) return; memcpy((void *)phdr->p_vaddr, elf + phdr->p_offset, phdr->p_filesz); if (phdr->p_memsz > phdr->p_filesz) { // 这个是bss段