General protection fault when try to switch 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
MindW1n
Posts: 4
Joined: Sun May 28, 2023 7:22 am

General protection fault when try to switch to user mode

Post by MindW1n »

The os has this gdt:

Code: Select all

GDTLM:
	.Null: equ $ - GDTLM
		dq 0
	
	.Code: equ $ - GDTLM
		dw 0xffff
		dw 0
		db 0
		db 10011010b
		db 10101111b
		db 0
	
	.Data: equ $ - GDTLM
		dw 0xffff
		dw 0
		db 0
		db 10010010b
		db 11001111b
		db 0

	.UserCode: equ $ - GDTLM
		dw 0xffff
		dw 0
		db 0
		db 11111010b
		db 10101111b
		db 0

	.UserData: equ $ - GDTLM
		dw 0xffff
		dw 0
		db 0
		db 11110010b
		db 10101111b
		db 0
	
	.Pointer:
		dw $ - GDTLM - 1
		dq GDTLM
And this code is used for switching to user mode:

Code: Select all

cli			
	push 0x23
	push rsp
	pushfq
	push 0x1B
	push _user
	iretq
But when I try it in bochs it says this:

Code: Select all

00392068645e[CPU0  ] check_cs(0x5045): not a valid code segment !
00392068648e[CPU0  ] check_cs(0x5534): not a valid code segment !
00392068651e[CPU0  ] check_cs(0x5534): not a valid code segment !
00392068654e[CPU0  ] check_cs(0x5534): not a valid code segment !
This repeats many times and then bochs says:

Code: Select all

00392068918e[CPU0  ] interrupt(long mode): not accessible or not code segment
00392068918e[CPU0  ] interrupt(long mode): not accessible or not code segment
Then it just restarts the whole system.
I've already asked this question on stackoverflow, but no one helped.
What do I do to make it work?
Octocontrabass
Member
Member
Posts: 5560
Joined: Mon Mar 25, 2013 7:01 pm

Re: General protection fault when try to switch to user mode

Post by Octocontrabass »

MindW1n wrote:But when I try it in bochs it says this:
I don't see anything in the code you've shared so far that could cause that error. Step through your code using the Bochs debugger and see if you can figure out where the error is happening. I suggest placing a magic breakpoint right before the IRETQ instruction.
SomeGuyWithAKeyboard
Member
Member
Posts: 27
Joined: Thu Aug 25, 2022 3:54 pm

Re: General protection fault when try to switch to user mode

Post by SomeGuyWithAKeyboard »

I'm going to take a stab in the dark and say that based on the issues I keep having, try changing bit 6 of the the flags/20-16 limit byte of your code segment from 0 to 1. If that bit is 0, it means its a 16 bit segment and if that bit is a 1, it means its a 32 bit segment. When that bit is zero, my system which runs on c++ compiled code doesnt boot even on qemu or bochs.
Octocontrabass
Member
Member
Posts: 5560
Joined: Mon Mar 25, 2013 7:01 pm

Re: General protection fault when try to switch to user mode

Post by Octocontrabass »

SomeGuyWithAKeyboard wrote:I'm going to take a stab in the dark and say that based on the issues I keep having, try changing bit 6 of the the flags/20-16 limit byte of your code segment from 0 to 1. If that bit is 0, it means its a 16 bit segment and if that bit is a 1, it means its a 32 bit segment.
It's a 64-bit code segment. Bit 6 must be 0 for a 64-bit code segment.
MindW1n
Posts: 4
Joined: Sun May 28, 2023 7:22 am

Re: General protection fault when try to switch to user mode

Post by MindW1n »

Solved! It's wasn't the general protection fault. It was the page fault, I only needed to allocate some page for user code and that solved the problem.
Octocontrabass
Member
Member
Posts: 5560
Joined: Mon Mar 25, 2013 7:01 pm

Re: General protection fault when try to switch to user mode

Post by Octocontrabass »

Don't you have a page fault handler? You should figure out why it wasn't called.
MindW1n
Posts: 4
Joined: Sun May 28, 2023 7:22 am

Re: General protection fault when try to switch to user mode

Post by MindW1n »

No, I don't have one
Octocontrabass
Member
Member
Posts: 5560
Joined: Mon Mar 25, 2013 7:01 pm

Re: General protection fault when try to switch to user mode

Post by Octocontrabass »

Why don't you have a page fault handler? Exception handlers should be one of the first things you set up, so you don't waste time trying to figure out where exceptions occur.
MindW1n
Posts: 4
Joined: Sun May 28, 2023 7:22 am

Re: General protection fault when try to switch to user mode

Post by MindW1n »

Yes, you're right. I should have done that first!
Post Reply