修复syscall无法处理的问题

This commit is contained in:
XIAOYI12 2024-08-30 16:12:44 +08:00
parent 9c0f4797ed
commit a2074a168f
11 changed files with 46 additions and 75 deletions

View File

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

View File

@ -1,6 +1,9 @@
#ifndef CRASHPOWEROS_STDIO_H
#define CRASHPOWEROS_STDIO_H
#include <stdint.h>
#include <stddef.h>
void put_char(char a);
#endif

23
apps/include/syscall.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef CRASHPOWEROS_SYSCALL_H
#define CRASHPOWEROS_SYSCALL_H
#define SYSCALL_PUTCHAR 1
#define SYSCALL_PRINT 2
#include <stdint.h>
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

View File

@ -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;
}

View File

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

View File

@ -1,8 +0,0 @@
global put_char
put_char:
push eax
mov eax, 21h
int 31h
pop eax
ret

2
apps/libs/syscall.c Normal file
View File

@ -0,0 +1,2 @@
#include "../include/syscall.h"

View File

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

View File

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

View File

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

View File

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