Page 1 of 2

Problems with kernel

Posted: Tue Nov 16, 2010 4:42 am
by usb2killer
im having a problem with the kernel.
im booting to my bootsector and from there jumping with protected mode to the kernel.
my problem starts then im trying to build my kernel from more then one file (.c), i don't see any errors in the compiling and linking process, but then i run it in bochs, it just ignores the functions in the other files (except main-basic.c).

this is the main-basic.c (bootsector jumps to this):

Code: Select all

#include "video-basic.h"
void main()
{
	clrscreen();
	out(0x3D5, (unsigned char)(0));
	out(0x3D4, 14);
	out(0x3D5, (unsigned char)(0 >> 8));
	print();
	for(;;);
}
this is the video-basic.c:

Code: Select all

#include "video-basic.h"

unsigned char in(unsigned short _port)
{
  // "=a" (result) means: put AL register in variable result when finished
  // "d" (_port) means: load EDX with _port
  unsigned char result;
  __asm__  ("in %%dx, %%al" : "=a" (result) : "d" (_port));
  return result;
}

void out(unsigned short _port, unsigned char _data)
{
  // "a" (_data) means: load EAX with _data
  // "d" (_port) means: load EDX with _port
  __asm__ ("out %%al, %%dx" : :"a" (_data), "d" (_port));
}

void clrscreen()
{
	unsigned char *vidmem = (unsigned char *)0xB8000;
	const long size = 80*25;
	long loop;

	// Clear visible video memory
	for (loop=0; loop<size; loop++) {
		*vidmem++ = 0;
		*vidmem++ = 0xF;
	}
}

void print()
{
  unsigned long i;
  unsigned char *vidmem = (unsigned char *)0xB8000;

  // Continue until we reach null character
  i = 0;
  while (i < 5) {
    *vidmem = 'X';
	i++;
	vidmem += 2;
  }
  
  out(0x3D4, 14);
  out(0x3D5, (unsigned char)(0));
  out(0x3D4, 15);
  out(0x3D5, (unsigned char)(i));
}
the video-basic.h:

Code: Select all

unsigned char in(unsigned short _port);
void out(unsigned short _port, unsigned char _data);
void clrscreen();
void print();

and the compiling code:

Code: Select all

nasm bootsect.asm -f bin -o build\bootsect.bin

gcc -ffreestanding -c main-basic.c -o build\main.o
gcc -c video-basic.c -o build\video.o

cd build

ld -e _main -Ttext 0x1000 -o kernel.o main.o video.o
ld -i -e _main -Ttext 0x1000 -o kernel.o main.o video.o
objcopy -R .note -R .comment -S -O binary kernel.o kernel.bin

cd ..

makeboot build\a.img build\bootsect.bin build\kernel.bin
i really dont know what to do...

Re: Problems with kernel

Posted: Tue Nov 16, 2010 4:48 am
by gerryg400

Code: Select all

ld -e _main -Ttext 0x1000 -o kernel.o main.o video.o
ld -i -e _main -Ttext 0x1000 -o kernel.o main.o video.o
I notice you're running ld twice. Why is that ?

Also, why do you use -i. It produces an incomplete link ?

Re: Problems with kernel

Posted: Tue Nov 16, 2010 5:02 am
by usb2killer
gerryg400 wrote:

Code: Select all

ld -e _main -Ttext 0x1000 -o kernel.o main.o video.o
ld -i -e _main -Ttext 0x1000 -o kernel.o main.o video.o
I notice you're running ld twice. Why is that ?

Also, why do you use -i. It produces an incomplete link ?
without the "-i", the linker show me this line:
"main.o:main-basic.c:(.text+0xa):undefined reference to '__main'"
and it doesn't produce "kernel.o".

i don't sure what that means...

Re: Problems with kernel

Posted: Tue Nov 16, 2010 5:06 am
by gerryg400
It means you have a linker error. Don't use -i. Fix the link error.

Re: Problems with kernel

