Change the bitness of the operating system that I write myself?

I write the OS. Due to the fact that I have little experience, I only know how to do this for 32-bit (my OS turns out to be 32-bit), but I want to make it 64-bit.

What should I do?

Here is the code of my kernel specifically for the assembler:

.set MAGIC, 0x1badb002
.set FLAGS, (1<<0 | 1<<1)
.set CHECKSUM, -(MAGIC + FLAGS)

.section .multiboot
  .long MAGIC
  .long FLAGS
  .long CHECKSUM

.section .text
.extern kernelMain
.global loader

loader:
    mov $kernel_stack, %esp
    push %eax
    push %ebx
    call kernelMain

_stop:
    cli
    hlt
    jmp _stop

.section .bss
.space 2*1024*1024; # 2 megabytes of space
kernel_stack:

It's not finished yet, of course, but it's close. And some more information:
I compile my code in GNU Assembler using as in Ubuntu; C++ code using g++.

If you find duplicates, please drop them the link.

Author: LeopardL GD, 2020-06-14

1 answers

The above code is not at all similar to the operating system code. You just have a call to the kernelMain function and a subsequent infinite loop waiting for hardware interrupts. There is nothing to convert to 64-bit mode, this code will look identical in any mode.

Switching to 64-bit mode in Intel x86 processors is performed by setting the LME flag (bit 8) in the Extended Feature Enable Register (EFER). See Intel® 64 and IA-32 Architectures Software Developer's Manual, Volume 3: System Programming Guide, section 2.2.1

efer

The instruction that fills this register is wrmsr.

After that, you need to fill in the GDT so that it contains a code segment with the 64-bit mode flag (21 bits in the descriptor), and, accordingly, execute the code in this segment. Read more here: https://wiki.osdev.org/User:Stephanvanschaik/Setting_Up_Long_Mode

 4
Author: MSDN.WhiteKnight, 2020-10-16 08:11:54