Linux Kernel - modrpc/info GitHub Wiki

Table of Contents

Overview

Linux Kernel

Intuition

  • ๋ฌธ์ œ:
    • ๊ธฐ๊ณ„๊ฐ€ ํ•˜๋‚˜ ์ฃผ์–ด์ ธ์žˆ๋‹ค. ํ”„๋กœ์„ธ์„œ, ๋ฉ”๋ชจ๋ฆฌ, ๋””์Šคํฌ ์‹œ์Šคํ…œ์ด ์žˆ๋‹ค.
    • ์ฆ‰, ์ด ๊ธฐ๊ณ„๋ฅผ ์“ฐ๋Š” ์œ ์ผํ•œ ๋ฐฉ๋ฒ•์€ ํ”„๋กœ๊ทธ๋žจ์„ ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ฆฌ๊ณ  IP๋ฅผ ์ง€์ •ํ•ด์ฃผ๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ํ”„๋กœ์„ธ์Šค์˜ ์ปจํŠธ๋กค์„ ์œ„ํ•ด์„œ๋Š” Bios firmware๋ฅผ ํ†ตํ•ด time-0๋ถ€ํ„ฐ ์žฅ์•…์„ํ•ด์•ผ ํ•œ๋‹ค. -- ์•„์ฃผ ๊ธธ๋“ค์ด๊ธฐ ํž˜๋“  ๋ง์„ ๊ธธ๋“ค์ธ๋‹ค๊ณ  ์ƒ๊ฐํ•˜์ž.
  • Multiprogramming:
    • ํ”„๋กœ๊ทธ๋žจ๋“ค ์—ฌ๋Ÿฟ์ด ํ”„๋กœ์„ธ์„œ ํ•˜๋‚˜๋ฅผ ๊ณต์œ ํ•˜๊ฒŒ ํ•˜๊ณ  ์‹ถ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด bookkeeping์ด ํ•„์š”ํ•˜๋‹ค. context switch๋ฅผ ์œ„ํ•ด์„œ ์ƒํƒœ๋ฅผ load/saveํ•ด์•ผ ํ•œ๋‹ค. Load/save๋Š” ์–ด๋””์— ํ•˜๋‚˜? ๋‹น์—ฐํžˆ memory์—. ๋˜ ํ˜„์žฌ ์‚ด์•„์žˆ๋Š” process๋“ค์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ process table์— ์ €์žฅ -- ์—ญ์‹œ ๋ฉ”๋ชจ๋ฆฌ์—.
    • ์ด์ƒ์˜ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ์€ meta-level์—์„œ OS๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ. ๊ทธ๋Ÿฌ๋‚˜ ๊ฐœ๋ณ„ ํ”„๋กœ์„ธ์Šค๋“ค๋„ ์ž์ฒด์ ์œผ๋กœ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ• ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด, ํŠน๋ณ„ํ•œ function (malloc/free)๋ฅผ ๋งŒ๋“ ๋‹ค.
  • Protection/Service:
    • ์‚ฌ์‹ค ํ”„๋กœ์„ธ์„œ ์ž…์žฅ์—์„œ๋Š” kernel์ด๋‚˜ user-space program์ด๋‚˜ ์ฐจ์ด๊ฐ€ ์—†์—ˆ๋‹ค. ์š”์ฆ˜์€ protection์„ ์œ„ํ•œ mechanism์„ ํ”„๋กœ์„ธ์„œ์—์„œ ์ œ๊ณต์„ํ•˜์ง€๋งŒ (e.g. flag bit).
    • ๋งŒ์•ฝ user-program์ด low-level memory (OS-maintained space)์— ์ง์ ‘ ๊ฐˆ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค๋ฉด ์˜จ๊ฐ–๋ฌธ์ œ๊ฐ€ ์ƒ๊ธด๋‹ค. Bad hacker๋Š” ๋งํ• ๊ฒƒ๋„ ์—†๊ณ , ์„ ๋Ÿ‰ํ•œ ์‚ฌ์šฉ์ž๋„ system์„ ์–ผ๋งˆ๋“ ์ง€ ๋ง๊ฐ€๋œจ๋ฆด์ˆ˜ ์žˆ๋‹ค.
    • ๋”ฐ๋ผ์„œ "Service" -- Don't do it yourself -- I'll do it for you.
  • Devices:
    • ๋…์ž์ ์œผ๋กœ ๋™์ž‘ํ•˜๋Š” ์™ธ๋ถ€ peripheral๋“ค์€ ์–ด๋–ป๊ฒŒ ์ œ์–ดํ• ๊ฒƒ์ธ๊ฐ€?
    • ํ‘œ์ค€ํ™”๋œ access(R/W): File + File operations

Overview

Creating VirtualBox env

$ sudo usermod -a -G sudo cjeong 
$ <change to bridged networking>
$ sudo vi /etc/network/interfaces
  auto eth1
  iface eth1 inet dhcp
$ sudo service networking restart
$ <REBOOT if failed to access network>

Kernel Building and Installation

Linux Device Drivers

Basics

How to check devices

  • ls /dev
    • do 'ls -l': "c" are char devices, "b" are block devices
  • cat /proc/devices: shows major device numbers

Scull driver

drivers

  • scull0-3: global/persistent memory area
  • scullpipe0-3: FIFO devices
  • scullsingle, scullpriv, sculluid, scullwid:

Signals

Basics

  • signal (aka software interrupts): notification to a process that an event has occurred
    • it is an interrupt in the sense, it allows to add reactive behavior through signal handler
  • signal generated by:
    • user process to be sent to another
    • user process to be sent to itself
    • the kernel for a user process
      • hardware exception: e.g. devide-by-zero, segfault
      • Ctrl-C, Ctrl-Z
      • software event: e.g. input became available on a file descriptor
  • classes of signals:
    • traditional/standard signals (numbered 1 ~ 31): used by the kernel to notify processes of events (numbered 1 to 31)
    • realtime signals

Details

  • Signal is generated by some event
  • Once generated, signal is later delivered to a process, which then takes some action in response to the signal.
  • Between the time it's generated and the time it's delivered, a signal is said to be pending
  • Normally, a pending signal is delivered to a process as soon is it next scheduled to run, or immediately if the process is already running

Signal masks

  • However, we need to ensure that a segment of code is not interrupted by the delivery of signal.
  • To do this, we can add a signl to the process's signal mask -- a set of signals whose delivery is currently blocked
    • blocked signals, when generated, remains pending until it's unblocked (i.e. removed from signal mask) later

Linux Signals

API

sigaction()/signal(): Change signal actions

  • two ways: signal() and sigaction()
    • sigaction can do more than signal and is more portable; more recommended
     #include <signal.h>

     int sigaction(int signum, const struct sigaction *act,
                   struct sigaction *oldact);

     struct sigaction {
       void     (*sa_handler)(int);
       void     (*sa_sigaction)(int, siginfo_t *, void *);
       sigset_t sa_mask;
       int      sa_flags;
       void     (*sa_restorer)(void);
     };
  • signal delivery:

kill(): Send signals

     #include <signal.h>
     int kill(pid_t pid, int sig);

Linux Application Level

Creating C Library

โš ๏ธ **GitHub.com Fallback** โš ๏ธ