I am writing a kernel, so that i am starting with a hello world program in kernel.
I have written a hello world kernel in c++ and it compiles successfully.
But when i boot it, it does not show anything on screen.
What's wrong with this code?
link.ld
OUTPUT_FORMAT("binary")
ENTRY(start)
SECTIONS{
. = 0x00100000;
.text :{
*(.text)
}
.rodata ALIGN (0x1000) : {
*(.rodata)
}
.data ALIGN (0x1000) : {
*(.data)
}
.bss : {
sbss = .;
*(COMMON)
*(.bss)
ebss = .;
}
}
loader.asm
[BITS 32]
global start
extern _main
start:
call _main
cli
hlt
video.h
#ifndef VIDEO_H
#define VIDEO_H
class Video{
public:
Video();
~Video();
void clear();
void write(char *cp);
void put(char c);
private:
unsigned short *videomem;
unsigned int off;
unsigned int pos;
};
#endif
video.cpp
#include "Video.h"
Video::Video(){
pos = 0;
off = 0;
videomem = (unsigned short*)0xb8000;
}
Video::~Video(){}
void Video::clear(){
unsigned int i;
for(i=0;i<(80*25);i++){
videomem[i] = (unsigned short)' '|0x0700;
}
pos = 0;
off = 0;
}
void Video::write(char *cp){
char *str = cp, *ch;
for(ch=str;*ch;ch++){
put(*ch);
}
}
void Video::put(char c){
if(pos>=80){
pos = 0;
off+=80;
}
if(off>=(80*25)){
clear();
}
videomem[off+pos] = (unsigned short)c|0x0700;
pos++;
}
Kernel.cpp
#include "Video.h"
int _main(void){
Video vid;
vid.clear();
vid.write("Hello World!");
}
I am compiling it using this commands:
g++ -c video.cpp -ffreestanding -nostdlib -fno-builtin -fno-rtti -fno-exceptions
g++ -c Kernel.cpp -ffreestanding -nostdlib -fno-builtin -fno-rtti -fno-exceptions
nasm -f aout Loader.asm -o Loader.o
ld -T linker.ld -o Kernel.bin Loader.o Video.o Kernel.o
It does not give any errors.
If it is possible to debug, then please help me to how to debug.
I am booting it in virtual box.
Any help will be appreciated.
The kernel is written in the C programming language [c-language]. More precisely, the kernel is typically compiled with gcc [gcc] under -std=gnu11 [gcc-c-dialect-options]: the GNU dialect of ISO C11. clang [clang] is also supported, see docs on Building Linux with Clang/LLVM.
An emergency condition; the system is probably dead. 1. KERN_ALERT. A problem that requires immediate attention.
If you want, you can check out the OS that was used as an example in my class this semester. It's done in c
. We were using GRUB bootloader, and qemu to run stuff.
It is written pretty cleanly in increments, the first increment being precisely the "Hello world" OS, and the last increment containing everything from a complete shell, threads & processes and lots of other stuff. You can find examples of source codes, makefiles and linker scripts in there (from the simple ones to quite complex examples)
http://sourceforge.net/projects/oszur11/
I would also link to the script following the increments but that is, unfortunately, not in English.
Oh, and also, there's one increment dedicated especially to debugging there, but that's more for debugging the procedures made for your OS than debugging the OS as a whole (if I remember correctly, we were using two consoles, one with qemu and one with gdb connected to the OS running in the other one).
When a PC boots, the BIOS will load whatever is in the MBR at physical address 0x7c00 and then jumps to that address. So you should link your code starting at 0x7c00 instead of at 0x00100000.
So, changing your linker script as follows should solve the problem:
...
SECTIONS{
. = 0x7c00;
...
}
EDIT: After looking at your code, I noticed some more problems. If you're not using a bootloader, there are some things you have to set up before you can start executing 32-bit code, most importantly, enabling protected mode. Here are some tutorials to help you on your way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With