Showing posts with label Linux Kernel. Show all posts
Showing posts with label Linux Kernel. Show all posts

Monday, November 26, 2012

How to manually find all of kernel modules on Linux

1. cat /boot/System.map | grep modules
Get the address of modules symbol from system.map file. modules symbol points to a list_head structure, which is the head of double- and circular- linked list of all the modules.

2. take a look at linux-src/include/linux/module.h
struct module {}
contains all the useful information about module structure

3. Here is code in kernel module to print out all the module names.

static int hello_init(void)
{

    printk("start list all the modules\n");

// Get this magic address from system.map
    struct list_head * module_h = (struct list_head*) 0xc06e1880;
    int count = 100;
    struct list_head *list_h = module_h->next;
    struct module *m;
    while(count--)
    {
if(list_h == module_h)
   break;
m = list_entry(list_h, struct module, list);
printk("name: %s\n", m->name);
list_h = list_h->next;
    }

    printk("count is %d\n", count);
    printk("\nhello, world\n");
    return 0;
}

Monday, September 10, 2012

System.map layout


Monday, July 30, 2012

memory management in kernel

http://duartes.org/gustavo/blog/post/how-the-kernel-manages-your-memory

Saturday, July 21, 2012

Linux Kernel Stack Size

From testing,

Linux kernel 2.6.24 set process kernel stack size as 4KB
while kernel 2.6.32 set size as 8KB.

Usage: calculating current from ESP, mask 12 or 13 bits....

http://lxr.linux.no/linux+v2.6.32/arch/x86/include/asm/page_32_types.h#L23

Debian kernel Downaloads

Debian 3.0 Woody
Kernle 2.4
http://www.debian.org/releases/woody/

Debian 3.1 Sage
Kernel: 2.4
http://www.debian.org/releases/sarge/debian-installer/

Debian 4.0 Etch
Kernle 2.6.18
http://www.debian.org/releases/etch/

Debian 5.0 Lenny
Kernel
http://www.debian.org/releases/lenny/

Debian 6.0 squeeze
Kernel: 2.6.32
http://www.debian.org/releases/squeeze/

It looks like Debian does not change too much kernel from mainline kernel.
While CentOS changes a lot.


Wednesday, July 18, 2012

Install Custom Kernel on Ubuntu

Please read: https://help.ubuntu.com/community/Kernel/Compile/

Basically, it only supports kernel from Ubuntu source. It doesn't support mainline kernel from kernel.org.
The source code from mainline kernel could successfully compile, but it cannot boot.
The experiment I had is: automatic restart after loading mainline kernel.

Experiment enviroment:
Ubuntu 11.10 + mainline kernnel 2.6.19


Monday, July 16, 2012

Install Custom Kernels on CentOS

Read: http://wiki.centos.org/HowTos/Custom_Kernel


Basic, it suports kernel from CentOS source, not from mainline kernel from kernel.org.

But I have been successfully compiled kernel version 2.6.19 based on CentOS 4.5, and version 2.6.24 based on CentOS 5.5.

Steps:
$make menuconfig // need to enable General config-->enable deprecated sysfs 
$make -j4
$make modules_install
$make install



Ref:
https://www.centos.org/modules/newbb/viewtopic.php?topic_id=15198&forum=37&post_id=88760#threadbottom

Wednesday, September 21, 2011

Understanding the kernel code, and CR3 register

Recently, I was working on the kernel code and CR3 register. This article is going to record what I have learn.
First Question: How can we get the kernel code of Operating System?
Get Virtual Address of kernel code:
$ cat /boot/System.map-2.6.32-33-generic | grep text
c0100000 T_text
The first one is start virtual address of kernel code

Get Physical Address of kernel code:
$ cat /proc/iomem
00100000-00590de6 : Kernel Code

More info: please see
http://fengweizhang.blogspot.com/2010/12/boot-cat-system.html
http://jianggmulab.blogspot.com/2009/09/blog-post.html

Second Question: How to translate virtual address to physical address? 
Control Register 3 (CR3) stores the base address of Page Directory Table. More info please see page 47 in ULK book. 

Third Question: Which CR3 I should use to translate VA to PA of kernel code?
Any CR3. For each process, it has its own address spaces and CR3. No matter which processes the operating system is running, the virtual address and physical address of kernel code will never change. Although the CR3 is different depends on the running process, same VA will map to same PA by CR3.

