just getting started

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
User avatar
richardhp
Posts: 8
Joined: Tue Feb 13, 2007 7:33 am

just getting started

Post by richardhp »

i have followed the tutorial and have managed to compile a complete kernel which uses an assembly boot loader to boot into the c kernel. currently all the kernel should do is print hello to the screen, however when i boot into it from grub it simply sits there and does nothing. the only compilation error i get is a warning that says
pointer targets in passing argument 1 of ‘puts’ differ in signedness
but i can't see how this would affect things so badly.

here is the assembly code:

; This is the kernel's entry point. We could either call main here,
; or we can use this to setup the stack or other nice stuff, like
; perhaps setting up the GDT and segments. Please note that interrupts
; are disabled at this point: More on interrupts later!
[BITS 32]
global start
start:
mov esp, _sys_stack ; This points the stack to our new stack area
jmp stublet
extern main
call main

; This part MUST be 4byte aligned, so we solve that issue using 'ALIGN 4'
ALIGN 4
mboot:
; Multiboot macros to make a few lines later more readable
MULTIBOOT_PAGE_ALIGN equ 1<<0
MULTIBOOT_MEMORY_INFO equ 1<<1
MULTIBOOT_AOUT_KLUDGE equ 1<<16
MULTIBOOT_HEADER_MAGIC equ 0x1BADB002
MULTIBOOT_HEADER_FLAGS equ MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO | MULTIBOOT_AOUT_KLUDGE
MULTIBOOT_CHECKSUM equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)
EXTERN code, bss, end

; This is the GRUB Multiboot header. A boot signature
dd MULTIBOOT_HEADER_MAGIC
dd MULTIBOOT_HEADER_FLAGS
dd MULTIBOOT_CHECKSUM

; AOUT kludge - must be physical addresses. Make a note of these:
; The linker script fills in the data for these ones!
dd mboot
dd code
dd bss
dd end
dd start

; This is an endless loop here. Make a note of this: Later on, we
; will insert an 'extern _main', followed by 'call _main', right
; before the 'jmp $'.
stublet:
extern main
call main
jmp $


; Shortly we will add code for loading the GDT right here!


; In just a few pages in this tutorial, we will add our Interrupt
; Service Routines (ISRs) right here!



; Here is the definition of our BSS section. Right now, we'll use
; it just to store the stack. Remember that a stack actually grows
; downwards, so we declare the size of the data before declaring
; the identifier '_sys_stack'
SECTION .bss
resb 8192 ; This reserves 8KBytes of memory here
_sys_stack:

is there something wrong here or is it to do with the way i compiled it. i used these scripts:

gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -c -o lib/main.o src/main.c
gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -c -o lib/scrn.o src/scrn.c
nasm -f aout -o lib/kernel.o src/kernel.s
ld -T lib/link.ld -o kernel.bin lib/main.o lib/kernel.o lib/scrn.o

what could be wrong?
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:

Post by Combuster »

I can't see anything seriously wrong from the assembly part. We probably need the rest of the sources to find the real problem.

apart from that:

Code: Select all

jmp stublet
extern main
call main 
is dead code, as well as using inline externs being questionable

Code: Select all

-fstrength-reduce -fomit-frame-pointer -finline-functions
these options are doubtful
-fomit-frame-pointer: the #1 thing not to do if you want to be able to debug your kernel
-finline-functions: this will merge functions together. Again, not for prototype code
-fstrength-reduce: not listed in the current GCC manual.
Although neither should influence the effectiveness of your code, it is wise to turn them off. If you want optimized code, use -O2 instead
"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 ]
User avatar
richardhp
Posts: 8
Joined: Tue Feb 13, 2007 7:33 am

Post by richardhp »

i thought the jmp stublet might not do anything, but i stuck it in anyway in case that was the problem. I don't have a clue what the compiler flags do, they were like that in the tutorial. I will try turning some of them off to see if it helps.

here is the c code i got off the internet:



main.c:






#include "include/system.h"

/* These define our textpointer, our background and foreground
* colors (attributes), and x and y cursor coordinates */
unsigned char *textmemptr;

int attrib = 0x0F;
int csr_x = 0, csr_y = 0;

