Posts

Showing posts from 2010

Linux SPI

My notes while reading about Linux and SPI. De facto - no standardization like i2c/smbus. Master/slave like i2c Faster clock 10MHz vs 400kHz i2c Full duplex, unlike i2c. Daisy chaining (in addition to / instead of) wired-AND bus like i2c. Dedicated chip select signals instead of i2c addressing. Linux software support only applies to master side, like i2c. Four clocking modes (?) - CPOL - clock starts low(0)/high(1) - CPHA - sample data on leading(0)/trailing(1) edge Signals - SCK - clock, idle when not used like in i2c - MOSI: Master Out, Slave In - MISO: Master In, Slave Out - nCSx: Chip select(s) - device specific signals Generally no interoperability above bit-level - word lengths vary. - endianness varies - no discovery or enumeration mechanism Subsets - MicroWire: half duplex - Synchronous serial protocl SSP - Programmable serial protocol PSP - three wire SPI with single data signal MOMI/SISO SPI slave chips - DataFlash <> driver name - probed based spi_board_info associate

Filter selection through pipe in Emacs

Emacs has this wonderful feature of filtering a selection through a unix pipe. It's just a bit hard to remember how to invoke. C-u M-| command RET

RPM cheat sheet

list installed packages, dpkg -l rpm -qa info on an installed package, dpkg -s rpm -qi contents of an installed package, dpkg -L rpm -q -l package where a file belongs to, dpkg -S rpm -q -f extract package contents without installing, dpkg -x rpm2cpio file.rpm | cpio -idv

debugging makefile variables

To debug makefile variable assigments. Use make -p (--print-data-base) to see which values get assigned to variables and where. Need to add to reading list: Managing projects with GNU make.

OpenSSH host key signature fingerprint

Note to self. It's ssh-keygen that can dump the host key fingerprint in a format suitable for PuTTY security alert comparisons. ssh-keygen -l -f /etc/ssh/ssh_host_rsa_key.pub

Linux i2c

My notes while reading Linux kernel i2c source code and the documentation. i2c-dev implements the character devices for userspace, a generic, "remote controlled" i2c chip driver ioctl to select slave read()/write() for a subset of smbus operations i2c-tools with library functions i2c adapter A host bus adapter instance class - allow probing for slave devices in class: HWMON, TV, DDC, SPD nr - 'id' number of instance algo - operations to implement i2c_add_[numbered_]adapter() exported API i2c_transfer() - sequence of low level i2c operations i2c_smbus_transfer() - sequence of high level smbus operations i2c algorithm operations to implement a type of host bus adapter: bitbang, philips bus controllers master_xfer() - implement low level i2c transaction segment: operations to read or write bytes to/from i2c device smbus_xfer() - implement high level SMbus access functionality() - query available functionality: SMBUS support, 10bit addressing master_xfer can b

LDD3 notes: Networking

My notes while reading Linux Device Drivers 3rd edition , network drivers. General and setup Network devices have no /dev entry point different namespace file operations don't make sense on network interface. Why? I think they could. [alloc|register|unregister|free]_netdev alloc_netdev variants alloc_[eth|fc|fddi|tr]dev separate [ltalk|fc|fddi|hippi|tr]_setup private data not a pointer to driver allocated data, but allocated along with the net_device. supply size to allocate_netdev() use netdev_priv(dev) to access the data interface flags IFF_* flags & IFF_DEBUG, enable debuggin via ioctl: netifdebug IFF_UP change -> open()/stop() any flag change -> set_multicast_list() Features, interesting ones NETIF_F_NO_CSUM - interface needs no checksums NETIF_F_HW_CSUM - interface hardware does checksums NETIF_F_VLAN_[TX|RX|FILTER] NETIF_F_HIGHDMA by default all socket buffers are in low memory Networking device structure jiffy timestamps for last tx/rx, tx watchdog timeout. Transmi

Free UML tools

Excellent summary of freely available UML tools, with screenshots. http://www.devcurry.com/2010/06/free-open-source-uml-tools.html But it's missing the one I'm using the most: http://plantuml.sourceforge.net/

LDD3 notes: TTY

