Transcription of Writing a Simple Operating System | from Scratch
1 IWriting a Simple Operating System from ScratchbyNick BlundellSchool of Computer Science, University of Birmingham,UKDraft: December 2, 2010 Copyrightc 2009 2010 Nick BlundellContentsContentsii1 Introduction12 Computer Architecture and the Boot Boot Process .. , Boot Blocks, and the Magic Number .. Emulation .. : A x86 CPU Emulator .. Usefulness of Hexadecimal Notation ..63 Boot Sector Programming (in 16-bit Real Mode) Sector Re-visited .. Real Mode .. , Hello? .. Registers .. it all Together .. , World!
2 , Addresses, and Labels .. X Marks the Spot .. 13 Question 1 .. Strings .. the Stack .. 17 Question 2 .. Structures .. 17 Question 3 .. Functions .. Files .. it all Together .. 21 Question 4 .. , Fetch me my Steth-o-scope .. 5 (Advanced) .. the Disk .. Memory Access Using Segments .. Disk Drives Work .. BIOS to Read the Disk .. it all Together .. 284 Entering 32-bit Protected to Life Without BIOS .. the Global Descriptor Table.
3 The GDT in Assembly .. the Switch .. it all Together .. 395 Writing , Building, and Loading Your C Compilation .. Raw Machine Code .. Variables .. Functions .. , Addresses, and Data .. our Kernel Code .. our Kernel .. a Boot Sector to Bootstrap our Kernel .. Our Way into the Kernel .. Builds with Make .. Our Operating System s Code Base .. Primer .. Pre-processor and Directives .. Declarations and Header Files .. 606 Developing Essential Device Drivers and a Input/Output.
4 Buses .. Programming .. Memory Access .. Driver .. the Display Device .. Screen Driver Implementation .. the Screen .. Interrupts .. Driver .. Driver .. System .. 707 Implementing Processing .. 71 CONTENTSiv8 Summary72 Bibliography73 Chapter1 IntroductionWe ve all used an Operating System (OS) before ( Windows XP, Linux, etc.), andperhaps we have even written some programs to run on one; but what is an OS actuallythere for? how much of what I see when I use a computer is done by hardware and howmuch is done by software?
5 And how does the computer actually work?The late Prof. Doug Shepherd, a lively teacher of mine at Lancaster University,once reminded me amid my grumbling about some annoying programming problem that,back in the day, before he could evenbeginto attempt any research, he had to writehis own Operating System , from Scratch . So it seems that, today, we take a lot forgranted about how these wonderful machines actually work underneith all those layersof software that commonly come bundled with them and which are required for theirday-to-day , concentrating on the widely used x86 architecture CPU, we will strip bare ourcomputer ofallsoftware and follow in Doug s early footsteps, learning along the wayabout.
6 How a computer boots How to write low-level programs in the barren landscape where no operatingsystem yet exists How to configure the CPU so that we can begin to use its extended functionality How to bootstrap code written in a higher-level language, so that we can reallystart to make some progress towards our own Operating System How to create some fundamental Operating System services, such as device drivers,file systems, multi-tasking that, in terms of practical Operating System functionality, this guide does notaim to be extensive, but instead aims to pool together snippets of information frommany sources into a self-contained and coherent document, that will give you a hands-onexperience of low-level programming, how Operating systems are written, and the kindof problems they must solve.
7 The approach taken by this guide is unique in that theparticular languages and tools ( assembly, C, Make, etc.) are not the focus butinstead are treated as a means to an end: we will learn what we need to about thesethings to help us achieve our main 1. INTRODUCTION2 This work is not intended as a replacement but rather as a stepping stone to excellentwork such as the Minix project [?] and to Operating System development in Architecture and theBoot The Boot ProcessNow, we begin our we reboot our computer, it must start up again, initially without any notion ofan Operating System .
8 Somehow, it must load the Operating System --- whatever variantthat may be --- from some permanent storage device that is currently attached to thecomputer ( a floppy disk, a hard disk, a USB dongle, etc.).As we will shortly discover, the pre-OS environment of your computer offers little inthe way of rich services: at this stage even a Simple file System would be a luxury ( and write logical files to a disk), but we have none of that. Luckily, what we do haveis the Basic Input/Output Software (BIOS), a collection of software routines that areinitially loaded from a chip into memory and initialised when the computer is switchedon.
9 BIOS provides auto-detection and basic control of your computer s essential devices,such as the screen, keyboard, and hard BIOS completes some low-level tests of the hardware, particularly whether ornot the installed memory is working correctly, it must boot the Operating System storedon one of your devices. Here, we are reminded, though, that BIOS cannot simply load afile that represents your Operating System from a disk, since BIOS has no notion of a file- System . BIOS must read specific sectors of data (usually 512 bytes in size) from specificphysical locations of the disk devices, such as Cylinder 2, Head 3, Sector 5 (details ofdisk addressing are described later, in Section XXX).
10 So, the easiest place for BIOS to find our OS is in the first sector of one of the disks( Cylinder 0, Head 0, Sector 0), known as theboot sector. Since some of our disks maynot contain an Operating systems (they may simply be connected for additional storage),then it is important that BIOS can determine whether the boot sector of a particulardisk is boot code that is intended for execution or simply data. Note that the CPU doesnot differentiate between code and data: both can be interpreted as CPU instructions,where code is simply instructions that have been crafted by a programmer into someuseful 2.