Booting sequence

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
kenneth_phough
Member
Member
Posts: 106
Joined: Sun Sep 18, 2005 11:00 pm
Location: Williamstown, MA; Worcester, MA; Yokohama, Japan
Contact:

Booting sequence

Post by kenneth_phough »

Currently I am working on my bootsector and I came across a few questions.

At this point, my bootsector just enables the A20 line. But after this can I go ahead and load my kernel like in Mr.Xsism's Xosdev Chapter 1? or would I first need to reset the disk system first?

And my last question is on the GDT. As a beginner is the following plan to hard?
1)System Code Segment
2)System Data Segment
3)User Code Segment
4)User Data Segment
5)Stack Segment

Thanks,
Kenneth
User avatar
JAAman
Member
Member
Posts: 880
Joined: Wed Oct 27, 2004 11:00 pm
Location: WA

Post by JAAman »

havent read that tut, but for the GDT question


that is quite simple -- though most people simply use the data segment for the stack (though this is not required) -- note that you will need a stack segment for each ring you use -- at least one for ring0 and 1 for ring3 (unless you use the data segment for one, but not the other)


there are advantages to using a separate stack segment (iirc, clicker32 does this), but it will make it harder to use LMode if you choose to (or port into LMode later)

advantages:
easier to handle kernel stack overflow (a stack fault is generated instead of a page fault -- stack fault can be handled without a valid stack)

disadvantages:
will not work in LMode
difficult to use with most C compilers (which assume DS.base = SS.base) -- though this can be avoided, it is more complicated


personally, i use a very simple flat mode: (ignoring, for now, TSS entries, and entries reserved for VBE/VMode/emu16/etc)

0 = null selector
1 = kernel CS -- 4GB, 0-based
2 = kernel DS/SS -- 4GB, 0-based
3 = user CS -- 4GB, 0-based
4 = user DS/SS -- 4GB, 0-based
User avatar
kenneth_phough
Member
Member
Posts: 106
Joined: Sun Sep 18, 2005 11:00 pm
Location: Williamstown, MA; Worcester, MA; Yokohama, Japan
Contact:

Post by kenneth_phough »

Thank you. I decided to use your GDT layout.
I encountered some problems trying to get my bootloader to enter protected mode.
The following is the GDT layout:

Code: Select all

gdt:
.gdt_kcsd  ;;Kernel code segment.
dw 0xFFFF  ;;Limit 15:00
dw 0  ;;Base 15:00
db 0  ;;Base 23:16
db 10011010b  ;;P, DPL, S, Type - execute/read
db 11001111b  ;;G, D/B, L, AVL, Seg. Limit 19:16 - 0x0F
db 0  ;;Base 31:24

.gdt_kdsd  ;;Kernel data/stack segment.
dw 0xFFFF  ;;Limit 15:00
dw 0  ;;Base 15:00
db 0  ;;Base 23:16
db 10010010b  ;;P, DPL, S, Type - write/read
db 11001111b  ;;G, D/B, L, AVL, Seg. Limit 19:16 - 0x0F
db 0  ;;Base 31:24

.gdt_ucsd  ;;User code segment.
dw 0xFFFF  ;;Limit 15:00
dw 0  ;;Base 15:00
db 0  ;;Base 23:16
db 11111010b  ;;P, DPL, S, Type - execute/read
db 11001111b  ;;G, D/B, L, AVL, Seg. Limit 19:16 - 0x0F
db 0  ;;Base 31:24

.gdt_udsd  ;;User data/code segment.
dw 0xFFFF  ;;Limit 15:00
dw 0  ;;Base 15:00
db 0  ;;Base 31:24
db 11110010b  ;;P, DPL, S, Type - write/read
db 11001111b  ;;G, D/B, L, AVL, Seg. Limit 19:16 - 0x0F
db 0  ;;Base 31:24

gdt_end:  ;;End of GDT

gdtr:
dw gdt_end-gdt-1
dd gdt
and the code to go into protected mode is:

Code: Select all

cli

lgdt [gdtr]

