Triple fault when switching to User mode

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
SlayterDev
Member
Member
Posts: 25
Joined: Wed Aug 13, 2014 4:22 pm

Triple fault when switching to User mode

Post 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.
User avatar
SpyderTL
Member
Member
Posts: 1074
Joined: Sun Sep 19, 2010 10:05 pm

Re: Triple fault when switching to User mode

Post 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.
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
User avatar
b.zaar
Member
Member
Posts: 294
Joined: Wed May 21, 2008 4:33 am
Location: Mars MTC +6:00
Contact:

Re: Triple fault when switching to User mode

Post 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?
"God! Not Unix" - Richard Stallman

Website: venom Dev
OS project: venom OS
Hexadecimal Editor: hexed
SlayterDev
Member
Member
Posts: 25
Joined: Wed Aug 13, 2014 4:22 pm

Re: Triple fault when switching to User mode

Post 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.
User avatar
b.zaar
Member
Member
Posts: 294
Joined: Wed May 21, 2008 4:33 am
Location: Mars MTC +6:00
Contact:

Re: Triple fault when switching to User mode

Post 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.
"God! Not Unix" - Richard Stallman

Website: venom Dev
OS project: venom OS
Hexadecimal Editor: hexed
SlayterDev
Member
Member
Posts: 25
Joined: Wed Aug 13, 2014 4:22 pm

Re: Triple fault when switching to User mode

Post 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.
User avatar
b.zaar
Member
Member
Posts: 294
Joined: Wed May 21, 2008 4:33 am
Location: Mars MTC +6:00
Contact:

Re: Triple fault when switching to User mode

Post by b.zaar »

No more help until you post a Bochs log...
"God! Not Unix" - Richard Stallman

Website: venom Dev
OS project: venom OS
Hexadecimal Editor: hexed
User avatar
SpyderTL
Member
Member
Posts: 1074
Joined: Sun Sep 19, 2010 10:05 pm

Re: Triple fault when switching to User mode

Post 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.)
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
Post Reply