context switching: getting page faults.

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
aki522
Posts: 5
Joined: Sun Apr 01, 2012 12:34 am

context switching: getting page faults.

Post 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.
Attachments
paging.h
(963 Bytes) Downloaded 34 times
paging.c
the init_paging func is referring to assembly code in .S file(not included).
(1.91 KiB) Downloaded 83 times
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: context switching: getting page faults.

Post by Combuster »

I only see code, where's your question?
"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
bubach
Member
Member
Posts: 1223
Joined: Sat Oct 23, 2004 11:00 pm
Location: Sweden
Contact:

Re: context switching: getting page faults.

Post by bubach »

opening thread.
seeing this.
Image
getting out.
quick.
"Simplicity is the ultimate sophistication."
http://bos.asmhackers.net/ - GitHub
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: context switching: getting page faults.

Post by bluemoon »

Narrow it down to which instruction, address and error code of the page fault, and tell us.
User avatar
iansjack
Member
Member
Posts: 4711
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: context switching: getting page faults.

Post 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.
Post Reply