general protection fault on iretq

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
antoni
Member
Member
Posts: 61
Joined: Sun May 24, 2020 9:11 am
Location: /dev/null

general protection fault on iretq

Post by antoni »

I'm receiving general protection fault with error code 24 on iretq instruction. Error code isn't equal to zero so exception is segment related. This error code means fifth descriptor in GDT, but my GDT has only three descriptors: null, code and date.

Why do I have this error and how can I fix it?
Octocontrabass
Member
Member
Posts: 5574
Joined: Mon Mar 25, 2013 7:01 pm

Re: general protection fault on iretq

Post by Octocontrabass »

Why did you make a new thread for this?

What's on the stack when IRETQ faults?
nexos
Member
Member
Posts: 1081
Joined: Tue Feb 18, 2020 3:29 pm
Libera.chat IRC: nexos

Re: general protection fault on iretq

Post by nexos »

Although I can't verify, I would assume that everything that gets pushed on the stack isn't getting popped off. Iretq is then loading bogus values in the registers. This might solve your problem.
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg
antoni
Member
Member
Posts: 61
Joined: Sun May 24, 2020 9:11 am
Location: /dev/null

Re: general protection fault on iretq

Post by antoni »

Why did you make a new thread for this?
In the previous topic I asked about valid IDT descriptor, but it turned out that the problem is completely unrelated to this so I created new topic for the real problem I'm facing.
What's on the stack when IRETQ faults?
Here you have:

Code: Select all

0x000000000000fff0 in ?? ()
(gdb) b kernel_start
Breakpoint 1 at 0x1255e0: file kernel.c, line 7.
(gdb) b irq1
Breakpoint 2 at 0x125830
(gdb) b exc13
Breakpoint 3 at 0x125ea4
(gdb) c
Continuing.

Breakpoint 1, kernel_start (mbd=0x132c4c, magic=732803074) at kernel.c:7
7		vga_init();
(gdb) x/100xd $sp
0x12b018:	1048760	0	0	0
0x12b028 <vga_mem>:	0	0	0	0
0x12b038:	0	0	0	0
0x12b048 <buf_p1>:	0	0	0	0
0x12b058:	0	0	0	0
0x12b068 <kbd_buffer+8>:	0	0	0	0
0x12b078 <kbd_buffer+24>:	0	0	0	0
0x12b088 <kbd_buffer+40>:	0	0	0	0
0x12b098 <kbd_buffer+56>:	0	0	0	0
0x12b0a8 <idt+8>:	0	0	0	0
0x12b0b8 <idt+24>:	0	0	0	0
0x12b0c8 <idt+40>:	0	0	0	0
0x12b0d8 <idt+56>:	0	0	0	0
0x12b0e8 <idt+72>:	0	0	0	0
0x12b0f8 <idt+88>:	0	0	0	0
0x12b108 <idt+104>:	0	0	0	0
0x12b118 <idt+120>:	0	0	0	0
0x12b128 <idt+136>:	0	0	0	0
0x12b138 <idt+152>:	0	0	0	0
0x12b148 <idt+168>:	0	0	0	0
0x12b158 <idt+184>:	0	0	0	0
0x12b168 <idt+200>:	0	0	0	0
0x12b178 <idt+216>:	0	0	0	0
0x12b188 <idt+232>:	0	0	0	0
0x12b198 <idt+248>:	0	0	0	0
(gdb) c
Continuing.

Breakpoint 2, 0x0000000000125830 in irq1 ()
(gdb) x/100xd $sp
0x12afb8:	1202424	0	8	0
0x12afc8:	582	0	1224680	0
0x12afd8:	24	0	0	0
0x12afe8:	1202136	0	1256524	0
0x12aff8:	0	0	0	0
0x12b008:	0	0	0	0
0x12b018:	1048760	0	720	0
0x12b028 <vga_mem>:	753664	0	0	0
0x12b038:	0	0	0	0
0x12b048 <buf_p1>:	0	0	0	0
0x12b058:	0	0	0	0
0x12b068 <kbd_buffer+8>:	0	0	0	0
0x12b078 <kbd_buffer+24>:	0	0	0	0
0x12b088 <kbd_buffer+40>:	0	0	0	0
0x12b098 <kbd_buffer+56>:	0	0	548263	1216000
0x12b0a8 <idt+8>:	0	0	548288	1216000
0x12b0b8 <idt+24>:	0	0	548313	1216000
0x12b0c8 <idt+40>:	0	0	0	0
0x12b0d8 <idt+56>:	0	0	0	0
0x12b0e8 <idt+72>:	0	0	0	0
0x12b0f8 <idt+88>:	0	0	548338	1216000
0x12b108 <idt+104>:	0	0	0	0
0x12b118 <idt+120>:	0	0	548363	1216000
0x12b128 <idt+136>:	0	0	0	0
0x12b138 <idt+152>:	0	0	0	0
(gdb) c
Continuing.

Breakpoint 3, 0x0000000000125ea4 in exc13 ()
(gdb) x/100xd $sp
0x12aef0:	24	0	1202257	0
0x12af00:	8	0	147	0
0x12af10:	1224488	0	24	0
0x12af20:	1202257	0	24	0
0x12af30:	24	0	0	0
0x12af40:	1202400	0	1205156	0
0x12af50:	1200832	0	1224744	0
0x12af60:	1208320	0	1224736	0
0x12af70:	1200400	0	1224576	0
0x12af80:	1201184	0	980	0
0x12af90:	1224772	0	0	0
0x12afa0:	1224776	0	1201584	0
0x12afb0:	0	0	1202424	0
0x12afc0:	8	0	582	0
0x12afd0:	1224680	0	24	0
0x12afe0:	0	0	1202136	0
0x12aff0:	1256524	0	0	0
0x12b000:	0	0	0	0
0x12b010:	0	0	1048760	0
0x12b020 <cursor>:	720	0	753664	0
0x12b030:	0	0	0	0
0x12b040 <kbd_status.1178>:	0	1	0	0
0x12b050:	0	0	0	0
0x12b060 <kbd_buffer>:	10	0	0	0
0x12b070 <kbd_buffer+16>:	0	0	0	0
(gdb) q
Octocontrabass
Member
Member
Posts: 5574
Joined: Mon Mar 25, 2013 7:01 pm

Re: general protection fault on iretq

Post by Octocontrabass »

Code: Select all

0x12afb8:	1202424	0	8	0
0x12afc8:	582	0	1224680	0
0x12afd8:	24	0	0	0
Let's rearrange that a bit, and add some notes to see where each value will go.

Code: Select all

0x12afb8: 0x00000000001258f8 -> RIP
0x12afc0: 0x0000000000000008 -> CS
0x12afc8: 0x0000000000000246 -> RFLAGS
0x12afd0: 0x000000000012afe8 -> RSP
0x12afd8: 0x0000000000000018 -> SS
Much better. Now, it looks like IRETQ is popping an invalid selector into SS. Of course, that means there was an invalid selector in SS when the interrupt occurred. Why was there an invalid selector in SS?
antoni
Member
Member
Posts: 61
Joined: Sun May 24, 2020 9:11 am
Location: /dev/null

Re: general protection fault on iretq

Post by antoni »

Why was there an invalid selector in SS?
It was so from beginning. Since GRUB called my kernel. I didn't even touch this register.
antoni
Member
Member
Posts: 61
Joined: Sun May 24, 2020 9:11 am
Location: /dev/null

Re: general protection fault on iretq

Post by antoni »

Setting ds and ss to 16 solved the problem. I didn't know they had to be set. This wasn't described in guide I followed when writing my first OS. Do I also need to set other segment registers (gs, fs etc.)?
Post Reply