GRUB + GDT + IDT = TRIPLE FAULT??

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.
ggodw000
Member
Member
Posts: 396
Joined: Wed Nov 18, 2015 3:04 pm
Location: San Jose San Francisco Bay Area
Contact:

Re: GRUB + GDT + IDT = TRIPLE FAULT??

Post by ggodw000 »

years ago, i tried to use linux for assembly, i dont remember the details but i do remember it sucked bad. i transitioned to using masm32 for windows.
key takeaway after spending yrs on sw industry: big issue small because everyone jumps on it and fixes it. small issue is big since everyone ignores and it causes catastrophy later. #devilisinthedetails
User avatar
Roman
Member
Member
Posts: 568
Joined: Thu Mar 27, 2014 3:57 am
Location: Moscow, Russia
Contact:

Re: GRUB + GDT + IDT = TRIPLE FAULT??

Post by Roman »

Brendan wrote:

Code: Select all

movl %eax,4(%esp)      # eax = address_of_gdt_ptr
Isn't it data_at(%esp + 4) = eax?
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
- Alan Kay
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: GRUB + GDT + IDT = TRIPLE FAULT??

Post by Brendan »

Hi,
Roman wrote:
Brendan wrote:

Code: Select all

movl %eax,4(%esp)      # eax = address_of_gdt_ptr
Isn't it data_at(%esp + 4) = eax?
For AT&T syntax (and not GAS in "intel syntax mode") it'd be "data_at(%esp + 4) = eax", but it is in "intel syntax mode" so it's "eax = data_at(%esp + 4)".


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
glauxosdever
Member
Member
Posts: 501
Joined: Wed Jun 17, 2015 9:40 am
Libera.chat IRC: glauxosdever
Location: Athens, Greece

Re: GRUB + GDT + IDT = TRIPLE FAULT??

Post by glauxosdever »

Hi,

ggodw000 wrote:years ago, i tried to use linux for assembly, i dont remember the details but i do remember it sucked bad. i transitioned to using masm32 for windows.
Linux for assembly? You probably meant that you tried either to write assembly code for linux, either to use a specific assembler to assemble your code. I can assume then that you didn't like either assembly code for linux, either you used GAS, which has an incomprehensive memory addressing scheme. Anyway NASM and MASM are not very different from what I have read.


Regards,
glauxosdever
ggodw000
Member
Member
Posts: 396
Joined: Wed Nov 18, 2015 3:04 pm
Location: San Jose San Francisco Bay Area
Contact:

Re: GRUB + GDT + IDT = TRIPLE FAULT??

Post by ggodw000 »

glauxosdever wrote:Hi,

ggodw000 wrote:years ago, i tried to use linux for assembly, i dont remember the details but i do remember it sucked bad. i transitioned to using masm32 for windows.
Linux for assembly? You probably meant that you tried either to write assembly code for linux, either to use a specific assembler to assemble your code. I can assume then that you didn't like either assembly code for linux, either you used GAS, which has an incomprehensive memory addressing scheme. Anyway NASM and MASM are not very different from what I have read.


Regards,
glauxosdever
i dont know which one is better, i just got used to masm32 as i had 2-3yrs experience development experience. many people here in this forum seems to use NASM, which i am thinking to explore.
when i attempted to linux, something that subtle issue keep driving me insane, i think it was like trying to mov some value into register from memory and just not working and kept tying up my whole project.
key takeaway after spending yrs on sw industry: big issue small because everyone jumps on it and fixes it. small issue is big since everyone ignores and it causes catastrophy later. #devilisinthedetails
User avatar
Quaker763
Posts: 17
Joined: Sun Mar 08, 2015 5:32 am
Location: Straya

Re: GRUB + GDT + IDT = TRIPLE FAULT??

Post by Quaker763 »

Hello,

First, I would SERIOUSLY recommend putting .intel_syntax noprefix at the top of your ASM sources. GNU AS syntax is so convoluted I'm genuinely surprised it even exists.

So now that we can use the instruction dest,src styleof assembly, I would wipe the entire GDT section and start again. Here, you can use mov eax, [esp + 4] to get your gdt struct from C and then the lgdt instruction with lgdt eax

Thanks,
Quaker763
glauxosdever
Member
Member
Posts: 501
Joined: Wed Jun 17, 2015 9:40 am
Libera.chat IRC: glauxosdever
Location: Athens, Greece

Re: GRUB + GDT + IDT = TRIPLE FAULT??

Post by glauxosdever »

