The Linux Kernel. 1.34MB 2013-07-11 22:01:50

The Linux Kernel
Copyright




1996-1999

David A Rusling
david.ruslingdigital.
om
REVIEW, Version 0.8-3

January 25, 1999

This book is for Linux enthusiasts who want to know how the Linux kernel works. It is
not an internals manual. Rather it des
ribes the prin
iples and me
hanisms that Linux
uses; how and why the Linux kernel works the way that it does. Linux is a moving target; this book is based upon the

urrent, stable, 2.0.33 sour
es as those are what most
individuals and
ompanies are now using.
This book is freely distributable, you may
opy and redistribute it under
ertain
onditions.
Please refer to the
opyright and distribution statement.

For Gill, Esther and Stephen

Legal Noti
e
UNIX is a trademark of Univel.
Linux is a trademark of Linus Torvalds, and has no
onne
tion to UNIXTM or
Univel.




Copyright
1996,1997,1998,1999 David A Rusling
3 Foxglove Close, Wokingham, Berkshire RG41 3NF, UK
david.ruslingarm.
om
This book (\The Linux Kernel") may be reprodu
ed and distributed in whole or
in part, without fee, subje
t to the following
onditions:

 The
opyright noti
e above and this permission noti
e must be preserved
omplete on all
omplete or partial

opies.

 Any translation or derived work must be approved by the author in writing
before distribution.

 If you distribute this work in part, instru
tions for obtaining the
omplete

version of this manual must be in
luded, and a means for obtaining a
omplete version provided.

 Small portions may be reprodu
ed as illustrations for reviews or quotes in
other works without this permission noti
e if proper
itation is given.

Ex

eptions to these rules may be granted for a
ademi
purposes: Write to the
author and ask. These restri
tions are here to prote
t us as authors, not to restri
t
you as learners and edu
ators.
All sour
e
ode in this do
ument is pla
ed under the GNU General Publi
Li
ense,
available via anonymous FTP from prep.ai.mit.edu:/pub/gnu/COPYING. It is also
reprodu
ed in appendix D.


Prefa
e
Linux is a phenomenon of the Internet. Born out of the hobby proje
t of a student it
has grown to be
ome more popular than any other freely available operating system.
To many Linux is an enigma. How
an something that is free be worthwhile? In
a world dominated by a handful of large software
orporations, how
an something
that has been written by a bun
h of \ha
kers" (si
) hope to
ompete? How
an
software
ontributed to by many di erent people in many di erent
ountries around

the world have a hope of being stable and e e
tive? Yet stable and e e
tive it is
and
ompete it does. Many Universities and resear
h establishments use it for their
everyday
omputing needs. People are running it on their home PCs and I would
wager that most
ompanies are using it somewhere even if they do not always realize
that they do. Linux is used to browse the web, host web sites, write theses, send
ele
troni
mail and, as always with
omputers, to play games. Linux is emphati
ally
not a toy; it is a fully developed and professionally written operating system used by
enthusiasts all over the world.
The roots of Linux
an be tra

ed ba
k to the origins of UnixTM . In 1969, Ken
Thompson of the Resear
h Group at Bell Laboratories began experimenting on a
multi-user, multi-tasking operating system using an otherwise idle PDP-7. He was
soon joined by Dennis Ri
hie and the two of them, along with other members of the
Resear
h Group produ
ed the early versions of UnixTM . Ri
hie was strongly in
uen
ed
by an earlier proje
t, MULTICS and the name UnixTM is itself a pun on the name
MULTICS. Early versions were written in assembly
ode, but the third version was
rewritten in a new programming language, C. C was designed and written by Ri
hie
expressly as a programming language for writing operating systems. This rewrite

allowed UnixTM to move onto the more powerful PDP-11/45 and 11/70
omputers
then being produ
ed by DIGITAL. The rest, as they say, is history. UnixTM moved
out of the laboratory and into mainstream
omputing and soon most major
omputer
manufa
turers were produ
ing their own versions.
Linux was the solution to a simple need. The only software that Linus Torvalds,
Linux's author and prin
iple maintainer was able to a ord was Minix. Minix is a
simple, UnixTM like, operating system widely used as a tea
hing aid. Linus was less
than impressed with its features, his solution was to write his own software. He took
UnixTM as his model as that was an operating system that he was familiar with in his
day to day student life. He started with an Intel 386 based PC and started to write.
Progress was rapid and, ex
ited by this, Linus o ered his e orts to other students

via the emerging world wide
omputer networks, then mainly used by the a
ademi

ommunity. Others saw the software and started
ontributing. Mu
h of this new
software was itself the solution to a problem that one of the
ontributors had. Before
long, Linux had be
ome an operating system. It is important to note that Linux
iii


