Is this GDT code correct/good?

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.
Post Reply
RharryR
Posts: 17
Joined: Thu Feb 11, 2016 1:15 am
Location: Milingimbi Island, Australia

Is this GDT code correct/good?

Post by RharryR »

Hey,
I'm working on my own hobby OS called 'KiteOS'. I've written some GDT (In GAS), and I was hoping you could take a look at it.
Here is the code:
gdt.s - Holds the GDT data

Code: Select all

.section .rodata

gdt_begin:

gdt_null:
.long 0
.long 0 

gdt_code:
.word 0xFFFF
.word 0x0000
.byte 0x0000
.byte 0b10011011
.byte 0b11011111
.byte 0x0000

gdt_data:
.word 0xFFFF
.word 0x0000
.byte 0x0000
.byte 0b10010011
.byte 0b11011111
.byte 0x0000

gdt_end:

.globl gdt_desc
gdt_desc:
.word gdt_end - gdt_begin - 1
.long gdt_begin
init.s - This is where I call LGDT and do other stuff

Code: Select all

.section .text

.global init
.type init, @function

.extern gdt_desc
init:
    cli
    lgdt gdt_desc
    ljmp $0x08, $reload
reload:
    movw $0x10, %ax
    movw %ax, %ds
    movw %ax, %es
    movw %ax, %fs
    movw %ax, %gs
    movw %ax, %ss
.cont_init:
    ret
Thats it. init is called on boot.
Does/should this code work? The OS boots completely fine and the kernel_main C functions is called.
Thanks for your time.
User avatar
Neroku
Posts: 24
Joined: Tue Dec 01, 2015 4:53 am

Re: Is this GDT code correct/good?

Post by Neroku »

It looks good. However, you could leave the access bit (of the access byte) just set to zero, since the CPU will set it to one when it access the corresponding segment:

Code: Select all

.section .rodata

gdt_begin:

gdt_null:
.long 0
.long 0 

gdt_code:
.word 0xFFFF
.word 0x0000
.byte 0x0000
.byte 0b10011010
.byte 0b11011111
.byte 0x0000

gdt_data:
.word 0xFFFF
.word 0x0000
.byte 0x0000
.byte 0b10010010
.byte 0b11011111
.byte 0x0000
currently working on kboot86, the Genesis of my kernel
Post Reply