From bf7bd4abcdd1e9ae1030c7a9dfae70c35791d771 Mon Sep 17 00:00:00 2001 From: Holger Dengler Date: Fri, 11 Jan 2019 08:35:40 +0100 Subject: linux-basic: Add Userspace-to-Kernel API slides Signed-off-by: Holger Dengler --- linux-basics/api/Kconfig | 5 ++ linux-basics/api/Makefile | 1 + linux-basics/api/frm_devicenodes.tex | 91 +++++++++++++++++++ linux-basics/api/frm_proc.tex | 165 +++++++++++++++++++++++++++++++++++ linux-basics/api/frm_sysfs.tex | 41 +++++++++ linux-basics/api/pres_kernel-api.tex | 52 +++++++++++ 6 files changed, 355 insertions(+) create mode 100644 linux-basics/api/Kconfig create mode 100644 linux-basics/api/Makefile create mode 100644 linux-basics/api/frm_devicenodes.tex create mode 100644 linux-basics/api/frm_proc.tex create mode 100644 linux-basics/api/frm_sysfs.tex create mode 100644 linux-basics/api/pres_kernel-api.tex (limited to 'linux-basics/api') diff --git a/linux-basics/api/Kconfig b/linux-basics/api/Kconfig new file mode 100644 index 0000000..db75e5b --- /dev/null +++ b/linux-basics/api/Kconfig @@ -0,0 +1,5 @@ +config KERNEL_API + bool "Linux Kernel API" + default y + help + Paper about Userspace-to-Kernel API diff --git a/linux-basics/api/Makefile b/linux-basics/api/Makefile new file mode 100644 index 0000000..3b553d6 --- /dev/null +++ b/linux-basics/api/Makefile @@ -0,0 +1 @@ +obj-$(CONFIG_KERNEL_API) += pres_kernel-api.pdf diff --git a/linux-basics/api/frm_devicenodes.tex b/linux-basics/api/frm_devicenodes.tex new file mode 100644 index 0000000..ebeae48 --- /dev/null +++ b/linux-basics/api/frm_devicenodes.tex @@ -0,0 +1,91 @@ +\subsubsection{Device Nodes} +% ---------------------------- +\begin{frame}[fragile] +\frametitle{Basics} +\begin{itemize} +\item Reference to Device Drivers and its Instances +\item Re-use file-syscalls to extend the System API +\item File-syscall redirection to Device Driver +\end{itemize} +\begin{beamerboxesrounded}[shadow=true]{Access to HW by Userspace applications} +\begin{scriptsize} +\begin{verbatim} +/* open file (permissions will be checked)*/ +fd = open("/dev/ttyS0, flags); + +/* read data from HW */ +read(fd, buf, buf_len); + +/* write data to HW */ +write(fd, buf, buf_len); + +/* close file after usage +close(fd); +\end{verbatim} +\end{scriptsize} +\end{beamerboxesrounded} +\end{frame} +% Device nodes are the main interfaces to device drivers. The device nodes +% are special files, which appears in a filesytem. Userspace application can +% interact with with the hardware via these device nodes. Instead of +% creating additional syscalls for accessing the hardware, the device node +% concept re-uses the file systemcalls read(), write(), and ioctl(). + +% ---------------------------- +\begin{frame}[fragile] +\frametitle{Special File} +\begin{itemize} +\item Special Files (inodes) +\item File Type: Block or Character +\item Major Number + \begin{itemize} + \item Reference to Device Driver + \item Handled by common Kernel code + \end{itemize} +\item Minor Number + \begin{itemize} + \item Reference to Device Instance + \item Handled by Device Driver + \end{itemize} +\item Permissions manage access to HW +\end{itemize} +\end{frame} +% There are two types of devide nodes, block and character devices. Each +% device node has a major and minor number. The type and major number is +% used to find the right driver in the right subsystem for the hardware +% access. The minor number is used by the driver for handling multiple +% instances. This allows to handle multiple instances of the same controller +% type with one single device driver implementation. + +% ---------------------------- +\begin{frame}[fragile] +\frametitle{Special File: Diagram} +\begin{figure}[h] +\centering +\includegraphics[width=8cm]{images/devicenode.png} +\end{figure} +\end{frame} + +% ---------------------------- +\begin{frame}[fragile] +\frametitle{Manage Special Files} +\begin{itemize} +\item manually: \texttt{mknod /dev/ttyS0 c 4 65} +\item hotplug: userspace /dev (udev) +\item kernel: \texttt{mount -t devtmpfs none /dev} +\end{itemize} +\end{frame} +% The device nodes can be created manually with the command "mknod", or by +% the hotplug mechanism userspace /dev (udev). The kernel also provides a +% pseudo fs "devtmpfs" with device nodes for all existing deivces. This is +% very useful for small devices, where it's too complex to setup udev and +% its rules. + +% ---------------------------- +\subsubsection*{} +\begin{frame} +\frametitle{Resources} +\begin{itemize} +\item \href{http://man7.org/linux/man-pages/man7/udev.7.html}{man 7 udev} +\end{itemize} +\end{frame} diff --git a/linux-basics/api/frm_proc.tex b/linux-basics/api/frm_proc.tex new file mode 100644 index 0000000..0f51d7a --- /dev/null +++ b/linux-basics/api/frm_proc.tex @@ -0,0 +1,165 @@ +\subsubsection{Pseudofs proc} +% ---------------------------- +\begin{frame} +\frametitle{Basics} +\begin{itemize} +\item most important pseudofs +\item mount-point: /proc +\item early mount required +\item stable Kernel API extension + \begin{itemize} + \item process/thread information + \item kernel extension + \begin{itemize} + \item modules + \item drivers + \end{itemize} + \item system state/utilization (e.g. loadavg, interrupts) + \begin{itemize} + \item loadavg + \item interrupts + \end{itemize} + \item system behavior (e.g. memory over-commit) + \begin{itemize} + \item memory over-commit + \item network + \item irq affinity + \end{itemize} + \end{itemize} +\end{itemize} +\end{frame} +% The pseudo filesystem proc is an important API-extension of the linux +% kernel. It provides access to informations about the processes and +% threads, as well as other kenel-internal information, extensions and +% behaviour. The proc filesystem must be mounted as early as possible, right +% after userspace gets control from kernel. + +% ---------------------------- +\begin{frame} +\frametitle{Process information} +\begin{itemize} +\item one directory for each process +\item PID as directory name +\item /proc/self: link to process' own information +\item per process: + \begin{itemize} + \item maps: memory mappings + \item status/stat: process status (human/machine readable) + \item fd/fdinfo: (open) file descriptors + \item map\_files: mmap-ed files + \item mounts: filesystem mounts + \item task/: information about each thread + \end{itemize} +\item tools accessing process information in proc + \begin{itemize} + \item ps + \item top + \end{itemize} +\item prevent tool output parsing +\item direct access to process information is preferred +\end{itemize} +\end{frame} +% The informations about processes are provides in separate subdirectories, +% one for each process. The directory name is the PID number of the process. +% The link /proc/self can be used by processes to access its own process +% information. +% The file "maps" shows the memory mappings of a process. It lists all +% mappings, text segment, dynamic libraries, heap and stack. +% The file "status" contains informations about the process status in a +% human-readable form. The same information, but in machine readable form, +% is in file "stat". +% The directory "fd" and fd_info" contains information about the open file +% handles of the process. +% The directory "tasks" contain infomration about the threads of a process. + +% ---------------------------- +\begin{frame} +\frametitle{Kernel extensions} +\begin{itemize} +\item system-wide information +\item all files shows the current state of loaded extensions +\item extensions: + \begin{itemize} + \item modules: \\ a list of loaded modules + \item devices: \\ major-number-to-driver mapping + \item filesystems: \\ all currently supported filesystems + \end{itemize} +\end{itemize} +\end{frame} +% The kernel code can be extended by loadable modules. Ther are files in +% /proc, which informs about the currently loaded extensions. Each time an +% extension is added or removed, the content of these files may change. +% The file "modules" shows all modules, which are currently loaded. It also +% contains the start address of the text segment in memory, reference count +% and used-by information. The tool lsmod make use of this information. +% The file "devices" shows the mappings between the major numbers and the +% related device driver, separate for character and block devices. +% The file "filesystems" show all supported filesystems at the moment. + +% ---------------------------- +\begin{frame} +\frametitle{System State/Utilization} +\begin{itemize} +\item system-wide information +\item system state + \begin{itemize} + \item uptime: time since system start + \item config.gz: kernel configuration + \end{itemize} +\item system utilization + \begin{itemize} + \item loadavg: average system load (with some history) + \item interrupts: histogram of all interrupts (total and per CPU) + \item meminfo: memory information + \item buddyinfo: information about contiguous free pages + \end{itemize} +\end{itemize} +\end{frame} +% The system state and utilization is also available via filehandles in +% /proc. +% The file "loadavg" shows how many processes are schedulable at the same +% time and the history of this value. +% The file "interrupts" contains the histogram for all interrupts (total and +% per CPU). +% The file "meminfo" contains information about the system memory, the file +% "buddyinfo" contains a list of contiguous free pages in memory zones. +% The file "config.gz" contains the configuration, with which the kernel has +% been build. The file is compressed an can be extracted e.g. by zcat or +% zless. + +% ---------------------------- +\begin{frame} +\frametitle{System Behavior} +\begin{itemize} +\item System behavior: sub-directory /proc/sys + \begin{itemize} + \item use tool \textbf{sysctl} to change the values + \item settings at system start: \textbf{/etc/sysctl.conf} or \textbf{/etc/sysctl.d} + \end{itemize} +\item Interrupt routing: sub-directory /proc/irq + \begin{itemize} + \item system-wide affinity mask + \item per IRQ affinity mask + \item IRQs are only routed to the CPUs, which are set in the affinity mask bitfield + \end{itemize} +\end{itemize} +\end{frame} +% The subdirectory "sys" contains a large number of file handles, which can +% be used for fintune the system behaviour. The filehandles can be accessed +% directly, but it is highly recommended to use the tool "sysctl" instead. +% Most distributions supports settings at system start via /etc/sysctl.conf +% or /etc/sysctl.d. +% The subdirectory "irq" can be used, to configure the iterrupt routing to +% CPUs. + +% ---------------------------- +\subsubsection*{} +\begin{frame} +\frametitle{Resources} +\begin{itemize} +\item \href{https://www.kernel.org/doc/Documentation/filesystems/proc.txt}{Kernel: Documentation/filesystems/proc.txt} +\item \href{http://man7.org/linux/man-pages/man5/proc.5.html}{man 5 proc} +\item \href{http://man7.org/linux/man-pages/man8/sysctl.8.html}{man 8 sysctl} +\item \href{http://man7.org/linux/man-pages/man5/sysctl.conf.5.html}{man 5 sysctl.conf} +\end{itemize} +\end{frame} diff --git a/linux-basics/api/frm_sysfs.tex b/linux-basics/api/frm_sysfs.tex new file mode 100644 index 0000000..c3aefb7 --- /dev/null +++ b/linux-basics/api/frm_sysfs.tex @@ -0,0 +1,41 @@ +\subsubsection{Pseudofs sysfs} +% ---------------------------- +\begin{frame} +\frametitle{Basics} +\begin{itemize} +\item externalize kernel objects +\item mount-point: /sys +\item early mount required +\item Kernel devices + \begin{itemize} + \item /sys/devices: all devices + \item /sys/block: only block devices + \item /sys/bus: devices, sorted by bus + \item /sys/class: devices, sorted by class + \end{itemize} +\item Kernel modules + \begin{itemize} + \item /sys/module: all modules + \item module parameters + \item module properties + \end{itemize} +\item Tooling support: + \begin{itemize} + \item bus tools: lspci, lsusb, etc + \item others: lsuio + \end{itemize} +\end{itemize} +\end{frame} +% The pseudo filesystem sys provices access to all kernel objects like +% devices, drivers and modules. The subdirectories sorts a subset of kernel +% objects accoding to different criteria, like bus, class, and so on. + +% ---------------------------- +\subsubsection*{} +\begin{frame} +\frametitle{Resources} +\begin{itemize} +\item \href{https://www.kernel.org/doc/Documentation/filesystems/sysfs.txt}{Kernel: Documentation/filesystems/sysfs.txt} +\item \href{http://man7.org/linux/man-pages/man5/sysfs.5.html}{man 5 sysfs} +\end{itemize} +\end{frame} diff --git a/linux-basics/api/pres_kernel-api.tex b/linux-basics/api/pres_kernel-api.tex new file mode 100644 index 0000000..bda7055 --- /dev/null +++ b/linux-basics/api/pres_kernel-api.tex @@ -0,0 +1,52 @@ +\input{configpres} + +\subsection{Linux Kernel-API} +\title{Linux Kernel-API} + +% ---------------------------- +\maketitle + +% ---------------------------- +\begin{frame} +\frametitle{Overview} +\tableofcontents +\end{frame} + +% ---------------------------- +\begin{frame} +\frametitle{Basics} +\begin{itemize} +\item Application: Userspace, unprivileged +\item Driver: Kernel, privileged +\item Userspace-to-Kernel API + \begin{itemize} + \item Syscalls + \item Device Nodes + \item Pseudo Filesystems + \item Netlink Sockets + \end{itemize} +\end{itemize} +\end{frame} +% Most CPUs has two operation modes, an privileged and an unprivileged. +% Linux supports this HW feature by separating the Userspace (Applications, +% Libraries) from the Kernel (scheduling, memory-management, drivers). The +% Linux Userspace API is the interfacebetween the Userspace and the Kernel. +% The main interface are the syscalls. The file systemcalls and the devide +% nodes allows specific HW access, without creating new syscalls. The pseudo +% filesystems also extends the Userspace API, by externalize structured +% information about the system to applications. The netlink sockets are +% mainly for managing and controling Kernel subsystems, like network. + +\subsection{Devices} +% ---------------------------- +\input{linux-basics/api/frm_devicenodes.tex} + +\subsection{Pseudo FS} +% ---------------------------- +\input{linux-basics/api/frm_proc.tex} + +% ---------------------------- +\input{linux-basics/api/frm_sysfs.tex} + +\subsection*{} +\input{tailpres} -- cgit v1.2.3