ontains no UnixTM
ode, it is a rewrite based on published POSIX standards. Linux
is built with and uses a lot of the GNU (GNU's Not UnixTM ) software produ
ed by
the Free Software Foundation in Cambridge, Massa
husetts.

Most people use Linux as a simple tool, often just installing one of the many good
CD ROM-based distributions. A lot of Linux users use it to write appli
ations or
to run appli
ations written by others. Many Linux users read the HOWTOs1 avidly
and feel both the thrill of su

ess when some part of the system has been
orre
tly
on gured and the frustration of failure when it has not. A minority are bold enough
to write devi
e drivers and o er kernel pat
hes to Linus Torvalds, the
reator and
maintainer of the Linux kernel. Linus a

epts additions and modi
ations to the
kernel sour

es from anyone, anywhere. This might sound like a re
ipe for anar
hy
but Linus exer
ises stri
t quality
ontrol and merges all new
ode into the kernel
himself. At any one time though, there are only a handful of people
ontributing
sour
es to the Linux kernel.
The majority of Linux users do not look at how the operating system works, how
it ts together. This is a shame be
ause looking at Linux is a very good way to
learn more about how an operating system fun
tions. Not only is it well written,
all the sour
es are freely available for you to look at. This is be
ause although the
authors retain the
opyrights to their software, they allow the sour
es to be freely
redistributable under the Free Software Foundation's GNU Publi
Li
ense. At rst
glan
e though, the sour
es
an be
onfusing; you will see dire
tories
alled kernel,
mm and net but what do they
ontain and how does that
ode work? What is needed
is a broader understanding of the overall stru
ture and aims of Linux. This, in
short, is the aim of this book: to promote a
lear understanding of how Linux, the
operating system, works. To provide a mind model that allows you to pi
ture what
is happening within the system as you
opy a le from one pla
e to another or read
ele
troni
mail. I well remember the ex
itement that I felt when I rst realized just
how an operating system a
tually worked. It is that ex
itement that I want to pass
on to the readers of this book.
My involvement with Linux started late in 1994 when I visited Jim Paradis who was
working on a port of Linux to the Alpha AXP pro
essor based systems. I had worked
for Digital Equipment Co. Limited sin
e 1984, mostly in networks and
ommuni
ations and in 1992 I started working for the newly formed Digital Semi
ondu
tor
division. This division's goal was to enter fully into the mer
hant
hip vendor market
and sell
hips, and in parti
ular the Alpha AXP range of mi
ropro
essors but also
Alpha AXP system boards outside of Digital. When I rst heard about Linux I
immediately saw an opportunity to have fun. Jim's enthusiasm was
at
hing and I
started to help on the port. As I worked on this, I began more and more to appre
iate
not only the operating system but also the
ommunity of engineers that produ
es it.
However, Alpha AXP is only one of the many hardware platforms that Linux runs
on. Most Linux kernels are running on Intel pro
essor based systems but a growing
number of non-Intel Linux systems are be
oming more
ommonly available. Amongst
these are Alpha AXP, ARM, MIPS, Spar
and PowerPC. I
ould have written this
book using any one of those platforms but my ba
kground and te
hni
al experien
es
with Linux are with Linux on the Alpha AXP and, to a lesser extent on the ARM.
This is why this book sometimes uses non-Intel hardware as an example to illustrate
1A

HOWTO is just what it sounds like, a do
ument des
ribing how to do something. Many have

been written for Linux and all are very useful.

some key point. It must be noted that around 95% of the Linux kernel sour
es are
ommon to all of the hardware platforms that it runs on. Likewise, around 95% of
this book is about the ma
hine independent parts of the Linux kernel.

Reader Pro le
This book does not make any assumptions about the knowledge or experien
e of
the reader. I believe that interest in the subje
t matter will en
ourage a pro
ess of
self edu
ation where ne

essary. That said, a degree of familiarity with
omputers,
preferably the PC will help the reader derive real bene t from the material, as will
some knowledge of the C programming language.

Organisation of this Book
This book is not intended to be used as an internals manual for Linux. Instead
it is an introdu
tion to operating systems in general and to Linux in parti
ular.
The
hapters ea
h follow my rule of \working from the general to the parti
ular".
They rst give an overview of the kernel subsystem that they are des
ribing before
laun
hing into its gory details.
I have deliberately not des
ribed the kernel's algorithms, its methods of doing things,
in terms of routine X()
alls routine Y() whi
h in
rements the foo eld of the bar
data stru
ture. You
an read the
ode to nd these things out. Whenever I need to
understand a pie
e of
ode or des
ribe it to someone else I often start with drawing
its data stru
tures on the white-board. So, I have des
ribed many of the relevant
kernel data stru
tures and their interrelationships in a fair amount of detail.
Ea
h
hapter is fairly independent, like the Linux kernel subsystem that they ea
h
des
ribe. Sometimes, though, there are linkages; for example you
annot des
ribe a
pro
ess without understanding how virtual memory works.
The Hardware Basi
s
hapter (Chapter 1) gives a brief introdu
tion to the modern
PC. An operating system has to work
losely with the hardware system that a
ts
as its foundations. The operating system needs
ertain servi
es that
an only be
provided by the hardware. In order to fully understand the Linux operating system,
you need to understand the basi
s of the underlying hardware.
The Software Basi
s
hapter (Chapter 2) introdu
es basi
software prin
iples and
looks at assembly and C programing languages. It looks at the tools that are used
to build an operating system like Linux and it gives an overview of the aims and
fun
tions of an operating system.
The Memory Management
hapter (Chapter 3) des
ribes the way that Linux handles
the physi
al and virtual memory in the system.
The Pro
esses
hapter (Chapter 4) des
ribes what a pro
ess is and how the Linux
kernel
reates, manages and deletes the pro
esses in the system.
Pro
esses
ommuni
ate with ea
h other and with the kernel to
oordinate their a
tivities. Linux supports a number of Inter-Pro
ess Communi
ation (IPC) me
hanisms.
Signals and pipes are two of them but Linux also supports the System V IPC me
hanisms named after the UnixTM release in whi
h they rst appeared. These interpro
ess
ommuni
ations me
hanisms are des
ribed in Chapter 5.

The Peripheral Component Inter
onne
t (PCI) standard is now rmly established
as the low
ost, high performan
e data bus for PCs. The PCI
hapter (Chapter 6)
des
ribes how the Linux kernel initializes and uses PCI buses and devi
es in the
system.
The Interrupts and Interrupt Handling
hapter (Chapter 7) looks at how the Linux
kernel handles interrupts. Whilst the kernel has generi
me
hanisms and interfa
es
for handling interrupts, some of the interrupt handling details are hardware and
ar
hite
ture spe
i
.
One of Linux's strengths is its support for the many available hardware devi
es for
the modern PC. The Devi
e Drivers
hapter (Chapter 8) des
ribes how the Linux
kernel
ontrols the physi
al devi
es in the system.
The File system
hapter (Chapter 9) des
ribes how the Linux kernel maintains the
les in the le systems that it supports. It des
ribes the Virtual File System (VFS)
and how the Linux kernel's real le systems are supported.
Networking and Linux are terms that are almost synonymous. In a very real sense
Linux is a produ
t of the Internet or World Wide Web (WWW). Its developers and
users use the web to ex
hange information ideas,
ode and Linux itself is often used
to support the networking needs of organizations. Chapter 10 des
ribes how Linux
supports the network proto
ols known
olle
tively as TCP/IP.
The Kernel Me
hanisms
hapter (Chapter 11) looks at some of the general tasks and
me
hanisms that the Linux kernel needs to supply so that other parts of the kernel
work e e
tively together.
The Modules
hapter (Chapter 12) des
ribes how the Linux kernel
an dynami
ally
load fun
tions, for example le systems, only when they are needed.
The Pro
essors
hapter (Chapter 13) gives a brief des
ription of some of the pro
essors that Linux has been ported to.
The Sour
es
hapter (Chapter 14) des
ribes where in the Linux kernel sour
es you
should start looking for parti
ular kernel fun
tions.

Conventions used in this Book
The following is a list of the typographi
al
onventions used in this book.
serif font

type font

See foo() in

foo/bar.

identi es
ommands or other text that is to be typed
literally by the user.
refers to data stru
tures or elds
within data stru
tures.

Throughout the text there referen
es to pie
es of
ode within the Linux kernel sour
e
tree (for example the boxed margin note adja
ent to this text ). These are given
in
ase you wish to look at the sour
e
ode itself and all of the le referen
es are
relative to /usr/sr
/linux. Taking foo/bar.
as an example, the full lename
would be /usr/sr
/linux/foo/bar.
If you are running Linux (and you should),
then looking at the
ode is a worthwhile experien
e and you
an use this book as an
aid to understanding the
ode and as a guide to its many data stru
tures.

Trademarks
ARM is a trademark of ARM Holdings PLC.
Caldera, OpenLinux and the \C" logo are trademarks of Caldera, In
.
Caldera OpenDOS 1997 Caldera, In
.
DEC is a trademark of Digital Equipment Corporation.
DIGITAL is a trademark of Digital Equipment Corporation.
Linux is a trademark of Linus Torvalds.
Motif is a trademark of The Open System Foundation, In
.
MSDOS is a trademark of Mi
rosoft Corporation.
Red Hat, glint and the Red Hat logo are trademarks of Red Hat Software, In
.
UNIX is a registered trademark of X/Open.
XFree86 is a trademark of XFree86 Proje
t, In
.
X Window System is a trademark of the X Consortium and the Massa
husetts Institute of Te
hnology.

The Author
I was born in 1957, a few weeks before Sputnik was laun
hed, in the north of England.
I rst met Unix at University, where a le
turer used it as an example when tea
hing
the notions of kernels, s
heduling and other operating systems goodies. I loved using
the newly delivered PDP-11 for my nal year proje
t. After graduating (in 1982 with
a First Class Honours degree in Computer S
ien
e) I worked for Prime Computers
(Primos) and then after a
ouple of years for Digital (VMS, Ultrix). At Digital I
worked on many things but for the last 5 years there, I worked for the semi
ondu
tor
group on Alpha and StrongARM evaluation boards. In 1998 I moved to ARM where
I have a small group of engineers writing low level rmware and porting operating
systems. My
hildren (Esther and Stephen) des
ribe me as a geek.
People often ask me about Linux at work and at home and I am only too happy
to oblige. The more that I use Linux in both my professional and personal life the
more that I be
ome a Linux zealot. You may note that I use the term `zealot' and
not `bigot'; I de ne a Linux zealot to be an enthusiast that re
ognizes that there
are other operating systems but prefers not to use them. As my wife, Gill, who
uses Windows 95 on
e remarked \I never realized that we would have his and her
operating systems". For me, as an engineer, Linux suits my needs perfe
tly. It is
a superb,
exible and adaptable engineering tool that I use at work and at home.
Most freely available software easily builds on Linux and I
an often simply download
pre-built exe
utable les or install them from a CD ROM. What else
ould I use to
learn to program in C++, Perl or learn about Java for free?

A
knowledgements
I must thank the many people who have been kind enough to take the time to email me with
omments about this book. I have attempted to in
orporated those


omments in ea
h new version that I have produ
ed and I am more than happy to
re
eive
omments, however please note my new e-mail address.
A number of le
turers have written to me asking if they
an use some or parts of
this book in order to tea
h
omputing.

My answer is an emphati
yes; this is one

use of the book that I parti
ularly wanted. Who knows, there may be another Linus
Torvalds sat in the
lass.
Spe
ial thanks must go to John Rigby and Mi
hael Bauer who gave me full, detailed
review notes of the whole book. Not an easy task. Alan Cox and Stephen Tweedie
have patiently answered my questions - thanks.
brighten up the
hapters a bit.

I used Larry Ewing's penguins to

Finally, thank you to Greg Hankins for a

epting

this book into the Linux Do
umentation Pro je
t and onto their web site.

Contents
Prefa
e

iii

1 Hardware Basi
s

1

1.1

The CPU

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

2

1.2

Memory

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

4

1.3

Buses

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

4

1.4

Controllers and Peripherals

1.5

Address Spa
es

1.6

. . . . . . . . . . . . . . . . . . . . . . . .

5

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

5

Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

6

2 Software Basi
s
2.1

2.2

2.3

7

Computer Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . .

7

2.1.1

Assembly Languages . . . . . . . . . . . . . . . . . . . . . . . .

7

2.1.2

The C Programming Language and Compiler

. . . . . . . . . .

8

2.1.3

Linkers

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

9

What is an Operating System?

. . . . . . . . . . . . . . . . . . . . . .

9

. . . . . . . . . . . . . . . . . . . . . . .

10

2.2.1

Memory management

2.2.2

Pro
esses

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

10

2.2.3

Devi
e drivers . . . . . . . . . . . . . . . . . . . . . . . . . . . .

11

2.2.4

The Filesystems

. . . . . . . . . . . . . . . . . . . . . . . . . .

11

Kernel Data Stru
tures

. . . . . . . . . . . . . . . . . . . . . . . . . .

11

2.3.1

Linked Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

12

2.3.2

Hash Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

12

2.3.3

Abstra
t Interfa
es . . . . . . . . . . . . . . . . . . . . . . . . .

13

3 Memory Management
3.1

15

An Abstra
t Model of Virtual Memory . . . . . . . . . . . . . . . . . .

16

3.1.1

Demand Paging . . . . . . . . . . . . . . . . . . . . . . . . . . .

18

3.1.2

Swapping

19

3.1.3

Shared Virtual Memory

3.1.4

Physi
al and Virtual Addressing Modes

3.1.5

A

ess Control

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . .

19

. . . . . . . . . . . . .

19

. . . . . . . . . . . . . . . . . . . . . . . . . . .

20

ix

3.2 Ca
hes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.3 Linux Page Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.4 Page Allo
ation and Deallo
ation . . . . . . . . . . . . . . . . . . . . . 23
3.4.1 Page Allo
ation . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.4.2 Page Deallo
ation . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.5
3.6
3.7
3.8

Memory Mapping . . . . . . . . . . .
Demand Paging . . . . . . . . . . . .
The Linux Page Ca
he . . . . . . . .
Swapping Out and Dis
arding Pages

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

25
26
27
28

3.8.1 Redu
ing the Size of the Page and Bu er Ca
hes
3.8.2 Swapping Out System V Shared Memory Pages .
3.8.3 Swapping Out and Dis
arding Pages . . . . . . .
3.9 The Swap Ca
he . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

29
30
30
31

3.10 Swapping Pages In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
4 Pro
esses
4.1 Linux Pro
esses . . . . . . . . . . . . . . . . .
4.2 Identi ers . . . . . . . . . . . . . . . . . . . .
4.3 S
heduling . . . . . . . . . . . . . . . . . . . .
4.3.1 S
heduling in Multipro
essor Systems
4.4 Files . . . . . . . . . . . . . . . . . . . . . . .
4.5 Virtual Memory . . . . . . . . . . . . . . . .

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

35
36
38
39
41
42
43

4.6 Creating a Pro
ess . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
4.7 Times and Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
4.8 Exe
uting Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
4.8.1 ELF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
4.8.2 S
ript Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
5 Interpro
ess Communi
ation Me
hanisms
51
5.1 Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
5.2 Pipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
5.3 So
kets . . . . . . . . . . . . . . .
5.3.1 System V IPC Me
hanisms
5.3.2 Message Queues . . . . . .
5.3.3 Semaphores . . . . . . . . .

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

55
55
55
56

5.3.4 Shared Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
6 PCI
61
6.1 PCI Address Spa
es . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
6.2 PCI Con guration Headers . . . . . . . . . . . . . . . . . . . . . . . . 62
6.3 PCI I/O and PCI Memory Addresses . . . . . . . . . . . . . . . . . . . 64

6.4 PCI-ISA Bridges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
6.5 PCI-PCI Bridges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
6.5.1

PCI-PCI Bridges: PCI I/O and PCI Memory Windows . . . . 65

6.5.2

PCI-PCI Bridges: PCI Con guration Cy
les and PCI Bus
Numbering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

6.6 Linux PCI Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . 66
6.6.1

The Linux Kernel PCI Data Stru
tures . . . . . . . . . . . . . 67

6.6.2

The PCI Devi
e Driver . . . . . . . . . . . . . . . . . . . . . . 68

6.6.3

PCI BIOS Fun
tions . . . . . . . . . . . . . . . . . . . . . . . . 70

6.6.4

PCI Fixup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72

7 Interrupts and Interrupt Handling

75

7.1 Programmable Interrupt Controllers . . . . . . . . . . . . . . . . . . . 77
7.2 Initializing the Interrupt Handling Data Stru
tures . . . . . . . . . . . 77
7.3 Interrupt Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78

8 Devi
e Drivers
81
8.1 Polling and Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
8.2 Dire
t Memory A

ess (DMA) . . . . . . . . . . . . . . . . . . . . . . 84
8.3 Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
8.4 Interfa
ing Devi
e Drivers with the Kernel . . . . . . . . . . . . . . . . 85
8.4.1

Chara
ter Devi
es . . . . . . . . . . . . . . . . . . . . . . . . . 86

8.4.2

Blo
k Devi
es . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87

8.5 Hard Disks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
8.5.1

IDE Disks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90

8.5.2

Initializing the IDE Subsystem . . . . . . . . . . . . . . . . . . 91

8.5.3

SCSI Disks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91

8.6 Network Devi
es . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
8.6.1

Initializing Network Devi
es . . . . . . . . . . . . . . . . . . . . 96

9 The File system

99

9.1 The Se
ond Extended File system (EXT2) . . . . . . . . . . . . . . . . 101
9.1.1

The EXT2 Inode . . . . . . . . . . . . . . . . . . . . . . . . . . 102

9.1.2

The EXT2 Superblo
k . . . . . . . . . . . . . . . . . . . . . . . 103

9.1.3

The EXT2 Group Des
riptor . . . . . . . . . . . . . . . . . . . 104

9.1.4

EXT2 Dire
tories . . . . . . . . . . . . . . . . . . . . . . . . . . 104

9.1.5

Finding a File in an EXT2 File System . . . . . . . . . . . . . 105

9.1.6

Changing the Size of a File in an EXT2 File System . . . . . . 106

9.2 The Virtual File System (VFS) . . . . . . . . . . . . . . . . . . . . . . 107
9.2.1

The VFS Superblo
k . . . . . . . . . . . . . . . . . . . . . . . . 109

9.2.2

The VFS Inode . . . . . . . . . . . . . . . . . . . . . . . . . . . 109

9.2.3 Registering the File Systems . . . . . . . . . . . . . . . . . . . 110
9.2.4 Mounting a File System . . . . . . . . . . . . . . . . . . . . . . 110
9.2.5 Finding a File in the Virtual File System . . . . . . . . . . . . 112
9.2.6 Creating a File in the Virtual File System . . . . . . . . . . . . 112
9.2.7 Unmounting a File System . . . . . . . . . . . . . . . . . . . . 112
9.2.8 The VFS Inode Ca
he . . . . . . . . . . . . . . . . . . . . . . . 113
9.2.9 The Dire
tory Ca
he . . . . . . . . . . . . . . . . . . . . . . . . 113
9.3 The Bu er Ca
he . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
9.3.1 The bdflush Kernel Daemon . . . . . . . . . . . . . . . . . . . 116
9.3.2 The update Pro
ess . . . . . . . . . . . . . . . . . . . . . . . . . 116
9.4 The /pro
File System . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
9.5 Devi
e Spe
ial Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
10 Networks

119

10.1 An Overview of TCP/IP Networking . . . . . . . . . . . . . . . . . . . 119
10.2 The Linux TCP/IP Networking Layers . . . . . . . . . . . . . . . . . . 122
10.3 The BSD So
ket Interfa
e . . . . . . . . . . . . . . . . . . . . . . . . . 124
10.4 The INET So
ket Layer . . . . . . . . . . . . . . . . . . . . . . . . . . 125
10.4.1 Creating a BSD So
ket . . . . . . . . . . . . . . . . . . . . . . 127
10.4.2 Binding an Address to an INET BSD So
ket . . . . . . . . . . 127
10.4.3 Making a Conne
tion on an INET BSD So
ket . . . . . . . . . 128
10.4.4 Listening on an INET BSD So
ket . . . . . . . . . . . . . . . . 129
10.4.5 A

epting Conne
tion Requests . . . . . . . . . . . . . . . . . . 130
10.5 The IP Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
10.5.1 So
ket Bu ers . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
10.5.2 Re
eiving IP Pa
kets . . . . . . . . . . . . . . . . . . . . . . . . 131
10.5.3 Sending IP Pa
kets . . . . . . . . . . . . . . . . . . . . . . . . . 132
10.5.4 Data Fragmentation . . . . . . . . . . . . . . . . . . . . . . . . 133
10.6 The Address Resolution Proto
ol (ARP) . . . . . . . . . . . . . . . . . 133
10.7 IP Routing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
10.7.1 The Route Ca
he . . . . . . . . . . . . . . . . . . . . . . . . . . 135
10.7.2 The Forwarding Information Database . . . . . . . . . . . . . . 136
11 Kernel Me
hanisms

139

11.1 Bottom Half Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
11.2 Task Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
11.3 Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
11.4 Wait Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
11.5 Buzz Lo
ks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
11.6 Semaphores . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143

12 Modules

145

12.1 Loading a Module

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146

12.2 Unloading a Module

. . . . . . . . . . . . . . . . . . . . . . . . . . . . 148

13 Pro
essors
13.1 X86

151

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151

13.2 ARM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
13.3 Alpha AXP Pro
essor

. . . . . . . . . . . . . . . . . . . . . . . . . . . 152

14 The Linux Kernel Sour
es

153

A Linux Data Stru
tures

159

B Useful Web and FTP Sites

177

C Linux Do
umentation Proje
t Manifesto

179

C.1

Overview

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179

C.2

Getting Involved

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180

C.3

Current Pro je
ts

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180

C.4

FTP sites for LDP works

C.5

Do
umentation Conventions . . . . . . . . . . . . . . . . . . . . . . . . 180

C.6

Copyright and Li
ense

C.7

Publishing LDP Manuals

. . . . . . . . . . . . . . . . . . . . . . . . . 180

. . . . . . . . . . . . . . . . . . . . . . . . . . . 181
. . . . . . . . . . . . . . . . . . . . . . . . . 181

D The GNU General Publi
Li
ense
D.1

Preamble

D.2

Terms and Conditions

D.3

How to Apply These Terms

183

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
. . . . . . . . . . . . . . . . . . . . . . . . . . . 184
. . . . . . . . . . . . . . . . . . . . . . . . 188

Glossary

191

Bibliography

194

List of Figures
1.1 A typi
al PC motherboard. . . . . . . . . . . . . . . . . . . . . . . . .

2

3.1 Abstra
t model of Virtual to Physi
al address mapping . . . . . . . . 16
3.2 Alpha AXP Page Table Entry . . . . . . . . . . . . . . . . . . . . . . . 20
3.3 Three Level Page Tables . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.4 The free area data stru
ture . . . . . . . . . . . . . . . . . . . . . . . 25
3.5 Areas of Virtual Memory . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.6 The Linux Page Ca
he . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
4.1 A Pro
ess's Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
4.2 A Pro
ess's Virtual Memory . . . . . . . . . . . . . . . . . . . . . . . . 44
4.3 Registered Binary Formats . . . . . . . . . . . . . . . . . . . . . . . . 47
4.4 ELF Exe
utable File Format . . . . . . . . . . . . . . . . . . . . . . . 48
5.1 Pipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
5.2 System V IPC Message Queues . . . . . . . . . . . . . . . . . . . . . . 56
5.3 System V IPC Semaphores . . . . . . . . . . . . . . . . . . . . . . . . 57
5.4 System V IPC Shared Memory . . . . . . . . . . . . . . . . . . . . . . 59
6.1 Example PCI Based System . . . . . . . . . . . . . . . . . . . . . . . . 62
6.2 The PCI Con guration Header . . . . . . . . . . . . . . . . . . . . . . 63
6.3 Type 0 PCI Con guration Cy
le . . . . . . . . . . . . . . . . . . . . . 65
6.4 Type 1 PCI Con guration Cy
le . . . . . . . . . . . . . . . . . . . . . 65
6.5 Linux Kernel PCI Data Stru
tures . . . . . . . . . . . . . . . . . . . . 67
6.6 Con guring a PCI System: Part 1 . . . . . . . . . . . . . . . . . . . . 69
6.7 Con guring a PCI System: Part 2 . . . . . . . . . . . . . . . . . . . . 70
6.8 Con guring a PCI System: Part 3 . . . . . . . . . . . . . . . . . . . . 71
6.9 Con guring a PCI System: Part 4 . . . . . . . . . . . . . . . . . . . . 71
6.10 PCI Con guration Header: Base Address Registers . . . . . . . . . . . 72
7.1 A Logi
al Diagram of Interrupt Routing . . . . . . . . . . . . . . . . . 76
7.2 Linux Interrupt Handling Data Stru
tures . . . . . . . . . . . . . . . . 79
8.1 Chara
ter Devi
es . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
xv

8.2 Bu er Ca
he Blo
k Devi
e Requests . . . . . . . . . . . . . . . . . . . 87
8.3 Linked list of disks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
8.4 SCSI Data Stru
tures . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
9.1
9.2
9.3
9.4
9.5
9.6
9.7

Physi
al Layout of the EXT2 File system . .
EXT2 Inode . . . . . . . . . . . . . . . . . . .
EXT2 Dire
tory . . . . . . . . . . . . . . . .
A Logi
al Diagram of the Virtual File System
Registered File Systems . . . . . . . . . . . .
A Mounted File System . . . . . . . . . . . .
The Bu er Ca
he . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

101
102
105
107
110
112
114

10.1
10.2
10.3
10.4
10.5

TCP/IP Proto
ol Layers . . . . . . . .
Linux Networking Layers . . . . . . .
Linux BSD So
ket Data Stru
tures . .
The So
ket Bu er (sk bu ) . . . . . .
The Forwarding Information Database

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

121
123
126
131
136

11.1
11.2
11.3
11.4

Bottom Half Handling Data Stru
tures .
A Task Queue . . . . . . . . . . . . . . .
System Timers . . . . . . . . . . . . . .
Wait Queue . . . . . . . . . . . . . . . .

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

139
140
142
143

12.1 The List of Kernel Modules . . . . . . . . . . . . . . . . . . . . . . . . 147

Chapter 1

Hardware Basi
s

An operating system has to work
losely with the hardware system that
a
ts as its foundations. The operating system needs
ertain servi
es that
an only be provided by the hardware.

In order to fully understand

the Linux operating system, you need to understand the basi
s of the
underlying hardware.

This
hapter gives a brief introdu
tion to that

hardware: the modern PC.

When the \Popular Ele
troni
s" magazine for January 1975 was printed with an
illustration of the Altair 8080 on its front
over, a revolution started. The Altair
8080, named after the destination of an early Star Trek episode,
ould be assembled
by home ele
troni
s enthusiasts for a mere $397. With its Intel 8080 pro
essor and
256 bytes of memory but no s
reen or keyboard it was puny by today's standards.
Its inventor, Ed Roberts,
oined the term \personal
omputer" to des
ribe his new
invention, but the term PC is now used to refer to almost any
omputer that you
an pi
k up without needing help. By this de nition, even some of the very powerful
Alpha AXP systems are PCs.
Enthusiasti
ha
kers saw the Altair's potential and started to write software and
build hardware for it. To these early pioneers it represented freedom; the freedom
from huge bat
h pro
essing mainframe systems run and guarded by an elite priesthood. Overnight fortunes were made by
ollege dropouts fas
inated by this new
phenomenon, a
omputer that you
ould have at home on your kit
hen table. A lot
of hardware appeared, all di erent to some degree and software ha
kers were happy
to write software for these new ma
hines. Paradoxi
ally it was IBM who rmly
ast
the mould of the modern PC by announ
ing the IBM PC in 1981 and shipping it to
ustomers early in 1982. With its Intel 8088 pro
essor, 64K of memory (expandable
to 256K), two
oppy disks and an 80
hara
ter by 25 lines Colour Graphi
s Adapter
(CGA) it was not very powerful by today's standards but it sold well. It was followed, in 1983, by the IBM PC-XT whi
h had the luxury of a 10Mbyte hard drive.
It was not long before IBM PC
lones were being produ
ed by a host of
ompanies
su
h as Compaq and the ar
hite
ture of the PC be
ame a de-fa
to standard. This
1

power

power

parallel port
COM1

COM2

CPU

Memory SIMM Slots

PCI Slots

ISA Slots

Figure 1.1: A typi
al PC motherboard.
de-fa
to standard helped a multitude of hardware
ompanies to
ompete together in
a growing market whi
h, happily for
onsumers, kept pri
es low. Many of the system
ar
hite
tural features of these early PCs have
arried over into the modern PC. For
example, even the most powerful Intel Pentium Pro based system starts running in
the Intel 8086's addressing mode. When Linus Torvalds started writing what was
to be
ome Linux, he pi
ked the most plentiful and reasonably pri
ed hardware, an
Intel 80386 PC.
Looking at a PC from the outside, the most obvious
omponents are a system box,
a keyboard, a mouse and a video monitor. On the front of the system box are some
buttons, a little display showing some numbers and a
oppy drive. Most systems
these days have a CD ROM and if you feel that you have to prote
t your data, then
there will also be a tape drive for ba
kups. These devi
es are
olle
tively known as
the peripherals.
Although the CPU is in overall
ontrol of the system, it is not the only intelligent
devi
e. All of the peripheral
ontrollers, for example the IDE
ontroller, have some
level of intelligen
e. Inside the PC (Figure 1.1) you will see a motherboard
ontaining
the CPU or mi
ropro
essor, the memory and a number of slots for the ISA or PCI
peripheral
ontrollers. Some of the
ontrollers, for example the IDE disk
ontroller
may be built dire
tly onto the system board.

1.1

The CPU

The CPU, or rather mi
ropro
essor, is the heart of any
omputer system. The mi
ropro
essor
al
ulates, performs logi
al operations and manages data
ows by reading
instru
tions from memory and then exe
uting them. In the early days of
omput-

ing the fun
tional
omponents of the mi
ropro
essor were separate (and physi
ally
large) units. This is when the term Central Pro
essing Unit was
oined. The modern
mi
ropro
essor
ombines these
omponents onto an integrated
ir
uit et
hed onto
a very small pie
e of sili
on. The terms CPU, mi
ropro
essor and pro
essor are all
used inter
hangeably in this book.
Mi
ropro
essors operate on binary data; that is data
omposed of ones and zeros.
These ones and zeros
orrespond to ele
tri
al swit
hes being either on or o . Just
as 42 is a de
imal number meaning \4 10s and 2 units", a binary number is a series
of binary digits ea
h one representing a power of 2. In this
ontext, a power means
the number of times that a number is multiplied by itself. 10 to the power 1 ( 101 )
is 10, 10 to the power 2 ( 102 ) is 10x10, 103 is 10x10x10 and so on. Binary 0001 is
de
imal 1, binary 0010 is de
imal 2, binary 0011 is 3, binary 0100 is 4 and so on. So,
42 de
imal is 101010 binary or (2 + 8 + 32 or 21 + 23 + 25 ). Rather than using binary
to represent numbers in
omputer programs, another base, hexade
imal is usually
used. In this base, ea
h digital represents a power of 16. As de
imal numbers only
go from 0 to 9 the numbers 10 to 15 are represented as a single digit by the letters
A, B, C, D, E and F. For example, hexade
imal E is de
imal 14 and hexade
imal 2A
is de
imal 42 (two 16s) + 10). Using the C programming language notation (as I do
throughout this book) hexade
imal numbers are prefa
ed by \0x"; hexade
imal 2A
is written as 0x2A .
Mi
ropro
essors
an perform arithmeti
operations su
h as add, multiply and divide
and logi
al operations su
h as \is X greater than Y?".
The pro
essor's exe
ution is governed by an external
lo
k. This
lo
k, the system
lo
k, generates regular
lo
k pulses to the pro
essor and, at ea
h
lo
k pulse, the
pro
essor does some work. For example, a pro
essor
ould exe
ute an instru
tion
every
lo
k pulse. A pro
essor's speed is des
ribed in terms of the rate of the system
lo
k ti
ks. A 100Mhz pro
essor will re
eive 100,000,000
lo
k ti
ks every se
ond. It
is misleading to des
ribe the power of a CPU by its
lo
k rate as di erent pro
essors
perform di erent amounts of work per
lo
k ti
k. However, all things being equal, a
faster
lo
k speed means a more powerful pro
essor. The instru
tions exe
uted by the
pro
essor are very simple; for example \read the
ontents of memory at lo
ation X
into register Y". Registers are the mi
ropro
essor's internal storage, used for storing
data and performing operations on it. The operations performed may
ause the
pro
essor to stop what it is doing and jump to another instru
tion somewhere else in
memory. These tiny building blo
ks give the modern mi
ropro
essor almost limitless
power as it
an exe
ute millions or even billions of instru
tions a se
ond.
The instru
tions have to be fet
hed from memory as they are exe
uted. Instru
tions
may themselves referen
e data within memory and that data must be fet
hed from
memory and saved there when appropriate.
The size, number and type of register within a mi
ropro
essor is entirely dependent
on its type. An Intel 4086 pro
essor has a di erent register set to an Alpha AXP
pro
essor; for a start, the Intel's are 32 bits wide and the Alpha AXP's are 64 bits
wide. In general, though, any given pro
essor will have a number of general purpose
registers and a smaller number of dedi
ated registers. Most pro
essors have the
following spe
ial purpose, dedi
ated, registers:
This register
ontains the address of the next instru
tion
to be exe
uted. The
ontents of the PC are automati
ally in
remented ea
h
time an instru
tion is fet
hed,

