62

Kernel entrance to-geek-

  • Upload
    mao999

  • View
    515

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Kernel entrance to-geek-
Page 2: Kernel entrance to-geek-

● Twitter : mao(@rivarten)● From: 愛媛県新居浜市

Niihama City, Ehime Prefecture

● 個人事業 Sole proprietorship.

プライダクト Pridact

https://www.pridact.com

● Since 2016.8.2〜

– 自社サービス開発運営developing & operating own services

– 自社製品開発/製造/販売developing & manufacturing & selling

own products– 受注開発

order developing– ハードからソフトまで

from hardware to software

Page 3: Kernel entrance to-geek-

I Love

Motorcycle!!!:)

In front of the vast cabbage field in Nagano prefecture

Page 4: Kernel entrance to-geek-

● Love making cocktail. Often go drinking to shot bar.

● Singapore Sling Raffles Style

Page 5: Kernel entrance to-geek-

10/28(Sat) 9:20 〜 18:00カーネルもくもく会@松山 #4kernel mokumoku-kai@matsuyama #4

● Place: 愛媛大学総合情報メディアセンター 2 階Center for Information Technology,

Ehime university. 2F● 終了後、懇親会開催予定!

飲まない懇親会!→飲む懇親会!ウェイ!– After Party: no alchol party (talking,sweets),

then yes alchol party! (talking,eating,drinking Yeahhhh!!!)

● カーネル、 OS 自作、低レイヤに興味ある方!!Anyone is ok who are interested in Kernel/OS/low-layer

● 毎月開催しています It’s held montly.

Page 6: Kernel entrance to-geek-
Page 7: Kernel entrance to-geek-
Page 8: Kernel entrance to-geek-

https://blogs.yahoo.co.jp/takashi_fujii_1964/GALLERY/show_image.html?id=32640290

Page 9: Kernel entrance to-geek-
Page 10: Kernel entrance to-geek-

・ kernel : Core part of OS

・ Manage computer resources so that    applications can operate using resources

https://ja.wikipedia.org/wiki/ カーネル

Page 11: Kernel entrance to-geek-

● Monolithic Kernel

– UNIX and its derivative OS. Linux,

– MS-DOS

– Windows 9x Series

● Micro Kernel

– Windows NT(NT Kernel)

● Hybrid Kernel

– Windows XP or later

Page 12: Kernel entrance to-geek-

The Bootstrap

Page 13: Kernel entrance to-geek-

Why “Boot”

Page 14: Kernel entrance to-geek-
Page 15: Kernel entrance to-geek-
Page 16: Kernel entrance to-geek-
Page 17: Kernel entrance to-geek-
Page 18: Kernel entrance to-geek-
Page 19: Kernel entrance to-geek-

Don’t you want to understand what’s

going on?

Page 20: Kernel entrance to-geek-

And ...

Page 21: Kernel entrance to-geek-

be them?

Page 22: Kernel entrance to-geek-

Yes!!! Wanna be!!

Page 23: Kernel entrance to-geek-

Geek’s World!!

Hello, geeks!!

Page 24: Kernel entrance to-geek-
Page 25: Kernel entrance to-geek-
Page 26: Kernel entrance to-geek-
Page 27: Kernel entrance to-geek-

1.Power ON!!!

2.Bootloader

3.OS

Page 28: Kernel entrance to-geek-

1.Load necessary programs into memory

2.Get hardware parameters

3.Create data structures in memory

4.Setting parameters on hardware

5.Repeat 2~4

Page 29: Kernel entrance to-geek-

● LILO

- Obsolete- ELILO(UEFI version)

● GRUB/GRUB2 (BIOS)

- Alive.- Load MBR into memory- Start OS with multistage boot.- Realmode/With address constraint/assembly.

● Boot Manager (UEFI)

- Modern style.- GPT(GUID Partition Table).- There is no boot sector.- Place the boot loader (UEFI image) on EFI partition.- The UEFI image is PE binary (the header similar to Win system application).- Operated in 32/64 bit protect mode.- No size limit, no memory capacity limit.- All can be written in C language.

