The code of loading the binary is like below
Code: Select all
static void
load_icode(struct Env *e, uint8_t *binary, size_t size)
{
// Hints:
// Load each program segment into virtual memory
// at the address specified in the ELF section header.
// You should only load segments with ph->p_type ==
ELF_PROG_LOAD.
// Each segment's virtual address can be found in ph->p_va
// and its size in memory can be found in ph->p_memsz.
// The ph->p_filesz bytes from the ELF binary, starting at
// 'binary + ph->p_offset', should be copied to virtual address
// ph->p_va. Any remaining memory bytes should be cleared to zero.
// (The ELF header should have ph->p_filesz <= ph->p_memsz.)
// Use functions from the previous lab to allocate and map pages.
//
// All page protection bits should be user read/write for now.
// ELF segments are not necessarily page-aligned, but you can
// assume for this function that no two segments will touch
// the same virtual page.
//
// You may find a function like segment_alloc useful.
//
// Loading the segments is much simpler if you can move data
// directly into the virtual addresses stored in the ELF binary.
// So which page directory should be in force during
// this function?
//
// Hint:
// You must also do something with the program's entry point,
// to make sure that the environment starts executing there.
// What? (See env_run() and env_pop_tf() below.)
// LAB 3: Your code here.
cprintf("Begin to load icode\n");
struct Proghdr *ph, *eph;
ph = (struct Proghdr *) (binary + ((struct Elf *)binary)->e_phoff);
eph = ph + ((struct Elf *)binary)->e_phnum;
for (; ph < eph; ph++)
{
debug("ph = %08x, eph = %08x,type=%08x\n",ph,eph,ph->p_type);
if(ph->p_type == ELF_PROG_LOAD)
{
segment_alloc(e, (void*)ph->p_va,ph->p_memsz);
memcpy((void *)ph->p_va, binary + ph->p_offset, ph->p_filesz);
debug("memmove success\n");
memset((void *)(ph->p_va + ph->p_filesz), 0x0, ph->p_memsz - ph->p_filesz);
debug("memset success\n");
}
}
//lcr3(boot_cr3);
cprintf("segment copy success\n");
// Now map one page for the program's initial stack
// at virtual address USTACKTOP - PGSIZE.
// LAB 3: Your code here.
struct Page * user_stack;
if(page_alloc(&user_stack) == -E_NO_MEM)
panic("No memory to alloc for user stack");
page_insert(e->env_pgdir, user_stack, (void *)(USTACKTOP - PGSIZE),PTE_W|PTE_U|PTE_P);
}
Bochs is exiting with the following message:
[CPU0 ] exception(): 3rd (13) exception with no resolution
occurs, I guess it is the problem of writing illegal memory in memcpy.
Can you give me some suggestions. Thanks
EDIT: code tags added as per popular request -- Combuster