Program Counter (PC)

Pro
essors have to have a

ess to large amounts of external
read/write random a

ess memory (RAM) whi
h fa
ilitates temporary storage
of data. The sta
k is a way of easily saving and restoring temporary values in
external memory. Usually, pro
essors have spe
ial instru
tions whi
h allow you
to push values onto the sta
k and to pop them o again later. The sta
k works
on a last in rst out (LIFO) basis. In other words, if you push two values, x
and y, onto a sta
k and then pop a value o of the sta
k then you will get ba
k
the value of y.

Sta
k Pointer (SP)

Some pro
essor's sta
ks grow upwards towards the top of memory whilst others
grow downwards towards the bottom, or base, of memory. Some pro
essor's
support both types, for example ARM.
Instru
tions may yield results; for example \is the
ontent
of register X greater than the
ontent of register Y?" will yield true or false as
a result. The PS register holds this and other information about the
urrent
state of the pro
essor. For example, most pro
essors have at least two modes
of operation, kernel (or supervisor) and user. The PS register would hold
information identifying the
urrent mode.

Pro
essor Status (PS)

1.2

Memory

All systems have a memory hierar
hy with memory at di erent speeds and sizes at
di erent points in the hierar
hy. The fastest memory is known as
a
he memory and
is what it sounds like - memory that is used to temporarily hold, or
a
he,
ontents
of the main memory. This sort of memory is very fast but expensive, therefore most
pro
essors have a small amount of on-
hip
a
he memory and more system based (onboard)
a
he memory. Some pro
essors have one
a
he to
ontain both instru
tions
and data, but others have two, one for instru
tions and the other for data. The
Alpha AXP pro
essor has two internal memory
a
hes; one for data (the D-Ca
he)
and one for instru
tions (the I-Ca
he). The external
a
he (or B-Ca
he) mixes the
two together. Finally there is the main memory whi
h relative to the external
a
he
memory is very slow. Relative to the on-CPU
a
he, main memory is positively
rawling.
The
a
he and main memories must be kept in step (
oherent). In other words, if
a word of main memory is held in one or more lo
ations in
a
he, then the system
must make sure that the
ontents of
a
he and memory are the same. The job of
a
he
oheren
y is done partially by the hardware and partially by the operating
system. This is also true for a number of major system tasks where the hardware
and software must
ooperate
losely to a
hieve their aims.
1.3

