"Segment not present" on some machines

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
User avatar
raistlinthewiz
Member
Member
Posts: 34
Joined: Wed Jun 29, 2005 11:00 pm
Contact:

"Segment not present" on some machines

Post by raistlinthewiz »

Hi, i do have a basic GDT code working nice on my test enviroment like bochs, qemu and vmware. The code & kernel just runs fine. Also on my laptop with 1GB ram, it just run well too. But when i try the kernel on my desktop system with 2GB ram, i do just get segment not present exception.

My current GDT code:

Code: Select all

#include "include/gdt.h"

void set_gdt_entry(int num, unsigned long base,unsigned long limit, unsigned char access, unsigned char gran)
{
  gdt[num].base=(base & 0xFFFF); /* lowest 2 bytes of base */
  gdt[num].base_middle=(base >> 16) & 0xFF; /* third third byte */
  gdt[num].base_high=(base >> 24 ) & 0xFF; /* highest byte */
  
  gdt[num].limit=(limit & 0xFFFF); /* lowest 2 bytes of limit */
  
  gdt[num].granularity=((limit >> 16) & 0x0F); /* 1 byte */
  gdt[num].granularity|=(gran & 0xF0); /* 1 byte */
  gdt[num].access=access;

}

  gp.base=(unsigned int)&gdt; /* set base address to our descriptor table */
  gp.limit=(sizeof(struct gdt_entry) * 3)-1; /* (size of our gdt table)-1  */
  
  /* NULL descriptor table - CPU needs it */
  set_gdt_entry(0,0,0,0,0);

set_gdt_entry(1,0x0,0xFFFFF,0x9A,0xC0);
set_gdt_entry(2,0x0,0xFFFFF,0x92,0xC0);
  gdt_flush(); 
anyone hit this problem before?

thanks
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

I tend to look somehwere else, like wether or not you zero memory before using it.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
mathematician
Member
Member
Posts: 437
Joined: Fri Dec 15, 2006 5:26 pm
Location: Church Stretton Uk

Post by mathematician »

What does gdt_flush() do? If I read the Intel manual aright, a segment not present fault can only occur when you are either loading a segment register (as in an int or far call, or explicitly) or else accessing one of the control registers.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

I can show you the code:

Code: Select all

gdt_flush
    lgdt _gp
    jmp 0x08:flush
flush:
    mov ax,0x10
    mov ds,ax
    mov es,ax
    mov fs,ax
    mov gs,ax
    ret
In set_gdt_entry, 0xC0 should be 0xCF.
Post Reply