OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 4:22 pm

All times are UTC - 6 hours




Post new topic This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: GDT crash after reload
PostPosted: Mon Dec 19, 2016 2:13 pm 
Offline

Joined: Thu Dec 08, 2016 12:32 pm
Posts: 5
Hi everyone,
actually I'm trying to reload GDT after exitBootServices but I have a problem with it because qemu is showing an error or is restarting after long jump.
OS loader loads kernel by loadImage and starts it by startImage. After that my kernel gets necessary data, calls exitBootServices and calls initGDT.

some code:
Creating descriptor from tutorial
Code:
void
create_descriptor(uint32_t base, uint32_t limit, uint16_t flag, void* position)
{
    uint64_t* descriptor = position;

    // Create the high 32 bit segment
    descriptor[0]  =  limit       & 0x000F0000;         // set limit bits 19:16
    descriptor[0] |= (flag <<  8) & 0x00F0FF00;         // set type, p, dpl, s, g, d/b, l and avl fields
    descriptor[0] |= (base >> 16) & 0x000000FF;         // set base bits 23:16
    descriptor[0] |=  base        & 0xFF000000;         // set base bits 31:24

    // Shift by 32 to allow for low part of segment
    descriptor[0] <<= 32;

    // Create the low 32 bit segment
    descriptor[0] |= base  << 16;                       // set base bits 15:0
    descriptor[0] |= limit  & 0x0000FFFF;               // set limit bits 15:0

}


Code:
struct gdt_entry
{
    unsigned short limit_low;
    unsigned short base_low;
    unsigned char base_middle;
    unsigned char access;
    unsigned char granularity;
    unsigned char base_high;
} __attribute__((packed));

struct gdt_ptr
{
    UINT16 limit;
    UINT32 base;
} __attribute__((packed));

struct gdt_entry gdt[5] __attribute__((aligned(8)));
struct gdt_ptr gp;
void genereateGDT() {
   gp.limit = (sizeof(struct gdt_entry) * 5) - 1;
   gp.base = &gdt[0];
    create_descriptor(0, 0, 0, &gdt[0]);
    create_descriptor(0, 0x000FFFFF, (GDT_CODE_PL0), &gdt[1]);
    create_descriptor(0, 0x000FFFFF, (GDT_DATA_PL0), &gdt[2]);
    create_descriptor(0, 0x000FFFFF, (GDT_CODE_PL3), &gdt[3]);
    create_descriptor(0, 0x000FFFFF, (GDT_DATA_PL3), &gdt[4]);
}

void loadGDTR() {
   __asm__ __volatile__  (
      "cli \n\t"
      "lgdt %0 \n\t"
      :
      : "m" (gp)
   );
}
void initGDT() {
   genereateGDT();
   loadGDTR();
   __asm__ __volatile__ (
      "pushl 0x08\n"
      "pushl $1f\n"
      "lret \n"
      "1:\n"
      "movw $0x10, %%ax \n\t"
      "movw %%ax, %%es \n\t"
      "movw %%ax, %%fs \n\t"
      "movw %%ax, %%gs \n\t"
      "movw %%ax, %%ss \n\t"
      "movw %%ax, %%ds \n\t"

   :
   :    
   );

}


Some memory dump from qemu:
Before load GDT:

Registers:
Code:
EIP=064fb560 EFL=00000016 [----AP-] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0008 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
CS =0010 00000000 ffffffff 00c09b00 DPL=0 CS32 [-RA]
SS =0008 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
DS =0008 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
FS =0008 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
GS =0008 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     07f7ed90 0000003f
IDT=     07c5d010 000007ff

GDT:
Code:
0000000007f7ed90: 0x00000000 0x00000000 0x0000ffff 0x00cf9300
0000000007f7eda0: 0x0000ffff 0x00cf9b00 0x0000ffff 0x00cf9200
0000000007f7edb0: 0x0000ffff 0x00cf9a00 0x0000ffff 0x00af9b00
0000000007f7edc0: 0x00000000 0x00000000


Before jump:

Registers:
Code:
ES =0008 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
CS =0010 00000000 ffffffff 00c09b00 DPL=0 CS32 [-RA]
SS =0008 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
DS =0008 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
FS =0008 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
GS =0008 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     06504010 00000027
IDT=     07c5d010 000007ff

