0

I'm trying to write a simple OS for 80386. When I was writing interrupt handler, I encounter interrupt 13 over and over again. I mean the program enters interrupt handler with number 13 after finishing last interrupt handler.

I read about 80386 manual and know interrupt 13 is General Protection Exception. But I don't know what exactly causes this exception or how to response to this exception.

I have tried to do nothing in interrupt handler for interrupt 13, but the program throw this exception over and over again.

Status of registers:

edi: 0x1000 4096
esi: 0x0 0
ebp: 0x105ed8 1072856
oesp: 0x105ea0 1072800
ebx: 0x0 0
edx: 0x3f8 1016
ecx: 0x105f24 1072932
eax: 0x35 53
-------------------
gs: 0x10 16
fs: 0x10 16
es: 0x10 16
ds: 0x10 16
trapno: 0xd 13
-------------------
err: 0x210 528
eip: 0x1022db 1057499
cs: 0x8 8
eflags: 0x12 18 0b 1 0010
-------------------
esp: 0x8 8
ss: 0x212 530
----------------

Here is my code: https://github.com/stskyblade/StarOS/blob/7dcfabc74c208fffa1f8eda3da5aa68a1b7c1bca/src/interrupt.cpp#L78

stskyblade
  • 19
  • 4
  • If the error code is right the descriptor with selector 4 is at fault. I don't feel like reviewing your code, but are you overwriting every descriptor in the GDT with a code descriptor? – Margaret Bloom Aug 12 '22 at 16:43
  • Yes, I overwrite every descriptor in the GDT with the same segment descriptor. Is this not right? And why? I have only used selector 1 in IDT. I have no idea what will use selector 4. The error code is 0x210, which is 0b 0010 0001 0000. How do you get selector 4 from the error code? I have read section 9.7 Error Code of that manual, but can't fully understand it. – stskyblade Aug 13 '22 at 01:14
  • Sorry, 0x210 >> 3 = 0x42. I must have missed the trailing 0. I don't understand why you are filling the GDT with a single code descriptor. You at least need a writable data segment for the stack and descriptors can be reused anyway. Plus, your C code is running with a temporary GDT? If you change the GDT you need to reload the selector registers if you want the changes to apply. – Margaret Bloom Aug 13 '22 at 05:05
  • I have read this link: https://wiki.osdev.org/GDT_Tutorial#What_to_Put_In_a_GDT . It describes what I should put in a GDT. But even this doesn't contain anything about 0x42. I will try it later. Actually I was trying to write an interrupt handler. According to 80386 manual, I need an IDT. And the selector in a interrupt descriptor points to a GDT entry. In my code, all interrupt descriptors have same selector, 1 << 3. So I think all the entries in GDT except selector 1 should be unused. And why do I encounter Interrupt 13 with selector 0x42? Does 80386 use GDT in an implictly way? – stskyblade Aug 13 '22 at 07:03
  • 1
    0x42 may be garbage. I didn't read all your code, it is just a red flag that you overwrite every descriptor with a code descriptor. To solve this go slow: remove all the interrupt related code, write it anew. Create a single entry in the IDT, that call a function that loop forever. Test it with `int xx`. If the machine freeze, it is working, if it reboots, something is wrong with your IDT setup (check your structures, offsets, packing and so on). investigate that. When done, let the ISR return, test it with multiple `int xx` and `print`. ... – Margaret Bloom Aug 13 '22 at 07:46
  • ... If it works, fill all other IDT entries as you need. Test them with different `int xx/yy` instructions. If you got here, your IDT is working (for infra-privilege calls). – Margaret Bloom Aug 13 '22 at 07:46
  • I truly appreciate it. – stskyblade Aug 13 '22 at 08:03
  • 1
    After I configed GDT like this link: https://wiki.osdev.org/GDT_Tutorial, I discovered that this interrupt 13 is caused by an IRET instruction in previous interrupt 8. According to the manual, an error code is supposed to be pushed into stack by CPU when interrupt number is 8, but it doesn't happen. So my code messed up. – stskyblade Aug 14 '22 at 02:33
  • This link is helpful: https://stackoverflow.com/questions/10581224/why-does-iret-from-a-page-fault-handler-generate-interrupt-13-general-protectio – stskyblade Aug 14 '22 at 02:35

0 Answers0