Page 1 of 1

C kernel refuses to run :(

Posted: Wed Jan 02, 2013 1:14 am
by plantony
I decided to make a simple C kernel (hobbyist reasons).

The code I used for both the bootloader and the kernel is available at:
http://code.google.com/p/onyxkernel/wiki/FirstStep

I compile it like this:

Code: Select all

gcc -m32 -ffreestanding -fno-builtin -nostdlib -c *.c
nasm -f elf loader.asm -o loader.o
ld -Ttext 0x1000 -melf_i386 -o kernel.bin loader.o main.o video.o 

nasm -f bin boot.asm -o boot.bin

cat boot.bin kernel.bin /dev/zero | dd iflag=fullblock bs=512 count=2880 of=floppy.img
But, the OS fails to run. I've tested it in VBox and Bochs. In VBox the cursor just freezes and the code fails to execute (the OS doesn't print anything). In Bochs, the emulator gets stuck in a kind of loop, where it fails with "3rd exception with no resolution" and then resets, only to have to same thing happen again. I've tried various different methods of compiling my kernel, but none of them seem to work. One thing I've also tried to to use QEMU to emulate my OS, but I am running it inside a linux VirtualBox guest (which might affect the results). When I run the kernel (which uses the QEMU bootloader) using the -kernel parameter it fails with

Code: Select all

qemu: fatal: Trying to execute code outside RAM or ROM at 0x000a0000
When I try and run the actual floppy image, it becomes stuck trying to load my floppy image, writing "pflash_write: Unimplemented flash cmd sequence" to the console.

Re: C kernel refuses to run :(

Posted: Wed Jan 02, 2013 1:25 am
by bluemoon
plantony wrote: The code I used for both the bootloader and the kernel is available at:
http://code.google.com/p/onyxkernel/wiki/FirstStep
I take a look on the code in the tutorial:

Code: Select all

int puts( char *message )
{
  int length;
  while(*message)
  {
    putc(*message++);
    length++;
  }
  return length;
}
Then I have a doubt on the code quality (Can you not spot the un-initialized length?)
And I read, tutorial bootloader based off of: http://osdever.net/tutorials/brunmar/tutorial_03.php
Ah, that totally explain it. Stop using the tutorial from hell.

The solution is look at Main_Page

ps. Did I call that a tutorial? No, it's not, it's just code dump with no explanation on why and what it is doing, and rely on specific assumptions. And how can anyone really code both MOV AX, 0 and XOR AX, AX just few lines apart?

Re: C kernel refuses to run :(

Posted: Wed Jan 02, 2013 1:35 am
by plantony
Thanks. I had noticed that length was not initialized with a value, but I had assumed that the code would work (why would a tutorial have non-functioning code?).

Re: C kernel refuses to run :(

Posted: Wed Jan 02, 2013 6:51 am
by Griwes
To stop you from blindly copying it, for example. Or because the tutorial is lame.

Re: C kernel refuses to run :(

Posted: Wed Jan 02, 2013 8:59 am
by AJ
bluemoon wrote:And how can anyone really code both MOV AX, 0 and XOR AX, AX just few lines apart?
In case the register gets trashed by cosmic radiation between the instructions :)

Re: C kernel refuses to run :(

Posted: Sun Jan 06, 2013 9:10 pm
by BMW
AJ wrote:
bluemoon wrote:And how can anyone really code both MOV AX, 0 and XOR AX, AX just few lines apart?
In case the register gets trashed by cosmic radiation between the instructions :)
no but why doesn't the noob use xor ax,ax in both instances... faster

Re: C kernel refuses to run :(

Posted: Mon Jan 07, 2013 3:10 am
by bubach
BMW: Your signature should be killed with fire before it lays eggs! :shock:

Re: C kernel refuses to run :(

Posted: Mon Jan 07, 2013 3:36 am
by Griwes
xor is actually slower than mov, but takes less space in binary (that's the reason why it is used in bootloaders).