Il kernel di Linux Riferimenti:
http://www.ltn.lv/~guntis/unix/kernel_modules.ppt (Dzintars Lepešs, University of Latvia)
http://freeelectrons.com
Il kernel di Linux Riferimenti:
http://www.ltn.lv/~guntis/unix/kernel_modules.ppt (Dzintars Lepešs, University of Latvia)
http://freeelectrons.com
Struttura dei sorgenti
arch Codice dipendente dall'architettura
documentation Documentazione del kernel;
drivers Tutti i device drivers
fs Filesystems
include File header del kernel
kernel Scheduler, …
lib Librerie
…
Makefile Makefile principale
Compilazione
Configurazione kernel:
make xconfig
oppure make menuconfig
oppure make oldconfig
Generano il file .config con le impostazioni scelte.
Compilazione:
make [opzioni]
Compilazione
make bzImage
crea il kernel compilato
make modules
compila i moduli
make modules_install
installa i moduli
make … -j 4
compilazione parallela
make mrproper
cancellazione file intermedi
Implementazione
Vengono memorizzati come file oggetto in formato ELF;
Viene memorizzato l’indirizzo di tutti i simboli esportati (/proc/syms <2.6 /proc/kallsyms - 2.6);
Viene momorizzato l’uso e le dipendenze dei moduli usati (/proc/modules), caricati con insmod o modprobe.
Per ogni modulo vengono mantenuti:
Una struttura dati con le informazioni su di esso;
Una stringa identificativa;
L’implementazione.
Module Object
Linking e Unlinking di moduli
Programs for linking and unlinking
insmod
Reads from the name of the module to be linked
Locates the file containing the module's object code
Computes the size of the memory area needed to store the module code, its name, and the module object
Invokes the create_module( ) system call
Invokes the query_module( ) system call
Using the kernel symbol table, the module symbol tables, and the address returned by the create_module( ) system call, relocates the object code included in the module's file.
Allocates a memory area in the User Mode address space and loads with a copy of the module object
Invokes the init_module( ) system call, passing to it the address of the User Mode memory area
Releases the User Mode memory area and terminates
Programs for linking and unlinking
lsmod
reads /proc/modules
rmmod
From reads the name of the module to be unlinked.
Invokes the query_module( )
Invokes the delete_module( ) system call, with the QM_REFS subcommand several times, to retrieve dependency information on the linked modules.
modprobe
takes care of possible complications due to module dependencies, uses depmod program and /etc/modules.conf file
Device drivers
Driver a caratteri
Accesso tramite flusso sequenziale di caratteri singoli
Individuabili per il loro tipo di file c (ls -l):
crwrw1 root uucp 4, 64 Feb 23 2004 /dev/ttyS0
crww1 jdoe tty 136, 1 Sep 13 06:51 /dev/pts/1
crw1 root root 13, 32 Feb 23 2004 /dev/input/mouse0
crwrwrw1 root root 1, 3 Feb 23 2004 /dev/null
Esempio: tastiera, mouse, porta parallela, IrDA, porta Bluetooth, console, terminale...
Driver a blocchi
Accesso casuale a blocchi di dati di dimensioni fisse.
Individuabili per il loro tipo di file b (ls l):
brwrw1 root disk 3, 1 Feb 23 2004 /dev/hda1
Esempi: hard disk e floppy disk, ram disk, dispositivi loop...
Numeri di device
Ogni device ha due numeri associati:
major number
Associato ad ogni driver in maniera unica
minor number
Associato ad ogni device in maniera unica
Creazione dei file di device
I file di device non vengono creati al momento di caricare il driver, devono essere creati esplicitamente:
mknod /dev/ [c|b]
Esempi:
mknod /dev/ttyS0 c 4 64
mknod /dev/hda1 b 3 1
Non si possono usare le funzioni della libreria standard C come printf(), strcat(), ...
Linux fornisce alcune funzioni, come ad esempio printk(), la cui interfaccia e molto simile a quella di printf().
Si possono usare solo gli header del kernel!
Compilazione di un kernel module
A kernel module is not an independent executable, but an object file which will be linked into the kernel in runtime and they should be compiled with
-c flag
_KERNEL_ symbol
MODULE symbol
LINUX symbol
CONFIG_MODVERSIONS symbol
Example of simple char device
/* The necessary header files */
/* Standard in kernel modules */
#include /* We’re doing kernel work */
#include /* Specifically, a module */
#if CONFIG_MODVERSIONS==1
#define MODVERSIONS
#include
#endif
#include
#include
#ifndef KERNEL_VERSION
#define KERNEL_VERSION(a,b,c) ((a)*65536+(b)*256+(c))
#endif
#include
#define SUCCESS 0
/* Device Declarations */
/* The name for our device, as it will appear
/* in /proc/devices */
#define DEVICE_NAME "char_dev"
#define BUF_LEN 80
/* Used to prevent */
/* concurent access into the same device */
static int Device_Open = 0;
/* The message the device will give when asked */
static char Message[BUF_LEN];
static char *Message_Ptr;
/* This function is called whenever a process
* attempts to open the device file */
static int device_open(struct inode *inode, struct file *file)
{
static int counter = 0;
#ifdef DEBUG
printk ("device_open(%p,%p)\n", inode, file);
#endif
printk("Device: %d.%d\n“, inode->i_rdev >> 8, inode->i_rdev & 0xFF);
if (Device_Open)
return -EBUSY;
Device_Open++;
sprintf(Message,
counter++,
Message_Ptr = Message;
MOD_INC_USE_COUNT;
return SUCCESS;
}
Comments