From cbea7cc02c868711109ae1a261d01fd0473eea0b Mon Sep 17 00:00:00 2001 From: Ciro Santilli Date: Sat, 30 Sep 2017 21:27:12 +0100 Subject: [PATCH] your inits are belongz to uz --- .gitignore | 1 + README.md | 39 +++++++++++++++++++++--------- kernel_module/user/README.md | 4 ++- kernel_module/user/init_hello.c | 2 +- kernel_module/user/init_poweroff.c | 11 +++++++++ rootfs_overlay/README.md | 2 +- rootfs_overlay/init.sh | 7 ++++++ runqemu | 1 + 8 files changed, 53 insertions(+), 14 deletions(-) create mode 100644 kernel_module/user/init_poweroff.c create mode 100755 rootfs_overlay/init.sh diff --git a/.gitignore b/.gitignore index 3a977e5..9cd153c 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ /rootfs_overlay/etc/init.d/S99 Module.symvers modules.order +trace* diff --git a/README.md b/README.md index f031760..46b58b7 100644 --- a/README.md +++ b/README.md @@ -258,6 +258,18 @@ and they will be run automatically before the login prompt. For convenience, we also setup a symlink from `S99` to `rootfs_overlay/etc/init.d/S99`. +### Custom init + +Is the default BusyBox `/init` too bloated for you, minimalism freak? + +No problem, just use the `init` kernel boot parameter: + + ./runqemu -e 'init=/init_hello.out' + +Remember that shell scripts can also be used for `init`: + + ./runqemu -e 'init=/init.sh' + ## Debugging To GDB the Linux kernel, first run: @@ -562,7 +574,7 @@ says: (EE) Failed to load module "modesetting" (module does not exist, 0) -## Count instructions +## Count boot instructions - - @@ -570,17 +582,21 @@ says: - `qemu/docs/tracing.txt` and `qemu/docs/replay.txt` - -Naive attempt: add to `S99`: +Best attempt so far: - poweroff + time ./runqemu -n -e 'init=/init_poweroff.out' -- -trace exec_tb,file=trace && \ + time ./qemu/scripts/simpletrace.py qemu/trace-events trace >trace.txt && \ + wc -l trace.txt -Then run as: +Parameter notes: - time ./runqemu -n -- -trace exec_tb,file=trace - ./qemu/scripts/simpletrace.py qemu/trace-events trace >trace.txt - wc -l trace +- `-n` is a good idea to reduce the chances that you send unwanted non-deterministic mouse or keyboard clicks to the VM. -This requires: +- `-e 'init=/init_poweroff.out'` is crucial as it reduces the instruction count from 40 million to 20 million, so most instructions were actually running on the VM. + + Without it, the bulk of the time seems to be spent in setting up the network with `ifup` that gets called from `/etc/init.d/S40network` from the default Buildroot BusyBox setup. + +This works because we have already done the following with QEMU: - `./configure --enable-trace-backends=simple`. This logs in a binary format to the trace file. @@ -588,13 +604,14 @@ This requires: This also alters the actual execution, and reduces the instruction count by 10M TODO understand exactly why, possibly due to the `All QSes seen` thing. -- the simple QEMU patch mentioned at: +- the simple QEMU patch mentioned at: of removing the `disable` from `exec_tb` in the `trace-events` template file in the QEMU source Possible improvements: -- replace init with our own C program that immediately does a `shutdown` system call +- to disable networking. Is replacing `init` enough? -- disable networking. Is replacing `init` enough? + - + - - logging with the default backend `log` greatly slows down the CPU, and in particular leads to this during kernel boot: diff --git a/kernel_module/user/README.md b/kernel_module/user/README.md index ce981c3..2213b5e 100644 --- a/kernel_module/user/README.md +++ b/kernel_module/user/README.md @@ -11,9 +11,11 @@ These programs can also be compiled and used on host. 1. Standalone 1. [myinsmod](myinsmod.c) 1. [myrmmod](myrmmod.c) - 1. [init_hello](init_hello.c) 1. [usermem](usermem.c) 1. [pagemap_dump](pagemap_dump.c) + 1. inits + 1. [init_hello](init_hello.c) + 1. [init_poweroff](init_poweroff.c) 1. [uio_read](uio_read.c) 1. Module tests 1. [anonymous_inode](anonymous_inode.c) diff --git a/kernel_module/user/init_hello.c b/kernel_module/user/init_hello.c index 6c0b424..17eb2b3 100644 --- a/kernel_module/user/init_hello.c +++ b/kernel_module/user/init_hello.c @@ -5,7 +5,7 @@ #include int main(void) { - puts("hello world"); + puts(__FILE__); while (1) sleep(0xFFFFFFFF); } diff --git a/kernel_module/user/init_poweroff.c b/kernel_module/user/init_poweroff.c new file mode 100644 index 0000000..1e651e4 --- /dev/null +++ b/kernel_module/user/init_poweroff.c @@ -0,0 +1,11 @@ +/* Userspace is for the weak. Die. + * https://stackoverflow.com/questions/28812514/how-to-shutdown-linux-using-c-or-qt-without-call-to-system */ + +#include +#include +#include + +int main(void) { + puts(__FILE__); + reboot(RB_POWER_OFF); +} diff --git a/rootfs_overlay/README.md b/rootfs_overlay/README.md index 0137cad..b021b44 100644 --- a/rootfs_overlay/README.md +++ b/rootfs_overlay/README.md @@ -7,6 +7,6 @@ We use it to for things like: - customized configuration files - userland module test scripts -Most tests correspond clearly to a given kernel module, but the following ones don't, e.g. they correspond to mainline tree features: +Most tests correspond clearly to a given kernel module, but the following ones don't: - [gpio](gpio.sh) diff --git a/rootfs_overlay/init.sh b/rootfs_overlay/init.sh new file mode 100755 index 0000000..3ee0be9 --- /dev/null +++ b/rootfs_overlay/init.sh @@ -0,0 +1,7 @@ +#!/bin/sh +# Can init be a sh script? Yes. +# Sure, why not: https://unix.stackexchange.com/questions/174062/init-as-a-shell-script/395375#395375 +echo 'hello init.sh' +while true; do + sleep 1 +done diff --git a/runqemu b/runqemu index 9f9f61d..cc026d5 100755 --- a/runqemu +++ b/runqemu @@ -117,4 +117,5 @@ case "$arch" in " ;; esac +echo "$cmd" eval "$cmd"