OSDev.org
https://forum.osdev.org/

Setting up GDT in C ?
https://forum.osdev.org/viewtopic.php?f=1&t=32594
Page 2 of 2

Author:  MichaelPetch [ Sun Nov 26, 2017 11:44 am ]
Post subject:  Re: Setting up GDT in C ?

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:
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:
# 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:
[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

Author:  osdevnewbie [ Sun Nov 26, 2017 4:02 pm ]
Post subject:  Re: Setting up GDT in C ?

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

Author:  MichaelPetch [ Sun Nov 26, 2017 5:16 pm ]
Post subject:  Re: Setting up GDT in C ?

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.

Author:  Octocontrabass [ Sun Nov 26, 2017 6:13 pm ]
Post subject:  Re: Setting up GDT in C ?

Now would be a good time to get a proper cross-compiler, so you can avoid future issues like this.

Author:  MichaelPetch [ Sun Nov 26, 2017 6:27 pm ]
Post subject:  Re: Setting up GDT in C ?

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.

Author:  osdevnewbie [ Mon Nov 27, 2017 1:12 am ]
Post subject:  Re: Setting up GDT in C ?

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?

Author:  Octocontrabass [ Mon Nov 27, 2017 10:09 am ]
Post subject:  Re: Setting up GDT in C ?

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.

Author:  osdevnewbie [ Tue Nov 28, 2017 1:26 am ]
Post subject:  Re: Setting up GDT in C ?

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.

Page 2 of 2 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/