Page 1 of 1

Triple fault when switching to User mode

Posted: Thu Sep 25, 2014 1:38 pm
by SlayterDev
Ok I've been beating on this for a while now and I have no idea whats going wrong. I'm trying to make the switch from kernel space to user land and I keep getting a triple fault. I have my gdt set up and the TSS but when I run the function to jump to user space it dies. Here is the code in question:

Code: Select all

enter_user_mode:
	mov ax, 0x23
	mov ds, ax
	mov es, ax
	mov fs, ax
	mov gs, ax

	push 0x23
	push esp
	pushfd
	push 0x1B
	lea eax, [a]
	push eax
	
	iretd
a:
	add esp, 4
Help me out. Am I missing something? Is there some small pre switch step I missed? Also, if I try to run a function from the kernel after the user mode switch, will that cause a triple fault? It may sound like a stupid question but from what I have been reading, I'm not sure.

Re: Triple fault when switching to User mode

Posted: Thu Sep 25, 2014 1:56 pm
by SpyderTL
Try running your OS in Bochs with the debugger enabled. It will tell you exactly what is wrong with your descriptors, and you can even step through this code line-by-line.

Re: Triple fault when switching to User mode

Posted: Thu Sep 25, 2014 2:00 pm
by b.zaar
Yeah do what SpyderTL says...

is a: the user space code?
It looks like you are trying to share the OS and user memory including the stack. Are they all mapped properly in the GDT?

Re: Triple fault when switching to User mode

Posted: Thu Sep 25, 2014 2:11 pm
by SlayterDev
Well I have mapped my kernel pages to be user accessible. And I have set up a user code gdt and user data gdt.

Re: Triple fault when switching to User mode

Posted: Thu Sep 25, 2014 4:37 pm
by b.zaar
Run the OS in bochs so you can see what's really going on.

Make the user task a simple jmp $ so you are not using the ss selector or other memory outside the cs selector. This will narrow it down a little between jumping to user space and accessing stack memory from user space.

The Bochs log will also tell you if it's a descriptor error or a page fault.

* Side note - You should probably have some basic exception handlers before jumping to user space. Even if it's just to dump the register values to screen and halt the CPU.

Re: Triple fault when switching to User mode

Posted: Fri Sep 26, 2014 9:49 am
by SlayterDev
Well I believe I solved the initial issue and am now in user mode. But now when I try to run system calls, specifically "int 0x80", I get an invalid opcode fault. I have no idea where to begin with this. I have interrupt 128 in my idt and its set for ring 3.

Re: Triple fault when switching to User mode

Posted: Fri Sep 26, 2014 2:23 pm
by b.zaar
No more help until you post a Bochs log...

Re: Triple fault when switching to User mode

Posted: Fri Sep 26, 2014 2:43 pm
by SpyderTL
SlayterDev wrote:Well I believe I solved the initial issue and am now in user mode. But now when I try to run system calls, specifically "int 0x80", I get an invalid opcode fault. I have no idea where to begin with this. I have interrupt 128 in my idt and its set for ring 3.
You would probably get a General Protection Fault if your IDT was wrong, so you probably are ending up at the wrong address. You should be able to get the address of the invalid opcode from the exception handler. Make sure it is running the code that you think it's running.

Or run it in Bochs and step through it, line-by-line. (Or SimNOW, if you just don't want to use Bochs for some reason.)