My notes while reading Linux Device Drivers 3rd edition , TTY drivers. TTY drivers are a generic implementation of a serial port interface with loose coupling of hardware access and data formatting. It is a char device composed of tty core for user space char device interface line discipline for data formatting tty driver for hardware access write can be called from interrupt context. must succeed for single byte write write buffer treatment put_char, flush_chars - start sending them, return right away wait_until_sent - start sending, wait until done or timeout flush_buffer - discard write buffered chars. read tty_insert_flip_char() for each char tty_flip_buffer_push() whenever count reaches size at the end of sequence set_termios() 38400,8n1 -style serial settings handshake mechanism selection tiocm{g|s}et() access control lines ioctl() tty driver first, except break fallback to inherit core functionality with -ENOICTLCMD Get Line Status Register LSR: data, overrun, parity, framing, b

iMovie event library on a network drive, NAS

I was shocked to find that iMovie '09 would not allow storing event libraries on networked disks. The AFP volume would show up, but remain non-functional with a yellow exclamation sign. There are plenty of instructions for hacking around the limitation with clumsy symbolic links. I discovered a much simpler approach, using an undocumented (?) setting built right into iMovie:"Allow Network Volumes". Use at your own risk, try the following command in Terminal defaults write -app iMovie allowNV -bool true

Mac Time Machine with Linux server

Finally, an easy way to have Time Machine accept a Linux server as a backup disk. Upgrade to Ubuntu 10.4 . Install netatalk Add option "tm" to a share in AppleVolumes.default config file. No need to mess with Manually created sparsebundles Shoehorn settings like TMShowUnsupportedNetworkVolumes The key is to have netatalk version 2.0.5 or above.

LDD3 notes: DMA

My notes while reading Linux Device Drivers 3rd edition , Memory mapping and DMA. Kernel addressing Physical address Logical Address Virtual address Bus address (physical address as understood by the DMA peripheral) __pa() , __va() for low memory only Concepts PFN - page frame number. (virtual or physical) address / page size struct page 'handle' to a page of memory. page_address() macro to get virtual address, if mapped kmap() kernel virtual address for any page in the system may sleep, but has an atomic version VMA - virtual memory area. a contiguous, homogeneous region of process virtual address space. visible in /proc/pid/maps vm_area_struct nopage() - process accessed a missing page, go get it vm_pgoff, offset in pages into the backing file mm_struct - a list of virtual memory areas for a single process. threads may share one. implementing mmap remap_pfn_range(), once and for all ( io_remap_page_range() ) - reserved pages only: locked in memory, outside of memory ma

LDD3 notes: Driver Model

My notes while reading Linux Device Drivers 3rd edition . 2.6 device model addresses aspects common to all types of devices: Power management Userspace communications /sysfs Hotplugging w/ udev Device classes Object lifecycle Kobject, Kset, Subsystem kobject per object, belongs to a ktype. kset for a collection of kobjects of same type Subsystem as a high-level portion of the kernel as a whole: block, devices, usb, pci Class as high-level view of what a device does Kobject embed struct kobject in your structure initialize name, ktype, kset and parent implement a destructor (release) implement reference counting by wrapping kobject_{get|put} back-cast kobject references to your structure using using container_of() Adding a Kobject to a Kset point kobject.kset to set point kobject.parent to the set kobject_register() Subsystem a kset with a semaphore rooted at the top of the sysfs hierarchy subsystem_{init|register|unregister|get|put} Bus, Device , Driver , Class , Class device Bus as a

LDD3 notes: USB

My notes while reading Linux Device Drivers 3rd edition . General naming: root_hub-hub_port[-hub_port]:config.interface write to bConfigurationValue will switch configuration & bound driver URBs skbuff analogy queued, async, acknowledged usb_alloc_urb(), usb_fill_[int|bulk]_urb(), usb_submit_urb() transfer buffers must be kmalloc():ed, not static or automatic device_id's can match specific vendor/product whole device class / subclass /protocol individual interface class MODULE_DEVICE_TABLE(usb, usb_device_id table)

vmware player network configuration

Vmware player 3.0.1 is a surprisingly capable and free virtualization environment. "Player" in the name is a bit misleading as many of the restrictions in earlier versions have been lifted. However, network configuration tool is not installed by default and needs to be manually extracted. Richard at f5 devcentral provides the details: You can install it manually by extracting it from the install file. VMware-player-3.0.1-227600.exe /e .\vmwareextract Open network.cab and copy vmnetcfg.exe to the root of your player install. You now have a nice graphical editor for your virtual network.

LDD3 notes: Data types

