Kernel refuses to work,damn code

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
Slasher

Kernel refuses to work,damn code

Post by Slasher »

[attachment deleted by admin]
crazybuddha

Re:Kernel refuses to work,damn code

Post by crazybuddha »

And what do you do with "kmain.bin"??
Slasher

Re:Kernel refuses to work,damn code

Post by Slasher »

i write it to the 7th sector on the first track.
it is then loaded at 0x4000 by the kernel setup code, which then sets up protected mode,load the segment selectors with a valid data selector(base 0x4000 limit 0xffff) and then uses a code selector (base 0x4000 limit 0xffff) to jump to the kernel template (kmain.bin loaded earlier at 0x4000)
crazybuddha

Re:Kernel refuses to work,damn code

Post by crazybuddha »

First, take a look at a dump of kmain.bin and make sure it is what you are expecting. AFAIK, LD doesn't produce a bare bones binary file. You have to strip all the crap out of it.

Actually, I'll save you the trouble. I stand (more or less) corrected. There is no difference (except for one byte!) between the LD binary version and an ELF file stripped down with objcopy. When this doesn't hold true, I couldn't say.

Given all that, I'll take a closer look at your code.
Slasher

Re:Kernel refuses to work,damn code

Post by Slasher »

I did that too. I used objdump -D kmain.o to get a full disassembly of the kmain.o (in coff-32 format)
i then used objcopy -R .note -R .comment -S kmain.o test.bin
to remove all those stuff you mentioned and create test.bin
i then used ndisasm -b 32 kmain.bin > kd.dis to create kd.dis a disassembly of the kmain.bin code
and ndisasm -b 32 test.bin >td.dis to create a dissassembly of test.bin

both files were identical in content and order. ???

i then read the file and performed each instruction, its okay as far as i can tell. there aren't any unexpected code in it, everything in the file is exactly in the right place and should give the desired result but unfortunately it doesn't.
once i get the kmain.o file, how do you suggest i remove all the unwanted coff-32 sections to get my binary file?
Also do you know how to tell gcc to compile kmain.o as an aout file instead of the default coff-32 format?
Thanks for your help!! :)
.bdjames

Re:Kernel refuses to work,damn code

Post by .bdjames »

You are jumping to the wrong location, try:

jmp KERNEL_CODE_SEL:4000h
Slasher

Re:Kernel refuses to work,damn code

Post by Slasher »

i've tried both
jmp KERNEL_CODE_SEL:0000h
and
jmp KERNEL_CODE_SEL:4000h
Nothing happened!!!
Are my selectors (base and limit) set properly?
is my pointer in the kernel template
(char *)vga_screen=(char *)0xb8000; set properly?
i looked in the dissambbled code i got fom ndisasm and
it put this pointer in ebx and uses

mov [ebx],byte 0x41 to reference it. should i place a selector with base 00000 in ss and ds?
Thanks!
.bdjames

Re:Kernel refuses to work,damn code

Post by .bdjames »

If you use a base of 4000 then vram is at b8000-4000!
Slasher

Re:Kernel refuses to work,damn code

Post by Slasher »

Thanks!!!! ;D i forgot about that.
I'll try it out and see what happens!!
but when i used a base of 0000h and the pointer was at 0xb8000 it still didn't work.
maybe i should test and see if the kernel template is even read by the floopy drive!
But i'll try what you just noted.
Any other ideas are warmly welcomed!!!
peace! 8)
.bdjames

Re:Kernel refuses to work,damn code

Post by .bdjames »

oic, you are loading a base of 4000<<4, and the kernel
at 0:4000.

Take out:

shl eax, 4
Slasher

Re:Kernel refuses to work,damn code

Post by Slasher »

yes!!!! I've finally seen it too.
thanks! Have to get home to try it though!
eax should be equal to 00004000h and not shl 4000h,4
men, good sight,that one escaped me for a whole week!
Thanks! I will update this once i have tried your suggestion!
crazybuddha

Re:Kernel refuses to work,damn code

Post by crazybuddha »

I decided to rewrite your code a bit, and got it to work without any problems, so generally you're on the right track. However, there are a number of pitfalls in what you have.

The biggest one of these are the 'fixups' for the GDT offsets. To spare yourself some head-banging you might want to forget all that for the nonce and use ORG instead. Then, after you get it working, muck around with the offset changes.

I presume you get all your messages okay before the C code. If not, you might want to back up some until everything before that is working as expected. You should be able to just write an attribute/character pair to the 0xb8000 to make sure you've gotten there okay.


gdtr:
dw gdt_end - gdt_start - 1
dd 0x00000000

I didn't bother to check what you do with all those fixups, but using zero for the table address seems fishy.
Slasher

Re:Kernel refuses to work,damn code

Post by Slasher »

Yes, its now working fine!!!! But i just found out some things.
1.when I link the kmain.c template with a base address of 4000h, i have to subtract this base address from all pointer to memory ie screen pointer now starts at b8000h - 4000h.
2. Also if i link it with this base address of 4000h i have to set the base address of the data selectors to 00000h
3. if i link this kmain.c template with a base address of 0000h,i have to use data selectors with base set to the address where i loaded the kernel, in this case 4000h
Has anyone out there experienced this? Also i was wondering if I enable paging, will i have to set all the selectors' base addresses to 0000h?
Thanks for all your help ;)
crazybuddha

Re:Kernel refuses to work,damn code

Post by crazybuddha »

This is because of the 'fixups', I believe. I find doing this to be kind of messy, so I use ORG in the bootsector and any secondary loader code, then all subsequent files -- which are being linked with LD -- I specify -Ttext 0x1000, or whatever the fixed start address is going to be.

If that doesn't make any sense, let me know. In setting up paging, you map the logical (i.e. virtual ) addresses to the physical, so it really doesn't matter. However, I suspect it's a little harder to deal with having base addresses not set to 0x0.

eric
Slasher

Re:Kernel refuses to work,damn code

Post by Slasher »

in the subsequent files, do you subtract this base address from the pointers? and are you using pagin?
Post Reply