/* Scrolls the screen */
void scroll(void)
{
unsigned blank, temp;

/* A blank is defined as a space... we need to give it
* backcolor too */
blank = 0x20 | (attrib << 8);

/* Row 25 is the end, this means we need to scroll up */
if(csr_y >= 25)
{
/* Move the current text chunk that makes up the screen
* back in the buffer by a line */
temp = csr_y - 25 + 1;
memcpy (textmemptr, textmemptr + temp * 80, (25 - temp) * 80 * 2);

/* Finally, we set the chunk of memory that occupies
* the last line of text to our 'blank' character */

memset (textmemptr + (25 - temp) * 80, blank, 80);
csr_y = 25 - 1;
}
}

/* Updates the hardware cursor: the little blinking line
* on the screen under the last character pressed! */
void move_csr(void)
{
unsigned temp;

/* The equation for finding the index in a linear
* chunk of memory can be represented by:
* Index = [(y * width) + x] */
temp = csr_y * 80 + csr_x;

/* This sends a command to indicies 14 and 15 in the
* CRT Control Register of the VGA controller. These
* are the high and low bytes of the index that show
* where the hardware cursor is to be 'blinking'. To
* learn more, you should look up some VGA specific
* programming documents. A great start to graphics:
* http://www.brackeen.com/home/vga */
outportb(0x3D4, 14);
outportb(0x3D5, temp >> 8);
outportb(0x3D4, 15);
outportb(0x3D5, temp);
}

/* Clears the screen */
void cls()
{
unsigned blank;
int i;

/* Again, we need the 'short' that will be used to
* represent a space with color */
blank = 0x20 | (attrib << 8);

/* Sets the entire screen to spaces in our current
* color */
for(i = 0; i < 25; i++)
memset (textmemptr + i * 80, blank, 80);

/* Update out virtual cursor, and then move the
* hardware cursor */
csr_x = 0;
csr_y = 0;
move_csr();
}

/* Puts a single character on the screen */
void putch(unsigned char c)
{
unsigned char *where;
unsigned att = attrib << 8;

/* Handle a backspace, by moving the cursor back one space */
if(c == 0x08)
{
if(csr_x != 0) csr_x--;
}
/* Handles a tab by incrementing the cursor's x, but only
* to a point that will make it divisible by 8 */
else if(c == 0x09)
{
csr_x = (csr_x + 8) & ~(8 - 1);
}
/* Handles a 'Carriage Return', which simply brings the
* cursor back to the margin */
else if(c == '\r')
{
csr_x = 0;
}
/* We handle our newlines the way DOS and the BIOS do: we
* treat it as if a 'CR' was also there, so we bring the
* cursor to the margin and we increment the 'y' value */
else if(c == '\n')
{
csr_x = 0;
csr_y++;
}
/* Any character greater than and including a space, is a
* printable character. The equation for finding the index
* in a linear chunk of memory can be represented by:
* Index = [(y * width) + x] */
else if(c >= ' ')
{
where = textmemptr + (csr_y * 80 + csr_x);
*where = c | att; /* Character AND attributes: color */
csr_x++;
}

/* If the cursor has reached the edge of the screen's width, we
* insert a new line in there */
if(csr_x >= 80)
{
csr_x = 0;
csr_y++;
}

/* Scroll the screen if needed, and finally move the cursor */
scroll();
move_csr();
}

/* Uses the above routine to output a string... */
void puts(unsigned char *text)
{
int i;

for (i = 0; i < strlen(text); i++)
{
putch(text);
}
}

/* Sets the forecolor and backcolor that we will use */
void settextcolor(unsigned char forecolor, unsigned char backcolor)
{
/* Top 4 bytes are the background, bottom 4 bytes
* are the foreground color */
attrib = (backcolor << 4) | (forecolor & 0x0F);
}

/* Sets our text-mode VGA pointer, then clears the screen for us */
void init_video(void)
{
textmemptr = (unsigned char *)0xB8000;
cls();
}




scrn.c:


#include "include/system.h"

/* These define our textpointer, our background and foreground
* colors (attributes), and x and y cursor coordinates */
unsigned char *textmemptr;

int attrib = 0x0F;
int csr_x = 0, csr_y = 0;

