OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Apr 19, 2024 6:01 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 14 posts ] 
Author Message
 Post subject: Why is multiboot information corrupted when passing to func?
PostPosted: Wed Oct 16, 2019 5:20 am 
Offline
Member
Member
User avatar

Joined: Sun Jul 21, 2019 7:34 am
Posts: 300
Hello.
This problem is probably not entirely related to the development of the OS, but it is PROBABLY not a bug in the code.
I think my problem is related to some sort of misunderstanding when working with memory.

I get a pointer to a multiboot structure and pass it to a function that copies that pointer and then uses the address of the graphics buffer to draw on the screen.

In kernel.c is connected header.h, which has a pointer to multiboot information and a function that copies it and paints over the screen

kernel.c
Code:
#include "kernel.h"

int main(unsigned long magic, multiboot_info_t* mbi)
{
    (void)magic;
    initMultibootInfo(mbi);
}


header.h
Code:
multiboot_info_t* MBI;

void initMultibootInfo(multiboot_info_t* mbiTemp)
{
    MBI = mbiTemp;
    volatile uint32_t *fb = (uint32_t *)((uint32_t) MBI->framebuffer_addr);
    uint32_t cell;

    for (cell = 0; cell < MBI->framebuffer_width * MBI->framebuffer_height; cell++)
        fb[cell] = 0x00FE01AC;
}


If I use MBI in the code - nothing works.
And if I use mbiTemp - everything works.

It turns out because of a simple copy of the pointer-nothing works.


Top
 Profile  
 
 Post subject: Re: Why is multiboot information corrupted when passing to f
PostPosted: Wed Oct 16, 2019 5:58 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5137
How are you compiling and linking your code into your kernel binary?


Top
 Profile  
 
 Post subject: Re: Why is multiboot information corrupted when passing to f
PostPosted: Wed Oct 16, 2019 6:11 am 
Offline
Member
Member
User avatar

Joined: Sun Jul 21, 2019 7:34 am
Posts: 300
Octocontrabass wrote:
How are you compiling and linking your code into your kernel binary?


I collect image so:


nasm -f elf32 ./source/kernel.asm -o kasm.o
gcc -m32 ./source/kernel.c -o kc.o
ld -m elf_i386 -T link.ld -o kernel-0 kasm.o kc.o


I have no idea why copying a pointer leads to such consequences, the code is correct and everything should be fine.
The pointer is damaged as if by itself.


Top
 Profile  
 
 Post subject: Re: Why is multiboot information corrupted when passing to f
PostPosted: Wed Oct 16, 2019 6:22 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5137
Why aren't you using a cross-compiler? Using one is strongly recommended, since it gives you a consistent development environment that anyone can duplicate

Do you have an online repository somewhere so we can see all of your code? I'm particularly interested in your linker script and your stack setup.


Top
 Profile  
 
 Post subject: Re: Why is multiboot information corrupted when passing to f
PostPosted: Wed Oct 16, 2019 6:30 am 
Offline
Member
Member

Joined: Fri Aug 26, 2016 1:41 pm
Posts: 692
Can you show how you define and set your stack (ESP)? Almost wonder if you set it up wrong and the stack is overwriting the BSS section where MBI may reside. Almost wondering if you set the stack to grow down from the beginning of stack memory and not the end.


Top
 Profile  
 
 Post subject: Re: Why is multiboot information corrupted when passing to f
PostPosted: Wed Oct 16, 2019 6:34 am 
Offline
Member
Member
User avatar

Joined: Sun Jul 21, 2019 7:34 am
Posts: 300
Octocontrabass wrote:
Why aren't you using a cross-compiler? Using one is strongly recommended, since it gives you a consistent development environment that anyone can duplicate


Isn't gcc a cross compiler?

Octocontrabass wrote:
Do you have an online repository somewhere so we can see all of your code? I'm particularly interested in your linker script and your stack setup.


linker script
Code:
/*
*  link.ld
*/
OUTPUT_FORMAT(elf32-i386)
ENTRY(start)
SECTIONS
{
   . = 0x100000;
   .text : { *(.text) }
   .data : { *(.data) }
   .bss  : { *(.bss)  }
}


Set up a stack? I didn't set up anything and I don't know what you're talking about. This may be the cause of the problem. What's it?


Top
 Profile  
 
 Post subject: Re: Why is multiboot information corrupted when passing to f
PostPosted: Wed Oct 16, 2019 6:36 am 
Offline
Member
Member

Joined: Fri Aug 26, 2016 1:41 pm
Posts: 692
Just show us kernel.asm . If you haven't set ESP (Stack pointer) that may be the cause of issues.


Top
 Profile  
 
 Post subject: Re: Why is multiboot information corrupted when passing to f
