Top Banner
1 Ubuntu system architecture Presentation by Jesse Sung [email protected] www.canonical.com Dec. 2011
47

Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

Apr 11, 2018

Download

Documents

ngoduong
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

1

Ubuntu system architecture

Presentation by

Jesse Sung

[email protected]

www.canonical.com

Dec. 2011

Page 2: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

2

Ubuntu system architecture

●Early init●Early userspace●Real userspace

Page 3: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

3

Ubuntu system architecture

Early init - kernel● Low level initializations● initcalls● Driver model

Page 4: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

4

Ubuntu system architecture

Early init – Low level initializations● arch-specific init

init/main.c:start_kernel() { … setup_arch(); …}

Page 5: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

5

Ubuntu system architecture

Early init – Low level initializations● init internal data structures

init/main.c:start_kernel() { … tick_init(); … pidhash_init(); …}

Page 6: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

6

Ubuntu system architecture

Early init – Low level initializations● mm_init

init/main.c:start_kernel() { … build_all_zonlists(); page_alloc_init(); … mm_init(); …}

Page 7: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

7

Ubuntu system architecture

Early init – Low level initializations● sched_init

init/main.c:start_kernel() { … sched_init(); …}

Page 8: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

8

Ubuntu system architecture

Early init – Low level initializations● init_IRQ

init/main.c:start_kernel() { … init_IRQ(); …}

Page 9: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

9

Ubuntu system architecture

Early init – Low level initializations● Timers

init/main.c:start_kernel() { … init_timers(); hrtimers_init(); …}

Page 10: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

10

Ubuntu system architecture

Early init – Low level initializations● a lot of works done in this stage● Please refer to init/main.c

Page 11: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

11

Ubuntu system architecture

Early init – initcalls● Functions to be called when kernel starts

includes/linux/init.h:early_initcall(fn)pure_initcall(fn)core_initcall(fn)…device_initcall(fn)device_initcall_sync(fn)late_initcall(fn)late_initcall_sync(fn)

Page 12: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

12

Ubuntu system architecture

Early init – initcalls● Pointers of initcalls are stored in an array

early_p1

early_p2

init_p1

init_p2

__initcall_start

__early_initcall_end

__initcall_end

early_init_1() { … }

early_init_2() { … }

init_1() { … }

init_2() { … }

Page 13: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

13

Ubuntu system architecture

Early init – initcalls● Arrange function pointers with macro

includes/linux/init.h:#define __define_initcall(level,fn,id) \ static initcall_t __initcall_##fn##id __used \ __attribute__((__section(“.initcall” level “.init”))) = fn

Page 14: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

14

Ubuntu system architecture

Early init – initcalls● module_init() is included in initcalls for build-in

modules

includes/linux/init.h:#define __initcall(fn) device_initcall(fn)...#define module_init(x) __initcall(x)

Page 15: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

15

Ubuntu system architecture

Early init – initcalls● early_initcalls would be called before smp start

init/main.c:do_pre_smp_initcalls() { initcall_t *fn;

for (fn = __initcall_start; fn < __early_initcall_end; fn++) do_one_initcall(*fn);}

Page 16: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

16

Ubuntu system architecture

Early init – initcalls● Other initcalls would be called after smp start

Init/main.c:kernel_init() { … smp_init(); sched_init_smp(); do_basic_setup(); …}

do_basic_setup() { … do_initcalls();}

Page 17: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

17

Ubuntu system architecture

Early init – initcalls● initcalls called order

● early_initcall● pure_initcall● core_initcall● core_initcall_sync● postcore_initcall● postcore_initcall_sync● arch_initcall● arch_initcall_sync

Page 18: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

18

Ubuntu system architecture

Early init – initcalls● initcalls called order (cont.)

● subsys_initcall● subsys_initcall_sync● fs_initcall● fs_initcall_sync● rootfs_initcall● device_initcall – module_init● device_initcall_sync● late_initcall / late_initcall_sync

Page 19: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

19

Ubuntu system architecture

Early init● Initialization code are stored in a seperate

memory section● Freed after kernel starts

Page 20: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

20

Ubuntu system architecture

Driver model● bus● device● driver

Page 21: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