GDT:
Code:
0000000006504010: 0x00000000 0x00000000 0x0000ffff 0x00cf9a00
0000000006504020: 0x0000ffff 0x00cf9200 0x0000ffff 0x00cffa00
0000000006504030: 0x0000ffff 0x00cff200 0x00000000 0x00000000


Sometimes qemu shows an error after long jump:
Quote:
qemu: fatal: Trying to execute code outside RAM or ROM
where all registers(cs, ds...) are 0x08


Top
 Profile  
 
 Post subject: Re: GDT crash after reload
PostPosted: Thu Nov 09, 2017 8:52 am 
Offline
Member
Member

Joined: Sat Jan 21, 2017 7:35 am
Posts: 35
Nobody will look at your code becauz its to laaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaarge.


Top
 Profile  
 
 Post subject: Re: GDT crash after reload
PostPosted: Thu Nov 09, 2017 8:57 am 
Offline
Member
Member
User avatar

Joined: Sun Oct 22, 2006 7:01 am
Posts: 2646
Location: Devon, UK
SukantPal wrote:
Nobody will look at your code becauz its to laaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaarge.


You have asserted this a couple of times in recent posts and this is false. As long as you use code tags as the OP has done, sometimes having all the code provides much needed context. I promise there are people here who will trawl through the code.

Cheers,
Adam


Top
 Profile  
 
 Post subject: Re: GDT crash after reload
PostPosted: Thu Nov 09, 2017 9:02 am 
Offline
Member
Member
User avatar

Joined: Sun Oct 22, 2006 7:01 am
Posts: 2646
Location: Devon, UK
@OP: Before the jump, you state that EIP is at 0x064fb560 (according to the register dump) Where is EIP following the jump and is it where you expect it to be?

The GDT segments look sensible at first glance, again, according to the register dump.

Cheers,
Adam


Top
 Profile  
 
 Post subject: Re: GDT crash after reload
PostPosted: Thu Nov 09, 2017 9:31 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4591
Location: Chichester, UK
It may be totally irrelevant, but I'm not comfortable with the idea of loading segment registers - particularly SS - within a C function.

That aside, stepping through the code in a debugger should show you exactly where things are going wrong. That may be enough to make the error obvious.


Top
 Profile  
 
 Post subject: Re: GDT crash after reload
PostPosted: Fri Nov 10, 2017 5:14 pm 
Offline
Member
Member

Joined: Fri Aug 26, 2016 1:41 pm
Posts: 671
adsko wrote:
Code:
   __asm__ __volatile__ (
      "pushl 0x08\n"
      "pushl $1f\n"
      "lret \n"
      "1:\n"
      "movw $0x10, %%ax \n\t"
      "movw %%ax, %%es \n\t"
      "movw %%ax, %%fs \n\t"
      "movw %%ax, %%gs \n\t"
      "movw %%ax, %%ss \n\t"
      "movw %%ax, %%ds \n\t"
   :
   :    
   );
}
You are destroying the contents of the AX register without telling the assembly template you clobbered the register. You should add "eax" to the clobber list. If the compiler assumes the value in EAX doesn't change then it may be expecting the value to be the same before and after the template is run. If it is different that could cause issues.

More importantly is this error which is the likely cause of your problems:
Code:
"pushl 0x08\n"
. It was probably a simple error but you are missing the `$` on the value. You are actually pushing the 32-bit value at memory location 0x00000008 rather than the value 8. Change it to:
Code:
"pushl $0x08\n"


Top
 Profile  
 
 Post subject: Re: GDT crash after reload
PostPosted: Sat Nov 11, 2017 5:50 am 
Offline
Member
Member

Joined: Fri Aug 26, 2016 1:41 pm
Posts: 671
Sorry, didn't realize until just now that this year old thread was recently resurrected. By now the OP probably figured out their mistake.


Top
 Profile  
 
 Post subject: Re: GDT crash after reload
PostPosted: Sat Nov 11, 2017 5:57 am 
Offline
Member
Member
User avatar

Joined: Sun Oct 22, 2006 7:01 am
Posts: 2646
Location: Devon, UK
Yup - I'll lock the thread. I didn't realise that it had been necro'd either.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 82 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group