My first test on my first os is going bad

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
Xeeynamo
Posts: 5
Joined: Wed Dec 15, 2010 10:27 am

My first test on my first os is going bad

Post by Xeeynamo »

Hi, I'm new here and after follow a lot the forums and the wiki I had start to develop my custom OS with C language. Firstly the problem was with the linker of MinGW (that give me the damn "cannot perform PE operations"), I tried to install the cygwin but honestly I don't know how to configure and/or use (yes, I know that emulate the unix environment, I tried to use some commands like ls etc). I tried also to install the last release of Ubuntu but other fails with gcc go there. As last change I opened Visual Studio (I use it almost every day) and I've configured rightly the project to compile binary files and yes, finally I compiled my kernel. Bochs and VMware can boot it, but it can't able to show anything: for example Bochs emu freeze on "Booting from Floppy..." and the last console's line is [BIOS ] Booting from 0000:7c00. No errors, no warnings, nothing of nothing!

This is the source code:

Code: Select all

; boot.asm

.386			; Compile for i386 processors
.model	TINY		; memory of model?

extrn	_KMain:near	; C prototype function

.code   
org	07c00h		; BootSector
						
;----------------------- CODE SEGMENT -----------------------
main:
	jmp short start
	nop
start:
        call           _KMain
        ret
        
        END main

Code: Select all

//kernel.c

#include "stddef.h"

WORD *vmem = (WORD*)0xB8000;

void kclear()
{
    for(int i=80*25; i; i--)
        vmem[i] = 0;
}
void kprint(char *text)
{
    WORD *t_vmem = vmem;
    while(*text)
    {
        *t_vmem++ = H_WHITE | (*text++<<8);
    }
}

extern "C" void KMain()
{
	kclear();
	kprint("Hello");
	while(true);
}
And these are the batch file that I use to compile and the respective output:

Code: Select all

@del *.obj
@bintools\ml /AT /c boot.asm
@bintools\cl /Gs /c /Zl *.cpp
@bintools\link /NOD boot.obj kmain.obj
@pause
createimg.exe

Code: Select all

Microsoft (R) Macro Assembler Version 9.00.30729.01
Copyright (C) Microsoft Corporation.  All rights reserved.

 Assembling: boot.asm
boot.asm(17) : warning A4023:with /coff switch, leading underscore required for
start address : main
Microsoft (R) 32-bit C/C++ Optimizing Compiler versione 15.00.30729.01 per 80x86

Copyright (C) Microsoft Corporation.  All rights reserved.

kmain.cpp
Microsoft (R) Incremental Linker Version 9.00.30729.01
Copyright (C) Microsoft Corporation.  All rights reserved.
(bintools contains the content of bin folder of VC9 environment and createimg.exe is an little software write by me that remove the MZ sector on output .exe file and add 0x55AA on 0x1FE). Maybe the problem is the A4023 error that doesn't recognize the main on asm file or the code/compile process is wrong and I don't understand how to fix it. Sorry for not using the GCC cross compiler on Windows but (as I write on first lines of this post) I don't know how to use it and I readed wiki a lot of times without success.

Any idea or tips?
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: My first test on my first os is going bad

Post by gerryg400 »

You know that when a PC boots, it starts in real mode, right ?
If a trainstation is where trains stop, what is a workstation ?
Xeeynamo
Posts: 5
Joined: Wed Dec 15, 2010 10:27 am

Re: My first test on my first os is going bad

Post by Xeeynamo »

gerryg400 wrote:You know that when a PC boots, it starts in real mode, right ?
Yes, I know, but I was thinking that compiling the asm file as 32 bit, switch automatically on protected mode, for example on a tutorial the boot.asm was only this

Code: Select all

[BITS 32]
[EXTERN _kmain]
[GLOBAL start]

start:
	call _kmain
	cli ; Stop interrupts
	hlt ; Halt the CPU
