Hi,
01000101 wrote:It doesn't triple fault, I think it just hangs somewhere, unless triple-faulting in (un)real mode is caught by the IDT?
In real mode (and unreal mode) most BIOS "exception handlers" will simply return with an IRET, which causes another exception, which causes another IRET, which causes another exception, which causes, um, I forgot.
I noticed a few things...
Code: Select all
start:
cli
cmp word [0x7e00], 0xBEEF ; if true, it's an AP
jne boot ; if not, jump to BSP boot
jmp 0x0000:0x7d23 ; calculated offest to "PBOOT:"
For the BSP, nothing reloads CS so you don't know if the value in CS will be 0x07C0 or 0x0000. This may or may not matter.
For the AP, the trampoline code sets CS appropriately, so there no need to use a far jump. It would make sense to reverse the branch - for example:
Code: Select all
start:
cli
cmp word [0x7e00], 0xBEEF ; if true, it's an AP
je 0x7d23
jmp 0x0000:boot
Or even better, determine if the value in CS matters and use near jumps in both cases. However, if you use a "CS:" segment override prefix or jump to a fixed address (e.g. "jmp [eax]") then the value in CS will matter.
Code: Select all
jmp 0x0000:0x7d23 ; calculated offest to "PBOOT:"
In general this is a pain in the neck - any time you change anything you'd need to re-calculate the offset by hand. I see no reason why you can't just use:
Of course an even better idea would be to use "jmp 0x0000:PBOOT" as the trampoline (e.g. copy this jump to 0x10000), so you don't need to worry about doing "cmp word [0x7e00], 0xBEEF". Also, what values do different BIOSs leave in DS when they start a boot loader, and exactly which addresses might the "cmp word [
DS:0x7e00], 0xBEEF" instruction refer to?
You're missing the BIOS Parameter Block (BPB). This table is optional, but Windows tends to think the disk is corrupt and refuses to format the disk unless the BPB is present (and sane); which causes compatability problem for people that use Windows. For example, it might cost people a floppy disk each time they test your OS if they don't have a (non-Windows) computer to reformat them afterwards.
Code: Select all
;------------GDT Table---------------;
GDTD:
GDT_size DW 0x27
GDT_base DD GDT_START
GDT_START:
; NULL
dd 0x0
dd 0x0
This isn't aligned correctly, and you could save some bytes by doing this:
Code: Select all
;------------GDT Table---------------;
align 4
GDT_START:
; NULL descriptor *and* GDTD
GDTD:
GDT_size: DW GDT_END - GDT_START - 1
GDT_base: DW GDT_START
dd 0x0
Also note that I wrote "GDT_base:
DW GDT_START" here, because the default operand size is 16-bit when you do "lgdt [GDTD]" so the highest 16-bits are ignored anyway. Doesn't matter much either way in this context though.
Code: Select all
boot:
; ------ initialize registers + stack ------
a16 mov sp, 0x9F00 ; set stack to 0:0x9F00
Is the "a16" prefix necessary here? I'm fairly sure the assembler knows that "sp" is 16-bit, so unless you messed up the "bits 16" somewhere...
The same applies to all the other "a16", "a32", "o16" and "o32" prefixes - none of them look necessary, and they just make the code look messy. The only reason to use any of these prefixes is with string instructions - for example "a32 rep stosd" in 16-bit code so that it uses ESI/EDI instead of SI/DI, but that's only because these instructions don't have any operands.
For mixed size jump instructions just use size specifiers (for example, in real mode, use "jmp far dword 0x1234:0x56789ABC" rather than "a32 jmp far 0x1234:0x56789ABC", because the assembler might generate an "a32" prefix followed by a 16-bit "jmp far 0x1234:0x9ABC" which will make the CPU jump to the wrong place - I'm not sure about this - I don't use these prefixes unless I have to).
Code: Select all
call xor_regs ; clear all general purpose registers (EAX-EDX)
Why do you clear EAX and then load something into it straight after? Do you actually need to clear any of the registers, or could you just delete every "call xor_regs" instruction (and then delete the "xor_regs" routine too)? If you do need to clear a few registers, then how many registers would need to be cleared before "call xor_regs" saves space instead of costing space?
Code: Select all
enter_PMODE:
a32 mov eax, cr0 ; set bit 0 in CR0 to enable PMODE
a16 or al, 1
a32 mov cr0, eax
.continue:
a16 mov bx, 0x10 ; load ds with 0x10 (GDT entry #2 [datasel32])
a16 mov ds, bx
a16 and al, 0xFE ; unset bit 0 in CR0 to disable PMODE
a32 mov cr0, eax
a16 mov ds, cx ; load ds, es, and ss with 0
a16 mov es, bx
a16 mov ss, bx
Ok. First, this code should be called "enable_unreal_mode" or something. It's currently called "enter_PMODE" but it doesn't enter protected mode (at least not for long). The ".continue:" label isn't referenced by anything. After you change CR0 (e.g. when you enable protected mode *and* when you disable protected mode) you should do a "jmp" to flush the instruction cache (especially for 80486, but not necessarily just for 80486) - I'm guessing that's what the ".continue:" label was originally used for (but there isn't even a label where protected mode is disabled). The only segment register you touch is DS, but for some reason when you get back to real mode you reset ES and SS too.
Maybe:
Code: Select all
set_DS_flat:
mov eax, cr0 ; set bit 0 in CR0 to enable PMODE
mov cx, 0x10 ; cx = 0x10 (GDT entry #2 [datasel32])
mov ebx, eax
xor dx,dx
or al, 1
mov cr0, eax
jmp .flush1
.flush1:
mov ds, cx
mov cr0, ebx
jmp .flush2
.flush2:
mov ds, dx ; load ds with zero
Your ".floppy_reset" code is used before you attempt to load anything, even though you know the BIOS just used the floppy to load your code without any problem - might be nice to shift this elsewhere, so that the floppy is only ever reset if there's a disk error.
I don't understand what the "if there's an error try random BIOS functions" idea is...

Here's your code:
Code: Select all
.floppy_reset: ; reset the floppy drive
int 0x13
a16 cmp ah, 0 ; error?
jne .floppy_reset ; yes, use Int 0x13 again with some random value in AH (Huh???)
This is also another spot where doing "xor ah,ah" will probably take up less space than doing "call xor_regs".
Code: Select all
; ------ int 0x13 f(2) ------
; -- ES:BX = Destination Seg:Off
; -- AH = Function Number
; -- AL = # of Sectors to load
; -- CH = Start Cylinder
; -- CL = Start Sector
; -- DH = Start Head
; -- DL = Drive Number (0 for FloppyA)
When the BIOS (or whatever) loads your boot sector it stores a "BIOS device number" in DL. You should store this value somewhere and then use it instead of assuming that the device number is zero. That way a chain-loader could boot your OS from the second floppy.
Code: Select all
a16 mov ax, 0x0212 ; reload function number and sector count
a16 inc dh ; increase current head
a16 cmp dh, 2 ; if > 1, set head = 0 and increase current cylinder
jne .load_stage2
a16 xor dh, dh
a16 inc ch
a16 cmp ch, 0x06 ; if 6 cylinders have been loaded, exit
jne .load_stage2
Yeah - if you had a BIOS Parameter Block (BPB) then your code could use values from the BPB to support just about all floppy disk formats (where you have an installer that just sets the right values in the BPB - e.g. "formatBootFloppy <size>").
Also, loading entire tracks is faster, but it's also a little less reliable. For example, if the first sector can be read without errors half the time and the last sector can be read without error half the time, then there'll be a 75% chance that reading the whole track will fail. If 4 sectors in the track can only be read half the time, then there'd be 93.75% chance that reading the whole track will fail. This means retrying a lot. In the same situation (lots of unreliable sectors) if you read single sectors you can drastically reduce the time spent failing and retrying. For this reason I attempt to load the entire track in one go, but if that doesn't work I attempt to load the track one sector at a time; and if loading the entire track in one go fails too often I'll give up and load everything one sector at a time. Mostly, slowly getting the job done is better than quickly going nowhere...
Of course my previous code supported "redundant floppies", where the sectors on one side of the disk are a mirror image of the sectors on the other side of the disk. If you combine this with the "one sector at a time if we have to" idea then you end up with a very good chance of booting, even on very unreliable floppies (which isn't such a bad idea considering that floppy disks are the most unreliable type of storage that's ever existed).
I didn't check any of the code to setup paging structures. It's much easier to use Bochs debugger (the "x <flags> <address>" command) to examine the linear address space to make sure it works right.
There's no real need to clear CR2 - if you get a page fault the CPU will set CR2, and if you don't get a page fault then you don't need to care what CR2 contains.
Could maybe use "or ah,0x01" to save a byte.
Code: Select all
memcpy:
push word cx ; push cx. no need to push (e)bx as it gets nulled -
; in the floppy int code.
a32 mov esi, dword 0x10000 ; set source address to low-mem (0x10000)
a32 mov ecx, dword 0x2400 ; set count to 18k (0x2400)
.loop:
a32 mov ebx, dword [esi] ; copy dword from 0x10000+ to ebx
a32 mov dword [a32 edi], ebx ; copy ebx to 0x1000000+
a32 add esi, dword 4 ; update source pointer
a32 add edi, dword 4 ; update dest pointer
a32 sub ecx, dword 4 ; update counter
a32 cmp ecx, dword 0 ; if counter is still > 0, keep looping
jne .loop
pop word cx ; pop cx to reload start sector/cylinder
a32 ret ; exit function
Um, no. Instead of setting the DS segment limit to 4 GiB, set the ES segment limit to 4 GiB and then use "a32 rep movsd" (or even "es: a32 rep movsd" if the source is above 1 MiB too). That should improve speed a little and save some bytes (as long as you remember that 32-bit "rep movsd" uses ECX instead of CX).
Next is "enable_A20:". Probably the easiest thing for me to do is point you towards
a post I made yesterday about A20. I think that covers everything (just ignore the "DOS stuff" at the beginning of the post).
Code: Select all
goto_kernel:
mov ax, 0x20 ; set ds, es, and ss to 0x20 (DATA64)
mov ds, ax
mov es, ax
mov ss, ax
mov esp, 0x90000 ; set stack to 0x90000
That's a bad place for a stack. How big is the EBDA? I don't know and neither do you. On "many CPU" systems the EBDA tends to be a lot larger than it is in "a few CPUs" systems. What happens when your stack trashes the EBDA (what effect would it have on the BIOS's SMM code and/or ACPI)? What happens when the BIOS's SMM code and/or ACPI trashes your stack? Hmmm - probably best not to find out -
BIOS Int 0x12 might be fun.
Finally, error messages are good - it's probably why someone invented "printf()". If something really doesn't work then display an error message; and if you think something might not work display a progress message (e.g. display "I'm about to start the kernel" before you start the kernel, so that if it locks up the user has at least some idea of where it went wrong). For errors, I'd also consider making some noise with the PC speaker (so people can tell that their headless server won't be booting soon).
01000101 wrote:btw, this thing is stuffed to the gills with code, and I assemble with the -Ox (nasm) optimization just to fit everything with 512 bytes.
Hehe. Most beginners try to fit everything into 512 bytes. Most experienced OS developers know it's never worth the hassle. The only thing you need to do in the first sector of the boot loader is to load the second sector of the boot loader (or maybe the second stage).
Cheers,
Brendan