Buses

The individual
omponents of the system board are inter
onne
ted by multiple
onne
tion systems known as buses. The system bus is divided into three logi
al fun
tions; the address bus, the data bus and the
ontrol bus. The address bus spe
i es
the memory lo
ations (addresses) for the data transfers. The data bus holds the data
transfered. The data bus is bidire
tional; it allows data to be read into the CPU and
written from the CPU. The
ontrol bus
ontains various lines used to route timing

and
ontrol signals throughout the system. Many
avours of bus exist, for example
ISA and PCI buses are popular ways of
onne
ting peripherals to the system.

1.4

Controllers and Peripherals

Peripherals are real devi
es, su
h as graphi
s
ards or disks
ontrolled by
ontroller
hips on the system board or on
ards plugged into it. The IDE disks are
ontrolled
by the IDE
ontroller
hip and the SCSI disks by the SCSI disk
ontroller
hips and
so on. These
ontrollers are
onne
ted to the CPU and to ea
h other by a variety
of buses. Most systems built now use PCI and ISA buses to
onne
t together the
main system
omponents. The
ontrollers are pro
essors like the CPU itself, they
an be viewed as intelligent helpers to the CPU. The CPU is in overall
ontrol of the
system.
All
ontrollers are di erent, but they usually have registers whi
h
ontrol them.
Software running on the CPU must be able to read and write those
ontrolling
registers. One register might
ontain status des
ribing an error. Another might be
used for
ontrol purposes;
hanging the mode of the
ontroller. Ea
h
ontroller on
a bus
an be individually addressed by the CPU, this is so that the software devi
e
driver
an write to its registers and thus
ontrol it. The IDE ribbon is a good example,
as it gives you the ability to a