Each process has its own address space. From the virtual address point of view, it has some address space at 0-3G region, and it also has some address space at 3-4G region (this is for kernel mode). All Processes get same mappings for the kernel address space (3-4G). Each process also has its own page directory table and page tables. CR3 is the base address of page directory table. The address space at 3-4G region must have its own translation entries in page directory table and page tables. These entries are always same in different page directory table and page tables. 

Process 1 has virtual address space from 0xc0100000-0xc0590de6 (kernel code), translate to physical address is 0x00100000-0x00590de6
Process 2 has virtual address space from 0xc0100000-0xc0590de6 (kernel code), translate to physical address is 0x00100000-0x00590de6

Thus, process 1 and process 2 has same kernel memory mappings. (CR3 is different)


Thursday, April 14, 2011

Linux Kernel Cross Reference Site

1. http://lxr.linux.no/linux/
2. http://www.linux-m32r.org/lxr/http/source/?a=i386

Compile new kernel on old kernel

http://jianggmulab.blogspot.com/2010/06/install-new-kernel-on-old-distros-such.html

Thursday, April 7, 2011

Compile Linux Kernel

$ wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.24.tar.gz
$ tar -xzf linux-2.6.24.tar.gz
$ cd linux-2.6.24
$ make menuconfig
$ make -j4
$ make modules_install /* Remember to do this */
$ sudo make install

FAQ
1. remember to install ncurses-devel for make menuconfig
2. install qt-devel for make xconfig

useful link about kernel compile:
http://www.cs.columbia.edu/~junfeng/10sp-w4118/hw/hw2.html

Some problems I had:
1. Compile kernel on Ubuntu with gcc version 4.6
Because gcc-4.6 doesn't support soemthing, it need to downgrade to gcc (e.g version 4.4)
How to downgrade gcc, please see here

Sunday, January 23, 2011

Get Linux kernel Souce Code

1. use wget (you could get any version you want):
$ wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.18.3.tar.bz2
or
$ wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.18.tar.gz

2. use git
$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git
$ cd linux-2.6
$ git reset --hard v2.6.17 /* Change current branch to 2.6.17 */
more details: http://www.kernel.org/pub/software/scm/git/docs/user-manual.html

Wednesday, December 29, 2010

How to write kernel model

1. install kernel-devel on CentOS 5.5 (kernel-dev on Ubuntu)
$ yum update // upgrade your kernel to latest kernel version
$ yum install kernel-devel // install kernel development library at /lib/module/
This is the source code header for current Linux kernel. You could check if they match each other.
$ uname -r
$ ls /lib/moudle/

2. write hello.c and Makefile 
hello.c:

#include <linux/module.h>
#include <linux/init.h>

static int hello_init(void)
{
    printk("\nhello, world\n");
    return 0;
}
static void hello_exit(void)
{
    printk("Goodbye, crule world\n");
}
module_init(hello_init);
module_exit(hello_exit);

Makefile:
# Comment/uncomment the following line to disable/enable debugging
#DEBUG = y
# Add your debugging flag (or not) to CFLAGS
ifeq ($(DEBUG),y)
  DEBFLAGS = -O -g # "-O" is needed to expand inlines
else
  DEBFLAGS = -O2
endif
CFLAGS += $(DEBFLAGS) -I$(LDDINCDIR)
ifneq ($(KERNELRELEASE),)
# call from kernel build system, change to you filename
obj-m    := hello.o
else
# remember to set right kernel directory
#KERNELDIR ?= /lib/modules/$(shell uname -r)/build
KERNELDIR ?= /lib/modules/2.6.18-194.26.1.el5/build
PWD       := $(shell pwd)
default:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) LDDINCDIR=$(PWD)/../include modules
endif
clean:
    rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
depend .depend dep:
    $(CC) $(CFLAGS) -M *.c > .depend
ifeq (.depend,$(wildcard .depend))
include .depend
endif

3.  install module, test, remove module
$tail -f /var/log/message // you will see message "hello word!" when install module
$insmod hello.ko // install module
$lsmod | grep hello // you will see module has been installed
$rmmod hello // remove the module

4. Addition info
LDD book: http://lwn.net/Kernel/LDD3/
Example source code: http://examples.oreilly.com/9780596005900/
BIOSren pdf tutorial: http://linux.chinaunix.net/bbs/viewthread.php?tid=1042600&extra=page%3D1%26amp%3Bfilter%3Ddigest

In example source code:  examples/sample/ directory is the sample example.