Posted: Tue Nov 16, 2010 5:11 am
by usb2killer
gerryg400 wrote:It means you have a linker error. Don't use -i. Fix the link error.
how can i fix it? i dont really know....

Re: Problems with kernel

Posted: Tue Nov 16, 2010 5:22 am
by Combuster
ld -e _main -Ttext 0x1000 -o kernel.o main.o video.o
ld -i -e _main -Ttext 0x1000 -o kernel.o main.o video.o
OMG not that tutorial again. The person who wrote that should be shot and his website should be erased from history. It's broken, broken, broken and tells you things that are just utter BS.


Try the Bare Bones

Re: Problems with kernel

Posted: Tue Nov 16, 2010 5:34 am
by gerryg400
Hey usb2killer. Are you following a tutorial ? If so which one ?

Re: Problems with kernel

Posted: Tue Nov 16, 2010 8:30 am
by Combuster
Third hit on google on my first try (google "ld -i -e _main -Ttext 0x1000" - it comes after the complaints from osdev.org). Read if you must - I refuse to honour that page with a link.

Re: Problems with kernel

Posted: Tue Nov 16, 2010 1:37 pm
by usb2killer
Combuster wrote:
ld -e _main -Ttext 0x1000 -o kernel.o main.o video.o
ld -i -e _main -Ttext 0x1000 -o kernel.o main.o video.o
OMG not that tutorial again. The person who wrote that should be shot and his website should be erased from history. It's broken, broken, broken and tells you things that are just utter BS.


Try the Bare Bones
I tried the bare bones' tutorial.
this is what I made so far is:
compile.bat:

Code: Select all

nasm bootsect.asm -f bin -o build\bootsect.bin

gcc -ffreestanding -c main-basic.c -o build\main.o
gcc -c video-basic.c -o build\video.o

ld -T link.ld -o build\kernel.bin build\main.o build\video.o

makeboot build\a.img build\bootsect.bin build\kernel.bin
link.ld:

Code: Select all

SECTIONS{
    . = 0x00100000;

    .text :{
        *(.text)
    }

    .rodata ALIGN (0x1000) : {
        *(.rodata)
    }

    .data ALIGN (0x1000) : {
        *(.data)
    }

    .bss : {
        sbss = .;
        *(COMMON)
        *(.bss)
        ebss = .;
    }
}
the good news is that the linker doesn't have any error
the bad news is that the kernel crashes every time i'm trying to run it on bochs.