Page 30: Kernel entrance to-geek-

GRUB

http://git.savannah.gnu.org/cgit/grub.gitgit://git.sv.gnu.org/grub.git

Page 31: Kernel entrance to-geek-
Page 32: Kernel entrance to-geek-

・ x86 / x86_64

Page 33: Kernel entrance to-geek-

● Preparation to boot kernel

● Load from boot disk  Load MBR --> 0x7C00

● Check hardware and prepare for transition to protected mode

● Transition to protected mode

● Expanding compressed linux kernel

● Boot kernelstart_kernel() !!!!! YEAH!!

Page 34: Kernel entrance to-geek-

1.Execute MBRboot.img↓

2.Execute GRUB Kernelcore.img↓

3.Execute Linux Kernel

Page 35: Kernel entrance to-geek-

・ Read own continuation program(boot.S , diskboot.S)First 1 sector of boot.img / core.img

+・ Program to expand compressed GRUB Kernel

After 2 sectors of core.img +

・ Compressed GRUB Kernel

Page 36: Kernel entrance to-geek-

_startgrub-core/boot/i386/pc/startup_raw.S

-> grub_main() grub-core/kern/main.c

-> ... -> grub_cmd_linux()

grub-core/loader/i386/linux.c -> grub_cmd_initrd()

grub-core/loader/i386/linux.c-> grub_linux_boot()

grub-core/loader/i386/linux.c

Page 37: Kernel entrance to-geek-
Page 38: Kernel entrance to-geek-

・ Read MBRMBR → 0x07C00 – 0x07BFFboot.imggrub-core/boot/i386/pc/boot.S

・ Read first 512 bytes (1 sector) of core.imgFirst 512 bytes of core.img → 0x70000 - 0x701FFgrub-core/boot/i386/pc/diskboot.S

・ Move to top 512 bytes of core.img0x70000 – 0x701FF → 0x08000 – 0x081FF

Page 39: Kernel entrance to-geek-

・ Transfer the control to beginning of core.img0x07C00 – 0x07E00 → 0x08000 – 0x08200

・ Read rest of core.img0x08000 – 0x081FF : boot.S(512byte)0x70000 - : rest of core.img

・ Move rest of core.img0x08000 – 0x081FF : boot.S0x08200 - : rest of core.img

・ Transfer the control to rest of core.img0x08000 – 0x08200 → 0x08200 – grub-core/boot/i386/pc/startup_raw.S

Page 40: Kernel entrance to-geek-

・ grub_cmd_linux・ Get kernel boot option・ Open kernel image file・ Read the kernel header・ Check the header・ Allocate and set the memory area

for passing the boot parameters to the kernel.・ Copy entire kernel header to parameter area・ Set kernel entry point to parameter

params->code32_start Read from kernel header : pref_address

= 0x1000000・ Setting command line options to pass to the kernel

Page 41: Kernel entrance to-geek-

・ grub_cmd_linux()・ Skip kernel setup routine・ Load decompression routine and compresse kernel to 0x100000, notify grub kernel to call grub_linux_boot() later.

Page 42: Kernel entrance to-geek-

・ grub_cmd_initrd()・ Load initrd file in memory

・ Processing by passing the initrd option string of the setting file grub.cfg as an argument・ Find the upper limit (addr_max) lower limit   (addr_min) of the free address range that can   load the initrd file. ・ Allocate memory zone

grub_relocator_alloc_chunk_align()・ Load initrd file into memory

Page 43: Kernel entrance to-geek-

・ grub_linux_boot()Transfer the control to linux kernel.

state.eip = params->code32_start;

code32_start:0x1000000Linux kernel has been loaded to 0x100000.Copy to 0x1000000 and jump to there by grub_relocator32_boot().

↓Linux kernel entry point is executed !!!!

Page 44: Kernel entrance to-geek-
Page 45: Kernel entrance to-geek-

● System partitionFAT32 format.First partition of the disk.Application (bootloader) written in UEFI SDK is arranged.

● UEFI application binary is PE binary.※Like Win EXE

