uint32_t loader() { Elf32_Ehdr *elf; Elf32_Phdr *ph = NULL; uint8_t buf[4096]; #ifdef HAS_DEVICE ide_read(buf, ELF_OFFSET_IN_DISK, 4096); #else ramdisk_read(buf, ELF_OFFSET_IN_DISK, 4096); #endif elf = (void*)buf; /* TODO: fix the magic number with the correct one */ const uint32_t elf_magic = 0x7f454c46; uint32_t *p_magic = (void *)buf; nemu_assert(*p_magic == elf_magic); /* Load each program segment */ for(; true; ) { /* Scan the program header table, load each segment into memory */ if(ph->p_type == PT_LOAD) { //PT_LOAT=1, Loadable program segment /* TODO: read the content of the segment from the ELF file * to the memory region [VirtAddr, VirtAddr + FileSiz) */ /* TODO: zero the memory region * [VirtAddr + FileSiz, VirtAddr + MemSiz) */ #ifdef IA32_PAGE /* Record the program break for future use. */ extern uint32_t brk; uint32_t new_brk = ph->p_vaddr + ph->p_memsz - 1; if(brk < new_brk) { brk = new_brk; } #endif } } volatile uint32_t entry = elf->e_entry; #ifdef IA32_PAGE mm_malloc(KOFFSET - STACK_SIZE, STACK_SIZE); #ifdef HAS_DEVICE create_video_mapping(); #endif write_cr3(get_ucr3()); #endif return entry; }
uint32_t loader() { Elf32_Ehdr *elf; Elf32_Phdr *ph = NULL; uint8_t buf[4096]; #ifdef HAS_DEVICE ide_read(buf, ELF_OFFSET_IN_DISK, 4096); #else ramdisk_read(buf, ELF_OFFSET_IN_DISK, 4096); #endif elf = (void*)buf; /* fix the magic number with the correct one */ const uint32_t elf_magic = 0x464c457f; uint32_t *p_magic = (void *)buf; nemu_assert(*p_magic == elf_magic); /* Load each program segment */ int i; for(i=0;i<elf->e_phnum;i++ ) { /* Scan the program header table, load each segment into memory */ ph=(void *)(elf->e_phoff + i * elf->e_phentsize + buf); if(ph->p_type == PT_LOAD) { //Log("success"); #ifdef IA32_PAGE /* Record the program break for future use. */ extern uint32_t brk; uint32_t new_brk = ph->p_vaddr + ph->p_memsz - 1; if(brk < new_brk) { brk = new_brk; } uint32_t pa=mm_malloc(ph->p_vaddr,ph->p_memsz); #endif #ifndef HAS_DEVICE ramdisk_read((void*)pa, ELF_OFFSET_IN_DISK + ph->p_offset, ph->p_filesz); #else ide_read((void*)pa, ELF_OFFSET_IN_DISK + ph->p_offset, ph->p_filesz); #endif memset((void*)(pa + ph->p_filesz), 0, ph->p_memsz - ph->p_filesz); } } volatile uint32_t entry = elf->e_entry; #ifdef IA32_PAGE mm_malloc(KOFFSET - STACK_SIZE, STACK_SIZE); #ifdef HAS_DEVICE create_video_mapping(); #endif write_cr3(get_ucr3()); #endif return entry; }
uint32_t loader() { Elf32_Ehdr *elf; Elf32_Phdr *ph = NULL; uint8_t buf[4096]; #ifdef HAS_DEVICE ide_read(buf, ELF_OFFSET_IN_DISK, 4096); #else ramdisk_read(buf, ELF_OFFSET_IN_DISK, 4096); #endif elf = (void*)buf; /* DONE: fix the magic number with the correct one */ const uint32_t elf_magic = 0x464c457f; uint32_t *p_magic = (void *)buf; nemu_assert(*p_magic == elf_magic); /* Load each program segment */ //panic("please implement me"); ph = (Elf32_Phdr *)(buf + elf->e_phoff); uint16_t i = 0; for(i = 0; i < elf->e_phnum; ++i) { //if(elf->e_phnum == 3) {HIT_BAD_TRAP;} /* Scan the program header table, load each segment into memory */ if(ph->p_type == PT_LOAD) { uint32_t hwaddr = mm_malloc(ph->p_vaddr, ph->p_memsz); /* DONE: read the content of the segment from the ELF file to the memory region [VirtAddr, VirtAddr + FileSiz) */ ramdisk_read((uint8_t *)hwaddr, ph->p_offset, ph->p_filesz); /* DONE: zero the memory region [VirtAddr + FileSiz, VirtAddr + MemSiz) */ memset((void *)hwaddr + ph->p_filesz, 0, ph->p_memsz - ph->p_filesz); #ifdef IA32_PAGE /* Record the program break for future use. */ extern uint32_t brk; uint32_t new_brk = ph->p_vaddr + ph->p_memsz - 1; if(brk < new_brk) { brk = new_brk; } #endif } ph++; } volatile uint32_t entry = elf->e_entry; #ifdef IA32_PAGE mm_malloc(KOFFSET - STACK_SIZE, STACK_SIZE); #ifdef HAS_DEVICE create_video_mapping(); #endif write_cr3(get_ucr3()); #endif return entry; }
uint32_t loader() { Elf32_Ehdr *elf; Elf32_Phdr *ph = NULL; uint8_t buf[HEAD_SIZE]; uint8_t buf_[_SIZE_]; #ifdef HAS_DEVICE ide_read(buf, ELF_OFFSET_IN_DISK, HEAD_SIZE); #else ramdisk_read(buf, ELF_OFFSET_IN_DISK, HEAD_SIZE); #endif elf = (void*)buf; /* TODO: fix the magic number with the correct one */ const uint32_t elf_magic = 0x464c457f; uint32_t *p_magic = (void *)buf; nemu_assert(*p_magic == elf_magic); int cnt; /* Load each program segment */ for(cnt = 0; cnt < elf->e_phnum; ++ cnt) { /* Scan the program header table, load each segment into memory */ /* #ifdef HAS_DEVICE ide_read(buf_, ELF_OFFSET_IN_DISK + elf->e_ehsize + cnt * elf->e_phentsize, elf->e_phentsize); #else ramdisk_read(buf_, ELF_OFFSET_IN_DISK + elf->e_ehsize + cnt * elf->e_phentsize, elf->e_phentsize); #endif */ ph = (void*)(buf + elf->e_ehsize + cnt * elf->e_phentsize); // ph = (void*)buf_; if(ph->p_type == PT_LOAD) { /* TODO: read the content of the segment from the ELF file * to the memory region [VirtAddr, VirtAddr + FileSiz) */ #ifdef HAS_DEVICE ide_read(buf_, ELF_OFFSET_IN_DISK + ph->p_offset, ph->p_filesz); #else ramdisk_read(buf_, ELF_OFFSET_IN_DISK + ph->p_offset, ph->p_filesz); #endif uint32_t hwaddr = mm_malloc(ph->p_vaddr, ph->p_memsz); memcpy((void *)hwaddr, (void *)(buf_), ph->p_filesz); /* TODO: zero the memory region * [VirtAddr + FileSiz, VirtAddr + MemSiz) */ // Log("%x %x %x %x", ph->p_vaddr, ph->p_filesz, ph->p_memsz, ph->p_vaddr + ph->p_filesz); memset((void *)hwaddr + ph->p_filesz, 0, ph->p_memsz - ph->p_filesz); #ifdef IA32_PAGE /* Record the program break for future use. */ extern uint32_t brk; uint32_t new_brk = ph->p_vaddr + ph->p_memsz - 1; if(brk < new_brk) { brk = new_brk; } #endif } } volatile uint32_t entry = elf->e_entry; #ifdef IA32_PAGE mm_malloc(KOFFSET - STACK_SIZE, STACK_SIZE); #ifdef HAS_DEVICE create_video_mapping(); #endif write_cr3(get_ucr3()); #endif // HIT_GOOD_TRAP; return entry; }
uint32_t loader() { Elf32_Ehdr *elf; Elf32_Phdr *ph = NULL; uint8_t buf[4096]; #ifdef HAS_DEVICE ide_read(buf, ELF_OFFSET_IN_DISK, 4096); #else ramdisk_read(buf, ELF_OFFSET_IN_DISK, 4096); #endif elf = (void*)buf; int i; /* TODO: fix the magic number with the correct one */ const uint32_t elf_magic = 0x464c457f; uint32_t *p_magic = (void *)buf; nemu_assert(*p_magic == elf_magic); /* Load each program segment */ //panic("please implement me"); for(ph=(Elf32_Phdr*)(buf+elf->e_phoff),i=0;i<elf->e_phnum;i++ ) { /* Scan the program header table, load each segment into memory */ if(ph->p_type == PT_LOAD ) { /* TODO: read the content of the segment from the ELF file * to the memory region [VirtAddr, VirtAddr + FileSiz) */ uint32_t va=(uint32_t)(ph->p_vaddr); uint8_t* my_vaddr=(uint8_t*)mm_malloc(va,ph->p_memsz); ide_read(my_vaddr,ph->p_offset,ph->p_filesz); //void* rstart=(void*)0; //memcpy(my_vaddr,rstart+ph->p_offset,ph->p_filesz); /* TODO: zero the memory region * [VirtAddr + FileSiz, VirtAddr + MemSiz) */ my_vaddr+=ph->p_filesz; int j; for(j=0;j<ph->p_memsz-ph->p_filesz;j++) *(my_vaddr+j)=0; #ifdef IA32_PAGE /* Record the program break for future use. */ extern uint32_t brk; uint32_t new_brk = ph->p_vaddr + ph->p_memsz - 1; if(brk < new_brk) { brk = new_brk; } #endif ph++; } } uint32_t entry = elf->e_entry; #ifdef IA32_PAGE mm_malloc(KOFFSET - STACK_SIZE, STACK_SIZE); #ifdef HAS_DEVICE create_video_mapping(); #endif write_cr3(get_ucr3()); #endif return entry; }