Page 1 of 1

Installing GDT - Request For Review

Posted: Wed Feb 23, 2022 2:40 am
by yasar11732
Hi,

I wrote this boot.S file using information I gathered from different sources (mostly osdev wiki). It is supposed to set up very basic GDT right after being booted by GRUB. I intend to use this GDT until I set up paging. It seems to be working so far, at least it is not crashing. I would like to know what you think about it:

Code: Select all

/* Constants */
.set ALIGN, 1 /* Page Alignment */
.set MEMINFO, 1<<1 /* Provide Meminfo */
.set FLAGS, ALIGN | MEMINFO
.set MAGIC, 0x1BADB002
.set CHECKSUM, -(MAGIC + FLAGS)

.section .multiboot
.align 4
.long MAGIC
.long FLAGS
.long CHECKSUM

.section .bss
.align 16
_stack_bottom:
.skip 16384
_stack_top:

.section .text
.global _start
.type _start, @function
_start:

    // LOAD GDT
    lgdt (gdtr)

    // refresh segment registers
    jmp $0x08,$_reload_segment_registers // for CS
_reload_segment_registers:
    mov $0x10, %cx
    mov %cx,%ds
    mov %cx,%es
    mov %cx,%fs
    mov %cx,%gs
    mov %cx,%ss

    mov $_stack_top, %esp
    // Save ebx and eax so _init doesnt trash it
    push %ebx
    push %eax
    
    call _init /* Global Constructors */

    // TODO: Install IDT Here

    call kernel_main

sleep:
    hlt
    jmp sleep

.size _start, . - _start

.section .rodata
.align 4
gdt:
    .long 0x0 // null descriptor
    .long 0x0
    .long 0xFFFF // kernel mode code segment
    .long 0xC09A00
    .long 0xFFFF // kernel mode data segment
    .long 0xC09200
gdtr:
    .word . - gdt - 1 // limit
    .long gdt // base

Re: Installing GDT - Request For Review

Posted: Wed Feb 23, 2022 12:37 pm
by Octocontrabass
Why did you set the segment limit to 256MB instead of 4GB?

Are you planning on writing a higher-half kernel?

Edit: and you didn't clear DF. Its value is undefined in Multiboot but the C ABI requires it to be cleared.

Re: Installing GDT - Request For Review

Posted: Wed Feb 23, 2022 1:19 pm
by yasar11732
Octocontrabass wrote:Why did you set the segment limit to 256MB instead of 4GB?
I thought I was setting it to 4GB. I think I made an error during calculations. Is it supposed to be;

Code: Select all

gdt:
    .long 0x0 // null descriptor
    .long 0x0
    .long 0xFFFF // kernel mode code segment
    .long 0xCF9A00
    .long 0xFFFF // kernel mode data segment
    .long 0xCF9200
Octocontrabass wrote:Are you planning on writing a higher-half kernel?
Eventually. For now, I am focusing on getting GDT/IDT done correctly. Next I will try to enable hardware interrupts
Octocontrabass wrote:Edit: and you didn't clear DF. Its value is undefined in Multiboot but the C ABI requires it to be cleared.
I was not aware of that. Thanks for the heads up!

Best Regards,

Re: Installing GDT - Request For Review

Posted: Wed Feb 23, 2022 1:45 pm
by Octocontrabass
yasar11732 wrote:Is it supposed to be;
Yep, that's 4GB.
yasar11732 wrote:Eventually. For now, I am focusing on getting GDT/IDT done correctly. Next I will try to enable hardware interrupts
Keep in mind a higher-half kernel must enable paging before running global constructors. And you're setting up exception handlers before you enable hardware interrupts, right?