Page 1 of 1

Semi broken bootloader when dealing w/ heap

Posted: Tue Jun 21, 2005 7:05 am
by Kawahee
Hello everybody, I'm very new to the OS dev scene so if I'm asking a really stupid question or this has a really obvious answer, tell me quickly and I'll leave.

I've just spent the better part of 6 hours writing my own bootloader and kernel, although heavily referenced from tutorials, it's 99% my handwritten code, and I've come accross a slight problem. Heap memory doesn't work. In my kernel.c file, if I have this:

Code: Select all

void _entry()
{
???unsigned char???*vidmem = (unsigned char *)0x0b8000;???

???vidmem[40] = '!';
???vidmem[41] = 0x07;
???for(;;);
}
It works as expected, and outputs a ! character in the 20th character of the screen. The problem is, if I move the unsigned char vidmem; outside of _entry and make it global, I get nothing.

The fact that I know nothing about asm and the GNU tools doesn't help either. I think the problem has to do with my linking methods and the bootloader. The bootloader loads the next 10 sectors of the image I'm using into 0x7e00, and then executes it in protected mode. But my linking script has the initial address at the 1mb mark or something.

I'd be really appreciative if someone could give me an answer to this problem that was in ultra-simple asm-illiterate language. That or you could make me go off and use the Linux boot script like I'm going to have to if I don't solve this problem. I have my bootloader attached and the linker script below. Tools are nasm, ld, gcc and my own file appending tool, kappend, as well as the .NET 2003 IDE.

Code: Select all

OUTPUT_FORMAT("binary") 
ENTRY(start) 
phys = 0x00100000; 
SECTIONS 
{ 
  .text phys : AT(phys) { 
    code = .; 
    *(.text) 
    . = ALIGN(4096); 
  } 
  .data : AT(phys + (data - code)) 
  { 
    data = .; 
    *(.data) 
    . = ALIGN(4096); 
  } 
  .bss : AT(phys + (bss - code)) 
  { 
    bss = .; 
    *(.bss) 
    . = ALIGN(4096); 
  } 
  end = .; 
}

Re:Semi broken bootloader when dealing w/ heap

Posted: Tue Jun 21, 2005 7:07 am
by Kawahee
Can't tell if source attached or not, I clicked preview then submit then no source thing appeared. You should use vB instead of YaBB.

Re:Semi broken bootloader when dealing w/ heap

Posted: Tue Jun 21, 2005 8:07 am
by Solar
I don't think the forum software will be changed now that we all grew accustomed to it's pro's and con's. ;)

As for your problem: When you take vidmem out of the function, it ceases being a local variable (stored in .data) and becoms a global one (stored in .rodata). Since your linker script doesn't link that section into the kernel binary... I think it's obvious from here. ;)

The BareBones have a linker script known to work.

Re:Semi broken bootloader when dealing w/ heap

Posted: Tue Jun 21, 2005 8:34 am
by Pype.Clicker
Solar wrote: it ceases being a local variable and becoms a global one. Since your linker script doesn't link that section into the kernel binary... I think it's obvious from here. ;)
okay, someone hands Solar a good blanket, he seems to be tired off (or maybe he's overheating ;) ?)

Local variable are of course stored on the stack, not in the .data section ... I doubt something like vidmem will be stored in .rodata unless you explicitly tell the compiler it's const.

My guess would be your kernel has been compiled to the wrong target address (e.g. data section is not loaded where it should) and thus the pointer extracted will get wrong.

Cross checking your bootloader ... it seems to load a maximum of 5120 bytes at physical address 7E00, while your script tells the kernel will be loaded at 1MB. That simply can't work. Consider using Grub/BareBones instead ...

Re:Semi broken bootloader when dealing w/ heap

Posted: Tue Jun 21, 2005 4:24 pm
by Kawahee
Thanks, I changed the phys = 1mb to phys = 0x00007e00 and I worked, I changed it previously to 0x7e000000 and it didn't work (because I'm retarded ;)). Thanks everybody who helped.

By the way, since I'm loading it at 7e00, and not at the 1mb mark, how much memory do I have left to play with (for the kernel), other than my 5120 byte restriction on loading my kernel?

Re:Semi broken bootloader when dealing w/ heap

Posted: Tue Jun 21, 2005 6:26 pm
by AR
It varies from computer to computer (BIOS Extended data area) but you have between about 0x500 > 0x90000 which is about 585KB.

Re:Semi broken bootloader when dealing w/ heap

