OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 8:26 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: General protection fault when try to switch to user mode
PostPosted: Sun May 28, 2023 7:54 am 
Offline

Joined: Sun May 28, 2023 7:22 am
Posts: 4
The os has this gdt:
Code:
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:
cli         
   push 0x23
   push rsp
   pushfq
   push 0x1B
   push _user
   iretq


But when I try it in bochs it says this:
Code:
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:
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?


Top
 Profile  
 
 Post subject: Re: General protection fault when try to switch to user mode
PostPosted: Wed May 31, 2023 11:58 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
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.


Top
 Profile  
 
 Post subject: Re: General protection fault when try to switch to user mode
PostPosted: Wed May 31, 2023 4:34 pm 
Offline
Member
Member

Joined: Thu Aug 25, 2022 3:54 pm
Posts: 27
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.


Top
 Profile  
 
 Post subject: Re: General protection fault when try to switch to user mode
PostPosted: Wed May 31, 2023 4:40 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
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.


Top
 Profile  
 
 Post subject: Re: General protection fault when try to switch to user mode
PostPosted: Thu Jun 01, 2023 5:42 am 
Offline

Joined: Sun May 28, 2023 7:22 am
Posts: 4
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.


Top
 Profile  
 
 Post subject: Re: General protection fault when try to switch to user mode
PostPosted: Thu Jun 01, 2023 11:10 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
Don't you have a page fault handler? You should figure out why it wasn't called.


Top
 Profile  
 
 Post subject: Re: General protection fault when try to switch to user mode
PostPosted: Thu Jun 01, 2023 11:17 am 
Offline

Joined: Sun May 28, 2023 7:22 am
Posts: 4
No, I don't have one


Top
 Profile  
 
 Post subject: Re: General protection fault when try to switch to user mode
PostPosted: Thu Jun 01, 2023 11:22 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
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.


Top
 Profile  
 
 Post subject: Re: General protection fault when try to switch to user mode
PostPosted: Thu Jun 01, 2023 12:36 pm 
Offline

Joined: Sun May 28, 2023 7:22 am
Posts: 4
Yes, you're right. I should have done that first!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], DotBot [Bot], Google [Bot], SemrushBot [Bot] and 53 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group