/* Scrolls the screen */
void scroll(void)
{
unsigned blank, temp;

/* A blank is defined as a space... we need to give it
* backcolor too */
blank = 0x20 | (attrib << 8);

/* Row 25 is the end, this means we need to scroll up */
if(csr_y >= 25)
{
/* Move the current text chunk that makes up the screen
* back in the buffer by a line */
temp = csr_y - 25 + 1;
memcpy (textmemptr, textmemptr + temp * 80, (25 - temp) * 80 * 2);

/* Finally, we set the chunk of memory that occupies
* the last line of text to our 'blank' character */

memset (textmemptr + (25 - temp) * 80, blank, 80);
csr_y = 25 - 1;
}
}

/* Updates the hardware cursor: the little blinking line
* on the screen under the last character pressed! */
void move_csr(void)
{
unsigned temp;

/* The equation for finding the index in a linear
* chunk of memory can be represented by:
* Index = [(y * width) + x] */
temp = csr_y * 80 + csr_x;

/* This sends a command to indicies 14 and 15 in the
* CRT Control Register of the VGA controller. These
* are the high and low bytes of the index that show
* where the hardware cursor is to be 'blinking'. To
* learn more, you should look up some VGA specific
* programming documents. A great start to graphics:
* http://www.brackeen.com/home/vga */
outportb(0x3D4, 14);
outportb(0x3D5, temp >> 8);
outportb(0x3D4, 15);
outportb(0x3D5, temp);
}

/* Clears the screen */
void cls()
{
unsigned blank;
int i;

/* Again, we need the 'short' that will be used to
* represent a space with color */
blank = 0x20 | (attrib << 8);

/* Sets the entire screen to spaces in our current
* color */
for(i = 0; i < 25; i++)
memset (textmemptr + i * 80, blank, 80);

/* Update out virtual cursor, and then move the
* hardware cursor */
csr_x = 0;
csr_y = 0;
move_csr();
}

/* Puts a single character on the screen */
void putch(unsigned char c)
{
unsigned char *where;
unsigned att = attrib << 8;

/* Handle a backspace, by moving the cursor back one space */
if(c == 0x08)
{
if(csr_x != 0) csr_x--;
}
/* Handles a tab by incrementing the cursor's x, but only
* to a point that will make it divisible by 8 */
else if(c == 0x09)
{
csr_x = (csr_x + 8) & ~(8 - 1);
}
/* Handles a 'Carriage Return', which simply brings the
* cursor back to the margin */
else if(c == '\r')
{
csr_x = 0;
}
/* We handle our newlines the way DOS and the BIOS do: we
* treat it as if a 'CR' was also there, so we bring the
* cursor to the margin and we increment the 'y' value */
else if(c == '\n')
{
csr_x = 0;
csr_y++;
}
/* Any character greater than and including a space, is a
* printable character. The equation for finding the index
* in a linear chunk of memory can be represented by:
* Index = [(y * width) + x] */
else if(c >= ' ')
{
where = textmemptr + (csr_y * 80 + csr_x);
*where = c | att; /* Character AND attributes: color */
csr_x++;
}

/* If the cursor has reached the edge of the screen's width, we
* insert a new line in there */
if(csr_x >= 80)
{
csr_x = 0;
csr_y++;
}

/* Scroll the screen if needed, and finally move the cursor */
scroll();
move_csr();
}

/* Uses the above routine to output a string... */
void puts(unsigned char *text)
{
int i;

for (i = 0; i < strlen(text); i++)
{
putch(text);
}
}

/* Sets the forecolor and backcolor that we will use */
void settextcolor(unsigned char forecolor, unsigned char backcolor)
{
/* Top 4 bytes are the background, bottom 4 bytes
* are the foreground color */
attrib = (backcolor << 4) | (forecolor & 0x0F);
}

/* Sets our text-mode VGA pointer, then clears the screen for us */
void init_video(void)
{
textmemptr = (unsigned char *)0xB8000;
cls();
}




system.h:

//#ifndef __SYSTEM_H
//#define __SYSTEM_H

/* MAIN.C */
extern unsigned char *memcpy(unsigned char *dest, const unsigned char *src, int count);
extern unsigned char *memset(unsigned char *dest, unsigned char val, int count);
extern unsigned short *memsetw(unsigned short *dest, unsigned short val, int count);
extern int strlen(unsigned char *str);
extern unsigned char inportb (unsigned short _port);
extern void outportb (unsigned short _port, unsigned char _data);


