Page 1 of 1

Kernel Programmer Newbie seeks help with his kernel

Posted: Fri Jan 09, 2009 8:55 am
by gravity0
Hello,
I have started developing my kernel (called KRISTEN) today, as working for my thesis for my school exams.
I am currently conpiling my kernel on Linux x86_64 (but I was able to compile a 32 bit kernel as I wanted).

Following the Bare Bones tutorial on OSdev I was able to get my first characted ('A') on my Bochs screen, so i continued implementing my own functions (following the Basic C tutorial kernel on osdever, since it was cited into the Getting Started page of this site's wiki).

So I was abel to get a kernel Source like this

Code: Select all

/************************************************************************
* PROJECT:		Kristen Kernel					*
* LICENSE:		GPL - See LICENSE in the top level directory	*
* FILE:			kernel.c					*
* VERSION:		0.0.1						*
* PURPOSE:		The Main Kernel Module				*
*									*
* PROGRAMMER:		Mattias Cibien					*
************************************************************************/
/// Clears the entire text screen
void kclearscr() {
	char *videoram = (char *) 0xb8000;
	unsigned int i=0;
	while(i < (80*25*2))
	{
		videoram[i]=' ';
		i++;
		videoram[i]=0x07;
		i++;
	};
};

/// Prints a message at a difined line
unsigned int kprint(char *message, unsigned int line) 
{
	char *videoram = (char *) 0xb8000;
	unsigned int i=0;
	
	i=(line*80*2);
	
	while(*message!=0)
	{
		if(*message=='\n') // check for a new line
		{
			line++;
			i=(line*80*2);
			*message++;
		} else {
			videoram[i]=*message;
			*message++;
			i++;
			videoram[i]=0x07;
			i++;
		};
	};
	
	return(1);
};

///This is our Kernel Startup Point
void kmain( void* mbd, unsigned int magic ) {
	kclearscr();
	kprint("Welcome to KRISTEN v0.0.1\nAs you noticed, this kernel isn't able to do anything", 0);
}

But when I open bochs with the new Kernel Image I am not able to see anything (I have also changed the value of blocks as said in the tutorial).... Does anybody know where I made a mistake? If you gave any advice for my Kernel please tell me,

Regards,
Mattias

EDIT: the loader file is like the one in the OSdev C Barebone tutorial.
The output should be
Welcome to KRISTEN v0.0.1
As you noticed, this kernel isn't able to do anything

Re: Kernel Programmer Newbie seeks help with his kernel

Posted: Fri Jan 09, 2009 9:27 am
by Craze Frog
Just a hunch, are you compiling with -O2 or -O3? Then you're probably seeing the effects of some missing volatile keywords.

This is the safe way:

Code: Select all

volatile char *videoram = (volatile char*)0xB8000;

Re: Kernel Programmer Newbie seeks help with his kernel

Posted: Fri Jan 09, 2009 9:39 am
by gravity0
ok... i'll chek now the outputs of those command... -O is used for setting the debug output, isn't it?

EDIT: tried with volatile unsigned (since IMHO -1 and bros are useless) char but it still does the same.
When I use boot after kernel 200+19 (since 9256/512 = 18.07) bochs restars.

Re: Kernel Programmer Newbie seeks help with his kernel

Posted: Fri Jan 09, 2009 9:46 am
by neonek
You missed volatile keyword in mem pointer as Craze Frog said. I think you shouldn't use -O optimisation option now.

Re: Kernel Programmer Newbie seeks help with his kernel

Posted: Fri Jan 09, 2009 9:48 am
by thegamefreak0134
I'm noticing that your kernel allows the main function to exit. This is normally something you want to avoid unless you want the system to shut down, and unless the assembly following this exit is prepared to do anything to handle it, the results are rather undefined. Most likely, you're throwing an exception or something right after displaying your text, which would do something I couldn't explain. Maybe not, do you have an idle loop in your assembly after you call the kernel function?

I'd just stick a:

for (;;) {}

at the end of your main function. Yes, it's hacky, but that way you guarantee that the results of any test runs you do will stick around for you to see them. Besides, if I recall correctly, you shouldn't ever need to leave the main function, even when you shut down the PC.

Also, is *message++ really desired? Do you want to increase the value of the first character? Don't you mean message++ to increase the value of the pointer? That seems off to me, although of course I'd prefer to iterate through message as an array.

-Nick

Re: Kernel Programmer Newbie seeks help with his kernel

Posted: Fri Jan 09, 2009 9:55 am
by Craze Frog
thegamefreak0134, he said he used the barebones loader, which has a HLT after the return from main(). However, I believe you found the problem, it should be message++ without the star.

Re: Kernel Programmer Newbie seeks help with his kernel

Posted: Fri Jan 09, 2009 9:58 am
by gravity0
Maybe it is a problem of the two functions that are not decleared somewhere (in fact it's C code and it doesn't suits me like c++ but I still need to study the ctors and the dtors).

EDIT: That's true! i want to increase the character! Not the pointer. Such a newbye error. i am testing now.
thegamefreak0134: in fact using an array would be better. Thank you, but that code is not mine at all, it's there just for testing in fact, since I need to learn how to move into the world of kernel before starting it.

EDIT2: this is the new code but stills not working, i think it is aproblem of kprint func that is not found.. should I add it into the loader.s file with

Code: Select all

extern kprint
By the way... this is the new code

Code: Select all

/************************************************************************
* PROJECT:		KRISTEN Kernel					*
* LICENSE:		GPL - See LICENSE in the top level directory	*
* FILE:			kernel.c					*
* VERSION:		0.0.1						*
* PURPOSE:		The Main Kernel Module				*
*									*
* PROGRAMMER:		Mattias Cibien					*
************************************************************************/

/**------------------------Prototypes---------------------------------**/
void kprint(char message[], unsigned int line);


//This is our Kernel Startup Point
void kmain( void* mbd, unsigned int magic )
{
	kprint("KRISTEN v0.0.1", 0);
}

//Prints a Message on the screen
void kprint(char message[], unsigned int line)
{
	volatile unsigned char *videoram = (volatile unsigned char *) 0xb8000;
	int char_iterator = 0;
	int position = line*80*2;
	while (message[char_iterator] != '\0')
	{
		videoram[position] = message[char_iterator];
		position++;
		videoram[position] = 0x07;
		position++;
		char_iterator++;
	}
}