2022年5月9日 星期一

1st Linux Kernel Module

Tips

  • C code
    • MODULE_LICENSE("Dual BSD/GPL"); is must for all drivers
    • Hook functions for insmod and rmmod
      • module_init(function_name): called by system for insmod
      • module_exit(function_name): called by system for rmmod
    •  EXPORT_SYMBOL macro helps to export functions
  • Makefile
    • CFILES := main.c sub.c # multiple files included
    • obj-m # indicate this module is in the kernel domain
    • hello-objs := $(CFILES:.c=.o)  # no redundant blanks
    • V=1 # shows detailed compile process
  • Concept of context
    • for driver, we have "Process context" and "Interrupt context"
    • Process context, aka user context, is triggered from user space process
    • Interrupt context, is trigger by HW interrupt
  • Methods
    • readelf -s hello.ko # display the symbol table; man readelf for more options
    • file hello.ko