about summary refs log tree commit diff stats
path: root/dev/c/system.html
diff options
context:
space:
mode:
Diffstat (limited to 'dev/c/system.html')
-rw-r--r--dev/c/system.html159
1 files changed, 159 insertions, 0 deletions
diff --git a/dev/c/system.html b/dev/c/system.html
new file mode 100644
index 0000000..65eede1
--- /dev/null
+++ b/dev/c/system.html
@@ -0,0 +1,159 @@
+<!DOCTYPE html>
+<html dir="ltr" lang="en">
+    <head>
+        <meta charset='utf-8'>
+        <title>System Development &amp; GDB</title>
+    </head>
+    <body>
+        <a href="../index.html">Development Index</a>
+
+        <h1>System Development</h1>
+
+        <p>System development requires knowing how to debug
+        kernel know how, in this example will be used Qemu
+        and GDB. Qemu creates the virtual machine that kernel
+        will run on and GDB will connect to it to help us
+        understand how things tick.</p>
+
+        <h2>Kernel Build</h2>
+
+        <pre>
+        $ tar xf linux-4.9.48.tar.xz
+        $ cd linux-4.9.48
+        </pre>
+
+        <p>Default configuration disable some security
+        configurations that allow us to debug (random memory
+        layout).</p>
+
+        <pre>
+        $ make x86_64_defconfig
+        </pre>
+
+        <p>Enable CONFIG_DEBUG_INFO, CONFIG_DEBUG_INFO_DWARF4
+        and CONFIG_GDB_SCRIPTS in the kernel;</p>
+
+        <pre>
+        make x86_64_defconfig
+        cat &lt;&lt;EOF &gt;.config-fragment
+        CONFIG_DEBUG_INFO=y
+        CONFIG_DEBUG_KERNEL=y
+        CONFIG_GDB_SCRIPTS=y
+        EOF
+        ./scripts/kconfig/merge_config.sh .config .config-fragment
+        </pre>
+
+        <p>Check or change to your needs the configuration;</p>
+        <pre>
+        $ make nconfig
+        </pre>
+
+        <p>Build Kernel and modules;</p>
+
+        <pre>
+        $ make -j $(nproc)
+        </pre>
+
+        <h2>Simple Init</h2>
+
+        <p>Now that you have the kernel compiled you can
+        create a simple init program, this program is called
+        when kernel finish to load and setup its internals
+        and is ready to launch first process.
+        Init program should not exit or kernel will panic.
+        Create init.S;</p>
+
+        <pre>
+        .global _start
+        _start:
+            mov $1, %rax
+            mov $1, %rdi
+            mov $message, %rsi
+            mov $message_len, %rdx
+            syscall
+            jmp .
+            message: .ascii "FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR\n"
+            .equ message_len, . - message
+        </pre>
+
+        <p>Assemble, link and create simple initial ram disk;</p>
+
+        <pre>
+        mkdir d
+        as --64 -o init.o init.S # assemble
+        ld -o d/init init.o      # link
+        cd d
+        find . | cpio -o -H newc | gzip > ../rootfs.cpio.gz
+        </pre>
+
+        <p>Clean temporary directory;</p>
+
+        <pre>
+        cd ..
+        rm -r d/
+        </pre>
+
+        <p>Can be used C to create init program;</p>
+
+        <pre>
+        #include &lt;stdio.h&gt;
+        #include &lt;unistd.h&gt;
+
+        int main() {
+            printf("FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR\n");
+            sleep(0xFFFFFFFF);
+            return 0;
+        }
+        </pre>
+
+        <pre>
+        $ gcc -static init.c -o init
+        </pre>
+
+        <h2>Start Debugging</h2>
+
+        <p>Test qemu, kernel and simple init program, you
+        should see
+        "FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR";</p>
+
+        <pre>
+        $ qemu-system-x86_64 -enable-kvm --kernel arch/x86_64/boot/bzImage \
+        --initrd rootfs.cpio.gz
+        </pre>
+
+        <p>If everything goes well you can start qemu without
+        starting the CPU (-S) and with gdb server on TCP port 1234 (-s).</p>
+
+        <pre>
+        $ qemu-system-x86_64 -enable-kvm --kernel arch/x86/boot/bzImage \
+                --initrd rootfs.cpio.gz \
+                -S -s
+        </pre>
+
+        <p>On another terminal start gdb;</p>
+
+        <pre>
+        gdb \
+            -ex "add-auto-load-safe-path $(pwd)" \
+            -ex "file vmlinux" \
+            -ex 'set arch i386:x86-64:intel' \
+            -ex 'target remote localhost:1234' \
+            -ex 'break start_kernel' \
+            -ex 'continue' \
+            -ex 'disconnect' \
+            -ex 'set arch i386:x86-64' \
+            -ex 'target remote localhost:1234'
+        </pre>
+
+
+        <a href="../index.html">Development Index</a>
+        <p>
+        This is part of the c9-doc Manual.
+        Copyright (C) 2016
+        c9 team.
+        See the file <a href="../../fdl-1.3-standalone.html">Gnu Free Documentation License</a>
+        for copying conditions.</p>
+
+    </body>
+
+</html>