OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 9:50 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Hi guys! I'm new here and I am new to OS development!
PostPosted: Mon Feb 13, 2023 8:46 pm 
Offline

Joined: Mon Feb 13, 2023 8:40 pm
Posts: 1
:o

So, I have been working with chatgpt to develop some code. I have a few ideas I wanted merged together and so I thought it would be interesting to ask chatgpt to try and resolve my issues...
I described to it a virtual machine monitor that I wanted written in the D programming language...it seemed to do everything all right. Then came the trickier part, I wanted to join D with assembly so that I could actually boot into the vmm.

I'm going to post the code that's been generated and (somewhat) revised by me below:
boot.asm
Code:
section .text align=1
start:
    ; Load the kernel code into memory
    mov edx, 0x8000
    mov dh, 0x02
    mov dl, 0x80
    mov ah, 0x02
    int 0x13
    jnc kernel_loaded
    jmp error

kernel_loaded:
    ; Set the entry point for the kernel
    mov eax, [0x8000 + 0x218]
    mov ecx, [0x8000 + 0x21A]
    mov edx, 0
    add edx, ecx
    add ecx, ecx
    add edx, ecx
    add ecx, ecx
    add edx, ecx
    add ecx, ecx
    add edx, ecx
    add ecx, ecx
    add edx, ecx
    add eax, edx
    add eax, 0x8000
    jmp eax
    ; Display "Loading complete." on the screen
    mov ah, 0x00
    mov al, 'L'
    int 0x10
    mov ah, 0x00
    mov al, 'o'
    int 0x10
    mov ah, 0x00
    mov al, 'a'
    int 0x10
    mov ah, 0x00
    mov al, 'd'
    int 0x10
    mov ah, 0x00
    mov al, 'i'
    int 0x10
    mov ah, 0x00
    mov al, 'n'
    int 0x10
    mov ah, 0x00
    mov al, 'g'
    int 0x10
    mov ah, 0x00
    mov al, ' '
    int 0x10
    mov ah, 0x00
    mov al, 'c'
    int 0x10
    mov ah, 0x00
    mov al, 'o'
    int 0x10
    mov ah, 0x00
    mov al, 'm'
    int 0x10
    mov ah, 0x00
    mov al, 'p'
    int 0x10
    mov ah, 0x00
    mov al, 'l'
    int 0x10
    mov ah, 0x00
    mov al, 'e'
    int 0x10
    mov ah, 0x00
    mov al, 't'
    int 0x10
    mov ah, 0x00
    mov al, 'e'

error:
    ; Error handling code here

    ; Pad the boot loader to 512 bytes
    times 510 - ($-$$) db 0
    dd 0xAA55




Annnd
vmmonitor.d
Code:
import core.stdc.stdio;
import core.stdc.string;
import core.stdc.stdlib;
import core.bitop;

class VirtualMemoryManager {
    private:
        // Page table for virtual memory
        uint[4096] pageTable;
    public:
        // Set up page tables for virtual memory
        void init() {
            for (int i = 0; i < 4096; i++) {
                pageTable[i] = i * 4096;
            }
        }
};

class VirtualMachineMonitor {
    private:
        // Instance of the virtual memory manager
        VirtualMemoryManager vmm;
        // Task queue for the task scheduler
        uint[1024] taskQueue;
    public:
        // Initialize the virtual memory manager
        void initVirtualMemory() {
            vmm.init();
        }
        // Initialize the task scheduler
        void initTaskScheduler() {
            for (int i = 0; i < 1024; i++) {
                taskQueue[i] = i;
            }
        }
};

// Define the kernel code
void initKernel() {
    // Initialize the virtual machine monitor
    VirtualMachineMonitor vmm = new VirtualMachineMonitor();
    vmm.initVirtualMemory();
    vmm.initTaskScheduler();
}



I guess my question for everyone is where do I go from here? I mean the code is obviously broken. Though I fail to see why. Could anyone help me out please?