mov eax, cr0
or al, 1
mov cr0, eax
but if I do that and have it do a self loop:

Code: Select all

hang:
jmp hang
and try it out on Bochs it constantly reloads again and again and again until I stop it. So I decided to look for more tutorials and found in Mr Xsism's Xosdev that he does:
1) mov eax, cr0
2) or eax, 1
3) mov cr3, eax
But I remember reading in Intel's manual that cr0 is the control register that has the system control flags that control the operating mode and state of the processor.
But I tried it out anyways and found out that it would load and print the text Entering Pmode...[OK]
But I still wasn't sure if it really is in p-mode. So following another person's tutorial I had it do a far jump to 32-bit code and print the letter 'P' but found out that it doesn't work. Would anyone know what might be the problem?

Thank,
Kenneth
User avatar
chase
Site Admin
Posts: 715
Joined: Wed Oct 20, 2004 10:46 pm
Libera.chat IRC: chase_osdev
Location: Texas
Discord: chase/matt.heimer
Contact:

Post by chase »

kenneth_phough wrote:...and try it out on Bochs it constantly reloads again and again and again until I stop it.
You had a fault and then another fault while trying to service the first fault.... The original fault could be that you GDT has some incorrect values or your addresses are incorrect.
kenneth_phough wrote:So I decided to look for more tutorials and found in Mr Xsism's Xosdev that he does:
1) mov eax, cr0
2) or eax, 1
3) mov cr3, eax
But I remember reading in Intel's manual that cr0 is the control register that has the system control flags that control the operating mode and state of the processor.
I think the tutorial might be bad, my code looks like:

Code: Select all

		cli
		mov al,0x80	; disable nmi
		out 0x70,al

		mov eax,cr0	; SET PMODE BIT IN CR0
		or al,1
		mov cr0,eax

		jmp SYS_CODE_SEL:Goto_PMode
You can also take a look at http://www.osdev.org/howtos/1/loader.asm for some working example code.
User avatar
kenneth_phough
Member
Member
Posts: 106
Joined: Sun Sep 18, 2005 11:00 pm
Location: Williamstown, MA; Worcester, MA; Yokohama, Japan
Contact:

Post by kenneth_phough »

Thank you, your souce code helped.
I found out that I forgot to declare the Intel's reserved segement, the null segment.
But even after I fixed the GDT entry it still resets itself. I cleaned up some of my code and did a series of test and found out that the fault happens right after I set the bit for PMode. But I still can't figure out why it resets itself after:

Code: Select all

mov eax, cr0
or ax, 1
mov cr0, eax
User avatar
chase
Site Admin
Posts: 715
Joined: Wed Oct 20, 2004 10:46 pm
Libera.chat IRC: chase_osdev
Location: Texas
Discord: chase/matt.heimer
Contact:

Post by chase »

Is your next instruction a far jump? (to start 32-bit PMode execution a far jump is needed to reload CS)

Is your far jump jumping to a seperate area of memory loaded with a 32-bit PMode executable that you've loaded or is it just trying to run more code in you bootsector?

Is this in the boot sector code or a secondary stage piece of code?

What is the base address in the CS segement descriptor and IP for your far jump?

What is the ORG in the code you're jumping to?
User avatar
kenneth_phough
Member
Member
Posts: 106
Joined: Sun Sep 18, 2005 11:00 pm
Location: Williamstown, MA; Worcester, MA; Yokohama, Japan
Contact:

Post by kenneth_phough »

My next instruction does a far jump to 32 bit code and it runs more code after the far jump:

Code: Select all

pmode:  ;;Subroutine to enter protected mode.
cli
xor ax, ax
mov ds, ax

lgdt [gdtr]

mov eax, cr0
or ax, 1
mov cr0, eax

jmp 0x08:clr_pipeline

The base address of my code segment is set to 0 - after the null segment.

The P-mode tut I got which I used to write my p-mode is from Gregor Brunmar - The world of Protected mode - http://www.osdever.net/tutorials/brunma ... ial_02.php