ess ea
h drive on the bus separately. Another good
example is the PCI bus whi
h allows ea
h devi
e (for example a graphi
s
ard) to be
a

essed independently.

1.5

Address Spa
es

The system bus
onne
ts the CPU with the main memory and is separate from the
buses
onne
ting the CPU with the system's hardware peripherals. Colle
tively the
memory spa
e that the hardware peripherals exist in is known as I/O spa
e. I/O
spa
e may itself be further subdivided, but we will not worry too mu
h about that
for the moment. The CPU
an a

ess both the system spa
e memory and the I/O
spa
e memory, whereas the
ontrollers themselves
an only a

ess system memory
indire
tly and then only with the help of the CPU. From the point of view of the
devi
e, say the
oppy disk
ontroller, it will see only the address spa
e that its
ontrol registers are in (ISA), and not the system memory. Typi
ally a CPU will
have separate instru
tions for a

essing the memory and I/O spa
e. For example,
there might be an instru
tion that means \read a byte from I/O address 0x3f0 into
register X". This is exa
tly how the CPU
ontrols the system's hardware peripherals,
by reading and writing to their registers in I/O spa
e. Where in I/O spa
e the
ommon peripherals (IDE
ontroller, serial port,
oppy disk
ontroller and so on)
have their registers has been set by
onvention over the years as the PC ar
hite
ture
has developed. The I/O spa
e address 0x3f0 just happens to be the address of one
of the serial port's (COM1)
ontrol registers.
There are times when
ontrollers need to read or write large amounts of data dire
tly
to or from system memory. For example when user data is being written to the
hard disk. In this
ase, Dire
t Memory A