Oh, and here are the commands for compiling said code:
Quote:
dmd -c vmmonitor.d
nasm -f elf64 -fbin -o bootloader.o boot.asm
ld -Ttext 0x7C00 -o boot.bin bootloader.o vmmonitor.o -L /usr/include/dmd/phobos/ -lphobos2
genisoimage -o boot.iso -b boot.bin -no-emul-boot boot.bin


Top
 Profile  
 
 Post subject: Re: Hi guys! I'm new here and I am new to OS development!
PostPosted: Thu Feb 23, 2023 12:15 pm 
Offline
Member
Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 1593
couchpotato wrote:
So, I have been working with chatgpt to develop some code.
Oh dear. ChatGPT is known to have a few issues with accuracy. No issues with humility however, so it will tell you total nonsense with absolute sincerity.
couchpotato wrote:
Code:
    ; Load the kernel code into memory
    mov edx, 0x8000
    mov dh, 0x02
    mov dl, 0x80
    mov ah, 0x02
    int 0x13
Yep, issues with accuracy alright. The data buffer is ES:BX, not EDX. You need to set CX for cylinder and sector number, and you really are not supposed to just set the drive number like that. Are you certain you are booting from the first hard drive? Besides, I don't even know if function 2 still works with modern hard drives. Oh, and AL is supposed to be the number of sectors read, and that is never set at all.
couchpotato wrote:
Code:
    mov eax, [0x8000 + 0x218]
    mov ecx, [0x8000 + 0x21A]
    mov edx, 0
    add edx, ecx
    add ecx, ecx
    add edx, ecx
    add ecx, ecx
    add edx, ecx
    add ecx, ecx
    add edx, ecx
    add ecx, ecx
    add edx, ecx
    add eax, edx
    add eax, 0x8000
    jmp eax
Er... what? That is the weirdest way to multiply a number with 31 I have ever seen. Anyway, the thing ends with "jmp eax", so that terminates the execution there. As far as I can see, it can never return from there, so the rest of the program is pointless. Never mind the fact that I have no idea what secret data is supposed to be at the two magic addresses given above. Even if 0x8000 is the kernel image, that is more than one sector behind the start, and I don't know any executable format that has such a long header.

The routine to print "Loading complete" could do with a string and a loop, and it just ends. It doesn't do anything afterward, it falls into the error handler. That you have not filled in, so that will just execute nonsense (please, at least put a hlt loop there, so the program stops and doesn't run off into uncharted territory).

I would suggest just using an existing bootloader. Programming for the BIOS can be fun and enlightening, but also tedious and frustrating, and it is only the start of development. Writing a BIOS bootloader yourself will not help you one jot or tittle with the actual kernel, which is presumably where you actually want to spend your time.

_________________
Carpe diem!


Top
 Profile  
 
 Post subject: Re: Hi guys! I'm new here and I am new to OS development!
PostPosted: Thu Feb 23, 2023 1:23 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
couchpotato wrote:
Code:
    ; Load the kernel code into memory
Quote:
genisoimage -o boot.iso -b boot.bin -no-emul-boot boot.bin

If you're actually booting that as an optical disc, you don't need to load anything - the kernel will already be somewhere in memory. (Though, since you're not using a linker script, it may not be where you want it, and it may be missing some pieces.)

couchpotato wrote:
Code:
    ; Display "Loading complete." on the screen
    mov ah, 0x00
    mov al, 'L'
    int 0x10

You're calling the function to set the video mode, not the function to display a character.

You don't have any code to switch the CPU to 64-bit mode before trying to jump to your 64-bit code. You don't have any code to collect a memory map. You don't have any code to enable the A20 line.

nullplan wrote:
Besides, I don't even know if function 2 still works with modern hard drives.

It does. It also works with devices emulating hard drives, including USB flash drives and some optical discs (genisoimage -hard-disk-boot).


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Majestic-12 [Bot], SemrushBot [Bot] and 62 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group