Page 1 of 2

GDT reloading problem

Posted: Sat Mar 19, 2011 2:05 pm
by wilson
me, I have a problem with GDT. my bootloader correctly switches in protected mode, when I reload the GDT by my kenel (written in C) I have a bugg. the bugg is this part of code

Code: Select all

 __asm__(" movw $0x10, %ax \n \
		  movw %ax, %ds	 \n \
		  movw %ax, %es	 \n \
		  movw %ax, %ss  \n \
		  movw %ax, %fs  \n \
		  movw %ax, %gs  \n \
		  ljmp $0x08, $retour \n \
		  retour: 	\n");
It nevers come back from this code, I don't know why ?? can You help me please ?

Re: GDT at runtime

Posted: Sun Mar 20, 2011 7:32 am
by Combuster
Several issues:
- Assembly is not declared volatile
- No inputs, outputs or clobbers specified.
- The code does not load a GDT, only segment registers.
- You hijacked a thread with a different question :(

Re: GDT at runtime

Posted: Sun Mar 20, 2011 8:14 am
by wilson
I used a "__asm__("lgdtl (gdt_ptr)") before the code I shows last time, so I don't know where is the bugg !

Re: GDT at runtime

Posted: Sun Mar 20, 2011 10:54 am
by Combuster
The only thing we can see based on the information provided is that your inline assembly skills are sub-par (details already mentioned - did you actually fix them?), and that the same holds for your debugging skills. You have not provided enough information for us to reproduce the mentioned bug. You have not pinpointed the exact operation causing the breakage. Your description of the code's behaviour is ambiguous. It could be anything from an exception (breaking the flow), bad jump (executing something else), or a system crash (so that the emulator/vm stops interpreting). You also have not visibly attempted to solve the problem using a debugger.

One likely explanation for all this would be is that you grabbed some code you didn't (fully) understand, dropped it in, found out it doesn't work, then complain here. Is that guess accurate?

At least you should have a tasklist by now.

Re: GDT at runtime

Posted: Sun Mar 20, 2011 11:03 am
by Chandra
Add this to the tasklist:

Generally, a better idea is to put that stuff in separate assembly file and assemble it. You may then link that file together with the kernel. This way, you can optimize that specific code, the way you want to, without ever worrying about compiler's optimization. I'd recommend, you run your code under Bochs and see if it fires an exception.

Re: GDT reloading problem

Posted: Mon Mar 21, 2011 8:20 pm
by os64dev
To keep it a bit constructive

consult the pages:
GDT
GDT Tutorial
and see if your implementation is up to par with the requirements presented there.

note: that the use of two related asm statement is considered bad practice as the compiler sees them as two different pieces of code with all the related problems related to clobbering of registers which combuster already mentioned. Thus merge them into one.

Re: GDT reloading problem

Posted: Thu Mar 24, 2011 8:05 am
by ym
maybe the retour's problem, disasmble it and check it's address.

Re: GDT reloading problem

Posted: Thu Mar 24, 2011 8:48 pm
by j8long
"maybe the retour's problem, disasmble it and check it's address."
i think so.
can you post your makefile here?
maybe it's your ldscript problem.
retour is a label,maybe it's address is mapped wrong address!

Re: GDT reloading problem

Posted: Fri Mar 25, 2011 9:49 am
by lup0
wilson wrote:me, I have a problem with GDT. my bootloader correctly switches in protected mode, when I reload the GDT by my kenel (written in C) I have a bugg. the bugg is this part of code

Code: Select all

 __asm__(" movw $0x10, %ax \n \
		  movw %ax, %ds	 \n \
		  movw %ax, %es	 \n \
		  movw %ax, %ss  \n \
		  movw %ax, %fs  \n \
		  movw %ax, %gs  \n \
		  ljmp $0x08, $retour \n \
		  retour: 	\n");
It nevers come back from this code, I don't know why ?? can You help me please ?
hi!

wilson please clear some doubts first.. what bootloader are you using? is it GRUB or LILO or some other that you have created or copied from somewhere else?

if its your bootloader then are you setting the 32-bit or 16-bit code modes correctly at all places? you didn't even mention the exact error or "bugg" you are recieving! is the computer (or emulator) resetting or showing some message?? :-s

let me suggest you the right thing to do in this case :-k
first write another post with the immediate preceding and succeeding codes (yes both!) in a line, clearly marking out the points where **you think** the "bugg" could be using comments. Mention all the details i asked here and others also that **you think** are significant. Also keep trying to solve your problem yourself as well!

If you think this is TOO MUCH for you then i suggest develop .NET applications with Visual Basic instead of trying to create an OS.. :lol:

bye

Re: GDT reloading problem

Posted: Fri Mar 25, 2011 3:48 pm
by Combuster
lup0 wrote:develop .NET applications with Visual Basic
Troll! Troll in the dungeons!

Re: GDT reloading problem

Posted: Fri Mar 25, 2011 8:50 pm
by wilson
lup0 !

firstly I didn't copy some code from anywhere, I wrote this code by myself by using my own knowledge
secondly if I wanted to develop .NET applications, I will not post anything here !!!

so to answer you, my bootloader is GRUB, I already saw http://wiki.osdev.org/GDT and http://wiki.osdev.org/GDT_Tutorial and my code follows their requirements, but it doesn't work.

this is my linker, as you wanted to see

Code: Select all

ENTRY(start)
SECTIONS
{
  .text 0x100000 :
  {
    code = .; _code = .; __code = .;
    *(.text)
    . = ALIGN(4096);
  }

  .data :
  {
     data = .; _data = .; __data = .;
     *(.data)
     *(.rodata)
     . = ALIGN(4096);
  }

  .bss :
  {
    bss = .; _bss = .; __bss = .;
    *(.bss)
    . = ALIGN(4096);
  }

  end = .; _end = .; __end = .;
}

Re: GDT reloading problem

Posted: Sat Mar 26, 2011 4:14 am
by Chandra
I can see how desperate you are to get help.
But, the fact is, every time you return, you don't post complete info, nor you descend your tasklist.
You didn't posted your descriptor table code, which is where the probable error is. Can we see how you setup the discriptor tables? The code involving gdt_ptr is not here.
The next time you're here, post every portion of code, that you think is associated to GDT setup.

Best Regards,
Chandra

Re: GDT reloading problem

Posted: Sat Mar 26, 2011 8:05 am
by wilson
ok!! this the structure for a descriptor in the GDT and the pointer structure

Code: Select all

struct struct_entry_gdt
{
	u16_int limite_low;
	u16_int base_low;
	u8_int base_middle;
	u8_int base_hight;
	u8_int access;
	u8_int granularity;
} __attribute__((packed));

typedef struct struct_entry_gdt gdt_entry_t;

struct gdt_ptr_struct
{
	u16_int limite;
	u32_int base;
} __attribute__((packed));

typedef struct gdt_ptr_struct gdt_ptr_t;


and here, there are the function to initialize GDT and to enter a new descriptor in

Code: Select all

void init_gdt()
{
	gdt_ptr.limite = (sizeof(gdt_entry_t) * 5) - 1;
	gdt_ptr.base = (u32_int)&gdt_entries;

	gdt_set_gate(0, 0x0, 0x0, 0x0, 0x0);			        /* Null descriptor */
	gdt_set_gate(1, 0x0, 0xFFFFFFFF, 0x9A, 0xCF);		/* code segment */

	gdt_set_gate(2, 0x0, 0xFFFFFFFF, 0x92, 0xCF);		/* data segment */
	gdt_set_gate(3, 0x0, 0xFFFFFFFF, 0xFA, 0xCF);		/*  code segment  (user mode) */
	gdt_set_gate(4, 0x0, 0xFFFFFFFF, 0xF2, 0xCF);		/* data segment (user mode) */
	
	memcpy((char *)0x0, (char *)gdt_entries, gdt_ptr.limite);

	asm("lgdtl (gdt_ptr)");
	asm("   movw $0x10, %ax	\n \
            movw %ax, %ds	\n \
            movw %ax, %es	\n \
            movw %ax, %fs	\n \
            movw %ax, %gs	\n \
            ljmp $0x08, $next	\n \
            next:		\n");*/

}

void gdt_set_gate(int num, u32_int base, u32_int limite, u8_int access, u8_int gran)
{
	gdt_entries[num].base_low = (base & 0xFFFF);
	gdt_entries[num].base_middle = (base >> 16) & 0xFF;
	gdt_entries[num].base_hight = (base >> 24) & 0xFF;

	gdt_entries[num].limite_low = (limite & 0xFFFF);
	gdt_entries[num].granularity = (limite >> 16) & 0x0F;

	gdt_entries[num].granularity |= gran & 0xF0;
	gdt_entries[num].access = access;
}


Re: GDT reloading problem

Posted: Sat Mar 26, 2011 8:54 am
by thepowersgang
I'm almost scared to ask, but what is the purpose of this line?

Code: Select all

memcpy((char *)0x0, (char *)gdt_entries, gdt_ptr.limite);

Re: GDT reloading problem

Posted: Sat Mar 26, 2011 9:11 am
by wilson
thepowersgang wrote:I'm almost scared to ask, but what is the purpose of this line?

Code: Select all

memcpy((char *)0x0, (char *)gdt_entries, gdt_ptr.limite);
this line permit to copy the GDT table at the adresse 0x0 here is its prototype

Code: Select all

memcpy( char *dest, char *src, int ln)