From: AceVest Date: Mon, 14 Jul 2014 15:58:45 +0000 (+0800) Subject: prepare jump to ring3 X-Git-Url: http://zhaoyanbai.com/repos/?a=commitdiff_plain;h=bdc5eb23bf18051951894099347ed01d22750dbd;p=kernel.git prepare jump to ring3 --- diff --git a/drivers/ide.c b/drivers/ide.c index b16639e..4cf9976 100644 --- a/drivers/ide.c +++ b/drivers/ide.c @@ -133,21 +133,21 @@ void ide_do_read(u64_t lba, u32_t scnt, char *buf) while(true) { - printd("%s pid %d is going to wait\n", __func__, sysc_getpid()); + //printd("%s pid %d is going to wait\n", __func__, sysc_getpid()); task->state = TASK_WAIT; irq_save(flags); finish = r->finish; - printd("%s pid %d finish %u read_scnt %u scnt %u\n", __func__, sysc_getpid(), r->finish, r->read_scnt, r->scnt); + //printd("%s pid %d finish %u read_scnt %u scnt %u\n", __func__, sysc_getpid(), r->finish, r->read_scnt, r->scnt); irq_restore(flags); if(finish) break; schedule(); - printd("%s pid %d is running\n", __func__, sysc_getpid()); + //printd("%s pid %d is running\n", __func__, sysc_getpid()); } - printd("%s pid %d is really running\n", __func__, sysc_getpid()); + //printd("%s pid %d is really running\n", __func__, sysc_getpid()); task->state = TASK_RUNNING; del_wait_queue(&r->wait, &wait); } @@ -232,7 +232,7 @@ void init_pci_controller(unsigned int classcode) void ide_default_intr() { - printd("%s\n", __func__); + //printd("%s\n", __func__); u8_t status = inb(REG_STATUS(0)); drv.irq_cnt++; @@ -262,7 +262,7 @@ void ide_default_intr() ide_printl(); - printd(" hard disk sig %04x read mode %x cnt %d\n", sig, drv.read_mode, drv.irq_cnt); + //printd(" hard disk sig %04x read mode %x cnt %d\n", sig, drv.read_mode, drv.irq_cnt); printl(MPL_IDE_INTR, "hard disk sig %x read mode %x cnt %d", sig, drv.read_mode, drv.irq_cnt); outb(PCI_IDE_CMD_STOP, drv.bus_cmd); diff --git a/fs/ext2.c b/fs/ext2.c index 25b09ac..346f012 100644 --- a/fs/ext2.c +++ b/fs/ext2.c @@ -64,7 +64,7 @@ void ext2_read_inode(unsigned int ino, ext2_inode_t *inode) blkid += ext2_gd(gn)->bg_inode_table; inoff *= EXT2_INODE_SIZE; - printk("group %u %u blkid %u blkoff %u\n", gn, gi, blkid, inoff); + printd("group %u %u blkid %u blkoff %u\n", gn, gi, blkid, inoff); BLKRW(blkid, 1, blk); @@ -78,10 +78,11 @@ void ext2_read_inode(unsigned int ino, ext2_inode_t *inode) void ext2_read_file(const ext2_inode_t *inode, char *buf) { assert(inode != 0); + assert(buf != 0); assert(inode->i_size > 0 && inode->i_size<=MAX_SUPT_FILE_SIZE); unsigned int blkcnt = inode->i_size / EXT2_BLOCK_SIZE; - int i; + int i=0; for(i=0; ii_block[i], 1, buf+i*EXT2_BLOCK_SIZE); @@ -91,12 +92,15 @@ void ext2_read_file(const ext2_inode_t *inode, char *buf) unsigned int left = inode->i_size % EXT2_BLOCK_SIZE; if(left) { + printk("read left %u bytes\n", left); + void *blk = ext2_alloc_block(); memcpy(buf+i*EXT2_BLOCK_SIZE, blk, left); ext2_free_block(blk); } + printk("read file done\n"); } unsigned int ext2_search_indir(const char *name, const ext2_inode_t *inode) @@ -114,7 +118,7 @@ unsigned int ext2_search_indir(const char *name, const ext2_inode_t *inode) { memcpy(tmp, dirent->name, dirent->name_len); tmp[dirent->name_len] = 0; - printk(" dirent %s inode %u rec_len %u name_len %u type %02d\n", + printd(" dirent %s inode %u rec_len %u name_len %u type %02d\n", tmp, dirent->inode, dirent->rec_len, dirent->name_len, dirent->file_type); if(strcmp(name, tmp) == 0) @@ -162,14 +166,10 @@ unsigned int ext2_search_inpath(const char *path) char file[MAX_FILE_NAME]; int len; - printk("--- %s\n", path); - while((len=get_filename_from_path(path, file)) != 0) { - printk("name len %u\n", len); ino = ext2_search_indir(file, inode); assert(ino != 0); - printk("FILE:%s inode %u\n", file, ino); path += len; @@ -247,7 +247,7 @@ void ext2_setup_fs() //printk("----- ino %u\n", ext2_search_inpath("/bin/test/test.txt")); - printk("----- ino %u\n", ext2_search_inpath("/bin/hello")); + //printk("----- ino %u\n", ext2_search_inpath("/bin/hello")); //printk("----- ino %u\n", ext2_search_inpath("/boot/Kernel")); //printk("----- ino %u\n", ext2_search_inpath("/boot/grub2/fonts/unicode.pf2")); } diff --git a/include/elf.h b/include/elf.h index 0181615..90a7c88 100644 --- a/include/elf.h +++ b/include/elf.h @@ -10,7 +10,7 @@ *-------------------------------------------------------------------------- */ -#ifndef _ELF_H +#ifndef _ELF_H #define _ELF_H typedef u16 Elf32_Half; @@ -33,20 +33,20 @@ typedef Elf32_Half Elf32_Versym; #define EI_NIDENT (16) typedef struct { - unsigned char e_ident[EI_NIDENT]; - Elf32_Half e_type; - Elf32_Half e_machine; - Elf32_Word e_version; - Elf32_Addr e_entry; - Elf32_Off e_phoff; - Elf32_Off e_shoff; - Elf32_Word e_flags; - Elf32_Half e_ehsize; // ELF Header Size in bytes - Elf32_Half e_phentsize; // Program header table entry size - Elf32_Half e_phnum; - Elf32_Half e_shentsize; - Elf32_Half e_shnum; - Elf32_Half e_shstrndx; //Section Header String Table Index + unsigned char e_ident[EI_NIDENT]; + Elf32_Half e_type; + Elf32_Half e_machine; + Elf32_Word e_version; + Elf32_Addr e_entry; + Elf32_Off e_phoff; + Elf32_Off e_shoff; + Elf32_Word e_flags; + Elf32_Half e_ehsize; // ELF Header Size in bytes + Elf32_Half e_phentsize; // Program header table entry size + Elf32_Half e_phnum; + Elf32_Half e_shentsize; + Elf32_Half e_shnum; + Elf32_Half e_shstrndx; //Section Header String Table Index }Elf32_Ehdr, *pElf32_Ehdr; #define ELFMAG "\177ELF" @@ -135,7 +135,7 @@ typedef struct Elf32_Word sh_type; /* Section type */ Elf32_Word sh_flags; /* Section flags */ Elf32_Addr sh_addr; /* virtual addr at execution */ - Elf32_Off sh_offset; /* Section file offset */ + Elf32_Off sh_offset; /* Section file offset */ Elf32_Word sh_size; /* Section size in bytes */ Elf32_Word sh_link; /* Link to another section */ Elf32_Word sh_info; /* Additional section infomation */ @@ -163,25 +163,25 @@ typedef struct } Elf32_Phdr,*pElf32_Phdr; /* p_type */ -#define PT_NULL 0 /* Program header table entry unused */ -#define PT_LOAD 1 /* Loadable program segment */ -#define PT_DYNAMIC 2 /* Dynamic linking information */ -#define PT_INTERP 3 /* Program interpreter */ -#define PT_NOTE 4 /* Auxiliary information */ -#define PT_SHLIB 5 /* Reserved */ -#define PT_PHDR 6 /* Entry for header table itself */ -#define PT_TLS 7 /* Thread-local storage segment */ -#define PT_NUM 8 /* Number of defined types */ -#define PT_LOOS 0x60000000 /* Start of OS-Specific */ -#define PT_GNU_EH_FRAME 0x6474E550 /* GCC .eh_frame_hdr segment */ +#define PT_NULL 0 /* Program header table entry unused */ +#define PT_LOAD 1 /* Loadable program segment */ +#define PT_DYNAMIC 2 /* Dynamic linking information */ +#define PT_INTERP 3 /* Program interpreter */ +#define PT_NOTE 4 /* Auxiliary information */ +#define PT_SHLIB 5 /* Reserved */ +#define PT_PHDR 6 /* Entry for header table itself */ +#define PT_TLS 7 /* Thread-local storage segment */ +#define PT_NUM 8 /* Number of defined types */ +#define PT_LOOS 0x60000000 /* Start of OS-Specific */ +#define PT_GNU_EH_FRAME 0x6474E550 /* GCC .eh_frame_hdr segment */ #define PT_GNU_STACK 0x6474E551 /* Indicates stack executability */ #define PT_GNU_RELRO 0x6474E552 /* Read-only after relocation */ -#define PT_LOSUNW 0x6FFFFFFA -#define PT_SHNWBSS 0x6FFFFFFA /* Sun Specific Segment */ +#define PT_LOSUNW 0x6FFFFFFA +#define PT_SHNWBSS 0x6FFFFFFA /* Sun Specific Segment */ #define PT_SUNWSTACK 0x6FFFFFFB /* Stack segment */ -#define PT_HISUNW 0x6FFFFFFF -#define PT_HIOS 0x6FFFFFFF /* End of OS-Specific */ -#define PT_LOPROC 0x70000000 /* Start of processor-specific */ -#define PT_HIPROC 0x7FFFFFFF /* End of processor-specific */ +#define PT_HISUNW 0x6FFFFFFF +#define PT_HIOS 0x6FFFFFFF /* End of OS-Specific */ +#define PT_LOPROC 0x70000000 /* Start of processor-specific */ +#define PT_HIPROC 0x7FFFFFFF /* End of processor-specific */ #endif //_ELF_H diff --git a/include/page.h b/include/page.h index 77e6309..f4fbe55 100644 --- a/include/page.h +++ b/include/page.h @@ -67,7 +67,7 @@ typedef unsigned long pte_t; #define PFN_UP(addr) (((addr) + PAGE_SIZE - 1) >> PAGE_SHIFT) #define PFN_DW(addr) ((addr) >> PAGE_SHIFT) -#define MAX_ORDER (5) +#define MAX_ORDER (11) #define MAX_OLD_ORDER (11) #define LOAD_CR3(pde) asm("movl %%edx, %%cr3"::"d"(va2pa(pde))) diff --git a/kernel/exec.c b/kernel/exec.c index ae677cc..a24ddab 100644 --- a/kernel/exec.c +++ b/kernel/exec.c @@ -37,7 +37,10 @@ int sysc_exec(const char *path, char *const argv[]) void *buf = (void *) alloc_pages(0, 5); assert(buf != 0); + printk("exec buf %08x \n", buf); + printd("begin read elf\n"); ext2_read_file(&inode, buf); + printd("end read elf\n"); pElf32_Ehdr ehdr = (pElf32_Ehdr) buf; @@ -51,60 +54,40 @@ int sysc_exec(const char *path, char *const argv[]) //printk("Entry: %08x phnum:%d\n", ehdr->e_entry, ehdr->e_phnum); int size = 0; - char *pv = NULL; // phdr 中第一个的VirtAddr int i; for(i=0; ie_phnum; i++) { pElf32_Phdr phdr; phdr = (pElf32_Phdr)(buf+ehdr->e_phoff+(i*ehdr->e_phentsize)); - //printk(" %d %08x\n", i, phdr->p_type); + + printk("Type %08x Off %08x Va %08x Pa %08x Fsz %08x Mmsz %08x\n", + phdr->p_type, phdr->p_offset, phdr->p_vaddr, phdr->p_paddr, + phdr->p_filesz, phdr->p_memsz); + if(phdr->p_type == PT_LOAD) { - size += phdr->p_memsz; - if(i==0) - pv = (char *)phdr->p_vaddr; + if(phdr->p_offset > 0 && phdr->p_memsz > 0) + { + size = ALIGN(phdr->p_offset + phdr->p_memsz, 0x1000); + } } } + printk("ELF MEM SIZE %u\n", size); + char *exe = (char *) kmalloc(size, 0); + assert(exe != 0); + printk("EXE ADDR %08x\n", exe); for(i=0; ie_phnum; i++) { pElf32_Phdr phdr; phdr = (pElf32_Phdr)(buf+ehdr->e_phoff+(i*ehdr->e_phentsize)); if(phdr->p_type != PT_LOAD) continue; -#if 0 - printk("%08x ", exe+phdr->p_vaddr-pv); - printk("p_offset:%d\n", phdr->p_offset); - - int j; - for(j=0; j<100;/*phdr->p_filesz*/ j++) - printk("%02x ", *((char *)(buf+phdr->p_offset)+j)); -#endif if(phdr->p_filesz != 0) { - memcpy((void*)(exe+phdr->p_vaddr-pv), - (void*)(buf+phdr->p_offset), - phdr->p_filesz); + memcpy((void*)(exe+phdr->p_offset), (void*)(buf+phdr->p_offset), phdr->p_filesz); } - - -#if 0 - u32 *pd =(u32*)pa2va(current->cr3); - u32 *pt; - u32 npd_min, npd_max, npt; - u32 pde, pte; - u32 vaddr = phdr->p_vaddr; - npd_min = get_npd(vaddr); - npd_max = get_npd(vaddr+phdr->p_memsz); - u32 npd; - - for(npd=npd_min; npd<=npd_max; npd++) - { - void *tmp = get_phys_pages(1); - pd[npd] = tmp | 7; - } -#endif } @@ -149,7 +132,7 @@ int sysc_exec(const char *path, char *const argv[]) } } - //printk("exe : %08x cr3:%08x\n", exe, pd); + printk("exe : %08x cr3:%08x\n", exe, pd); /* 准备内核栈的数据并从ret_from_fork返回 */ pt_regs_t * regs = ((pt_regs_t *)(TASK_SIZE+(unsigned long)current)) - 1; @@ -160,19 +143,16 @@ int sysc_exec(const char *path, char *const argv[]) regs->es = SELECTOR_USER_DS; regs->fs = SELECTOR_USER_DS; regs->gs = SELECTOR_USER_DS; - regs->esp = (KRNLADDR-4*sizeof(unsigned long)); + regs->esp = (KRNLADDR-4*sizeof(unsigned long)); regs->eflags = 0x200; - regs->cs = SELECTOR_USER_CS; - regs->eip = (unsigned long)ehdr->e_entry; + regs->cs = SELECTOR_USER_CS; + regs->eip = (unsigned long)ehdr->e_entry; current->esp = (unsigned long) regs; current->eip = (unsigned long)ret_from_fork_user; *((unsigned long *)regs->esp) = (unsigned long)ehdr->e_entry; - //kfree(buf); - - //printk("eip: %08x \n", regs->eip); + kfree(buf); - //load_cr3(current); return 0; } diff --git a/kernel/init.c b/kernel/init.c index 741c6a4..1ba0696 100644 --- a/kernel/init.c +++ b/kernel/init.c @@ -22,21 +22,42 @@ Desc gdt[NGDT]; char __initdata kernel_init_stack[KRNL_INIT_STACK_SIZE] __attribute__ ((__aligned__(PAGE_SIZE))); int debug_wait_queue_get(); + +void ring3() +{ + while(1) + { + systest(); + } +} +static char user_task_stack[PAGE_SIZE] __attribute__ ((__aligned__(PAGE_SIZE))); +void user_task_entry() +{ + printk("user_task_entry\n"); + asm("movl $0x23,%%eax; \ + movw %%ax,%%ds; \ + movw %%ax,%%es; \ + movw %%ax,%%fs; \ + movw %%ax,%%gs; \ + pushl $0x23; \ + pushl %%ebx; \ + pushl $0x282; \ + pushl $0x1B; \ + leal ring3,%%eax; \ + pushl %%eax; \ + iret;"::"b"(user_task_stack+PAGE_SIZE)); +} + void init_task_entry() { int cnt = 0; pid_t id = sysc_getpid(); - if(id == 1) - { - int r = sysc_exec("/bin/shell", 0); - } - while(1) { - printl(MPL_TASK_1+id-1, "task:%d [%08x] weight %d cnt %d", id, current, current->weight, cnt++); - int v = debug_wait_queue_get(); - printk("task:%d wait queue get %d\n", id, v); + printl(MPL_TASK_1, "task:%d [%08x] weight %d cnt %d", id, current, current->weight, cnt++); + //int v = debug_wait_queue_get(); + //printk("task:%d wait queue get %d\n", id, v); } } @@ -51,8 +72,8 @@ void kernel_task(void *entry) void root_task_entry() { - kernel_task(init_task_entry); //kernel_task(init_task_entry); + kernel_task(user_task_entry); int cnt = 0; while(1) diff --git a/mm/slub.c b/mm/slub.c index 39df055..560f290 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -15,7 +15,7 @@ list_head_t slub_caches = LIST_HEAD_INIT(slub_caches); #define SLUB_MIN_SHIFT 5 -#define SLUB_MAX_SHIFT 12 +#define SLUB_MAX_SHIFT 16 #define SLUB_INIT_CACHE_SIZE ((SLUB_MAX_SHIFT) - (SLUB_MIN_SHIFT)) #define KMALLOC_MIN_SIZE (1UL<<(SLUB_MIN_SHIFT)) #define KMALLOC_MIN_ALIGN (1UL<<(SLUB_MIN_SHIFT))