Some problem in JOS lab3

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
zhongshu_gu
Posts: 6
Joined: Tue Jan 22, 2008 9:47 pm
Location: Beijing, China

Some problem in JOS lab3

Post by zhongshu_gu »

It is related to the creation of a environment (the alias of process in JOS). It need to load the static binary to the user memory and switch to run the environment from the kernel.
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);
}
every time I ran into the memcpy(), there will be error like

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
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Some problem in JOS lab3

Post by Combuster »

zhongshu_gu wrote:every time I ran into the memcpy(), there will be error like

Bochs is exiting with the following message:
[CPU0 ] exception(): 3rd (13) exception with no resolution
Bochs tells you far more than just that. Have a look at it, and at the very least post it here if we are to make a sensible diagnosis.

p.s. please use

Code: Select all

...
tags
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
zhongshu_gu
Posts: 6
Joined: Tue Jan 22, 2008 9:47 pm
Location: Beijing, China

Post by zhongshu_gu »

but where can I get more log from bochs? Can you please tell me as I am newbie to it. Thanks
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

Bochs gives a CPU dump directly above the line you posted.
User avatar
zhongshu_gu
Posts: 6
Joined: Tue Jan 22, 2008 9:47 pm
Location: Beijing, China

Post by zhongshu_gu »

CPU dump before the memcpy

Code: Select all

CPU#0
eax:0x00200000, ebx:0x00004000, ecx:0xf0117394, edx:0x00000000
ebp:0xf0116f94, esp:0xf0116f3c, esi:0x00004000, edi:0xf0117394
eip:0xf0102f24, eflags:0x00000086, inhibit_mask:0
cs:s=0x0008, dl=0x0000ffff, dh=0x00cf9a00, valid=1
ss:s=0x0010, dl=0x0000ffff, dh=0x00cf9300, valid=7
ds:s=0x0010, dl=0x0000ffff, dh=0x00cf9300, valid=7
es:s=0x0010, dl=0x0000ffff, dh=0x00cf9200, valid=1
fs:s=0x0023, dl=0x0000ffff, dh=0x00cff300, valid=1
gs:s=0x0023, dl=0x0000ffff, dh=0x00cff200, valid=1
ldtr:s=0x0000, dl=0x00000000, dh=0x00000000, valid=0
tr:s=0x0028, dl=0x37400068, dh=0xf0008917, valid=1
gdtr:base=0xf0117320, limit=0x2f
idtr:base=0xf0172f40, limit=0x7ff
dr0:0x00000000, dr1:0x00000000, dr2:0x00000000
dr3:0x00000000, dr6:0xffff0ff0, dr7:0x00000400
cr0:0x80050033, cr1:0x00000000, cr2:0x00000000
cr3:0x01fbc000, cr4:0x00000000
cpu dump after the exception occurs

Code: Select all

CPU#0
eax:0x00000010, ebx:0x00200000, ecx:0xf0118360, edx:0x00000000
ebp:0xf0116f20, esp:0xf0116f14, esi:0x00003995, edi:0x00200000
eip:0xf0104254, eflags:0x00010006, inhibit_mask:0
cs:s=0x0008, dl=0x0000ffff, dh=0x00cf9a00, valid=1
ss:s=0x0010, dl=0x0000ffff, dh=0x00cf9300, valid=7
ds:s=0x0010, dl=0x0000ffff, dh=0x00cf9300, valid=7
es:s=0x0010, dl=0x0000ffff, dh=0x00cf9200, valid=1
fs:s=0x0023, dl=0x0000ffff, dh=0x00cff300, valid=1
gs:s=0x0023, dl=0x0000ffff, dh=0x00cff200, valid=1
ldtr:s=0x0000, dl=0x00000000, dh=0x00000000, valid=0
tr:s=0x0028, dl=0x37400068, dh=0xf0008917, valid=1
gdtr:base=0xf0117320, limit=0x2f
idtr:base=0xf0172f40, limit=0x7ff
dr0:0x00000000, dr1:0x00000000, dr2:0x00000000
dr3:0x00000000, dr6:0xffff0ff0, dr7:0x00000400
cr0:0x80050033, cr1:0x00000000, cr2:0x00200000
cr3:0x01fbc000, cr4:0x00000000
The exception occurs at the time when the function is trying to do the copy like below
*d++=*s++
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

You get a pagefault on the 2MB mark. It probably means you didn't (properly) map in the memory needed:

Code: Select all

edi:0x00200000 
cr2:0x00200000
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

Well done combuster. I'm not grepping through all that unformatted dump - can a mod possibly stick code tags in?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

JamesM wrote:Well done combuster. I'm not grepping through all that unformatted dump - can a mod possibly stick code tags in?
In this case i can't see the source of the bug. If it is in this code, it allocates memory then writes the same bits of memory, so either it's something obscure that I missed (out of memory, perhaps) or the bug is not here. IMHO its time for the OP to put debugging skills to good use.

P.S. I know from experience that there are two common ways to triplefault: borking the IDT (which leads to a double GPF and usually some funky messages) and borking the pagetables (which leads to a double pagefault and a set CR2) since CR2 was set and different between the dumps I'm pretty sure that I know which one it was. :wink:
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply