INT6 when trying to load GRUB module

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
User avatar
pieman2201
Posts: 4
Joined: Sun Jun 12, 2016 9:42 pm

INT6 when trying to load GRUB module

Post by pieman2201 »

First post here, apologies if something is wrong.

I'm trying to load a GRUB module as a very simple program. However, when calling it I get an INT6 "Invalid Opcode" exception. Everything seems in order, however, and I don't know what I'm doing wrong. My code is below.

Here is the "program" which I compile into a flat binary:

Code: Select all

mov eax, 0xDEADBEEF
loop:
    jmp loop
Here is where I call my C code from Assembly:

Code: Select all

extern kmain
push ebx
call kmain
Here is where I attempt to call the program from my C code:

Code: Select all

#include "multiboot.h"

//including other stuff, functions, etc.

int kmain(unsigned int ebx){
    //load gdt, setup interrupts, etc.

    puts("Hello, world!");

    multiboot_info_t *mb_info = (multiboot_info_t *)ebx;
    void (*start_program)();

    unsigned int module_address = mb_info->mods_addr;
    unsigned int module_count = mb_info->mods_count;
    
    if (module_count == 1){
        start_program = (void *)module_address;
        start_program();
    }

    return 0;
}
If you need anything else I would be happy to supply it.
Any help is appreciated. Thanks!
Last edited by pieman2201 on Mon Jun 13, 2016 5:14 pm, edited 2 times in total.
You attempt things that you do not even plan because of your extreme stupidity.
User avatar
iansjack
Member
Member
Posts: 4706
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: IRQ6 when trying to load GRUB module

Post by iansjack »

Here is the "program" which I compile into a flat binary
GRUB modules are Elf-format files, not flat binaries.
User avatar
BrightLight
Member
Member
Posts: 901
Joined: Sat Dec 27, 2014 9:11 am
Location: Maadi, Cairo, Egypt
Contact:

Re: IRQ6 when trying to load GRUB module

Post by BrightLight »

Undefined opcode exception is INT 6, not IRQ 6.
You know your OS is advanced when you stop using the Intel programming guide as a reference.
User avatar
iansjack
Member
Member
Posts: 4706
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: IRQ6 when trying to load GRUB module

Post by iansjack »

You may want to read the following with regard to writing GRUB modules: http://blog.fpmurphy.com/2010/06/grub2- ... output=pdf
User avatar
pieman2201
Posts: 4
Joined: Sun Jun 12, 2016 9:42 pm

Re: INT6 when trying to load GRUB module

Post by pieman2201 »

iansjack wrote:GRUB modules are Elf-format files, not flat binaries.
I'm using https://littleosbook.github.io/#loading ... al-program as an outline for this, and it says to make a flat binary. Is this wrong?
You attempt things that you do not even plan because of your extreme stupidity.
User avatar
pieman2201
Posts: 4
Joined: Sun Jun 12, 2016 9:42 pm

Re: IRQ6 when trying to load GRUB module

Post by pieman2201 »

omarrx024 wrote:Undefined opcode exception is INT 6, not IRQ 6.
Apologies, corrected. Thanks.
You attempt things that you do not even plan because of your extreme stupidity.
User avatar
iansjack
Member
Member
Posts: 4706
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: INT6 when trying to load GRUB module

Post by iansjack »

pieman2201 wrote:
iansjack wrote:GRUB modules are Elf-format files, not flat binaries.
I'm using https://littleosbook.github.io/#loading ... al-program as an outline for this, and it says to make a flat binary. Is this wrong?
I must admit that I've never tried the procedure outlined in that book. I'd suggest that you single-step through the code to see where it hits the invalid opcode. It should then be fairly obvious what is going wrong.

But if you want to run a program in kernel mode, why load it as a GRUB module. Why not just compile it in as part of the kernel?
Boris
Member
Member
Posts: 145
Joined: Sat Nov 07, 2015 3:12 pm

Re: INT6 when trying to load GRUB module

Post by Boris »

You are trying to read a multi boot module, not a grub module.

Multi boot modules can be anything . A binary, a zip file, or even a MP3.

If you are not doing a flat file, chances is you are trying to execute the start of an ELF file , which has a header starting with "ELF". You are trying to tell your CPU to execute an ASCII string.

Doing a flat binary removes any complicated part of the module , leaving only a valid stream for the CPU. It seems nice. But it has also restrictions :

* Your module has to be position independent . Because grub will put it at a random location. You are to use only relative addresses.
* Which means , it will be *VERy* difficult to create global variables in C.
* your module won't be able to access your kernel symbols, except if you manually give it pointers which you won't be able to store due to the lack of globals.

So, I would avoid doing flat binaries.
User avatar
pieman2201
Posts: 4
Joined: Sun Jun 12, 2016 9:42 pm

Re: INT6 when trying to load GRUB module

Post by pieman2201 »

Boris wrote:You are trying to read a multi boot module, not a grub module.

Multi boot modules can be anything . A binary, a zip file, or even a MP3.

If you are not doing a flat file, chances is you are trying to execute the start of an ELF file , which has a header starting with "ELF". You are trying to tell your CPU to execute an ASCII string.

Doing a flat binary removes any complicated part of the module , leaving only a valid stream for the CPU. It seems nice. But it has also restrictions :

* Your module has to be position independent . Because grub will put it at a random location. You are to use only relative addresses.
* Which means , it will be *VERy* difficult to create global variables in C.
* your module won't be able to access your kernel symbols, except if you manually give it pointers which you won't be able to store due to the lack of globals.

So, I would avoid doing flat binaries.
I know that flat binaries aren't a permanent solution, but I just wanted to try this out. Since something is broken, I'm trying to figure out why it doesn't work in order to prevent possibly having to fix a lot of stuff down the road.
You attempt things that you do not even plan because of your extreme stupidity.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: IRQ6 when trying to load GRUB module

Post by Combuster »

iansjack wrote:GRUB modules are Elf-format files, not flat binaries.
That is not true.
Multiboot 2 wrote:@item boot module
Other auxiliary files that a boot loader loads into memory along with
an OS image, but does not interpret in any way other than passing their
locations to the operating system when it is invoked.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: IRQ6 when trying to load GRUB module

Post by xenos »

Combuster wrote:
iansjack wrote:GRUB modules are Elf-format files, not flat binaries.
That is not true.
Actually it is true: http://wiki.osdev.org/Writing_GRUB2_Modules
But the OP meant a multiboot module (which, as you correctly stated, can be anything), not a GRUB module (which is Elf).
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
User avatar
iansjack
Member
Member
Posts: 4706
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: INT6 when trying to load GRUB module

Post by iansjack »

You're right, I took the OP at his word when he said he was trying to use a GRUB module. The article I linked to is clearly not applicable to his real requirements.
Post Reply