hello_world_logo.gif (12859 bytes) maple_leaf.gif (1004 bytes)

This could be your company's banner advertisement

April 1999

Issue 2, Volume 1

About the Author

wpe5.jpg (2707 bytes)

Dean Curtis Parsons
is a computer science student at the Memorial University of Newfoundland.

Memorial University of Newfoundland

Hello World!
Editorial
News
Comics
Events
 
Sponsors
 
Hello World! Teams:
  Editors
  Sponsorship
  Site Design
 
Member Universities
 
Past Issues
 
Contact Us
   
   

Site hosting provided by
smarttbutton.gif (2795 bytes)

   

The Core of Linux: Hacking the Kernel

-- Dean C. Parsons

Intro to the Linux Kernel

The Linux Kernel is the center of what makes the Linux and Unix operating systems. The kernel is the foreman or mediator over all your programs and hardware components.

As the core of the operating system, the kernel has access to certain services that interact with the systems hardware which no other program has direct access to. If a program wants to carry out a special task, then it must go though the kernel first.

When a program wants to run, write data to a disk, write to certain parts of memory, it asks the kernel for permission first. The kernel considers the program's request and proceeds to do a number of tasks for that program. The kernel is very strict and will not let programs away with illegal operations. If a program goes against what the kernel says, it must be punished for it's actions.

If a program that got the "ok" from the kernel and then decides to write some data to memory outside it's designated block(s), the kernel will kill that program and do a core dump. A "core dump" is an operation the kernel carries out when a program tries to perform a task it is not allowed to do. The kernel dumps the address space in memory the program was using to the hard drive before the kernel kills that program.

The paradigm of a strict kernel is not new, the idea has been around for a while. It's a great idea, and i think has been implemented very well on a Linux os. The majority of the kernel was written in C with partial implementations written in assembly language which makes the it flexible and very fast.

A Conversation with the Kernel

  • (program app) Hello Mr. Kernel, I was wondering if I could have some memory to run and play in?
  • (kernel) Hmm... Ok, but if you try to run outside the memory space allocate for you then I am going to stop you!
  • (program app) Ok then, it's a deal. . . . .
  • (kernel) Hey! I never gave you permission to write data in that memory segment, you will pay!
  • (program app) Oh no??.... I didn't mean it.. I.. I.. AHHHH.... ***Dead*** ( core dump ).
  • (kernel) Ask me next time you want to do something like that!
  • (kernel) When will those programs ever listen?!

Usually the operation of killing a program that was "bad" does not bring down or crash the whole Linux operating system, it just kills the program or process that has stepped out side it's "play ground" the kernel initially allocated for it. After a core dump the kernel just goes about it's business of answering requests from other processes on the system.

(when a core dump occurs it is possible in some cases to start up the program that caused the core dump to where it left off just before the dump.)

Hacking / Configuring the Kernel

The "make" command

The kernel is usually compiled like other C programs using the "make" command. Make is a smart program that does most of the compilation processing using configuration information you provide via ( xconfig, config, and menuconfig -- see below for more details.). So you can actually compile the program in which your hacking the kernel with.

There are a few ways in which to configure the kernel. Depending on what you want to do with your kernel, you have the options of:

  1. Make config
    A text based interactive program that helps you config the kernel by answering Y-yes N-no questions.
    config.jpg (61605 bytes)
  2. Make menuconfig
    Interactive menu configuration program, selections in the kernel are made using toggle switches.
    menuconf.jpg (49182 bytes)
  3. Make xconfig
    An X Windows program. A graphical menu is displayed in X, and all possible options or modules are enabled/disabled with radio buttons and other common GUI interactions. ( very user friendly )
    • There is help inside the xconfig program, just use the "help" button. if your stuck.

    xconfig.jpg (46899 bytes)

  • Be careful which options you disable when configuring the kernel.
  • It's recommended you read more on hacking and configuring the kernel before you start the actual hack, the kernel is NOT something you was to play carelessly with!

Most default installs of Linux have relatively big kernels. These kernels provide you with great flexibility in regards to a wide variety of hardware driver support. They often include all of the most commonly used hardware component device drivers and most of the kernel options. Generic kernels like this are created to deal with what ever type hardware you have installed, but they are inefficient and big. This means longer boot time, and less memory for your applications to run in. Hacking and reconfiguring the kernel is used for cleaning out, and getting rid of extra drivers and options not needed for your system. Once you have hacked out the unused materials your kernel will be faster, cleaner and more efficient to run.

