ARTICLE

Procfs from the inside
Contributed by Fernando Apesteguia in Misc on 2006-03-20 14:59:21
Page 2 of 5

OS basics: user-land vs. Kernel-land

Modern operating systems use features from modern processors. These processors allow to execute code in two or more levels of privilege. Linux kernel uses two of the available levels to perform user mode and kernel mode (i.e., user-land and kernel-land).

The main goal is to provide an independent environment for kernel execution. In this way, if a user process crashes it may affect to other user space process, but never to kernel space.

The complexity added is to copy data from user-land to kernel-land and reverse. This is what procfs does in a way that is well known by linux users: using files.

Looking at cpuinfo

cpuinfo is one of the files that can be found in /proc. It provides information about your cpu. As we can see, a lot of useful information is displayed. This is what my cpuinfo says about my cpu:

processor : 0
vendor_id : AuthenticAMD
cpu family : 15
model : 4
model name : Mobile AMD Athlon(tm) 64 Processor 2800+
stepping : 10
cpu MHz : 801.854
cache size : 1024 KB
fpu : yes
fpu_exception : yes
cpuid level : 1
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx
fxsr sse sse2 syscall nx mmxext lm 3dnowext 3dnow
bogomips : 1576.96
TLB size : 1088 4K pages
clflush size : 64
cache_alignment : 64
address sizes : 40 bits physical, 48 bits virtual
power management: ts fid vid ttp

Where does this information come from? If we navigate by the source code of the linux kernel, we find a file in /usr/include/asm/asmx86_64 (your path can be different, of course). In this directory, we can see the processor.h file. Let's look inside and search for a structure named cpuinfo_x86.

struct cpuinfo_x86 {
__u8 x86; /* CPU family */
__u8 x86_vendor; /* CPU vendor */ __u8 x86_model; __u8 x86_mask; /* We know that wp_works_ok = 1, hlt_works_ok = 1, hard_math = 1, etc... */ char wp_works_ok; /* It doesn't on 386's */ char hlt_works_ok; /* Problems on some 486Dx4's and old 386's */ char hard_math; char rfu; int cpuid_level; /* Maximum supported CPUID level, 1= no CPUID */ __u32 x86_capability[NCAPINTS]; char x86_vendor_id[16]; char x86_model_id[64]; int x86_cache_size; /* in KB valid for CPUS which support this call */ int fdiv_bug; int f00f_bug; int coma_bug; unsigned long loops_per_jiffy; } ____cacheline_aligned;

This is one of the structures where the linux kernel keeps information about your system characteristics.

Is difficult to achieve this information in user-land mode, so the kernel gathers it in kernel-land and offers it to user-land via procfs. This is a good approach and it avoids the extensive use of copy_from_user and copy_to_user functions.



Article Index
Procfs from the inside
OS basics: user-land vs. Kernel-land
Implementing procfs
Some examples on using procfs
Conclusions
 
Discussion(s)
Good information
Written by beparas on 2008-07-04 05:39:48
This is article contains good information.
Thank You
Discuss! Reply!