Linux Kernel Internals - Part 1
Application
Application
Application
Application
System Call Interface/Interrupt Handling
Kernel Subsystem
Device Drivers
Application
Application
Application
Application
System Call Interface/Interrupt Handling
Kernel Subsystem
Device Drivers
DATA
Prev
Next
DATA
Prev
Next
DATA
Prev
Next
DATA
Prev
Next
DATA
Prev
Next
typedef struct list_head
{
struct list_head *prev;
struct list_head *next;
};
struct some_other_struct
{
char *data1;
int data2;
struct list_head *head;
}
https://www.oreilly.com/library/view/linux-device-drivers/0596000081/ch10s05.html
typedef struct example_struct
{
struct example_struct *prev;
struct example_struct *next;
};
struct some_other_struct
{
char *data1;
int data2;
struct example_struct *head;
};
typedef struct example_struct
{
struct example_struct *prev;
struct example_struct *next;
};
struct some_other_struct
{
char *data1;
int data2;
struct example_struct *head;
} __randomize_struct;
#define offsetof(a,b) ((int)(&(((a*)(0))->b)))
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) ); })
https://www.geeksforgeeks.org/introduction-to-red-black-tree/
2
13
22
15
19
9
8
6
2
13
22
15
19
9
8
6
newvruntime = minimum_vruntime
newvruntime = time_elapsed * niceness
int 3
instruction?Some things to note:
def_idts, apic_idts, idt_table
idt_data
- Not what the CPU Usesidt_init_desc
converts a single idt_data
to a gate_desc
gate_desc
is the format x86 CPU WantsIO APIC
IO APIC
IO APIC
CPU/IDT
enum
{
HI_SOFTIRQ=0,
TIMER_SOFTIRQ,
NET_TX_SOFTIRQ,
NET_RX_SOFTIRQ,
BLOCK_SOFTIRQ,
BLOCK_IOPOLL_SOFTIRQ,
TASKLET_SOFTIRQ,
SCHED_SOFTIRQ,
HRTIMER_SOFTIRQ,
RCU_SOFTIRQ,
NR_SOFTIRQS
};
raise_softirq(TIMER_SOFTIRQ)
From: https://www.oreilly.com/library/view/understanding-the-linux/0596005652/ch04s07.html
void tasklet_hadnler(unsigned long data);
worker_thread
function is used for the kernel worker thread
work_struct
DECLARE_WORK
or INIT_WORK
- initailize a worker queue (work_struct)schedule_work
- don't need to describe this oneflush_scheduled_work
- wait for work to be donerequest_irq/free_irq
- register and unregister an interrupt handler
IRQF_DISABLED
- Disable all interrupts when this handler executesIRQF_SAMPLE_RANDOM
- Use this handler as an entropy sourceIRQF_TIMER
- Processes system timer interruptsIRQF_SHARED
- Can be shared by mutliple handlers#include <linux/module.h>
static int __init start(void)
{
printk(KERN_INFO "Hello World!\n");
return 0;
}
static void __exit mod_stop(void)
{
printk(KERN_INFO "Goodbye World\n");
return;
}
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Michael Wittner");
MODULE_DESCRIPTION("Simple Demo.");
module_init(start);
module_exit(mod_stop);
Defines which functions called on load/removal of a kernel module
Macros for licensing and defining init and exit
Where can you find printk messages?
# Basic Makefile for Kernel Modules - Kernel module with one C file
obj-m := example.o # Your C file should match the H file
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
# Inserting kernel modules
insmod example.ko optparam1="param" optparam2=2
#Removing modules
rmmod
#If on pwn.college practice mode, do this instead
vm build /path/to/.c/file
vm start
vm connect
#Look at vm --help and vm <command> --help for more details
Note: to export symbols, use macro EXPORT_SYMBOL
For this homework, you will be creating a kernel module that implements an interrupt handler.
You will need to create an interrupt handler for an IRQ number and share it with another handler. Every time it gets interrupted, a kernel thread should be created where it increases a counter by 5. After completing it, it should print out the value using a deferred work mechanism.
Things you need to keep in mind for this homework: