OSDev.org
https://forum.osdev.org/

global array probably overwrites idt
https://forum.osdev.org/viewtopic.php?f=1&t=56379
Page 1 of 1

Author:  lowLevelGod [ Sat Jul 23, 2022 3:01 pm ]
Post subject:  global array probably overwrites idt

Hello! I'm a beginner os dev and I am developing a 32 bit os with a custom bios bootloader.
I set up the GDT and the IDT and i successfully implemented keyboard support so far.
I now want to enable paging, so I started by getting the memory map using "INT 0x15, EAX = 0xE820" as suggested by the wiki and I wanted to store the result in a bitmap(usable/unusable RAM), so i can continue creating the page frame allocator. I have a 32768 uin32_t bitmap that is represented as a global array in kernel code. Unfortunately, when i want to go through it (using a for from 0 to 32768 for example), I noticed that it overwrites memory I didn't want to and I think it overwrites even the IDT, as shown below by BOCHS.
The kernel is loaded at 0x1000.

Code:
(0) [0x000000001f70] 0008:0000000000001f70 (unk. ctxt): mov dword ptr ds:[eax*4+15776], 0x08080808 ; c70485a03d000008080808


The bitmap is stored at 15776(I found out by trying to debug my code).
Code:
<bochs:8> info idt
Interrupt Descriptor Table (base=0x0000000000003480, limit=2047):
IDT[0x00]=32-Bit Interrupt Gate target=0x0008:0x000015ba, DPL=0
IDT[0x01]=32-Bit Interrupt Gate target=0x0008:0x000015c1, DPL=0
IDT[0x02]=32-Bit Interrupt Gate target=0x0008:0x000015c8, DPL=0
IDT[0x03]=32-Bit Interrupt Gate target=0x0008:0x000015cf, DPL=0
IDT[0x04]=32-Bit Interrupt Gate target=0x0008:0x000015d6, DPL=0
IDT[0x05]=32-Bit Interrupt Gate target=0x0008:0x000015dd, DPL=0
IDT[0x06]=32-Bit Interrupt Gate target=0x0008:0x000015e4, DPL=0
IDT[0x07]=32-Bit Interrupt Gate target=0x0008:0x000015eb, DPL=0
IDT[0x08]=32-Bit Interrupt Gate target=0x0008:0x000015f5, DPL=0
IDT[0x09]=32-Bit Interrupt Gate target=0x0008:0x000015fd, DPL=0
IDT[0x0a]=32-Bit Interrupt Gate target=0x0008:0x00001607, DPL=0
IDT[0x0b]=32-Bit Interrupt Gate target=0x0008:0x0000160f, DPL=0
IDT[0x0c]=32-Bit Interrupt Gate target=0x0008:0x00001617, DPL=0
IDT[0x0d]=32-Bit Interrupt Gate target=0x0008:0x0000161f, DPL=0
IDT[0x0e]=32-Bit Interrupt Gate target=0x0008:0x00001627, DPL=0
IDT[0x0f]=32-Bit Interrupt Gate target=0x0008:0x0000162f, DPL=0
IDT[0x10]=32-Bit Interrupt Gate target=0x0008:0x00001639, DPL=0
IDT[0x11]=32-Bit Interrupt Gate target=0x0008:0x00001643, DPL=0
IDT[0x12]=32-Bit Interrupt Gate target=0x0008:0x0000164d, DPL=0
IDT[0x13]=32-Bit Interrupt Gate target=0x0008:0x00001657, DPL=0
IDT[0x14]=32-Bit Interrupt Gate target=0x0008:0x00001661, DPL=0
IDT[0x15]=32-Bit Interrupt Gate target=0x0008:0x0000166b, DPL=0
IDT[0x16]=32-Bit Interrupt Gate target=0x0008:0x00001675, DPL=0
IDT[0x17]=32-Bit Interrupt Gate target=0x0008:0x0000167f, DPL=0
IDT[0x18]=32-Bit Interrupt Gate target=0x0008:0x00001689, DPL=0
IDT[0x19]=32-Bit Interrupt Gate target=0x0008:0x00001693, DPL=0
IDT[0x1a]=32-Bit Interrupt Gate target=0x0008:0x0000169d, DPL=0
IDT[0x1b]=32-Bit Interrupt Gate target=0x0008:0x000016a7, DPL=0
IDT[0x1c]=32-Bit Interrupt Gate target=0x0008:0x000016b1, DPL=0
IDT[0x1d]=32-Bit Interrupt Gate target=0x0008:0x000016bb, DPL=0
IDT[0x1e]=32-Bit Interrupt Gate target=0x0008:0x000016c5, DPL=0
IDT[0x1f]=32-Bit Interrupt Gate target=0x0008:0x000016cf, DPL=0
IDT[0x20]=32-Bit Interrupt Gate target=0x0008:0x000016d9, DPL=0
IDT[0x21]=32-Bit Interrupt Gate target=0x0008:0x000016e3, DPL=0
IDT[0x22]=32-Bit Interrupt Gate target=0x0008:0x000016ed, DPL=0
IDT[0x23]=32-Bit Interrupt Gate target=0x0008:0x000016f7, DPL=0
IDT[0x24]=32-Bit Interrupt Gate target=0x0008:0x00001701, DPL=0
IDT[0x25]=32-Bit Interrupt Gate target=0x0008:0x0000170b, DPL=0
IDT[0x26]=32-Bit Interrupt Gate target=0x0008:0x00001715, DPL=0
IDT[0x27]=32-Bit Interrupt Gate target=0x0008:0x0000171f, DPL=0
IDT[0x28]=32-Bit Interrupt Gate target=0x0008:0x00001729, DPL=0
IDT[0x29]=32-Bit Interrupt Gate target=0x0008:0x00001733, DPL=0
IDT[0x2a]=32-Bit Interrupt Gate target=0x0008:0x0000173d, DPL=0
IDT[0x2b]=32-Bit Interrupt Gate target=0x0008:0x00001747, DPL=0
IDT[0x2c]=32-Bit Interrupt Gate target=0x0008:0x00001751, DPL=0
IDT[0x2d]=32-Bit Interrupt Gate target=0x0008:0x0000175b, DPL=0
IDT[0x2e]=32-Bit Interrupt Gate target=0x0008:0x00001765, DPL=0
IDT[0x2f]=32-Bit Interrupt Gate target=0x0008:0x0000176f, DPL=0