/* SCRN.C */
extern void cls();
extern void putch(unsigned char c);
extern void puts(unsigned char *str);
extern void settextcolor(unsigned char forecolor, unsigned char backcolor);
extern void init_video();
//#endif
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: just getting started

Post by pcmattman »

richardhp wrote: jmp stublet
extern main
call main

stublet:
extern main
call main
jmp $
Ummm... what on earth is this?

Your bootloader ASM should have at the beginning instead of

Code: Select all

    jmp stublet
    extern main
    call main
it should have this

Code: Select all

extern _main ; _main is the C name for main
call _main ; call it
haltonret:
hlt
jmp $
You don't need the stublet. Basically, you put the above code underneath the AOUT kludge (or the Multiboot header) and then it should run.

Remember to always put the leading underscore on C functions being called from assembly!
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re: just getting started

Post by Candy »

pcmattman wrote:Remember to always put the leading underscore on C functions being called from assembly!
Remember to make a crosscompiler so you're not haunted into believing hacks for Fortran still apply in 2007.
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:

Post by Combuster »

Code: Select all

outportb(0x3D4, 14);
outportb(0x3D5, temp >> 8 );
outportb(0x3D4, 15);
outportb(0x3D5, temp);
Are you sure you have a 100% Hardware level VGA compatible (i.e., its not some onboard/laptop chip). Bochs/qemu/msvpc will do.

----

Code: Select all

extern unsigned char *memcpy(unsigned char *dest, const unsigned char *src, int count);
extern unsigned char *memset(unsigned char *dest, unsigned char val, int count);
extern unsigned short *memsetw(unsigned short *dest, unsigned short val, int count); 
which libc are you using? probably not your own/ported one?
Like candy said, get a crosscompiler.

----

btw, have you debugged your text output?:
like

Code: Select all

void main(void) {
  char * videomemory = (char *) 0xB8000;
  videomemory[0] = ':';
  videomemory[1] = 7;
  videomemory[2] = ')';
  videomemory[3] = 7;
}
"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 ]
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: just getting started

Post by pcmattman »

Candy wrote:
pcmattman wrote:Remember to always put the leading underscore on C functions being called from assembly!
Remember to make a crosscompiler so you're not haunted into believing hacks for Fortran still apply in 2007.
Shhh... don't tell me stuff I don't wanna hear! :D

Seriously though, I couldn't be bothered making a cross-compiler, so I just live with putting leading underscores on everything...

Even so, if I ever do get around to putting together a cross-compiler (it's not actually that hard) I'll take what you've said and use it.
User avatar
richardhp
Posts: 8
Joined: Tue Feb 13, 2007 7:33 am

Post by richardhp »

i tried putting _main instead of main but the linker complained alot so i took it off again. what the heck is a cross compiler??

the computer i am running it on has an nvidia 7600 so i don't think it is a hardware issue.

also i don't know which libc i am using, how would i find out and what difference does it make?
User avatar
richardhp
Posts: 8
Joined: Tue Feb 13, 2007 7:33 am

Post by richardhp »

i just opened my output file in a hex editor, and all i get is "Hello World!" on the first line then a while bunch of zeros all the way to the end.

could it be something to do with the linker script as it seems to not be creating the binary correctly.

this is the script i am using

OUTPUT_FORMAT("binary")
ENTRY(start)
phys = 0x00100000;
SECTIONS
{
.text phys : AT(phys) {
code = .;
*(.text)
. = ALIGN(4096);
}
.data : AT(phys + (data - code))
{
data = .;
*(.data)
. = ALIGN(4096);
}
.bss : AT(phys + (bss - code))
{
bss = .;
*(.bss)
. = ALIGN(4096);
}
end = .;
}


i also put
char * videomemory = (char *) 0xB8000;
videomemory[0] = ':';
videomemory[1] = 7;
videomemory[2] = ')';
videomemory[3] = 7;
in main before anything else is called so it should at least have done something which means it probably isn't linking it correctly.
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:

Post by Combuster »

GCC Cross-Compiler <- Recommended reading
"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 ]
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post by Brynet-Inc »

Combuster wrote:GCC Cross-Compiler <- Recommended reading
I personally think he should read the whole damn wiki :lol: ...
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post by Candy »

I do believe this is another RODATA thing.

Add a bit of *(.rodata) to your .text section.
Post Reply