Step-by-step implementation of a simple OS (load kernel)

Source: Internet
Author: User

This piece of updated code is more, so it is not convenient to all posted out ... Specific code everyone directly into the group to find it (OS0.3)

In addition, I configured the Bochs virtual machine, so that everyone can be downloaded to use the Bochs virtual machine directly debugging.

Now I'll just post the main code,,,,,,

Setupasm. S (here, jump to C function direct call on it)

Start32:
    # Segment Register for each segment in initialize protected mode
    MOVW    $PROT _mode_dseg,%ax
    movw    %ax,%ds
    movw    %ax,%es
    MOVW    %ax,%fs
    movw    %ax,%gs
    movw    %ax,%ss

    movl    $start,%esp      # Before the code was useless, Let's get the stack covered.

    # here to jump to the C language program call    setupmain
    
    # OK, go here to Die Loop, the next section, C language, so it looks much simpler # in
    fact, I think, or this simple
1:
    hlt
    JMP     1b


SETUPMAIN.C (here directly paste the code of the redirect Elf file, before the PE format, and then looked at the next, PE redirection length is relatively large, so it is directly using the ELF. )

  Initialize kernel load address
  elf = (struct elfhdr*) 0x10000;

  Loaded into kernel
  readseg (UINT) elf, sectsize*8, kernel_disk_offset);

  Determine if the kernel is an elf file format
  if (elf->magic! = elf_magic)   
    goto bad;

  Redirect to elf file
  ph = (struct proghdr*) ((uchar*) elf + elf->phoff);
  Eph = ph + elf->phnum;
  for (; ph < Eph; ph++)
    readseg (Ph->va & 0xFFFFFF, Ph->memsz, Ph->offset + kernel_disk_offset);  Here is to add the offset, using Winhex to see the ELF header format, can be specific analysis out

  //Get the kernel entry point, and jump into the kernel
  entry = (void (*) (void)) (Elf->entry & 0xFFFFFF );
  Entry ();   Jumping into the kernel portal

MAIN.C (here, simply write, to test the setup load kernel is correct, implement the printf function, so it is more convenient to write later)

/**
 * Kernel entry
 *
/int Kmain ()
{
	//here, just display a message, simply
	printf ("In the kernel code\n");
	In fact, this is not allowed to return.
	///But that's it, see if it goes back there's no error return
	0;
}



Here's a screenshot of the run ... Feel the map, see more people, so strange


OK, that's all, don't understand the group to ask it [QQ qun:545250960].

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.