OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Mar 29, 2024 3:18 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 23 posts ]  Go to page Previous  1, 2
Author Message
 Post subject: Re: Setting up GDT in C ?
PostPosted: Sun Nov 26, 2017 11:44 am 
Offline
Member
Member

Joined: Fri Aug 26, 2016 1:41 pm
Posts: 671
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


Top
 Profile  
 
 Post subject: Re: Setting up GDT in C ?
PostPosted: Sun Nov 26, 2017 4:02 pm 
Offline
Member
Member

Joined: Sat Mar 26, 2016 1:25 pm
Posts: 36
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


Top
 Profile  
 
 Post subject: Re: Setting up GDT in C ?
PostPosted: Sun Nov 26, 2017 5:16 pm 
Offline
Member
Member

Joined: Fri Aug 26, 2016 1:41 pm
Posts: 671
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.


Top
 Profile  
 
 Post subject: Re: Setting up GDT in C ?
PostPosted: Sun Nov 26, 2017 6:13 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5103
Now would be a good time to get a proper cross-compiler, so you can avoid future issues like this.


Top
 Profile  
 
 Post subject: Re: Setting up GDT in C ?
PostPosted: Sun Nov 26, 2017 6:27 pm 
Offline
Member
Member

Joined: Fri Aug 26, 2016 1:41 pm
Posts: 671
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.


Top
 Profile  
 
 Post subject: Re: Setting up GDT in C ?
PostPosted: Mon Nov 27, 2017 1:12 am 
Offline
Member
Member

Joined: Sat Mar 26, 2016 1:25 pm
Posts: 36
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?


Top
 Profile  
 
 Post subject: Re: Setting up GDT in C ?
PostPosted: Mon Nov 27, 2017 10:09 am 
Offline
Member
Member

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


Top
 Profile  
 
 Post subject: Re: Setting up GDT in C ?
PostPosted: Tue Nov 28, 2017 1:26 am 
Offline
Member
Member

Joined: Sat Mar 26, 2016 1:25 pm
Posts: 36
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 23 posts ]  Go to page Previous  1, 2

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 187 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group