Posted: Wed Jun 22, 2005 1:27 am
by Solar
Pype.Clicker wrote: okay, someone hands Solar a good blanket, he seems to be tired off (or maybe he's overheating ;) ?)
Probably both. Add to that that they took away the Cygwin installation from my office PC so I can't double-check before posting (something which I always do when I get the chance).
Local variable are of course stored on the stack, not in the .data section ...
Ah... but the value that the local variable is initialized with ends up in .data, doesn't it? (Still can't test it...)

But it seems like the problem is solved. I really should retire from posting technical OS-related stuff while I'm sans project / environment. ;)

Re:Semi broken bootloader when dealing w/ heap

Posted: Wed Jun 22, 2005 2:27 am
by distantvoices
Hm, that's a pity, Solar. A developer should have as many development utilities at hands as he wants. Period.

I simply told my boss that I need cygwin. I have installed it. Period. No one is gonna depriving me of it. *gg*

isn't the initialization stuff stored in .rodata? I'm not sure either, have to do some work ere doing testing stuff.

Re:Semi broken bootloader when dealing w/ heap

Posted: Wed Jun 22, 2005 2:48 am
by slash
Initialized variables are stored in .data section and can be altered at runtime.However .rodata is read only section and cannot be altered.As far as the problem, since the const keyword is not used the variable will go in ".data".

Re:Semi broken bootloader when dealing w/ heap

Posted: Wed Jun 22, 2005 2:52 am
by Solar
beyond infinity wrote: A developer should have as many development utilities at hands as he wants. Period.
I would second you there... but seeing how I don't need Cygwin for anything work-related, and have only ever used it for private projects in between compiler runs, I can certainly see some merit on only giving developers what they need for work.

Unfortunately it's a bunch of morons making the decission what it is you need for work. Like the {beep} who did send me to a "XML with Java" course because one of my next projects will be a SOAP service... in C++. :D

Re:Semi broken bootloader when dealing w/ heap

Posted: Wed Jun 22, 2005 3:15 am
by distantvoices
ha, it isn't different here either.

Those who have the least knack of what we are doing here take the important decisions - like intending to get the host developers away from the BS2000 mainframe to client server cobol development - not as if that would increase productivity at any rate because a mainframe developer is really and deeply used to command line tools. To add to that, they want this developed stuff to be deployed (or better:ported back) to the bs2000 mainframe with automation (thanks, where are the frog pills?)

now to the topic (for the sake of it ere someone shows intent to deprive me of my head): fine, but the *LOCAL VARIABLE* itself is stored on the stack unless you declare it either register or static. The *value* for initialization is located in .data. Or do I understand there something wrong?

Re:Semi broken bootloader when dealing w/ heap

Posted: Wed Jun 22, 2005 3:47 am
by Pype.Clicker
all will depend on the variable type. If you're loading a whole array or a struct, then yes, chances are that GCC will prepare some "static struct s _gcc_oddity={0,1,2,3,4};" and then issue "memcpy(&your_struct_var, &_gcc_odditiy,sizeof(your_struct_var));" when you just wrote "your_struct_var = { 0,1,2,3,4 };"

When talking about atomic values, however, their value will usually be hardcoded as immediates in the codeflow.

Code: Select all

void _entry()
{
   0:   55                      push   %ebp
   1:   89 e5                   mov    %esp,%ebp
   3:   83 ec 04                sub    $0x4,%esp
   
      unsigned char   *vidmem = (unsigned char *)0x0b8000;   
   6:   c7 45 fc 00 80 0b 00    movl   $0xb8000,0xfffffffc(%ebp)
   
      vidmem[40] = '!';
   d:   8b 45 fc                mov    0xfffffffc(%ebp),%eax
  10:   83 c0 28                add    $0x28,%eax
  13:   c6 00 21                movb   $0x21,(%eax)
      vidmem[41] = 0x07;
  16:   8b 45 fc                mov    0xfffffffc(%ebp),%eax
  19:   83 c0 29                add    $0x29,%eax
  1c:   c6 00 07                movb   $0x7,(%eax)
      for(;;);
  1f:   eb fe                   jmp    1f <_entry+0x1f>
  ...

Code: Select all

   unsigned char   *vidmem = (unsigned char *)0x0b8000;      
void _entry()
{
   0:   55                      push   %ebp
   1:   89 e5                   mov    %esp,%ebp
    
      vidmem[40] = '!';
   3:   a1 00 00 00 00          mov    0x0,%eax
                        4: R_386_32     vidmem
   8:   83 c0 28                add    $0x28,%eax
   b:   c6 00 21                movb   $0x21,(%eax)
      vidmem[41] = 0x07;
   e:   a1 00 00 00 00          mov    0x0,%eax
PS: my deepest condoleance to your preciousss cygwin setup.