and nothing relative to switch on protected mode (I can't use this code because I've GCC and LD problems) and 32bit data was used without problems
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: My first test on my first os is going bad

Post by gerryg400 »

Yes, I know, but I was thinking that compiling the asm file as 32 bit, switch automatically on protected mode
No.
and nothing relative to switch on protected mode (I can't use this code because I've GCC and LD problems) and 32bit data was used without problems
Without problems ? Surely the fact that it doesn't work is a problem.

The truth is that the PC boots in real mode and nothing happens automatically.
If a trainstation is where trains stop, what is a workstation ?
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: My first test on my first os is going bad

Post by xenos »

Xeeynamo wrote:for example on a tutorial the boot.asm was only this...
I guess that example used a boot loader like GRUB, which loads the kernel and switches from real mode to protected mode before executing it. But in that case you need to install GRUB on your boot medium (floppy, floppy image etc.). If you just put your code into the boot sector, the system starts in real mode and you need to perform the transition to protected mode on your own.
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
User avatar
miker00lz
Member
Member
Posts: 144
Joined: Wed Dec 08, 2010 3:16 am
Location: St. Louis, MO USA

Re: My first test on my first os is going bad

Post by miker00lz »

to the OP, if you want to understand protected mode here is a link to the best tutorial i've seen on it.

http://members.tripod.com/protected_mod ... tmode.html
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: My first test on my first os is going bad

Post by Combuster »

I tried to install the cygwin but honestly I don't know how to configure and/or use (yes, I know that emulate the unix environment, I tried to use some commands like ls etc).
Does that summarize to: "I can't read instructions"? Although spammed all over the place, apparently it needs to be said again: OS development is difficult - if you can't do relatively simple tasks like those ones then you're almost guaranteed not to be ready for OS development.
"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 ]
Xeeynamo
Posts: 5
Joined: Wed Dec 15, 2010 10:27 am

Re: My first test on my first os is going bad

Post by Xeeynamo »

Thanks to all for replies! In these days I've read some GDT's tutorials and finally my bootloader goes to protected mode but now I've a CPU crash.

Code: Select all

00224665807e[CPU0 ] interrupt(): gate descriptor is not valid sys seg (vector=0x03)
00224665807e[CPU0 ] interrupt(): gate descriptor is not valid sys seg (vector=0x0d)
00224665807e[CPU0 ] interrupt(): gate descriptor is not valid sys seg (vector=0x08)
00224665807i[CPU0 ] CPU is in protected mode (active)
00224665807i[CPU0 ] CS.d_b = 16 bit
00224665807i[CPU0 ] SS.d_b = 16 bit
00224665807i[CPU0 ] EFER   = 0x00000000
00224665807i[CPU0 ] | RAX=0000000060000011  RBX=0000000000000000
00224665807i[CPU0 ] | RCX=0000000000000000  RDX=0000000000000000
00224665807i[CPU0 ] | RSP=000000000000ffd4  RBP=0000000000000000
00224665807i[CPU0 ] | RSI=00000000000e32f8  RDI=000000000000ffac
00224665807i[CPU0 ] |  R8=0000000000000000   R9=0000000000000000
00224665807i[CPU0 ] | R10=0000000000000000  R11=0000000000000000
00224665807i[CPU0 ] | R12=0000000000000000  R13=0000000000000000
00224665807i[CPU0 ] | R14=0000000000000000  R15=0000000000000000
00224665807i[CPU0 ] | IOPL=0 id vip vif ac vm RF nt of df if tf sf zf af PF cf
00224665807i[CPU0 ] | SEG selector     base    limit G D
00224665807i[CPU0 ] | SEG sltr(index|ti|rpl)     base    limit G D
00224665807i[CPU0 ] |  CS:0000( 0004| 0|  0) 00000000 0000ffff 0 0
00224665807i[CPU0 ] |  DS:0000( 0005| 0|  0) 00000000 0000ffff 0 0
00224665807i[CPU0 ] |  SS:0000( 0005| 0|  0) 00000000 0000ffff 0 0
00224665807i[CPU0 ] |  ES:0000( 0005| 0|  0) 00000000 0000ffff 0 0
00224665807i[CPU0 ] |  FS:0000( 0005| 0|  0) 00000000 0000ffff 0 0
00224665807i[CPU0 ] |  GS:0000( 0005| 0|  0) 00000000 0000ffff 0 0
00224665807i[CPU0 ] |  MSR_FS_BASE:0000000000000000
00224665807i[CPU0 ] |  MSR_GS_BASE:0000000000000000
00224665807i[CPU0 ] | RIP=0000000000007cee (0000000000007cee)
00224665807i[CPU0 ] | CR0=0x60000011 CR2=0x0000000000000000
00224665807i[CPU0 ] | CR3=0x00000000 CR4=0x00000000
00224665807i[CPU0 ] 0x0000000000007cee>> int3  : CC
00224665807e[CPU0 ] exception(): 3rd (13) exception with no resolution, shutdown status is 00h, resetting
00224665807i[SYS  ] bx_pc_system_c::Reset(HARDWARE) called
00224665807i[CPU0 ] cpu hardware reset
I think that this is because I haven't init the IDT but initialize it go to CPU hang.