Hi,

Quaker763 wrote:Hello,

First, I would SERIOUSLY recommend putting .intel_syntax noprefix at the top of your ASM sources. GNU AS syntax is so convoluted I'm genuinely surprised it even exists.

So now that we can use the instruction dest,src styleof assembly, I would wipe the entire GDT section and start again. Here, you can use mov eax, [esp + 4] to get your gdt struct from C and then the lgdt instruction with lgdt eax

Thanks,
Quaker763
First, I would seriously recommend using GAS native syntax, if you choose to use this assembler. Using non native syntax may be inconvenient and/or dangerous. Second, if you don't want to use GAS native syntax (no surprise for me), you should use another assembler instead of GAS. Currently I'm using NASM, but I'm concerned that it doesn't support architectures other than x86.


Regards,
glauxosdever
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

Re: GRUB + GDT + IDT = TRIPLE FAULT??

Post by Octacone »

Guys this is not a topic about syntax versus another syntax, from now I'll be using NASM on Linux and I don't need anything >x86 right now.

I still don't know why am I getting that error when compiling my OS, everything compiles all right and then my OS appears in QEMU and then crashes with that error. Can you please help me with that, and yes I have that null segment thingy.

-thehardcoreOS
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
Octocontrabass
Member
Member
Posts: 5587
Joined: Mon Mar 25, 2013 7:01 pm

Re: GRUB + GDT + IDT = TRIPLE FAULT??

Post by Octocontrabass »

Your code is jumping off into nowhere. You'll have to figure out what it was doing before that. This is a good time to learn some debugger skills.

Even if you aren't able to figure out what's wrong, you're more likely to get a helpful answer if you can tell us where in your code the crash occurs.
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

Re: GRUB + GDT + IDT = TRIPLE FAULT??

Post by Octacone »

Qemu: fatal: Trying to execute code outside RAM or ROM at 0x13f87dbd

Can't figure it out.
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
User avatar
iansjack
Member
Member
Posts: 4706
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: GRUB + GDT + IDT = TRIPLE FAULT??

Post by iansjack »

Sounds like a bad stack. Use your debugger to investigate it just before the error.
User avatar
osdever
Member
Member
Posts: 492
Joined: Fri Apr 03, 2015 9:41 am
Contact:

Re: GRUB + GDT + IDT = TRIPLE FAULT??

Post by osdever »

thehardcoreOS wrote:I can't use default NASM because default makefile from osdev.org doesn't allow me that I guess.

I still don't know why am I getting tripe faults and do I need to replace "eax" with some numbers/pointers?
Is there any straight forward way of fixing this or it is just that code is so broken it can't be fixed?

-thehardcoreOS
I can give you my makefile. Edit it for your OS, please.

Code: Select all

