KernelAnalysis-HOWTO: Static variables

Next Previous Contents

## 14. Static variables

## 14.1 Overview

Linux is written in ''C'' language, and as every application has:

1. Local variables
2. Module variables (inside the source file and relative only to that module)
3. Global/Static variables present in only 1 copy (the same for all modules)

When a Static variable is modified by a module, all other modules will see the new value.

Static variables under Linux are very important, cause they are the only kind to add new support to kernel: they typically are pointers to the head of a list of registered elements, which can be:

- added
- deleted
- maybe modified

                               _______      _______      _______
    Global variable  -------> |Item(1)| -> |Item(2)| -> |Item(3)|  ..
                              |_______|    |_______|    |_______|

## 14.2 Main variables

### Current

                               ________________
    Current ----------------> | Actual process |
                              |________________|

Current points to ''task_struct'' structure, which contains all data about a process like:

- pid, name, state, counter, policy of scheduling
- pointers to many data structures like: files, vfs, other processes, signals...

Current is not a real variable, it is

    static inline struct task_struct * get_current(void) {
       struct task_struct *current;
       __asm__("andl %%esp,%0; ":"=r" (current) : "0" (~8191UL));
       return current;
    }
    #define current get_current()

Above lines just takes value of ''esp'' register (stack pointer) and get it available like a variable, from which we can point to our task_struct structure.

From ''current'' element we can access directly to any other process (ready, stopped or in any other state) kernel data structure, for example changing STATE (like a I/O driver does), PID, presence in ready list or blocked list, etc.

### Registered filesystems

                           ______      _______      ______
    file_systems  ------> | ext2 | -> | msdos | -> | ntfs |
     [fs/super.c]         |______|    |_______|    |______|

When you use command like ''modprobe some_fs'' you will add a new entry to file systems list, while removing it (by using ''rmmod'') will delete it.

### Mounted filesystems

                            ______      _______      ______
    mount_hash_table  ---->|   /  | -> | /usr  | -> | /var |
    [fs/namespace.c]       |______|    |_______|    |______|

When you use ''mount'' command to add a fs, the new entry will be inserted in the list, while an ''umount'' command will delete the entry.

### Registered Network Packet Type

                            ______      _______      ______
         ptype_all  ------>|  ip  | -> |  x25  | -> | ipv6 |
    [net/core/dev.c]       |______|    |_______|    |______|

For example, if you add support for IPv6 (loading relative module) a new entry will be added in the list.

### Registered Network Internet Protocol

                              ______      _______      _______
    inet_protocol_base ----->| icmp | -> |  tcp  | -> |  udp  |
    [net/ipv4/protocol.c]    |______|    |_______|    |_______|

Also others packet type have many internal protocols in each list (like IPv6).

                              ______      _______      _______
    inet6_protos ----------->|icmpv6| -> | tcpv6 | -> | udpv6 |
    [net/ipv6/protocol.c]    |______|    |_______|    |_______|

### Registered Network Device

                              ______      _______      _______
    dev_base --------------->|  lo  | -> |  eth0 | -> |  ppp0 |
    [drivers/core/Space.c]   |______|    |_______|    |_______|

### Registered Char Device

                              ______      _______      ________
    chrdevs ---------------->|  lp  | -> | keyb  | -> | serial |
    [fs/devices.c]           |______|    |_______|    |________|

''chrdevs'' is not a pointer to a real list, but it is a standard vector.

### Registered Block Device

                              ______      ______      ________
    bdev_hashtable --------->|  fd  | -> |  hd  | -> |  scsi  |
    [fs/block_dev.c]         |______|    |______|    |________|

''bdev_hashtable'' is an hash vector.

Next Previous Contents