Page 1 of 1

context switching: getting page faults.

Posted: Sat Apr 14, 2012 3:53 pm
by aki522
Hi
I am learning to write the basic OS and right now I am experimenting with the tasks.
I wrote the code for context switching for tasks but I am getting page faults. I do not know why though.
This is how the physical mem is set up: kernel 4 MB to 8MB. User process: 8MB to 12 MB.
Kernel is identity mapped and the user process is at virtual addr 128 MB.


I wrote a simple test program to execute the context switch:

Code: Select all

void task()
{
	int entry_file=0;
	int a;
	char *buf_1= (char *)0x8000000; //set the address of the buffer that has the binary file data to virt add 128 MB
	a=file_read(buf_1,30);//read the 30 bytes of file data into buffer
	entry_file=0x8048234; /entry point into ELF file
	/*tss.esp0 is already set to kernel stack 8MB in the main func before the context switch*/
	context_switch(entry_file, 0xC00000); //0xC00000=12 MB is the user stack base ptr addr
}

Code: Select all

context_switch:
	cli
	# Save esp so we can continue accessing arguments
	movl    %esp, %ebp
	# Set up segment registers
	movw    $USER_DS, %ax
	movw    %ax, %ds
	movw    %ax, %es
	movw    %ax, %fs
	movw    %ax, %gs
	# Push the interrupt context onto the stack: ss, esp, eflags, cs, eip
	pushl	$USER_DS                # Stack Segment
	movl	8(%ebp), %eax           # Stack Pointer
	pushl   %eax
	pushf                           # eflags
	# Unmask interrupts by modifying eflags
	orl     $0x200, (%esp)
	pushl   $USER_CS                # Code Segment
	movl    4(%ebp), %eax           # Instruction Pointer
	pushl   %eax
	# "return" to user space
	iret
In the attachments, I have the paging files I am using.

Re: context switching: getting page faults.

Posted: Sat Apr 14, 2012 3:58 pm
by Combuster
I only see code, where's your question?

Re: context switching: getting page faults.

Posted: Sat Apr 14, 2012 4:07 pm
by bubach
opening thread.
seeing this.
Image
getting out.
quick.

Re: context switching: getting page faults.

Posted: Sat Apr 14, 2012 4:39 pm
by bluemoon
Narrow it down to which instruction, address and error code of the page fault, and tell us.

Re: context switching: getting page faults.

Posted: Sun Apr 15, 2012 11:52 am
by iansjack
If you want to write your own OS you are going to get a lot of Page Faults and GPFs before you get a half-stable system. You really need to track these down for yourself if you want to get anywhere.

I would recommend that you single step through the code at the machine-code level until you get the fault, then examine the registers, the faulting address and look at the virtual memory to see what is happenning. It should be fairly obvious, and you'll learn a lot about your code this way.

Can I recommend the use of SimNow for this sort of low-level debugging; it's a bit slow as an emulator, but has excellent debugging facilities.