C kernel refuses to run :(

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
plantony
Posts: 2
Joined: Wed Jan 02, 2013 12:59 am

C kernel refuses to run :(

Post 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.
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: C kernel refuses to run :(

Post 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?
plantony
Posts: 2
Joined: Wed Jan 02, 2013 12:59 am

Re: C kernel refuses to run :(

Post 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?).
User avatar
Griwes
Member
Member
Posts: 374
Joined: Sat Jul 30, 2011 10:07 am
Libera.chat IRC: Griwes
Location: Wrocław/Racibórz, Poland
Contact:

Re: C kernel refuses to run :(

Post by Griwes »

To stop you from blindly copying it, for example. Or because the tutorial is lame.
Reaver Project :: Repository :: Ohloh project page
<klange> This is a horror story about what happens when you need a hammer and all you have is the skulls of the damned.
<drake1> as long as the lock is read and modified by atomic operations
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: C kernel refuses to run :(

Post 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 :)
User avatar
BMW
Member
Member
Posts: 286
Joined: Mon Nov 05, 2012 8:31 pm
Location: New Zealand

Re: C kernel refuses to run :(

Post 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
Currently developing Lithium OS (LiOS).

Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
User avatar
bubach
Member
Member
Posts: 1223
Joined: Sat Oct 23, 2004 11:00 pm
Location: Sweden
Contact:

Re: C kernel refuses to run :(

Post by bubach »

BMW: Your signature should be killed with fire before it lays eggs! :shock:
"Simplicity is the ultimate sophistication."
http://bos.asmhackers.net/ - GitHub
User avatar
Griwes
Member
Member
Posts: 374
Joined: Sat Jul 30, 2011 10:07 am
Libera.chat IRC: Griwes
Location: Wrocław/Racibórz, Poland
Contact:

Re: C kernel refuses to run :(

Post by Griwes »

xor is actually slower than mov, but takes less space in binary (that's the reason why it is used in bootloaders).
Reaver Project :: Repository :: Ohloh project page
<klange> This is a horror story about what happens when you need a hammer and all you have is the skulls of the damned.
<drake1> as long as the lock is read and modified by atomic operations
Post Reply