nibblebits / PeachOS

Simple kernel designed for a online course
GNU General Public License v2.0
133 stars 55 forks source link

boot.asm and kernel.asm #11

Open JaihsonK opened 1 year ago

JaihsonK commented 1 year ago

Hey! There are a few problems with the code to switch to pmode:

  1. we should turn on a20 line before switching to pmode
  2. we set the segment registers in kernel.asm, but it should be done even before we call the data driver in boot.asm

sample code at https://serpaeos.sourceforge.io/

nibblebits commented 1 year ago

Taking a look at references from OSDEV.ORG I can confirm that switching A20 after protected mode is perfectly acceptable: https://wiki.osdev.org/A20_Line

We must be setting the segment registers in kernel.asm failure to do so is undefined behaviour. If you believe I am wrong please further clairfy points 1 and 2.

nibblebits commented 1 year ago

Hi, Wait a moment I am reopening the issue as you may be on to something here.

step2:
    cli ; Clear Interrupts
    mov ax, 0x00
    mov ds, ax
    mov es, ax
    mov ss, ax
    mov sp, 0x7c00
    sti ; Enables Interrupts

.load_protected:
    cli
    lgdt[gdt_descriptor]
    mov eax, cr0
    or eax, 0x1
    mov cr0, eax
    jmp CODE_SEG:load32

Here we are setting the segment selectors to zero which points to the NULL selector. Therefore you are correct the actual loading of the sectors should fail however this is not the case so i will need to look into that perhaps I knew something back then that I have since forgot. I will look into it.

Regarding the A20 line yes we enable it too late but its not because of protected mode, its because the address of our kernel starts at the 20 line which may be disabled. So we will look into fixing both of these problems in a future video

Thanks, Dan

nibblebits commented 1 year ago

Update: It is possible that the sector loading succeeds because it is done at the hardware level, therefore bypassing the MMU(Memory managment unit) this would make sense why it would work even if the segment selectors are incorrectly setup. Never the less I believe theirs enough evidence here to support that this is a bit of undefined behaviour. I will look into making a video to fix these problems thanks for bringing them to me,

JaihsonK commented 1 year ago

OK thanks for the update. Here is a wiki forum FYI https://forum.osdev.org/viewtopic.php?f=1&t=56605&p=343733#p343733

nibblebits commented 1 year ago

Hi So just to confirm you solved this problem by loading the segment selectors in the bootloader rather than in kernel.asm right?

Are you booting on real hardware? Thanks, Dan

JaihsonK commented 1 year ago

Hey! Yes, that is correct, the first thing under load32.

I have not tried it on real hardware yet, I'm waiting for a PATA hard disk to come in the mail. However, I'm told that KVM maps very closely to real hardware (more than QEMU) and my OS works on KVM now that I made this adjustment.

nibblebits commented 1 year ago

Great thanks for the update, with this being the case then it seems clear that obviously where the selectors were set to the NULL selector this has caused undefined behaviour. Not noticed by myself since it works on QEMU.

Thanks for reporting I'll get to fixing that shortly and happy new year

JaihsonK commented 1 year ago

Thanks, you too!