Two things need to be done:
APIC Timer initialization: PIT ok → measure how many ticks are done by APIC timer using PIT (1ms) interrupt; disable interrupt temporarily; set APIC timer and disable PIT IRQ; enable APIC timer.
A miscellaneous garden for playing with the kernel :P. Because this is a personal note on Linux kernel, I do not give any guarantee that all the information listed here is accurate. Notice that this note will introduce some notions we have already learned in undergraduate courses, but it will give more details on the implementations of the kernel.
You may build QEMU from source by following this tutorial, note that you should configure QEMU build by
$ ./configure --enable-slirp
$ sudo apt install libslirp-dev -y
Clone the official Linux kernel repo from kernel.org (or some other custom git repositories).
cd
to the directory. Assume we are under ./linux
.
Install necessary dependencies via package manger.
$ sudo apt update
$ sudo apt upgrade
$ sudo apt install libncurses-dev flex bison openssl libssl-dev \\
dkms libelf-dev libudev-dev libpci-dev \\
libiberty-dev autoconf dwarves
# Install qemu
$ sudo apt install qemu qemu-system qemu-kvm libvirt-daemon-system \\
libvirt-clients bridge-utils
$ sudo apt install gdb
Generate the configuration file for make
.
$ make ARCH=x86_64 x86_64_defconfig
<*>
<*>
If you are later going to play with custom kernel modules, these changes will also be necessary/helpful:
[ ]
sudo rmmod -f some_module
) Enable loadable modules support → Module unloading - Forced module unloading [*]
Then build the kernel.
$ make -j`nproc` bzImage
The image will be located under ./linux/arch/x86_64/boot/bzImage
, and by default, the kernel is built with debug symbols and a gdb script:
$ ls ./arch/x86_64/boot
bzImage
$ ls | grep vm on git:master|…1
vmlinux
vmlinux-gdb.py
# Not needed.
vmlinux.o
vmlinux.symvers
Setup the gdb.
$ echo "add-auto-load-safe-path ./linux/vmlinux-gdb.py" >> ~/.gdbinit
Since this image is bare-metal image without any filesystem support, we need to first build a running filesystem for it. The buildroot
project can help us on this. Details about this project can be found at https://buildroot.org.
$ cd ./linux && git clone <https://git.buildroot.net/buildroot.git>
$ cd buildroot && make menuconfig
Set the following options on the prompt.
x86_64
[*]
ext4
variant[*]
; this helps us to later send files into the QEMU guest through SSH conveniently.Start QEMU. Note -append
allows us to pass the boot parameters directly to the Linux kernel running in the QEMU VM.
$ sudo qemu-system-x86_64 \\
-kernel arch/x86_64/boot/bzImage \\
-nographic \\
-drive format=raw,file=buildroot/output/images/rootfs.ext4,if=virtio \\
-append "root=/dev/vda console=ttyS0 nokaslr other-paras-here-if-needed" \\
-m 4G \\
-enable-kvm \\
-cpu host \\
-smp $(nproc) \\
-nic user,model=e1000e
Attach gdb to the instance.
$ sudo gdb ./linux/vmlinux
$ (gdb) target remote :1234
$ c # conitnues running.
I’ve added printk
to cpu_idle
.
Fetch the cloud image from the website.