home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   alt.os.development      Operating system development chatter      4,255 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 3,544 of 4,255   
   Alexei A. Frounze to All   
   Re: paging makes OS to reboot infinitely   
   04 Dec 22 18:06:42   
   
   From: alexfrunews@gmail.com   
      
   On Sunday, December 4, 2022 at 4:48:08 AM UTC-8, אורי ויסבלום wrote:   
   > Hello, I'm writing an OS and I can't find my problem. The second i set the   
   CR0 paging bit, the system reboots itself, and goes to an infinite loop.    
   > currently i dont care about page allocations, i'll write this part later,   
   for now i want a paging setup that works, even with only one page directory   
   entry.    
   > if you know what might cause this, please let me know.    
   > the kernel calls initPDT with some arbitrary number and goes to an infinite   
   loop.    
   > my code is as followed (ignore irq, and print, they work fine):    
   >    
   > #define PDT_SIZE 1024    
   >    
   > typedef enum {    
   > PRESENT = 1,    
   > READWRITE = 2,    
   > USER = 4,    
   > WRITETHROUGH = 8,    
   > CACHE = 16    
   > } PageDirectoryFlags;    
   >    
   > typedef uint32_t PDEntry;    
   >    
   > typedef struct {    
   > PDEntry entries[PDT_SIZE];    
   > } PD;    
   >    
   > typedef uint32_t PTEntry;    
   >    
   > typedef struct {    
   > PTEntry entries[PDT_SIZE];    
   > } PT;    
   >    
   > void startVirtualMode(uint32_t address) {    
   > __asm__("mov %0, %%cr3"::"r"(address));    
   > uint32_t cr0 = 0;    
   > __asm__("mov %%cr0, %0":"=r"(cr0));    
   > cr0 |= 0x80000000;    
   > __asm__("mov %0, %%cr0"::"r"(cr0));    
   > }    
   >    
   > void initPDT(uint32_t address) {    
   > PD* table = (PD*)address;    
   >    
   > for (int i = 0; i < PDT_SIZE; i++) {    
   > table->entries[i] = READWRITE | PRESENT | USER | (i>>22);    
   > }    
   >    
   > // last entry points to the pdt itself    
   > table->entries[PDT_SIZE-1] = READWRITE | PRESENT | address;    
   >    
   > irqInstallHandler(14, pagefault);    
   >    
   > startVirtualMode(address);    
   > }   
      
   If your page tables aren't properly set up, your kernel will almost certainly   
   triple-fault and reset because it itself uses page tables to access memory.   
   When you screw up your page tables, memory accesses either won't work at all   
   due to permissions or will read/write wrong memory cells, not the ones   
   you're expeting.   
      
   Are you setting up 4KB pages or 4MB pages? You can't magically have both.   
   For 4KB pages you aren't setting up any page tables or you're not showing your   
   code for this.   
   For 4MB pages recursive mapping won't work. And you're not setting   
   PDE.PS=1 for this either.   
      
   Also, with PDT_SIZE=1024, (i>>22) is always 0 in   
   ----8<----   
   for (int i = 0; i < PDT_SIZE; i++) {    
     table->entries[i] = READWRITE | PRESENT | USER | (i>>22);    
   }    
   ----8<----   
   For 4MB pages you probably want (i<<22) here.   
      
   You need to understand the page table hierarchy/structure and   
   properly set it up.   
      
   Alex   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca