Page 1 of 1

Valid GDT entry?

Posted: Sat Feb 25, 2012 3:51 pm
by blackfireize
Hey everyone,

Started my OS a little while back, so far I am only able to output strings and numbers (both hex and decimal). However, I began trying to
create a GDT to eventually get some MM and interrupts going. I was trying to understand the code from Bran's tutorial which encoded a
GDT entry. I'll post it below although im sure many of you have probably seen it before:

Code: Select all

entry->base_low = (base & 0xFFFF);
entry->base_mid = (base >> 16) & 0xFF;
entry->base_high = (base >> 24) & 0xFF;

entry->limit_low = (limit & 0xFFFF);
entry->granularity = ((limit >> 16) & 0x0F);

entry->granularity |= (gran & 0xF0);
entry->access = access;
While I do understand what the code is doing, I'm at my whits trying to understand why it does what it does. Also,
when I did the computation by hand, using the values which were passed to the function I got different values
than what the entry's actually contain. :shock:

For example when I computed (Where 'KERNEL_CODE_SEG' is 0x9A):

Code: Select all

encode(0x0, 0xFFFFFFFF, KERNEL_CODE_SEG, 0xCF, &gdt[1]);
The header of the function is

Code: Select all

static void encode(uint64_t base, uint64_t limit, uint8_t access, uint8_t gran, gdt_entry_t * entry)
I got the following for what I thought the values should be:

Code: Select all

base_mid = 0x0
base_low = 0x0        > All of these were what the function returned
base_high = 0x0
limit_low = 0xFFFF

granularity = 0xCF    > The function returns 0xFFCF
access = 0x9A         > The function returns 0xFC9A
Like I said, I'm totally confused. Did I do my math wrong or is it something I'm not seeing? Thanks.

Re: Valid GDT entry?

Posted: Sat Feb 25, 2012 4:09 pm
by gerryg400
The function returns 0xFFCF
encode is a void function so it didn't return this value. I guess you mean that it placed that value in the GDT structure. I'm guessing there is a bug in the function (kprintf ?) that you are using to examine the GDT.

Re: Valid GDT entry?

Posted: Sat Feb 25, 2012 4:19 pm
by blackfireize
gerryg400 wrote:
The function returns 0xFFCF
encode is a void function so it didn't return this value. I guess you mean that it placed that value in the GDT structure. I'm guessing there is a bug in the function (kprintf ?) that you are using to examine the GDT.
Yes that's what I meant. Its possible that my itoa is in error (I had problems with it in the past but lately
it has worked reliably) I hate to spam with code but, here is my itoa function
for anyone who may be able to find a problem.

Code: Select all

char * itoa (unsigned int d, unsigned int base)
{
	char temp[1024], * buf;
	int i = 0;
	
	if (d == 0)
		return "0";
	
	else if (base == 10)
	{
		while (d)
		{
			temp[i++] = (char)((d % base + 48));
			d /= base;
		}
		buf = strrev(temp);
	}
	
	else if (base == 16)
	{
		while (d)
		{
			if ((d % base) >= 0 && (d % base) <= 9)
				temp[i++] = (char)((d % base + 48));
			else
				temp[i++] = (char)((d % base + 55));
			d /= base;
		}
		buf = strrev(temp);
	}
	return buf;

Re: Valid GDT entry?

Posted: Sat Feb 25, 2012 4:50 pm
by gerryg400

Code: Select all

char temp[1024], * buf;
   int i = 0;
  
1. When a function returns you no longer have access to its local variables. You cannot do

Code: Select all

return buf;
2. You passed a 'string' to strrev without null terminating it.

3. Your itoa (int to ascii) takes an unsigned. That may, depending on how its called cause sign extension.

[EDIT] removed my incorrect advice. Too early on a Sunday to be giving advice.

Re: Valid GDT entry?

Posted: Sat Feb 25, 2012 6:14 pm
by blackfireize
gerryg400 wrote:

Code: Select all

char temp[1024], * buf;
   int i = 0;
  
1. When a function returns you no longer have access to its local variables. You cannot do

Code: Select all

return buf;
2. You passed a 'string' to strrev without null terminating it.

3. Your itoa (int to ascii) takes an unsigned. That may, depending on how its called cause sign extension.

[EDIT] removed my incorrect advice. Too early on a Sunday to be giving advice.
Your second point was exactly what was wrong. Adding a null terminator makes every square-up right.
At least I know Im not crazy (for now 8) ) thank you so much! :mrgreen:

Re: Valid GDT entry?

Posted: Sat Feb 25, 2012 11:31 pm
by gerryg400
I hope you also understand the first point. Have you fixed that ?