● If Linux is compiled with CONFIG_EFI_STUB, the PE header is inserted in the first sector of the kernel image, the boot manager recognizes it as PE binary, and the boot process proceeds normally.

Page 46: Kernel entrance to-geek-
Page 47: Kernel entrance to-geek-

● arch/x86/boot/header.S [ _start:

Kernel Setup Code.CONFIG_EFI_STUB=y → EFI Boot Stub combinedKernel Header.Setting of stack etc..

● arch/x86/boot/main.c [ void main(void)  Old.From LILO.

Various settings in real mode.Go to protect mode with go_to_protected_mode().

● arch/x86/boot/compressed/head_32.S [ startup_32:

Kernel Startup.Top of compressed version image.Various settings and transfer control of compressed kernel deployment.

● arch/x86/kernel/head_32.S [ startup_32: (BootStrap Processor Entry Point

Top of decompressed version image.Various settings and call i386_start_kernel() → start_kernel()

● arch/x86/kernel/head_32.S [ startup_32_smp: (Secondary Processor Entry PointAfter the second core.Various settings and call i386_start_kernel() → start_kernel()

Page 48: Kernel entrance to-geek-

● init/main.c start_kernel()

1.Interrupt disabled with local_irq_disable()2.Initialize BSP(BootStrap Processor) data structure with boot_cpu_init()2.setup_arch()

・Setting of interrupt relation early_trap_init()・ Initialize the data structure used for page table reconfiguration. early_ioremap_init()・Create physical address map setup_memory_map() (old ver) → e820__memory_setup() (new ver)・Complete the page table init_mem_mapping()・Create page structure paging_init()・Many other initialization processes

3.Interrupt handler setting with trap_init()4.Initialize page structure and memory allocator with mm_init()5.Initialize scheduler with sched_init()6.early_irq_init(), init_IRQ(), tick_init(), rcu_init_nohz(), init_timers(), hrtimers_init(), softirq_init(), timekeeping_init(), time_init(), sched_clock_postinit(), local_irq_enable() … 7.Initialize slab allocator with kmem_cache_init_late().8.console_init(), sched_clock_init(), pidmap_init(), acpi_early_init(), fork_init() …9.Execute the /sbin/init program with rest_init(). Also start up the second and subsequent CPU cores here.

Page 49: Kernel entrance to-geek-

Search AP’s Entry Point :

startup_32_smp/secondary_startup_64

Page 50: Kernel entrance to-geek-

[ver4.9.16 arch/x86/realmode/init.c:46]static void __init setup_real_mode(void){

……16-bit segment relocations…32-bit linear relocations.trampoline_header = (struct trampoline_header *)_va(real_mode_header->trampoline_header);

#ifdef CONFIG_X86_32trampoline_header->start = __pa_symbol(startup_32_smp);trampoline_header->gdt_limit = __BOOT_DS + 7;trampoline_header->gdt_base = __pa_symbol(boot_gdt);

#elserdmsrl(MSR_EFER, efer);trampoline_header->efer = efer & ~EFER_LMA;trampoline_header->start = (u64) secondary_startup_64;tranpoline_cr4_features = &trampoline_header→cr4;*trampoline_cr4_features = mmu_cr4_features;trampoline_pgd = (u64 *) __va(real_mode_header->trampoline_pgd);trampoline_pgd[0] = trampoline_pgd_entry.pgd;trampoline_pgd [511] = init_level4_pgt[511].pgd;

#endif}

WHO CALL YOUR NAME???

Page 51: Kernel entrance to-geek-

・ Searched AP’s Entry Point : startup_32_smp/secondary_startup_64

start_kernel

→rest_init →kernel_thread(Kernel threading: kernel_init() ) →kernel_init →kernel_init_freeable →do_pre_smp_initcalls →early_initcall sequence →init_real_mode →setup_real_mode

Page 52: Kernel entrance to-geek-

[ver4.9.16 arch/x86/realmode/init.c:138]static int __init init_real_mode(void){ setup_real_mode(); setup_real_mode_permissions(); return 0;}early_initcall(init_real_mode);

Page 53: Kernel entrance to-geek-

・ early_initcall

Linker Script[ver4.9.16 include/asm-generated/vmlinux.lds.h:]

You can refer to the address with __initcall_start, __ initcall0_start, and so on.

Page 54: Kernel entrance to-geek-

・ early_initcall

[ver 4.9.16 arch/init/main.c:479]static void __init do_pre_smp_initcalls(void){ initcall_t *fn; for (fn = __initcall_start; fn < __initcall0_start; fn++) do_one_initcall(*fn);}[ver 4.9.16 ainit/main.c:766]int __init_or_module do_one_initcall(initcall_t fn){

…if (initcall_debug) ret = do_one_initcall_debug(fn);else ret = fn();…return ret;

}[ver 4.9.16 arch/init/main.c:479]static noinline void __init kernel_init_freeable(void){ … do_pre_smp_initcalls(); //Execute function belonging to early_initcall … do_basic_setup(); //Execute initcall and sequentially call other than .early.}[ver 4.9.16 arch/init/main.c:479]static int __ref _kernel_init(void *unused){ int ret; kernel_init_freeable(); …}

Page 55: Kernel entrance to-geek-

[ver 4.9.16 arch/init/main.c:479]

static noinline void __ref rest_init(void){ int pid; rcu_scheduler_starting(); kernel_thread(kernel_init, NULL, CLONE_FS); numa_defaut_policy(); pid = kernel_thread(kthreadd, NULL, CLONE_FS | CLONE_FILES); rcu_read_lock(); kthrad_task = find_task_by_pid_ns(pid, &init_pid_ns); rcu_read_unlock(); complete(&kthreadd_done); init_idle_bootup_task(current); schedule_preempt_disable(); cpu_startup_entry(CPUHP_ONLINE);}

Page 56: Kernel entrance to-geek-
Page 57: Kernel entrance to-geek-

10/28(Sat) 9:20 〜 18:00カーネルもくもく会@松山 #4kernel mokumoku-kai@matsuyama #4

● Place: 愛媛大学総合情報メディアセンター 2 階Center for Information Technology,

Ehime university. 2F● 終了後、懇親会開催予定!

飲まない懇親会!→飲む懇親会!ウェイ!– After Party: no alchol party (talking,sweets),

then yes alchol party! (talking,eating,drinking Yeahhhh!!!)

● カーネル、 OS 自作、低レイヤに興味ある方!!Anyone is ok who are interested in Kernel/OS/low-layer

● 毎月開催しています It’s held montly.

Page 58: Kernel entrance to-geek-
Page 59: Kernel entrance to-geek-
Page 60: Kernel entrance to-geek-

● vmlinux.o + .tmp_kallsyms2.o --(ld)--> vmlinux (ELF)

● vmlinux --(nm)--> System.map

● vmlinux --(objcopy)--> Image/vmlinux.bin

● Image/vmlinux.bin --(gzip)--> piggy.gz

● piggy.gz --(gcc)--> piggy.o

● head_(BITS).o + misc.o + ... + piggy.o --(ld)--> vmlinux (ELF)

● header.o + main.o + ... --(ld)--> setup.elf

● vmlinux (ELF) --(objcopy)--> vmlinux.bin

● setup.elf --(objcopy)--> setup.bin

● vmlinux.bin + setup.bin --(Special tool)--> bzImage

※.tmp_kallsyms2.o  Object code that retrieves only symbols from all object files and holds them as a single ELF section header.

Page 61: Kernel entrance to-geek-

10/28(Sat) 9:20 〜 18:00カーネルもくもく会@松山 #4kernel mokumoku-kai@matsuyama #4

● Place: 愛媛大学総合情報メディアセンター 2 階Center for Information Technology,

Ehime university. 2F● 終了後、懇親会開催予定!

飲まない懇親会!→飲む懇親会!ウェイ!– After Party: no alchol party (talking,sweets),

then yes alchol party! (talking,eating,drinking Yeahhhh!!!)

● カーネル、 OS 自作、低レイヤに興味ある方!!Anyone is ok who are interested in Kernel/OS/low-layer

● 毎月開催しています It’s held montly.

Page 62: Kernel entrance to-geek-