preface

Today, I was supposed to read a book and put FragileOS in order, but I’m still scratching my head and only read a little book. Then adjusted the train of thought of this series, originally for the operating system is the purpose of the original rational learning and to make a toy type of operating system before review, just want to outline for knowledge of the operating system can have a know, now it want to reduce write a review of the system, before, after all, only more than 2000 lines, but still want to have a show of the whole train of thought. Then add some learning about the Linux 0.12 source code. So maybe a little bit off the title, but there you go

What is an operating system

This section was originally intended to be an overview of computer systems and operating systems, but was deleted halfway through because it felt too watery. Just summarize a few sentences and fill in whatever comes next. An overview of computer systems should be part of the building blocks of computers, which are also chapters 1 and 2 of Operating Systems: The Essence and Principles of Design. But I felt that if you want to learn the internal code of the operating system, it would be better to switch to assembly content.

Let’s get down to business. What is an operating system

The most fundamental way for a computer to operate is to execute by finger

For typing Hello,World! The instructions tell the CPU to move ‘H’, ‘E’, ‘L’, etc. from memory to video memory so that they can be seen on the screen. This is the primitive way computers work

The operating system is the abstraction of the hardware level, so that we do not have to face the hardware. If we want to output characters on the screen again, we just call the operating system write(Windows seems to be this name), printf in C language is a system call

The operating system also manages memory, terminals, disks, networks, files, etc. Windows 2000 alone should have more than 30 million lines of code. There are, of course, toy kernels with rudimentary memory, process management, and file systems that can be done in a few thousand lines of code.

Startup of the operating system

For X86 computers, these are all done at boot time

  • CS = 0xFFFF and IP = 0x0000 at startup

    In this case, the CPU handles the real mode, that is, the addressing mode is CS:IP (real mode and protected mode belong to the CPU working mode, among which the big difference is the addressing mode)

  • Addressing 0 xffff0

  • Check hardware, like keyboards and monitors

  • Read sector 0 of disk 0 track 0 into 0x7c00

    This is where 512 bytes are read, the legendary bootloader, and this is where the first piece of code the computer executes

  • Set cs = 0x7c00 IP = 0x0000

FragileOS/boot

This is the FragileOS boot I wrote about earlier, in Intel assembly format

The main logic is:

  • First load to position 0x7c00
  • Initialization is performed
  • Call the CPU-provided interrupt to read the data
  • After reading, jump directly to the starting position of the kernel, which is the end of the boot

(Part of code)

org 0x7c00; ; Load to memory 0x7c00 LoadAddr EQU 08000h; The kernel memory address BufferAddr EQU 7E0h; BaseOfStack EQU 07C00H Entry: mov AX, 0; Mov SS, ax mov ds, ax mov AX, BufferAddr mov es, ax; Mov AX, 0 MOV SS, AX mov sp, BaseOfStack MOV DI, AX MOV si, AX MOV BX, 0; ES:BX data store buffer mov CH, 1; CH is used to store the cylinder number mov DH, 0; DH is used to store the magnetic head number mov CL, 0; CL stores the sector number read_floppy:; CMP byte [load_count], 0; If =0, start load je begin_load MOV Bx, 0 inc CL mov AH, 0x02; AH = 02 indicates that the disk is read. AL means practice reading several sectors mov DL, 0; Drive number, normally we only have one floppy drive, so int 0x13; Call BIOS interrupt to achieve disk read function JC FINCopy the code

Linux 0.12 / boot

The boot of Linux 0.12 is naturally much more complex than the above, Linux uses the AS86 format of the boot assembly, the rest of the assembly is AT&T

Boot in Linux 0.12 has three files:

  • bootsect
  • head
  • setup

(The code is too long to paste all, there is a need to private message me)

bootsect

The main purpose of bootsect is to move itself to 0x90000, load the setup module *(setup.s)* after Bootsect, and load the System module at 0x10000, which is the main part of the kernel

Bootsect begins with some constant definitions

SETUPLEN = 4				  ! nr of setup-sectors
BOOTSEG  = 0x07c0			! original address of boot-sector
INITSEG  = 0x9000			! we move boot here - out of the way
SETUPSEG = 0x9020			! setup starts here
SYSSEG   = 0x1000			! system loaded at 0x10000 (65536).
ENDSEG   = SYSSEG + SYSSIZE		! where to stop loading
Copy the code
  • _start Set the destination address and source address

    Ds: si and es: di

  • Then the rep directive is executed

    The rep instruction is repeated. It is judged by the value of the CX register and stops if the value of cx is 0

  • Movw instruction

    Start moving cx words from [si] to [di], so 256 words, 512 bytes, have been moved

  • Finally, switch to 0x9000 to start the execution

_start:
	mov	ax,#BOOTSEG
	mov	ds,ax
	mov	ax,#INITSEG
	mov	es,ax
	mov	cx,#256
	sub	si,si
	sub	di,di
	rep
	movw
	jmpi	go,INITSEG
Copy the code
  • The current code is all after 0x90000
  • Reset the segment register and stack pointer first
go:	mov	ax,cs
	mov	ds,ax
	mov	es,ax
! put stack at 0x9ff00.
	mov	ss,ax
	mov	sp,#0xFF00		! arbitrary value >>512
Copy the code
  • This part is the same as my previous one, which calls interrupts to read the contents of the disk, except Linux reads the Setup module in sector 2

  • If that fails, reset the drive and jump back to re-read

  • On success, skip to ok_load_setup

  • Ok_load_setup sets the root file SYSTEM device and reads the SYSTEM module *(the main part of the kernel)* to 0x10000, ending with a jump to the SETUP module

load_setup:
	mov	dx,#0x0000		! drive 0, head 0
	mov	cx,#0x0002		! sector 2, track 0
	mov	bx,#0x0200		! address = 512, in INITSEG
	mov	ax,#0x0200+SETUPLEN	! service 2, nr of sectors
	int	0x13			! read it
	jnc	ok_load_setup		! ok - continue
	mov	dx,#0x0000
	mov	ax,#0x0000		! reset the diskette
	int	0x13
	j	load_setup
Copy the code

summary

A simple boot program, as the name implies, is to do some boot work, do some initialization Settings and then read into the real kernel part, into the OS.

A complete Boot for Linux 0.12 should also include setup.s for the last Settings before the OS starts (going into protected mode, etc.) and head.s for the Settings after the OS starts. But because these two parts contain some other important concepts, I plan to write another one.