Problem loading the kernel (Without the Pmode enabled )

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
richi18007
Member
Member
Posts: 35
Joined: Mon Mar 07, 2011 1:41 pm

Problem loading the kernel (Without the Pmode enabled )

Post by richi18007 »

I was trying to load a small piece of code with the bootloader .
Now , the problem is everything works fine when I load a flat binary prepared by nasm to the memory address 0x2000:0000 (note that PMode is still not enabled , so if i'm wrong please do correct me while I read more about this at osdev forums)

The sequence of operations

Bootloader Up -> Loading module from sector 2 gets printed

Now , instead of the "Loading module from sector 2 gets printed" , i want to print an alphabet 'C' via a Compiled C file. This 'C' is printed with the help of asm directives in the C file (And BIOS interrupt 0x13) . But it doesn seem to work , I tried to different methods to just load the C file correctly , but it doesn work .And I am pretty sure that the issue is with the loading and linking of C code only because I can load and print an nasm prepared flat binary very easily . These are the two scripts I used for linking

link.ld

Code: Select all

OUTPUT_FORMAT("BINARY")
ENTRY(main)
phys = 0x2000;
SECTIONS
{
  .text phys : AT(phys) {
    code = .;
    *(.text)
    *(.rodata)
    . = ALIGN(4096);
  }
  .data : AT(phys + (data - code))
  {
    data = .;
    *(.data)
    . = ALIGN(4096);
  }
  .bss : AT(phys + (bss - code))
  {
    bss = .;
    *(.bss)
    . = ALIGN(4096);
  }
  end = .;
}


Then , i just added them up into a floppy disk image

Code: Select all

ld -T link.ld -o kernel.bin main.o
dd if=kernel.bin of=floppy.img seek=1 bs=512 count=1 conv=notrunc (seeking  1 because bootloader resides at sector 1 )
qemu -fda floppy.img 
Doesn work
if I replace kernel.bin with main.bin (the second stage in assembly , flat binary) ... it works

And the second script

Code: Select all

ld -e _main -Ttext 0x20000 -o kernel.o main.o 
ld -i -e _main -Ttext 0x20000 -o kernel.o main.o 
objcopy -R .note -R .comment -S -O binary kernel.o kernel.bin
dd if=kernel.bin of=floppy.img seek=1 bs=512 count=1 conv=notrunc
qemu -fda floppy.img 
Once again , this doesn work either .
Can anybody please tell me what am I doing wrong ? Please excuse me for my english .
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: Problem loading the kernel (Without the Pmode enabled )

Post by Owen »

GCC doesn't produce real mode code
User avatar
eino
Member
Member
Posts: 49
Joined: Fri Sep 16, 2011 10:00 am
Location: Finland

Re: Problem loading the kernel (Without the Pmode enabled )

Post by eino »

Check out open watcom c compiler for real mode c compiler http://www.openwatcom.org/index.php/Main_Page
I'm Eino Tuominen from Finland, a web software dev learning low level stuff and reading / trying out kernel dev
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: Problem loading the kernel (Without the Pmode enabled )

Post by Combuster »

richi18007 wrote:ld -e _main -Ttext 0x20000 -o kernel.o main.o
ld -i -e _main -Ttext 0x20000 -o kernel.o main.o
objcopy -R .note -R .comment -S -O binary kernel.o kernel.bin
Will someone DDOS osdever.net until that tutorial from hell is gone forever?
"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 ]
richi18007
Member
Member
Posts: 35
Joined: Mon Mar 07, 2011 1:41 pm

Re: Problem loading the kernel (Without the Pmode enabled )

Post by richi18007 »

So , i need to switch to Pmode before writing something fancy C code :-/ .. Wattcomm compiler , here we go.
Thanks for the quick reply btw =D>
richi18007
Member
Member
Posts: 35
Joined: Mon Mar 07, 2011 1:41 pm

Re: Problem loading the kernel (Without the Pmode enabled )

Post by richi18007 »

Owen wrote:GCC doesn't produce real mode code
Ironically it does ..

And i figured out how to get the code up and working finally. But now, the problem is in C code , I cannot make a function call. I implemented this little piece of code as a test.

Code: Select all

#ifndef _CODE16GCC_H_
#define _CODE16GCC_H_
__asm__(".code16gcc\n");
#endif
/*
 * A simple bootloader skeleton for x86, using gcc.
 *
 * Prashant Borole (boroleprashant at Google mail)
 * */
#include "code16gcc.h" 
/* XXX these must be at top */
__asm__ ("jmp main");
 
 
#define __NOINLINE  __attribute__((noinline))
#define __REGPARM   __attribute__ ((regparm(3)))
#define __NORETURN  __attribute__((noreturn))
 
/* BIOS interrupts must be done with inline assembly */
void    __NOINLINE __REGPARM print(const char   *s){
	int i ;
        for(i=0;s[i];i++)
        {
        	char ch = s[i];
        	__asm__("mov $0x0E ,  %ah");
                __asm__("xor %bh ,  %bh");
                __asm__("mov $0xEF ,  %bl");
		__asm__("mov %0 , %%al ;": : "r"(ch):"%eax");
                __asm__("or %al , %al");
                __asm__("int $0x10");

        }
        return ;
}
/* and for everything else you can use C! Be it traversing the filesystem, or verifying the kernel image etc.*/
 
void __NORETURN main(){
   print("woo hoo!\n");
  		char ch='A';
                __asm__("mov $0x0E ,  %ah");
                __asm__("xor %bh ,  %bh");
                __asm__("mov $0xEF ,  %bl");
		__asm__("mov %0 , %%al ;": : "r"(ch):"%eax");
                __asm__("or %al , %al");
                __asm__("int $0x10");
                
    while(1);
}
While it has no problem printing the char ch in main , the print function fails repeatedly. And the problem actually is

Code: Select all

it inserts a call like this mov byte ptr ds[edi] , al ; And this results in making a call which is far into the memory than what real mode can refer.
I am trying though :) ..Will be back when I have completely given up .
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Problem loading the kernel (Without the Pmode enabled )

Post by gerryg400 »

richi18007 wrote:Ironically it does ..
How is that irony ? Do you know what irony is ?
If a trainstation is where trains stop, what is a workstation ?
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Problem loading the kernel (Without the Pmode enabled )

Post by bluemoon »

do not use code16gcc
http://www.delorie.com/gnu/docs/binutils/as_270.html

it is also considered unstable
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Problem loading the kernel (Without the Pmode enabled )

Post by JamesM »

Ah the standard "I've come here for help but I know better than you" newbie attitude.

I look forward to your fall from your pedestal when you realise that GCC can not, in fact, reliably generate real mode code.
richi18007
Member
Member
Posts: 35
Joined: Mon Mar 07, 2011 1:41 pm

Re: Problem loading the kernel (Without the Pmode enabled )

Post by richi18007 »

JamesM wrote:Ah the standard "I've come here for help but I know better than you" newbie attitude.

I look forward to your fall from your pedestal when you realise that GCC can not, in fact, reliably generate real mode code.
Sorry but I did not mean offense to anyone. I only use gcc16asm till I have Pmode enabled , after that , I will use the standard 32 bit set up. Hope gcc16asm works till then. And if it doesn't work. I will switch back to assembly. My sole intention was to learn. Whether , we can make real mode 16 bit code or not. And I did learn.
Post Reply