From b03d9581807d1faa071dececc4dcb0ac2102fadf Mon Sep 17 00:00:00 2001 From: Silvino Silva Date: Fri, 15 Sep 2017 02:29:30 +0100 Subject: dev c language system dev revision --- dev/c/index.html | 87 +----------------------------- dev/c/system.html | 159 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ dev/index.html | 1 + 3 files changed, 161 insertions(+), 86 deletions(-) create mode 100644 dev/c/system.html diff --git a/dev/c/index.html b/dev/c/index.html index 684c7d2..1c3b478 100644 --- a/dev/c/index.html +++ b/dev/c/index.html @@ -7,92 +7,6 @@ Development Index -

System Development

- -

System development requires debug kernel know how, - in this example will be used qemu and gdb. A simple init - program is created. Create init.S;

- -
-	.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
-	
- -

Assemble, link and create simple initial ram disk;

- -
-	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
-	
- -

Clean temporary directory;

- -
-	cd ..
-	rm -r d/
-	
- -

Can be used C to create init program;

- -
-	#include <stdio.h>
-	#include <unistd.h>
-
-	int main() {
-	    printf("FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR\n");
-	    sleep(0xFFFFFFFF);
-	    return 0;
-	}
-	
- -
-	$ gcc -static init.c -o init
-	
- -

Test qemu, kernel and simple init program, you should see - "FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR";

- -
-	$ qemu-system-x86_64 -enable-kvm --kernel arch/x86_64/boot/bzImage \
-    	--initrd rootfs.cpio.gz
-	
- -

If everything goes well you can start qemu without start CPU at - startup (-S) and with gdb server on TCP port 1234 (-s).

- -
-	$ qemu-system-x86_64 -enable-kvm --kernel arch/x86/boot/bzImage \
-                --initrd rootfs.cpio.gz \
-		-S -s
-	
- -

On another terminal start gdb;

- -
-	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'
-	
- -

C & GDB

C program with autotools @@ -172,6 +86,7 @@

         strace -c ./program
         
+ Development Index

This is part of the c9-doc Manual. 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 @@ + + + + + System Development & GDB + + + Development Index + +

System Development

+ +

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.

+ +

Kernel Build

+ +
+        $ tar xf linux-4.9.48.tar.xz
+        $ cd linux-4.9.48
+        
+ +

Default configuration disable some security + configurations that allow us to debug (random memory + layout).

+ +
+        $ make x86_64_defconfig
+        
+ +

Enable CONFIG_DEBUG_INFO, CONFIG_DEBUG_INFO_DWARF4 + and CONFIG_GDB_SCRIPTS in the kernel;

+ +
+        make x86_64_defconfig
+        cat <<EOF >.config-fragment
+        CONFIG_DEBUG_INFO=y
+        CONFIG_DEBUG_KERNEL=y
+        CONFIG_GDB_SCRIPTS=y
+        EOF
+        ./scripts/kconfig/merge_config.sh .config .config-fragment
+        
+ +

Check or change to your needs the configuration;

+
+        $ make nconfig
+        
+ +

Build Kernel and modules;

+ +
+        $ make -j $(nproc)
+        
+ +

Simple Init

+ +

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;

+ +
+        .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
+        
+ +

Assemble, link and create simple initial ram disk;

+ +
+        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
+        
+ +

Clean temporary directory;

+ +
+        cd ..
+        rm -r d/
+        
+ +

Can be used C to create init program;

+ +
+        #include <stdio.h>
+        #include <unistd.h>
+
+        int main() {
+            printf("FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR\n");
+            sleep(0xFFFFFFFF);
+            return 0;
+        }
+        
+ +
+        $ gcc -static init.c -o init
+        
+ +

Start Debugging

+ +

Test qemu, kernel and simple init program, you + should see + "FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR";

+ +
+        $ qemu-system-x86_64 -enable-kvm --kernel arch/x86_64/boot/bzImage \
+        --initrd rootfs.cpio.gz
+        
+ +

If everything goes well you can start qemu without + starting the CPU (-S) and with gdb server on TCP port 1234 (-s).

+ +
+        $ qemu-system-x86_64 -enable-kvm --kernel arch/x86/boot/bzImage \
+                --initrd rootfs.cpio.gz \
+                -S -s
+        
+ +

On another terminal start gdb;

+ +
+        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'
+        
+ + + Development Index +

+ This is part of the c9-doc Manual. + Copyright (C) 2016 + c9 team. + See the file Gnu Free Documentation License + for copying conditions.

+ + + + diff --git a/dev/index.html b/dev/index.html index cb238d7..e463f6f 100644 --- a/dev/index.html +++ b/dev/index.html @@ -45,6 +45,7 @@
  • Control Flow
  • Functions
  • Input & Output
  • +
  • System Development