Skip to content

Instantly share code, notes, and snippets.

@PEMessage
Forked from MaskRay/kernel-arm64.md
Created May 13, 2024 17:11
Show Gist options
  • Save PEMessage/c10e4500bcd0bff38416baa03855595d to your computer and use it in GitHub Desktop.
Save PEMessage/c10e4500bcd0bff38416baa03855595d to your computer and use it in GitHub Desktop.
Linux kernel's arm64 port

In the kernel source directory, run

make O=/tmp/linux/arm64 ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -j60 defconfig all

Then create an Alpine aarch64 image following https://hackmd.io/@starnight/Run_Alpine_on_QEMU_aarch64_Virtual_Machine

wget https://dl-cdn.alpinelinux.org/alpine/latest-stable/releases/aarch64/netboot/vmlinuz-lts https://dl-cdn.alpinelinux.org/alpine/latest-stable/releases/aarch64/netboot/config-lts

qemu-img create -f qcow2 alpine-aarch64.qcow2 16G
qemu-system-aarch64 -smp 2 -M virt -cpu cortex-a57 -m 1G -nographic \
  -kernel vmlinuz-lts -initrd initramfs-lts \
  --append "console=ttyAMA0 ip=dhcp alpine_repo=http://dl-cdn.alpinelinux.org/alpine/latest-stable/main/ modloop=http://dl-cdn.alpinelinux.org/alpine/latest-stable/releases/aarch64/netboot/modloop-lts" \
  -hda alpine-aarch64.qcow2 \
  -netdev user,id=unet -device virtio-net-device,netdev=unet -net user

On the guest,

# user: root, no password
setup-alpine  # install to vda

On the host,

sudo modprobe nbd max_part=8
sudo qemu-nbd -c /dev/nbd0 alpine-aarch64.qcow2
sudo mount /dev/nbd0p1 /mnt/
cp /mnt/vmlinuz-lts ./vmlinuz-lts.img
sudo chmod o+r /mnt/initramfs-lts
cp /mnt/initramfs-lts ./initramfs-lts.img
sudo umount /dev/nbd0p1
sudo qemu-nbd -d /dev/nbd0
qemu-system-aarch64 -smp 2 -M virt -cpu cortex-a57 -m 1G -nographic \
  -kernel vmlinuz-lts.img -initrd initramfs-lts.img \
  --append "console=ttyAMA0 root=/dev/vda3 rw rootfstype=ext4" \
  -hda alpine-aarch64.qcow2 \
  -device e1000,netdev=net0 \
  -net nic -netdev user,hostfwd=tcp:127.0.0.1:2222-:22,id=net0

To debug a just built kernel, replace -kernel vmlinuz-lts.img with -kernel /tmp/linux/arm64/arch/arm64/boot/Image and add -s -S. Then, in another terminal, run

gdb /tmp/linux/arm64/vmlinux
(gdb) tar rem :1234
(gdb) hb start_kernel
(gdb) c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment