Appendix Zero: How computers work ?
In this appendix we are going to discuss some basic background knowledge
about computers, how do they work and take a deep look
at concepts of OSs and system software.
Section quotes:
- "Computers don't actually think. You just think they think.
(We think. They don't)"
-- found on 'fortune-mod'
- "The question of whether computers can think is just like the question of
whether submarines can swim."
-- Edsger W. Dijkstra
- "Where a calculator on the ENIAC is equpped with 18,000 vaccuum tubes and
weighs 30 tons, computers in the future may have only 1,000 vaccuum tubes
and perhaps weigh 1 1/2 tons."
-- Popular Mechanics, March 1949
- "A computer lets you make more mistakes faster than any other invention,
with the possible exceptions of handguns and Tequilla."
-- Mitch Ratcliffe
- "The human mind ordinarily operates at only ten percent of its capacity,
the rest is overhead for the operating system."
-- found on 'fortune-mod'
Section contents:
What is the computer ?
If we define computer as a machine that help us (humans) to do
arithmatic calculation then we should consider wooden abacuses
(from the Chinese civilization) to be a computer.
Well, it was the first trial, then it comes some mechanical tools
like the mechanical counter made by Pascal which is composed of
gears on cylenders rolling to represent addition, subtraction, multiplication
and division on integers, yet those are not actually computers.
The need for fast calculation regarding projectiles and ciphers
in the world war appeard. Talent humans provided with mathematical
tables were used, but this was not enough, so researches for
automatic calculating machines were funded, but non were accomplished
before the end of the two wars.
First there were an electromechanical machines that utilize Relays
(still used nowadays to control the blinking lights of cars and buses),
Mark1 was made in 1944 as an example of those, it uses 3000 Relay,
and it could do 3 addition operations in a second.
After the war, companies like IBM and some universites continued
the military researches, and the first generation of computers was made.
The generation of vaccuum tubes (electronic valves), they have short life
time, consume alot of power and generate alot of heat, such computers
were very large at size (more than 30 tons) so large that experts
could not even imagin future computers with less than 1.5 tons!
and they were having flashing lights as human interface,
the required manipulations (program instruction) is given through attaching
points with wires on a board.
The most famou one was ENIAC with about 18000 vaccuum tubes
and capable of doing some thousands of addition operations in a second.
ENIAC Electronic Numerical Integrator And Computer 1943-1946
EDVAC Electronic Discrete Variable Automatic Computer 1946-
EDSAC Electronic Delay Storage Automatic Computer
UNIVAC UNIVersal Automatic Computer 1951
IBM 650 - IBM 709
it was remarkable because of the fact that it was not just a calculator nor
a specific task computer for military applications (eg. cryptography),
it was a general purpose computer that instructions could be given
to be executed, a sort of what later called computer program could be given.
When the Transistor was discovered, it find it's place in this
industry, it's smaller, needs less power, generate less heat (thus has loner life).
This lead to the second generation of computers 1950s-1960s,
high level computer languages (those are more close to humans than
hardware) appeared, they are slower than machine language and low
level languages. FORTRAN 56 was one of the first high level languages.
IBM 1401 was one of the second generation computers.
Integrated Circuits (ICs) invention lead to the third generation of
computers(1970s). An IC is a small silicon ship having all types of
electronic componenets including Transistors
(many thousands of Transistors could be put in a 0.5 cm2 ship).
With this generation many new concepts appeared like "Microcomputer" and "Operating system",
it is the time of C programming language, the computer language
that changed the world of computing, it's a high level language
that give the programmer access to the full instruction set of the computer
and do any thing as fast as low level languages.
Microcomputer (or Personal Computer) is a commercial slower smaller
computer that needs no entire floor, no special cooling system,
and it fits on a desk at someones home or office and needs no specialized
expert to operate. On the other world, somewhere else (universites,laboratories,military,...etc),
UNIX appeared at Bell Labs, this system introduce many concept, it was
cabable of running on a computer (called server) having many terminals
(a keyboard and a monitor) so users on each working indepedently
apperentely at the same time by giving each a small slice of time,
this is called Time Sharing, many users could access to the
system which is called Multi user
(which is defined by fortune-mod as "a computer using many humans!")
and even on a single terminal a user could run many programs
at the same time (called Multi tasking)
The fourth generation of computers appeard, where there is no
difference between large and microcomputers in terms of performence,
the main difference was in the usage, consepts like Graphical User
Interface (GUI) and "User friendly" in the consumer market computing
while on UNIX they want security, stability and reliability.
Nowadays you may hear about the fifth generation,
featuring Artificial Intelligence (AI) or neural networks
but this is just a media exaggeration for terms they don't understand,
since AI does exist since the third generation,
and the Backgammon game found on many GNU/Linux distributions,
GNU backgammon (gnubg) is a neural networks application!
The fifth generation is expected to be a result of a new invention
like super connectivity, light based CPU (instead of electeronics),
a composition on electronic Silicon ships with biologogical technology.
Why do we need computers ?
From generation to generation, computers do more and more jobs,
not just addition operations. The computer today is general purpose
machine, even special purpose computers (like game consoles) are
general purpose computers running special purpose programs.
Why do we need computers ? this importent question that should
be asked before you buy one (specially at a company or a governmental office),
computers cost money, it's not for decoration, it's not to encourage tourism!
not just the cost of computers but you have to consider the cost of
support service, software development, operators and courses for employees
(like cars you should consider the cost of it's fuel)
why we pay all that ? does it worth ?
Computers do more work at less time, saving time, money and employees,
the resulted unemployement is not a problem because
"Machines should work. People should think." (attributed to IBM)
if the work could be done with one man using the computer then
the rest are overload! instead of moving from place to place
collecting signatures holding a big folder this could be done
at one place through computer networks, large paper archives
could be replaced with computerized databases, all kinds
of reports (no matter how old, how large...etc) could
be given to decision makers, not just we save time and money
but also we have better accuracy.
We need computers to automate tasks, in order to get things doen automatically
with least possible human effort, if this is fulfilled then computers
are used correctely not just a decoration for the office.
Warning
In developing counteries computers are just another way to wast tax money,
getting more unneeded people employeed, and an excuse for human errors,
when you go to a bank there, it's easer for them to told you
the computer is broken, your name is not here, your file at our
office at the other side of the country!
when you go to some office, you may hear songs coming out
from the multimedia player at the computer, while the employee
search paper archives for your file, the othor employee
seems busy and he hardly gives you any attention while he is really
playing solitaire game!
When making economical comparison between two computer systems
you have to calculate benefits over price, a computer with flat LCD screen and
subwoufer speakers used as data entry terminal has no benefit
over black and white CRT, the difference in price here is wasted.
Where as such recent machine (with LCD and speakers) would be
meaningful at sales poits where it could be very useful to
the client to know more about the product.
A "computerized" education program that show a non-interactive
bear reading "one plus one equals two, yes correct!"
does not differ from putting that on a cassette, it does not
attract students. If one presents a slide show where questions
and answers appear one by one over a nice background of waterfalls,
students usually get bored and play with shadow on the white screen!
Tip
Presenting a shiny blue slide show, text falling as rain with sound
effects of gun shoting, showing pictures of high buildings, revers, trees,
smilling girls or whatever will not change the fact that this company
is loosing and will not convince me to buy.
The Software.
"Those parts of the system that you can hit with a hammer (not advised)
are called hardware; those program instructions that you can't hit them
with a hammer, but you can only curse (not advised) at are called software."
(adapted from "Levitating Trains and Kamikaze Genes: Technological Literacy for the 1990's")
General purpose computers are designed to do different kinds of manipultions,
they are given to them as a sequence of instructions to be executed,
instructions kept in a storage medium, read, and loaded into main memory
then executed one by one with no human interaction unless those
instructions specify so (eg. ask the user to enter a number),
computer works silentely unless the instruction specify it should
display something (print something or produce a sound).
The sequence of instructions is called a program.
Programs could be written on high level language that is very close
to humans and mathematical symbools, the file containing human
readable computer (high level) instructions is called source code file
write "Enter width :"
ask for W
write "Enter height :"
ask for H
calculate A=W*H
write "Rectangle area is " A
or it could be written in a low level language like assembly,
the following assembly code that multiply width with height
and put the result in area
(without printing question, asking for width and height nor printing the result)
; intel styled assembly code
; some code to display & enter the values
; ...
; some code to calculate area
mov eax,width
mov ebx,height
mult eax,ebx
mov area,eax
; some code to display the result
; ...
assembly language is just machine language but using symbols
instead of instruction numbers (which is useful to eliminate many typing mistakes)
for example the assembly instruction "int 0x80"
means in machine code (of Intel 80x86) the hexadecimal code
"0xcd80", or in binary code "11001101-10000000",
this substitution is done by a program called assembler.
A high level language source code of a program is translated to machine codes
(the only code understod by computers)
each time the program is called using another program called interupter,
or the entire source file is converted to produce a machine code file
called binary file or precompiled file,
once and for all, this process is called compiling
and done by a program called compiler.
Now we may ask "How do I program my favorite game ?" ,
actually users should not do that, you buy software (or software license)
or get it from the internet, install it and use it.
But if you are interested to become a programmer, we
provide a drop from an ocean in this book (refere to programming chapters)
How does the computer work ?
An electric gate (circuit) has two states connected (on or active, ie allow electric
current to pass) denoted by 1 or disconnected (off) denoted by 0
just like a light bulb switch, the numbers 0 and 1 have nothing todo
with current intensity (in ambers) nor potential difference (in volts),
but in deed it works only in a specific range of voltage where it's
assumed 1, below that it's 0. There is nothing between 0 and 1,
there is not big 1 and small 1, this is called digital signal,
the oppsite is called analog signal which takes a wave form,
the higher the voltage, the higher the value it represents,
thats why there are more interference here, but in digital signals
there are no clear 1 and unclear 1.
We (humans) form any number as digits of 10 numbers
from the set {0,1,2,3,4,5,6,7,8,9} this is called decimal system,
each digit has 10 times value of the one to the right (eg. 3, 30, 300....etc)
The binary system formed by 0s and 1s where each digit has 2 time the value
of the one to the right, for example (1)b means 1
but (10)b means 2 and (100)b means 4
000001b= 1d
000010b= 2d
000100b= 4d
001000b= 8d
010000b=16d
100000b=32d
if there are more than 1 add the value of each, this could be done
very easy by writing the value of each digit above it
001011b= 001000b+000010b+000001b=8d+2d+1d=11d
32 16 8 4 2 1
0 0 1 0 1 1b= 1d+2d+8d = 11d
to do the opposite conversion we divide by the largest expected
power of 2 , to convert 11 from decimal to binary, divide it by 32 to
get 0 and a remainder of 11 which is denoted like this 11/32=0r11
put the result (0) to the left most digit, take the remainder and divide it
by half of what we divide it before, 11/16=0r11 and put the result
to the second left most digit, do that again 11/8=1r3
and again 3/4=0r3 and again 3/2=1r1 and
one more time 1/1=1r0 t last we get (001011)b.
The addition of binary numbers is done exactely as decimals,
where we have only 4 possibilities for digits those are:
0b+0b=0b,
0b+1b=1b,
1b+0b=1b and
1b+1b=(10)b
(which mean you put 0 and 1 is overloaded to next digit),
see the following example
3d + 13d = ?
3d = 000011b
13d = 001101b
1111
000011b
001101b +
----------
010000b --> 16d
Subtraction and nevative numbers are handled by using the left most bit
as sign bit, if it's 0 then it indicate a non-negative number (0 or positive)
represented normally by the rest bits, but if it's 1 then it indicate
a negative number, that is minus the second complement of rest bits.
The second complement of a number equals the first complement
plus one, the first complement (also called binary negation, in C we write
~ prefix) is found by tuggling all 1s to 0s and 0s to 1s. The
following example show this with 7 digits for value (and one for sign):
00001101b=13d
11110011b= ??
~11110011b=00001100b
11110011b=-(00001100b+1)=-(00001101b)=-13d
wondering why did they use such a freakish way ?
try to add a number and it's negative value, you should get 0
(there is a 1 digit overload, but we take the right most 8 bits)
13d + (-13)d = 0
1 1111111
00001101b
11110011b +
------------
1 00000000b
this is very close to a counter (based on gears, like Km counter in a car,
or cassette recoderes) if it's on 0 and you move it one step backward
you will get 9999 (to indicate -1), two steps backward from 0 to get 9998
(to indicate -2), imagin a binary counter
starting from 00000000b, one step backward
11111111b or two 11111110b
to represent -1 or -2.
Multiplication and division instructions are more complicated and usually
take loner time to be executed. As a special case, multiplication or division
with any (integer) power of two could be done by shifting digits to left
or right (respectively) in the number of the power minus 1, just like
multiplication by powers of 10 in decimal system:
11d x 8d = 11d x (2)3d
0001011b << 2d = 1011000b = 88d
we have discussed just integer operations, real numbers or rational numbers
are treaded in the (approximated) form of float-point, which is very
similar to enineering form like when we write speed of light equals
0.3x(10)9 Km/s, the charge of electron
0.16x(10)-18 C.
The first digit next to the poing does not represent a tenth,
but actually it depends on the power on 10, a float point number
is formed from mantissa (the fractional part) and exponent (the integer power).
Beside arithmatics, computers could do some operations like
"AND", "OR" and "XOR" operations, both logically and binary.
With logical operations it takes the whole value is it 0 (false) or not (true),
with binary operation it apply it on each digit.
"OR" (| in C) could be used to put 1 in a digit no matter whether it was 0 or 1 before.
"XOR" (^ in C) gives 1 only if exactely one of the operands is 1 (not both)
could be used to tuggle digits by applying them to straight 1s
(or used to trun off digits by applying them to themselves)
"AND" (& in C) gives 1 only if both operands are 1s
and could be used to apply masks. Here an example:
110110b 110110b 110110b
001100b | 001100b & 001100b ^
-------- -------- --------
111110b 000100b 111010b
Computer units.
Computer main units are:
- Input Units
- Output Units
- Central Processing Unit (CPU)
- Arithmatics and Logic Unit (ALU)
- Control Unit (CU)
- Registers
- Level 1 Cache Memory
- Level 2 Cache Memory
- Main memory (primary storage)
- Random Access Memory (RAM)
- Read Only Memory (ROM)
- Secondary storage.
Tip
RAM is not the only random access memory.
Input units get entries from the user (eg. keyboard and mouse)
Output units produce results for the user (eg. monitor and printer).
The processor and some other elements forms the brian of the computer
that is CPU. Processing is done by it's two units:
Control unit (fetch next instruction) and ALU (do the processing),
although it could read data from hard disk or RAM but
it can't operate on them directely but instead after being
put into very small memory called registers,
each register could only hold specific small amount of data called
word, it's the largest unit that the processor could manipulate at once
(in x86 after 386 it's 32-bits).
Main memory (eg. RAM) is the place where instructions (programs) should be
in order to be executed, putting them there called loading.
Level 1 cache is a small expensive memory that is faster than main memory
and nearer to the processor, used to speed frequent access to main memory.
Similarely Level 2 cache with secondary storage.
Operating systems - the kernel.
When you turn on the computer a Frameware (program saved on the ROM) called
Basic Input/Output System (BIOS) is executed.
Tip
ROM is not actually read only, then how it comes BIOS is loaded on it ?
there are types of ROM could be reprogrammed with some special devices or rays
usually by the manufacturer.
this program do some checks called Power On Self Test (POST)
like RAM Parity check, reset and initiate some devices, if every thing
is fine, it loads and execute a program called Boot-Loader.
The main job for BIOS when it was deigned is to provide
routines (BIOS Calls) that could be called to ease writing system software.
Machine Language (and Assembly) provide instructions to do arithmatic,
logical and main memory manipulations but it does not provide any
way to handle sound card, hard disk nor monitor (also files but it's not our case),
those devices could be accessed through their ports (has nothing todo with networks)
called I/O Ports. In machine language and Assembly (and C)
a way to send an integer (byte or more) to a specified I/O port number,
different devices have different forms of methods to deal with them,
and even same device could have many possibilities
(eg. Sound Blaster sound card
could have base I/O port of 220, 230,... to 270).
Tip
If two devices take the same I/O port numbers we call this conflict,
cards used to be configured to specify different port
using jumper switches, but now I/O ports of ISA cards that support
PNP (Plug and play) are specified by software (PNP OS or PNP BIOS)
This problem do not even exist with PCI cards where PNP is meaningless.
A device could take more than one I/O port, one for status
(ready, expecting command, expecting arguments,...),
other for commands, ...etc. writing a program in such way
is very annoying, it goes like this "wait till ready status",
"write command", "wait a change in status",...etc.
BIOS calls provide a library of routines that could be called through
interrupt using "int" instruction (in Assembly) followed
by a number representing a BIOS utility, for example
"int 10h" is for display BIOS routines,
there is a fixed memory address holding a table of memory addresses
of each interrupt, that instruction read the address corresponding to
interrupt number 10 (in hexadecimal) and call that address,
what you want to do is specified through registers
before you call "int", for exmple register AH (in x86) specify
the number of the function you want, writing a character to screen for example.
After loading the boot loader, it (the boot loader) continues to load
the operating system. The Operating System (OS) offers hight level
routines compared with BIOS calls, for example, BIOS deals with
hard disks through Sector/Head/Cylinder adresses
(read to address numbered "SS/HH/CC" and write to address "ss/hh/cc")
while the OS adds the higher level of files (the file name "/so/and/so").
The OS adds it's routines to interrupts table, for example
in DOS the main OS interrupts is 21 (hexadecimal),
and the function number to open a file is 3D (hexadecimal)
which should be put in register AH before calling 'int 0x21'
myfile db "readme.txt"
mov dx, myfile
mov ax,0x3D00
int 0x21
one of OS advantages is that its interrupts are hardware independent,
it's the same for different platform (but some famous OSs work only
on one platform!).
Linux kernel uses interrupt number 80 (hexadecimal),
and it offer standard POSIX functions listed on
the file "asm/unistd.h" as "__NR_systemcallname",
function number should be placed on EAX register and arguments
are passed throuh other registers EBX, ECX, EDX, ESI, EDI and EDP
before calling "int 0x80".
The following example writes the famous "Hello, world!"
string to standard output, it's taken from "Assembly-HOWTO"
by Konstantin Boldyshev(1999-2002) and Francois-Rene Rideau(1996-1999):
; hello.asm - hello world in NASM asm (from Assembly-HOWTO)
; type 'nasm -f elf hello.asm && ld -s -o hello hello.o'
; sections are .text (ro code) .data (rw data) .bss (rw uninitialized data)
section .data
msg db "Hello, world!",0x0a
len equ $ - msg
section .text
global _start
_start:
; write(1,msg,len); // 1 is stdout filedesc
mov edx,len ; len of message
mov ecx,msg ; address of msg
mov ebx,1 ; 1=stdout filedesc(fd)
mov eax,4 ; 4=sys_write
int 0x80 ; call linux kernel
; exit(0);
mov ebx,0 ; 0=exit code
mov eax,1 ; 1=sys_exit
int 0x80 ; call linux kernel
The OS provide a way to load program files and execute them, those files are
no just machine codes, they follow some syntax having extra header
containg something like how much memory it needs, place for holding arguments,
environment variables, and tables of files opened by it and addresses of memory blocks allocated by it
(to be freed automatically when it's closed),
recent systems could load libraries dynamically, and more mature systems
(like Linux) support many formats of executable files on each it act differentely.
Tip
The design of BIOS is very old (for example it does not consider multitasking)
that's why Linux forbids accessing to BIOS and offers its own substitute.
Tip
GNU Compilers Collection (gcc) allow you to write in Assembly even inside
a C source file, thanks to GAS (the command name is 'as'),
but it's in AT&T style of assembly not Intel, where regiters
are prefixed with "%", immediate constants with "$"
and the variable value need no prefix but to refere to it's address
we use "$", indexing are given as a offset followed by a pointer
address between parenthesis. Source first, Destination last as in "mov"
and "add". Instruction may optionally be suffixed with b, w or l to indicate
a 8-bit, 16-bit or 32-bit operand size.
Filenames are suffixed with S (for example "hello.S")
and could be assembled (with "as") and linked (with "ld")
like this: "as hello.S -o hello.o && ld -s -o hello hello.o".
You could convert C to asseblemy (mainly for education and fun)
using: "gcc -S -fverbose-asm -O2 hello.c -o hello.S"
Highr level routines provided by libraries of high level languages like C,
instead or remembering numbers and tables you use easy to remember
functions like "open", and high level languages are hardware-independent
and platform-independet, but some high level languages works only on one
platform like VB. Programs that use libraries need to be linked with
those librares (to add the code of libraries to your program), putting
them in one executable file is called static linking which gives
large executables, the other choice is to save space shared by many larg
executable by putting shared codes in a special object files called
in windows dynamically linked libraries (.DLL files) and in UNIX
shared objects (.so files), this way of linking called dynamic linking.
Dynamically linked executables are smaller and when they start they take
some time to load their libraries.
Operating systems - the toolset.
We have discussed OS kernel, it's something you (the user) never
feels, it works between the programs (and libraries) and hardware.
The rest system software are called system toolset,
in some properietary systems the kernel and the toolset are one piece
as a black box, but usually in GNU/Linux the kernel is Linux and the toolset
is from GNU (meanwhile in GNU/HURD both from GNU).
When you write in a programming language, you use text editor.
When you modify configurations, you use text editor.
Text editors are the first and most simple system software,
the first piece of GNU was EMACS text editor (did I say simple!),
then it comes the compiler, that convert text (source code) into
executable programs, and command interpreter (shell), but
in some cases the GUI will be part of the system.
Drivers and modules.
When we say Linux device drivers we mean kernel modules (a type of them),
but device driver and modules are technically two different things
We have mintioned that DOS uses interrupt number 21 (hexadecimal).
A device driver saves the address of this interrupt, then reprogram
it to point to its own address, then exits without freeing memory blocks,
this is called "Terminate but Stay Resedent in memory" (TSR),
and buy the way, it the same method used to make viruses.
After that, when a program calls the OS, it put function number in register
AX then calls interrupt number 21 (hexadecimal) but it no longer points
to original DOS interrupt, it calls the driver interrupt, the dirver
checks the regiter AX whether it holds a number of function related
its job (if it's a file system driver then open a file is related)
elsewhere it calls the address of real OS interrupt (which it saves before).
If there are more than one driver, each calls the previous thinking that it's
the OS, if any part of this sequence is broken, the OS becomes not reachable.
Removing (unloading) them could only be done by crusting from the last one to
the first (you could not remove one in the middle)
and removing any one could only be done by itself (if it's supported)
because no other process knows the previous address.
This was about TSR drivers (eg. local keyboard layout driver and
bidirectional languages support), there are other type of drivers
specified in "config.sys" by DEVICE directive, they are loaded
at boot time, could not be unloaded (without rebooting)
they are binary files of special format (differs from version to another)
and they do not have to reprogram interrupts table or use TSR.
Linux kernel has a unique way of extending the kernel
called modules, they are part of system code (in object file format),
recall that projects with multipled source files have each one compiled
into object file, then joined together in linking process,
but kernel modules are not linked, they are left to be loaded
later by adding them to the same virtual page of memory.
In other words, one could compile an entire program and join it, at any time,
to the kernel as module, Linux is the only monolithic kernel (see below) that could
change part of the kernel without rebooting.
for example, a web server called Tux is composed Linux kernel module
and user land parts, this web server broke speed records!
Any module should contain two functions
init_module and cleanup_module
(both take no arguments and the return type of them
are int and void, respectively)
and should have a global variable 'char *kernel_version="X.Y.Z"',
it may contain other functions that are registered and added to the list
of function provided by the kernel, it could register some virtual
files in /proc file system (refere to Kernel Hacker Guide)
or handle file operations on a device on /dev
(not by registering the device file name but its major and minor
numbers, the device file could be created using mknod),
visit http://coffee.sf.net
to learn how to make a coffee-machine controled by a computer running Linux
with something like "echo 'cappuccino' > /dev/coffee ",
it's funny to read "Remember to give the right permission to
/dev/coffee, depending on whether you want only root making coffee or not",
you could even control any device in your home visit http://edc.sf.net.
The next example is taken from Module-HOWTO by Bryan Henderson
it's a module that prints a message when it's loaded and unloaded.
/* hello.c - dummy kernel module from Module-HOWTO
* Compile this with
* gcc -c hello.c -Wall
* put it in lib/modules and do modprobe hello
*/
#define __KERNEL__ /* We're part of the kernel */
#define MODULE /* Not a permanent part, though. */
#include <linux/modversions.h>
#include <linux/module.h>
#define _LOOSE_KERNEL_NAMES
#include <linux/tty.h> /* console_print() interface */
int init_module() {
console_print("Hello, world - this is the kernel speaking\n");
/* or simply use printk("Hello") */
return 0; /* 0 success ; else failed */
}
void cleanup_module() {
console_print("Short is the life of an LKM\n");
}
notice that it does not call any function from C library (or any other library)
because it depends on kernel and we are kernel! you could call your own functions
or kernel functions like printk.
Please refere too Linux 2.4.x Initialization for IA-32 HOWTO
by Randy Dunlap <rddunlap[AT]ieee[DOT]org>
to trace calls in Linux kernel source files.
Microkernel and Monolithic kernels.
Most systems was running Monolithic kernels (including Linux).
Now, many people believe that future systems will be based on Microkernels.
The idea of a Microkernel is to reduce the kernel to the
bare minimum functions shared by all operating systems at their core
(manage access to hardware, memory, and CPU time between processes)
where the rest functionality (network protocols, filesystems, permissions,
encryption, ...,etc.) is done by a set of special programs called
servers (or daemons) running inter-kernel tasks in parallel as
processes at user land.
All this to get a modularized system which is (theoretically) easy to develop
and maintain. Inter-kernel Communications (in between servers or between them
and the microkernel) using Message Passing makes Microkernel-based systems
slower than Monolithic kernel-based system because of design
overhead, where we need 2 task switching timeslices (in and out) for each server
(refere to Kernel Analysis HOWTO by Roberto Arcomano).
More over many of them (like Mac OS X, OSF/1, NEXTSTEP, Lites,
and MkLinux) do not make use of microkernel design features (eg. modularization)
because they run only a single server on the top of the microkernel.
Although Linux kernel is not a microkernel, it's a modularized system
because of kernel modules! more over it has a few parts of kernel
code running in parallel (similar to Microkernel servers) called kernel
threads (like kswapd), as few as (about) 10 threads, those only where it's
effeciant to use kernel threads to reduce the overhead,
they are selected carefully (that need I/O operations) so that
they won't take the whole timeslice of task switching
(the algorithm of multitasking is to switch to the next ready process when
either the timeslice ends or in case of sleeping; eg. waiting for I/O operation).
As we have noticed that genius design of Linux compine the advantages
of best known solutions and tries to overcome their disadvantages.
The most promising Microkernel-based systems are
Plan9 (from Bell-Labs) and GNU/Hurd (part of GNU), they are expected to be
the future competent to Linux.
They extend many Unix concepts like union directories or UnionFS.
Plan9 is not aimed to be a Unix system (but something very similar)
while Hurd is (through POSIX compitability).
Plan9 is named after a movie called "Plan 9 from Outer Space" while
HURD is mutually recursive acronym that stand
for "HIRD Unix-Replacing Daemons", where HIRD stands for "HURD of Interfaces Representing Depth",
Hurd is currently running over GNU Mach Microkernel or GNU L4 Microkernel
but it's aimed to be Microkernel independent.
Because they are running many servers on the top of
the microkernel, which makes them modularized systems, this
won't make them better than Linux (it's modularized system),
but their servers could run unpriviliged on user land which make it
safer to insert buggy codes, a user could run the Hurd within
itself (a sub-Hurd), while in usual Linux, modules could only be
inserted by the root user (priviliged user), this mean adding
support for some new (untested) filesystem put the whole system on risk,
but using a package called "user-mode-linux" this feature is achived
(you could safely test buggy codes and run even a complete Linux kernel
as a user process within different Linux kernel).
Because of Message passing between servers running over the microkernel;
Hurd and Plan9 (or operating systems based on it like Inferno)
has features that could be useful for distributed computing,
but there are some Linux kernel patches (like OpenMosix and BProc)
that make Linux the best known system for that task.
In short current state Linux could
provide someways (maybe non trivial) for those extended features
and outperformence those systems, see Eric S. Raymond's article
at www.faqs.org/docs/artu/plan9.html
about Plan9.
Memory management.
There is something very strange about the design of 80x86
(including Pentium) based PCs, it's a long story back to the time
it was designed, where they assumed that the larger size of it's
memory is 640 KB, the size of registers was 16-bits (before 32-bit 80386 CPUs),
the largest number it could hold is 0xFFFF (hexadecimal),
this mean 64 KB only, but we need at least 20-bits (the addressing bus width in 8086),
we need 4-bits more or one hexadecimal digit, they call it physical page,
and we need extra register, the first register is called offset
register and the second segment register, the memory address is
represented in the form 'segment:offset' but the brain damage
was putting physical page number in the most significant hexadecimal
digit of the segment register instead of least (leaving no digits for future,
where memory should not be limited to 1 MB), for example the physical
address 0x51234 could be represented as (among other) 0x5000:0x1234,
0x5100:0x0234, 0x5120:0x0034 or 0x5124:0x0004
they all point to the same address, to get the physical address add
the offset to 16*segment (the segment shifted to left one hexadecimal digit),
this give us 20-bits, ie. only one megabyte although we have used
32-bits (two 16-bit registers) capable of representing 4GB.
The reason that justify this (not using the least hexadecimal digit in
segment register for physical page address)
is that the assumption that each loaded program (including TSRs) should has
its own unique segment, because 16-bit machine language instructions to jump (like goto)
to some address just take the 16-bit offset which should be the same no matter
where the program is loaded. They divided memory into segments, in each one
a program is loaded at offset 0x0000, if we use the least hexadecimal
digit in segment register for physical page address this will give us
only 16 valid segments (in 20-bits addressing bus CPUs), which
mean we could only have 16 loaded programs, they toke the other choice
to have more loaded prorams. Another problem raised from dividing memory
to segments is that the loading a program needs continuous (consecutive)
free memory memory blocks of its size, if there is not continuous segments
then the program size is limit to segment size, this is called Segmentation problem.
In DOS it's not enough to have 128KB free memory to be able to load
a 128KB program (or to allocate a 128KB block), it has also to be continuous.
Later 80286 CPU appeared with 24-bit for addressing bus, then
full 32-bit CPUs camed starting with 80386, and they have more memory
than the assumed limit, for that they work into two modes;
real mode: compatible to 8086 where it won't see beyond first megabytes
which is called Conventional memory, the rest is accessed by
copying far (also called high) parts to a small block in conventional memory
aand read it there.
In DOS copying is done using EMS or XMS,
that last one is provided by HIMEM.SYS which MS Windows do not work
without it being loaded.
The other mode is called protected mode, where memory address could be
the physical address (flat addressing) just a 32-bit number,
or it could be virtual address to solve Segmentation problem
(the condition of continuity of segments)
just by dividing segments to pages of fixed size, and use it as
unit of allocation having continous virtual address while
its physical address not not be so, it could physically
be separated by data or codes of other programs
but they are not visible to it. The segment in protected
mode is called in some references 'program page'
so that it won't confuse with segment in real mode.
And so, the address in the register is the offset from the begining
of the virtual segment, 32-bit registers are used (that start with E, like EAX).
Segment register are used differentely in protected mode,
they are divided into parts like 'selector' referes to the ID number
of virtual segment from which offset is mesured, and 'limit'
to the maximum allowed offset.
Another problem raised called Pagination Problem,
it is wasting memory because of having page size fixed,
the waste is at most page size times number of programs,
minimizing the size of the page (to reduce wasting)
will raise the overhead of maintaining more entries at
table mapping virtual addresses to physical (size of table, lookup time).
DPMI - DOS Protected Mode Interface (it's not part of DOS, it comes with independentely with Windows, for example)
provide routines on interrupt number 0x2f (hexadecimal)
to start protected mode and interrupt number 0x31 (hexadecimal)
to allocare memory and to emulate real mode interrupt routines
like 0x21 (hexadecimal) which expects
(real) segment:offset addressing.
MS Windows (95 or 98) works into two stages, when it's booting
(you see its logo), press ESC, to see DOS running in real mode
loading device drivers specified by "config.sys" like
HIMEM.SYS then "autoexec.bat" execute a sequence of 16-bit system programs
maybe having viruses (if it was not a boot sector virus),
nothing visible except first megabyte of memory, yet.
Then the protected mode starts, the multitasking system, the GUI,...etc.
now DOS programs could not be executed because they don't use
interrupt 0x31 (hexadecimal), and because conventional memory
could be full, the program loaded out of first MB , the DOS program
could not see itself through segment/offset addressing.
Windows emulates DOS (inside the GUI, but they work natively
before loading GUI or when selecting "restart in DOS mode")
where the program seems loaded in first megabyte of memory.
This approach is quite dangerous because programs (specially viruses)
running before starting protected mode could access (through EMS, XMS or similar methods)
to any part of memory that refere to any program
without supervision from the OS.
Linux was designed with security in mind, of course they did not follow
such approuch. Once kernel is loaded the first thing todo is to jump
to protected mode. Linux differs from all other systems by having
only 4 segments (kernel code, kernel data, user land applications code and their data)
unlike other systems having segment for every program.
Having the kernel segment separated from user land programs so it's far away
from their hands. Also, having code segment separated from data segment
allow more than one instence of a program or a library to be
loaded and allocated once for code (and many for data) to save memory,
because the code is the same and just data differes.
Solving both segmentation and pagentation problems, the first by
dividing segments to pages, and the second by having only 4KB per page
without paying the overhead price on speed because it has
what we call Hierarchical Paging using fast binary search tree algothirm
(the search loop is to classify the set into two subsets, droping
half of it and search the other the same way, in each cycle).
All user land application could only access to memory through
virtual addressing, only kernel could access to absolute physical addresses,
so programs could not access memory blocks of other programs,
the only way to see what in other programs memory is
throgh swap file or disk.
Tip
Swapping is the process of generating more virtual memory,
when the system needs more memory (than what physically exists)
it search for a not in-use block (hold by a sleeping process)
and send it (or some pages of it) to swap device
freeing physical memory. When a swaped-off blocks are needed
they are sent back somewhere in physical memory taking the place
of another not in-use block, here comes the name.
Programs are not notified of this change of physical address
because they have virtual addesses managed by the kernel.
If a program don't want it's critical data to be swaped to world visible
swap device, it asks for a lock on memory (lock from being swaped to disk),
but this needs root privileges because locking memory consume resources
(having many locked memory then no memory left to be swaped into free memory)
Tip
That's why you get unsafe memory warning when generating SSH private security
keys when running as regular user!
If you are wondering, DOS manages memory by keeping entries cooresponding
to memory block of variable size, starting with one byte "magic" M or Z (if it's the last block)
followed by block size and to which program does it refere
(a pointer to a structure called PSP - Program Segment Prefix),
when the system wants to manage those blocks (free/allocate)
it has pass those entries sequentially.
|
Best viewed with free web browsers
You may get more high quality software
from here for free

Generously Hosted by www.JadMadi.net
|