My notes while reading Linux Device Drivers 3rd edition . size_t vs ssize_t for negative values as error codes C99 [u]intptr_t, integer which would fit a pointer. Not used in Linux. Similar to eCos cyg_addrword. fixed size integers u8..s32 within kernel ioctl structures shared with userspace use prefix __u8 _t Typedefs for portability only out of fashion inconvenient printk use Important constants: HZ. PAGE_SIZE/SHIFT Endianness cpu_to_le32() .. be64_to_cpu() Padding force struct to match hardware expectations, without padding for performance: __attribute__ ((packed)) Error codes in pointers ERR_PTR() -> IS_ERR() -> PTR_ERR() Lists list_del_init() - re-init for future re-insertion list_for_each_entry() - foreach and list_entry combined

LDD3 notes: Memory

My notes while reading Linux Device Drivers 3rd edition . Allocation Flags - GFP_KERNEL - system call, i.e. behalf of a process running in kernel -> may sleep - GFP_ATOMIC - won't sleep, but has limited memory available Slab caches for quick allocation of constant size objects ( == object pool ) - a.k.a lookaside cache - ctor/dtor can be called in atomic context, indicated by a flag Mempool - a wrapper for slab cache which keeps some free memory ready - try to avoid vmalloc contiguous region of virtual memory non-hardware related work only, like loading modules arbitrary size GFP_KERNEL internally, cannot use in atomic context limited address space available, avoid. kmalloc arbitrary size contiguous region of physical memory with 1:1 mapped virtual addresses get_free_page[s]() & get_order() power of 2 number of pages more than 1 is failure prone with 1:1 mapped virtual addresses alloc_pages() indirect access through struct page get/put_cpu_var() good for cou

Insert a picture by reference in MS Word

I've always disliked manual, repetitive tasks with MS Word. There is one less to worry about, now that I've found about inserting and linking pictures. In insert picture file dialog, select "Insert and Link", to make a link referencing the original file to keep it automatically updated when the picture changes. have an embedded copy in the document to avoid broken links when sending out the Word document. How to Insert Images in a Word Document without Embedding

LDD3 notes: Passage of time

My notes while reading Linux Device Drivers 3rd edition .Jiffies get_jiffies_64() comparison macros: time_after/before jiffies <-> timeval / timespec get_cycles() short busy waits n/u/mdelay() 1ms/1s resolution sleeps msleep(), ssleep() Working with 1/HZ resolution sleeps wait_event_timeout() - returns timeout left, never negative set_current_state(interruptible), schedule_timeout() in_atomic(), in_interrupt() Sleeping with waitqueues macro: one init_waitqueue_head, multiple wait_event(_interruptible), one wake_up to wake up them all. manual wait_event alternative: prepare_to_wait(), schedule(), finish_wait(), signal_pending() 'exclusive' sleepers are woken up as specified batches or individually, they won't behave like a herd going to schedule soon, use wake_up_onterruptible_sync, it won't reschedule right away never use sleep_on, it's broken w/ race condition Tasklets atomic context in timer interrupt handler or softirq multiple schedules re

RCU

Reading a few LWN articles on RCUs (1 2 3) really shed some light on their properties and use. On read-intensive scenarios, it's more efficient to replace read/write locking with an RCU. This is possible because all Linux platforms have atomic pointer read/write operations. Read critical section dereferences pointers through a mechanism with platform specific memory ordering guarantees: rcu_dereference() may not sleep may not keep or pass dereferenced pointers outside the critical section Write critical section Protects agains concurrent writes using regular spinlock mutex Makes a copy of the original structure, making updates in the copy Swaps in the new version, while still keeping the old version (atomic) Invokes synchronize_rcu() to wait for all readers to exit their current rcu read critical sections. ( A non-pre-emptible kernel can simpy wait for all CPUs to switch contexts. ) Free the old version Properly used RCUs are immune to deadlocks, but synchronous writes may be

LDD3 notes: Concurrency

My notes while reading Linux Device Drivers 3rd edition . Reasons to pay attention to concurrency Early kernels had no SMP, no pre-emption -> enough to protect from interrupts SMP and pre-emption both pose similar concurrency requirements, even though you'd be willing to ignore the other one. shared resources -> avoid semaphores: sema_init, up, down (declare_mutex) read/write semaphore pairs: init_rwsem, [up|down]_[read|write], downgrade_write, trylock semaphores are dangerous as automatic variables Completions init_completion, wait_for_completion (uninterruptible), complete, complete_all, complete_and_exit (thread) Spinlocks higher performance than semaphores disables pre-emption on current cpu may not sleep while holding one mutual exclusion with interrupts ok with spin_lock_irqsave, within one function R/W spinlocks Lockless data structures: kfifo generic circular buffer atomic_t, an 24bit integer atomic bit operations set_bit, clear_bit, change_bit, test_bit

