Setting up the GDT in c using gcc.

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.
Octocontrabass
Member
Member
Posts: 5572
Joined: Mon Mar 25, 2013 7:01 pm

Re: Setting up the GDT in c using gcc.

Post by Octocontrabass »

bloodline wrote:Shouldn't the first opcode be lgdtl (%0), as we are loading a pointer?
I forgot to mention: we're not loading a pointer. The operand being passed via inline ASM is the GDTR struct itself, not a pointer to said struct. This has two advantages: the compiler can generate the pointer however it wishes instead of loading it into a register, and the compiler is aware that the value of the struct has significance instead of just the value of the pointer to the struct.

That last bit is one of the many reasons why inline assembly is hard. The compiler assumes values not used as inputs or outputs have no significance to the inline assembly, so if you pass a pointer to inline assembly, the compiler only sees the value of the pointer as being significant, and not the object it points to. You can use a "memory" clobber to tell the compiler that the inline assembly may affect values other than its inputs and outputs, but then the compiler must spill all objects to memory.

None of that is a problem if you use an external function written in assembly.
bloodline wrote:I attempted to build my interrupt stack frame using inline asm, but I must have made a mistake somewhere as it caused the interrupts to not return properly, so currently my int handler code is in a separate asm file... I will attempt to do it inline again when I get better at x86 asm.
I don't think that's a supported use case for inline assembly. There's a function attribute you can use, but it's specific to x86 and requires some extra compiler options you may not want to use.
User avatar
bloodline
Member
Member
Posts: 264
Joined: Tue Sep 15, 2020 8:07 am
Location: London, UK

Re: Setting up the GDT in c using gcc.

Post by bloodline »

Octocontrabass wrote:
bloodline wrote:Shouldn't the first opcode be lgdtl (%0), as we are loading a pointer?
I forgot to mention: we're not loading a pointer. The operand being passed via inline ASM is the GDTR struct itself, not a pointer to said struct. This has two advantages: the compiler can generate the pointer however it wishes instead of loading it into a register, and the compiler is aware that the value of the struct has significance instead of just the value of the pointer to the struct.

That last bit is one of the many reasons why inline assembly is hard. The compiler assumes values not used as inputs or outputs have no significance to the inline assembly, so if you pass a pointer to inline assembly, the compiler only sees the value of the pointer as being significant, and not the object it points to. You can use a "memory" clobber to tell the compiler that the inline assembly may affect values other than its inputs and outputs, but then the compiler must spill all objects to memory.

None of that is a problem if you use an external function written in assembly.
:D I think I need to tread very carefully with inline asm... It is fun though and that is the whole reason to do this. I appreciate your guidance here!
bloodline wrote:I attempted to build my interrupt stack frame using inline asm, but I must have made a mistake somewhere as it caused the interrupts to not return properly, so currently my int handler code is in a separate asm file... I will attempt to do it inline again when I get better at x86 asm.
I don't think that's a supported use case for inline assembly. There's a function attribute you can use, but it's specific to x86 and requires some extra compiler options you may not want to use.
I guess given C's dependence upon the stack, screwing around with it is a recipe for disaster, especially since I have no idea what the compiler is doing (despite spending too much time on Compiler Explorer... https://godbolt.org) and my ignorance with x86 asm.
CuriOS: A single address space GUI based operating system built upon a fairly pure Microkernel/Nanokernel. Download latest bootable x86 Disk Image: https://github.com/h5n1xp/CuriOS/blob/main/disk.img.zip
Discord:https://discord.gg/zn2vV2Su
User avatar
bloodline
Member
Member
Posts: 264
Joined: Tue Sep 15, 2020 8:07 am
Location: London, UK

Re: Setting up the GDT in c using gcc.

Post by bloodline »

Ok managed to get it all up and running quite easily.

Now time to figure out the multiboot structure and add a memory manager :)
Attachments
simpleOS.elf.zip
(4.87 KiB) Downloaded 22 times
MyOSScreenShot.png
CuriOS: A single address space GUI based operating system built upon a fairly pure Microkernel/Nanokernel. Download latest bootable x86 Disk Image: https://github.com/h5n1xp/CuriOS/blob/main/disk.img.zip
Discord:https://discord.gg/zn2vV2Su
Post Reply