ess (DMA)
ontrollers are used to allow
hardware peripherals to dire
tly a

ess system memory but this a

ess is under stri
t
ontrol and supervision of the CPU.

1.6

Timers

All operating systems need to know the time and so the modern PC in
ludes a spe
ial
peripheral
alled the Real Time Clo
k (RTC). This provides two things: a reliable
time of day and an a

urate timing interval. The RTC has its own battery so that
it
ontinues to run even when the PC is not powered on, this is how your PC always
\knows" the
orre
t date and time. The interval timer allows the operating system
to a

urately s
hedule essential work.

Chapter 2

Software Basi
s

A program is a set of
omputer instru
tions that perform a parti
ular task.
That program
an be written in assembler, a very low level
omputer
language, or in a high level, ma
hine independent language su
h as the
C programming language.

An operating system is a spe
ial program

whi
h allows the user to run appli
ations su
h as spreadsheets and word
pro
essors.

This
hapter introdu
es basi
programming prin
iples and

gives an overview of the aims and fun
tions of an operating system.

2.1
2.1.1

Computer Languages
Assembly Languages

The instru
tions that a CPU fet
hes from memory and exe
utes are not at all understandable to human beings. They are ma
hine
odes whi
h tell the
omputer
pre
isely what to do. The hexade
imal number 0x89E5 is an Intel 80486 instru
tion
whi
h
opies the
ontents of the ESP register to the EBP register. One of the rst
software tools invented for the earliest
omputers was an assembler, a program whi
h
takes a human readable sour
e le and assembles it into ma
hine
ode. Assembly
languages expli
itly handle registers and op