LDD3 notes: Debugging

My notes while reading Linux Device Drivers 3rd edition . printk_ratelimit() tells if we're not flooding the log Kernel configuration CONFIG_DEBUG_SLAB -> canary killed CONFIG_DEBUG_SPINLOCK_SLEEP -> potential sleeps with spinlocks detected CONFIG_DEBUG_INFO, CONFIG_FRAME_POINTER for gdb debugging seq_file - kernel in-memory file buffer, similar to open_memstream or ostringstream. a cleaner interface for implementing a /proc file iterator/visitor: start, show, next, stop api for the visitor: seq_printf into a seq_file fops implemented for reading debugging a live kernel through a 'dynamic' core dump gdb vmlinux /proc/kcore, core-file /proc/kcore cannot modify data, breakpoint, watchpoint or single-step add-symbol-file for modules, using /sys/module/*/sections/.* like with a jtag emulator print *(address) kdb -debugging from SGI ia32 only built-in to kernel, pause/break key as 'ctrl-c' takes you into debugger has breakpoints and can modify data sees modu

LDD3 notes: Device registration and operations

My notes while reading Linux Device Drivers 3rd edition . register_chrdrv replaced with cdev_add(), cdev_del() and struct cdev register_chrdev_region, alloc_chrdev_region, unregister_chrdev_region to statically, dynamically pick a contiguous block of dev_t's i.e. major and minor numbers. assign them a device name ( /dev/devices and sysfs ) no need to know major/minor numbers at open look at inode->cdev, deduce filp->private_data using using container_of Notes on proper behaviour with open/close if the device cannot seek: nonseekable_open() , no_llseek struct file represents an open file descriptor in kernel, can be shared by multiple processes one open, fork&dup, single struct file, multiple close, one release Notes on proper behaviour with read/write, Select(BSD)/Poll(SystemV)/Epoll(Linux) O_NONBLOCK ( == O_NDELAY) and no progress possible -> immediate -EAGAIN _interruptible fails -> -ERESTARTSYS, VFS will retry or return -EINTR poll reports device writab

LDD3 notes: Compiling and loading modules

My notes while reading Linux Device Drivers 3rd edition . out of tree module makefile boilerplate with dual purpose standalone: invokes kernel tree modules target referenced by M: acts like in-tree kbuild makefile disposable sections with __init, __initdata, __exit module loading races register facilities only when really ready to take calls at failure, previously registered facilities can be in use already module_param() automatically exposed in /sys/module with given access permissions, can be read/written to module won't be notified of writes

Shared libraries and ELF

I just finished reading How to write Shared Libraries by Ulrich Drepper, a very nice guide to understanding the details of dynamic linking and use of ELF format with Linux. ELF structure like program header and segments Clear explanation of GOT and PLT with concrete examples Types of relocations: relative, symbol relocation and evils of TEXTREL Tracing dynamic linking w/ LD_DEBUG concept of gnu-hash style and backwards compatibility with -hash-style=both attributes constuctor, destructor common variables and -fno-common controlling exported symbols with static / -fvisibility / attribute / #pragma visibility / export maps symbol versioning: implementing backwards compatibility with a single dso: export maps and symver foo@VERS1 for past versions / symver foo@@VERS2 for the latest dangers of depending on unfined symbols and -Wl,-z,defs deprecated rpath vs runpath relative paths to dso:s using dynamic string tokens like $ORIGIN --as-needed to prune the list of DSO in DT_NEEDED

gnu binutils

I decided to read through the man pages of binutils, to see if there's something interesting I've never come across. The only things that were not previously very familiar were the c++filt and addr2line tools. Anyway, here are the notes I made while reading about each of the tools in the binutils package. ar and ranlib manage archives: static libraries of compiled object files. Most often these are used indirectly through libtool and automake. nm Lists symbols from object files. This is useful when troubleshooting linking problems. With nm you can check which data and functions are actually found in each intermediate object or library. objcopy Copies and converts object files. Typical use of objcopy is to convert and ELF file into a binary file. This is effectively pre-computing the work of an elf loader for targets where elf loader is not relevant at run time, like ROM code. Objcopy can also work the other way. Objcopy can wrap a binary file in an elf object, so that binary d

Dangers of C library