Compiling the Kernel after the Hack

It is recommended to start building from a clean slate. You should first do a "make mrproper" in the directory of your kernel ( usually "/usr/src/linux" on most Linux systems. The "make mrproper" command removes any configuration files along with the remains of any previous kernel builds or scraps that may be laying about in the source tree. ( another way to start with a "clean" build is to do a "make clean" )

Then a "make dep" should follow the "make clean/mrproper". - This will figure out what drivers in your kernel is dependent on other drivers. ( see links below for Kernel Hackers Guide ).

Then do:

  • make in "/usr/src/linux/" will give a kernel called: "vmlinux"
  • make bzImage in "/usr/src/linux" will give you a commpressed kernel called "bzImage"
  • What ever name your kernel is make sure it's correct in the lilo.conf file. ( see LILO below for details )

    xdone.jpg (16052 bytes)
  • Before rebooting after the kernel compile make sure there is an entry for the new kernel in your "/etc/lilo.conf" file, if no entry is listed there, then the new kernel will not be directly accessed by the boot loader "LILO". If you have a new kernel and want to use it you should tell LILO about it.

Updating LILO

LILO:
Lilo is a boot loader that most linux users use for booting there Linux boxes

Example of a "lilo.conf" file ( usually located in "/etc/" )

# This line is a comment line
#LILO global section
    boot = /dev/hda2
    timeout = 500
    prompt
    default = linuxbox #"linuxbox" is default kernel
    vga = normal
    read-only
#End of globol section ends

# bootable kernel "vmlinuz-2.0.36-1" in directory "/boot/"
# kernel number one
    image = /boot/vmlinuz-2.0.36-1
    label = linuxbox
    vga = normal
    root = /dev/hda2
#end of kernel one section

# bootable kernel "zImage" in directory "/usr/src/linux/"
# THE NEW HACKED KERNEL!
    image = /usr/scr/linux/zImage
    label = hackedkernel
    vga = normal
    root = /dev/hda2
# End of HACKED KERNEL section

Note:
After compiling your new kernel make sure the "HACKED KERNEL" section is added to your LILO configuration file ( usually "/etc/lilo.conf" )

In Closing

Hacking the kernel can have it's advantages. As stated earlier the Kernel is the mediator over the whole Linux OS. It is the master of the OS who gives permissions to programs and can take those permissions away. You are able to modify the core of Linux, make it faster cleaner and in some case recompiling the kernel fixes some things. For example, if your kernel is configured for some hardware you don't have, the kernel may want to prob for a hardware component to make sure it's there or not, that is if the driver for that device is compiled into the kernel. This is some cases may cause some problems.

The kernel handles memory management, system processes and controls and keeps tabs on system resource making sure things are working correctly.

Read up as mush as you can on compiling kernels.

Hope this helps you along the way, good luck!

Great Linux Links

Linux online
General linux info

Linux Apps
Great place to find some cool applications.

The Linux Kernel Archives
The Linux Kernel Archives, source code and more.

Linuxberg Linux Distribution Archive
source, libs, docs...

RedHat
The offical RedHat site

Kernel Hackers Guide
A kernel hacker information site, questions and answers

LinuxPower Dot Org
"Helping users harness the Power of Linux"

Linux Application and Utilities.
Apps and Utilities.

Linux Kernel
Lots of information about the kernel, how is runs, memory management, demand paging, process etc...

Lilo HowTo
Questions and answers regarding the LILO boot loader.

linuxberg.com
Great place to find apps, games, untilities and more.

Columns
  Development
  Security
  Network Security
 
Feature Articles
  The 3D Chipset Wars

Active Server Pages

Dynamic HTML

Help Students Across Canada Go Higher!

The Core of Linux: Hacking the Kernel

An Introduction to PHP

Java's Magic Beans

Mac OS X Server: Unix for the Mac

Mentoring to a Multitude

MPEG-4: A Bird's Eye View

ATAPI Music CD Programming Interface in Linux

Snow Crash

Unified Modeling Language

Rendering Bézier Forms via Forward Differencing

April 1999

Issue 2, Volume 1

This could be your company's banner advertisement

hw_publegal.gif (2694 bytes)