Dokumen yang terkait

ALOKASI WAKTU KYAI DALAM MENINGKATKAN KUALITAS SUMBER DAYA MANUSIA DI YAYASAN KYAI SYARIFUDDIN LUMAJANG (Working Hours of Moeslem Foundation Head In Improving The Quality Of Human Resources In Kyai Syarifuddin Foundation Lumajang)

1 46 7

Analisis Komparasi Internet Financial Local Government Reporting Pada Website Resmi Kabupaten dan Kota di Jawa Timur The Comparison Analysis of Internet Financial Local Government Reporting on Official Website of Regency and City in East Java

19 819 7

FAKTOR-FAKTOR YANG BERPENGARUH TERHADAP PENDAPATAN TENAGA KERJA PENGRAJIN ALUMUNIUM DI DESA SUCI KECAMATAN PANTI KABUPATEN JEMBER The factors that influence the alumunium artisans labor income in the suci village of panti subdistrict district jember

0 24 6

The Correlation between students vocabulary master and reading comprehension

16 145 49

The correlation intelligence quatient (IQ) and studenst achievement in learning english : a correlational study on tenth grade of man 19 jakarta

0 57 61

An analysis of moral values through the rewards and punishments on the script of The chronicles of Narnia : The Lion, the witch, and the wardrobe

1 59 47

The Effectiveness of group work in teaching reading: a quasi experimental study at the second grade of Mts Neger Parung

0 25 102

Analyzing The Content Validity Of The English Summative Tests In Vocational Schools (A Case Study In Odd Semester Of Second Year Technology Major In Tangerang Vocational Schools)

1 50 155

The Effectiveness of Computer-Assisted Language Learning in Teaching Past Tense to the Tenth Grade Students of SMAN 5 Tangerang Selatan

4 116 138

The correlation between listening skill and pronunciation accuracy : a case study in the firt year of smk vocation higt school pupita bangsa ciputat school year 2005-2006

9 128 37