GDT reloading problem

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.
wilson
Posts: 13
Joined: Fri Mar 18, 2011 8:42 am

GDT reloading problem

Post 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 ?
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:

Re: GDT at runtime

Post 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 :(
"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 ]
wilson
Posts: 13
Joined: Fri Mar 18, 2011 8:42 am

Re: GDT at runtime

Post 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 !
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:

Re: GDT at runtime

Post 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.
"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
Chandra
Member
Member
Posts: 487
Joined: Sat Jul 17, 2010 12:45 am

Re: GDT at runtime

Post 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.
Programming is not about using a language to solve a problem, it's about using logic to find a solution !
User avatar
os64dev
Member
Member
Posts: 553
Joined: Sat Jan 27, 2007 3:21 pm
Location: Best, Netherlands

Re: GDT reloading problem

Post 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.
Author of COBOS
ym
Posts: 8
Joined: Thu Mar 24, 2011 2:05 am

Re: GDT reloading problem

Post by ym »

maybe the retour's problem, disasmble it and check it's address.
j8long
Posts: 12
Joined: Wed Jun 09, 2010 8:13 am

Re: GDT reloading problem

Post 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!
User avatar
lup0
Posts: 5
Joined: Fri Mar 25, 2011 4:01 am
Location: India
Contact:

Re: GDT reloading problem

Post 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
Everyday schedule: wake up >> eat >> write OS >> eat >> sleep ;-)
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:

Re: GDT reloading problem

Post by Combuster »

lup0 wrote:develop .NET applications with Visual Basic
Troll! Troll in the dungeons!
"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 ]
wilson
Posts: 13
Joined: Fri Mar 18, 2011 8:42 am

Re: GDT reloading problem

Post 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 = .;
}
User avatar
Chandra
Member
Member
Posts: 487
Joined: Sat Jul 17, 2010 12:45 am

Re: GDT reloading problem

Post 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
Programming is not about using a language to solve a problem, it's about using logic to find a solution !
wilson
Posts: 13
Joined: Fri Mar 18, 2011 8:42 am

Re: GDT reloading problem

Post 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;
}

User avatar
thepowersgang
Member
Member
Posts: 734
Joined: Tue Dec 25, 2007 6:03 am
Libera.chat IRC: thePowersGang
Location: Perth, Western Australia
Contact:

Re: GDT reloading problem

Post 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);
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
wilson
Posts: 13
Joined: Fri Mar 18, 2011 8:42 am

Re: GDT reloading problem

Post 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)
Post Reply