☰
×
◈ Introductions
◈ Linux Kernel Compilation
◈ Linux Kernel Debugging
◈ Linux Kernel APIs - I
◈ Linux Kernel APIS - II
◈ Linux Kernel APIS - III
◈ KGDB Setup
◈ QEMU Setup
◈ Linux System Calls Table
◈ Useful Linux Commands
Home
About
Consulting
Training
Tutorials
Interview Questions
Contact
Tutorials
Linux Kernel APIs
Home
Linux Kernel APIs
◈ Introductions
◈ Linux Kernel Compilation
◈ Linux Kernel Debugging
◈ Linux Kernel APIs - I
◈ Linux Kernel APIS - II
◈ Linux Kernel APIS - III
◈ KGDB Setup
◈ QEMU Setup
◈ Linux System Calls Table
◈ Useful Linux Commands
Tutorials - Linux Kernel Function
Kernel APIs - III
« Prev
Next »
Linux Kernel APIs - III
☛ Kernel time, timers & delays APIs
☛ Linux Memory Management APIs
☛ Interrupt Handling APIs
☛ Interrupt Handling API
☛ Other Useful APIs
☛ More memory management APIs
☛ File Compression
☛ File Search
11. Kernel time, timers & delays: #include
/* declares value of HZ */ #include
/* which includes linux/jiffies.h */ int time_after(unsigned long a, unsigned long b); int time_before(unsigned long a, unsigned long b); #include
void do_gettimeofday(struct timeval *tv); /* time returned in seconds.microseconds value */ #include
/* Busy-do-nothing loops */ void ndelay(unsigned long nanosecs); void udelay(unsigned long microsecs); void mdelay(unsigned long millisecs); #include
/* Puts process to sleep */ void msleep(unsigned int millisecs); unsigned long msleep_interruptible(unsigned int millisecs); void ssleep(unsigned int seconds); #include
struct timer_list { ... unsigned long expires; void (*function)(unsigned long); unsigned long data; }; void init_timer(struct timer_list *timer); struct timer_list TIMER_INITIALIZER(function, expires, data); void add_timer(struct timer_list *timer); int del_timer(struct timer_list *timer); int mod_timer(struct timer_list *timer, unsigned long expires); Tasklets: #include
#include
struct tasklet_struct { ... void (*func)(unsigned long); unsigned long data; }; void tasklet_init(struct tasklet_struct t, void (*func)(unsigned long),unsigned long data); DECLARE_TASKLET(name,func,data); DECLARE_TASKLET_DISABLED(name,func,data); void tasklet_disable(struct tasklet_struct *t); void tasklet_enable(struct tasklet_struct *t); void tasklet_schedule(struct tasklet_struct *t); void tasklet_hi_schedule(struct tasklet_struct *t); void tasklet_kill(struct tasklet_struct *t); Workqueues: #include
struct workqueue_struct *create_workqueue(const char *name); DECLARE_WORK(name, void (*function)(void *),void *data); INIT_WORK(struct work_struct work,void (*function)(void *),void *data); int queue_work(struct workqueue_struct *que,struct work_struct *work); int queue_delayed_work(struct workqueue_struct *queue, struct work_struct *work, unsigned long delay); void destroy_workqueue(struct workqueue_struct *queue); 12. Linux Memory Management APIs : #include
void *kmalloc(size_t size, int flags); /* flags = GFP_KERNEL, GFP_ATOMIC, */ void kfree(void *); get_zeroed_page(unsigned int flags); __get_free_page(unsigned int flags); __get_free_pages(unsigned int flags, unsigned int order); void free_page(unsigned long addr); void free_pages(unsigned long addr, unsigned long order); kmem_cache_t *kmem_cache_create(const char *name, size_t size, size_t offset,unsigned long flags, void (*constructor)(void *, kmem_cache_t *,unsigned long flags), void (*destructor)(void *,kmem_cache_t *,unsigned long flags)); void *kmem_cache_alloc(kmem_cache_t *cache, int flags); void kmem_cache_free(kmem_cache_t *cache, const void *obj); int kmem_cache_destroy(kmem_cache_t *cache); #include
void *vmalloc(unsigned long size); void vfree(void *addr); void *ioremap(unsigned long offset, unsigned long size); void iounmap(void *addr); #include
struct page *virt_to_page(void *kaddr); void *page_address(struct page *page); int get_user_pages(struct task_struct *tsk, struct mm_struct *mm, unsigned long start, int len, in write, int force, struct page **pages, struct vma_area_struct **vmas); void SetPageDirty(struct page *page); void page_cache_release(struct page *page); #include
void *kmap(struct page *page); void kunmap(struct page *page); use append=”mem=255M” to reserve last 1 MB for DMA in lilo/grub dmabuf = ioremap(0xff000000/* 255 M */ , 0x100000 /* 1 M */ ); #include
unsigned long virt_to_bus(volatile void *address); void *bus_to_virt(unsigned long address); 13. Interrupt Handling : #include
int request_irq(unsigned int irq, irqreturn_t (*handler)(int, void *), unsigned long flags, const char *devname, void *devID); void free_irq(unsigned int irq, void *devID); /* flags are IRQF_DISABLED, IRQF_SHARED, IRQF_SAMPLE_RAMDOM ...) */ Enabling & disabling interrupts : #include
/* Turn off/on a single interrupt across all CPUs*/ void disable_irq(int irq); void enable_irq(int irq); #include
/* Turn off all interrupts on current processor */ void local_irq_save(unsigned long flags); void local_irq_disable(void); void local_irq_restore(unsigned long flags); void local_irq_enable(void); 14. Linux Linked Lists: #include
LIST_HEAD(list); or struct list_head head; INIT_LIST_HEAD(&head); list_add(struct list_head *new, struct list_head *head); list_add_tail(struct list_head *new, struct list_head *head); list_del(struct list_head *entry); list_move(struct list_head *entry, struct list_head *head); list_move_tail(struct list_head *entry, struct list_head *head); list_empty(struct list_head *head); list_splice(struct list_head *list, struct list_head *head); list_entry(struct list_head *ptr, type_of_struct, field_name); list_for_each(struct list_head *cursor, struct list_head *list); 15. Kernel Notification #include
struct notifier_block myBlock { int (*notifier_call)(struct notifier_block *self,unsigned long value,void *clientData); struct notifier_block *next; int priority; }; int notifier_chain_register(struct notifier_block **list, struct notifier_block *myBlock }; int notifier_chain_unregister(struct notifier_block **list, struct notifier_block *myBlock }; int notifier_call_chain(struct notifier_block **list, unsigned long value, void *clientData); 16. Other usfull functions: get_user — Get a simple variable from user space. put_user — Write a simple value into user space. access_ok() container_of() lilkely() unlikely() strlen_user — Get the size of a string in user space. __strncpy_from_user — Copy a NUL terminated string from userspace, with less checking. strncpy_from_user — Copy a NUL terminated string from userspace. clear_user — Zero a block of memory in user space. __clear_user — Zero a block of memory in user space, with less checking. strnlen_user — Get the size of a string in user space. copy_to_user — Copy a block of data into user space. copy_from_user — Copy a block of data from user space. 17. More memory management function: page_dup_rmap — duplicate pte mapping to a page read_cache_pages — populate an address space with some pages & start reads against them page_cache_readahead — generic adaptive readahead filemap_flush — mostly a non-blocking flush sync_page_range — write and wait on all pages in the passed range unlock_page — unlock a locked page end_page_writeback — end writeback against a page find_get_page — find and get a page reference find_get_page — find and get a page reference 16x50 UART Driver uart_handle_dcd_change — handle a change of carrier detect state uart_handle_cts_change — handle a change of clear-to-send state uart_update_timeout — update per-port FIFO timeout. uart_get_baud_rate — return baud rate for a particular port uart_get_divisor — return uart clock divisor uart_register_driver — register a driver with the uart core layer uart_unregister_driver — remove a driver from the uart core layer uart_add_one_port — attach a driver-defined port structure uart_remove_one_port — detach a driver defined port structure serial8250_suspend_port — suspend one serial port serial8250_resume_port — resume one serial port serial8250_register_port — register a serial port serial8250_unregister_port — remove a 16x50 serial port at runtime
« Prev
Next »
Helpful Links
Interview Questions
Corporate Training
Recommended Books
Linux Consulting
Apply for Job Assistance
If you have any queries please email us at
info@motionzen.com