Interrupts doesnt work in GRUB[solved]

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
Klakap
Member
Member
Posts: 299
Joined: Sat Mar 10, 2018 10:16 am

Interrupts doesnt work in GRUB[solved]

Post by Klakap »

Good day!
When I'm into my kernel put GRUB, after the setting of the IDT table Virtualbox has get a severe error. Qemu also. While I GRUB did not, IDT and interrupts was working. Please how I can in GRUB do interrupts?
Last edited by Klakap on Mon Sep 17, 2018 11:11 am, edited 1 time in total.
MichaelPetch
Member
Member
Posts: 799
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: Interrupts doesnt work in GRUB

Post by MichaelPetch »

Without seeing your code it is hard to tell, but one common issue when booting GRUB is that people don't follow the GRUB documentation that suggests you can't rely on the GDTR being set properly. Try creating your own GDT to ensure you have a valid one. If you don't it is possible when you load the IDT and an interrupt eventually fires it tries to use a bad descriptor.

That is just a guess, but it could be something else, but without more information/code it is hard to tell.
Klakap
Member
Member
Posts: 299
Joined: Sat Mar 10, 2018 10:16 am

Re: Interrupts doesnt work in GRUB

Post by Klakap »

When I tried to set up the GDT, the linker gave me error:

Code: Select all

compile/kasm.o: In function `gdt_end':
kernel.asm:(.text+0x45): relocation truncated to fit: R_386_16 against `.text'
This is my code:

Code: Select all

gdt:

gdt_null:
   dq 0
gdt_code:
   dw 0FFFFh
   dw 0
db 0
db 10011010b
db 11001111b
db 0
gdt_data:
   dw 0FFFFh
   dw 0
db 0
db 10010010b
db 11001111b
db 0
gdt_end

gdt_desc:
   db gdt_end - gdt
   dw gdt
Please, what is wrong?
User avatar
Schol-R-LEA
Member
Member
Posts: 1925
Joined: Fri Oct 27, 2006 9:42 am
Location: Athens, GA, USA

Re: Interrupts doesnt work in GRUB

Post by Schol-R-LEA »

If you don't mind me asking, did you ever set up a public repo for your project, as I recommended some time back? If you have, you could post a link to the repo so we can review the code without you having to post large sections of it here.
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.
MichaelPetch
Member
Member
Posts: 799
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: Interrupts doesnt work in GRUB

Post by MichaelPetch »

Code: Select all

gdt_desc:
   db gdt_end - gdt
   dw gdt
should be:

Code: Select all

gdt_desc:
   dw gdt_end - gdt - 1
   dd gdt
The size is a word (not byte) and the address is a dword and not a word. The GDT size should also have 1 subtracted from it.
Klakap
Member
Member
Posts: 299
Joined: Sat Mar 10, 2018 10:16 am

Re: Interrupts doesnt work in GRUB

Post by Klakap »

Thank you, GDT is load correctly. But interrupts doesnt work :( .
User avatar
Ender
Posts: 24
Joined: Thu Jul 19, 2018 9:40 pm

Re: Interrupts doesnt work in GRUB

Post by Ender »

Klakap wrote:Thank you, GDT is load correctly. But interrupts doesnt work :( .
Did you attempt to set up an IDT as well?
Klakap
Member
Member
Posts: 299
Joined: Sat Mar 10, 2018 10:16 am

Re: Interrupts doesnt work in GRUB

Post by Klakap »

Yes, while I have not used grub(I worked in the QEMU -kernel) IDT worked well. My code is at https://github.com/Klaykap/LightningOS/issues/2.
MichaelPetch
Member
Member
Posts: 799
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: Interrupts doesnt work in GRUB

Post by MichaelPetch »

Klakap wrote:Yes, while I have not used grub(I worked in the QEMU -kernel) IDT worked well. My code is at https://github.com/Klaykap/LightningOS/issues/2.
I see some issues with random snippets of code, but not the code to your entire project.
Klakap
Member
Member
Posts: 299
Joined: Sat Mar 10, 2018 10:16 am

Re: Interrupts doesnt work in GRUB

Post by Klakap »

Oh, there is my code in assember https://github.com/Klaykap/LightningOS/issues/4
MichaelPetch
Member
Member
Posts: 799
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: Interrupts doesnt work in GRUB

Post by MichaelPetch »

You are probably new to github. You shouldn't be putting your files into issues. You should be uploading them into the code repository.Also helps if you provide any shells scripts / makefiles / linker scripts you are using as well to build your kernel.
User avatar
pvc
Member
Member
Posts: 201
Joined: Mon Jan 15, 2018 2:27 pm

Re: Interrupts doesnt work in GRUB

Post by pvc »

I am not sure if that's the main problem but I see that you're missing segment register setup after loading new GDT in start.
First:

Code: Select all

xor ax, ax
mov ds, ax
It makes DS register set to 0 which is invalid in protected mode (loading null segment selector).
Second:
You need to fix up CS register after loading new GDT. You do that by performing a far jump. Something like that:

Code: Select all

...
lgdt [gdt_desc]
jmp 0x0008:fix_cs ; 0x0008 is just an example but should work with your GDT
fix_cs:
...
Third: Just after fix_cs you should put new values in other segment registers

To summarize. Your start function should look more like this:

Code: Select all

start:
cli ;block interrupts
;we must load gdt
lgdt [gdt_desc]
jmp 0x0008:fix_cs
fix_cs:
mov ax, 0x0010
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
mov esp, stack_space	;set stack pointer
call kmain
jmp $	;halt the CPU
Using interrupts involves pushing and popping CS register on the stack. QEMU internal loader uses 0x08 for code and 0x10 for data segment selector values. While GRUB uses 0x10 for code and 0x18 for data (at least in versions I've dealt with). With that in mind, think what would happen after returning from ISR. At entry CPU pushed current CS value on the stack (which is 0x10 for GRUB). Then it did it's thing and tried to pop CS register (again 0x10 for GRUB). But in your new GDT 0x10 is DATA!! segment selector. And these can't be loaded into CS.
Klakap
Member
Member
Posts: 299
Joined: Sat Mar 10, 2018 10:16 am

Re: Interrupts doesnt work in GRUB

Post by Klakap »

Very thank for replies! When I set up gdt with your code, interrupts works!
Post Reply