Yours,
Kenneth
User avatar
JAAman
Member
Member
Posts: 880
Joined: Wed Oct 27, 2004 11:00 pm
Location: WA

Post by JAAman »

i see 1 potential problem:

jmp 0x08:clr_pipeline -- this will only work if your current segment is 0 based (if your using a RMode CS other than 0, it will fail...)

i dont know what your using, since you didnt post all your code, but 2 qestions:

1) is this in your bootsector or a second stage

2) what is in CS at the time this code is executed
User avatar
kenneth_phough
Member
Member
Posts: 106
Joined: Sun Sep 18, 2005 11:00 pm
Location: Williamstown, MA; Worcester, MA; Yokohama, Japan
Contact:

Post by kenneth_phough »

1) is this in your bootsector or a second stage
The code is all in my bootsector.
2) what is in CS at the time this code is executed
To tell you the truth, I'm not sure, I never set the cs register in my bootsect at all. Should I have set it to 0?

The source for my bootsector is at:
http://www.kenax.byethost9.com/osdev/boot/boot.asm

Thanks,
Kenneth
User avatar
JAAman
Member
Member
Posts: 880
Joined: Wed Oct 27, 2004 11:00 pm
Location: WA

Post by JAAman »

when your boot sector begins execution, the absolute memory address will always be 0x7C00 -- but there are many ways of addressing this memory, and there is no gaurentee what CS:EIP will be

commonly it is either:
0x0:0x7C00
or
0x07C0:0x0

the biggest issue is: what is your org statement -- yours is correct, so this isnt your problem with that JMP, however, your not setting CS

this will cause problems with any near jump within your code (forgeting about this, some computers will handle the near jumps correctly, but others wont -- depending on what they set CS to before calling your code) however, most JMPs in your bootsector are likely to be short -- which is reletive, and will never be a problem, near JMPs require knowing what the offset within the segment is, and you dont know what segment your in until you specify it


as is, your code will work on most computers (setting CS = 0 seams to be quite common), but not all, because it will only work if CS=DS=0 when your code begins execution -- which is not gaurenteed

the general recomendation is to make a far jump near the begining of your code which forces CS:EIP to a known value, then set DS to the same value (be sure it matches your org statement)

example:

Code: Select all

org 0x7C00
JMP 0x0:start

start:
XOR AX,AX
MOV DS,AX
this will set both CS and DS to 0, and set your org statement to match


note:
if you choose to use org 0 and set CS/DS=0x07C0, you will need to manually correct your JMP to 32bit:

JMP 0x08:clr_pipline + 0x7C00
User avatar
chase
Site Admin
Posts: 715
Joined: Wed Oct 20, 2004 10:46 pm
Libera.chat IRC: chase_osdev
Location: Texas
Discord: chase/matt.heimer
Contact:

Post by chase »

As JAAMan stated you have so CS issues that will need to be corrected, personally I prefer to just use all relative jumps in my boot sector to avoid the issue. However I'm not usually trying to start PMode in my bootsectors. Most developers seem to preform the PMode init in a secondary stage boot loader. I've seen some attempts to try to cram the entire OS in the boot sector so whatever works for you....

But anyway back to your code.... Maybe I'm missing it but I don't see where your are setting your gdt base before executing lgdt

Code: Select all

pmode:  ;;Subroutine to enter protected mode.
cli  ;;disable interrupts.
xor ax, ax  ;;set ax register to 0;
mov ds, ax  ;;set data segment register to 0x00.

lgdt [gdtr]
And your data used by lgdt...

Code: Select all

gdtr:  ;;gdt descriptor table
dw gdt_end  ;;limit
dd 0  ;;base
User avatar
kenneth_phough
Member
Member
Posts: 106
Joined: Sun Sep 18, 2005 11:00 pm
Location: Williamstown, MA; Worcester, MA; Yokohama, Japan
Contact:

Post by kenneth_phough »

THANK YOU VERY MUCH.
BOTH OF YOU SAVED MY LIFE. :D
Post Reply