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** ⚠️