Problems with kernel

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.
usb2killer
Posts: 15
Joined: Tue Nov 16, 2010 4:23 am

Re: Problems with kernel

Post by usb2killer »

gerryg400 wrote: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.
so according to the bootloader's requirements, what i need to do to make things up and going?
yes, im assuming that the main function starts at the first byte, if its not, what i need to do to run it from the bootloader?
why do i need to enable a20?
what value i need to put in ES when i go to protected mode and why?
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: Problems with kernel

Post by neon »

Hello,

All of the information provided should be enough to answer your question. You know your bootloaders limitations, so you need to either:

-Verify using a debugger that your entry point is indeed at the start of the binary it might not be. or:
-Prepend a header to your kernel binary that tells your bootloader where the entry point is actually at. or:
-Add file loading capability to your bootloader. Have it load and execute the entry point directly so no assumption is made (recommended)
why do i need to enable a20?
You dont need to enable A20, however you will if you want to access the HMA.
what value i need to put in ES when i go to protected mode and why?
You only set DS, SS, and CS to a valid data segment when entering pmode. The other segments are invalid so if they are referenced it will result in a #GPF.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
usb2killer
Posts: 15
Joined: Tue Nov 16, 2010 4:23 am

Re: Problems with kernel

Post by usb2killer »

OK... i think i fixed it.

i changed the name of the main function to "kmain".
im using this linking script:

Code: Select all

ENTRY(main)
phys = 0x00001000;
SECTIONS
{
  .text phys : AT(phys) {
    code = .;
    *(.text)
    *(.rodata)
    . = ALIGN(4096);
  }
  .data : AT(phys + (data - code))
  {
    data = .;
    *(.data)
    . = ALIGN(4096);
  }
  .bss : AT(phys + (bss - code))
  {
    bss = .;
    *(.bss)
    . = ALIGN(4096);
  }
  end = .;
}
and the bat file:

Code: Select all

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

gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -I./include -c -o build\main.o main-basic.c
gcc -c video-basic.c -o build\video.o
gcc -c string-basic.c -o build\string.o

ld -T link.ld -o build\kernel.o build\main.o build\video.o build\string.o
objcopy -R .note -R .comment -S -O binary build\kernel.o build\kernel.bin

makeboot build\a.img build\bootsect.bin build\kernel.bin
pause
but!!!
now im having a new problem... and I think its related to the way i compile things.
im trying to print "hello world" on the screen, but then i am trying, im having some very weird problems...

then i trying to print something with a length of 1 or 3 characters its look like the string is empty. when the length is 4-5 its normal, and when its more then 5 its crashes...
thats the way im trying to use the function.

Code: Select all

const char hello[] = "hello";
kprint(&hello);
this is the function:

Code: Select all

void kprint(char *_message)
{
  unsigned int i=0;
  unsigned char *vidmem = (unsigned char *)0xB8000;

  while (_message[i] != 0) {
    *vidmem = _message[i];
	vidmem += 2;
	i++;
  }

  out(0x3D4, 14);
  out(0x3D5, (unsigned char)(0));
  out(0x3D4, 15);
  out(0x3D5, (unsigned char)(i));
}
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Problems with kernel

Post by gerryg400 »

Why does the name (kmain) matter ?

Did you fix the other problems ? In particular, the problem that you are assuming that main begins at the first byte in your binary ?

Please post your binary file.
If a trainstation is where trains stop, what is a workstation ?
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Problems with kernel

Post by JamesM »

You're not setting the attribute byte for each character. So the colour (background, foreground, blink) it appears is completely random.
usb2killer
Posts: 15
Joined: Tue Nov 16, 2010 4:23 am

Re: Problems with kernel

Post by usb2killer »

gerryg400 wrote:Why does the name (kmain) matter ?

Did you fix the other problems ? In particular, the problem that you are assuming that main begins at the first byte in your binary ?

Please post your binary file.
i dont know why... but the compilization works when i name the main function "kmain", and all the other stuff works... so i think it start at the first byte...
JamesM wrote:You're not setting the attribute byte for each character. So the colour (background, foreground, blink) it appears is completely random.
i have another function that clear the screen to black.

a.img is the combination of bootsect.bin and kernel.bin and its runnable (im using bochs)
Attachments
build.rar
(1.1 KiB) Downloaded 92 times
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:

Re: Problems with kernel

Post by Combuster »

i think it start at the first byte...
Don't think, know. You can use objdump to find out.
"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 ]
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Problems with kernel

Post by gerryg400 »

This is what the beginning of your code looks like

Code: Select all

00000000 <.>:
       0:	83 ec 2c             	sub    $0x2c,%esp
       3:	c7 44 24 1a 68 65 6c 	movl   $0x6c6c6568,0x1a(%esp)
       a:	6c 
       b:	66 c7 44 24 1e 6f 00 	movw   $0x6f,0x1e(%esp)
      12:	e8 55 00 00 00       	call   *** clrscreen   0x6c
      17:	8d 44 24 1a          	lea    0x1a(%esp),%eax
      1b:	89 04 24             	mov    %eax,(%esp)
      1e:	e8 0a 01 00 00       	call   *** kprint 0x12d
      23:	eb fe                	jmp    0x23

Can you see the bytes from offset 7 (0x68 0x65 0x6c 0x6c) ? That's 'h', 'e', 'l', 'l'. I don't know how you did that but it doesn't doesn't look normal. It's not normal.
If a trainstation is where trains stop, what is a workstation ?
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Problems with kernel

Post by gerryg400 »

Code: Select all

const char hello[] = "hello";
kprint(&hello);
Oh I just noticed this. It's wrong. The first line is okay. But &hello is not the address of the string. It's the address of the entire array. You should say

Code: Select all

kprint(hello);
Make sure you have function prototypes for all your functions and that your code has no warnings before you run it.

And please, write an assembler stub to stick at address 0 that can call 'main' for you.
If a trainstation is where trains stop, what is a workstation ?
usb2killer
Posts: 15
Joined: Tue Nov 16, 2010 4:23 am

Re: Problems with kernel

Post by usb2killer »

i did all the things that you guys said (&hello to hello, assembler stub at the start), and still the same thing happed... what should i do?

bulid.rar - 5 character print (working)
build2.rar - 6 character print (crashing)
Attachments
build2.rar
(1.12 KiB) Downloaded 85 times
build.rar
(1.1 KiB) Downloaded 101 times
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Problems with kernel

Post by gerryg400 »

Please post your source code.
If a trainstation is where trains stop, what is a workstation ?
usb2killer
Posts: 15
Joined: Tue Nov 16, 2010 4:23 am

Re: Problems with kernel

Post by usb2killer »

gerryg400 wrote:Please post your source code.
This is the source code...
Attachments
kernel2.1.rar
(40.12 KiB) Downloaded 112 times
usb2killer
Posts: 15
Joined: Tue Nov 16, 2010 4:23 am

Re: Problems with kernel

Post by usb2killer »

usb2killer wrote:
gerryg400 wrote:Please post your source code.
This is the source code...
i just found out that the kernel is 9 sectors and im loading in the bootsector only 8... #-o
its working!!!
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Problems with kernel

Post by gerryg400 »

Well done, but...

Are you sure you're setting the segment regs (es in particular) once you're in protected mode ? gcc assumes you have.

gcc also assumes the direction bit is clear. Have you done that ?

You haven't explicitly put the stub at the start of the .text segment. In fact you haven't actually defined any segments in stub.asm. And on your link command line you haven't listed stub.o as the first file. It works but that may be a fluke of the way nasm works. You need to check that.
If a trainstation is where trains stop, what is a workstation ?
Post Reply