I think I need to modify their compiling and linking process (maybe link.ld?) because this tutorial assumes that i'm using GRUB (i'm not).
[-o<

Re: Problems with kernel

Posted: Tue Nov 16, 2010 3:01 pm
by Combuster
because this tutorial assumes that i'm using GRUB
It doesn't assume you are using grub, it tells you to use grub. Of course it will not work if you randomly mix up things. Especially not when you keep using parts of a known broken tutorial.

What do you want? If you seek a custom bootloader, then there's a bunch of resources on the wiki you should have tried and read already - If you want a working kernel, why are you not sticking to what the instructions tell you to do?

Re: Problems with kernel

Posted: Tue Nov 16, 2010 3:25 pm
by usb2killer
Combuster wrote:
because this tutorial assumes that i'm using GRUB
It doesn't assume you are using grub, it tells you to use grub. Of course it will not work if you randomly mix up things. Especially not when you keep using parts of a known broken tutorial.

What do you want? If you seek a custom bootloader, then there's a bunch of resources on the wiki you should have tried and read already - If you want a working kernel, why are you not sticking to what the instructions tell you to do?
i already build a working bootloader by my self, and i want to use it

Re: Problems with kernel

Posted: Tue Nov 16, 2010 3:41 pm
by Combuster
i already build a working bootloader by my self, and i want to use it
Next time, say so in your first post

So, if you wrote your own bootloader, you know its specifications. Therefore you know to what specifications your kernel must comply. What is that specification? Can you post proof that the kernel is following them? Realize that guesswork is not going to help anyone. If you don't understand why, read the above link.

In short: Don't think, know.

Re: Problems with kernel

Posted: Tue Nov 16, 2010 3:53 pm
by usb2killer
Combuster wrote:
i already build a working bootloader by my self, and i want to use it
Next time, say so in your first post

So, if you wrote your own bootloader, you know its specifications. Therefore you know to what specifications your kernel must comply. What is that specification? Can you post proof that the kernel is following them? Realize that guesswork is not going to help anyone. If you don't understand why, read the above link.

In short: Don't think, know.
srry... :oops:

here is the bootloader (the file's name is bootsect.asm)

Code: Select all

[BITS 16]       ; We need 16-bit intructions for Real mode
[ORG 0x7C00]    ; The BIOS loads the boot sector into memory location 0x7C00

start:
	; Update the segment registers
	mov ax, cs
	mov ds, ax
	mov es, ax

reset:                      ; Reset the floppy drive
	mov ax, 0           ;
	mov dl, 0           ; Drive=0 (=A)
	int 13h             ;
	jc reset            ; ERROR => reset again

read:
	mov ax, 0      ; ES:BX = 1000:0000
	mov es, ax          ;
	mov bx, 1000h           ;

	mov ah, 02h           ; Load disk data to ES:BX
	mov al, 02h           ; Load 5 sectors
	mov ch, 0           ; Cylinder=0
	mov cl, 02h           ; Sector=2
	mov dh, 0           ; Head=0
	mov dl, 0           ; Drive=0
	int 13h            ; Read!

	jc read             ; ERROR => Try again
	
cli

;gdt_init
xor ax, ax
mov ds, ax              ; Set DS-register to 0 - used by lgdt

;protected mode!	
mov eax, cr0            ; Copy the contents of CR0 into EAX
or eax, 1               ; Set bit 0
mov cr0, eax            ; Copy the contents of EAX into CR0

lgdt [gdt_desc]         ; Load the GDT descriptor

jmp 08h:clear_pipe

[BITS 32] 
clear_pipe:
	mov ax, 10h             ; Save data segment identifyer
	mov ds, ax              ; Move a valid data segment into the data segment register
	mov ss, ax              ; Move a valid data segment into the stack segment register
	mov esp, 090000h        ; Move the stack pointer to 090000h
	
	mov byte [ds:0B8000h], 'E'      ; Move the ASCII-code of 'P' into first video memory
	mov byte [ds:0B8001h], 1Bh      ; Assign a color code
	
	jmp 08h:01000h          ; Jump to section 08h (code), offset 01000h
	
gdt:                    ; Address for the GDT

gdt_null:               ; Null Segment
        dd 0
        dd 0

gdt_code:               ; Code segment, read/execute, nonconforming
        dw 0FFFFh
        dw 0
        db 0
        db 10011010b
        db 11001111b
        db 0

gdt_data:               ; Data segment, read/write, expand down
        dw 0FFFFh
        dw 0
        db 0
        db 10010010b
        db 11001111b
        db 0

gdt_end:                ; Used to calculate the size of the GDT

gdt_desc:                       ; The GDT descriptor
        dw gdt_end - gdt - 1    ; Limit (size)
        dd gdt                  ; Address of the GDT




times 510-($-$$) db 0           ; Fill up the file with zeros
dw 0AA55h                ; Boot sector identifyer
im sure it works. like i said in the first post, it worked with one kernel file so it should work with more...

Re: Problems with kernel

Posted: Tue Nov 16, 2010 4:25 pm
by gerryg400
Did you set ES when you got to protected mode ? Did you enable a20 ?

You're loading your kernel at 0x1000 and your bootloader is at 0x7c00. If your kernel is bigger than 0x6c00 what will happen ?

When you use objcopy to make your binary, you are assuming that _main appears at the very beginning of the file (at offset 0). Does this in fact happen ? You are making the very dangerous assumption that the entry to main will be the very first byte in the .text segment of your linked code.

Re: Problems with kernel

Posted: Wed Nov 17, 2010 1:37 am
by egos

Code: Select all

mov al,02h ; Load 5 sectors
???