彻底解决elf文件装载问题

This commit is contained in:
XIAOYI12 2024-08-31 01:23:06 +08:00
parent a2074a168f
commit be9c0b187c
12 changed files with 60 additions and 17 deletions

View File

@ -3,6 +3,7 @@
#define SYSCALL_PUTCHAR 1 #define SYSCALL_PUTCHAR 1
#define SYSCALL_PRINT 2 #define SYSCALL_PRINT 2
#define SYSCALL_GETC 3
#include <stdint.h> #include <stdint.h>
@ -20,4 +21,10 @@ static inline void syscall_putchar(char c){
asm volatile("int $31\n\t" : "=a"(rets) : "0"(SYSCALL_PUTCHAR), "r"(ebx) : "memory", "cc"); 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;
}
#endif #endif

View File

@ -1,11 +1,33 @@
#include "../include/syscall.h" #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;
}
void hlt(){ void hlt(){
while (1); while (1);
} }
int main(){ int main(){
syscall_print("Hello! User Application!\n"); syscall_print("Debug info\n");
//char* info;
//gets(info);
//syscall_print(info);
//put_char('A'); //put_char('A');
hlt(); hlt();
return 0; return 0;

View File

@ -19,6 +19,7 @@ extern syscall
asm_syscall_handler: asm_syscall_handler:
cli
push ds push ds
push es push es
push fs push fs

View File

@ -7,6 +7,7 @@
// b 0x211972 // b 0x211972
// b 0x20d0a6 // b 0x20d0a6
#define ACCESS_ONCE(x) (*(volatile typeof(x) *) (x))
#define LONG_MAX 9223372036854775807L #define LONG_MAX 9223372036854775807L
#define LONG_MIN -9223372036854775808L #define LONG_MIN -9223372036854775808L

View File

@ -6,9 +6,10 @@
#include "isr.h" #include "isr.h"
#define KHEAP_INITIAL_SIZE 0xf00000 #define KHEAP_INITIAL_SIZE 0xf00000
#define KHEAP_START 0xa0000000 #define KHEAP_START 0x90000000
#define STACK_SIZE 32768 #define STACK_SIZE 32768
#define USER_EXEC_FILE_START 0xa0000000
#define USER_START 0xb0000000 #define USER_START 0xb0000000
#define USER_END (USER_START + 0xf00000) #define USER_END (USER_START + 0xf00000)
#define USER_HEAP_END (USER_END - STACK_SIZE) #define USER_HEAP_END (USER_END - STACK_SIZE)
@ -21,8 +22,8 @@ typedef char ALIGN[16];
#include "multiboot.h" #include "multiboot.h"
#include "common.h" #include "common.h"
typedef struct page { typedef volatile struct page {
uint32_t present: 1; volatile uint32_t present: 1;
uint32_t rw: 1; uint32_t rw: 1;
uint32_t user: 1; uint32_t user: 1;
uint32_t accessed: 1; uint32_t accessed: 1;

View File

@ -7,6 +7,7 @@
#define SYSCALL_PUTC 1 #define SYSCALL_PUTC 1
#define SYSCALL_PRINT 2 #define SYSCALL_PRINT 2
#define SYSCALL_GETC 3
void syscall_install(); void syscall_install();

View File

@ -139,9 +139,9 @@ void kernel_main(multiboot_t *multiboot) {
//klogf(user_process("service.bin","Service") != -1,"Service base process init.\n"); //klogf(user_process("service.bin","Service") != -1,"Service base process init.\n");
klogf(user_process("init.bin","Init") != -1,"Init base process init.\n"); klogf(user_process("init.bin","Init") != -1,"Init base process init.\n");
int pid = kernel_thread(setup_shell,NULL,"CPOS-Shell"); //int pid = kernel_thread(setup_shell,NULL,"CPOS-Shell");
klogf(pid != -1,"Launch kernel shell.\n"); //klogf(pid != -1,"Launch kernel shell.\n");
kernel_thread(check_task,&pid,"CPOS-CK"); //kernel_thread(check_task,&pid,"CPOS-CK");
//panic_pane("System out of memory error!",OUT_OF_MEMORY); //panic_pane("System out of memory error!",OUT_OF_MEMORY);

View File

@ -7,7 +7,7 @@
page_directory_t *kernel_directory = 0; // 内核用页目录 page_directory_t *kernel_directory = 0; // 内核用页目录
page_directory_t *current_directory = 0; // 当前页目录 page_directory_t *current_directory = 0; // 当前页目录
uint32_t *frames; volatile uint32_t *frames;
uint32_t nframes; uint32_t nframes;
extern struct task_struct *current; extern struct task_struct *current;
@ -73,11 +73,11 @@ void alloc_frame(page_t *page, int is_kernel, int is_writable) {
void alloc_frame_line(page_t *page, uint32_t line,int is_kernel, int is_writable) { void alloc_frame_line(page_t *page, uint32_t line,int is_kernel, int is_writable) {
set_frame(line); set_frame(line);
memset(page,0,4); memset(page,0,4);
page->present = 1; // 现在这个页存在了 page->present = 1; // 现在这个页存在了
page->rw = is_writable ? 1 : 0; // 是否可写由is_writable决定 page->rw = is_writable ? 1 : 0; // 是否可写由is_writable决定
page->user = is_kernel ? 0 : 1; // 是否为用户态由is_kernel决定 page->user = is_kernel ? 0 : 1; // 是否为用户态由is_kernel决定
page->frame = line / 0x1000; page->frame = line / 0x1000;
} }
void free_frame(page_t *page) { void free_frame(page_t *page) {

View File

@ -4,6 +4,7 @@
#include "../include/description_table.h" #include "../include/description_table.h"
#include "../include/graphics.h" #include "../include/graphics.h"
#include "../include/io.h" #include "../include/io.h"
#include "../include/shell.h"
static void syscall_puchar(uint32_t ebx,uint32_t ecx,uint32_t edx,uint32_t esi,uint32_t edi){ static void syscall_puchar(uint32_t ebx,uint32_t ecx,uint32_t edx,uint32_t esi,uint32_t edi){
printf("%c",ebx); printf("%c",ebx);
@ -13,9 +14,14 @@ static void syscall_print(uint32_t ebx,uint32_t ecx,uint32_t edx,uint32_t esi,ui
printf("%s",ebx); printf("%s",ebx);
} }
static char syscall_getc(uint32_t ebx,uint32_t ecx,uint32_t edx,uint32_t esi,uint32_t edi){
return getc();
}
void *sycall_handlers[MAX_SYSCALLS] = { void *sycall_handlers[MAX_SYSCALLS] = {
[SYSCALL_PUTC] = syscall_puchar, [SYSCALL_PUTC] = syscall_puchar,
[SYSCALL_PRINT] = syscall_print, [SYSCALL_PRINT] = syscall_print,
[SYSCALL_GETC] = syscall_getc,
}; };
typedef size_t (*syscall_t)(size_t, size_t, size_t, size_t, size_t); typedef size_t (*syscall_t)(size_t, size_t, size_t, size_t, size_t);
@ -28,7 +34,6 @@ size_t syscall() {
asm("mov %%edx, %0\n\t" : "=r"(edx)); asm("mov %%edx, %0\n\t" : "=r"(edx));
asm("mov %%esi, %0\n\t" : "=r"(esi)); asm("mov %%esi, %0\n\t" : "=r"(esi));
asm("mov %%edi, %0\n\t" : "=r"(edi)); asm("mov %%edi, %0\n\t" : "=r"(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) { if (0 <= eax && eax < MAX_SYSCALLS && sycall_handlers[eax] != NULL) {
eax = ((syscall_t)sycall_handlers[eax])(ebx, ecx, edx, esi, edi); eax = ((syscall_t)sycall_handlers[eax])(ebx, ecx, edx, esi, edi);
} else { } else {

View File

@ -227,7 +227,12 @@ int32_t user_process(char *path, char *name){ // 用户进程创建
alloc_frame(pg,0,1); alloc_frame(pg,0,1);
} }
char* buffer = user_alloc(new_task,size); for (int i = USER_EXEC_FILE_START; i < USER_EXEC_FILE_START + size; i++) {
page_t *pg = get_page(i,1,page, false);
alloc_frame(pg,0,1);
}
char* buffer = USER_EXEC_FILE_START;//user_alloc(new_task,size);
memset(buffer,0,size); memset(buffer,0,size);
vfs_readfile(path,buffer); vfs_readfile(path,buffer);
@ -238,7 +243,6 @@ int32_t user_process(char *path, char *name){ // 用户进程创建
printf("Unknown exec file format.\n"); printf("Unknown exec file format.\n");
return -1; return -1;
} }
// printf("Process Main Address: %08x\n",ehdr->e_entry);
uint32_t main = ehdr->e_entry; uint32_t main = ehdr->e_entry;
load_elf(ehdr,page); load_elf(ehdr,page);

View File

@ -24,7 +24,6 @@ char getc() {
int gets(char *buf, int buf_size) { int gets(char *buf, int buf_size) {
int index = 0; int index = 0;
char c; char c;
logk("GETS DEBUG I\n");
while ((c = getc()) != '\n') { while ((c = getc()) != '\n') {
if (c == '\b') { if (c == '\b') {
if (index > 0) { if (index > 0) {
@ -36,7 +35,6 @@ int gets(char *buf, int buf_size) {
putchar(c); putchar(c);
} }
} }
logk("GETS DEBUG II\n");
buf[index] = '\0'; buf[index] = '\0';
putchar(c); putchar(c);
return index; return index;

View File

@ -32,10 +32,13 @@ void load_segment(Elf32_Phdr *phdr,page_directory_t *dir, void *elf) {
alloc_frame(get_page(i,1,dir,false),0,1); 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; uint32_t p_vaddr = (void *)phdr->p_vaddr;
uint32_t p_filesz = (void *)phdr->p_filesz;
uint32_t p_memsz = (void *)phdr->p_memsz;
memcpy((void *)phdr->p_vaddr, elf + phdr->p_offset, phdr->p_filesz); memcpy((void *)phdr->p_vaddr, elf + phdr->p_offset, phdr->p_filesz);
if (phdr->p_memsz > phdr->p_filesz) { // 这个是bss段
memset((void *)(phdr->p_vaddr + phdr->p_filesz), 0, phdr->p_memsz - phdr->p_filesz); if (p_memsz > p_filesz) { // 这个是bss段
memset((void *)(p_vaddr + p_filesz), 0, p_memsz - p_filesz);
} }
} }