Monday, January 11, 2010

Linux memory

KernelTrap High Memory




HighMem
  • ZONE_HIGHMEM - dynamically mapped pages/processes > 896MB
LowMem
  • ZONE_NORMAL - normal addressable pages 16-896MB
  • ZONE_DMA - DMA-able pages <>

Process memory

Stack segment
  • Parameters
  • Return address
  • Return frame pointer
  • Local variables
Heap segment - dynamic memory allocation

Data segment
  • BSS - uninitialized global data structures
  • Data - initialized global variables and data structures
Text/code segment

All threads within a process share code and data segment.
All threads have their own stack.

Wednesday, January 7, 2009

Linux kernel cross compile with crosstool

Steps to build gcc/glibc toolchain using crosstool
  • Download crosstool
  • Set CC=gcc-3.4
  • Change /bin/sh to bash
  • Build crosstool
$ sh demo-powerpc-603.sh

Steps to cross compile Linux kernel using gcc/glibc toolchain
  • Set the path to gcc/glibc toolchain
  • Build Linux kernel
$ make ARCH=powerpc CROSS_COMPILE=powerpc-603-linux-gnu-

Tuesday, January 6, 2009

Linux kernel cross compile with buildroot

Steps to build gcc/uClibc toolchain using buildroot
  • Download buildroot
  • Change configuration to build for powerpc-603
$ make menuconfig
  • Build buildroot
$ make

Steps to cross compile Linux kernel using gcc/uClibc toolchain
  • Set the path to gcc/uClibc toolchain
  • Build Linux kernel
$ make ARCH=powerpc CROSS_COMPILE=powerpc-linux-

Monday, January 5, 2009

Linux bootable disk image using EXTLINUX, debootstrap

Steps to create a bootable disk image using EXTLINUX, debootstrap, Linux kernel and run using QEMU
  • Create disk image
$ dd if=/dev/zero of=rootfs.img bs=1M count=2560
  • Create ext2 filesystem
$ mke2fs -j rootfs.img
$ tune2fs -c 0 -i 0 rootfs.img
  • Mount disk image
$ sudo mount rootfs.img rootfs -o loop -t ext2
  • Run debootstrap
$ sudo debootstrap --arch i386 intrepid rootfs
  • Copy Linux kernel and initial ramdisk to boot directory
$ cp vmlinuz rootfs/boot
$ cp initrd.img rootfs/boot
  • Install EXTLINUX bootloader
$ extlinux --install rootfs/boot
$ vi rootfs/boot/extlinux.conf
default linux
timeout 10
prompt 1

label linux
kernel /boot/vmlinuz
append initrd=/boot/initrd.img root=/dev/sda clocksource=kvm-clock
  • Unmount disk image
$ sudo umount rootfs
  • Run in QEMU
$ qemu -hda rootfs.img -m 1024

Sunday, January 4, 2009

Linux bootable disk image using EXTLINUX, Busybox

Steps to create a minimal root filesystem using BusyBox and run Linux kernel using QEMU
  • Download BusyBox and build with CONFIG_STATIC=y
  • Create disk image
$ dd if=/dev/zero of=rootfs.img bs=1M count=10
  • Make ext2 filesystem
$ mke2fs -j rootfs.img
$ tune2fs -c 0 -i 0 rootfs.img
  • Mount disk image
$ sudo mount rootfs.img rootfs -o loop -t ext2
  • Make boot directory
$ mkdir rootfs/boot
  • Copy busybox to disk image
$ rsync -a busybox*/_install/ rootfs
  • Create directories and files
$ cd rootfs
$ mkdir etc etc/init.d dev proc sys

$ cd etc
$ vi inittab
::sysinit:/etc/init.d/rcS
::askfirst:/bin/sh
$ chmod 644 inittab

$ cd init.d
$ vi rcS
#! /bin/sh
mount -a
$ chmod 744 rcS

$ cd ..
$ vi fstab
proc /proc proc defaults 0 0
$ chmod 644 fstab
$ cd ../..
  • Unmount disk image
$ sudo umount rootfs
  • Run in QEMU
$ qemu -hda rootfs.img -m 1024

Saturday, January 3, 2009

Linux bootable disk image using EXTLINUX, buildroot

Steps to create a root filesystem using buildroot and run Linux kernel using QEMU
  • Download buildroot
$ git clone git://git.buildroot.net/buildroot
  • Update buildroot
$ cd buildroot
$ git pull
  • Change configuration to build for i386