ARROW=\e[1;34m==>\e[1;37m

#U365 Makefile configuration

LSCRIPT    = link
BUILD_DIR  = build
SRC_DIR    = src
INC_DIR    = include
OS_VER     = "0.6"

#Filenames

ARCH       = i686
BINFORMAT  = elf
BITS       = 32
ISODIR     = $(BUILD_DIR)/iso/fs
GRUB_BIN   = /usr/lib/grub/i386-pc/
CROSS      = ~/opt/cross
CROSSBIN   = $(CROSS)/bin/
GAS        = $(CROSSBIN)$(ARCH)-$(BINFORMAT)-as #AT&T-syntax assembler
CC         = $(CROSSBIN)$(ARCH)-$(BINFORMAT)-gcc
IASM       = yasm #Intel-syntax assembler
BINFILE    = u365#Compiled ELF binary and ISO image name.
EMU        = qemu-system-i386
EFLAGS     = -cdrom $(BINFILE).iso
DEFS       = -DOSVER=$(OS_VER)
MKRSCFLAGS = -d $(GRUB_BIN) -o $(BINFILE).iso $(ISODIR)
CFLAGS     = -O3 -ffreestanding -Wall -Wextra -Wmaybe-uninitialized -fno-exceptions -std=gnu11 -Iinclude -c $(DEFS)
GASFLAGS   =
IASFLAGS   = -f $(BINFORMAT)$(BITS)
LDFLAGS    = -T $(LSCRIPT).ld -o $(BUILD_DIR)/bin/$(BINFILE).$(BINFORMAT) -O2 -nostdlib

# Source file names computing
# Multiboot header first
SOURCES  := $(SRC_DIR)/arch/$(ARCH)/boot.s
# Al common sources
SOURCES  += $(shell find $(SRC_DIR) -name "*.c"   -and -not -path "$(SRC_DIR)/arch/*" -type f -print)
SOURCES  += $(shell find $(SRC_DIR) -name "*.s"   -and -not -path "$(SRC_DIR)/arch/*" -type f -print)
SOURCES  += $(shell find $(SRC_DIR) -name "*.asm" -and -not -path "$(SRC_DIR)/arch/*" -type f -print)
# Architecture-dependent sources
SOURCES  += $(shell find $(SRC_DIR)/arch/$(ARCH) -name '*.c' -type f -print)
SOURCES  += $(shell find $(SRC_DIR)/arch/$(ARCH) -name '*.s' -and -not -path "$(SRC_DIR)/arch/$(ARCH)/boot.s" -type f -print)
SOURCES  += $(shell find $(SRC_DIR)/arch/$(ARCH) -name '*.asm' -type f -print)

FAKE := $(shell echo $(SOURCES))
# Object file names computing
OBJS     := $(patsubst $(SRC_DIR)/%.c,"$(BUILD_DIR)/obj/%.c.o",$(SOURCES))
OBJS     := $(patsubst $(SRC_DIR)/%.s,"$(BUILD_DIR)/obj/%.s.o",$(OBJS))
OBJS     := $(patsubst $(SRC_DIR)/%.asm,"$(BUILD_DIR)/obj/%.asm.o",$(OBJS))
#End

# target      _welcome dependencies
all:          _welcome clean directories compile link iso run
	@echo -n

# Welcome user at make call
_welcome:
	@echo -e " \e[1;33mMakefile:\e[0m \e[1;32mU365\e[0m"

compile:     _welcome clean directories _compile $(SOURCES)
#	@echo -e " $(ARROW) Compiling GDT\e[0m"
#	@$(IASM) $(SRC_DIR)/arch/$(ARCH)/gdt.s -o $(BUILD_DIR)/obj/gdt.o $(IASFLAGS)
#	@echo -e " $(ARROW) Compiling IDT\e[0m"
#	@$(IASM) $(SRC_DIR)/arch/$(ARCH)/idt.s -o $(BUILD_DIR)/obj/idt.o $(IASFLAGS)
#	@echo -e " $(ARROW) Compiling C sources\e[0m"
#	@$(CC) $(SOURCES) $(SRC_DIR)/arch/$(ARCH)/init.c $(IOBJS) $(CFLAGS) $(LDFLAGS)
	@echo -n

link:        _welcome clean directories compile
	@echo -e " $(ARROW) Linking\e[0m"
	@$(CC) $(OBJS) $(LDFLAGS)


iso:         _welcome directories
	@echo -e " $(ARROW) Generating an ISO image\e[0m"

	@echo "insmod gfxterm;  \
insmod vbe;  \
timeout=5;   \
loadfont /boot/grub/fonts/unicode.pf2;  \
set gfxmode=1024x768;  \
terminal_output gfxterm;  \
menuentry "U365 Basic System 1.0"  \
{  \
	multiboot /boot/u365.elf;  \
	boot;  \
}  \
" > $(BUILD_DIR)/iso/fs/grub.cfg

	@cp $(BUILD_DIR)/iso/fs/grub.cfg $(BUILD_DIR)/iso/fs/boot/grub/grub.cfg
	@cp $(BUILD_DIR)/bin/$(BINFILE).$(BINFORMAT) $(BUILD_DIR)/iso/fs/boot/
	@grub-mkrescue $(MKRSCFLAGS) &> /dev/null
	@echo -n

run:         _welcome
	@echo -e " $(ARROW) Booting the ISO image\e[0m"
	$(EMU) $(EFLAGS)
	@echo -n

clean:       _welcome
	@echo -e " $(ARROW) Cleaning\e[0m"
	@rm $(BUILD_DIR) *.iso -rf
	@echo -n

directories: _welcome
	@echo -e " $(ARROW) Creating build directories\e[0m"
	@mkdir -p $(BUILD_DIR)/obj/arch/$(ARCH) $(BUILD_DIR)/bin $(BUILD_DIR)/iso/fs/boot/grub $(BUILD_DIR)/iso/fs/fonts
	@echo -n

Makefile:
	@echo -e " \e[1;31mStrange make bug prevented\e[0m"

# Compilation notification - do not remove
_compile:
	@echo -e " $(ARROW) Compiling\e[0m"

# Compilation routines
%.c:          _welcome directories _compile
	@echo -e " \e[0;32m Building C file:\e[0m \e[1;32m$@\e[0m"
	@$(CC) $@ -o $(patsubst $(SRC_DIR)/%.c,$(BUILD_DIR)/obj/%.c.o,$@) $(CFLAGS)
	@echo -n

%.s:          _welcome directories _compile
	@echo -e " \e[0;32m Building GAS file:\e[0m \e[1;32m$@\e[0m"
	@$(GAS) $@ -o $(patsubst $(SRC_DIR)/%.s,$(BUILD_DIR)/obj/%.s.o,$@)
	@echo -n

%.asm:        _welcome directories _compile
	@echo -e " \e[0;32m Building IAS file:\e[0m \e[1;32m$@\e[0m"
	@$(IASM) $@ -o $(patsubst $(SRC_DIR)/%.asm,$(BUILD_DIR)/obj/%.asm.o,$@) $(IASFLAGS)
	@echo -n

Developing U365.
Source:
only testing: http://gitlab.com/bps-projs/U365/tree/testing

OSDev newbies can copy any code from my repositories, just leave a notice that this code was written by U365 development team, not by you.
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

Re: GRUB + GDT + IDT = TRIPLE FAULT??

Post by Octacone »

catnikita255 wrote:
thehardcoreOS wrote:I can't use default NASM because default makefile from osdev.org doesn't allow me that I guess.

I still don't know why am I getting tripe faults and do I need to replace "eax" with some numbers/pointers?
Is there any straight forward way of fixing this or it is just that code is so broken it can't be fixed?

-thehardcoreOS
I can give you my makefile. Edit it for your OS, please.

Code: Select all

ARROW=\e[1;34m==>\e[1;37m

#U365 Makefile configuration

LSCRIPT    = link
BUILD_DIR  = build
SRC_DIR    = src
INC_DIR    = include
OS_VER     = "0.6"

#Filenames

ARCH       = i686
BINFORMAT  = elf
BITS       = 32
ISODIR     = $(BUILD_DIR)/iso/fs
GRUB_BIN   = /usr/lib/grub/i386-pc/
CROSS      = ~/opt/cross
CROSSBIN   = $(CROSS)/bin/
GAS        = $(CROSSBIN)$(ARCH)-$(BINFORMAT)-as #AT&T-syntax assembler
CC         = $(CROSSBIN)$(ARCH)-$(BINFORMAT)-gcc
IASM       = yasm #Intel-syntax assembler
BINFILE    = u365#Compiled ELF binary and ISO image name.
EMU        = qemu-system-i386
EFLAGS     = -cdrom $(BINFILE).iso
DEFS       = -DOSVER=$(OS_VER)
MKRSCFLAGS = -d $(GRUB_BIN) -o $(BINFILE).iso $(ISODIR)
CFLAGS     = -O3 -ffreestanding -Wall -Wextra -Wmaybe-uninitialized -fno-exceptions -std=gnu11 -Iinclude -c $(DEFS)
GASFLAGS   =
IASFLAGS   = -f $(BINFORMAT)$(BITS)
LDFLAGS    = -T $(LSCRIPT).ld -o $(BUILD_DIR)/bin/$(BINFILE).$(BINFORMAT) -O2 -nostdlib

# Source file names computing
# Multiboot header first
SOURCES  := $(SRC_DIR)/arch/$(ARCH)/boot.s
# Al common sources
SOURCES  += $(shell find $(SRC_DIR) -name "*.c"   -and -not -path "$(SRC_DIR)/arch/*" -type f -print)
SOURCES  += $(shell find $(SRC_DIR) -name "*.s"   -and -not -path "$(SRC_DIR)/arch/*" -type f -print)
SOURCES  += $(shell find $(SRC_DIR) -name "*.asm" -and -not -path "$(SRC_DIR)/arch/*" -type f -print)
# Architecture-dependent sources
SOURCES  += $(shell find $(SRC_DIR)/arch/$(ARCH) -name '*.c' -type f -print)
SOURCES  += $(shell find $(SRC_DIR)/arch/$(ARCH) -name '*.s' -and -not -path "$(SRC_DIR)/arch/$(ARCH)/boot.s" -type f -print)
SOURCES  += $(shell find $(SRC_DIR)/arch/$(ARCH) -name '*.asm' -type f -print)

FAKE := $(shell echo $(SOURCES))
# Object file names computing
OBJS     := $(patsubst $(SRC_DIR)/%.c,"$(BUILD_DIR)/obj/%.c.o",$(SOURCES))
OBJS     := $(patsubst $(SRC_DIR)/%.s,"$(BUILD_DIR)/obj/%.s.o",$(OBJS))
OBJS     := $(patsubst $(SRC_DIR)/%.asm,"$(BUILD_DIR)/obj/%.asm.o",$(OBJS))
#End

# target      _welcome dependencies
all:          _welcome clean directories compile link iso run
	@echo -n

# Welcome user at make call
_welcome:
	@echo -e " \e[1;33mMakefile:\e[0m \e[1;32mU365\e[0m"

compile:     _welcome clean directories _compile $(SOURCES)
#	@echo -e " $(ARROW) Compiling GDT\e[0m"
#	@$(IASM) $(SRC_DIR)/arch/$(ARCH)/gdt.s -o $(BUILD_DIR)/obj/gdt.o $(IASFLAGS)
#	@echo -e " $(ARROW) Compiling IDT\e[0m"
#	@$(IASM) $(SRC_DIR)/arch/$(ARCH)/idt.s -o $(BUILD_DIR)/obj/idt.o $(IASFLAGS)
#	@echo -e " $(ARROW) Compiling C sources\e[0m"
#	@$(CC) $(SOURCES) $(SRC_DIR)/arch/$(ARCH)/init.c $(IOBJS) $(CFLAGS) $(LDFLAGS)
	@echo -n

link:        _welcome clean directories compile
	@echo -e " $(ARROW) Linking\e[0m"
	@$(CC) $(OBJS) $(LDFLAGS)


iso:         _welcome directories
	@echo -e " $(ARROW) Generating an ISO image\e[0m"

	@echo "insmod gfxterm;  \
insmod vbe;  \
timeout=5;   \
loadfont /boot/grub/fonts/unicode.pf2;  \
set gfxmode=1024x768;  \
terminal_output gfxterm;  \
menuentry "U365 Basic System 1.0"  \
{  \
	multiboot /boot/u365.elf;  \
	boot;  \
}  \
" > $(BUILD_DIR)/iso/fs/grub.cfg

	@cp $(BUILD_DIR)/iso/fs/grub.cfg $(BUILD_DIR)/iso/fs/boot/grub/grub.cfg
	@cp $(BUILD_DIR)/bin/$(BINFILE).$(BINFORMAT) $(BUILD_DIR)/iso/fs/boot/
	@grub-mkrescue $(MKRSCFLAGS) &> /dev/null
	@echo -n

run:         _welcome
	@echo -e " $(ARROW) Booting the ISO image\e[0m"
	$(EMU) $(EFLAGS)
	@echo -n

clean:       _welcome
	@echo -e " $(ARROW) Cleaning\e[0m"
	@rm $(BUILD_DIR) *.iso -rf
	@echo -n

directories: _welcome
	@echo -e " $(ARROW) Creating build directories\e[0m"
	@mkdir -p $(BUILD_DIR)/obj/arch/$(ARCH) $(BUILD_DIR)/bin $(BUILD_DIR)/iso/fs/boot/grub $(BUILD_DIR)/iso/fs/fonts
	@echo -n

Makefile:
	@echo -e " \e[1;31mStrange make bug prevented\e[0m"

# Compilation notification - do not remove
_compile:
	@echo -e " $(ARROW) Compiling\e[0m"

# Compilation routines
%.c:          _welcome directories _compile
	@echo -e " \e[0;32m Building C file:\e[0m \e[1;32m$@\e[0m"
	@$(CC) $@ -o $(patsubst $(SRC_DIR)/%.c,$(BUILD_DIR)/obj/%.c.o,$@) $(CFLAGS)
	@echo -n

%.s:          _welcome directories _compile
	@echo -e " \e[0;32m Building GAS file:\e[0m \e[1;32m$@\e[0m"
	@$(GAS) $@ -o $(patsubst $(SRC_DIR)/%.s,$(BUILD_DIR)/obj/%.s.o,$@)
	@echo -n

%.asm:        _welcome directories _compile
	@echo -e " \e[0;32m Building IAS file:\e[0m \e[1;32m$@\e[0m"
	@$(IASM) $@ -o $(patsubst $(SRC_DIR)/%.asm,$(BUILD_DIR)/obj/%.asm.o,$@) $(IASFLAGS)
	@echo -n


Thanks for sharing, I will see what can I make out of this. :)
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
Post Reply