Code: Select all

00856901543i[BIOS ] Booting from 0000:7c00
00856925640i[CPU0 ] LOCK prefix unallowed (op1=0x53, attr=0x0, mod=0x0, nnn=0)
00856925644i[CPU0 ] LOCK prefix unallowed (op1=0x53, attr=0x0, mod=0x0, nnn=0)
What I can do?

Combuster wrote:Does that summarize to: "I can't read instructions"? Although spammed all over the place, apparently it needs to be said again: OS development is difficult - if you can't do relatively simple tasks like those ones then you're almost guaranteed not to be ready for OS development.
Some things are simple for me, other are very difficult (like configuring cygwin) but I want to try to develop an OS only for fun and to increase my knowledges. I won't spamming on forums that I've this and that problem, I read carefully all the guides or tutorials around the web and only when I've big difficulties I write on a forum. I hope that my approach is correct, because I want to learn and someday teach to the people that have my some old problems
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: My first test on my first os is going bad

Post by Combuster »

other are very difficult (like configuring cygwin)
That just proved my point: it is the best maintained, most tested, and about the most followed tutorial we have here. It works. If it doesn't, well I'm sorry but any University of Operating Systems won't accept people that fail a entry test like that one, which is well below the capabilities you truly need.

Please, do reconsider writing an OS. With your apparent level of expertise this will only end in frustration. Run some large-scale project of your own, and learn how to deal with guides and documents. Then try the tutorial again and you can get through it. It just doesn't work if you skip the easy part because you don't get it and then head off to do some even more difficult part.

I hope we can meet again, in a year or two, under better circumstances.



That said, if you want to solve your problem (or any reproducable crash for that matter), start by grabbing a bochs version with debugger and put that to good use: check what the processor does, compare that to what you want it to do, then find out where the difference comes from.
"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
piranha
Member
Member
Posts: 1391
Joined: Thu Dec 21, 2006 7:42 pm
Location: Unknown. Momentum is pretty certain, however.
Contact:

Re: My first test on my first os is going bad

Post by piranha »

I hope that these comments don't seem mean, but it really is true. Most of us who realize that OSdev probably wont work for someone of you skill level came to this conclusion by watching many many people fail, or by failing themselves (I am in the second category).