This is the IDT + entries.

Code:
<bochs:9> info gdt
Global Descriptor Table (base=0x0000000000007f15, limit=23):
GDT[0x0000]=??? descriptor hi=0x00000000, lo=0x00000000
GDT[0x0008]=Code segment, base=0x00000000, limit=0xffffffff, Execute/Read, Non-Conforming, Accessed, 32-bit
GDT[0x0010]=Data segment, base=0x00000000, limit=0xffffffff, Read/Write, Accessed


This is the gdt + entries.

Code:
00015823627e[CPU0  ] interrupt(): not accessible or not code segment cs=0x0008
00015823627e[CPU0  ] interrupt(): not accessible or not code segment cs=0x0008
00015823627e[CPU0  ] interrupt(): not accessible or not code segment cs=0x0008
(0).[15823627] [0x000000001005] 0008:0000000000001005 (unk. ctxt): jmp .-2  (0x00001005)     ; ebfe
00015823627e[CPU0  ] exception(): 3rd (13) exception with no resolution, shutdown status is 00h, resetting

And the error generated by the possible overwrite.
Code:
uint32_t bitmap[PAGE_NUMBER];

void init_bitmap()
{
   mem_map* mmap = (mem_map*)MEM_MAP_LOCATION;

    //initialize bitmap with PAGE_ALLOCATED
   for (size_t i = 0; i < PAGE_NUMBER; ++i)
   {
        bitmap[i] = 0x08080808;
   }

}



And this is the code for initializing the bitmap if it helps.(the value used for initialization is just a debug value)

I really don't know how to solve this issue. Can you please help me?

Author:  Octocontrabass [ Mon Jul 25, 2022 10:10 am ]
Post subject:  Re: global array probably overwrites idt

lowLevelGod wrote:
The bitmap is stored at 15776(I found out by trying to debug my code).

Code:
<bochs:9> info gdt
Global Descriptor Table (base=0x0000000000007f15, limit=23):

Your bitmap is stored at address 0x3DA0 and its length is 0x20000 bytes.

Your GDT is stored at address 0x7F15.

It's just a coincidence that the first access to your GDT after you've overwritten it is an interrupt.

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/