Did anybody start project 1? What sorting algorithm did you use? Could also use qsort library function if you want Let's make a quick macro that does something useful: SIZE(), and it gives the number of elements in a local array Could it give an error or something if it's not a local array? Alright, how about a FOREACH that works on local arrays? void*, since it's in project 1: Pointers are 64-bit unsigned integers Pointers can point to any type, since it's just a number Use size_t for data structure sizes ssize_t for signed size_t (why is this ok?) sizeof returns the size of variables void* can point to anything! So you have to cast it to a more useful type Can add a variable here if you want (will be optimized out) Allocating Memory: malloc and free calloc Don't forget to deallocate! Arrays vs. Python lists, and realloc - Userspace: C++ vector - Kernel: Kernel linked lists Notes about linking: We'll build, but not link, a file with a C function Then later, write a program that uses it Does it work if we build with clang on one file, and gcc on another? If we compile the library on FreeBSD? If we engineer a name conflict? Inefficiency of static linking Multifunction machines to save space Dynamic linking instead with .so files kernel will use .ko Kernel Modules! A note about versions: Review from 253, and what do version numbers mean? LKM: Around since Linux 1.2 (1995) Why? (kinda covered it last time) - Debugging - Recompile less often - Reboot less often (reset and update both) - Not slower - Kernel is smaller - Reload a particular driver without rebooting Why not? - More complicated - Management system required for hotplug - I used to dislike them Used for: - Device Drivers (most of the time) - Filesystems - Can add system calls - Network drivers (operate protocol) - Executable interpreter (ELF is normal, can add more) Utilities: insmod rmmod depmod lsmod modinfo modprobe Inserting Modules: insmod or modprobe Unresolved symbols: - Symbols are exported from the kernel OR MODULES - Dependencies among modules - /proc/kallsyms - Use count remove with rmmod or modprobe -r Could not find kernel version - Descriptive error messages are sometimes lacking How do you tell if it worked? - Depends on the module - lsmod will tell you if it's there Automatic Loading - Do you really want this? Maybe...probably not - unload after 1min with autoclean Where are LKMs found? - Usually /lib/modules Versions: Kernel version must match module version - Memory layout, kernel subroutines, etc - Did anything important really change? insmod -f - Symbol versioning - gcc versions - What about multiple kernels? + People do that sometimes... - Symbol licensing errors and viral GPL Boot without device drivers? - This is complicated and probably not a good idea - Loader can load stuff into RAM before kernel starts - Still need to understand FS and run programs How insmod works: init_module system call init_module subroutine in module - Linking is done at insertion time Debugging is possible with kdb Memory: Linux kernel is not pageable - virtual addresses = real addresses Modules can't be paged either, but virtual addresses != real addresses - kmalloc Demo: blink.c /proc filesystem: Demo adding something crypto.c Let's make read pedantic