$ make menuconfig
  • Build buildroot
$ make
  • Run in QEMU
$ qemu -kernel /boot/vmlinuz -initrd /boot/initrd.img -hda binaries/uclibc/rootfs.i386.ext2 -append "root=/dev/sda rw" -m 1024

Steps to create a bootable disk image using EXTLINUX, buildroot, Linux kernel and run using QEMU
  • Mount buildroot root filesystem
$ sudo mount binaries/uclibc/rootfs.i386.ext2 buildrootfs -o loop -t ext2
  • Create disk image
$ dd if=/dev/zero of=rootfs.img bs=1M count=10
  • Create ext2 filesystem
$ mke2fs -j rootfs.img
$ tune2fs -c 0 -i 0 rootfs.img
  • Mount disk image
$ sudo mount rootfs.img rootfs -o loop -t ext2
  • Copy buildroot to disk image
$ rsync -a buildrootfs/ rootfs
  • Create boot directory
$ mkdir rootfs/boot
  • Copy Linux kernel and initial ramdisk to boot directory
$ cp vmlinuz rootfs/boot
$ cp initrd.img rootfs/boot
  • Install EXTLINUX bootloader
$ extlinux --install rootfs/boot
$ vi rootfs/boot/extlinux.conf
default linux
timeout 10
prompt 1

label linux
kernel /boot/vmlinuz
append initrd=/boot/initrd.img root=/dev/sda clocksource=kvm-clock
  • Unmount disk image
$ sudo umount rootfs
  • Run in QEMU
$ qemu -hda rootfs.img -m 1024

Friday, January 2, 2009

Linux kernel development using Eclipse/CDT and QEMU

Steps to build, install and run Linux kernel using QEMU.
  • Download Linux kernel tree
$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git linux-2.6.git
  • Update Linux kernel tree
$ cd linux-2.6.git
$ git pull
  • Prepare to build Linux kernel by reusing old Linux kernel config
$ cp /boot/config-`uname -r` .config
$ make oldconfig
  • View Linux kernel config
$ make menuconfig
  • Build Linux kernel
$ make
  • Install Linux kernel modules
$ make modules_install
  • Install Linux kernel
$ make install
  • Build initial ramdisk
$ update-initramfs -k 2.6 -c
  • Update GRUB
$ update-grub
  • Browse source code
$ cscope -Rk
  • Run new Linux kernel in QEMU
$ qemu -kernel /boot/vmlinuz-2.6 -initrd /boot/initrd.img-2.6 -hda /dev/zero -m 1024
  • In QEMU, enter Ctrl+Alt+2 to get into QEMU monitor
  • To check everything is ok
(qemu) info status

Steps to build and run Linux kernel using Eclipse/CDT and QEMU
  • Install Eclipse/CDT and disable indexer
  • Create new C Makefile project with default location pointing to linux-2.6
  • Build Linux kernel in Eclipse
  • Configure Eclipse debugger to use C/C++ application as vmlinuz, stop on startup at start_kernel(), use gdbserver with connection type tcp/1234
  • Run QEMU with GDB-stub
$ qemu -S -s -kernel /boot/vmlinuz-2.6 -hda /dev/zero -m 1024
  • Run Linux kernel in Eclipse
  • Debug in Eclipse will stop at start_kernel

Thursday, January 1, 2009

Linux startup sequence

BIOS
  • System startup
Stage 1 boot loader
  • MBR
Stage 2 boot loader
  • GRUB - /boot/grub
  • Loads kernel - /boot/vmlinuz
  • Runs kernel - startup_32()
Kernel
  • Runs non-architecture kernel - start_kernel()
  • Loads initial ramdisk - /boot/initrd.img
  • Loads driver modules - /lib/modules
  • Loads root filesystem
  • Runs /sbin/init
Init
  • Reads /etc/inittab
  • Reads /etc/event.d
  • Runs default run level
Run levels
  • /etc/rc0.d System halt
  • /etc/rc1.d Single user
  • /etc/rc2.d Full multi-user (default)
  • /etc/rc3.d Full multi-user
  • /etc/rc4.d Full multi-user
  • /etc/rc5.d Full multi-user
  • /etc/rc6.d System reboot
Mount filesystems
  • /proc
  • /sys

Linux resources

Linux.com
Linux.org
Kernel.org
Linux Kernel Newbies
Linux Journal

eLinux
CE Linux Forum
Embedded Linux conference
Linux Devices
Free Electrons
Tuxology
Scratchbox
Buildroot