How to make a GDT?

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.
zap8600
Member
Member
Posts: 195
Joined: Tue Nov 02, 2021 11:26 am
Libera.chat IRC: zap8600

Re: How to make a GDT?

Post by zap8600 »

devc1 wrote:I looked in to limines github repo and It seems that limines GDT 32 bit. And you are trying to run 64 bit code in 32 bit mode ? If yes and the kernel loads perfectly into the startup function, I can help you but you will not be able to use the terminal, otherwise you can just create your own terminal.

When you create a kernel, you are nomore dependent of bootloader stuff, the bootloader passed the initdata, now its your turn to startup the OS (a long trip).
I was able to use the Limine terminal in my 64-bit OS, though. If I have to, I'll use the Multiboot protocol, but I wouldn't like to if I don't have to.
zap8600
Member
Member
Posts: 195
Joined: Tue Nov 02, 2021 11:26 am
Libera.chat IRC: zap8600

Re: How to make a GDT?

Post by zap8600 »

So is there a reason why my GDT isn't working?
Octocontrabass
Member
Member
Posts: 5562
Joined: Mon Mar 25, 2013 7:01 pm

Re: How to make a GDT?

Post by Octocontrabass »

devc1 wrote:Yeahhh, I am not a fan of reading btw.
OS development requires lots of reading. Perhaps you should choose a different hobby.
zap8600 wrote:So is there a reason why my GDT isn't working?
I don't see any obvious problems. Try using your debugger to examine the memory contents at the address your GDTR points to.
devc1
Member
Member
Posts: 439
Joined: Fri Feb 11, 2022 4:55 am
Location: behind the keyboard

Re: How to make a GDT?

Post by devc1 »

Has anyone ever using limine here ?
devc1
Member
Member
Posts: 439
Joined: Fri Feb 11, 2022 4:55 am
Location: behind the keyboard

Re: How to make a GDT?

Post by devc1 »

OS development requires lots of reading. Perhaps you should choose a different hobby.
Of course, I mainly read if I have to solve my own problems or problems that I know I will face later. Otherwise, I don't see myself reading the whole limine documentation because I'll never use it, as you've already seen in my github repo, All the programs are written from scratch with no included libraries.

I don't recommend any begineer to start with a premade bootloader, to learn he should start from scratch and discover things from the root of the tree, not from the middle.
nexos
Member
Member
Posts: 1081
Joined: Tue Feb 18, 2020 3:29 pm
Libera.chat IRC: nexos

Re: How to make a GDT?

Post by nexos »

devc1 wrote:I don't recommend any begineer to start with a premade bootloader
No. It's best for a beginner to skip the headache of the bootloader stage and start working on stuff that will actually motivate them.
devc1 wrote:Otherwise, I don't see myself reading the whole limine documentation because I'll never use it
In that case, don't post about subjects you aren't very familiar with, and potentially mislead someone. I'm telling you this from experience :wink:
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: How to make a GDT?

Post by iansjack »

Why stop at the boot loader? I recommend beginners to write their own BIOS. Best to start at the root of the tree. :lol:

Hardcore enthusiasts would recommend writing your own microcode.
nullplan
Member
Member
Posts: 1790
Joined: Wed Aug 30, 2017 8:24 am

Re: How to make a GDT?

Post by nullplan »

iansjack wrote:Why stop at the boot loader? I recommend beginners to write their own BIOS. Best to start at the root of the tree. :lol:

Hardcore enthusiasts would recommend writing your own microcode.
Invent your own CPU, build it from discrete components. Next step: Use uranium to cause bit flips in RAM precisely in such a way as to write whatever program you wanted in there.

In case anyone wonders what my sincere opinion is: The kernel is the important thing, everything else is just getting the machine ready for the kernel. Build the kernel!
Carpe diem!
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: How to make a GDT?

Post by iansjack »

nullplan wrote:Invent your own CPU, build it from discrete components.
That's actually quite a fun exercise (the designing, not the building). It's even the basis of a computer game or two.

But I agree with you. Before the kernel it's just building blocks that teach you very little (other than how to write a boot loader). The interesting stuff starts once your kernel is loaded.
zap8600
Member
Member
Posts: 195
Joined: Tue Nov 02, 2021 11:26 am
Libera.chat IRC: zap8600

Re: How to make a GDT?

Post by zap8600 »

Octocontrabass wrote: I don't see any obvious problems. Try using your debugger to examine the memory contents at the address your GDTR points to.
Is there any way I could set up my own framebuffer from my OS? I could use Limine's framebuffer, but I'm not sure how I would print to it.
Octocontrabass
Member
Member
Posts: 5562
Joined: Mon Mar 25, 2013 7:01 pm

