Escolar Documentos
Profissional Documentos
Cultura Documentos
//
//
//
//
//
The assembly instructions are in turn translated into machine instructions. When the CPU wants
to execute a program it first stores (by reading it from storage device) that program (in
machine language format, of course) in the computer's main memory (RAM or random access
memory). Along with the program, memory can also hold data that is being used or processed
by the program. Main memory consists of a sequence of locations. These locations are
numbered, and the sequence number of a location is called its address. An address provides a
way of picking out one particular piece of information from among the millions stored in
memory. The CPU executes a program that is stored as a sequence of machine language
instructions in main memory. It does this by repeatedly fetching an instruction from memory
and then executing that instruction. This process of fetch an instruction, executes it, fetch
another instruction, execute it, and so on forever -- is called the fetch-and-execute cycle.
As shown in Figure 1, our example assembly code is translated into machine language and
loaded into the main memory. The program counter (PC) will hold the address of the next
instruction to be executed (we are assuming that each machine instruction is encoded in such a
way that it takes 4 bytes each). So, as in Figure 1, when the first instruction is ready to be
executed (fetched from memory location and decoded) in the instruction register (IR), the PC is
pointing towards the next instruction (PC=PC+4) in-line to be executed. Let us now take a
detailed overview of the fetch and execution cycle.
To start off the fetch cycle, the address which is stored in the PC is transferred to the memory
address register (MAR). The CPU then transfers the instruction located at the address stored in
the MAR to the memory buffer register (MBR) via the data lines connecting the CPU to memory.
This transfer from memory to CPU is coordinated by the control unit (CU). To finish the cycle,
the newly fetched instruction is transferred to the IR and unless told otherwise, the CU
increments (by size of instruction) the PC to point to the next address location in memory. All
these require several clock cycle to finish. After the CPU has finished fetching an instruction, the
CU checks the contents of the IR and determines which type of execution is to be carried out
load
load
add
store
Memory
Address
Fetch &
Execute
Logic
PC
R3,
R4,
R3,
R3,
b
c
R4
a
3046 1011111000101010..1
3050
3050 1011111000110011..0
IR
3054 1111101010101010..1
load R3, b
3058 1010110001010101..1
CPU
Machine
language
RAM
Start
Decode instruction
in IR
Privileged
?
Yes
U/S bit=1
?
Save current PC
No
No
No
Any
Interrupt ?
Yes
Execute instruction
in IR
Set user/supervisor
bit to 1
Yes
Set user/supervisor
bit to 1
IN HARDWARE
IN HARDWARE
Save current PC
Start
Privileged
?
Fetch an instruction from a
memory address, found in
the PC
Yes
PC Address >
PAGE_OFFSET
?
No
Is it in Kernel
Segment?
?
No
U/S bit =1
?
Yes
Yes
Destination=
mode register
?
Is it in Kernel
Segment
?
Yes
No
Decode instruction
in IR
No
Its a
TRAP
?
Yes
No
No
Any
Interrupt ?
Execute instruction
in IR
Yes
Save current PC
IN HARDWARE
Figure 3: Instruction Fetch & Execution Cycle in Intel Architecture
ROM
Power Up
POST
Loader
BIOS
OS
Boot Device
CMOS
Hardware Process
Data Flow
After the loader completely loads the Linux kernel into the memory, it will first uncompress itself
as the Linux kernel is installed compressed. This is due to several reasons but the important
reason is that copying a small compressed kernel is much faster then copying a big
uncompressed kernel. After that, the kernel does the following tasks to create a proper
environment for user programs to run:
Checks what hardware is there and configures some of its device drivers
appropriately.
Enable virtual memory/paging support.
Do memory layout analysis and initializations.
Initialize and enable interrupts.
Do other initializations, such as, scheduling, timers etc.
Mount root file system.
Create the first kernel thread (init) that performs operating system environment
initializations.
Go into the idle loop, which is the loop executing when nothing else is to be
scheduled.
After all that the system is truly running and ready for user commands and user tasks. It will
then print a prompt so that user can login to the system and run their processes.
Figure 6 shows the overall sketch of the system booting process in Linux.
STARTUP
BIOS POST
Either
KERNEL
STARTUP
ENVIRONMENT
INITIALIZATION
idle loop
Kernel source:
arch/i386/kernel/process.c
Nothing
to do
init thread
Kernel source: init/main.c
Ready to
rock & roll
Figure 6: PC boot process and kernel source code accessed during booting.
References:
1. The Fetch and Execute Cycle: Machine Language, http://math.hws.edu/javanotes/
c1/s1.html.
2. Operating Systems, Gary Nutt, Addison Wesley Publication, Third Edition, 2004.
3. Operating Systems, William Stallings, Prentice Hall Inc., 1995.
4. The Linux System Administrator's Guide, Version 8, Lars Wirzenius, http://www.tldp.org/
LDP/sag/sag.pdf.
5. Linux Red Hat 9 Reference Guide, http://www.redhat.com/docs/manuals/linux/RHL-9Manual/pdf/rhl-rg-en-9.pdf.
6. Linux Kernel 2.4 Internals, Tigran Aivazian, http://www.moses.uklinux.net/patches/lki.html.
7. The Linux/i386 boot protocol, Linux Redhat Source Code, Documentation/i386/boot.txt.