21

Ubuntu system architecture

Driver model - bus● A bus is a channel between the processor and

one or more devices● PCI, USB, …

Page 22: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

22

Ubuntu system architecture

Driver model – bus● Ability to enumerate devices on the bus

Page 23: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

23

Ubuntu system architecture

Driver model – bus● Descriptor of each device

● Vendor ID● Device ID● Device class● …

Page 24: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

24

Ubuntu system architecture

Driver model – bus● platform bus: a virtual bus● integrated peripherals on SOCs● “legacy” devices

● i8042● pcspkr● serial8250● …

Page 25: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

25

Ubuntu system architecture

Driver model – device● Every device is represented by an instance of struct device

includes/linux/device.h:

struct device { … struct bus_type *bus; struct device_driver *driver; …};

Page 26: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

26

Ubuntu system architecture

Driver model – driver● The device driver-model tracks all of the drivers

known to the system

includes/linux/device.h:

struct device_driver { … struct bus_type *bus; … int (*probe) (struct device *dev); …};

Page 27: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

27

Ubuntu system architecture

Driver model – driver vs device● Match is a bus-specific task● bus_driver should provide a match function

includes/linux/device.h:

struct bus_type { … int (*match) (struct device *dev, struct device_driver *drv); …};

Page 28: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

28

Ubuntu system architecture

Driver model – example: pci_driver● Register with supported device table

includes/linux/mod_devicetable.h:

struct pci_device_id { __u32 vendor, device; __u32 subvendor, subdevice; __u32 class, class_mask; …};

Page 29: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

29

Ubuntu system architecture

Driver model – example: pci_driver●probe() will be called when matched

includes/linux/pci.h:

struct pci_driver { … const struct pci_device_id *id_table; int (*probe) (struct pci_dev *dev, const struct pci_device_id *id); …};

Page 30: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

30

Ubuntu system architecture

Early userspace● We do not want a huge kernel including

everything to support booting from various hardwares / setup

Page 31: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

31

Ubuntu system architecture

Early userspace - initrd● Devide system startup into two phase● initial RAM disk (initrd)

Page 32: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

32

Ubuntu system architecture

Early userspace - initrd● Kernel mounts initrd as rootfs

Page 33: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

33

Ubuntu system architecture

Early userspace - initrd● Execute /init

Page 34: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

34

Ubuntu system architecture

Early userspace – init in initrd● Load essential kernel modules

Page 35: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

35

Ubuntu system architecture

Early userspace – init in initrd● Parse some boot / rootfs related boot

parameters

Page 36: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

36

Ubuntu system architecture

Early userspace – init in initrd● Convert UUID and LABEL into device name

Page 37: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

37

Ubuntu system architecture

Early userspace – init in initrd● Start plymouth

● Hide scary boot messages from user

Page 38: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

38

Ubuntu system architecture

Early userspace – init in initrd● Start udev

Page 39: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

39

Ubuntu system architecture

Early userspace – init in initrd● Handle crypted rootfs / nfsroot / netboot

Page 40: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

40

Ubuntu system architecture

Early userspace – init in initrd● Mount rootfs

Page 41: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

41

Ubuntu system architecture

Early userspace – init in initrd● End udev

Page 42: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

42

Ubuntu system architecture

Early userspace – init in initrd● Chroot to rootfs and transfer to init in rootfs

Page 43: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

43

Ubuntu system architecture

Early userspace – build an initrd● Created with mkinitfs, based on things in

/usr/share/initramfs-tools

Page 44: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

44

Ubuntu system architecture

Early userspace – build an initrd● Settings: /etc/initramfs/tools/initramfs.conf● MODULES=[ most | netboot | dep | list ]

Page 45: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

45

Ubuntu system architecture

Real userspace● upstart is started by initrd

Page 46: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

46

Ubuntu system architecture

Real userspace● upstart starts/stops services according to

runlevel

Page 47: Ubuntu system architecture - Ubuntu Hardware Debuggingodm.ubuntu.com/uhs/2011/Ubuntu System Architecture.pdf · Ubuntu system architecture Early init - kernel ... Functions to be

47

Thank you

Jesse Sung

[email protected]

www.canonical.com