PostPosted: Wed Oct 16, 2019 6:38 am 
Offline
Member
Member
User avatar

Joined: Sun Jul 21, 2019 7:34 am
Posts: 300
MichaelPetch wrote:
Just show us kernel.asm . If you haven't set ESP (Stack pointer) that may be the cause of issues.


kernel.asm

Code:
bits 32

%define MULTIBOOT_MAGIC 0x1BADB002
%define MULTIBOOT_FLAGS (1<<0 | 1<<1 | 1<<2)

section .text
align 4
multiboot_header:
    dd MULTIBOOT_MAGIC
    dd MULTIBOOT_FLAGS
    dd -(MULTIBOOT_MAGIC + MULTIBOOT_FLAGS)
    dd 0
    dd 0
    dd 0
    dd 0
    dd 0
    dd 0
    dd 800 ; width
    dd 600 ; height
    dd 32 ; bbp

global start
extern main

start:
  cli
  mov esp, stack_space
  push ebx
  push eax
  call main
  hlt

section .bss
resb 8192
stack_space:


Top
 Profile  
 
 Post subject: Re: Why is multiboot information corrupted when passing to f
PostPosted: Wed Oct 16, 2019 6:45 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5137
mrjbom wrote:
Isn't gcc a cross compiler?

Not when it's called "gcc". You should be using something like "i686-elf-gcc". If you don't already have it, you can build it yourself by following these instructions.

I don't think it's the cause of your problem, but your linker script is missing the rodata section. You can see an example linker script here that might help.


Top
 Profile  
 
 Post subject: Re: Why is multiboot information corrupted when passing to f
PostPosted: Wed Oct 16, 2019 7:15 am 
Offline
Member
Member

Joined: Fri Aug 26, 2016 1:41 pm
Posts: 692
What if you add `-ffreestanding` option to your GCC options, and then if that doesn't work rename main to kmain (in addition to using -ffreestanding).


Top
 Profile  
 
 Post subject: Re: Why is multiboot information corrupted when passing to f
PostPosted: Wed Oct 16, 2019 8:21 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4594
Location: Chichester, UK
I'm not convinced that you are showing us the exact code and the exact instructions that you are using. (For example, you include a header file that we can't see, and you don't include a header file that we can see. Also the gcc instruction that you say you are using would result in an error.)

If you could link to a repository of all your code then it would be easier to try compiling it to see where the error is.

As an aside, I wouldn't include the definition of a function in a header file.


Top
 Profile  
 
 Post subject: Re: Why is multiboot information corrupted when passing to f
PostPosted: Wed Oct 16, 2019 8:54 am 
Offline
Member
Member
User avatar

Joined: Sun Jul 21, 2019 7:34 am
Posts: 300
iansjack wrote:
I'm not convinced that you are showing us the exact code and the exact instructions that you are using. (For example, you include a header file that we can't see, and you don't include a header file that we can see. Also the gcc instruction that you say you are using would result in an error.)

If you could link to a repository of all your code then it would be easier to try compiling it to see where the error is.

As an aside, I wouldn't include the definition of a function in a header file.


Here is a link to the repository with the source files.
I think you'll be able to compile all this and have a look.


Top
 Profile  
 
 Post subject: Re: Why is multiboot information corrupted when passing to f
PostPosted: Wed Oct 16, 2019 12:51 pm 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4594
Location: Chichester, UK
Well, I'm at a loss.

I downloaded your code and compiled it (with a little bit of extra work), and it works fine.

But it didn't compile first time because of an error with your gcc command. You need to add the -c switch to it so that it produces a simple object file. Otherwise it will try and link the program. I'm not sure why it works for you, but this may explain your strange result.

I'll repeat what I said about header files. Other than very simple functions, it is best to keep them just for declarations and to put function definitions in separate .c files.


Top
 Profile  
 
 Post subject: Re: Why is multiboot information corrupted when passing to f
PostPosted: Wed Oct 16, 2019 11:35 pm 
Offline
Member
Member
User avatar

Joined: Sun Jul 21, 2019 7:34 am
Posts: 300
iansjack wrote:
Well, I'm at a loss.

I downloaded your code and compiled it (with a little bit of extra work), and it works fine.

But it didn't compile first time because of an error with your gcc command. You need to add the -c switch to it so that it produces a simple object file. Otherwise it will try and link the program. I'm not sure why it works for you, but this may explain your strange result.

I'll repeat what I said about header files. Other than very simple functions, it is best to keep them just for declarations and to put function definitions in separate .c files.


It's funny. I recompiled the same code on a new virtual machine and everything worked, I have no idea what it was.

Thank you for saying that everything works for you, otherwise I would be looking for a non-existent problem in the code or compilation.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot], SemrushBot [Bot] and 104 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