Setting up GDT in C ?

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.
MichaelPetch
Member
Member
Posts: 799
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: Setting up GDT in C ?

Post by MichaelPetch »

I had a chance to look at it. Since you are using MinGW (or Cygwin) the structure packing is done differently than non-windows platforms. To get around this problem you need to use this option with GCC: -mno-ms-bitfields. I also recommend using `-f win32` (if you continue to choose not to use a cross compiler) on Windows platforms if using MinGW/Cygwin rather than `-f elf`. You have an issue in kernel_entry.asm as well - you forgot to put the code in a section (.text). The code you archived also requires enabling the memcpy again and should be changed to

Code: Select all

memcpy((char *)gdtp.base, (char *)gdt, gdtp.limit+1);
Note the +1 on the end since you want to copy the complete 24 byte structure (not 23).Your Makefile would be altered to look like:

Code: Select all

# Default make target
all: os.img


# Build the os image
os.img: boot.bin kernel.bin
        cat boot.bin kernel.bin > os.img

# Build the kernel binary
kernel.bin: kernel_entry.o kernel.o  scrn.o gdt.o
        ld -nostdlib --file-alignment 0 --section-alignment 32 -o kernel.tmp -Ttext 0x1000 kernel_entry.o kernel.o  scrn.o gdt.o
        objcopy -O binary  kernel.tmp kernel.bin

# Build the kernel object file
kernel.o: kernel.c
        gcc -mno-ms-bitfields -ffreestanding -c kernel.c -o kernel.o

# Build the scrn object file
scrn.o: scrn.c
        gcc -mno-ms-bitfields -ffreestanding -c scrn.c -o scrn.o

# Build the gdt object file
gdt.o: gdt.c
        gcc -mno-ms-bitfields -ffreestanding -c gdt.c -o gdt.o


# Build the kernel entry object file
kernel_entry.o: kernel_entry.asm
        nasm kernel_entry.asm -f win32 -o kernel_entry.o


# Build the boot binary
boot.bin: boot.asm
        nasm -f bin -o boot.bin boot.asm

clean:
        rm -f *.o *.tmp *.bin
kernel_entry.asm should look like:

Code: Select all

[bits 32]
section .text
extern _kmain

call _kmain
jmp $

global _load_gdtr
extern _gdtp
_load_gdtr:
    lgdt [_gdtp]
    mov ax, 0x10
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    mov ss, ax
    jmp 0x08:next
next:
    ret
osdevnewbie
Member
Member
Posts: 36
Joined: Sat Mar 26, 2016 1:25 pm

Re: Setting up GDT in C ?

Post by osdevnewbie »

THANKS very much MichaelPetch for your help.
Now the code is working.
before your fix the base adress loaded in the gdtr was shifted 16bits to left!
info gdtr gives some thing like:
base 0X0000000008000000, limit 23
MichaelPetch
Member
Member
Posts: 799
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: Setting up GDT in C ?

Post by MichaelPetch »

Yes, the shifted value in the base (when I used Bochs info gdt) and the fact you had marked it packed clued me into the fact your Makefile didn't compile with Cygwin/MinGW GCC using -mno-ms-bitfields. This issue is one that has come up before on the forum.
Octocontrabass
Member
Member
Posts: 5586
Joined: Mon Mar 25, 2013 7:01 pm

Re: Setting up GDT in C ?

Post by Octocontrabass »

Now would be a good time to get a proper cross-compiler, so you can avoid future issues like this.
MichaelPetch
Member
Member
Posts: 799
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: Setting up GDT in C ?

Post by MichaelPetch »

Octocontrabass wrote:Now would be a good time to get a proper cross-compiler, so you can avoid future issues like this.
I did actually suggest it to avoid the hassles. Hope he takes our advice.
osdevnewbie
Member
Member
Posts: 36
Joined: Sat Mar 26, 2016 1:25 pm

Re: Setting up GDT in C ?

Post by osdevnewbie »

MichaelPetch wrote:I did actually suggest it to avoid the hassles. Hope he takes our advice.
Which cross-compiler for WIN host and bare metal PC target you can suggest me?
Can you open a thread /write a tuto on the wiki, please, about making use of map file (generated by ld) and bochs logout in debugging? Or can you point us for a good tutos about the subject?
Octocontrabass
Member
Member
Posts: 5586
Joined: Mon Mar 25, 2013 7:01 pm

Re: Setting up GDT in C ?

Post by Octocontrabass »

osdevnewbie wrote:Which cross-compiler for WIN host and bare metal PC target you can suggest me?
This one. Building your own cross-compiler is ideal (I prefer to use MSYS2), but there are also prebuilt toolchains available at the bottom of that page.
osdevnewbie
Member
Member
Posts: 36
Joined: Sat Mar 26, 2016 1:25 pm

Re: Setting up GDT in C ?

Post by osdevnewbie »

OK, many thanks Octocontrabass.
I've tried to build a crosscompiler for raspberry some times ago with no success, so I don't retry this experience at the moment, I'll take a prebuilt toolchain.
Post Reply