Re: How to make a GDT?

Post by Octocontrabass »

zap8600 wrote:Is there any way I could set up my own framebuffer from my OS?
In theory yes, but in practice probably not. You would need a driver for the display adapter, and each display adapter requires a different driver.
zap8600 wrote:I could use Limine's framebuffer, but I'm not sure how I would print to it.
It's a bitmap. You plot pixels on it the same way you plot pixels in any bitmap.

Is there any particular reason why you don't want to use a debugger to check your GDT?
zap8600
Member
Member
Posts: 195
Joined: Tue Nov 02, 2021 11:26 am
Libera.chat IRC: zap8600

Re: How to make a GDT?

Post by zap8600 »

Octocontrabass wrote: Is there any particular reason why you don't want to use a debugger to check your GDT?
I was wanting to make progress. That usually gives me the motivation to continue working on my OS.
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: How to make a GDT?

Post by iansjack »

zap8600 wrote:
Octocontrabass wrote: Is there any particular reason why you don't want to use a debugger to check your GDT?
I was wanting to make progress. That usually gives me the motivation to continue working on my OS.
The best way to make progress is to learn how to use a debugger. That stops you wasting a lot of time on uninformed guesses.
zap8600
Member
Member
Posts: 195
Joined: Tue Nov 02, 2021 11:26 am
Libera.chat IRC: zap8600

Re: How to make a GDT?

Post by zap8600 »

iansjack wrote: The best way to make progress is to learn how to use a debugger. That stops you wasting a lot of time on uninformed guesses.
That sort of makes sense. I should look at some new problems that I've noticed with my code.

First, when the assembly code reloads the segments, it doesn't point to the 64-bit data segment.

Code: Select all

asm volatile (
		"mov %0, %%rdi\n"
		"lgdt (%%rdi)\n"
		"mov $0x10, %%ax\n"
		"mov %%ax, %%ds\n"
		"mov %%ax, %%es\n"
		"mov %%ax, %%ss\n"
		"mov $0x2b, %%ax\n"
		"ltr %%ax\n"
		: : "r"(&gdt[0].pointer)
);
I believe that it is pointing to the 16-bit segment. It should instead point to 0x30, which I believe is the 64-bit segment.
Second, I think that the TSS data isn't being written to the write entry.

Code: Select all

for (int i = 0; i < 32; ++i) {
		gdt[i].pointer.limit = sizeof(gdt[i].entries)+sizeof(gdt[i].tss_extra)-1;
		gdt[i].pointer.base  = (uintptr_t)&gdt[i].entries;

		uintptr_t addr = (uintptr_t)&gdt[i].tss;
		gdt[i].entries[5].limit_low = sizeof(gdt[i].tss);
		gdt[i].entries[5].base_low = (addr & 0xFFFF);
		gdt[i].entries[5].base_middle = (addr >> 16) & 0xFF;
		gdt[i].entries[5].base_high = (addr >> 24) & 0xFF;
		gdt[i].tss_extra.base_highest = (addr >> 32) & 0xFFFFFFFF;
}
If this is the case though, I believe the problem should be fixed by replacing the 5 with 7.

I may also add this mini debugger to my kernel.
zap8600
Member
Member
Posts: 195
Joined: Tue Nov 02, 2021 11:26 am
Libera.chat IRC: zap8600

Re: How to make a GDT?

Post by zap8600 »

I think I have found another problem. I think that my TSS's offset (base) is located in one of the GDT entries.

Code: Select all

void init_gdt()
{
    for (int i = 1; i < 32; ++i) {
		memcpy(&gdt[i], &gdt[0], sizeof(*gdt));
	}

    for (int i = 0; i < 32; ++i) {
		gdt[i].pointer.limit = sizeof(gdt[i].entries)+sizeof(gdt[i].tss_extra)-1;
		gdt[i].pointer.base  = (uintptr_t)&gdt[i].entries;

		uintptr_t addr = (uintptr_t)&gdt[i].tss;
		gdt[i].entries[7].limit_low = sizeof(gdt[i].tss);
		gdt[i].entries[7].base_low = (addr & 0xFFFF);
		gdt[i].entries[7].base_middle = (addr >> 16) & 0xFF;
		gdt[i].entries[7].base_high = (addr >> 24) & 0xFF;
		gdt[i].tss_extra.base_highest = (addr >> 32) & 0xFFFFFFFF;
	}
...
}
Is this wrong with my current segments?
Post Reply