You do seem to be motivated though, you've got that going for you. I have no doubt that you'll be able to build an OS, just not right now. As combuster said, spend a year or two working on larger C programs and learn assembly (if you haven't already). Then do a lot of research into the CPU architecture that you will be using. Really read up before trying again.

Good luck,
-JL
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
Casm
Member
Member
Posts: 221
Joined: Sun Oct 17, 2010 2:21 pm
Location: United Kingdom

Re: My first test on my first os is going bad

Post by Casm »

Xeeynamo wrote:Some things are simple for me, other are very difficult (like configuring cygwin) but I want to try to develop an OS only for fun and to increase my knowledges. I won't spamming on forums that I've this and that problem, I read carefully all the guides or tutorials around the web and only when I've big difficulties I write on a forum. I hope that my approach is correct, because I want to learn and someday teach to the people that have my some old problems
[DELETED]

Please don't post flamebait.
Xeeynamo
Posts: 5
Joined: Wed Dec 15, 2010 10:27 am

Re: My first test on my first os is going bad

Post by Xeeynamo »

Ok, after a lot of tries I'm able to enter on protected mode and write an simple message to the screen. My code was correct but cygwin and my batch file were give me some problems.
Before I was compiling the code with this way:
1) Compile the boot.asm with nasm and the output as binary/pe/elf
2) Compile the kernel.c with the gcc of MinGW
3) Link boot.o and kernel.o with the ld of MinGW or CygWin
4) Receive an beautiful error that tell me that ld cannot perform PE operations on non PE output file

I was trying to compile the mbr and the kernel with masm and Microsoft C compilator but the CPU crashed on Bochs when I load the compiled OS (as I write on my previous post)...
...But after I change the way compiling with these steps with Ubuntu
1) Same as before
2) Same as before
3) ld -Bstatic --oformat binary -okernel.bin kernel.o -Ttext 0x10000 -Map kernel.map
4) cat boot.bin kernel.bin > os.img

Ok, the MBR work and switch the OS on protected mode, kernel is loaded and display correctly what I put on 0xB8000 but only on Bochs. On VMware it perform continuous reboots and on QEMU it freeze after entering on protected mode. I'm a bit confuse and I don't know how I can resolve these problems :(


EDIT:
I've rewrited the entire bootloader and now work also on QEMU but it crash on VMware and on a real machine from an USB drive. The CPU crash when this step is reached:

Code: Select all

jmp dword (gdt_code - gdt_table):pmode
jasorod
Posts: 2
Joined: Sun Dec 26, 2010 11:15 pm

Re: My first test on my first os is going bad

Post by jasorod »

I'm not sure what you're using for your GDT, but from the looks of the Bochs crash dump, it does not look like it's been setup correctly. All the segment descriptors are set to the null GDT segment descriptor, and you're still in 16-bit mode, even if "protected" mode is enabled by setting the first bit of CR0.

I would suggest reading the Intel Software Developer Manuals (specifically the system programmers guide, or part "3A") in order to familiarize yourself with how these data-structures, registers, and CPU features actually work, and the different features than can be enabled or disabled through their use.
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: My first test on my first os is going bad

Post by xenos »

Xeeynamo wrote:

Code: Select all

00224665807i[CPU0 ] 0x0000000000007cee>> int3  : CC
00224665807e[CPU0 ] exception(): 3rd (13) exception with no resolution, shutdown status is 00h, resetting
00224665807i[SYS  ] bx_pc_system_c::Reset(HARDWARE) called
00224665807i[CPU0 ] cpu hardware reset
I think that this is because I haven't init the IDT but initialize it go to CPU hang.

What I can do?
You are right - you need to set up an IDT before you can use any interrupts. This includes software interrupts such as the one-byte debug interrupt instruction int3. The bochs dump shows that you are trying to execute an int3, but without an IDT this causes a triple fault.
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
Xeeynamo
Posts: 5
Joined: Wed Dec 15, 2010 10:27 am

Re: My first test on my first os is going bad

Post by Xeeynamo »

YEAH, RESOLVED!!!!! Simply removed the 'ret' instruction. My bootloader called an function that loaded the gdt, set the first bit of CR0 to 1, execute 'ret' and do an jmp dword to the kernel. Simply I replace the 'ret' with a jump and now also VMware is able to execute the OS!!! It was a piece of cake, I was only a bit unlucky :P
Post Reply