I just read through Ulrich Drepper's Defensive Programming article. He highlights dangers of C library, provides safer alternatives for commonly used idioms and introduces a number of tools for pinpointing problems in code. These are my rather unstructured notes of things most interesting to me. Syntax for forward declarations within paramer list and array size annotation for future checks int addvec(int n; int arr[n], int n) tools for formatting an arbitrarily long string aprintf() which allocates the buffer, for constant format string only FILE *open_memstream() , for an ostringstream -like in-memory file fast string handling within stack strdupa() and alloca() parsing strings of arbitrary size %as format string with buffer allocated by scanf parsing simple delimited files with arbitrary length of lines ssize_t getdelim() ssize_t getline() practical examples of using file descriptor variants of regular functions for avoiding race conditions eg. chown -> fchown O_NOFO

Booting Linux over JTAG

Note to self. A working gdb script to boot Linux kernel on an atmel board over JTAG. Unfortunately this is not completely automatic. u-boot needs to be interrupted for loading the uImage through gdb. U-boot can then be resumed and kernel booted with bootm 0x22000000 . Also, either the emulator needs to be configured to ignore data aborts or kernel futex support to be disabled. target remote emulator:2001 monitor reset halt define bootstrap file /path/to/at91sam9260ek/at91sam9260ek-nandflashboot-2.11-r1.elf load j *0x200000 #bootstrap entry point in sram end define u-boot file /path/to/u-boot/u-boot load j *0x23f00000 # u-boot entry point in sdram end define linux restore /path/to/arch/arm/boot/uImage binary 0x22000000 file /path/to/vmlinux end

Optimizing with gcc

My notes while reading Optimizing applications with gcc & glibc by Ulrich Drepper. __extension__ to mark intentional use of GNU extension. poor mans 'lambda' (({ })) predicate to determine if parameter value is constant at runtime: __builtin_constant_p() mark pure functions with __attribute__((__const__)) mark functions which never return with __attribute__((__noreturn__)) __stdcall__ attribute on x86 makes callee correct the stack at ret, in Unix convention the caller corrects the stack mem p cpy returns a pointer just after the last copied byte. calloc knows if memory is already full of 0's -> faster than malloc & memset int_least16_t, int_fast16_t when actual variable size is uninteresting, speed is replace stlen and + with strchr(s,'\0') or rawmemchr() gcc computed gotos and jump tables: && for the address of a label -pg and -profile : gprof and sprof

Eclipse CDT

I'll have to try out the Eclipse CDT sometime - integration of native development tools such as the GNU Autotools: Linux Tools - a plugin to explore remote system files through Eclipse : eclipse-rse

64bit PC's and memory

Just to refresh my knowledge on the PC platforms, I read the excellent Wikipedia article on the 64-bit x86 . PC processors and 64-bitness The current 64-bit instruction set is a backwards compatible extension to the Intel instruction set. It is implemented in all typical processors from all common vendors. It has multiple confusing names, all meaning the same: Intel 64, amd64, EM64T, x86_64, x86-64, IA-32e. Intel Itanium (IA-64) is a completely different thing, and has nothing to do with current 64bit systems. Not all of the 64 bits are in use today. Some of them are cleverly saved for the future by dividing the virtual address space into two halves. By having the unused gap in the middle as a forbidden zone, the address bits reserved for future use are safe from short term abuse by implementations. Maximum physical memory in a PC Having more than 4GB of physical memory is not an issue for 64-bit systems. Even a single process can use more than 4GB. Using more than 4GB of physical memo

Linux Kernel in a nutshell

Although I've been working with Linux kernel for ages, I decided to browse through Linux Kernel in a nutshell , an excellent book on the practical things regarding working with the kernel. There certainly were things that I was not aware of. Here are my notes on these things. Using a read-only source tree with O=/path/to/build/output Some basic static analysis with sparse with C=1 or C=2. checkstack namespacecheck Localversion-prefixed files in object and source trees. Finding all kernel modules required by the running system, using modaliases in the /sys tree and modprobe with --show-depends. Or get-driver.sh helper script. Kernel argument max_addr to force a upper bound on physical addresses used. Probably a good for validating a software image for a low cost board version. I've used mem=XX for this before, but I believe max_addr could yield even more realistic results. The book included nice "demos" for tools like quilt for managing a set of patches agai

USB logic analyzer from Saleae

Yesterday I received my Logic , an affordable USB-attached logic analyzer. The software for Mac OS X is in early stages with very basic capture functionality only. But it already helped me troubleshoot one system. I've used its Windows software in the past, which is much better. I hope the features like integrated protocol analyzers will be soon be part of the Mac package as well.