Page 1 of 1

General protection fault when try to switch to user mode

Posted: Sun May 28, 2023 7:54 am
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?

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

Posted: Wed May 31, 2023 11:58 am
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.

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

Posted: Wed May 31, 2023 4:34 pm
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.

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

Posted: Wed May 31, 2023 4:40 pm
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.

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

Posted: Thu Jun 01, 2023 5:42 am
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.

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

Posted: Thu Jun 01, 2023 11:10 am
by Octocontrabass
Don't you have a page fault handler? You should figure out why it wasn't called.

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

Posted: Thu Jun 01, 2023 11:17 am
by MindW1n
No, I don't have one

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

Posted: Thu Jun 01, 2023 11:22 am
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.

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

Posted: Thu Jun 01, 2023 12:36 pm
by MindW1n
Yes, you're right. I should have done that first!